Subversion Repositories SvarDOS

Rev

Rev 1780 | Rev 1782 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1290 bernd.boec 1
/* This file is part of the svarlang project and is published under the terms
2
 * of the MIT license.
3
 *
1779 mateusz.vi 4
 * Copyright (C) 2021-2024 Mateusz Viste
1290 bernd.boec 5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
23
 */
24
 
25
/* if WITHSTDIO is enabled, then remap file operations to use the standard
26
 * stdio amenities */
27
#ifdef WITHSTDIO
28
 
29
#include <stdio.h>   /* FILE, fopen(), fseek(), etc */
30
typedef FILE* FHANDLE;
31
#define FOPEN(x) fopen(x, "rb")
32
#define FCLOSE(x) fclose(x)
33
#define FSEEK(f,b) fseek(f,b,SEEK_CUR)
34
#define FREAD(f,t,b) fread(t, 1, b, f)
35
 
36
#else
37
 
1300 mateusz.vi 38
#include <i86.h>  /* FP_SEG, FP_OFF */
1290 bernd.boec 39
typedef unsigned short FHANDLE;
40
 
41
#endif
42
 
43
 
44
#include "svarlang.h"
45
 
46
 
47
/* supplied through DEFLANG.C */
48
extern char svarlang_mem[];
49
extern unsigned short svarlang_dict[];
50
extern const unsigned short svarlang_memsz;
51
extern const unsigned short svarlang_string_count;
52
 
1298 mateusz.vi 53
 
1290 bernd.boec 54
const char *svarlang_strid(unsigned short id) {
1299 mateusz.vi 55
  unsigned short left = 0, right = svarlang_string_count - 1, x;
1298 mateusz.vi 56
  unsigned short v;
1290 bernd.boec 57
 
1298 mateusz.vi 58
  if (svarlang_string_count == 0) return("");
1290 bernd.boec 59
 
1298 mateusz.vi 60
  while (left <= right) {
1299 mateusz.vi 61
    x = left + ((right - left ) >> 2);
1298 mateusz.vi 62
    v = svarlang_dict[x * 2];
63
 
64
    if (id == v) return(svarlang_mem + svarlang_dict[x * 2 + 1]);
65
 
66
    if (id > v) {
67
      left = x + 1;
68
    } else {
69
      right = x - 1;
70
    }
71
  }
72
 
73
  return("");
1290 bernd.boec 74
}
75
 
1293 mateusz.vi 76
 
1290 bernd.boec 77
/* routines below are simplified (dos-based) versions of the libc FILE-related
78
 * functions. Using them avoids a dependency on FILE, hence makes the binary
1779 mateusz.vi 79
 * smaller if the application does not need to pull fopen() and friends
80
 * I use pragma aux directives for more compact size. open-watcom only. */
1290 bernd.boec 81
#ifndef WITHSTDIO
82
 
1781 mateusz.vi 83
static unsigned short FOPEN(const char far *s);
1290 bernd.boec 84
 
1779 mateusz.vi 85
#pragma aux FOPEN = \
86
"push ds" \
1781 mateusz.vi 87
"push es" \
88
"pop ds" \
1779 mateusz.vi 89
"mov ax, 0x3D00" /* open file, read-only (fname at DS:DX) */ \
90
"int 0x21" \
91
"jnc DONE" \
92
"xor ax, ax" \
93
"DONE:" \
94
"pop ds" \
1781 mateusz.vi 95
parm [es dx] \
1779 mateusz.vi 96
value [ax];
1290 bernd.boec 97
 
98
 
1780 mateusz.vi 99
static void FCLOSE(unsigned short handle);
1290 bernd.boec 100
 
1780 mateusz.vi 101
#pragma aux FCLOSE = \
102
"mov ah, 0x3E" \
103
"int 0x21" \
104
modify [ax]  /* AX might contain an error code on failure */ \
105
parm [bx]
1290 bernd.boec 106
 
