Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[r190]: uCommonUtils.pas Maximize Restore History

Download this file

uCommonUtils.pas    320 lines (287 with data), 8.7 kB

  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
(*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Initial Developer of this code is John Hansen.
* Portions created by John Hansen are Copyright (C) 2009 John Hansen.
* All Rights Reserved.
*
*)
unit uCommonUtils;
interface
uses
Classes;
const
DEFAULT_CHARSET = 1;
SW_SHOWMINNOACTIVE = 7;
HKEY_CLASSES_ROOT = LongWord($80000000);
HKEY_CURRENT_USER = LongWord($80000001);
HKEY_LOCAL_MACHINE = LongWord($80000002);
type
HWND = type LongWord;
type
TWaveFormatEx = packed record
wFormatTag: Word; { format type }
nChannels: Word; { number of channels (i.e. mono, stereo, etc.) }
nSamplesPerSec: Cardinal; { sample rate }
nAvgBytesPerSec: Cardinal; { for buffer estimation }
nBlockAlign: Word; { block size of data }
wBitsPerSample: Word; { number of bits per sample of mono data }
cbSize: Word; { the count in bytes of the size of }
end;
type
_mthd = record
id : array[0..3] of Char;
len : Cardinal;
fmt : Word;
track : Word;
div_ : Word;
end;
MTHD = _mthd;
type
_mtrk = record
id : array[0..3] of Char;
len : Cardinal;
end;
MTRK = _mtrk;
procedure WriteSmallIntToStream(aStream : TStream; value : SmallInt; bLittleEndian : Boolean = True);
procedure ReadSmallIntFromStream(aStream : TStream; var value : SmallInt; bLittleEndian : Boolean = True);
procedure WriteWordToStream(aStream : TStream; value : Word; bLittleEndian : Boolean = True);
procedure ReadWordFromStream(aStream : TStream; var value : Word; bLittleEndian : Boolean = True);
procedure WriteCardinalToStream(aStream : TStream; value : Cardinal; bLittleEndian : Boolean = True);
procedure ReadCardinalFromStream(aStream : TStream; var value : Cardinal; bLittleEndian : Boolean = True);
procedure WriteWaveFormatToStream(aStream : TStream; fmt : TWaveFormatEx);
function ReadMIDIMTHDFromStream(aStream : TStream; var head : MTHD) : boolean;
function ReadMIDIMTRKFromStream(aStream : TStream; var head : MTRK) : boolean;
function HiWord(L: Cardinal): Word;
function HiByte(W: Word): Byte;
function GetByte(val : Cardinal; idx : integer) : Byte;
function BytesToCardinal(b1 : byte; b2 : byte = 0; b3 : byte = 0; b4 : Byte = 0) : Cardinal; {overload;}
//function BytesToCardinal(b : array of byte) : Cardinal; overload;
procedure GetFileList(const Directory : string; const Pattern : string; List : TStringlist);
procedure GetSubDirectories(const Directory : string; List : TStringlist);
procedure OSSleep(const ms : Cardinal);
procedure PostWindowMessage(aHwnd : HWND; aMsg : Cardinal; wParam, lParam : Integer);
function MulDiv(const x, num, den : integer) : integer;
implementation
uses
{$IFNDEF FPC}
Windows,
{$ENDIF}
SysUtils;
procedure WriteWordToStream(aStream : TStream; value : Word; bLittleEndian : Boolean);
var
B1, B2 : byte;
begin
if bLittleEndian then
begin
B1 := Lo(value);
B2 := Hi(value);
end
else
begin
B1 := Hi(value);
B2 := Lo(value);
end;
aStream.Write(B1, 1);
aStream.Write(B2, 1);
end;
procedure WriteSmallIntToStream(aStream : TStream; value : SmallInt; bLittleEndian : Boolean);
begin
WriteWordToStream(aStream, Word(value), bLittleEndian);
end;
procedure ReadWordFromStream(aStream : TStream; var value : Word; bLittleEndian : Boolean);
var
B1, B2 : byte;
begin
B1 := 0;
B2 := 0;
aStream.Read(B1, 1);
aStream.Read(B2, 1);
if bLittleEndian then
begin
value := Word(Word(B1) + (Word(B2) shl 8));
end
else
begin
value := Word(Word(B2) + (Word(B1) shl 8));
end;
end;
procedure ReadSmallIntFromStream(aStream : TStream; var value : SmallInt; bLittleEndian : Boolean);
var
w : word;
begin
w := 0;
ReadWordFromStream(aStream, w, bLittleEndian);
value := SmallInt(w);
end;
procedure WriteCardinalToStream(aStream : TStream; value : Cardinal; bLittleEndian : Boolean);
var
b1, b2, b3, b4 : byte;
begin
if bLittleEndian then
begin
b1 := GetByte(value, 0);
b2 := GetByte(value, 1);
b3 := GetByte(value, 2);
b4 := GetByte(value, 3);
end
else
begin
b1 := GetByte(value, 3);
b2 := GetByte(value, 2);
b3 := GetByte(value, 1);
b4 := GetByte(value, 0);
end;
aStream.Write(b1, 1);
aStream.Write(b2, 1);
aStream.Write(b3, 1);
aStream.Write(b4, 1);
end;
procedure ReadCardinalFromStream(aStream : TStream; var value : Cardinal; bLittleEndian : Boolean);
var
b1, b2, b3, b4 : byte;
begin
b1 := 0; b2 := 0; b3 := 0; b4 := 0;
aStream.Read(b1, 1);
aStream.Read(b2, 1);
aStream.Read(b3, 1);
aStream.Read(b4, 1);
if bLittleEndian then
begin
value := BytesToCardinal(b1, b2, b3, b4);
end
else
begin
value := BytesToCardinal(b4, b3, b2, b1);
end;
end;
procedure WriteWaveFormatToStream(aStream : TStream; fmt : TWaveFormatEx);
begin
WriteWordToStream(aStream, fmt.wFormatTag);
WriteWordToStream(aStream, fmt.nChannels);
WriteCardinalToStream(aStream, fmt.nSamplesPerSec);
WriteCardinalToStream(aStream, fmt.nAvgBytesPerSec);
WriteWordToStream(aStream, fmt.nBlockAlign);
WriteWordToStream(aStream, fmt.wBitsPerSample);
WriteWordToStream(aStream, fmt.cbSize);
end;
function ReadMIDIMTHDFromStream(aStream : TStream; var head : MTHD) : boolean;
begin
try
aStream.Read(head.id, 4);
ReadCardinalFromStream(aStream, head.len, False);
ReadWordFromStream(aStream, head.fmt, False);
ReadWordFromStream(aStream, head.track, False);
ReadWordFromStream(aStream, head.div_, False);
Result := True;
except
Result := False;
end;
end;
function ReadMIDIMTRKFromStream(aStream : TStream; var head : MTRK) : boolean;
begin
try
aStream.Read(head.id, 4);
ReadCardinalFromStream(aStream, head.len, False);
Result := True;
except
Result := False;
end;
end;
function HiWord(L: Cardinal): Word;
begin
Result := Word(L shr 16);
end;
function HiByte(W: Word): Byte;
begin
Result := Byte(W shr 8);
end;
function GetByte(val : Cardinal; idx : integer) : Byte;
begin
case idx of
0 : Result := Lo(Word(val));
1 : Result := Hi(Word(val));
2 : Result := Lo(HiWord(val));
3 : Result := Hi(HiWord(val));
else
Result := 0;
end;
end;
procedure OSSleep(const ms : Cardinal);
begin
{$IFDEF FPC}
// not sure what to do here yet
{$ELSE}
Windows.Sleep(ms);
{$ENDIF}
end;
procedure PostWindowMessage(aHwnd : HWND; aMsg : Cardinal; wParam, lParam : Integer);
begin
{$IFDEF FPC}
// ;
{$ELSE}
PostMessage(aHwnd, aMsg, wParam, lParam);
{$ENDIF}
end;
function MulDiv(const x, num, den : integer) : integer;
begin
Result := (x * num) div den;
end;
function BytesToCardinal(b1 : byte; b2 : byte = 0; b3 : byte = 0; b4 : Byte = 0) : Cardinal;
begin
Result := Cardinal(b1) + (Cardinal(b2) shl 8) + (Cardinal(b3) shl 16) + (Cardinal(b4) shl 24);
end;
{
function BytesToCardinal(b : array of byte) : Cardinal;
var
i : integer;
begin
Result := 0;
for i := Low(b) to High(b) do
Result := (Result shl 8) + b[i];
end;
}
procedure GetFileList(const Directory : string; const Pattern : string; List : TStringlist);
var
SearchRec : TSearchRec;
iRes : Integer;
begin
iRes := FindFirst(IncludeTrailingPathDelimiter(Directory) + Pattern, faAnyFile, SearchRec);
try
while iRes = 0 do
begin
if (SearchRec.Attr and faDirectory) <> faDirectory then
List.Add(SearchRec.Name);
iRes := FindNext(SearchRec);
end;
finally
FindClose(SearchRec);
end;
end;
procedure GetSubDirectories(const Directory : string; List : TStringlist);
var
SearchRec : TSearchRec;
iRes : Integer;
begin
iRes := FindFirst(IncludeTrailingPathDelimiter(Directory) + '*.*', faDirectory, SearchRec);
try
while iRes = 0 do
begin
if (SearchRec.Attr and faDirectory) = faDirectory then
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
List.Add(SearchRec.Name);
iRes := FindNext(SearchRec);
end;
finally
FindClose(SearchRec);
end;
end;
end.