1 |
|
---|
2 | /***********************************************************************
|
---|
3 |
|
---|
4 | $Id: string.c 333 2006-07-25 18:58:11Z root $
|
---|
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 ||
|
---|
71 | verminor >= VERMINORBREAK))) {
|
---|
72 | fseek(fp,0,SEEK_END);
|
---|
73 | size = ftell(fp) - ((sizeof(ULONG) * 2) + (sizeof(USHORT) * 2));
|
---|
74 | if (size && size == len) {
|
---|
75 | fseek(fp,(sizeof(ULONG) * 2) + (sizeof(USHORT) * 2),SEEK_SET);
|
---|
76 | /* NOTE: Make one memory object for both str and strs
|
---|
77 | * for efficiency.
|
---|
78 | */
|
---|
79 | totalsize = size + sizeof(ULONG);
|
---|
80 | totalsize += (totalsize % sizeof(ULONG));
|
---|
81 | len = totalsize;
|
---|
82 | totalsize += (numStr * sizeof(char *));
|
---|
83 | totalsize += 4;
|
---|
84 | rc = DosAllocMem((PPVOID)&str,totalsize,
|
---|
85 | PAG_COMMIT | PAG_READ | PAG_WRITE);
|
---|
86 | if (!rc && str) {
|
---|
87 | strs = (char **)(str + len);
|
---|
88 | if(fread(str,1,size,fp) == size) {
|
---|
89 | p = str;
|
---|
90 | for(x = 0;x < numStr;x++) {
|
---|
91 | if(p - str >= size)
|
---|
92 | break;
|
---|
93 | strs[x] = p;
|
---|
94 | while(*p)
|
---|
95 | p++;
|
---|
96 | p++;
|
---|
97 | }
|
---|
98 | if(x == numStr)
|
---|
99 | ok = TRUE;
|
---|
100 | }
|
---|
101 | if(ok)
|
---|
102 | /* set pages to readonly */
|
---|
103 | DosSetMem(str,totalsize,PAG_COMMIT | PAG_READ);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | fclose(fp);
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (!ok) {
|
---|
111 | numStr = 0;
|
---|
112 | if(str)
|
---|
113 | DosFreeMem(str);
|
---|
114 | str = NULL;
|
---|
115 | strs = NULL;
|
---|
116 | }
|
---|
117 |
|
---|
118 | return ok;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | //== GetPString() return a readonly pointer to the requested string in memory ==
|
---|
123 |
|
---|
124 | char *GetPString (ULONG id)
|
---|
125 | {
|
---|
126 | return id < numStr && str && strs && strs[id] ? strs[id] : NullStr;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | //== StringsLoaded() return TRUE is strings loaded
|
---|
131 |
|
---|
132 | BOOL StringsLoaded (void)
|
---|
133 | {
|
---|
134 | return numStr && str && strs;
|
---|
135 | }
|
---|