Subversion Repositories SvarDOS

Rev

Rev 437 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 437 Rev 539
Line 28... Line 28...
28
#include <stdio.h>
28
#include <stdio.h>
29
#include <stdlib.h>
29
#include <stdlib.h>
30
#include <string.h>
30
#include <string.h>
31
 
31
 
32
 
32
 
-
 
33
 
-
 
34
struct bitmap {
-
 
35
  unsigned char bits[8192];
-
 
36
};
-
 
37
 
-
 
38
static void bitmap_set(struct bitmap *b, unsigned short id) {
-
 
39
  b->bits[id / 8] |= 1 << (id & 7);
-
 
40
}
-
 
41
 
-
 
42
static int bitmap_get(const struct bitmap *b, unsigned short id) {
-
 
43
  return(b->bits[id / 8] & (1 << (id & 7)));
-
 
44
}
-
 
45
 
-
 
46
static void bitmap_init(struct bitmap *b) {
-
 
47
  memset(b, 0, sizeof(struct bitmap));
-
 
48
}
-
 
49
 
-
 
50
 
-
 
51
 
33
/* read a single line from fd and fills it into dst, returns line length
52
/* read a single line from fd and fills it into dst, returns line length
34
 * ending CR/LF is trimmed, as well as any trailing spaces */
53
 * ending CR/LF is trimmed, as well as any trailing spaces */
35
static unsigned short readl(char *dst, size_t dstsz, FILE *fd) {
54
static unsigned short readl(char *dst, size_t dstsz, FILE *fd) {
36
  unsigned short l, lastnonspace = 0;
55
  unsigned short l, lastnonspace = 0;
37
 
56
 
Line 81... Line 100...
81
  return(s + colpos + 1);
100
  return(s + colpos + 1);
82
}
101
}
83
 
102
 
84
 
103
 
85
/* opens a CATS-style file and compiles it into a ressources lang block */
104
/* opens a CATS-style file and compiles it into a ressources lang block */
86
static unsigned short gen_langstrings(unsigned char *buff, const char *langid) {
105
static unsigned short gen_langstrings(unsigned char *buff, const char *langid, struct bitmap *b, const struct bitmap *refb, const unsigned char *refblock) {
87
  unsigned short len = 0, linelen;
106
  unsigned short len = 0, linelen;
88
  FILE *fd;
107
  FILE *fd;
89
  char fname[] = "NLS\\SVARCOM.XX";
108
  char fname[] = "NLS\\SVARCOM.XX";
90
  char linebuf[512];
109
  char linebuf[512];
91
  char *ptr;
110
  char *ptr;
92
  unsigned short id, linecount;
111
  unsigned short id, linecount;
93
 
112
 
-
 
113
  bitmap_init(b);
-
 
114
 
94
  strcpy(fname + strlen(fname) - 2, langid);
115
  strcpy(fname + strlen(fname) - 2, langid);
95
 
116
 
96
  fd = fopen(fname, "rb");
117
  fd = fopen(fname, "rb");
97
  if (fd == NULL) {
118
  if (fd == NULL) {
98
    printf("ERROR: FAILED TO OPEN '%s'\r\n", fname);
119
    printf("ERROR: FAILED TO OPEN '%s'\r\n", fname);
Line 117... Line 138...
117
    memcpy(buff + len, &id, 2);
138
    memcpy(buff + len, &id, 2);
118
    len += 2;
139
    len += 2;
119
    buff[len++] = strlen(ptr) + 1;
140
    buff[len++] = strlen(ptr) + 1;
120
    memcpy(buff + len, ptr, strlen(ptr) + 1);
141
    memcpy(buff + len, ptr, strlen(ptr) + 1);
121
    len += strlen(ptr) + 1;
142
    len += strlen(ptr) + 1;
-
 
143
 
-
 
144
    /* if reference bitmap provided: check that the id is valid */
-
 
145
    if ((refb != NULL) && (bitmap_get(refb, id) == 0)) {
-
 
146
      printf("WARNING: %s[#%u] has an invalid id (%u.%u not present in ref lang)\r\n", fname, linecount, id >> 8, id & 0xff);
-
 
147
    }
-
 
148
 
-
 
149
    /* make sure this id is not already present */
-
 
150
    if (bitmap_get(b, id) == 0) {
-
 
151
      /* set bit in bitmap to remember I have this string */
-
 
152
      bitmap_set(b, id);
-
 
153
    } else {
-
 
154
      printf("WARNING: %s[#%u] has a duplicated id (%u.%u)\r\n", fname, linecount, id >> 8, id & 0xff);
-
 
155
    }
-
 
156
  }
-
 
157
 
-
 
158
  fclose(fd);
-
 
159
 
-
 
160
  /* if refblock provided, pull missing strings from it */
-
 
161
  if (refblock != NULL) {
-
 
162
    for (;;) {
-
 
163
      id = *((unsigned short *)refblock);
-
 
164
      if ((id == 0) && (refblock[2] == 0)) break;
-
 
165
      if (bitmap_get(b, id) == 0) {
-
 
166
        printf("WARNING: %s is missing string %u.%u (pulled from ref lang)\r\n", fname, id >> 8, id & 0xff);
-
 
167
        /* copy missing string from refblock */
-
 
168
        memcpy(buff + len, refblock, refblock[2] + 3);
-
 
169
        len += refblock[2] + 3;
-
 
170
      }
-
 
171
      refblock += refblock[2] + 3;
-
 
172
    }
122
  }
173
  }
123
 
174
 
124
  /* write the block terminator (0-long string) */
175
  /* write the block terminator (0-long string) */
125
  buff[len++] = 0; /* id */
176
  buff[len++] = 0; /* id */
126
  buff[len++] = 0; /* id */
177
  buff[len++] = 0; /* id */
127
  buff[len++] = 0; /* len */
178
  buff[len++] = 0; /* len */
128
 
179
 
129
  fclose(fd);
-
 
130
 
-
 
131
  return(len);
180
  return(len);
132
}
181
}
133
 
