Subversion Repositories SvarDOS

Rev

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

Rev 597 Rev 599
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 "deflang.c"
-
 
30
 
-
 
31
#include "svarlang.h"
29
#include "svarlang.h"
32
 
30
 
33
 
31
 
-
 
32
/* supplied through DEFLANG.C */
-
 
33
extern char svarlang_mem[];
-
 
34
extern const unsigned short svarlang_memsz;
-
 
35
 
-
 
36
 
34
const char *svarlang_strid(unsigned short id) {
37
const char *svarlang_strid(unsigned short id) {
35
  const char *ptr = svarlang_mem;
38
  const char *ptr = svarlang_mem;
36
  /* find the string id in langblock memory */
39
  /* find the string id in langblock memory */
37
  for (;;) {
40
  for (;;) {
38
    if (((unsigned short *)ptr)[0] == id) {
41
    if (((unsigned short *)ptr)[0] == id) {
39
      ptr += 3;
42
      ptr += 3;
40
      return(ptr);
43
      return(ptr);
41
    }
44
    }
42
    if (ptr[2] == 0) return(NULL);
45
    if (ptr[2] == 0) return(NULL);
43
    ptr += ptr[2] + 3;
46
    ptr += ptr[2] + 3;
44
  }
47
  }
45
}
48
}
46
 
49
 
47
 
50
 
48
int svarlang_load(const char *progname, const char *lang, const char *nlspath) {
51
int svarlang_load(const char *progname, const char *lang, const char *nlspath) {
49
  unsigned short langid;
52
  unsigned short langid;
50
  FILE *fd;
53
  FILE *fd;
51
  char buff[128];
54
  char buff[128];
52
  unsigned short buff16[2];
55
  unsigned short buff16[2];
53
  unsigned short i;
56
  unsigned short i;
54
 
57
 
55
  if ((lang == NULL) || (nlspath == NULL)) return(-1);
58
  if ((lang == NULL) || (nlspath == NULL)) return(-1);
56
 
59
 
57
  langid = *((unsigned short *)lang);
60
  langid = *((unsigned short *)lang);
58
  langid &= 0xDFDF; /* make sure lang is upcase */
61
  langid &= 0xDFDF; /* make sure lang is upcase */
59
 
62
 
60
  /* copy nlspath to buff and remember len */
63
  /* copy nlspath to buff and remember len */
61
  for (i = 0; nlspath[i] != 0; i++) buff[i] = nlspath[i];
64
  for (i = 0; nlspath[i] != 0; i++) buff[i] = nlspath[i];
62
 
65
 
63
  /* */
66
  /* */
64
  if ((i > 0) && (buff[i - 1] == '\\')) i--;
67
  if ((i > 0) && (buff[i - 1] == '\\')) i--;
65
 
68
 
66
  buff[i++] = '\\';
69
  buff[i++] = '\\';
67
  strcpy(buff + i, progname);
70
  strcpy(buff + i, progname);
68
  strcat(buff + i, ".lng");
71
  strcat(buff + i, ".lng");
69
 
72
 
70
  fd = fopen(buff, "rb");
73
  fd = fopen(buff, "rb");
71
  if (fd == NULL) return(-2);
74
  if (fd == NULL) return(-2);
72
 
75
 
73
  /* read hdr, should be "SvL\33" */
76
  /* read hdr, should be "SvL\33" */
74
  if ((fread(buff, 1, 4, fd) != 4) || (memcmp(buff, "SvL\33", 4) != 0)) {
77
  if ((fread(buff, 1, 4, fd) != 4) || (memcmp(buff, "SvL\33", 4) != 0)) {
75
    fclose(fd);
78
    fclose(fd);
76
    return(-3);
79
    return(-3);
77
  }
80
  }
78
 
81
 
79
  /* read next lang id in file */
82
  /* read next lang id in file */
80
  while (fread(buff16, 1, 4, fd) == 4) {
83
  while (fread(buff16, 1, 4, fd) == 4) {
81
 
84
 
82
    /* is it the lang I am looking for? */
85
    /* is it the lang I am looking for? */
83
    if (buff16[0] != langid) { /* skip to next lang */
86
    if (buff16[0] != langid) { /* skip to next lang */
84
      fseek(fd, buff16[1], SEEK_CUR);
87
      fseek(fd, buff16[1], SEEK_CUR);
85
      continue;
88
      continue;
86
    }
89
    }
87
 
90
 
88
    /* found - but do I have enough memory space? */
91
    /* found - but do I have enough memory space? */
89
    if (buff16[1] >= sizeof(svarlang_mem)) {
92
    if (buff16[1] >= svarlang_memsz) {
90
      fclose(fd);
93
      fclose(fd);
91
      return(-4);
94
      return(-4);
92
    }
95
    }
93
 
96
 
94
    /* load strings */
97
    /* load strings */
95
    if (fread(svarlang_mem, 1, buff16[1], fd) != buff16[1]) break;
98
    if (fread(svarlang_mem, 1, buff16[1], fd) != buff16[1]) break;
96
    fclose(fd);
99
    fclose(fd);
97
    return(0);
100
    return(0);
98
  }
101
  }
99
 
102
 
100
  fclose(fd);
103
  fclose(fd);
101
  return(-5);
104
  return(-5);
102
}
105
}
103
 
106