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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
{$include default.tmpl}
function ReadTemplate(fname:PAnsiChar;var buf:PAnsiChar):integer;
var
f:THANDLE;
size:integer;
begin
if (fname=nil) or (fname^=#0) then
f:=INVALID_HANDLE_VALUE
else
f:=Reset(fname);
if f=THANDLE(INVALID_HANDLE_VALUE) then
result:=0
else
begin
size:=FileSize(f);
mGetMem(buf,size+1);
buf[size+1]:=#0;
BlockRead(f,buf^,size);
CloseHandle(f);
result:=size;
end;
end;
function StatOut(report,log,template:PAnsiChar):boolean;
const
bufsize = 16384;
var
fout:THANDLE;
tt,tf:array [0..15] of AnsiChar;
timebuf:array [0..17] of AnsiChar; // for current date / time
outbuf:PAnsiChar;
outpos:PAnsiChar;
procedure OutChar(var pc:PAnsiChar);
begin
outpos^:=pc^;
inc(pc);
inc(outpos);
if (outpos-outbuf)=bufsize then
begin
BlockWrite(fout,outbuf^,bufsize);
outpos:=outbuf;
end;
end;
procedure OutStr(pc:PAnsiChar);
begin
while pc^<>#0 do
OutChar(pc);
end;
procedure OutputBlock(var start:PAnsiChar;var Root:pCells;asortmode:integer);
const
blocksize = 8192;
var
i,max,cnt,len:integer;
items:cardinal;
Cell:pStatCell;
ls,ls1:array [0..511] of AnsiChar;
block:array [0..blocksize-1] of AnsiChar;
begin
len:=StrIndex(start,'%end%');
if len=0 then
len:=StrLen(start)
else
dec(len);
if len>6143 then
err('Template block too large');
Resort(Root,asortmode);
case asortmode of
stArtist,stAlbum,stPath: begin
Cell:=Root^.Cells[0];
max:=Cell^.Count;
if asortmode=stPath then OnlyPath(ls,Cell^.MFile); // speed optimization
for i:=0 to Root^.Count-1 do
begin
with Root^.Cells[i]^ do
begin
AltCount:=0;
if asortmode=stArtist then cnt:=lstrcmpia(Cell^.Artist,Artist)
else if asortmode=stAlbum then cnt:=lstrcmpia(Cell^.Album,Album)
else cnt:=lstrcmpia(ls,OnlyPath(ls1,MFile));
if cnt=0 then
inc(max,Count)
else
begin
Cell^.AltCount:=max;
Cell:=Root^.Cells[i];
if asortmode=stPath then OnlyPath(ls,Cell^.MFile); // speed optimization
max:=Count;
end;
end;
end;
Cell^.AltCount:=max;
Resort(Root,stAltCount);
if (asortmode=stAlbum) and (Root^.Cells[0]^.Album^=#0) then
begin
if Root^.Count>1 then
max:=Root^.Cells[1]^.AltCount
else
max:=0;
end
else
max:=Root^.Cells[0]^.AltCount;
end;
stCount: begin
max:=Root^.Cells[0]^.Count;
end;
stLength: begin
max:=Root^.Cells[0]^.Length;
end;
else
max:=1;
end;
items:=1;
if ReportItems>0 then
for i:=0 to Root^.Count-1 do
begin
with Root^.Cells[i]^ do
begin
if (asortmode=stAlbum) and (Album^=#0) then continue;
case asortmode of
stArtist,
stAlbum,
stPath : cnt:=AltCount;
stCount : cnt:=Count;
stLength: cnt:=Length;
else
cnt:=1;
end;
if cnt=0 then break;
move(start^,block,len);
block[len]:=#0;
StrReplace(block,'%date%' ,ShowTime(ls,LastTime));
StrReplace(block,'%length%' ,IntToTime(ls,Length));
StrReplace(block,'%artist%' ,Artist);
StrReplace(block,'%title%' ,Title);
StrReplace(block,'%album%' ,Album);
StrReplace(block,'%file%' ,MFile);
StrReplace(block,'%path%' ,OnlyPath(ls,MFile));
StrReplace(block,'%num%' ,IntToStr(ls,items));
StrReplace(block,'%currenttime%',timebuf);
StrReplace(block,'%totaltime%' ,tt);
StrReplace(block,'%totalfiles%' ,tf);
StrReplace(block,'%percent%' ,IntToStr(ls,round(cnt*100/max)));
StrReplace(block,'%count%' ,IntToStr(ls,cnt));
OutStr(block);
end;
if items=ReportItems then break;
inc(items);
end;
inc(start,len+5);
end;
var
TmplBuf:PAnsiChar;
ptr:PAnsiChar;
i,j,k:integer;
size:integer;
lsortmode:integer;
MyTime:TSYSTEMTIME;
Root:pCells;
b1,tmp:PAnsiChar;
begin
result:=false;
GetLocalTime(MyTime);
ShowTime(timebuf,PackTime(MyTime));
Lock:=true;
Root:=BuildTree(log,b1);
if Root<>nil then
begin
Resort(Root,stArtist);
Lock:=false;
size:=ReadTemplate(template,TmplBuf);
if size=0 then
begin
StrDup(TmplBuf,IntTmpl);
size:=StrLen(IntTmpl);
end;
ptr:=TmplBuf;
fout:=Rewrite(report);
if fout=THANDLE(INVALID_HANDLE_VALUE) then
exit;
mGetMem(outbuf,bufsize);
outpos:=outbuf;
i:=0;
k:=0;
for j:=0 to Root^.Count-1 do
begin
inc(k);
with Root^.Cells[j]^ do
inc(i,Length*Count);
end;
IntToTime(tt,i); // total time
IntToStr(tf,k); // total files
lsortmode:=stDate;
while (ptr-TmplBuf)<size do
begin
while (ptr^<>'%') and (ptr^<>#0) do
OutChar(ptr);
if ptr^=#0 then break;
if StrCmp(ptr,'%block_',7)=0 then
begin
if ptr>@TmplBuf then
begin
if (ptr-1)^<' ' then
k:=-1;
end;
inc(ptr,7);
if StrCmp(ptr,'end%',4)=0 then
begin
i:=4;
end
else if StrCmp(ptr,'freqartist%',11)=0 then
begin
lsortmode:=stArtist;
i:=11;
end
else if StrCmp(ptr,'freqsongs%',10)=0 then
begin
lsortmode:=stCount;
i:=10;
end
else if StrCmp(ptr,'freqalbum%',10)=0 then
begin
lsortmode:=stAlbum;
i:=10;
end
else if StrCmp(ptr,'lastsongs%',10)=0 then
begin
lsortmode:=stDate;
i:=10;
end
else if StrCmp(ptr,'songtime%',9)=0 then
begin
lsortmode:=stLength;
i:=9;
end
else if StrCmp(ptr,'freqpath%',9)=0 then
begin
lsortmode:=stPath;
i:=9;
end
else
begin
OutChar(ptr);
continue;
end;
inc(ptr,i);
if k<0 then
begin
while (ptr^<' ') and (ptr^<>#0) do inc(ptr);
k:=0;
end;
if (ReportMask and lsortmode)=0 then
begin
tmp:=StrPos(ptr,'%block_end%');
if tmp<>nil then
ptr:=tmp+11
else
break;
end;
end
else if StrCmp(ptr,'%start%',7)=0 then
begin
if ptr>@TmplBuf then
begin
if (ptr-1)^<' ' then
k:=-1;
end;
inc(ptr,7);
if k<0 then
begin
while (ptr^<' ') and (ptr^<>#0) do inc(ptr);
k:=0;
end;
OutputBlock(ptr,Root,lsortmode);
end
else if StrCmp(ptr,'%currenttime%',13)=0 then
begin
inc(ptr,13);
OutStr(timebuf);
end
else if StrCmp(ptr,'%totalfiles%',12)=0 then
begin
inc(ptr,12);
OutStr(tf);
end
else if StrCmp(ptr,'%totaltime%',11)=0 then
begin
inc(ptr,11);
OutStr(tt);
end
else
OutChar(ptr);
end;
BlockWrite(fout,outbuf^,outpos-outbuf);
CloseHandle(fout);
mFreeMem(outbuf);
mFreeMem(TmplBuf);
ClearStatCells(Root);
result:=true;
end;
mFreeMem(b1);
end;
|