source: oup/releases/0.11a/Unit2_functions.pas@ 352

Last change on this file since 352 was 19, checked in by alloc, 18 years ago
File size: 6.7 KB
RevLine 
[19]1UNIT Unit2_functions;
2INTERFACE
3USES Classes, SysUtils, StrUtils, Math, Unit3_data, Unit4_Exporters;
4
5FUNCTION LoadDatInfos(filename:String):Boolean;
6FUNCTION LoadDatFile(fileid:LongWord):Tdata;
7FUNCTION LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
8FUNCTION LoadRawFilePart(address,size:LongWord; target:Pointer):Boolean;
9FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
10FUNCTION FormatFileSize(size:LongWord):String;
11FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
12FUNCTION ExportFile(fileid:LongWord; convert:Boolean):Integer;
13FUNCTION GetWinFileName(fileid:LongWord; substring:String):String;
14FUNCTION GetExtractPath:String;
15
16
17
18IMPLEMENTATION
19
20
21FUNCTION LoadDatInfos(filename:String):Boolean;
22 VAR i:LongWord;
23 dat_file:TFileStream;
24 BEGIN
25 Result:=True;
26 IF Length(filename)>0 THEN dat_file.Free;
27 dat_filename:=filename;
28 dat_file:=TFileStream.Create(filename, fmOpenRead);
29 dat_file.Read(dat_header,SizeOf(dat_header));
30 FOR i:=0 TO High(dat_header.Ident) DO
31 IF dat_header.Ident[i]<>header_ident1[i] THEN BEGIN
32 Result:=False;
33 Exit;
34 END;
35{ FOR i:=0 TO High(dat_header.Ident2) DO
36 IF dat_header.Ident2[i]<>header_ident2[i] THEN BEGIN
37 Result:=False;
38 Exit;
39 END;
40}
41 SetLength(dat_filesmap,dat_header.Files);
42 SetLength(dat_files,dat_header.Files);
43 FOR i:=0 TO dat_header.Files-1 DO dat_file.Read(dat_filesmap[i],SizeOf(dat_filesmap[i]));
44 FOR i:=0 TO dat_header.Files-1 DO BEGIN
45 dat_files[i].Extension:=dat_filesmap[i].Extension;
46 dat_files[i].Extension:=ReverseString(dat_files[i].Extension);
47 dat_files[i].Size:=dat_filesmap[i].FileSize;
48 dat_files[i].FileType:=dat_filesmap[i].FileType;
49 dat_files[i].DatAddr:=dat_filesmap[i].DataAddr-8+dat_header.DataAddr;
50 IF (dat_filesmap[i].FileType AND $01)=0 THEN BEGIN
51 dat_file.Seek(dat_filesmap[i].NameAddr+dat_header.NamesAddr,soFromBeginning);
52 SetLength(dat_files[i].Name,100);
53 dat_file.Read(dat_files[i].Name[1],100);
54 dat_files[i].Name:=MidStr(dat_files[i].Name,1+4,Pos(#0,dat_files[i].Name)-1-4);
55 END ELSE BEGIN
56 dat_files[i].Name:='';
57 END;
58 dat_files[i].FileName:=FormatNumber(i,5,'0')+'-'+dat_files[i].Name+'.'+dat_files[i].Extension;
59 END;
60 dat_file.Seek($40+dat_header.Files*$14,soFromBeginning);
61 SetLength(dat_namedfilesmap,dat_header.NamedFiles);
62 FOR i:=0 TO dat_header.NamedFiles-1 DO dat_file.Read(dat_namedfilesmap[i],SizeOf(dat_namedfilesmap[i]));
63
64 dat_file.Seek($40+dat_header.Files*$14+dat_header.NamedFiles*$8,soFromBeginning);
65 SetLength(dat_extensionsmap,dat_header.Extensions);
66 FOR i:=0 TO dat_header.Extensions-1 DO dat_file.Read(dat_extensionsmap[i],SizeOf(dat_extensionsmap[i]));
67
68 dat_file.Free;
69 END;
70
71
72FUNCTION LoadDatFile(fileid:LongWord):Tdata;
73 VAR dat_file:TFileStream;
74 BEGIN
75 dat_file:=TFileStream.Create(dat_filename, fmOpenRead);
76 dat_file.Seek(dat_files[fileid].DatAddr,soFromBeginning);
77 SetLength(Result,dat_files[fileid].Size);
78 dat_file.Read(Result[0],dat_files[fileid].Size);
79 dat_file.Free;
80 END;
81
82
83FUNCTION LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
84 VAR dat_file:TFileStream;
85 BEGIN
86 dat_file:=TFileStream.Create(dat_filename, fmOpenRead);
87 Result:=True;
88 dat_file.Seek(dat_files[fileid].DatAddr+offset,soFromBeginning);
89 dat_file.Read(target^,size);
90 dat_file.Free;
91 END;
92
93
94FUNCTION LoadRawFilePart(address,size:LongWord; target:Pointer):Boolean;
95 VAR
96 filestream:TFileStream;
97 BEGIN
98 Result:=True;
99 filestream:=TFileStream.Create(AnsiReplaceStr(dat_filename,'.dat','.raw'),fmOpenRead);
100 filestream.Seek(address,soFromBeginning);
101 filestream.Read(target^,size);
102 filestream.Free;
103 END;
104
105
106FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
107 BEGIN
108 Result:=AnsiReplaceStr(Format('%'+IntToStr(width)+'u',[value]),' ',leadingzeros);
109 END;
110
111
112FUNCTION FormatFileSize(size:LongWord):String;
113 BEGIN
114 IF size>=1024*1024*1024 THEN BEGIN
115 Result:=FloatToStrF(size/1024/1024/1024,ffFixed,5,1)+' GB';
116 END ELSE BEGIN
117 IF size>=1024*1024 THEN BEGIN
118 Result:=FloatToStrF(size/1024/1024,ffFixed,5,1)+' MB';
119 END ELSE BEGIN
120 IF size>=1024 THEN BEGIN
121 Result:=FloatToStrF(size/1024,ffFixed,5,1)+' KB';
122 END ELSE BEGIN
123 Result:=IntToStr(size)+' B';
124 END;
125 END;
126 END;
127 END;
128
129
130FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
131 VAR
132 string_build,ascii_version:String;
133 i:LongWord;
134 BEGIN
135 string_build:='';
136 ascii_version:='';
137 FOR i:=0 TO High(data) DO BEGIN
138 IF NOT HexOnly THEN
139 IF (i MOD 16)=0 THEN
140 string_build:=string_build+'0x'+IntToHex(i,6)+' ';
141 string_build:=string_build+IntToHex(data[i],2);
142 IF NOT HexOnly THEN BEGIN
143 IF data[i]>=32 THEN ascii_version:=ascii_version+Chr(data[i])
144 ELSE ascii_version:=ascii_version+'.';
145 IF ((i+1) MOD 2)=0 THEN string_build:=string_build+#32;
146 IF ((i+1) MOD 16)=0 THEN BEGIN
147 string_build:=string_build+#32+ascii_version+CrLf;
148 ascii_version:='';
149 END;
150 END;
151 END;
152 Result:=string_build;
153 END;
154
155
156FUNCTION ExportFile(fileid:LongWord; convert:Boolean):Integer;
157 VAR
158 i:Byte;
159 BEGIN
160 Result:=export_noerror;
161
162 ExportDefFileHeader(fileid);
163
164 IF (dat_files[fileid].FileType AND $02)=0 THEN BEGIN
165 ExportDatFile(fileid);
166
167 FOR i:=1 TO Length(ExportHandlers)+1 DO BEGIN
168 IF i<=Length(ExportHandlers) THEN BEGIN
169 IF ExportHandlers[i].Ext=dat_files[fileid].Extension THEN BEGIN
170 IF ExportHandlers[i].needed THEN BEGIN
171 CASE ExportHandlers[i].Handler(fileid,convert) OF
172 0: Result:=0;
173 ELSE
174 Result:=export_handlererror;
175 END;
176 END;
177 Break;
178 END;
179 END ELSE BEGIN
180 Result:=export_nohandler;
181 END;
182 END;
183 END;
184 END;
185
186
187FUNCTION GetWinFileName(fileid:LongWord; substring:String):String;
188 VAR
189 name:String;
190 BEGIN
191 name:=dat_files[fileid].Name;
192 name:=AnsiReplaceStr(name,'\','__');
193 name:=AnsiReplaceStr(name,'/','__');
194 name:=AnsiReplaceStr(name,'>','__');
195 name:=AnsiReplaceStr(name,'<','__');
196 Result:=FormatNumber(fileid,5,'0')+'-'+name+'.'+substring+'.'+dat_files[fileid].Extension;
197 END;
198
199FUNCTION GetExtractPath:String;
200 BEGIN
201 Result:=ExtractFilePath(dat_filename)+'\extracted_'+ExtractFileName(dat_filename);
202 END;
203
204
205END.
Note: See TracBrowser for help on using the repository browser.