182
 
134
 
183
 
-
 
184
#define MEMBLOCKSZ 65500
-
 
185
 
135
int main(int argc, char **argv) {
186
int main(int argc, char **argv) {
136
  FILE *fd;
187
  FILE *fd;
137
  int ecode = 0;
188
  int ecode = 0;
138
  char *buff;
189
  char *buff, *refblock;
-
 
190
  static struct bitmap bufbitmap;
-
 
191
  static struct bitmap refbitmap;
139
  unsigned short i;
192
  unsigned short i;
140
 
193
 
141
  if (argc < 2) {
194
  if (argc < 2) {
142
    puts("usage: tlumacz en fr pl etc");
195
    puts("usage: tlumacz en fr pl etc");
143
    return(1);
196
    return(1);
144
  }
197
  }
145
 
198
 
146
  buff = malloc(65500);
199
  buff = malloc(MEMBLOCKSZ);
-
 
200
  refblock = malloc(MEMBLOCKSZ);
147
  if (buff == NULL) {
201
  if ((buff == NULL) || (refblock == NULL)) {
148
    puts("out of memory");
202
    puts("out of memory");
149
    return(1);
203
    return(1);
150
  }
204
  }
151
 
205
 
152
  fd = fopen("svarcom.lng", "wb");
206
  fd = fopen("svarcom.lng", "wb");
Line 173... Line 227...
173
    id[1] = argv[i][1];
227
    id[1] = argv[i][1];
174
    id[2] = 0;
228
    id[2] = 0;
175
    if (id[0] >= 'a') id[0] -= 'a' - 'A';
229
    if (id[0] >= 'a') id[0] -= 'a' - 'A';
176
    if (id[1] >= 'a') id[1] -= 'a' - 'A';
230
    if (id[1] >= 'a') id[1] -= 'a' - 'A';
177
 
231
 
178
    sz = gen_langstrings(buff, id);
232
    sz = gen_langstrings(buff, id, &bufbitmap, (i != 1)?&refbitmap:NULL, (i != 1)?refblock:NULL);
179
    if (sz == 0) {
233
    if (sz == 0) {
180
      printf("ERROR COMPUTING LANG '%s'\r\n", id);
234
      printf("ERROR COMPUTING LANG '%s'\r\n", id);
181
      ecode = 1;
235
      ecode = 1;
182
      break;
236
      break;
183
    } else {
237
    } else {
Line 189... Line 243...
189
        (fwrite(buff, 1, sz, fd) != sz)) {
243
        (fwrite(buff, 1, sz, fd) != sz)) {
190
      printf("ERROR WRITING TO OUTPUT FILE\r\n");
244
      printf("ERROR WRITING TO OUTPUT FILE\r\n");
191
      ecode = 1;
245
      ecode = 1;
192
      break;
246
      break;
193
    }
247
    }
194
    /* if EN, then it is also the default block */
248
    /* compute the default block for reference language */
195
    if (strcmp(id, "EN") == 0) {
249
    if (i == 1) {
196
      FILE *fd2;
250
      FILE *fd2;
197
      fd2 = fopen("DEFAULT.LNG", "wb");
251
      fd2 = fopen("DEFAULT.LNG", "wb");
198
      if (fd2 == NULL) {
252
      if (fd2 == NULL) {
199
        puts("ERROR: FAILED TO OPEN OR CREATE DEFAULT.LNG");
253
        puts("ERROR: FAILED TO OPEN OR CREATE DEFAULT.LNG");
200
        break;
254
        break;
201
      }
255
      }
202
      fwrite(id, 1, 2, fd2);    /* lang block id */
256
      fwrite(id, 1, 2, fd2);    /* lang block id */
203
      fwrite(&sz, 1, 2, fd2);   /* lang block size */
257
      fwrite(&sz, 1, 2, fd2);   /* lang block size */
204
      fwrite(buff, 1, sz, fd2); /* langblock content (strings) */
258
      fwrite(buff, 1, sz, fd2); /* langblock content (strings) */
205
      fclose(fd2);
259
      fclose(fd2);
-
 
260
      /* remember reference data for other languages */
-
 
261
      memcpy(refblock, buff, MEMBLOCKSZ);
-
 
262
      memcpy(&refbitmap, &bufbitmap, sizeof(struct bitmap));
206
    }
263
    }
207
  }
264
  }
208
 
265
 
209
  fclose(fd);
266
  fclose(fd);
210
 
267