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