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
|
---|
12 | 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
|
---|
13 |
|
---|
14 | ***********************************************************************/
|
---|
15 |
|
---|
16 | #define INCL_DOS
|
---|
17 | #define INCL_WIN
|
---|
18 | #include <os2.h>
|
---|
19 |
|
---|
20 | #include <stdlib.h>
|
---|
21 | #include <stdio.h>
|
---|
22 | #include <string.h>
|
---|
23 | #include <share.h>
|
---|
24 | #include <io.h>
|
---|
25 |
|
---|
26 | #include "fm3dll.h"
|
---|
27 | #include "fm3str.h"
|
---|
28 | #include "version.h"
|
---|
29 |
|
---|
30 | static char **strs, *str;
|
---|
31 | static ULONG numStr;
|
---|
32 |
|
---|
33 | //== LoadStrings() load strings from file ==
|
---|
34 |
|
---|
35 | BOOL LoadStrings(char *filename)
|
---|
36 | {
|
---|
37 | BOOL ok = FALSE;
|
---|
38 | ULONG size, len, totalsize;
|
---|
39 | USHORT vermajor = 0, verminor = 0;
|
---|
40 | register char *p;
|
---|
41 | register ULONG x;
|
---|
42 | FILE *fp;
|
---|
43 | APIRET rc;
|
---|
44 |
|
---|
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)
|
---|
51 | filename = "FM3RES.STR";
|
---|
52 | numStr = 0;
|
---|
53 | if (str)
|
---|
54 | DosFreeMem(str);
|
---|
55 | strs = NULL;
|
---|
56 | str = NULL;
|
---|
57 |
|
---|
58 | fp = _fsopen(filename, "rb", SH_DENYWR);
|
---|
59 | if (fp) {
|
---|
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);
|
---|
71 | size = ftell(fp) - ((sizeof(ULONG) * 2) + (sizeof(USHORT) * 2));
|
---|
72 | if (size && size == len) {
|
---|
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);
|
---|
84 | if (!rc && str) {
|
---|
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 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | fclose(fp);
|
---|
106 | }
|
---|
107 |
|
---|
108 | if (!ok) {
|
---|
109 | numStr = 0;
|
---|
110 | if (str)
|
---|
111 | DosFreeMem(str);
|
---|
112 | str = NULL;
|
---|
113 | strs = NULL;
|
---|
114 | }
|
---|
115 |
|
---|
116 | return ok;
|
---|
117 | }
|
---|
118 |
|
---|
119 | //== GetPString() return a readonly pointer to the requested string in memory ==
|
---|
120 |
|
---|
121 | char *GetPString(ULONG id)
|
---|
122 | {
|
---|
123 | return id < numStr && str && strs && strs[id] ? strs[id] : NullStr;
|
---|
124 | }
|
---|
125 |
|
---|
126 | //== StringsLoaded() return TRUE is strings loaded
|
---|
127 |
|
---|
128 | BOOL StringsLoaded(void)
|
---|
129 | {
|
---|
130 | return numStr && str && strs;
|
---|
131 | }
|
---|
132 |
|
---|
133 | #pragma alloc_text(STRINGS,LoadStrings,GetPString)
|
---|