Subversion Repositories SvarDOS

Rev

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

Rev 1293 Rev 1295
1
/*
1
/*
2
 * Copyright (C) 2021-2023 Mateusz Viste
2
 * Copyright (C) 2021-2023 Mateusz Viste
3
 *
3
 *
-
 
4
 * Dictionary-based lookups contributed by Bernd Boeckmann, 2023
-
 
5
 *
4
 * usage: tlumacz en fr pl etc
6
 * usage: tlumacz en fr pl etc
5
 *
7
 *
6
 * computes an out.lng file that contains all language ressources.
8
 * computes an out.lng file that contains all language resources.
7
 *
9
 *
8
 */
10
 */
9
 
11
 
10
 
12
 
11
#include <stdio.h>
13
#include <stdio.h>
12
#include <stdlib.h>
14
#include <stdlib.h>
13
#include <string.h>
15
#include <string.h>
14
#include <ctype.h>
16
#include <ctype.h>
15
 
17
 
16
#include "svarlang.h"
18
#include "svarlang.h"
17
 
19
 
18
#define STRINGS_CAP 65000   /* string storage size in characters */
20
#define STRINGS_CAP 65000   /* string storage size in characters */
19
#define DICT_CAP    10000   /* dictionary size in elements */
21
#define DICT_CAP    10000   /* dictionary size in elements */
20
 
22
 
21
/* read a single line from fd and fills it into dst, returns line length
23
/* read a single line from fd and fills it into dst, returns line length
22
 * ending CR/LF is trimmed, as well as any trailing spaces */
24
 * ending CR/LF is trimmed, as well as any trailing spaces */
23
static unsigned short readl(char *dst, size_t dstsz, FILE *fd) {
25
static unsigned short readl(char *dst, size_t dstsz, FILE *fd) {
24
  unsigned short l, lastnonspace = 0;
26
  unsigned short l, lastnonspace = 0;
25
 
27
 
26
  if (fgets(dst, (int)dstsz, fd) == NULL) return(0xffff); /* EOF */
28
  if (fgets(dst, (int)dstsz, fd) == NULL) return(0xffff); /* EOF */
27
  /* trim at first CR or LF and return len */
29
  /* trim at first CR or LF and return len */
28
  for (l = 0; (dst[l] != 0) && (dst[l] != '\r') && (dst[l] != '\n'); l++) {
30
  for (l = 0; (dst[l] != 0) && (dst[l] != '\r') && (dst[l] != '\n'); l++) {
29
    if (dst[l] != ' ') lastnonspace = l;
31
    if (dst[l] != ' ') lastnonspace = l;
30
  }
32
  }
31
 
33
 
32
  if (lastnonspace < l) l = lastnonspace + 1; /* rtrim */
34
  if (lastnonspace < l) l = lastnonspace + 1; /* rtrim */
33
  dst[l] = 0;
35
  dst[l] = 0;
34
 
36
 
35
  return(l);
37
  return(l);
36
}
38
}
37
 
39
 
38
 
40
 
39
/* parse a line in format "[?]1.50:somestring". fills id and returns a pointer to
41
/* parse a line in format "[?]1.50:somestring". fills id and returns a pointer to
40
 * the actual string part on success, or NULL on error */
42
 * the actual string part on success, or NULL on error */
41
static const char *parseline(unsigned short *id, const char *s) {
43
static const char *parseline(unsigned short *id, const char *s) {
42
  int i;
44
  int i;
43
  int dotpos = 0, colpos = 0, gotdigits = 0;
45
  int dotpos = 0, colpos = 0, gotdigits = 0;
44
 
46
 
45
  /* strings prefixed by '?' are flagged as "dirty": ignore this flag here */
47
  /* strings prefixed by '?' are flagged as "dirty": ignore this flag here */
46
  if (*s == '?') s++;
48
  if (*s == '?') s++;
47
 
49
 
48
  /* I must have a . and a : in the first 9 bytes */
50
  /* I must have a . and a : in the first 9 bytes */
49
  for (i = 0;; i++) {
51
  for (i = 0;; i++) {
50
    if (s[i] == '.') {
52
    if (s[i] == '.') {
51
      if ((dotpos != 0) || (gotdigits == 0)) break;
53
      if ((dotpos != 0) || (gotdigits == 0)) break;
52
      dotpos = i;
54
      dotpos = i;
53
      gotdigits = 0;
55
      gotdigits = 0;
54
    } else if (s[i] == ':') {
56
    } else if (s[i] == ':') {
55
      if (gotdigits != 0) colpos = i;
57
      if (gotdigits != 0) colpos = i;
56
      break;
58
      break;
57
    } else if ((s[i] < '0') || (s[i] > '9')) {
59
    } else if ((s[i] < '0') || (s[i] > '9')) {
58
      break;
60
      break;
59
    }
61
    }
60
    gotdigits++;
62
    gotdigits++;
61
  }
63
  }
62
  /* did I collect everything? */
64
  /* did I collect everything? */
63
  if ((dotpos == 0) || (colpos == 0)) return(NULL);
65
  if ((dotpos == 0) || (colpos == 0)) return(NULL);
64
 
66
 
65
  *id = atoi(s);
67
  *id = atoi(s);
66
  *id <<= 8;
68
  *id <<= 8;
67
  *id |= atoi(s + dotpos + 1);
69
  *id |= atoi(s + dotpos + 1);
68
 
70
 
69
  /* printf("parseline(): %04X = '%s'\r\n", *id, s + colpos + 1); */
71
  /* printf("parseline(): %04X = '%s'\r\n", *id, s + colpos + 1); */
70
 
72
 
71
  return(s + colpos + 1);
73
  return(s + colpos + 1);
72
}
74
}
73
 
