source: oup/releases/0.30a/Unit2_functions.pas@ 390

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