source: oup/releases/0.32a/Unit2_functions.pas@ 1149

Last change on this file since 1149 was 35, checked in by alloc, 18 years ago
File size: 8.0 KB
Line 
1UNIT Unit2_functions;
2INTERFACE
3USES Classes, Dialogs, SysUtils, StrUtils, Math,
4 Unit3_data;
5
6TYPE
7 TExportSet=SET OF (DO_dat,DO_raw,DO_convert,DO_toone);
8
9FUNCTION BoolToStr(bool:Boolean):String;
10FUNCTION HexToLong(hex:String):LongWord;
11FUNCTION Decode_Int(buffer:Tdata):LongWord;
12FUNCTION Encode_Int(input:LongWord):Tdata;
13FUNCTION Decode_Float(buffer:Tdata):Single;
14FUNCTION Encode_Float(input:Single):Tdata;
15FUNCTION DataToBin(data:Tdata):String;
16FUNCTION BinToInt(bin:String):Byte;
17
18FUNCTION ExportFile(fileid:LongWord; filename:String; settings:TExportSet; path:String):Integer;
19
20FUNCTION StringSmaller(string1,string2:String):Boolean;
21
22FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
23FUNCTION FormatFileSize(size:LongWord):String;
24FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
25FUNCTION DecodeHexString(hex:String):Tdata;
26FUNCTION GetWinFileName(name:String):String;
27FUNCTION GetExtractPath:String;
28
29FUNCTION Explode(_string:String; delimiter:Char):TStringArray;
30
31
32IMPLEMENTATION
33USES Unit4_Exporters, Unit15_Classes;
34
35TYPE
36 TValueSwitcher=Record
37 CASE IsFloat: Boolean OF
38 True: (ValueFloat:Single);
39 False: (ValueInt:LongWord);
40 END;
41
42FUNCTION BoolToStr(bool:Boolean):String;
43 BEGIN
44 IF bool THEN
45 Result:='true'
46 ELSE
47 Result:='false';
48 END;
49
50FUNCTION HexToLong(hex:String):LongWord;
51 FUNCTION NormalizeHexString(VAR hex:String):Boolean;
52 VAR
53 i:Byte;
54 BEGIN
55 IF hex[1]='$' THEN BEGIN
56 FOR i:=1 TO Length(hex)-1 DO BEGIN
57 hex[i]:=hex[i+1];
58 END;
59 SetLength(hex, Length(hex)-1);
60 END;
61 IF (hex[1]='0') AND (UpCase(hex[2])='X') THEN BEGIN
62 FOR i:=1 TO Length(hex)-2 DO BEGIN
63 hex[i]:=hex[i+2];
64 END;
65 SetLength(hex, Length(hex)-2);
66 END;
67 IF Length(hex)=0 THEN
68 Result:=False
69 ELSE
70 Result:=True;
71 END;
72 VAR
73 i:Byte;
74 BEGIN
75 IF NormalizeHexString(hex) THEN BEGIN
76 hex:=UpperCase(hex);
77 Result:=0;
78 FOR i:=1 TO Length(hex) DO BEGIN
79 Result:=Result SHL 4;
80 CASE hex[i] OF
81 '0'..'9': Result:=Result+Ord(hex[i])-48;
82 'A'..'F': Result:=Result+Ord(hex[i])-55;
83 ELSE
84 Result:=0;
85 Exit;
86 END;
87 END;
88 END ELSE BEGIN
89 Result:=0;
90 END;
91 END;
92
93FUNCTION Decode_Int(buffer:Tdata):LongWord;
94 BEGIN
95 Result:=buffer[0]+buffer[1]*256+buffer[2]*256*256+buffer[3]*256*256*256;
96 END;
97FUNCTION Encode_Int(input:LongWord):Tdata;
98 BEGIN
99 SetLength(Result,4);
100 Result[0]:=input MOD 256;
101 input:=input DIV 256;
102 Result[1]:=input MOD 256;
103 input:=input DIV 256;
104 Result[2]:=input MOD 256;
105 input:=input DIV 256;
106 Result[3]:=input MOD 256;
107 END;
108FUNCTION Decode_Float(buffer:Tdata):Single;
109 VAR _valueswitcher:TValueSwitcher;
110 BEGIN
111 _valueswitcher.ValueInt:=Decode_Int(buffer);
112 Result:=_valueswitcher.ValueFloat;
113 IF IsNAN(Result) THEN Result:=0.0;
114 END;
115FUNCTION Encode_Float(input:Single):Tdata;
116 VAR _valueswitcher:TValueSwitcher;
117 BEGIN
118 _valueswitcher.ValueFloat:=input;
119 Result:=Encode_Int(_valueswitcher.ValueInt);
120 END;
121
122FUNCTION DataToBin(data:Tdata):String;
123 VAR
124 i,j:Byte;
125 singlebyte:Byte;
126 bytepart:String;
127 BEGIN
128 SetLength(bytepart,8);
129 Result:='';
130 FOR i:=0 TO High(data) DO BEGIN
131 singlebyte:=data[i];
132 FOR j:=7 DOWNTO 0 DO BEGIN
133 bytepart[j+1]:=Char((singlebyte AND $01)+48);
134 singlebyte:=singlebyte SHR 1;
135 END;
136 Result:=Result+bytepart+' ';
137 END;
138 END;
139FUNCTION BinToInt(bin:String):Byte;
140 VAR
141 Add: Integer;
142 i: Byte;
143 BEGIN
144 Result:=0;
145 IF Length(bin)<>8 THEN Exit;
146 Add:=1;
147 FOR i:=8 DOWNTO 1 DO BEGIN
148 IF NOT (bin[i] IN ['0','1']) THEN Exit;
149 IF bin[i] = '1' THEN Inc(Result,Add);
150 Add:=Add SHL 1;
151 END;
152 END;
153
154
155
156FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
157 BEGIN
158 Result:=AnsiReplaceStr(Format('%'+IntToStr(width)+'u',[value]),' ',leadingzeros);
159 END;
160
161FUNCTION FormatFileSize(size:LongWord):String;
162 BEGIN
163 IF size>=1000*1024*1024 THEN BEGIN
164 Result:=FloatToStrF(size/1024/1024/1024,ffFixed,5,1)+' GB';
165 END ELSE BEGIN
166 IF size>=1000*1024 THEN BEGIN
167 Result:=FloatToStrF(size/1024/1024,ffFixed,5,1)+' MB';
168 END ELSE BEGIN
169 IF size>=1000 THEN BEGIN
170 Result:=FloatToStrF(size/1024,ffFixed,5,1)+' KB';
171 END ELSE BEGIN
172 Result:=IntToStr(size)+' B';
173 END;
174 END;
175 END;
176 END;
177
178FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
179 VAR
180 string_build,ascii_version:String;
181 i:LongWord;
182 BEGIN
183 string_build:='';
184 ascii_version:='';
185 FOR i:=0 TO High(data) DO BEGIN
186 IF NOT HexOnly THEN
187 IF (i MOD 16)=0 THEN
188 string_build:=string_build+'0x'+IntToHex(i,6)+' ';
189 string_build:=string_build+IntToHex(data[i],2);
190 IF NOT HexOnly THEN BEGIN
191 IF data[i]>=32 THEN ascii_version:=ascii_version+Chr(data[i])
192 ELSE ascii_version:=ascii_version+'.';
193 IF ((i+1) MOD 2)=0 THEN string_build:=string_build+#32;
194 IF ((i+1) MOD 16)=0 THEN BEGIN
195 string_build:=string_build+#32+ascii_version+CrLf;
196 ascii_version:='';
197 END;
198 END;
199 END;
200 Result:=string_build;
201 END;
202
203FUNCTION DecodeHexString(hex:String):Tdata;
204 VAR
205 i:LongWord;
206 BEGIN
207 SetLength(Result, Length(hex) DIV 2);
208 FOR i:=0 TO Length(Result) DO BEGIN
209 Result[i]:=0;
210 CASE UpCase(hex[1+i*2]) OF
211 '0'..'9': Result[i]:=(Ord(UpCase(hex[1+i*2]))-48)*16;
212 'A'..'F': Result[i]:=(Ord(UpCase(hex[1+i*2]))-55)*16;
213 END;
214 CASE UpCase(hex[1+i*2+1]) OF
215 '0'..'9': Result[i]:=Result[i]+(Ord(UpCase(hex[1+i*2+1]))-48);
216 'A'..'F': Result[i]:=Result[i]+(Ord(UpCase(hex[1+i*2+1]))-55);
217 END;
218 END;
219 END;
220
221
222
223FUNCTION StringSmaller(string1,string2:String):Boolean;
224 VAR
225 i:Integer;
226 len:Integer;
227 BEGIN
228 len:=Min(Length(string1),Length(string2));
229 FOR i:=1 TO len DO
230 IF Ord(string1[i])<>Ord(string2[i]) THEN BEGIN
231 Result:=Ord(string1[i])<Ord(string2[i]);
232 Exit;
233 END;
234 Result:=Length(string1)<Length(string2);
235 END;
236
237
238
239
240FUNCTION ExportFile(fileid:LongWord; filename:String; settings:TExportSet; path:String):Integer;
241 VAR
242 i:Byte;
243 extension:String;
244 rawlist:TRawList;
245 BEGIN
246 Result:=export_noerror;
247 extension:=RightStr(filename,4);
248 IF DO_toone IN settings THEN BEGIN
249 ExportDatFile(fileid,path+'\'+GetWinFileName(filename));
250 END ELSE BEGIN
251 IF DO_dat IN settings THEN ExportDatFile(fileid,path+'\'+GetWinFileName(filename));
252 IF DO_raw IN settings THEN BEGIN
253 rawlist:=OniDataConnection.GetRawList(fileid);
254 IF Length(rawlist)>0 THEN BEGIN
255 FOR i:=0 TO High(rawlist) DO BEGIN
256 ExportRawFile(fileid,rawlist[i].src_offset,path+'\'+GetWinFileName(filename));
257 END;
258 END;
259 END;
260 END;
261 END;
262
263FUNCTION Explode(_string:String; delimiter:Char):TStringArray;
264 VAR
265 start,len:Word;
266 BEGIN
267 SetLength(Result, 0);
268 start:=1;
269 WHILE PosEx(delimiter,_string,start)>0 DO BEGIN
270 len:=PosEx(delimiter,_string,start)-start;
271 SetLength(Result, Length(Result)+1);
272 Result[High(Result)]:=MidStr(_string,start,len);
273 start:=start+len+1;
274 END;
275 SetLength(Result, Length(Result)+1);
276 Result[High(Result)]:=MidStr(_string,start,Length(_string)-start+1);
277 END;
278
279FUNCTION GetWinFileName(name:String):String;
280 BEGIN
281 Result:=name;
282 Result:=AnsiReplaceStr(Result,'\','__');
283 Result:=AnsiReplaceStr(Result,'/','__');
284 Result:=AnsiReplaceStr(Result,'>','__');
285 Result:=AnsiReplaceStr(Result,'<','__');
286 END;
287
288FUNCTION GetExtractPath:String;
289 BEGIN
290 Result:=ExtractFilePath(OniDataConnection.FileName)+'\extracted_'+ExtractFileName(OniDataConnection.Filename);
291 END;
292
293
294END.
Note: See TracBrowser for help on using the repository browser.