Subversion Repositories SvarDOS

Rev

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

Rev 601 Rev 623
1
/*
1
/*
2
 * Copyright (C) 2021-2022 Mateusz Viste
2
 * Copyright (C) 2021-2022 Mateusz Viste
3
 *
3
 *
4
 * usage: tlumacz en fr pl etc
4
 * usage: tlumacz en fr pl etc
5
 *
5
 *
6
 * computes an out.lng file that contains all language ressources.
6
 * computes an out.lng file that contains all language ressources.
7
 *
7
 *
8
 * DAT format:
8
 * DAT format:
9
 *
9
 *
10
 * 4-bytes signature:
10
 * 4-bytes signature:
11
 * "SvL\x1b"
11
 * "SvL\x1b"
12
 *
12
 *
13
 * Then "LANG BLOCKS" follow. Each LANG BLOCK is prefixed with 4 bytes:
13
 * Then "LANG BLOCKS" follow. Each LANG BLOCK is prefixed with 4 bytes:
14
 * II LL    - II is the LANG identifier ("EN", "PL", etc) and LL is the size
14
 * II LL    - II is the LANG identifier ("EN", "PL", etc) and LL is the size
15
 *            of the block (65535 bytes max).
15
 *            of the block (65535 bytes max).
16
 *
16
 *
17
 * Inside a LANG BLOCK is a set of strings:
17
 * Inside a LANG BLOCK is a set of strings:
18
 *
18
 *
19
 * II L S  where II is the string's 16-bit identifier, L is its length (1-255)
19
 * II LL S  where II is the string's 16-bit identifier, LL is its length
20
 *         and S is the actual string. All strings are ASCIIZ-formatted (ie.
20
 *          (1-65535) and S is the actual string. All strings are ASCIIZ (ie.
21
 *         end with a NULL terminator).
21
 *          they end with a NULL terminator).
22
 *
22
 *
23
 * The list of strings ends with a single 0-long string.
23
 * The list of strings ends with a single 0-long string.
24
 */
24
 */
25
 
25
 
26
 
26
 
27
#include <stdio.h>
27
#include <stdio.h>
28
#include <stdlib.h>
28
#include <stdlib.h>
29
#include <string.h>
29
#include <string.h>
30
 
30
 
31
 
31
 
32
 
32
 
33
struct bitmap {
33
struct bitmap {
34
  unsigned char bits[8192];
34
  unsigned char bits[8192];
35
};
35
};
36
 
36
 
37
static void bitmap_set(struct bitmap *b, unsigned short id) {
37
static void bitmap_set(struct bitmap *b, unsigned short id) {
38
  b->bits[id / 8] |= 1 << (id & 7);
38
  b->bits[id >> 3] |= 1 << (id & 7);
39
}
39
}
40
 
40
 
41
static int bitmap_get(const struct bitmap *b, unsigned short id) {
41
static int bitmap_get(const struct bitmap *b, unsigned short id) {
42
  return(b->bits[id / 8] & (1 << (id & 7)));
42
  return(b->bits[id >> 3] & (1 << (id & 7)));
43
}
43
}
44
 
44
 
45
static void bitmap_init(struct bitmap *b) {
45
static void bitmap_init(struct bitmap *b) {
46
  memset(b, 0, sizeof(struct bitmap));
46
  bzero(b, sizeof(struct bitmap));
47
}
47
}
48
 
48
 
49
 
49
 
50
 
50
 
51
/* read a single line from fd and fills it into dst, returns line length
51
/* read a single line from fd and fills it into dst, returns line length
52
 * ending CR/LF is trimmed, as well as any trailing spaces */
52
 * ending CR/LF is trimmed, as well as any trailing spaces */
53
static unsigned short readl(char *dst, size_t dstsz, FILE *fd) {
53
static unsigned short readl(char *dst, size_t dstsz, FILE *fd) {
54
  unsigned short l, lastnonspace = 0;
54
  unsigned short l, lastnonspace = 0;
55
 
55
 
56
  if (fgets(dst, dstsz, fd) == NULL) return(0xffff); /* EOF */
56
  if (fgets(dst, dstsz, fd) == NULL) return(0xffff); /* EOF */
57
  /* trim at first CR or LF and return len */
57
  /* trim at first CR or LF and return len */
58
  for (l = 0; (dst[l] != 0) && (dst[l] != '\r') && (dst[l] != '\n'); l++) {
58
  for (l = 0; (dst[l] != 0) && (dst[l] != '\r') && (dst[l] != '\n'); l++) {
59
    if (dst[l] != ' ') lastnonspace = l;
59
    if (dst[l] != ' ') lastnonspace = l;
60
  }
60
  }
61
 
61
 
62
  if (lastnonspace < l) l = lastnonspace + 1; /* rtrim */
62
  if (lastnonspace < l) l = lastnonspace + 1; /* rtrim */
63
  dst[l] = 0;
63
  dst[l] = 0;
64
 
64
 
65
  return(l);
65
  return(l);
66
}
66
}
67
 
