Subversion Repositories SvarDOS

Rev

Rev 814 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 814 Rev 816
1
/* This file is part of the svarlang project and is published under the terms
1
/* This file is part of the svarlang project and is published under the terms
2
 * of the MIT license.
2
 * of the MIT license.
3
 *
3
 *
4
 * Copyright (C) 2021-2022 Mateusz Viste
4
 * Copyright (C) 2021-2022 Mateusz Viste
5
 *
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
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
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:
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
12
 *
13
 * The above copyright notice and this permission notice shall be included in
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
14
 * all copies or substantial portions of the Software.
15
 *
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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,
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
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
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
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
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
22
 * DEALINGS IN THE SOFTWARE.
23
 */
23
 */
24
 
24
 
25
#include <stdio.h>   /* FILE */
25
#include <stdio.h>   /* FILE */
26
#include <stdlib.h>  /* NULL */
26
#include <stdlib.h>  /* NULL */
27
#include <string.h>  /* memcmp(), strcpy() */
27
#include <string.h>  /* memcmp(), strcpy() */
28
 
28
 
29
#include "svarlang.h"
29
#include "svarlang.h"
30
 
30
 
31
 
31
 
32
/* supplied through DEFLANG.C */
32
/* supplied through DEFLANG.C */
33
extern char svarlang_mem[];
33
extern char svarlang_mem[];
34
extern const unsigned short svarlang_memsz;
34
extern const unsigned short svarlang_memsz;
35
 
35
 
36
 
36
 
37
const char *svarlang_strid(unsigned short id) {
37
const char *svarlang_strid(unsigned short id) {
38
  const char *ptr = svarlang_mem;
38
  const char *ptr = svarlang_mem;
39
  /* find the string id in langblock memory */
39
  /* find the string id in langblock memory */
40
  for (;;) {
40
  for (;;) {
41
    if (((unsigned short *)ptr)[0] == id) return(ptr + 4);
41
    if (((unsigned short *)ptr)[0] == id) return(ptr + 4);
42
    if (((unsigned short *)ptr)[1] == 0) return(ptr + 2); /* end of strings - return an empty string */
42
    if (((unsigned short *)ptr)[1] == 0) return(ptr + 2); /* end of strings - return an empty string */
43
    ptr += ((unsigned short *)ptr)[1] + 4;
43
    ptr += ((unsigned short *)ptr)[1] + 4;
44
  }
44
  }
45
}
45
}
46
 
46
 
47
 
47
 
48
int svarlang_load(const char *progname, const char *lang, const char *nlspath) {
48
int svarlang_load(const char *progname, const char *lang, const char *nlspath) {
49
  unsigned short langid;
49
  unsigned short langid;
50
  FILE *fd;
50
  FILE *fd;
51
  char buff[128];
51
  char buff[128];
52
  unsigned short buff16[2];
52
  unsigned short buff16[2];
53
  unsigned short i;
53
  unsigned short i;
54
 
54
 
55
  if (lang == NULL) return(-1);
55
  if (lang == NULL) return(-1);
56
  if (nlspath == NULL) nlspath = ""; /* nlspath can be NULL, treat is as empty */
56
  if (nlspath == NULL) nlspath = ""; /* nlspath can be NULL, treat is as empty */
57
 
57
 
58
  langid = *((unsigned short *)lang);
58
  langid = *((unsigned short *)lang);
59
  langid &= 0xDFDF; /* make sure lang is upcase */
59
  langid &= 0xDFDF; /* make sure lang is upcase */
60
 
60
 
61
  TRYNEXTPATH:
61
  TRYNEXTPATH:
62
 
62
 
63
  /* skip any leading ';' separators */
63
  /* skip any leading ';' separators */
64
  while (*nlspath == ';') nlspath++;
64
  while (*nlspath == ';') nlspath++;
65
 
65
 
66
  /* copy nlspath to buff and remember len */
66
  /* copy nlspath to buff and remember len */
67
  for (i = 0; (nlspath[i] != 0) && (nlspath[i] != ';'); i++) buff[i] = nlspath[i];
67
  for (i = 0; (nlspath[i] != 0) && (nlspath[i] != ';'); i++) buff[i] = nlspath[i];
68
  nlspath += i;
68
  nlspath += i;
69
 
69
 
70
  /* ignore the trailing backslash (I add one myself) */
70
  /* add a trailing backslash if there is none (non-empty paths empty) */
71
  if ((i > 0) && (buff[i - 1] == '\\')) i--;
71
  if ((i > 0) && (buff[i - 1] != '\\')) buff[i++] = '\\';
72
 
72
 
73
  buff[i++] = '\\';
-
 
74
  strcpy(buff + i, progname);
73
  strcpy(buff + i, progname);
75
  strcat(buff + i, ".lng");
74
  strcat(buff + i, ".lng");
76
 
75
 
77
  fd = fopen(buff, "rb");
76
  fd = fopen(buff, "rb");
78
  if (fd == NULL) { /* failed to open file - either abort or try next path */
77
  if (fd == NULL) { /* failed to open file - either abort or try next path */
79
    if (*nlspath == 0) return(-2);
78
    if (*nlspath == 0) return(-2);
80
    goto TRYNEXTPATH;
79
    goto TRYNEXTPATH;
81
  }
80
  }
82
 
81
 
83
  /* read hdr, should be "SvL\33" */
82
  /* read hdr, should be "SvL\33" */
84
  if ((fread(buff, 1, 4, fd) != 4) || (memcmp(buff, "SvL\33", 4) != 0)) {
83
  if ((fread(buff, 1, 4, fd) != 4) || (memcmp(buff, "SvL\33", 4) != 0)) {
85
    fclose(fd);
84
    fclose(fd);
86
    return(-3);
85
    return(-3);
87
  }
86
  }
88
 
87
 
89
  /* read next lang id in file */
88
  /* read next lang id in file */
90
  while (fread(buff16, 1, 4, fd) == 4) {
89
  while (fread(buff16, 1, 4, fd) == 4) {
91
 
90
 
92
    /* is it the lang I am looking for? */
91
    /* is it the lang I am looking for? */
93
    if (buff16[0] != langid) { /* skip to next lang */
92
    if (buff16[0] != langid) { /* skip to next lang */
94
      fseek(fd, buff16[1], SEEK_CUR);
93
      fseek(fd, buff16[1], SEEK_CUR);
95
      continue;
94
      continue;
96
    }
95
    }
97
 
96
 
98
    /* found - but do I have enough memory space? */
97
    /* found - but do I have enough memory space? */
99
    if (buff16[1] >= svarlang_memsz) {
98
    if (buff16[1] >= svarlang_memsz) {
100
      fclose(fd);
99
      fclose(fd);
101
      return(-4);
100
      return(-4);
102
    }
101
    }
103
 
102
 
104
    /* load strings */
103
    /* load strings */
105
    if (fread(svarlang_mem, 1, buff16[1], fd) != buff16[1]) break;
104
    if (fread(svarlang_mem, 1, buff16[1], fd) != buff16[1]) break;
106
    fclose(fd);
105
    fclose(fd);
107
    return(0);
106
    return(0);
108
  }
107
  }
109
 
108
 
110
  fclose(fd);
109
  fclose(fd);
111
  return(-5);
110
  return(-5);
112
}
111
}
113
 
112