/[projet1]/public/pc/tools/osdk/main/bas2tap/sources/bas2tap.cpp
Defence Force logotype

Contents of /public/pc/tools/osdk/main/bas2tap/sources/bas2tap.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1473 - (show annotations)
Sun May 6 13:55:26 2018 UTC (16 months, 2 weeks ago) by dbug
File size: 10055 byte(s)
Bas2Tap 1.1
- Added filtering of strings before parsing, to remove superfluous spaces, tabs and other carriage returns before starting the syntax conversion pass

1
2 #include "infos.h"
3
4 #include "common.h"
5
6 #include <stdlib.h>
7 #include <stdio.h>
8 #ifdef _WIN32
9 #include <conio.h>
10 #else
11 #define memicmp strncasecmp
12 #endif
13 #include <string.h>
14
15 //
16 // Things to do:
17 // - Handle auto-numeration and labels
18 // - Option to optimize the programs (truncate variables to two characters, remove comments)
19 //
20
21 const char *keywords[]=
22 {
23 // 128-246: BASIC keywords
24 "END","EDIT","STORE","RECALL","TRON","TROFF","POP","PLOT",
25 "PULL","LORES","DOKE","REPEAT","UNTIL","FOR","LLIST","LPRINT","NEXT","DATA",
26 "INPUT","DIM","CLS","READ","LET","GOTO","RUN","IF","RESTORE","GOSUB","RETURN",
27 "REM","HIMEM","GRAB","RELEASE","TEXT","HIRES","SHOOT","EXPLODE","ZAP","PING",
28 "SOUND","MUSIC","PLAY","CURSET","CURMOV","DRAW","CIRCLE","PATTERN","FILL",
29 "CHAR","PAPER","INK","STOP","ON","WAIT","CLOAD","CSAVE","DEF","POKE","PRINT",
30 "CONT","LIST","CLEAR","GET","CALL","!","NEW","TAB(","TO","FN","SPC(","@",
31 "AUTO","ELSE","THEN","NOT","STEP","+","-","*","/","^","AND","OR",">","=","<",
32 "SGN","INT","ABS","USR","FRE","POS","HEX$","&","SQR","RND","LN","EXP","COS",
33 "SIN","TAN","ATN","PEEK","DEEK","LOG","LEN","STR$","VAL","ASC","CHR$","PI",
34 "TRUE","FALSE","KEY$","SCRN","POINT","LEFT$","RIGHT$","MID$"
35 // 247- : Error messages
36 };
37
38 unsigned char head[14]={ 0x16,0x16,0x16,0x24,0,0,0,0,0,0,5,1,0,0 };
39
40
41
42
43 void Tap2Bas(unsigned char *ptr_buffer,size_t file_size,const char *destFile)
44 {
45 unsigned int i, car;
46
47 FILE *out = stdout;
48 if (destFile && (strlen(destFile) != 0))
49 {
50 out = fopen(destFile, "wb");
51 }
52 if (out == NULL)
53 {
54 printf("Can't open file for writing\n");
55 exit(1);
56 }
57
58
59 if (ptr_buffer[0]!=0x16 || ptr_buffer[3]!=0x24)
60 {
61 ShowError("Not an Oric file");
62 }
63 if (ptr_buffer[6])
64 {
65 ShowError("Not a BASIC file");
66 }
67 i=13;
68 while (ptr_buffer[i++]);
69 while (ptr_buffer[i] || ptr_buffer[i+1])
70 {
71 i+=2;
72 fprintf(out," %u ",ptr_buffer[i]+(ptr_buffer[i+1]<<8));
73 i+=2;
74 while ((car=ptr_buffer[i++]))
75 {
76 if (car<128)
77 fputc(car,out);
78 else
79 if (car < 247)
80 {
81 fprintf(out,"%s", keywords[car - 128]);
82 }
83 else
84 {
85 // Probably corrupted listing
86 // 247 : NEXT WITHOUT FOR
87 fprintf(out,"CORRUPTED_ERROR_CODE_%u", car);
88 }
89 }
90 fputc('\r',out);
91 fputc('\n', out);
92 }
93
94 fclose(out);
95 }
96
97
98
99 // tap2bas
100 int search_keyword(const char *str)
101 {
102 for (unsigned int i=0;i<sizeof(keywords)/sizeof(char *);i++)
103 {
104 if (strncmp(keywords[i],str,strlen(keywords[i]))==0)
105 {
106 return i;
107 }
108 }
109 return -1;
110 }
111
112
113
114 void Bas2Tap(const char *sourceFile,const char *destFile,bool autoRun,bool useColor)
115 {
116 unsigned char buf[48192];
117 unsigned int end, lastptr, adr;
118 int j,ptr,keyw,string,rem,data;
119
120 // Mike: Need to improve the parsing of this with a global function to split
121 // a text file in separate lines.
122 std::vector<std::string> textData;
123 if (!LoadText(sourceFile,textData))
124 {
125 ShowError("Unable to load source file");
126 }
127
128 std::string currentFile=sourceFile;
129 int currentLineNumber=0;
130 int i=0;
131 std::vector<std::string>::const_iterator lineIt=textData.begin();
132 while (lineIt!=textData.end())
133 {
134 const std::string& currentLine=StringTrim(*lineIt);
135
136 ++currentLineNumber;
137
138 if (!currentLine.empty())
139 {
140 const char* ligne=currentLine.c_str();
141 if (ligne[0]=='#')
142 {
143 // Preprocessor directive
144 if (memicmp(ligne,"#file",5)==0)
145 {
146 //"#file font.BAS""
147 // Very approximative "get the name of the file and reset the line counter" code.
148 // Will clean up that when I will have some more time.
149 ligne+=5;
150 currentFile=ligne;
151 currentLineNumber=0;
152 }
153 else
154 {
155 ShowError("Unknown preprocessor directive in file %s line number line %d",currentFile.c_str(),currentLineNumber);
156 }
157 }
158 else
159 {
160 // Standard line
161 buf[i++]=0;
162 buf[i++]=0;
163
164 int number=get_value(ligne,-1);
165 if (number<0)
166 {
167 // Mike: Need to add better diagnostic here
168 ShowError("Missing line number in file %s line %d",currentFile.c_str(),currentLineNumber);
169 break;
170 }
171 buf[i++]=number&0xFF;
172 buf[i++]=number>>8;
173
174 ptr=0;
175 rem=0;
176 bool color=useColor;
177 string=0;
178 data=0;
179 if (ligne[ptr]==' ') ptr++;
180
181 while (ligne[ptr])
182 {
183 if (rem)
184 {
185 if (color)
186 {
187 color=false;
188 buf[i++]=27; // ESCAPE
189 buf[i++]='B'; // GREEN labels
190 }
191 buf[i++]=ligne[ptr++];
192 }
193 else
194 if (string)
195 {
196 if (ligne[ptr]=='"') string=0;
197 buf[i++]=ligne[ptr++];
198 }
199 else
200 if (data)
201 {
202 if (ligne[ptr]==':') data=0;
203 buf[i++]=ligne[ptr++];
204 }
205 else
206 {
207 const char* pLine=(ligne+ptr);
208 keyw=search_keyword(pLine);
209 if (keyw==29 || ligne[ptr]=='\'') rem=1;
210 if (keyw==17) data=1;
211 if (ligne[ptr]=='"') string=1;
212 if (keyw>=0)
213 {
214 buf[i++]=keyw+128;
215 ptr+=strlen(keywords[keyw]);
216 }
217 else
218 {
219 buf[i++]=ligne[ptr++];
220 }
221 }
222 }
223 buf[i++]=0;
224 }
225 }
226 ++lineIt;
227 }
228 buf[i++]=0;
229 buf[i++]=0;
230
231 //following line modified by Wilfrid AVRILLON (Waskol) 06/20/2009
232 //It should follow this rule of computation : End_Address=Start_Address+File_Size-1
233 //Let's assume a 1 byte program, it starts at address #501 and ends at address #501 (Address=Address+1-1) !
234 //It was a blocking issue for various utilities (tap2wav for instance)
235 //end=0x501+i-1; //end=0x501+i;
236 end=0x501+i;
237
238 if (autoRun) head[7]=0x80; // Autorun for basic :)
239 else head[7]=0;
240
241 head[8]=end>>8;
242 head[9]=end&0xFF;
243
244 for(j=4,lastptr=0;j<i;j++)
245 {
246 if (buf[j]==0)
247 {
248 adr=0x501+j+1;
249 buf[lastptr]=adr&0xFF;
250 buf[lastptr+1]=adr>>8;
251 lastptr=j+1;
252 j+=4;
253 }
254 }
255
256 //
257 // Save file
258 //
259 FILE *out=fopen(destFile,"wb");
260 if (out==NULL)
261 {
262 printf("Can't open file for writing\n");
263 exit(1);
264 }
265 fwrite(head,1,13,out);
266 // write the name
267 if (currentFile.length() > 0)
268 {
269 char *currentFileDup = strdup(currentFile.c_str());
270 char *fileName = currentFileDup;
271 // only take the file name from the path
272 // try to find '\\'
273 char *lastsep = strrchr(fileName, '\\');
274 if (lastsep != NULL)
275 {
276 // if there is something after the separator
277 if (lastsep + 1 != 0)
278 fileName = lastsep + 1;
279 }
280 else
281 {
282 // try to find /
283 lastsep = strrchr(fileName, '/');
284 if (lastsep != NULL)
285 {
286 // if there is something after the separator
287 if (lastsep + 1 != 0)
288 fileName = lastsep + 1;
289 }
290 }
291 // remove the extension if there is one
292 char *lastdot = strrchr(fileName, '.');
293 if (lastdot != NULL)
294 *lastdot = 0;
295 fwrite(fileName, 1, strlen(fileName), out);
296 free(currentFileDup);
297 }
298 fwrite("\x00", 1, 1, out);
299 fwrite(buf,1,i+1,out);
300 // oricutron bug work around
301 //fwrite("\x00", 1, 1, out);
302 fclose(out);
303 }
304
305
306
307
308 #define NB_ARG 2
309
310 int main(int argc, char **argv)
311 {
312 //
313 // Some initialization for the common library
314 //
315 SetApplicationParameters(
316 "Bas2Tap",
317 TOOL_VERSION_MAJOR,
318 TOOL_VERSION_MINOR,
319 "{ApplicationName} - Version {ApplicationVersion} - This program is a part of the OSDK\r\n"
320 "\r\n"
321 "Author:\r\n"
322 " Fabrice Frances \r\n"
323 "\r\n"
324 "Purpose:\r\n"
325 " Converting a text file containing a BASIC source code to a binary\r\n"
326 " encoded TAPE file that can be loaded using the CLOAD command.\r\n"
327 " (and the opposite operation as well).\r\n"
328 "\r\n"
329 "Parameters:\r\n"
330 " <options> <sourcefile> <destinationfile>\r\n"
331 "\r\n"
332 "Options:\r\n"
333 " -b2t[0|1] for converting to tape format with autorun (1) or not (0)\r\n"
334 " -t2b for converting from tape format text\r\n"
335 " -color[0|1] for enabling colored comments"
336 "\r\n"
337 "Example:\r\n"
338 " {ApplicationName} -b2t1 final.txt osdk.tap\r\n"
339 " {ApplicationName} -t2b osdk.tap program.txt\r\n"
340 );
341
342 bool basicToTape=true;
343 bool autoRun=true;
344 bool useColor=false;
345
346 ArgumentParser argumentParser(argc,argv);
347
348 while (argumentParser.ProcessNextArgument())
349 {
350 if (argumentParser.IsSwitch("-t2b"))
351 {
352 // Tape to BASIC source code
353 basicToTape=false;
354 }
355 else
356 if (argumentParser.IsSwitch("-b2t"))
357 {
358 // BASIC source code to tape
359 basicToTape=true;
360 autoRun=argumentParser.GetBooleanValue(true);
361 }
362 else
363 if (argumentParser.IsSwitch("-color"))
364 {
365 // Handling of color codes
366 useColor=argumentParser.GetBooleanValue(false);
367 }
368 }
369
370 if (argumentParser.GetParameterCount()!=NB_ARG)
371 {
372 ShowError(0);
373 }
374
375
376 std::string nameSrc(argumentParser.GetParameter(0));
377 std::string nameDst(argumentParser.GetParameter(1));
378
379 if (basicToTape)
380 {
381 Bas2Tap(nameSrc.c_str(),nameDst.c_str(),autoRun,useColor);
382 }
383 else
384 {
385 // Load the source file
386 void* ptr_buffer_void;
387 size_t file_size;
388 if (!LoadFile(nameSrc.c_str(),ptr_buffer_void,file_size))
389 {
390 ShowError("Unable to load the source file");
391 }
392 unsigned char *ptr_buffer=(unsigned char*)ptr_buffer_void;
393
394 Tap2Bas(ptr_buffer,file_size,nameDst.c_str());
395 }
396
397 exit(0);
398 }

  ViewVC Help
Powered by ViewVC 1.1.26