75
 
74
 
76
 
75
/* converts escape sequences like "\n" or "\t" into actual bytes, returns
77
/* converts escape sequences like "\n" or "\t" into actual bytes, returns
76
 * the new length of the string. */
78
 * the new length of the string. */
77
static unsigned short unesc_string(char *linebuff) {
79
static unsigned short unesc_string(char *linebuff) {
78
  unsigned short i;
80
  unsigned short i;
79
  for (i = 0; linebuff[i] != 0; i++) {
81
  for (i = 0; linebuff[i] != 0; i++) {
80
    if (linebuff[i] != '\\') continue;
82
    if (linebuff[i] != '\\') continue;
81
    memmove(linebuff + i, linebuff + i + 1, strlen(linebuff + i));
83
    memmove(linebuff + i, linebuff + i + 1, strlen(linebuff + i));
82
    if (linebuff[i] == 0) break;
84
    if (linebuff[i] == 0) break;
83
    switch (linebuff[i]) {
85
    switch (linebuff[i]) {
84
      case 'e':
86
      case 'e':
85
        linebuff[i] = 0x1B; /* ESC code, using hex because '\e' is not ANSI C */
87
        linebuff[i] = 0x1B; /* ESC code, using hex because '\e' is not ANSI C */
86
        break;
88
        break;
87
      case 'n':
89
      case 'n':
88
        linebuff[i] = '\n';
90
        linebuff[i] = '\n';
89
        break;
91
        break;
90
      case 'r':
92
      case 'r':
91
        linebuff[i] = '\r';
93
        linebuff[i] = '\r';
92
        break;
94
        break;
93
      case 't':
95
      case 't':
94
        linebuff[i] = '\t';
96
        linebuff[i] = '\t';
95
        break;
97
        break;
96
    }
98
    }
97
  }
99
  }
98
  return(i);
100
  return(i);
99
}
101
}
100
 
102
 
101
#pragma pack(1)
103
#pragma pack(1)
102
typedef struct dict_entry {
104
typedef struct dict_entry {
103
    unsigned short id;
105
  unsigned short id;
104
    unsigned short offset;
106
  unsigned short offset;
105
} dict_entry_t;
107
} dict_entry_t;
106
#pragma pack()
108
#pragma pack()
107
 
109
 
108
typedef struct svl_lang {
110
typedef struct svl_lang {
109
  char id[2];
111
  char id[2];
110
  unsigned short num_strings;
112
  unsigned short num_strings;
111
 
113
 
112
  dict_entry_t *dict;
114
  dict_entry_t *dict;
113
  size_t dict_cap;
115
  size_t dict_cap;
114
 
116
 
115
  char *strings;
117
  char *strings;
116
  char *strings_end;
118
  char *strings_end;
117
  size_t strings_cap;
119
  size_t strings_cap;
118
 
120
 
119
} svl_lang_t;
121
} svl_lang_t;
120
 
122
 
121
 
123
 
122
static svl_lang_t * svl_lang_new(char langid[2], size_t dict_cap, size_t strings_cap)
124
static svl_lang_t *svl_lang_new(const char langid[2], size_t dict_cap, size_t strings_cap) {
123
{
-
 
124
  svl_lang_t *l;
125
  svl_lang_t *l;
125
 
126
 
126
  l = malloc(sizeof(svl_lang_t));
127
  l = malloc(sizeof(svl_lang_t));
127
  if (!l) return NULL;
128
  if (!l) return(NULL);
128
 
129
 
129
  l->id[0] = (char)toupper(langid[0]);
130
  l->id[0] = (char)toupper(langid[0]);
130
  l->id[1] = (char)toupper(langid[1]);
131
  l->id[1] = (char)toupper(langid[1]);
131
 
132
 
132
  l->dict = malloc(dict_cap * sizeof(dict_entry_t));
133
  l->dict = malloc(dict_cap * sizeof(dict_entry_t));
133
  if (!l->dict) {
134
  if (!l->dict) return(NULL);
134
    return NULL;
-
 
135
  }
135
 
136
  l->dict_cap = dict_cap;
136
  l->dict_cap = dict_cap;
137
 
137
 
138
  l->num_strings = 0;
138
  l->num_strings = 0;
139
  l->strings = l->strings_end = malloc(strings_cap);
139
  l->strings = l->strings_end = malloc(strings_cap);
140
  if (!l->strings) {
140
  if (!l->strings) {
141
    free(l->dict);
141
    free(l->dict);
142
    return NULL;
142
    return(NULL);
143
  }
143
  }
144
  l->strings_cap = strings_cap;
144
  l->strings_cap = strings_cap;
-
 
145
 
145
  return l;
146
  return(l);
146
}
147
}
147
 
