Subversion Repositories SvarDOS

Rev

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

Rev Author Line No. Line
597 mateuszvis 1
/* This file is part of the svarlang project and is published under the terms
2
 * of the MIT license.
3
 *
1251 mateusz.vi 4
 * Copyright (C) 2021-2023 Mateusz Viste
597 mateuszvis 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
 
1281 mateusz.vi 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
 
971 mateusz.vi 38
#include <i86.h>
1281 mateusz.vi 39
typedef unsigned short FHANDLE;
40
 
41
#endif
42
 
597 mateuszvis 43
#include <stdlib.h>  /* NULL */
44
#include <string.h>  /* memcmp(), strcpy() */
45
 
46
#include "svarlang.h"
47
 
48
 
599 mateuszvis 49
/* supplied through DEFLANG.C */
50
extern char svarlang_mem[];
51
extern const unsigned short svarlang_memsz;
52
 
53
 
597 mateuszvis 54
const char *svarlang_strid(unsigned short id) {
55
  const char *ptr = svarlang_mem;
56
  /* find the string id in langblock memory */
57
  for (;;) {
623 mateuszvis 58
    if (((unsigned short *)ptr)[0] == id) return(ptr + 4);
59
    if (((unsigned short *)ptr)[1] == 0) return(ptr + 2); /* end of strings - return an empty string */
60
    ptr += ((unsigned short *)ptr)[1] + 4;
597 mateuszvis 61
  }
62
}
63
 
1281 mateusz.vi 64
/* 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
66
 * smaller if the application does not need to pull fopen() and friends */
67
#ifndef WITHSTDIO
971 mateusz.vi 68
static unsigned short FOPEN(const char *s) {
69
  unsigned short fname_seg = FP_SEG(s);
70
  unsigned short fname_off = FP_OFF(s);
1281 mateusz.vi 71
  unsigned short res = 0; /* fd 0 is already used by stdout so it's a good error value */
971 mateusz.vi 72
  _asm {
73
    push dx
74
    push ds
75
 
76
    mov ax, fname_seg
77
    mov dx, fname_off
78
    mov ds, ax
79
    mov ax, 0x3d00  /* open file, read-only (fname at DS:DX) */
80
    int 0x21
81
    pop ds
82
    jc ERR
83
    mov res, ax
84
 
85
    ERR:
86
    pop dx
87
  }
88
 
89
  return(res);
90
}
91
 
92
 
93
static void FCLOSE(unsigned short handle) {
94
  _asm {
95
    push bx
96
 
97
    mov ah, 0x3e
98
    mov bx, handle
99
    int 0x21
100
 
101
    pop bx
102
  }
103
}
104
 
105
 
106
static unsigned short FREAD(unsigned short handle, void *buff, unsigned short bytes) {
107
  unsigned short buff_seg = FP_SEG(buff);
108
  unsigned short buff_off = FP_OFF(buff);
109
  unsigned short res = 0;
110
 
111
  _asm {
112
    push bx
113
    push cx
114
    push dx
115
 
116
    mov bx, handle
117
    mov cx, bytes
118
    mov dx, buff_off
119
    mov ax, buff_seg
120
    push ds
121
    mov ds, ax
122
    mov ah, 0x3f    /* read cx bytes from file handle bx to DS:DX */
123
    int 0x21
124
    pop ds
125
    jc ERR
126
 
127
    mov res, ax
128
    ERR:
129
 
130
    pop dx
131
    pop cx
132
    pop bx
133
  }
134
 
135
  return(res);
136
}
137
 
138
 
1281 mateusz.vi 139
static void FSEEK(unsigned short handle, unsigned short bytes) {
971 mateusz.vi 140
  _asm {
141
    push bx
142
    push cx
143
    push dx
144
 
145
    mov ax, 0x4201  /* move file pointer from cur pos + CX:DX */
146
    mov bx, handle
147
    xor cx, cx
148
    mov dx, bytes
149
    int 0x21
150
 
151
    pop dx
152
    pop cx
153
    pop bx
154
  }
155
}
1281 mateusz.vi 156
#endif
971 mateusz.vi 157
 
1277 mateusz.vi 158
int svarlang_load(const char *fname, const char *lang) {
597 mateuszvis 159
  unsigned short langid;
1277 mateusz.vi 160
  char hdr[4];
597 mateuszvis 161
  unsigned short buff16[2];
1281 mateusz.vi 162
  FHANDLE fd;
597 mateuszvis 163
 
164
  langid = *((unsigned short *)lang);
165
  langid &= 0xDFDF; /* make sure lang is upcase */
166
 
1277 mateusz.vi 167
  fd = FOPEN(fname);
1281 mateusz.vi 168
  if (!fd) return(-1);
814 mateusz.vi 169
 
597 mateuszvis 170
  /* read hdr, should be "SvL\33" */
1277 mateusz.vi 171
  if ((FREAD(fd, hdr, 4) != 4) || (memcmp(hdr, "SvL\33", 4) != 0)) {
971 mateusz.vi 172
    FCLOSE(fd);
597 mateuszvis 173
    return(-3);
174
  }
175
 
176
  /* read next lang id in file */
971 mateusz.vi 177
  while (FREAD(fd, buff16, 4) == 4) {
597 mateuszvis 178
 
179
    /* is it the lang I am looking for? */
180
    if (buff16[0] != langid) { /* skip to next lang */
1281 mateusz.vi 181
      FSEEK(fd, buff16[1]);
597 mateuszvis 182
      continue;
183
    }
184
 
185
    /* found - but do I have enough memory space? */
599 mateuszvis 186
    if (buff16[1] >= svarlang_memsz) {
971 mateusz.vi 187
      FCLOSE(fd);
597 mateuszvis 188
      return(-4);
189
    }
190
 
191
    /* load strings */
971 mateusz.vi 192
    if (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1]) break;
193
    FCLOSE(fd);
597 mateuszvis 194
    return(0);
195
  }
196
 
971 mateusz.vi 197
  FCLOSE(fd);
597 mateuszvis 198
  return(-5);
199
}