source: trunk/dll/strutil.c@ 907

Last change on this file since 907 was 907, checked in by Steven Levine, 18 years ago

Avoid out of memory traps in Compare Directories
Rework Compare Directories progress display for 2 second update rate
Start refactoring to reduce dependence on fm3dll.h
Add timer services (IsITimerExpired etc.)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 KB
RevLine 
[333]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
[907]9 Copyright (c) 2006, 2008 Steven H. Levine
[333]10
11 22 Jul 06 SHL Comments
[793]12 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
[907]13 05 Jan 08 SHL Rename from string.c to avoid string.h conflict
[333]14
15***********************************************************************/
16
[2]17#include <stdio.h>
18#include <share.h>
[333]19
[907]20#include "strutil.h"
[2]21#include "fm3str.h"
22#include "version.h"
23
[907]24extern PSZ NullStr; // 05 Jan 08 SHL fixme to be in some .h
25
[551]26static char **strs, *str;
27static ULONG numStr;
[2]28
[333]29//== LoadStrings() load strings from file ==
[2]30
[551]31BOOL LoadStrings(char *filename)
[333]32{
[551]33 BOOL ok = FALSE;
34 ULONG size, len, totalsize;
35 USHORT vermajor = 0, verminor = 0;
[2]36 register char *p;
37 register ULONG x;
[551]38 FILE *fp;
39 APIRET rc;
[2]40
[333]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)
[2]47 filename = "FM3RES.STR";
48 numStr = 0;
[551]49 if (str)
[2]50 DosFreeMem(str);
51 strs = NULL;
52 str = NULL;
53
[551]54 fp = _fsopen(filename, "rb", SH_DENYWR);
[333]55 if (fp) {
[551]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);
[2]67 size = ftell(fp) - ((sizeof(ULONG) * 2) + (sizeof(USHORT) * 2));
[333]68 if (size && size == len) {
[551]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);
[333]80 if (!rc && str) {
[551]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 }
[2]99 }
100 }
101 fclose(fp);
102 }
103
[333]104 if (!ok) {
[2]105 numStr = 0;
[551]106 if (str)
[2]107 DosFreeMem(str);
108 str = NULL;
109 strs = NULL;
110 }
111
[333]112 return ok;
[2]113}
114
[333]115//== GetPString() return a readonly pointer to the requested string in memory ==
[2]116
[551]117char *GetPString(ULONG id)
[333]118{
119 return id < numStr && str && strs && strs[id] ? strs[id] : NullStr;
[2]120}
121
[333]122//== StringsLoaded() return TRUE is strings loaded
[2]123
[551]124BOOL StringsLoaded(void)
[333]125{
[551]126 return numStr && str && strs;
[2]127}
[793]128
129#pragma alloc_text(STRINGS,LoadStrings,GetPString)
Note: See TracBrowser for help on using the repository browser.