148
 
148
 
149
 
149
/* compacts the dict and string buffer */
150
/* compacts the dict and string buffer */
150
static void svl_compact_lang(svl_lang_t *l)
151
static void svl_compact_lang(svl_lang_t *l) {
151
{
-
 
152
  size_t bytes;
152
  size_t bytes;
153
  bytes = l->strings_end - l->strings;
153
  bytes = l->strings_end - l->strings;
154
  if (bytes < l->strings_cap) {
154
  if (bytes < l->strings_cap) {
155
    l->strings = l->strings_end = realloc(l->strings, bytes);
155
    l->strings = l->strings_end = realloc(l->strings, bytes);
156
    l->strings_end += bytes;
156
    l->strings_end += bytes;
157
    l->strings_cap = bytes;
157
    l->strings_cap = bytes;
158
  }
158
  }
159
  l->dict_cap = l->num_strings;
159
  l->dict_cap = l->num_strings;
160
  l->dict = realloc(l->dict, l->dict_cap * sizeof(dict_entry_t));
160
  l->dict = realloc(l->dict, l->dict_cap * sizeof(dict_entry_t));
161
}
161
}
162
 
162
 
163
 
163
 
164
static void svl_lang_free(svl_lang_t *l)
164
static void svl_lang_free(svl_lang_t *l) {
165
{
-
 
166
  l->num_strings = 0;
165
  l->num_strings = 0;
167
  if (l->dict) {
166
  if (l->dict) {
168
    free(l->dict);
167
    free(l->dict);
169
    l->dict = NULL;
168
    l->dict = NULL;
170
  }
169
  }
171
  if (l->strings) {
170
  if (l->strings) {
172
    free(l->strings);
171
    free(l->strings);
173
    l->strings = l->strings_end = NULL;
172
    l->strings = l->strings_end = NULL;
174
  }
173
  }
175
  l->dict_cap = 0;
174
  l->dict_cap = 0;
176
  l->strings_cap = 0;
175
  l->strings_cap = 0;
177
}
176
}
178
 
177
 
179
 
178
 
180
static size_t svl_strings_bytes(svl_lang_t *l)
179
static size_t svl_strings_bytes(const svl_lang_t *l) {
181
{
-
 
182
  return l->strings_end - l->strings;
180
  return(l->strings_end - l->strings);
183
}
181
}
184
 
182
 
185
 
183
 
186
static size_t svl_dict_bytes(svl_lang_t *l)
184
static size_t svl_dict_bytes(const svl_lang_t *l) {
187
{
-
 
188
  return l->num_strings * sizeof(dict_entry_t);
185
  return(l->num_strings * sizeof(dict_entry_t));
189
}
186
}
190
 
187
 
191
 
188
 
192
static int svl_add_str(svl_lang_t *l, unsigned short id, const char *s)
189
static int svl_add_str(svl_lang_t *l, unsigned short id, const char *s) {
193
{
-
 
194
  size_t len = strlen(s) + 1;
190
  size_t len = strlen(s) + 1;
195
  size_t cursor;
191
  size_t cursor;
196
 
192
 
197
  if (l->strings_cap < svl_strings_bytes(l) + len ||
-
 
198
      l->dict_cap < (l->num_strings + 1) * sizeof(dict_entry_t)) {
193
  if ((l->strings_cap < svl_strings_bytes(l) + len) || (l->dict_cap < (l->num_strings + 1) * sizeof(dict_entry_t))) {
199
    return 0;
194
    return(0);
200
  }
195
  }
201
 
196
 
202
  /* find dictionary insert position, search backwards in assumption
197
  /* find dictionary insert position, search backwards in assumption
203
     that in translation files, strings are generally ordered ascending */
198
     that in translation files, strings are generally ordered ascending */
204
  for (cursor = l->num_strings; cursor > 0 && l->dict[cursor-1].id > id; cursor--);
199
  for (cursor = l->num_strings; cursor > 0 && l->dict[cursor-1].id > id; cursor--);
205
 
200
 
206
  memmove(&(l->dict[cursor+1]), &(l->dict[cursor]), sizeof(dict_entry_t)*(l->num_strings - cursor));
201
  memmove(&(l->dict[cursor+1]), &(l->dict[cursor]), sizeof(dict_entry_t)*(l->num_strings - cursor));
207
  l->dict[cursor].id = id;
202
  l->dict[cursor].id = id;
208
  l->dict[cursor].offset = l->strings_end - l->strings;
203
  l->dict[cursor].offset = l->strings_end - l->strings;
209
 
204
 
210
  memcpy(l->strings_end, s, len);
205
  memcpy(l->strings_end, s, len);
211
  l->strings_end += len;
206
  l->strings_end += len;
212
  l->num_strings++;
207
  l->num_strings++;
213
 
208
 
214
  return 1;
209
  return(1);
215
}
210
}
216
 
