source: trunk/dll/strutil.c@ 1213

Last change on this file since 1213 was 1213, checked in by John Small, 17 years ago

Ticket 187: Move data declarations/definitions out of fm3dll.h

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