Subversion Repositories SvarDOS

Compare Revisions

Ignore whitespace Rev 970 → Rev 971

/svarlang.lib/trunk/history.txt
0,0 → 1,3
 
20220226
- replaced fopen() and friends by direct DOS calls (smaller memory footprint)
/svarlang.lib/trunk/svarlang.c
22,7 → 22,7
* DEALINGS IN THE SOFTWARE.
*/
 
#include <stdio.h> /* FILE */
#include <i86.h>
#include <stdlib.h> /* NULL */
#include <string.h> /* memcmp(), strcpy() */
 
45,9 → 45,99
}
 
 
static unsigned short FOPEN(const char *s) {
unsigned short fname_seg = FP_SEG(s);
unsigned short fname_off = FP_OFF(s);
unsigned short res = 0xffff;
_asm {
push dx
push ds
 
mov ax, fname_seg
mov dx, fname_off
mov ds, ax
mov ax, 0x3d00 /* open file, read-only (fname at DS:DX) */
int 0x21
pop ds
jc ERR
mov res, ax
 
ERR:
pop dx
}
 
return(res);
}
 
 
static void FCLOSE(unsigned short handle) {
_asm {
push bx
 
mov ah, 0x3e
mov bx, handle
int 0x21
 
pop bx
}
}
 
 
static unsigned short FREAD(unsigned short handle, void *buff, unsigned short bytes) {
unsigned short buff_seg = FP_SEG(buff);
unsigned short buff_off = FP_OFF(buff);
unsigned short res = 0;
 
_asm {
push bx
push cx
push dx
 
mov bx, handle
mov cx, bytes
mov dx, buff_off
mov ax, buff_seg
push ds
mov ds, ax
mov ah, 0x3f /* read cx bytes from file handle bx to DS:DX */
int 0x21
pop ds
jc ERR
 
mov res, ax
ERR:
 
pop dx
pop cx
pop bx
}
 
return(res);
}
 
 
static void FJUMP(unsigned short handle, unsigned short bytes) {
_asm {
push bx
push cx
push dx
 
mov ax, 0x4201 /* move file pointer from cur pos + CX:DX */
mov bx, handle
xor cx, cx
mov dx, bytes
int 0x21
 
pop dx
pop cx
pop bx
}
}
 
 
int svarlang_load(const char *progname, const char *lang, const char *nlspath) {
unsigned short langid;
FILE *fd;
unsigned short fd;
char buff[128];
unsigned short buff16[2];
unsigned short i;
73,39 → 163,39
strcpy(buff + i, progname);
strcat(buff + i, ".lng");
 
fd = fopen(buff, "rb");
if (fd == NULL) { /* failed to open file - either abort or try next path */
fd = FOPEN(buff);
if (fd == 0xffff) { /* failed to open file - either abort or try next path */
if (*nlspath == 0) return(-2);
goto TRYNEXTPATH;
}
 
/* read hdr, should be "SvL\33" */
if ((fread(buff, 1, 4, fd) != 4) || (memcmp(buff, "SvL\33", 4) != 0)) {
fclose(fd);
if ((FREAD(fd, buff, 4) != 4) || (memcmp(buff, "SvL\33", 4) != 0)) {
FCLOSE(fd);
return(-3);
}
 
/* read next lang id in file */
while (fread(buff16, 1, 4, fd) == 4) {
while (FREAD(fd, buff16, 4) == 4) {
 
/* is it the lang I am looking for? */
if (buff16[0] != langid) { /* skip to next lang */
fseek(fd, buff16[1], SEEK_CUR);
FJUMP(fd, buff16[1]);
continue;
}
 
/* found - but do I have enough memory space? */
if (buff16[1] >= svarlang_memsz) {
fclose(fd);
FCLOSE(fd);
return(-4);
}
 
/* load strings */
if (fread(svarlang_mem, 1, buff16[1], fd) != buff16[1]) break;
fclose(fd);
if (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1]) break;
FCLOSE(fd);
return(0);
}
 
fclose(fd);
FCLOSE(fd);
return(-5);
}