211
 
217
 
212
 
218
static int svl_find(svl_lang_t *l, unsigned short id)
213
static int svl_find(const svl_lang_t *l, unsigned short id) {
219
{
-
 
220
   size_t left = 0, right = l->num_strings - 1, x;
214
  size_t left = 0, right = l->num_strings - 1, x;
221
   unsigned short v;
215
  unsigned short v;
222
 
216
 
223
   if (l->num_strings == 0) return 0;
217
  if (l->num_strings == 0) return(0);
224
 
218
 
225
   while (left <= right ) {
219
  while (left <= right ) {
226
      x = left + ( (right - left ) >> 2 );
220
    x = left + ( (right - left ) >> 2 );
227
      v = l->dict[x].id;
221
    v = l->dict[x].id;
228
      if ( id == v ) return 1;
222
    if ( id == v ) {
-
 
223
      return(1);
229
      else if ( id > v ) left = x + 1;
224
    } else if (id > v) {
-
 
225
      left = x + 1;
-
 
226
    } else {
230
      else right = x - 1;
227
      right = x - 1;
231
   }
228
    }
-
 
229
  }
232
   return 0;
230
  return(0);
233
}
231
}
234
 
232
 
-
 
233
 
235
/* opens a CATS-style file and compiles it into a ressources lang block
234
/* opens a CATS-style file and compiles it into a ressources lang block
236
 * returns 0 on error, or the size of the generated data block otherwise */
235
 * returns 0 on error, or the size of the generated data block otherwise */
