aboutsummarylogtreecommitdiffstats
path: root/asyncqueue.pas
blob: 2da01ac238c3c9e4644dcd59dd2c1d0e913af7b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
{ Contains classes TAsyncWriter, TAsyncWriteExecutor, TFilesQueue for concurrent writing
html files using queue}
unit AsyncQueue;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils;


type

  { TAsyncWriter }
  { Writer of file with given filename and data }
  TAsyncWriter = class
  private
    { Name of file, absolute path }
    filename : String;
    { Text of html file }
    data : String;

  public
    { Creates a writer for text file aFilename with content aData }
    constructor Create(aFilename : String; aData : String );
    { Write document using TStringList }
    procedure writeDocument();
  end;


  { TAsyncWriteExecutor }
  { Writes in separate thread writer aw }
  TAsyncWriteExecutor = class(TThread)
    public
      { Contains writer }
      aw : TAsyncWriter;
      { Name of file with absolute path }
      filename : String;
      { Text of HTML or other file}
      data : String;
      { Creates thread, CreateSuspended should be true}
      constructor Create(CreateSuspended : boolean);
      { Setter for members filename, data }
      procedure setArgs(aFilename : String; aData : String);
  protected
    { Launch aw.writedocument }
    procedure Execute(); override;
  end;



   { TFilesQueue }
   { Queue which has a list of filename and data and process
   each elements using TAsyncWriteExecutor inside processEach }
   TFilesQueue = class

     public
           { Dynamic array of file names }
           filenames : Array of String;
           { Dynamic arrays of file data }
           files : Array Of String;
           { Index of last added file}
           last : integer;
           { Create queue }
           constructor Create();
           { add file data and file name to queue}
           procedure AddToJob(data : String; filename : String);
           { processing of each elements in queue }
           procedure processEach();


   end;







implementation

{ TFilesQueue }

constructor TFilesQueue.Create();
begin
  last := -1;
  SetLength(filenames, 9999);
  SetLength(files, 9999);
end;

procedure TFilesQueue.AddToJob(data: String; filename : String);
begin
   last:=last+1;
   filenames[last]:=filename;
   files[last] := data;
end;

procedure TFilesQueue.processEach();
var
   i : Integer;
   aw : Array Of TAsyncWriteExecutor;
begin
  setLength(aw, last+1);
  for i:=0 to last do begin
      aw[i] := TAsyncWriteExecutor.Create(True);
      aw[i].setArgs(filenames[i], files[i]);
  end;
  for i:=0 to last do
  begin
      aw[i].Start;

  end;
end;


constructor TAsyncWriteExecutor.Create(CreateSuspended : boolean);
begin
    inherited Create(CreateSuspended);
    FreeOnTerminate := True;

end;

procedure TAsyncWriteExecutor.setArgs(aFilename: String; aData: String);
begin
  filename := aFilename;
  data := aData;
end;


procedure TAsyncWriteExecutor.Execute;

begin
  aw := TAsyncWriter.Create(filename, data);
  aw.writedocument;
end;

constructor TAsyncWriter.Create( aFilename : String; aData : String);
begin
  filename := aFileName;
  data := aData;

end;


procedure TAsyncWriter.writedocument();
var

  buffer : TStringList;


begin
  buffer  := TStringList.Create;


    if FileExists(filename) then DeleteFile(filename);
    buffer.Text:=data;
    buffer.SaveToFile(filename);

         buffer.Free;



end;

end.