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

Last change on this file since 536 was 243, checked in by root, 20 years ago

Comments

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