237
static unsigned short svl_lang_from_cats_file(svl_lang_t *l, svl_lang_t *refl) {
236
static unsigned short svl_lang_from_cats_file(svl_lang_t *l, svl_lang_t *refl) {
238
  unsigned short linelen;
237
  unsigned short linelen;
239
  FILE *fd;
238
  FILE *fd;
240
  char fname[] = "xx.txt";
239
  char fname[] = "xx.txt";
241
  static char linebuf[8192];
240
  static char linebuf[8192];
242
  const char *ptr;
241
  const char *ptr;
243
  unsigned short id, maxid=0, maxid_line, linecount;
242
  unsigned short id, maxid=0, maxid_line, linecount;
244
  int i;
243
  int i;
245
 
244
 
246
  fname[strlen(fname) - 6] = (char)tolower( l->id[0] );
245
  fname[strlen(fname) - 6] = (char)tolower( l->id[0] );
247
  fname[strlen(fname) - 5] = (char)tolower( l->id[1] );
246
  fname[strlen(fname) - 5] = (char)tolower( l->id[1] );
248
 
247
 
249
  fd = fopen(fname, "rb");
248
  fd = fopen(fname, "rb");
250
  if (fd == NULL) {
249
  if (fd == NULL) {
251
    printf("ERROR: FAILED TO OPEN '%s'\r\n", fname);
250
    printf("ERROR: FAILED TO OPEN '%s'\r\n", fname);
252
    return(0);
251
    return(0);
253
  }
252
  }
254
 
253
 
255
  for (linecount = 1;; linecount++) {
254
  for (linecount = 1;; linecount++) {
256
    linelen = readl(linebuf, sizeof(linebuf), fd);
255
    linelen = readl(linebuf, sizeof(linebuf), fd);
257
    if (linelen == 0xffff) break; /* EOF */
256
    if (linelen == 0xffff) break; /* EOF */
258
    if ((linelen == 0) || (linebuf[0] == '#')) continue;
257
    if ((linelen == 0) || (linebuf[0] == '#')) continue;
259
 
258
 
260
    /* convert escaped chars to actual bytes (\n -> newline, etc) */
259
    /* convert escaped chars to actual bytes (\n -> newline, etc) */
261
    linelen = unesc_string(linebuf);
260
    linelen = unesc_string(linebuf);
262
 
261
 
263
    /* read id and get ptr to actual string ("1.15:string") */
262
    /* read id and get ptr to actual string ("1.15:string") */
264
    ptr = parseline(&id, linebuf);
263
    ptr = parseline(&id, linebuf);
265
 
264
 
266
    /* handle malformed lines */
265
    /* handle malformed lines */
267
    if (ptr == NULL) {
266
    if (ptr == NULL) {
268
      printf("WARNING: %s[#%u] is malformed (linelen = %u):\r\n", fname, linecount, linelen);
267
      printf("WARNING: %s[#%u] is malformed (linelen = %u):\r\n", fname, linecount, linelen);
269
      puts(linebuf);
268
      puts(linebuf);
270
      continue;
269
      continue;
271
    }
270
    }
272
 
271
 
273
    /* ignore empty strings (but emit a warning) */
272
    /* ignore empty strings (but emit a warning) */
274
    if (ptr[0] == 0) {
273
    if (ptr[0] == 0) {
275
      printf("WARNING: %s[#%u] ignoring empty string %u.%u\r\n", fname, linecount, id >> 8, id & 0xff);
274
      printf("WARNING: %s[#%u] ignoring empty string %u.%u\r\n", fname, linecount, id >> 8, id & 0xff);
276
      continue;
275
      continue;
277
    }
276
    }
278
 
277
 
279
    /* warn about dirty lines */
278
    /* warn about dirty lines */
280
    if (linebuf[0] == '?') {
279
    if (linebuf[0] == '?') {
281
      printf("WARNING: %s[#%u] string id %u.%u is flagged as 'dirty'\r\n", fname, linecount, id >> 8, id & 0xff);
280
      printf("WARNING: %s[#%u] string id %u.%u is flagged as 'dirty'\r\n", fname, linecount, id >> 8, id & 0xff);
282
    }
281
    }
283
 
282
 
284
    /* add the string contained in current line, if conditions are met */
283
    /* add the string contained in current line, if conditions are met */
285
    if (!svl_find(l, id)) {
284
    if (!svl_find(l, id)) {
286
      if (refl == NULL || svl_find(refl, id)) {
285
      if ((refl == NULL) || (svl_find(refl, id))) {
287
        if (!svl_add_str(l, id, ptr)) {
286
        if (!svl_add_str(l, id, ptr)) {
288
          printf("ERROR: %s[#%u] output size limit exceeded\r\n", fname, linecount);
287
          printf("ERROR: %s[#%u] output size limit exceeded\r\n", fname, linecount);
289
          fclose(fd);
288
          fclose(fd);
290
          return 0;
289
          return(0);
291
        }
290
        }
292
        if (id >= maxid) {
291
        if (id >= maxid) {
293
          maxid = id;
292
          maxid = id;
294
          maxid_line = linecount;
293
          maxid_line = linecount;
295
        }
-
 
296
        else {
294
        } else {
297
          printf("WARNING:%s[#%u] file unsorted - line %u has higher id %u.%u\r\n", fname, linecount, maxid_line, maxid >> 8, maxid & 0xff);
295
          printf("WARNING:%s[#%u] file unsorted - line %u has higher id %u.%u\r\n", fname, linecount, maxid_line, maxid >> 8, maxid & 0xff);
298
        }
296
        }
299
      }
-
 
300
      else {
297
      } else {
301
        printf("WARNING: %s[#%u] has an invalid id (%u.%u not present in ref lang)\r\n", fname, linecount, id >> 8, id & 0xff);
298
        printf("WARNING: %s[#%u] has an invalid id (%u.%u not present in ref lang)\r\n", fname, linecount, id >> 8, id & 0xff);
302
      }
299
      }
303
    }
-
 
304
    else {
300
    } else {
305
      printf("WARNING: %s[#%u] has a duplicated id (%u.%u)\r\n", fname, linecount, id >> 8, id & 0xff);
301
      printf("WARNING: %s[#%u] has a duplicated id (%u.%u)\r\n", fname, linecount, id >> 8, id & 0xff);
306
    }
302
    }
307
  }
303
  }
308
 
304
 
309
  fclose(fd);
305
  fclose(fd);
310
 
306
 
311
  /* if reflang provided, pull missing strings from it */
307
  /* if reflang provided, pull missing strings from it */
312
  if (refl != NULL) {
308
  if (refl != NULL) {
313
    for (i = 0; i < refl->num_strings; i++) {
309
    for (i = 0; i < refl->num_strings; i++) {
314
      id = refl->dict[i].id;
310
      id = refl->dict[i].id;
315
      if (!svl_find(l, id)) {
311
      if (!svl_find(l, id)) {
316
        printf("WARNING: %s is missing string %u.%u (pulled from ref lang)\r\n", fname, id >> 8, id & 0xff);
312
        printf("WARNING: %s is missing string %u.%u (pulled from ref lang)\r\n", fname, id >> 8, id & 0xff);
317
        if (!svl_add_str(l, id, refl->strings + refl->dict[i].offset)) {
313
        if (!svl_add_str(l, id, refl->strings + refl->dict[i].offset)) {
318
          printf("ERROR: %s[#%u] output size limit exceeded\r\n", fname, linecount);
314
          printf("ERROR: %s[#%u] output size limit exceeded\r\n", fname, linecount);
319
          return 0;
315
          return(0);
320
        }
316
        }
321
      }
317
      }
322
    }
318
    }
323
  }
319
  }
324
 
320
 
325
  return(svl_strings_bytes(l));
321
  return(svl_strings_bytes(l));
326
}
322
}
327
 