67
 
68
 
68
 
69
/* parse a line in format "1.50:somestring". fills id and returns a pointer to
69
/* parse a line in format "1.50:somestring". fills id and returns a pointer to
70
 * the actual string part on success, or NULL on error */
70
 * the actual string part on success, or NULL on error */
71
static char *parseline(unsigned short *id, char *s) {
71
static char *parseline(unsigned short *id, char *s) {
72
  int i;
72
  int i;
73
  int dotpos = 0, colpos = 0, gotdigits = 0;
73
  int dotpos = 0, colpos = 0, gotdigits = 0;
74
 
74
 
75
  /* I must have a . and a : in the first 9 bytes */
75
  /* I must have a . and a : in the first 9 bytes */
76
  for (i = 0;; i++) {
76
  for (i = 0;; i++) {
77
    if (s[i] == '.') {
77
    if (s[i] == '.') {
78
      if ((dotpos != 0) || (gotdigits == 0)) break;
78
      if ((dotpos != 0) || (gotdigits == 0)) break;
79
      dotpos = i;
79
      dotpos = i;
80
      gotdigits = 0;
80
      gotdigits = 0;
81
    } else if (s[i] == ':') {
81
    } else if (s[i] == ':') {
82
      if (gotdigits != 0) colpos = i;
82
      if (gotdigits != 0) colpos = i;
83
      break;
83
      break;
84
    } else if ((s[i] < '0') || (s[i] > '9')) {
84
    } else if ((s[i] < '0') || (s[i] > '9')) {
85
      break;
85
      break;
86
    }
86
    }
87
    gotdigits++;
87
    gotdigits++;
88
  }
88
  }
89
  /* did I collect everything? */
89
  /* did I collect everything? */
90
  if ((dotpos == 0) || (colpos == 0)) return(NULL);
90
  if ((dotpos == 0) || (colpos == 0)) return(NULL);
91
  if (s[colpos + 1] == 0) return(NULL);
91
  if (s[colpos + 1] == 0) return(NULL);
92
 
92
 
93
  *id = atoi(s);
93
  *id = atoi(s);
94
  *id <<= 8;
94
  *id <<= 8;
95
  *id |= atoi(s + dotpos + 1);
95
  *id |= atoi(s + dotpos + 1);
96
 
96
 
97
  /* printf("parseline(): %04X = '%s'\r\n", *id, s + colpos + 1); */
97
  /* printf("parseline(): %04X = '%s'\r\n", *id, s + colpos + 1); */
98
 
98
 
99
  return(s + colpos + 1);
99
  return(s + colpos + 1);
100
}
100
}
101
 
101
 
102
 
102
 
