source: oup/releases/0.31a/Unit2_functions.pas@ 627

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