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