103
/* opens a CATS-style file and compiles it into a ressources lang block */
103
/* opens a CATS-style file and compiles it into a ressources lang block */
104
static unsigned short gen_langstrings(unsigned char *buff, const char *langid, struct bitmap *b, const struct bitmap *refb, const unsigned char *refblock) {
104
static unsigned short gen_langstrings(unsigned char *buff, const char *langid, struct bitmap *b, const struct bitmap *refb, const unsigned char *refblock) {
105
  unsigned short len = 0, linelen;
105
  unsigned short len = 0, linelen;
106
  FILE *fd;
106
  FILE *fd;
107
  char fname[] = "XX.TXT";
107
  char fname[] = "XX.TXT";
108
  char linebuf[512];
108
  static char linebuf[8192];
109
  char *ptr;
109
  char *ptr;
110
  unsigned short id, linecount;
110
  unsigned short id, linecount;
111
 
111
 
112
  bitmap_init(b);
112
  bitmap_init(b);
113
 
113
 
114
  memcpy(fname + strlen(fname) - 6, langid, 2);
114
  memcpy(fname + strlen(fname) - 6, langid, 2);
115
 
115
 
116
  fd = fopen(fname, "rb");
116
  fd = fopen(fname, "rb");
117
  if (fd == NULL) {
117
  if (fd == NULL) {
118
    printf("ERROR: FAILED TO OPEN '%s'\r\n", fname);
118
    printf("ERROR: FAILED TO OPEN '%s'\r\n", fname);
119
    return(0);
119
    return(0);
120
  }
120
  }
121
 
121
 
122
  for (linecount = 1;; linecount++) {
122
  for (linecount = 1;; linecount++) {
123
 
123
 
124
    linelen = readl(linebuf, sizeof(linebuf), fd);
124
    linelen = readl(linebuf, sizeof(linebuf), fd);
125
    if (linelen == 0xffff) break; /* EOF */
125
    if (linelen == 0xffff) break; /* EOF */
126
    if ((linelen == 0) || (linebuf[0] == '#')) continue;
126
    if ((linelen == 0) || (linebuf[0] == '#')) continue;
127
 
127
 
128
    /* read id and get ptr to actual string ("1.15:string") */
128
    /* read id and get ptr to actual string ("1.15:string") */
129
    ptr = parseline(&id, linebuf);
129
    ptr = parseline(&id, linebuf);
130
    if (ptr == NULL) {
130
    if (ptr == NULL) {
131
      printf("ERROR: line #%u of %s is malformed\r\n", linecount, fname);
131
      printf("ERROR: line #%u of %s is malformed (linelen = %u):\r\n", linecount, fname, linelen);
-
 
132
      puts(linebuf);
132
      len = 0;
133
      len = 0;
133
      break;
134
      break;
134
    }
135
    }
135
 
136
 
136
    /* write string into block (II L S) */
137
    /* write string into block (II LL S) */
137
    memcpy(buff + len, &id, 2);
138
    memcpy(buff + len, &id, 2);
138
    len += 2;
139
    len += 2;
-
 
140
    {
139
    buff[len++] = strlen(ptr) + 1;
141
      unsigned short slen = strlen(ptr) + 1;
-
 
142
      memcpy(buff + len, &slen, 2);
-
 
143
      len += 2;
140
    memcpy(buff + len, ptr, strlen(ptr) + 1);
144
      memcpy(buff + len, ptr, slen);
141
    len += strlen(ptr) + 1;
145
      len += slen;
-
 
146
    }
142
 
147
 
143
    /* if reference bitmap provided: check that the id is valid */
148
    /* if reference bitmap provided: check that the id is valid */
144
    if ((refb != NULL) && (bitmap_get(refb, id) == 0)) {
149
    if ((refb != NULL) && (bitmap_get(refb, id) == 0)) {
145
      printf("WARNING: %s[#%u] has an invalid id (%u.%u not present in ref lang)\r\n", fname, linecount, id >> 8, id & 0xff);
150
      printf("WARNING: %s[#%u] has an invalid id (%u.%u not present in ref lang)\r\n", fname, linecount, id >> 8, id & 0xff);
146
    }
151
    }
147
 
152
 
148
    /* make sure this id is not already present */
153
    /* make sure this id is not already present */
149
    if (bitmap_get(b, id) == 0) {
154
    if (bitmap_get(b, id) == 0) {
150
      /* set bit in bitmap to remember I have this string */
155
      /* set bit in bitmap to remember I have this string */
151
      bitmap_set(b, id);
156
      bitmap_set(b, id);
152
    } else {
157
    } else {
153
      printf("WARNING: %s[#%u] has a duplicated id (%u.%u)\r\n", fname, linecount, id >> 8, id & 0xff);
158
      printf("WARNING: %s[#%u] has a duplicated id (%u.%u)\r\n", fname, linecount, id >> 8, id & 0xff);
154
    }
159
    }
155
  }
160
  }
156
 
161
 
157
  fclose(fd);
162
  fclose(fd);
158
 
163
 
159
  /* if refblock provided, pull missing strings from it */
164
  /* if refblock provided, pull missing strings from it */
160
  if (refblock != NULL) {
165
  if (refblock != NULL) {
161
    for (;;) {
166
    for (;;) {
-
 
167
      unsigned short slen;
162
      id = *((unsigned short *)refblock);
168
      id = ((unsigned short *)refblock)[0];
-
 
169
      slen = ((unsigned short *)refblock)[1];
163
      if ((id == 0) && (refblock[2] == 0)) break;
170
      if ((id == 0) && (slen == 0)) break;
164
      if (bitmap_get(b, id) == 0) {
171
      if (bitmap_get(b, id) == 0) {
165
        printf("WARNING: %s is missing string %u.%u (pulled from ref lang)\r\n", fname, id >> 8, id & 0xff);
172
        printf("WARNING: %s is missing string %u.%u (pulled from ref lang)\r\n", fname, id >> 8, id & 0xff);
166
        /* copy missing string from refblock */
173
        /* copy missing string from refblock */
167
        memcpy(buff + len, refblock, refblock[2] + 3);
174
        memcpy(buff + len, refblock, slen + 4);
168
        len += refblock[2] + 3;
175
        len += slen + 4;
169
      }
176
      }
170
      refblock += refblock[2] + 3;
177
      refblock += slen + 4;
171
    }
178
    }
172
  }
179
  }
173
 
180
 
174
  /* write the block terminator (0-long string) */
181
  /* write the block terminator (0-long string) */
175
  buff[len++] = 0; /* id */
182
  buff[len++] = 0; /* id */
176
  buff[len++] = 0; /* id */
183
  buff[len++] = 0; /* id */
177
  buff[len++] = 0; /* len */
184
  buff[len++] = 0; /* len */
-
 
185
  buff[len++] = 0; /* len */
-
 
186
  buff[len++] = 0; /* empty string */
178
 
187
 
179
  return(len);
188
  return(len);
180
}
189
}
181
 
