/[projet1]/public/pc/tools/oric/txt2bas/txt2bas.c
Defence Force logotype

Contents of /public/pc/tools/oric/txt2bas/txt2bas.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1188 - (show annotations)
Wed Sep 17 21:04:56 2014 UTC (5 years, 2 months ago) by Jylam
File MIME type: text/plain
File size: 2785 byte(s)
Fix ugly bus in parsing

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 char *keywords[]= {
6 "END","EDIT","STORE","RECALL","TRON","TROFF","POP","PLOT",
7 "PULL","LORES","DOKE","REPEAT","UNTIL","FOR","LLIST","LPRINT","NEXT","DATA",
8 "INPUT","DIM","CLS","READ","LET","GOTO","RUN","IF","RESTORE","GOSUB","RETURN",
9 "REM","HIMEM","GRAB","RELEASE","TEXT","HIRES","SHOOT","EXPLODE","ZAP","PING",
10 "SOUND","MUSIC","PLAY","CURSET","CURMOV","DRAW","CIRCLE","PATTERN","FILL",
11 "CHAR","PAPER","INK","STOP","ON","WAIT","CLOAD","CSAVE","DEF","POKE","PRINT",
12 "CONT","LIST","CLEAR","GET","CALL","!","NEW","TAB(","TO","FN","SPC(","@",
13 "AUTO","ELSE","THEN","NOT","STEP","+","-","*","/","^","AND","OR",">","=","<",
14 "SGN","INT","ABS","USR","FRE","POS","HEX$","&","SQR","RND","LN????","EXP","COS",
15 "SIN","TAN","ATN","PEEK","DEEK","LOG","LEN","STR$","VAL","ASC","CHR$","PI",
16 "TRUE","FALSE","KEY$","SCRN","POINT","LEFT$","RIGHT$","MID$"
17 };
18
19 unsigned char buf[48*1024];
20 unsigned char head[14]={ 0x16,0x16,0x16,0x24,0,0,0,0,0,0,5,1,0,0 };
21
22 int search_keyword(char *str);
23
24 int main(int argc, char **argv)
25 {
26 unsigned int i, number, end, lastptr, adr;
27 int j,ptr,keyw,string,rem,data;
28 unsigned char ligne[256];
29 FILE *in,*out;
30 if (argc!=3) {
31 perror("Usage : txt2bas txtfile <Oric-BASIC-file>\n");
32 exit(1);
33 }
34 in=fopen(argv[1],"r");
35 if (in==NULL) { perror("Can't open input file\n"); exit(1); }
36 out=fopen(argv[2],"wb");
37 if (out==NULL) { perror("Can't open file for writing\n"); exit(1); }
38
39 i=0;
40 while(1) {
41 buf[i++]=0; buf[i++]=0;
42 if(fscanf(in,"%u",&number)==0) break;
43 buf[i++]=number&0xFF; buf[i++]=number>>8;
44 j=0; while((ligne[j]=getc(in))!='\n') j++; ligne[j]=0;
45 ptr=0; rem=0; string=0; data=0;
46 if (ligne[ptr]==' ') ptr++;
47 while(ligne[ptr]) {
48 if (rem) {
49 buf[i++]=ligne[ptr++];
50 } else if (string) {
51 if (ligne[ptr]=='"') string=0;
52 buf[i++]=ligne[ptr++];
53 } else if (data) {
54 if (ligne[ptr]==':') data=0;
55 buf[i++]=ligne[ptr++];
56 } else {
57 keyw=search_keyword((char*)(ligne+ptr));
58 if (keyw==29 || ligne[ptr]=='\'') rem=1;
59 if (keyw==17) data=1;
60 if (ligne[ptr]=='"') string=1;
61 if (keyw>=0) {
62 buf[i++]=keyw+128; ptr+=strlen(keywords[keyw]);
63 } else {
64 buf[i++]=ligne[ptr++];
65 }
66 }
67 }
68 buf[i++]=0;
69 }
70 buf[i++]=0;
71 end=0x501+i; head[8]=end>>8; head[9]=end&0xFF;
72 for(j=4,lastptr=0;j<i;j++)
73 if (buf[j]==0) {
74 adr=0x500+j+1;
75 buf[lastptr]=adr&0xFF; buf[lastptr+1]=adr>>8;
76 lastptr=j+1;
77 j+=4;
78 }
79 fwrite(head,1,14,out);
80 fwrite(buf,1,i,out); fclose(out);
81 return 0;
82 }
83
84 int search_keyword(char *str)
85 {
86 int i;
87 for (i=0;i<sizeof(keywords)/sizeof(char *);i++)
88 if (strncmp(keywords[i],str,strlen(keywords[i]))==0)
89 return i;
90 return -1;
91 }

  ViewVC Help
Powered by ViewVC 1.1.26