323
 
328
 
324
 
329
static int svl_write_header(unsigned short num_strings, FILE *fd)
325
static int svl_write_header(unsigned short num_strings, FILE *fd) {
330
{
-
 
331
  return (fwrite("SvL\x1a", 1, 4, fd) == 4) &&
-
 
332
          (fwrite(&num_strings, 1, 2, fd) == 2);
326
  return((fwrite("SvL\x1a", 1, 4, fd) == 4) && (fwrite(&num_strings, 1, 2, fd) == 2));
333
}
327
}
334
 
328
 
335
 
329
 
336
static int svl_write_lang(svl_lang_t *l, FILE *fd)
330
static int svl_write_lang(const svl_lang_t *l, FILE *fd) {
337
{
-
 
338
  unsigned short strings_bytes = svl_strings_bytes(l);
331
  unsigned short strings_bytes = svl_strings_bytes(l);
339
 
332
 
340
  return (fwrite(&l->id, 1, 2, fd) == 2) &&
333
  return((fwrite(&l->id, 1, 2, fd) == 2) &&
341
         (fwrite(&strings_bytes, 1, 2, fd) == 2) &&
334
         (fwrite(&strings_bytes, 1, 2, fd) == 2) &&
342
         (fwrite(l->dict, 1, svl_dict_bytes(l), fd) == svl_dict_bytes(l)) &&
335
         (fwrite(l->dict, 1, svl_dict_bytes(l), fd) == svl_dict_bytes(l)) &&
343
         (fwrite(l->strings, 1, svl_strings_bytes(l), fd) == svl_strings_bytes(l));
336
         (fwrite(l->strings, 1, svl_strings_bytes(l), fd) == svl_strings_bytes(l)));
344
}
337
}
345
 
338
 
346
 
339
 
