summaryrefslogtreecommitdiff
path: root/plugins/Watrack/srv_format.pas
blob: 58c046c7123dcde35033daffca182b2d4eba7587 (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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
{format service}
unit srv_format;

interface

uses windows,wat_api;

procedure DefFillFormatList (hwndList:HWND);
procedure DefCheckFormatList(hwndList:HWND);
function ProcessFormatLink:integer;

function GetFileFormatInfo(var dst:tSongInfo):integer;
function CheckExt(fname:pWideChar):integer;

function DeleteKnownExt(src:pWideChar):pWideChar;
function KnownFileType(fname:PWideChar):boolean;
function isContainer(fname:PWideChar):boolean;

function ServiceFormat(wParam:WPARAM;lParam:LPARAM):integer;cdecl;
procedure RegisterFormat(ext:PAnsiChar;proc:tReadFormatProc;flags:dword=0);
procedure ClearFormats;

type
  MusEnumProc = function(param:PAnsiChar;lParam:LPARAM):bool;stdcall;

function EnumFormats(param:MusEnumProc;lParam:LPARAM):bool;

type
  pwFormat = ^twFormat;
  twFormat = record
    This:tMusicFormat;
    Next:pwFormat;
  end;

const
  FormatLink:pwFormat=nil;
  
implementation

uses
  CommCtrl,common;
type
  pFmtArray = ^tFmtArray;
  tFmtArray = array [0..10] of tMusicFormat;

const
  StartSize = 32;
  Step      = 8;

const
  fmtLink:pFmtArray=nil;
  FmtNum:integer=0;
  FmtMax:integer=0;

function ProcessFormatLink:integer;
var
  ptr:pwFormat;
begin
  result:=0;
  ptr:=FormatLink;
  while ptr<>nil do
  begin
    RegisterFormat(@ptr.This.ext,ptr.This.proc,ptr.This.flags);
    inc(result);
    ptr:=ptr^.Next;
  end;
end;

function EnumFormats(param:MusEnumProc;lParam:LPARAM):bool;
var
  tmp:pFmtArray;
  i,j:integer;
  s:array [0..8] of AnsiChar;
begin
  if (FmtNum>0) and (@param<>nil) then
  begin
    GetMem(tmp,FmtNum*SizeOf(tMusicFormat));
    move(fmtLink^,tmp^,FmtNum*SizeOf(tMusicFormat));
    i:=0;
    j:=FmtNum;
    s[8]:=#0;
    repeat
      move(tmp^[i].ext,s,8);
      if not param(s,lParam) then break;
      inc(i);
    until i=j;
    FreeMem(tmp);
    result:=true;
  end
  else
    result:=false;
end;

function FindFormat(ext:PAnsiChar):integer;
var
  i:integer;
  ss:array [0..7] of AnsiChar;
begin
  i:=0;
  int64(ss):=0;
  StrCopy(ss,ext,7);
  while i<FmtNum do
  begin
    if int64(fmtLink^[i].ext)=int64(ss) then
    begin
      result:=i;
      exit;
    end;
    inc(i);
  end;
  result:=WAT_RES_NOTFOUND;
end;

procedure DefFillFormatList(hwndList:HWND);
var
  item:LV_ITEMA;
  lvc:LV_COLUMN;
  newItem:integer;
  i:integer;
  p:pMusicFormat;
begin
  FillChar(item,SizeOf(item),0);
  FillChar(lvc,SizeOf(lvc),0);
  ListView_SetExtendedListViewStyle(hwndList, LVS_EX_CHECKBOXES);
  lvc.mask:=LVCF_FMT;
  lvc.fmt :={LVCFMT_IMAGE or} LVCFMT_LEFT;
  ListView_InsertColumn(hwndList,0,lvc);

  item.mask:=LVIF_TEXT or LVIF_PARAM;
  item.iItem:=1000;
  i:=0;
  while i<FmtNum do
  begin
    p:=@fmtLink^[i];
    item.pszText:=@p^.ext;
    item.lParam := p^.flags;
    newItem:=SendMessageA(hwndList,LVM_INSERTITEMA,0,LPARAM(@item));
    if newItem>=0 then
    begin
      if (p^.flags and WAT_OPT_DISABLED)=0 then
        ListView_SetCheckState(hwndList,newItem,TRUE);
    end;
    inc(i);
  end;
  ListView_SetColumnWidth(hwndList,0,LVSCW_AUTOSIZE);
end;

procedure DefCheckFormatList(hwndList:HWND);
var
  i,j,k:integer;
  item:LV_ITEMA;
  szTemp:array [0..109] of AnsiChar;
  p:pMusicFormat;
begin
  FillChar(item,SizeOf(item),0);
  item.mask:=LVIF_TEXT;
  item.pszText:=@szTemp;
  item.cchTextMax:=100;
  k:=ListView_GetItemCount(hwndList)-1;
  for i:=0 to k do
  begin
    item.iItem:=i;
    SendMessageA(hwndList,LVM_GETITEMA,0,LPARAM(@item));
    j:=FindFormat(item.pszText);
    if j<>WAT_RES_NOTFOUND then // always?
    begin
      p:=@fmtLink^[j];
      if ListView_GetCheckState(hwndList,i)=0 then
        p^.flags:=p^.flags or WAT_OPT_DISABLED
      else
        p^.flags:=p^.flags and not WAT_OPT_DISABLED;
    end;
  end;
end;

function DeleteKnownExt(src:pWideChar):pWideChar;
var
  s :array [0..7] of WideChar;
  ss:array [0..7] of AnsiChar;
  i,j:integer;
begin
  GetExt(src,s);
  if s[0]<>#0 then
  begin
    int64(ss):=0;
    i:=0;
    while (s[i]<>#0) and (i<8) do
    begin
      ss[i]:=AnsiChar(s[i]);
      inc(i);
    end;
    j:=0;
    while j<FmtNum do
    begin
      if int64(fmtLink^[j].ext)=int64(ss) then
      begin
        i:=StrLenW(s);
        src[integer(StrLenW(src))-i-1]:=#0;
        break;
      end;
      inc(j);
    end;
  end;
  result:=src;
end;

function KnownFileType(fname:PWideChar):boolean;
var
  i:integer;
  s :array [0..7] of WideChar;
  ss:array [0..7] of AnsiChar;
begin
  result:=false;
  if (fname=nil) or (fname^=#0) then
    exit;
  GetExt(fname,s);
  int64(ss):=0;
  if s[0]<>#0 then
  begin
    i:=0;
    while (s[i]<>#0) and (i<8) do
    begin
      ss[i]:=AnsiChar(s[i]);
      inc(i);
    end;
    i:=0;
    while i<FmtNum do
    begin
      if (int64(fmtLink^[i].ext)=int64(ss)) then
      begin
        if ((fmtLink^[i].flags and WAT_OPT_DISABLED)=0) then
          result:=true;
        break;
      end;
      inc(i);
    end;
  end;
end;

function isContainer(fname:PWideChar):boolean;
begin
  if CheckExt(fname)=WAT_RES_OK then
  begin
    result:=(fmtLink^[0].flags and WAT_OPT_CONTAINER)<>0;
  end
  else
    result:=false;
end;

function GetFileFormatInfo(var dst:tSongInfo):integer;
begin
  result:=CheckExt(dst.mfile);
  if result=WAT_RES_OK then
  begin
    fmtLink^[0].proc(dst);
  end;
end;

function CheckExt(fname:pWideChar):integer;
var
  i:integer;
  tmp:tMusicFormat;
  ls:array [0..7] of WideChar;
  ss:array [0..7] of AnsiChar;
begin
  GetExt(fname,ls);
  i:=0;
  int64(ss):=0;
  while (ls[i]<>#0) and (i<8) do
  begin
    ss[i]:=AnsiChar(ls[i]);
    inc(i);
  end;
  i:=0;
  while i<FmtNum do
  begin
    if (int64(fmtLink^[i].ext)=int64(ss)) then
    begin
      if ((fmtLink^[i].flags and WAT_OPT_DISABLED)=0) then
      begin
        if i>0 then
        begin
          tmp:=fmtLink^[i];
          move(fmtLink^[0],fmtLink^[1],SizeOf(tMusicFormat)*i);
          fmtLink^[0]:=tmp;
        end;
        result:=WAT_RES_OK;
        exit;
      end
      else
        break;
    end;
    inc(i);
  end;
  result:=WAT_RES_NOTFOUND;
end;

function ServiceFormat(wParam:WPARAM;lParam:LPARAM):integer;cdecl;
var
  p:integer;
  nl:pFmtArray;
begin
  result:=WAT_RES_NOTFOUND;
  if LoWord(wParam)<>WAT_ACT_REGISTER then
    p:=FindFormat(PAnsiChar(lParam))
  else
    p:=0;
  case LoWord(wParam) of
    WAT_ACT_REGISTER: begin
      if @pMusicFormat(lParam)^.proc=nil then
        exit;
      p:=FindFormat(pMusicFormat(lParam)^.ext);
      if (p=WAT_RES_NOTFOUND) or ((wParam and WAT_ACT_REPLACE)<>0) then
      begin
        if (p<>WAT_RES_NOTFOUND) and ((fmtLink^[p].flags and WAT_OPT_ONLYONE)<>0) then
          exit;
        if FmtNum=FmtMax then // expand array when append
        begin
          if FmtMax=0 then
            FmtMax:=StartSize
          else
            inc(FmtMax,Step);
          GetMem(nl,FmtMax*SizeOf(tMusicFormat));
          if fmtLink<>nil then
          begin
            move(fmtLink^,nl^,FmtNum*SizeOf(tMusicFormat));
            FreeMem(fmtLink);
          end;
          fmtLink:=nl;
        end;
        if p=WAT_RES_NOTFOUND then
        begin
          p:=FmtNum;
          result:=WAT_RES_OK;
          inc(FmtNum);
        end
        else
          result:=int_ptr(@fmtLink^[p].proc);
        move(pMusicFormat(lParam)^,fmtLink^[p],SizeOf(tMusicFormat));// fill
      end;
    end;
    WAT_ACT_UNREGISTER: begin
      if p<>WAT_RES_NOTFOUND then
      begin
        dec(FmtNum);
        if p<FmtNum then // last
          Move(fmtLink^[p+1],fmtLink^[p],SizeOf(tMusicFormat)*(FmtNum-p));
        result:=WAT_RES_OK;
      end;
    end;
    WAT_ACT_DISABLE: begin
      if p<>WAT_RES_NOTFOUND then
      begin
        fmtLink^[p].flags:=fmtLink^[p].flags or WAT_OPT_DISABLED;
        result:=WAT_RES_DISABLED
      end;
    end;
    WAT_ACT_ENABLE: begin
      if p<>WAT_RES_NOTFOUND then
      begin
        fmtLink^[p].flags:=fmtLink^[p].flags and not WAT_OPT_DISABLED;
        result:=WAT_RES_ENABLED
      end;
    end;
    WAT_ACT_GETSTATUS: begin
      if p<>WAT_RES_NOTFOUND then
      begin
        if (fmtLink^[p].flags and WAT_OPT_DISABLED)<>0 then
          result:=WAT_RES_DISABLED
        else
          result:=WAT_RES_ENABLED;
      end;
    end;
  end;
end;

procedure RegisterFormat(ext:PAnsiChar;proc:tReadFormatProc;flags:dword=0);
var
  tmp:tMusicFormat;
begin
  FillChar(tmp,SizeOf(tMusicFormat),0);
  StrCopy (tmp.ext,ext,7);
  tmp.proc:=proc;
  tmp.flags:=flags;
  ServiceFormat(WAT_ACT_REGISTER,LPARAM(@tmp));
end;

procedure ClearFormats;
begin
  if FmtNum>0 then
    FreeMem(fmtLink);
end;

end.