| 1 | 
 | 
|---|
| 2 | /***********************************************************************
 | 
|---|
| 3 | 
 | 
|---|
| 4 |   $Id: string.c 551 2007-02-28 01:33:51Z gyoung $
 | 
|---|
| 5 | 
 | 
|---|
| 6 |   External strings file support
 | 
|---|
| 7 | 
 | 
|---|
| 8 |   Copyright (c) 1993-98 M. Kimes
 | 
|---|
| 9 |   Copyright (c) 2006 Steven H. Levine
 | 
|---|
| 10 | 
 | 
|---|
| 11 |   22 Jul 06 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 <share.h>
 | 
|---|
| 23 | #include <io.h>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "fm3dll.h"
 | 
|---|
| 26 | #include "fm3str.h"
 | 
|---|
| 27 | #include "version.h"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #pragma alloc_text(STRINGS,LoadStrings,GetPString)
 | 
|---|
| 30 | 
 | 
|---|
| 31 | static char **strs, *str;
 | 
|---|
| 32 | static ULONG numStr;
 | 
|---|
| 33 | 
 | 
|---|
| 34 | //== LoadStrings() load strings from file ==
 | 
|---|
| 35 | 
 | 
|---|
| 36 | BOOL LoadStrings(char *filename)
 | 
|---|
| 37 | {
 | 
|---|
| 38 |   BOOL ok = FALSE;
 | 
|---|
| 39 |   ULONG size, len, totalsize;
 | 
|---|
| 40 |   USHORT vermajor = 0, verminor = 0;
 | 
|---|
| 41 |   register char *p;
 | 
|---|
| 42 |   register ULONG x;
 | 
|---|
| 43 |   FILE *fp;
 | 
|---|
| 44 |   APIRET rc;
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   /* Load strings from requested file or FM3RES.STR
 | 
|---|
| 47 |    * with some quiet error-checking.
 | 
|---|
| 48 |    * Return TRUE on success, FALSE on error.
 | 
|---|
| 49 |    */
 | 
|---|
| 50 | 
 | 
|---|
| 51 |   if (!filename)
 | 
|---|
| 52 |     filename = "FM3RES.STR";
 | 
|---|
| 53 |   numStr = 0;
 | 
|---|
| 54 |   if (str)
 | 
|---|
| 55 |     DosFreeMem(str);
 | 
|---|
| 56 |   strs = NULL;
 | 
|---|
| 57 |   str = NULL;
 | 
|---|
| 58 | 
 | 
|---|
| 59 |   fp = _fsopen(filename, "rb", SH_DENYWR);
 | 
|---|
| 60 |   if (fp) {
 | 
|---|
| 61 |     if (fread(&numStr,
 | 
|---|
| 62 |               sizeof(numStr),
 | 
|---|
| 63 |               1,
 | 
|---|
| 64 |               fp) &&
 | 
|---|
| 65 |         numStr == IDS_NUMSTRS &&
 | 
|---|
| 66 |         fread(&len, sizeof(len), 1, fp) &&
 | 
|---|
| 67 |         fread(&vermajor, sizeof(vermajor), 1, fp) &&
 | 
|---|
| 68 |         fread(&verminor, sizeof(verminor), 1, fp) &&
 | 
|---|
| 69 |         (vermajor >= VERMAJORBREAK &&
 | 
|---|
| 70 |          (vermajor > VERMAJORBREAK || verminor >= VERMINORBREAK))) {
 | 
|---|
| 71 |       fseek(fp, 0, SEEK_END);
 | 
|---|
| 72 |       size = ftell(fp) - ((sizeof(ULONG) * 2) + (sizeof(USHORT) * 2));
 | 
|---|
| 73 |       if (size && size == len) {
 | 
|---|
| 74 |         fseek(fp, (sizeof(ULONG) * 2) + (sizeof(USHORT) * 2), SEEK_SET);
 | 
|---|
| 75 |         /* NOTE:  Make one memory object for both str and strs
 | 
|---|
| 76 |          * for efficiency.
 | 
|---|
| 77 |          */
 | 
|---|
| 78 |         totalsize = size + sizeof(ULONG);
 | 
|---|
| 79 |         totalsize += (totalsize % sizeof(ULONG));
 | 
|---|
| 80 |         len = totalsize;
 | 
|---|
| 81 |         totalsize += (numStr * sizeof(char *));
 | 
|---|
| 82 |         totalsize += 4;
 | 
|---|
| 83 |         rc = DosAllocMem((PPVOID) & str, totalsize,
 | 
|---|
| 84 |                          PAG_COMMIT | PAG_READ | PAG_WRITE);
 | 
|---|
| 85 |         if (!rc && str) {
 | 
|---|
| 86 |           strs = (char **)(str + len);
 | 
|---|
| 87 |           if (fread(str, 1, size, fp) == size) {
 | 
|---|
| 88 |             p = str;
 | 
|---|
| 89 |             for (x = 0; x < numStr; x++) {
 | 
|---|
| 90 |               if (p - str >= size)
 | 
|---|
| 91 |                 break;
 | 
|---|
| 92 |               strs[x] = p;
 | 
|---|
| 93 |               while (*p)
 | 
|---|
| 94 |                 p++;
 | 
|---|
| 95 |               p++;
 | 
|---|
| 96 |             }
 | 
|---|
| 97 |             if (x == numStr)
 | 
|---|
| 98 |               ok = TRUE;
 | 
|---|
| 99 |           }
 | 
|---|
| 100 |           if (ok)
 | 
|---|
| 101 |             /* set pages to readonly */
 | 
|---|
| 102 |             DosSetMem(str, totalsize, PAG_COMMIT | PAG_READ);
 | 
|---|
| 103 |         }
 | 
|---|
| 104 |       }
 | 
|---|
| 105 |     }
 | 
|---|
| 106 |     fclose(fp);
 | 
|---|
| 107 |   }
 | 
|---|
| 108 | 
 | 
|---|
| 109 |   if (!ok) {
 | 
|---|
| 110 |     numStr = 0;
 | 
|---|
| 111 |     if (str)
 | 
|---|
| 112 |       DosFreeMem(str);
 | 
|---|
| 113 |     str = NULL;
 | 
|---|
| 114 |     strs = NULL;
 | 
|---|
| 115 |   }
 | 
|---|
| 116 | 
 | 
|---|
| 117 |   return ok;
 | 
|---|
| 118 | }
 | 
|---|
| 119 | 
 | 
|---|
| 120 | //== GetPString() return a readonly pointer to the requested string in memory ==
 | 
|---|
| 121 | 
 | 
|---|
| 122 | char *GetPString(ULONG id)
 | 
|---|
| 123 | {
 | 
|---|
| 124 |   return id < numStr && str && strs && strs[id] ? strs[id] : NullStr;
 | 
|---|
| 125 | }
 | 
|---|
| 126 | 
 | 
|---|
| 127 | //== StringsLoaded() return TRUE is strings loaded
 | 
|---|
| 128 | 
 | 
|---|
| 129 | BOOL StringsLoaded(void)
 | 
|---|
| 130 | {
 | 
|---|
| 131 |   return numStr && str && strs;
 | 
|---|
| 132 | }
 | 
|---|