1780 mateusz.vi 107
 
1290 bernd.boec 108
static unsigned short FREAD(unsigned short handle, void *buff, unsigned short bytes) {
109
  unsigned short buff_seg = FP_SEG(buff);
110
  unsigned short buff_off = FP_OFF(buff);
111
  unsigned short res = 0;
112
 
113
  _asm {
114
    push bx
115
    push cx
116
    push dx
117
 
118
    mov bx, handle
119
    mov cx, bytes
120
    mov dx, buff_off
121
    mov ax, buff_seg
122
    push ds
123
    mov ds, ax
124
    mov ah, 0x3f    /* read cx bytes from file handle bx to DS:DX */
125
    int 0x21
126
    pop ds
127
    jc ERR
128
 
129
    mov res, ax
130
    ERR:
131
 
132
    pop dx
133
    pop cx
134
    pop bx
135
  }
136
 
137
  return(res);
138
}
139
 
140
 
1781 mateusz.vi 141
static void FSEEK(unsigned short handle, unsigned short bytes);
142
 
143
#pragma aux FSEEK = \
144
"mov ax, 0x4201"  /* move file pointer from cur pos + CX:DX */ \
145
"xor cx, cx" \
146
"int 0x21" \
147
parm [bx] [dx] \
148
modify [ax cx dx]
149
 
1290 bernd.boec 150
#endif
151
 
1293 mateusz.vi 152
 
1290 bernd.boec 153
int svarlang_load(const char *fname, const char *lang) {
154
  unsigned short langid;
155
  unsigned short buff16[2];
156
  FHANDLE fd;
1376 mateusz.vi 157
  signed char exitcode = 0;
1373 mateusz.vi 158
  struct {
159
    unsigned long sig;
160
    unsigned short string_count;
161
  } hdr;
1290 bernd.boec 162
 
163
  langid = *((unsigned short *)lang);
164
  langid &= 0xDFDF; /* make sure lang is upcase */
165
 
166
  fd = FOPEN(fname);
167
  if (!fd) return(-1);
168
 
1373 mateusz.vi 169
  /* read hdr, sig should be "SvL\x1a" (0x1a4c7653) */
170
  if ((FREAD(fd, &hdr, 6) != 6) || (hdr.sig != 0x1a4c7653L) || (hdr.string_count != svarlang_string_count)) {
1376 mateusz.vi 171
    exitcode = -2;
172
    goto FCLOSE_AND_EXIT;
1290 bernd.boec 173
  }
174
 
1374 mateusz.vi 175
  for (;;) {
176
    /* read next lang id and string table size in file */
177
    if (FREAD(fd, buff16, 4) != 4) {
1376 mateusz.vi 178
      exitcode = -3;
179
      goto FCLOSE_AND_EXIT;
1374 mateusz.vi 180
    }
1290 bernd.boec 181
 
182
    /* is it the lang I am looking for? */
1374 mateusz.vi 183
    if (buff16[0] == langid) break;
1290 bernd.boec 184
 
1375 mateusz.vi 185
    /* skip to next lang (in two steps to avoid a potential uint16 overflow) */
1374 mateusz.vi 186
    FSEEK(fd, svarlang_string_count * 4);
187
    FSEEK(fd, buff16[1]);
188
  }
1290 bernd.boec 189
 
1375 mateusz.vi 190
  /* load dictionary & strings, but only if I have enough memory space */
191
  if ((buff16[1] >= svarlang_memsz)
192
   || (FREAD(fd, svarlang_dict, svarlang_string_count * 4) != svarlang_string_count * 4)
193
   || (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1])) {
1376 mateusz.vi 194
    exitcode = -4;
1290 bernd.boec 195
  }
196
 
1376 mateusz.vi 197
  FCLOSE_AND_EXIT:
198
 
1290 bernd.boec 199
  FCLOSE(fd);
1376 mateusz.vi 200
  return(exitcode);
1290 bernd.boec 201
}