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