Changeset 551 for trunk/dll/internal/mkstr.c
- Timestamp:
- Feb 28, 2007, 2:33:51 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/dll/internal/mkstr.c
r243 r551 12 12 13 13 ***********************************************************************/ 14 15 14 16 15 #define INCL_DOS … … 32 31 */ 33 32 34 int 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); 33 int index(const char *s, const char c) 34 { 35 36 char *p; 37 38 p = strchr(s, c); 39 if (p == NULL || !*p) 40 return -1; 41 return (int)(p - s); 42 42 } 43 44 43 45 44 /* … … 68 67 #define DEC "0123456789" 69 68 70 int literal (char *fsource) { 71 72 register int wpos,w; 73 int len,oldw; 74 char *fdestin,*freeme,wchar; 75 76 if(!fsource || !*fsource) 69 int literal(char *fsource) 70 { 71 72 register int wpos, w; 73 int len, oldw; 74 char *fdestin, *freeme, wchar; 75 76 if (!fsource || !*fsource) 77 77 return 0; 78 78 len = strlen(fsource) + 1; 79 79 freeme = fdestin = malloc(len + 1); 80 memset(fdestin, 0,len);/* start out with zero'd string */81 82 w = 0; 83 while (fsource[w]) {/* until end of string */84 switch (fsource[w]) {85 86 switch(fsource[w + 1]) {87 case 'x' :/* hexadecimal */88 89 w += 2;/* get past "\x" */90 if(index(HEX,(char)toupper(fsource[w])) != -1) {91 92 while(((wpos = index(HEX,(char)toupper(fsource[w]))) != -1) &&93 94 95 96 97 98 99 wchar = 'x';/* just an x */100 101 102 103 104 case '\\' :/* we want a "\" */105 106 107 108 109 case 't' :/* tab char */110 111 112 113 114 case 'n' :/* new line */115 116 117 118 119 case 'r' :/* carr return */120 121 122 123 124 case 'b' :/* back space */125 126 127 128 129 case 'f':/* formfeed */130 131 132 133 134 case 'a':/* bell */135 136 137 138 139 case '\'' :/* single quote */140 141 142 143 144 case '\"' :/* double quote */145 146 147 148 149 default :/* decimal */150 w++;/* get past "\" */151 152 if(index(DEC,fsource[w]) != -1) {153 154 do {/* cvt to binary */155 156 } while (index(DEC,fsource[w]) != -1 && w < oldw + 3);157 158 159 160 161 *fdestin++ = wchar;162 163 164 165 166 default:167 168 169 }170 w++;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 171 } 172 *fdestin = 0; 172 *fdestin = 0; /* terminate the string */ 173 173 174 174 len = fdestin - freeme; 175 memcpy(fsource, freeme,len + 1);/* swap 'em */175 memcpy(fsource, freeme, len + 1); /* swap 'em */ 176 176 free(freeme); 177 177 178 return len; 178 return len; /* return length of string */ 179 179 } 180 180 181 182 int main (void){183 184 FILE *fpin,*fpout;185 ULONG len,x = 0,totallen = 0,thislen;186 USHORT vermajor = 0,verminor = 0;181 int main(void) 182 { 183 184 FILE *fpin, *fpout; 185 ULONG len, x = 0, totallen = 0, thislen; 186 USHORT vermajor = 0, verminor = 0; 187 187 static char str[8192]; 188 char *outfile = "FM3RES.STR",*infile = "FM3DLL.STR";188 char *outfile = "FM3RES.STR", *infile = "FM3DLL.STR"; 189 189 190 190 vermajor = VERMAJOR; 191 191 verminor = VERMINOR; 192 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); 193 "\n Compiles FM3DLL.STR into FM3RES.STR. For FM/2 version %d.%d.\n", 194 vermajor, verminor); 196 195 /* open input file */ 197 fpin = fopen(infile, "r");198 if (fpin) {196 fpin = fopen(infile, "r"); 197 if (fpin) { 199 198 /* open (create) output file */ 200 fpout = fopen(outfile, "wb");201 if (fpout) {199 fpout = fopen(outfile, "wb"); 200 if (fpout) { 202 201 /* 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 206 207 208 202 if (fseek(fpout, (sizeof(ULONG) * 2) + (sizeof(USHORT) * 2), SEEK_SET)) { 203 printf("\n **Seek error on %s!\n", outfile); 204 fclose(fpin); 205 fclose(fpout); 206 remove(outfile); 207 return 1; 209 208 } 210 209 /* 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; 210 while (!feof(fpin)) { 211 if (!fgets(str, sizeof(str), fpin)) 212 break; 213 str[8191] = 0; 214 if (*str && str[strlen(str) - 1] == '\n') 215 str[strlen(str) - 1] = 0; 216 len = (*str) ? literal(str) : 0; 217 /* write string to output file, including terminating NULL */ 218 thislen = fwrite(str, 1, len + 1, fpout); 219 if (thislen != len + 1) { 220 printf("\n **Write error on %s!\n", outfile); 221 fclose(fpin); 222 fclose(fpout); 223 remove(outfile); 224 return 1; 225 } 226 totallen += thislen; 227 x++; 228 } 229 if (x > IDS_NUMSTRS) 230 printf("\n **Warning: There are more strings in file " 231 "than there should be -- should be %lu, are %lu.\n", 232 IDS_NUMSTRS, x); 233 else if (x < IDS_NUMSTRS) { 234 printf("\n **Error -- insufficient strings in file -- " 235 "should be %lu, are %lu.\n", IDS_NUMSTRS, x); 236 fclose(fpin); 237 fclose(fpout); 238 remove(outfile); 239 return 1; 245 240 } 246 241 /* start output file with number of strings (long), 247 242 * 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 255 256 257 243 if (fseek(fpout, 0, SEEK_SET) || 244 fwrite(&x, sizeof(x), 1, fpout) != 1 || 245 fwrite(&totallen, sizeof(totallen), 1, fpout) != 1 || 246 fwrite(&vermajor, sizeof(vermajor), 1, fpout) != 1 || 247 fwrite(&verminor, sizeof(verminor), 1, fpout) != 1) { 248 printf("\n **Error -- bad seek or write to %s!\n", outfile); 249 fclose(fpin); 250 fclose(fpout); 251 remove(outfile); 252 return 1; 258 253 } 259 254 fclose(fpout); 260 255 printf("\n%s successfully written from %s (%lu strings).\n", 261 outfile,infile,x);256 outfile, infile, x); 262 257 } 263 258 else 264 printf("\n **Can't create %s!\n", outfile);259 printf("\n **Can't create %s!\n", outfile); 265 260 fclose(fpin); 266 261 } 267 262 else 268 printf("\n **Can't open %s!\n", infile);263 printf("\n **Can't open %s!\n", infile); 269 264 return 0; 270 265 } 271
Note:
See TracChangeset
for help on using the changeset viewer.