1 |
|
2 |
#include <stdlib.h> |
3 |
#include <stdio.h> |
4 |
#include <string.h> |
5 |
#include <sstream> |
6 |
#include <iostream> |
7 |
#include <vector> |
8 |
|
9 |
#include "infos.h" |
10 |
#include "common.h" |
11 |
#include "Floppy.h" |
12 |
|
13 |
|
14 |
|
15 |
|
16 |
void main(int argc, char *argv[]) |
17 |
{ |
18 |
// |
19 |
// Some initialization for the common library |
20 |
// |
21 |
SetApplicationParameters( |
22 |
"FloppyBuilder", |
23 |
TOOL_VERSION_MAJOR, |
24 |
TOOL_VERSION_MINOR, |
25 |
"{ApplicationName} - Version {ApplicationVersion} - This program is a part of the OSDK\r\n" |
26 |
"\r\n" |
27 |
"Author:\r\n" |
28 |
" (c) 2002 Debrune Jerome for the initial version \r\n" |
29 |
" (c) 2013 Pointier Mickael for the subsequent changes \r\n" |
30 |
"\r\n" |
31 |
"Purpose:\r\n" |
32 |
" Generating bootable floppies for the Oric computer.\r\n" |
33 |
"\r\n" |
34 |
"Usage:\r\n" |
35 |
" {ApplicationName} <description file path>\r\n" |
36 |
"\r\n" |
37 |
); |
38 |
|
39 |
// makedisk filetobuild.txt default.dsk ..\build\%OSDKDISK% |
40 |
|
41 |
long param=1; |
42 |
|
43 |
if (argc>1) |
44 |
{ |
45 |
for (;;) |
46 |
{ |
47 |
long nb_arg=argc; |
48 |
const char *ptr_arg=argv[param]; |
49 |
|
50 |
if (nb_arg==argc) break; |
51 |
} |
52 |
} |
53 |
|
54 |
|
55 |
if (argc!=2) |
56 |
{ |
57 |
ShowError(nullptr); |
58 |
} |
59 |
|
60 |
|
61 |
// |
62 |
// Open the description file |
63 |
// |
64 |
const char* description_name(argv[param]); |
65 |
std::vector<std::string> script; |
66 |
if (!LoadText(description_name,script)) |
67 |
{ |
68 |
ShowError("Can't load script file '%s'\n",description_name); |
69 |
} |
70 |
|
71 |
// |
72 |
// Copy the floppy disk |
73 |
// |
74 |
Floppy floppy; |
75 |
|
76 |
std::string outputLayoutFileName; |
77 |
std::string targetFloppyDiskName; |
78 |
|
79 |
int lineNumber=0; |
80 |
for (auto it(script.begin());it!=script.end();++it) |
81 |
{ |
82 |
++lineNumber; |
83 |
|
84 |
std::string currentLine(*it); |
85 |
|
86 |
std::size_t found = currentLine.find(";"); |
87 |
if (found!=std::string::npos) |
88 |
{ |
89 |
// Comments, just skip them |
90 |
currentLine=currentLine.substr(0,found); |
91 |
} |
92 |
|
93 |
std::istringstream iss(currentLine); |
94 |
|
95 |
std::string item; |
96 |
std::vector<std::string> tokens; |
97 |
while (std::getline(iss,item,' ')) |
98 |
{ |
99 |
// Remove eventual superfluous spaces and tabs |
100 |
item=StringTrim(item); |
101 |
if (!item.empty()) |
102 |
{ |
103 |
tokens.push_back(item); |
104 |
} |
105 |
} |
106 |
|
107 |
if (!tokens.empty()) |
108 |
{ |
109 |
if (tokens[0]=="LoadDiskTemplate") |
110 |
{ |
111 |
if (tokens.size()==2) |
112 |
{ |
113 |
const std::string& templateFisk(tokens[1]); |
114 |
if (!floppy.LoadDisk(templateFisk.c_str())) |
115 |
{ |
116 |
ShowError("Can't load '%s'\n",templateFisk.c_str()); |
117 |
} |
118 |
|
119 |
} |
120 |
else |
121 |
{ |
122 |
ShowError("Syntax error line (%d), syntax is 'LoadDiskTemplate FilePath' \n",lineNumber); |
123 |
} |
124 |
} |
125 |
else |
126 |
if (tokens[0]=="DefineDisk") |
127 |
{ |
128 |
if (tokens.size()==4) |
129 |
{ |
130 |
int numberOfSides =std::atoi(tokens[1].c_str()); |
131 |
int numberOfTracks =std::atoi(tokens[2].c_str()); |
132 |
int numberOfSectors =std::atoi(tokens[3].c_str()); |
133 |
|
134 |
if ( numberOfSides!=2 ) |
135 |
{ |
136 |
ShowError("Syntax error line (%d), numberOfSides has to be 2 (so far)\n",lineNumber); |
137 |
} |
138 |
|
139 |
if ( numberOfTracks!=42 ) |
140 |
{ |
141 |
ShowError("Syntax error line (%d), numberOfTracks has to be 42 (so far)\n",lineNumber); |
142 |
} |
143 |
|
144 |
if ( numberOfSectors!=17 ) |
145 |
{ |
146 |
ShowError("Syntax error line (%d), numberOfSectors has to be 17 (so far)\n",lineNumber); |
147 |
} |
148 |
|
149 |
if (!floppy.CreateDisk(numberOfSides,numberOfTracks,numberOfSectors)) |
150 |
{ |
151 |
ShowError("Can't create disk\n"); |
152 |
} |
153 |
|
154 |
} |
155 |
else |
156 |
{ |
157 |
ShowError("Syntax error line (%d), syntax is 'DefineDisk numberOfSides numberOfTracks numberOfSectors' \n",lineNumber); |
158 |
} |
159 |
} |
160 |
else |
161 |
if (tokens[0]=="OutputLayoutFile") |
162 |
{ |
163 |
if (tokens.size()==2) |
164 |
{ |
165 |
outputLayoutFileName=tokens[1]; |
166 |
} |
167 |
else |
168 |
{ |
169 |
ShowError("Syntax error line (%d), syntax is 'OutputLayoutFile FilePath' \n",lineNumber); |
170 |
} |
171 |
} |
172 |
else |
173 |
if (tokens[0]=="OutputFloppyFile") |
174 |
{ |
175 |
if (tokens.size()==2) |
176 |
{ |
177 |
targetFloppyDiskName=tokens[1]; |
178 |
} |
179 |
else |
180 |
{ |
181 |
ShowError("Syntax error line (%d), syntax is 'targetFloppyDiskName FilePath' \n",lineNumber); |
182 |
} |
183 |
} |
184 |
else |
185 |
if (tokens[0]=="SetPosition") |
186 |
{ |
187 |
if (tokens.size()==3) |
188 |
{ |
189 |
int currentTrack =std::atoi(tokens[1].c_str()); |
190 |
if ( (currentTrack<0) || (currentTrack>41) ) |
191 |
{ |
192 |
ShowError("Syntax error line (%d), TrackNumber has to be between 0 and 41' \n",lineNumber); |
193 |
} |
194 |
int currentSector=std::atoi(tokens[2].c_str()); |
195 |
if ( (currentSector<0) || (currentSector>41) ) |
196 |
{ |
197 |
ShowError("Syntax error line (%d), SectorNumber has to be between 1 and 17' \n",lineNumber); |
198 |
} |
199 |
floppy.SetPosition(currentTrack,currentSector); |
200 |
} |
201 |
else |
202 |
{ |
203 |
ShowError("Syntax error line (%d), syntax is 'SetPosition TrackNumber SectorNumber' \n",lineNumber); |
204 |
} |
205 |
} |
206 |
else |
207 |
if (tokens[0]=="WriteSector") |
208 |
{ |
209 |
if (tokens.size()==2) |
210 |
{ |
211 |
std::string fileName=tokens[1]; |
212 |
if (!floppy.WriteSector(fileName.c_str())) |
213 |
{ |
214 |
ShowError("Error line (%d), could not write file '%s' to disk. Make sure you have a valid floppy format declared. \n",lineNumber,fileName.c_str()); |
215 |
} |
216 |
} |
217 |
else |
218 |
{ |
219 |
ShowError("Syntax error line (%d), syntax is 'WriteSector FilePath' \n",lineNumber); |
220 |
} |
221 |
} |
222 |
else |
223 |
if (tokens[0]=="AddFile") |
224 |
{ |
225 |
if (tokens.size()==3) |
226 |
{ |
227 |
std::string fileName=tokens[1]; |
228 |
int loadAddress=ConvertAdress(tokens[2].c_str()); |
229 |
if (!floppy.WriteFile(fileName.c_str(),loadAddress,false)) |
230 |
{ |
231 |
ShowError("Error line (%d), could not write file '%s' to disk. Make sure you have a valid floppy format declared. \n",lineNumber,fileName.c_str()); |
232 |
} |
233 |
} |
234 |
else |
235 |
{ |
236 |
ShowError("Syntax error line (%d), syntax is 'AddFile FilePath LoadAddress' \n",lineNumber); |
237 |
} |
238 |
} |
239 |
else |
240 |
if (tokens[0]=="AddTapFile") |
241 |
{ |
242 |
if (tokens.size()==2) |
243 |
{ |
244 |
std::string fileName=tokens[1]; |
245 |
if (!floppy.WriteFile(fileName.c_str(),-1,true)) |
246 |
{ |
247 |
ShowError("Error line (%d), could not write file '%s' to disk. Make sure you have a valid floppy format declared. \n",lineNumber,fileName.c_str()); |
248 |
} |
249 |
} |
250 |
else |
251 |
{ |
252 |
ShowError("Syntax error line (%d), syntax is 'AddTapFile FilePath' \n",lineNumber); |
253 |
} |
254 |
} |
255 |
else |
256 |
if (tokens[0]=="AddDefine") |
257 |
{ |
258 |
if (tokens.size()==3) |
259 |
{ |
260 |
floppy.AddDefine(tokens[1],tokens[2]); |
261 |
} |
262 |
else |
263 |
{ |
264 |
ShowError("Syntax error line (%d), syntax is 'AddDefine DefineName DefineValue' \n",lineNumber); |
265 |
} |
266 |
} |
267 |
else |
268 |
{ |
269 |
ShowError("Syntax error line (%d), unknown keyword '%s' \n",lineNumber,tokens[0].c_str()); |
270 |
} |
271 |
} |
272 |
} |
273 |
|
274 |
|
275 |
|
276 |
if (!floppy.SaveDescription(outputLayoutFileName.c_str())) |
277 |
{ |
278 |
ShowError("Failed saving description '%s'\n",outputLayoutFileName.c_str()); |
279 |
} |
280 |
|
281 |
if (!floppy.SaveDisk(targetFloppyDiskName.c_str())) |
282 |
{ |
283 |
ShowError("Failed saving disk '%s'\n",targetFloppyDiskName.c_str()); |
284 |
} |
285 |
else |
286 |
{ |
287 |
printf("Successfully created '%s'\n",targetFloppyDiskName.c_str()); |
288 |
} |
289 |
} |