347
static int svl_write_c_source(svl_lang_t *l, const char *fn, unsigned short biggest_langsz)
340
static int svl_write_c_source(const svl_lang_t *l, const char *fn, unsigned short biggest_langsz) {
348
{
-
 
349
  FILE *fd;
341
  FILE *fd;
350
  int i;
342
  int i;
351
  unsigned short strings_bytes = svl_strings_bytes(l);
343
  unsigned short strings_bytes = svl_strings_bytes(l);
352
  unsigned short nextnlat = 0;
344
  unsigned short nextnlat = 0;
-
 
345
  unsigned short allocsz;
353
 
346
 
354
  fd = fopen(fn, "wb");
347
  fd = fopen(fn, "wb");
355
  if (fd == NULL) {
348
  if (fd == NULL) {
356
    puts("ERROR: FAILED TO OPEN OR CREATE DEFLANG.C");
349
    puts("ERROR: FAILED TO OPEN OR CREATE DEFLANG.C");
357
    return 0;
350
    return(0);
358
  } else {
-
 
359
    unsigned short allocsz = biggest_langsz + (biggest_langsz / 20);
-
 
360
    printf("biggest lang block is %u bytes -> allocating a %u bytes buffer (5%% safety margin)\n", biggest_langsz,
-
 
361
           allocsz);
-
 
362
    fprintf(fd, "/* THIS FILE HAS BEEN GENERATED BY TLUMACZ (PART OF THE SVARLANG LIBRARY) */\r\n");
-
 
363
    fprintf(fd, "const unsigned short svarlang_memsz = %uu;\r\n", allocsz);
-
 
364
    fprintf(fd, "const unsigned short svarlang_string_count = %uu;\r\n\r\n", l->num_strings);
-
 
365
    fprintf(fd, "char svarlang_mem[%u] = {\r\n", allocsz);
-
 
366
    for (i = 0; i < strings_bytes; i++) {
-
 
367
      if (!fprintf(fd, "0x%02x", l->strings[i])) {
-
 
368
        fclose(fd);
-
 
369
        return 0;
-
 
370
      }
-
 
371
 
-
 
372
      if (i + 1 < strings_bytes) fprintf(fd, ",");
-
 
373
      nextnlat++;
-
 
374
      if (l->strings[i] == '\0' || nextnlat == 16) {
-
 
375
        fprintf(fd, "\r\n");
-
 
376
        nextnlat = 0;
-
 
377
      }
-
 
378
    }
351
  }
379
    fprintf(fd, "};\r\n\r\n");
-
 
380
 
352
 
-
 
353
  allocsz = biggest_langsz + (biggest_langsz / 20);
-
 
354
  printf("biggest lang block is %u bytes -> allocating a %u bytes buffer (5%% safety margin)\n", biggest_langsz, allocsz);
-
 
355
  fprintf(fd, "/* THIS FILE HAS BEEN GENERATED BY TLUMACZ (PART OF THE SVARLANG LIBRARY) */\r\n");
-
 
356
  fprintf(fd, "const unsigned short svarlang_memsz = %uu;\r\n", allocsz);
381
    fprintf(fd, "unsigned short svarlang_dict[%u] = {\r\n", l->num_strings * 2);
357
  fprintf(fd, "const unsigned short svarlang_string_count = %uu;\r\n\r\n", l->num_strings);
-
 
358
  fprintf(fd, "char svarlang_mem[%u] = {\r\n", allocsz);
-
 
359
 
382
    for (i = 0; i < l->num_strings; i++) {
360
  for (i = 0; i < strings_bytes; i++) {
383
      if (!fprintf(fd, "0x%04x,0x%04x", l->dict[i].id, l->dict[i].offset)) {
361
    if (!fprintf(fd, "0x%02x", l->strings[i])) {
384
        fclose(fd);
362
      fclose(fd);
385
        return 0;
363
      return(0);
386
      }
364
    }
-
 
365
 
387
      if (i + 1 < l->num_strings) fprintf(fd, ",");
366
    if (i + 1 < strings_bytes) fprintf(fd, ",");
-
 
367
    nextnlat++;
-
 
368
    if (l->strings[i] == '\0' || nextnlat == 16) {
388
      fprintf(fd, "\r\n");
369
      fprintf(fd, "\r\n");
-
 
370
      nextnlat = 0;
389
    }
371
    }
-
 
372
  }
390
    fprintf(fd, "};\r\n");
373
  fprintf(fd, "};\r\n\r\n");
391
 
374
 
-
 
375
  fprintf(fd, "unsigned short svarlang_dict[%u] = {\r\n", l->num_strings * 2);
-
 
376
  for (i = 0; i < l->num_strings; i++) {
-
 
377
    if (!fprintf(fd, "0x%04x,0x%04x", l->dict[i].id, l->dict[i].offset)) {
392
    fclose(fd);
378
      fclose(fd);
-
 
379
      return(0);
-
 
380
    }
-
 
381
    if (i + 1 < l->num_strings) fprintf(fd, ",");
-
 
382
    fprintf(fd, "\r\n");
393
  }
383
  }
-
 
384
  fprintf(fd, "};\r\n");
394
 
385
 
-
 
386
  fclose(fd);
-
 
387
 
395
  return 1;
388
  return(1);
396
}
389
}
397
 
390
 
398
 
391
 