190
 
182
 
191
 
183
#define MEMBLOCKSZ 65000
192
#define MEMBLOCKSZ 65000
184
 
193
 
185
int main(int argc, char **argv) {
194
int main(int argc, char **argv) {
186
  FILE *fd;
195
  FILE *fd;
187
  int ecode = 0;
196
  int ecode = 0;
188
  char *buff, *refblock;
197
  char *buff, *refblock;
189
  static struct bitmap bufbitmap;
198
  static struct bitmap bufbitmap;
190
  static struct bitmap refbitmap;
199
  static struct bitmap refbitmap;
191
  unsigned short i;
200
  unsigned short i;
192
 
201
 
193
  if (argc < 2) {
202
  if (argc < 2) {
194
    puts("usage: tlumacz en fr pl etc");
203
    puts("usage: tlumacz en fr pl etc");
195
    return(1);
204
    return(1);
196
  }
205
  }
197
 
206
 
198
  buff = malloc(MEMBLOCKSZ);
207
  buff = malloc(MEMBLOCKSZ);
199
  refblock = malloc(MEMBLOCKSZ);
208
  refblock = malloc(MEMBLOCKSZ);
200
  if ((buff == NULL) || (refblock == NULL)) {
209
  if ((buff == NULL) || (refblock == NULL)) {
201
    puts("out of memory");
210
    puts("out of memory");
202
    return(1);
211
    return(1);
203
  }
212
  }
204
 
213
 
205
  fd = fopen("out.lng", "wb");
214
  fd = fopen("out.lng", "wb");
206
  if (fd == NULL) {
215
  if (fd == NULL) {
207
    puts("ERR: failed to open or create SVARCOM.LNG");
216
    puts("ERR: failed to open or create SVARCOM.LNG");
208
    return(1);
217
    return(1);
209
  }
218
  }
210
 
219
 
211
  /* write sig */
220
  /* write sig */
212
  fwrite("SvL\x1b", 1, 4, fd);
221
  fwrite("SvL\x1b", 1, 4, fd);
213
 
222
 
214
  /* write lang blocks */
223
  /* write lang blocks */
215
  for (i = 1; i < argc; i++) {
224
  for (i = 1; i < argc; i++) {
216
    unsigned short sz;
225
    unsigned short sz;
217
    char id[3];
226
    char id[3];
218
 
227
 
219
    if (strlen(argv[i]) != 2) {
228
    if (strlen(argv[i]) != 2) {
220
      printf("INVALID LANG SPECIFIED: %s\r\n", argv[i]);
229
      printf("INVALID LANG SPECIFIED: %s\r\n", argv[i]);
221
      ecode = 1;
230
      ecode = 1;
222
      break;
231
      break;
223
    }
232
    }
224
 
233
 
225
    id[0] = argv[i][0];
234
    id[0] = argv[i][0];
226
    id[1] = argv[i][1];
235
    id[1] = argv[i][1];
227
    id[2] = 0;
236
    id[2] = 0;
228
    if (id[0] >= 'a') id[0] -= 'a' - 'A';
237
    if (id[0] >= 'a') id[0] -= 'a' - 'A';
229
    if (id[1] >= 'a') id[1] -= 'a' - 'A';
238
    if (id[1] >= 'a') id[1] -= 'a' - 'A';
230
 
239
 
231
    sz = gen_langstrings(buff, id, &bufbitmap, (i != 1)?&refbitmap:NULL, (i != 1)?refblock:NULL);
240
    sz = gen_langstrings(buff, id, &bufbitmap, (i != 1)?&refbitmap:NULL, (i != 1)?refblock:NULL);
232
    if (sz == 0) {
241
    if (sz == 0) {
233
      printf("ERROR COMPUTING LANG '%s'\r\n", id);
242
      printf("ERROR COMPUTING LANG '%s'\r\n", id);
234
      ecode = 1;
243
      ecode = 1;
235
      break;
244
      break;
236
    } else {
245
    } else {
237
      printf("computed %s lang block of %u bytes\r\n", id, sz);
246
      printf("computed %s lang block of %u bytes\r\n", id, sz);
238
    }
247
    }
239
    /* write lang ID to file, followed by block size and then the actual block */
248
    /* write lang ID to file, followed by block size and then the actual block */
240
    if ((fwrite(id, 1, 2, fd) != 2) ||
249
    if ((fwrite(id, 1, 2, fd) != 2) ||
241
        (fwrite(&sz, 1, 2, fd) != 2) ||
250
        (fwrite(&sz, 1, 2, fd) != 2) ||
242
        (fwrite(buff, 1, sz, fd) != sz)) {
251
        (fwrite(buff, 1, sz, fd) != sz)) {
243
      printf("ERROR WRITING TO OUTPUT FILE\r\n");
252
      printf("ERROR WRITING TO OUTPUT FILE\r\n");
244
      ecode = 1;
253
      ecode = 1;
245
      break;
254
      break;
246
    }
255
    }
247
    /* compute the default block for reference language */
256
    /* compute the default block for reference language */
248
    if (i == 1) {
257
    if (i == 1) {
249
      unsigned short x;
258
      unsigned short x;
250
      FILE *fd2;
259
      FILE *fd2;
251
      fd2 = fopen("DEFLANG.C", "wb");
260
      fd2 = fopen("DEFLANG.C", "wb");
252
      if (fd2 == NULL) {
261
      if (fd2 == NULL) {
253
        puts("ERROR: FAILED TO OPEN OR CREATE DEFLANG.C");
262
        puts("ERROR: FAILED TO OPEN OR CREATE DEFLANG.C");
254
        break;
263
        break;
255
      }
264
      }
256
      fprintf(fd2, "/* THIS FILE HAS BEEN AUTOGENERATE BY TLUMACZ (PART OF THE SVARLANG LIBRARY) */\r\n");
265
      fprintf(fd2, "/* THIS FILE HAS BEEN AUTOGENERATE BY TLUMACZ (PART OF THE SVARLANG LIBRARY) */\r\n");
257
      fprintf(fd2, "const unsigned short svarlang_memsz = %uu;\r\n", sz * 2);
266
      fprintf(fd2, "const unsigned short svarlang_memsz = %uu;\r\n", sz * 2);
258
      fprintf(fd2, "char svarlang_mem[%u] = {\r\n", sz * 2);
267
      fprintf(fd2, "char svarlang_mem[%u] = {\r\n", sz * 2);
259
      for (x = 0; x < sz; x++) {
268
      for (x = 0; x < sz; x++) {
260
        fprintf(fd2, "%u", buff[x]);
269
        fprintf(fd2, "%u", buff[x]);
261
        if (x + 1 < sz) fprintf(fd2, ",");
270
        if (x + 1 < sz) fprintf(fd2, ",");
262
        if ((x & 15) == 15) fprintf(fd2, "\r\n");
271
        if ((x & 15) == 15) fprintf(fd2, "\r\n");
263
      }
272
      }
264
      fprintf(fd2, "};\r\n");
273
      fprintf(fd2, "};\r\n");
265
      fclose(fd2);
274
      fclose(fd2);
266
      /* remember reference data for other languages */
275
      /* remember reference data for other languages */
267
      memcpy(refblock, buff, MEMBLOCKSZ);
276
      memcpy(refblock, buff, MEMBLOCKSZ);
268
      memcpy(&refbitmap, &bufbitmap, sizeof(struct bitmap));
277
      memcpy(&refbitmap, &bufbitmap, sizeof(struct bitmap));
269
    }
278
    }
270
  }
279
  }
271
 
280
 
272
  fclose(fd);
281
  fclose(fd);
273
 
282
 
274
  return(ecode);
283
  return(ecode);
275
}
284
}
276
 
285