source: trunk/dll/internal/mkstr.c@ 2

Last change on this file since 2 was 2, checked in by root, 23 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
1#define INCL_DOS
2#define INCL_WIN
3
4#include <os2.h>
5#include <stdlib.h>
6#include <stdio.h>
7#include <string.h>
8#include <ctype.h>
9#include "..\version.h"
10#include "..\fm3str.h"
11
12/*
13 * Creates indexed FM3RES.STR file from text file FM3DLL.STR.
14 * FM3DLL.STR contains text strings, one per line, for use by
15 * FM3DLL.DLL.
16 */
17
18int index (const char *s,const char c) {
19
20 char *p;
21
22 p = strchr(s,c);
23 if(p == NULL || !*p)
24 return -1;
25 return (int)(p - s);
26}
27
28
29/*
30 * literal.c
31 * Translate a string with tokens in it according to several conventions:
32 * 1. \x1b translates to char(0x1b)
33 * 2. \27 translates to char(27)
34 * 3. \" translates to "
35 * 4. \' translates to '
36 * 5. \\ translates to \
37 * 6. \r translates to carriage return
38 * 7. \n translates to linefeed
39 * 8. \b translates to backspace
40 * 9. \t translates to tab
41 * 10. \a translates to bell
42 * 11. \f translates to formfeed
43 *
44 * Synopsis
45 * *s = "this\x20is\32a test of \\MSC\\CSM\7"
46 * literal(s);
47 *
48 * ( s now equals "this is a test of \MSC\CSM")
49 */
50
51#define HEX "0123456789ABCDEF"
52#define DEC "0123456789"
53
54int literal (char *fsource) {
55
56 register int wpos,w;
57 int len,oldw;
58 char *fdestin,*freeme,wchar;
59
60 if(!fsource || !*fsource)
61 return 0;
62 len = strlen(fsource) + 1;
63 freeme = fdestin = malloc(len + 1);
64 memset(fdestin,0,len); /* start out with zero'd string */
65
66 w = 0; /* set index to first character */
67 while(fsource[w]) { /* until end of string */
68 switch(fsource[w]) {
69 case '\\':
70 switch(fsource[w + 1]) {
71 case 'x' : /* hexadecimal */
72 wchar = 0;
73 w += 2; /* get past "\x" */
74 if(index(HEX,(char)toupper(fsource[w])) != -1) {
75 oldw = w;
76 while(((wpos = index(HEX,(char)toupper(fsource[w]))) != -1) &&
77 w < oldw + 2) {
78 wchar = (char)(wchar << 4) + (char)wpos;
79 w++;
80 }
81 }
82 else
83 wchar = 'x'; /* just an x */
84 w--;
85 *fdestin++ = wchar;
86 break;
87
88 case '\\' : /* we want a "\" */
89 w++;
90 *fdestin++ = '\\';
91 break;
92
93 case 't' : /* tab char */
94 w++;
95 *fdestin++ = '\t';
96 break;
97
98 case 'n' : /* new line */
99 w++;
100 *fdestin++ = '\n';
101 break;
102
103 case 'r' : /* carr return */
104 w++;
105 *fdestin++ = '\r';
106 break;
107
108 case 'b' : /* back space */
109 w++;
110 *fdestin++ = '\b';
111 break;
112
113 case 'f': /* formfeed */
114 w++;
115 *fdestin++ = '\x0c';
116 break;
117
118 case 'a': /* bell */
119 w++;
120 *fdestin++ = '\07';
121 break;
122
123 case '\'' : /* single quote */
124 w++;
125 *fdestin++ = '\'';
126 break;
127
128 case '\"' : /* double quote */
129 w++;
130 *fdestin++ = '\"';
131 break;
132
133 default : /* decimal */
134 w++; /* get past "\" */
135 wchar = 0;
136 if(index(DEC,fsource[w]) != -1) {
137 oldw = w;
138 do { /* cvt to binary */
139 wchar = (char)(wchar * 10 + (fsource[w++] - 48));
140 } while (index(DEC,fsource[w]) != -1 && w < oldw + 3);
141 w--;
142 }
143 else
144 wchar = fsource[w];
145 *fdestin ++ = wchar;
146 break;
147 }
148 break;
149
150 default :
151 *fdestin++ = fsource[w];
152 break;
153 }
154 w++;
155 }
156 *fdestin = 0; /* terminate the string */
157
158 len = fdestin - freeme;
159 memcpy(fsource,freeme,len + 1); /* swap 'em */
160 free(freeme);
161
162 return len; /* return length of string */
163}
164
165
166int main (void) {
167
168 FILE *fpin,*fpout;
169 ULONG len,x = 0,totallen = 0,thislen;
170 USHORT vermajor = 0,verminor = 0;
171 static char str[8192];
172 char *outfile = "FM3RES.STR",*infile = "FM3DLL.STR";
173
174 vermajor = VERMAJOR;
175 verminor = VERMINOR;
176 printf("\nFM/2 resource string compiler copyright (c) 1998 by M. Kimes"
177 "\n Compiles FM3DLL.STR into FM3RES.STR. For FM/2 version %d.%d.\n",
178 vermajor,
179 verminor);
180 /* open input file */
181 fpin = fopen(infile,"r");
182 if(fpin) {
183 /* open (create) output file */
184 fpout = fopen(outfile,"wb");
185 if(fpout) {
186 /* skip space for numstrs and textlen parameter in outfile */
187 if(fseek(fpout,(sizeof(ULONG) * 2) + (sizeof(USHORT) * 2),SEEK_SET)) {
188 printf("\n **Seek error on %s!\n",outfile);
189 fclose(fpin);
190 fclose(fpout);
191 remove(outfile);
192 return 1;
193 }
194 /* step through strings, reading from input, writing to output */
195 while(!feof(fpin)) {
196 if(!fgets(str,sizeof(str),fpin))
197 break;
198 str[8191] = 0;
199 if(*str &&
200 str[strlen(str) - 1] == '\n')
201 str[strlen(str) - 1] = 0;
202 len = (*str) ? literal(str) : 0;
203 /* write string to output file, including terminating NULL */
204 thislen = fwrite(str,1,len + 1,fpout);
205 if(thislen != len + 1) {
206 printf("\n **Write error on %s!\n",outfile);
207 fclose(fpin);
208 fclose(fpout);
209 remove(outfile);
210 return 1;
211 }
212 totallen += thislen;
213 x++;
214 }
215 if(x > IDS_NUMSTRS)
216 printf("\n **Warning: There are more strings in file "
217 "than there should be -- should be %lu, are %lu.\n",
218 IDS_NUMSTRS,
219 x);
220 else if(x < IDS_NUMSTRS) {
221 printf("\n **Error -- insufficient strings in file -- "
222 "should be %lu, are %lu.\n",
223 IDS_NUMSTRS,
224 x);
225 fclose(fpin);
226 fclose(fpout);
227 remove(outfile);
228 return 1;
229 }
230 /* start output file with number of strings (long),
231 * size of text (long), and version (2 shorts) */
232 if(fseek(fpout,0,SEEK_SET) ||
233 fwrite(&x,sizeof(x),1,fpout) != 1 ||
234 fwrite(&totallen,sizeof(totallen),1,fpout) != 1 ||
235 fwrite(&vermajor,sizeof(vermajor),1,fpout) != 1 ||
236 fwrite(&verminor,sizeof(verminor),1,fpout) != 1) {
237 printf("\n **Error -- bad seek or write to %s!\n",outfile);
238 fclose(fpin);
239 fclose(fpout);
240 remove(outfile);
241 return 1;
242 }
243 fclose(fpout);
244 printf("\n%s successfully written from %s (%lu strings).\n",
245 outfile,infile,x);
246 }
247 else
248 printf("\n **Can't create %s!\n",outfile);
249 fclose(fpin);
250 }
251 else
252 printf("\n **Can't open %s!\n",infile);
253 return 0;
254}
255
Note: See TracBrowser for help on using the repository browser.