399
int main(int argc, char **argv) {
392
int main(int argc, char **argv) {
400
  FILE *fd;
393
  FILE *fd;
401
  int ecode = 0;
394
  int ecode = 0;
402
  svl_lang_t *lang, *reflang = NULL;
-
 
403
 
-
 
404
  int i;
395
  int i;
405
  unsigned short biggest_langsz = 0;
396
  unsigned short biggest_langsz = 0;
-
 
397
  svl_lang_t *lang, *reflang = NULL;
406
 
398
 
407
  if (argc < 2) {
399
  if (argc < 2) {
408
    puts("tlumacz ver " SVARLANGVER " - this tool is part of the SvarLANG project.");
400
    puts("tlumacz ver " SVARLANGVER " - this tool is part of the SvarLANG project.");
409
    puts("converts a set of CATS-style translations in files EN.TXT, PL.TXT, etc");
401
    puts("converts a set of CATS-style translations in files EN.TXT, PL.TXT, etc");
410
    puts("into a single resource file (OUT.LNG).");
402
    puts("into a single resource file (OUT.LNG).");
411
    puts("");
403
    puts("");
412
    puts("usage: tlumacz en fr pl ...");
404
    puts("usage: tlumacz en fr pl ...");
413
    return(1);
405
    return(1);
414
  }
406
  }
415
 
407
 
416
  fd = fopen("out.lng", "wb");
408
  fd = fopen("out.lng", "wb");
417
  if (fd == NULL) {
409
  if (fd == NULL) {
418
    puts("ERR: failed to open or create OUT.LNG");
410
    puts("ERR: failed to open or create OUT.LNG");
419
    return(1);
411
    return(1);
420
  }
412
  }
421
 
413
 
422
  /* write lang blocks */
414
  /* write lang blocks */
423
  for (i = 1; i < argc; i++) {
415
  for (i = 1; i < argc; i++) {
424
    unsigned short sz;
416
    unsigned short sz;
425
    char id[3];
417
    char id[3];
426
 
418
 
427
    if (strlen(argv[i]) != 2) {
419
    if (strlen(argv[i]) != 2) {
428
      printf("INVALID LANG SPECIFIED: %s\r\n", argv[i]);
420
      printf("INVALID LANG SPECIFIED: %s\r\n", argv[i]);
429
      ecode = 1;
421
      ecode = 1;
430
      break;
422
      break;
431
    }
423
    }
432
    id[0] = argv[i][0];
424
    id[0] = argv[i][0];
433
    id[1] = argv[i][1];
425
    id[1] = argv[i][1];
434
    id[2] = 0;
426
    id[2] = 0;
435
 
427
 
436
    if ((lang = svl_lang_new(id, DICT_CAP, STRINGS_CAP)) == NULL) {
428
    if ((lang = svl_lang_new(id, DICT_CAP, STRINGS_CAP)) == NULL) {
437
      printf("OUT OF MEMORY\r\n");
429
      printf("OUT OF MEMORY\r\n");
438
      return(1);
430
      return(1);
439
    }
431
    }
440
 
432
 
441
    sz = svl_lang_from_cats_file(lang, reflang);
433
    sz = svl_lang_from_cats_file(lang, reflang);
442
    if (sz == 0) {
434
    if (sz == 0) {
443
      printf("ERROR COMPUTING LANG '%s'\r\n", id);
435
      printf("ERROR COMPUTING LANG '%s'\r\n", id);
444
      ecode = 1;
436
      ecode = 1;
445
      break;
437
      break;
446
    } else {
438
    } else {
447
      printf("computed %s lang block of %u bytes\r\n", id, sz);
439
      printf("computed %s lang block of %u bytes\r\n", id, sz);
448
      if (sz > biggest_langsz) biggest_langsz = sz;
440
      if (sz > biggest_langsz) biggest_langsz = sz;
449
    }
441
    }
450
    svl_compact_lang(lang);
442
    svl_compact_lang(lang);
451
 
443
 
452
    /* write header if first (reference) language */
444
    /* write header if first (reference) language */
453
    if (i == 1) {
445
    if (i == 1) {
454
      if (!svl_write_header(lang->num_strings, fd)) {
446
      if (!svl_write_header(lang->num_strings, fd)) {
455
        printf("ERROR WRITING TO OUTPUT FILE\r\n");
447
        printf("ERROR WRITING TO OUTPUT FILE\r\n");
456
        ecode = 1;
448
        ecode = 1;
457
        break;
449
        break;
458
      }
450
      }
459
    }
451
    }
460
 
452
 
461
    /* write lang ID to file, followed string table size, and then
453
    /* write lang ID to file, followed string table size, and then
462
       the dictionary and string table for current language */
454
       the dictionary and string table for current language */
463
    if (!svl_write_lang(lang, fd)) {
455
    if (!svl_write_lang(lang, fd)) {
464
      printf("ERROR WRITING TO OUTPUT FILE\r\n");
456
      printf("ERROR WRITING TO OUTPUT FILE\r\n");
465
      ecode = 1;
457
      ecode = 1;
466
      break;
458
      break;
467
    }
459
    }
468
 
460
 
469
    /* remember reference data for other languages */
461
    /* remember reference data for other languages */
470
    if (i == 1) {
462
    if (i == 1) {
471
      reflang = lang;
463
      reflang = lang;
472
    }
-
 
473
    else {
464
    } else {
474
      svl_lang_free(lang);
465
      svl_lang_free(lang);
475
      lang = NULL;
466
      lang = NULL;
476
    }
467
    }
477
  }
468
  }
478
 
469
 
479
  /* compute the deflang.c file containing a dump of the reference block */
470
  /* compute the deflang.c file containing a dump of the reference block */
480
  if (!svl_write_c_source(reflang, "deflang.c", biggest_langsz)) {
471
  if (!svl_write_c_source(reflang, "deflang.c", biggest_langsz)) {
481
    puts("ERROR: FAILED TO OPEN OR CREATE DEFLANG.C");
472
    puts("ERROR: FAILED TO OPEN OR CREATE DEFLANG.C");
482
    ecode = 1;
473
    ecode = 1;
483
  }
474
  }
484
 
475
 
485
  /* clean up */
476
  /* clean up */
486
  if (reflang) {
477
  if (reflang) {
487
    svl_lang_free(reflang);
478
    svl_lang_free(reflang);
488
    reflang = NULL;
479
    reflang = NULL;
489
  }
480
  }
490
 
481
 
491
  return(ecode);
482
  return(ecode);
492
}
483
}
493
 
484