| 1 |  | 
|---|
| 2 | /*********************************************************************** | 
|---|
| 3 |  | 
|---|
| 4 | $Id: mkstr.c 551 2007-02-28 01:33:51Z gyoung $ | 
|---|
| 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 | #define INCL_DOS | 
|---|
| 16 | #define INCL_WIN | 
|---|
| 17 | #include <os2.h> | 
|---|
| 18 |  | 
|---|
| 19 | #include <stdlib.h> | 
|---|
| 20 | #include <stdio.h> | 
|---|
| 21 | #include <string.h> | 
|---|
| 22 | #include <ctype.h> | 
|---|
| 23 |  | 
|---|
| 24 | #include "..\version.h" | 
|---|
| 25 | #include "..\fm3str.h" | 
|---|
| 26 |  | 
|---|
| 27 | /* | 
|---|
| 28 | * Creates indexed FM3RES.STR file from text file FM3DLL.STR. | 
|---|
| 29 | * FM3DLL.STR contains text strings, one per line, for use by | 
|---|
| 30 | * FM3DLL.DLL. | 
|---|
| 31 | */ | 
|---|
| 32 |  | 
|---|
| 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 | } | 
|---|
| 43 |  | 
|---|
| 44 | /* | 
|---|
| 45 | * literal.c | 
|---|
| 46 | * Translate a string with tokens in it according to several conventions: | 
|---|
| 47 | * 1.  \x1b translates to char(0x1b) | 
|---|
| 48 | * 2.  \27  translates to char(27) | 
|---|
| 49 | * 3.  \"   translates to " | 
|---|
| 50 | * 4.  \'   translates to ' | 
|---|
| 51 | * 5.  \\   translates to \ | 
|---|
| 52 | * 6.  \r   translates to carriage return | 
|---|
| 53 | * 7.  \n   translates to linefeed | 
|---|
| 54 | * 8.  \b   translates to backspace | 
|---|
| 55 | * 9.  \t   translates to tab | 
|---|
| 56 | * 10. \a   translates to bell | 
|---|
| 57 | * 11. \f   translates to formfeed | 
|---|
| 58 | * | 
|---|
| 59 | * Synopsis | 
|---|
| 60 | *    *s = "this\x20is\32a test of \\MSC\\CSM\7" | 
|---|
| 61 | *    literal(s); | 
|---|
| 62 | * | 
|---|
| 63 | *    ( s now equals "this is a test of \MSC\CSM") | 
|---|
| 64 | */ | 
|---|
| 65 |  | 
|---|
| 66 | #define HEX "0123456789ABCDEF" | 
|---|
| 67 | #define DEC "0123456789" | 
|---|
| 68 |  | 
|---|
| 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 | 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 | int main(void) | 
|---|
| 182 | { | 
|---|
| 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, verminor); | 
|---|
| 195 | /* open input file */ | 
|---|
| 196 | fpin = fopen(infile, "r"); | 
|---|
| 197 | if (fpin) { | 
|---|
| 198 | /* open (create) output file */ | 
|---|
| 199 | fpout = fopen(outfile, "wb"); | 
|---|
| 200 | if (fpout) { | 
|---|
| 201 | /* skip space for numstrs and textlen parameter in outfile */ | 
|---|
| 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; | 
|---|
| 208 | } | 
|---|
| 209 | /* step through strings, reading from input, writing to output */ | 
|---|
| 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; | 
|---|
| 240 | } | 
|---|
| 241 | /* start output file with number of strings (long), | 
|---|
| 242 | * size of text (long), and version (2 shorts) */ | 
|---|
| 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; | 
|---|
| 253 | } | 
|---|
| 254 | fclose(fpout); | 
|---|
| 255 | printf("\n%s successfully written from %s (%lu strings).\n", | 
|---|
| 256 | outfile, infile, x); | 
|---|
| 257 | } | 
|---|
| 258 | else | 
|---|
| 259 | printf("\n **Can't create %s!\n", outfile); | 
|---|
| 260 | fclose(fpin); | 
|---|
| 261 | } | 
|---|
| 262 | else | 
|---|
| 263 | printf("\n **Can't open %s!\n", infile); | 
|---|
| 264 | return 0; | 
|---|
| 265 | } | 
|---|