Subversion Repositories SvarDOS

Rev

Rev 1281 | Rev 1293 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1281 Rev 1290
Line 46... Line 46...
46
#include "svarlang.h"
46
#include "svarlang.h"
47
 
47
 
48
 
48
 
49
/* supplied through DEFLANG.C */
49
/* supplied through DEFLANG.C */
50
extern char svarlang_mem[];
50
extern char svarlang_mem[];
-
 
51
extern unsigned short svarlang_dict[];
51
extern const unsigned short svarlang_memsz;
52
extern const unsigned short svarlang_memsz;
52
 
-
 
-
 
53
extern const unsigned short svarlang_string_count;
53
 
54
 
54
const char *svarlang_strid(unsigned short id) {
55
const char *svarlang_strid(unsigned short id) {
55
  const char *ptr = svarlang_mem;
56
   size_t left = 0, right = svarlang_string_count - 1, x;
-
 
57
   unsigned short v;
-
 
58
 
56
  /* find the string id in langblock memory */
59
   if (svarlang_string_count == 0) return "";
-
 
60
 
57
  for (;;) {
61
   while (left <= right ) {
58
    if (((unsigned short *)ptr)[0] == id) return(ptr + 4);
62
      x = left + ( (right - left ) >> 2 );
59
    if (((unsigned short *)ptr)[1] == 0) return(ptr + 2); /* end of strings - return an empty string */
63
      v = svarlang_dict[x * 2];
-
 
64
      if ( id == v )  {
60
    ptr += ((unsigned short *)ptr)[1] + 4;
65
        return svarlang_mem + svarlang_dict[x * 2 + 1];
61
  }
66
      }
-
 
67
      else if ( id > v ) left = x + 1;
-
 
68
      else right = x - 1;
-
 
69
   }
-
 
70
   return "";
62
}
71
}
63
 
72
 
64
/* routines below are simplified (dos-based) versions of the libc FILE-related
73
/* routines below are simplified (dos-based) versions of the libc FILE-related
65
 * functions. Using them avoids a dependency on FILE, hence makes the binary
74
 * functions. Using them avoids a dependency on FILE, hence makes the binary
66
 * smaller if the application does not need to pull fopen() and friends */
75
 * smaller if the application does not need to pull fopen() and friends */
Line 155... Line 164...
155
}
164
}
156
#endif
165
#endif
157
 
166
 
158
int svarlang_load(const char *fname, const char *lang) {
167
int svarlang_load(const char *fname, const char *lang) {
159
  unsigned short langid;
168
  unsigned short langid;
160
  char hdr[4];
169
  char hdr[5];
161
  unsigned short buff16[2];
170
  unsigned short buff16[2];
162
  FHANDLE fd;
171
  FHANDLE fd;
-
 
172
  unsigned short string_count;
163
 
173
 
164
  langid = *((unsigned short *)lang);
174
  langid = *((unsigned short *)lang);
165
  langid &= 0xDFDF; /* make sure lang is upcase */
175
  langid &= 0xDFDF; /* make sure lang is upcase */
166
 
176
 
167
  fd = FOPEN(fname);
177
  fd = FOPEN(fname);
168
  if (!fd) return(-1);
178
  if (!fd) return(-1);
169
 
179
 
170
  /* read hdr, should be "SvL\33" */
180
  /* read hdr, should be "SvL1\x1a" */
171
  if ((FREAD(fd, hdr, 4) != 4) || (memcmp(hdr, "SvL\33", 4) != 0)) {
181
  if ((FREAD(fd, hdr, 5) != 5) || (memcmp(hdr, "SvL1\x1a", 5) != 0)) {
172
    FCLOSE(fd);
182
    FCLOSE(fd);
173
    return(-3);
183
    return(-3);
174
  }
184
  }
175
 
185
 
-
 
186
  /* read string count */
-
 
187
  if ((FREAD(fd, &string_count, 2) != 2) || (string_count != svarlang_string_count)) {
-
 
188
    FCLOSE(fd);
-
 
189
    return(-6);
-
 
190
  }
-
 
191
 
176
  /* read next lang id in file */
192
  /* read next lang id and string table size in file */
177
  while (FREAD(fd, buff16, 4) == 4) {
193
  while (FREAD(fd, buff16, 4) == 4) {
178
 
194
 
179
    /* is it the lang I am looking for? */
195
    /* is it the lang I am looking for? */
180
    if (buff16[0] != langid) { /* skip to next lang */
196
    if (buff16[0] != langid) { /* skip to next lang */
-
 
197
      FSEEK(fd, svarlang_string_count * 4);
181
      FSEEK(fd, buff16[1]);
198
      FSEEK(fd, buff16[1]);
182
      continue;
199
      continue;
183
    }
200
    }
184
 
201
 
185
    /* found - but do I have enough memory space? */
202
    /* found - but do I have enough memory space? */
186
    if (buff16[1] >= svarlang_memsz) {
203
    if (buff16[1] >= svarlang_memsz) {
187
      FCLOSE(fd);
204
      FCLOSE(fd);
188
      return(-4);
205
      return(-4);
189
    }
206
    }
190
 
207
 
191
    /* load strings */
208
    /* load dictionary & strings */
-
 
209
    if ((FREAD(fd, svarlang_dict, svarlang_string_count * 4) != svarlang_string_count * 4) ||
192
    if (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1]) break;
210
       (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1])) {
-
 
211
      FCLOSE(fd);
-
 
212
      return -7;
-
 
213
    }
193
    FCLOSE(fd);
214
    FCLOSE(fd);
194
    return(0);
215
    return(0);
195
  }
216
  }
196
 
217
 
197
  FCLOSE(fd);
218
  FCLOSE(fd);