Subversion Repositories SvarDOS

Rev

Rev 1295 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1295 Rev 1296
Line 99... Line 99...
99
  }
99
  }
100
  return(i);
100
  return(i);
101
}
101
}
102
 
102
 
103
#pragma pack(1)
103
#pragma pack(1)
104
typedef struct dict_entry {
104
struct dict_entry {
105
  unsigned short id;
105
  unsigned short id;
106
  unsigned short offset;
106
  unsigned short offset;
107
} dict_entry_t;
107
};
108
#pragma pack()
108
#pragma pack()
109
 
109
 
110
typedef struct svl_lang {
110
struct svl_lang {
111
  char id[2];
111
  char id[2];
112
  unsigned short num_strings;
112
  unsigned short num_strings;
113
 
113
 
114
  dict_entry_t *dict;
114
  struct dict_entry *dict;
115
  size_t dict_cap;
115
  size_t dict_cap;
116
 
116
 
117
  char *strings;
117
  char *strings;
118
  char *strings_end;
118
  char *strings_end;
119
  size_t strings_cap;
119
  size_t strings_cap;
120
 
120
 
121
} svl_lang_t;
121
};
122
 
122
 
123
 
123
 
124
static svl_lang_t *svl_lang_new(const char langid[2], size_t dict_cap, size_t strings_cap) {
124
static struct svl_lang *svl_lang_new(const char langid[2], size_t dict_cap, size_t strings_cap) {
125
  svl_lang_t *l;
125
  struct svl_lang *l;
126
 
126
 
127
  l = malloc(sizeof(svl_lang_t));
127
  l = malloc(sizeof(struct svl_lang));
128
  if (!l) return(NULL);
128
  if (!l) return(NULL);
129
 
129
 
130
  l->id[0] = (char)toupper(langid[0]);
130
  l->id[0] = (char)toupper(langid[0]);
131
  l->id[1] = (char)toupper(langid[1]);
131
  l->id[1] = (char)toupper(langid[1]);
132
 
132
 
133
  l->dict = malloc(dict_cap * sizeof(dict_entry_t));
133
  l->dict = malloc(dict_cap * sizeof(struct dict_entry));
134
  if (!l->dict) return(NULL);
134
  if (!l->dict) 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;
Line 146... Line 146...
146
  return(l);
146
  return(l);
147
}
147
}
148
 
148
 
149
 
149
 
150
/* compacts the dict and string buffer */
150
/* compacts the dict and string buffer */
151
static void svl_compact_lang(svl_lang_t *l) {
151
static void svl_compact_lang(struct svl_lang *l) {
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(struct dict_entry));
161
}
161
}
162
 
162
 
163
 
163
 
164
static void svl_lang_free(svl_lang_t *l) {
164
static void svl_lang_free(struct svl_lang *l) {
165
  l->num_strings = 0;
165
  l->num_strings = 0;
166
  if (l->dict) {
166
  if (l->dict) {
167
    free(l->dict);
167
    free(l->dict);
168
    l->dict = NULL;
168
    l->dict = NULL;
169
  }
169
  }
Line 174... Line 174...
174
  l->dict_cap = 0;
174
  l->dict_cap = 0;
175
  l->strings_cap = 0;
175
  l->strings_cap = 0;
176
}
176
}
177
 
177
 
178
 
178
 
179
static size_t svl_strings_bytes(const svl_lang_t *l) {
179
static size_t svl_strings_bytes(const struct svl_lang *l) {
180
  return(l->strings_end - l->strings);
180
  return(l->strings_end - l->strings);
181
}
181
}
182
 
182
 
183
 
183
 
184
static size_t svl_dict_bytes(const svl_lang_t *l) {
184
static size_t svl_dict_bytes(const struct svl_lang *l) {
185
  return(l->num_strings * sizeof(dict_entry_t));
185
  return(l->num_strings * sizeof(struct dict_entry));
186
}
186
}
187
 
187
 
188
 
188
 
189
static int svl_add_str(svl_lang_t *l, unsigned short id, const char *s) {
189
static int svl_add_str(struct svl_lang *l, unsigned short id, const char *s) {
190
  size_t len = strlen(s) + 1;
190
  size_t len = strlen(s) + 1;
191
  size_t cursor;
191
  size_t cursor;
192
 
192
 
193
  if ((l->strings_cap < svl_strings_bytes(l) + len) || (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(struct dict_entry))) {
194
    return(0);
194
    return(0);
195
  }
195
  }
196
 
196
 
197
  /* find dictionary insert position, search backwards in assumption
197
  /* find dictionary insert position, search backwards in assumption
198
     that in translation files, strings are generally ordered ascending */
198
     that in translation files, strings are generally ordered ascending */
199
  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--);
200
 
200
 
201
  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(struct dict_entry) * (l->num_strings - cursor));
202
  l->dict[cursor].id = id;
202
  l->dict[cursor].id = id;
203
  l->dict[cursor].offset = l->strings_end - l->strings;
203
  l->dict[cursor].offset = l->strings_end - l->strings;
204
 
204
 
205
  memcpy(l->strings_end, s, len);
205
  memcpy(l->strings_end, s, len);
206
  l->strings_end += len;
206
  l->strings_end += len;
Line 208... Line 208...
208
 
208
 
209
  return(1);
209
  return(1);
210
}
210
}
211
 
211
 
212
 
212
 
213
static int svl_find(const svl_lang_t *l, unsigned short id) {
213
static int svl_find(const struct svl_lang *l, unsigned short id) {
214
  size_t left = 0, right = l->num_strings - 1, x;
214
  size_t left = 0, right = l->num_strings - 1, x;
215
  unsigned short v;
215
  unsigned short v;
216
 
216
 
217
  if (l->num_strings == 0) return(0);
217
  if (l->num_strings == 0) return(0);
218
 
218
 
219
  while (left <= right ) {
219
  while (left <= right ) {
220
    x = left + ( (right - left ) >> 2 );
220
    x = left + ( (right - left ) >> 2 );
221
    v = l->dict[x].id;
221
    v = l->dict[x].id;
222
    if ( id == v ) {
222
    if ( id == v ) return(1); /* found! */
223
      return(1);
223
 
224
    } else if (id > v) {
224
    if (id > v) {
225
      left = x + 1;
225
      left = x + 1;
226
    } else {
226
    } else {
227
      right = x - 1;
227
      right = x - 1;
228
    }
228
    }
229
  }
229
  }
Line 231... Line 231...
231
}
231
}
232
 
232
 
233
 
233
 
234
/* 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
235
 * 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 */
236
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(struct svl_lang *l, struct svl_lang *refl) {
237
  unsigned short linelen;
237
  unsigned short linelen;
238
  FILE *fd;
238
  FILE *fd;
239
  char fname[] = "xx.txt";
239
  char fname[] = "xx.txt";
240
  static char linebuf[8192];
240
  static char linebuf[8192];
241
  const char *ptr;
241
  const char *ptr;
Line 325... Line 325...
325
static int svl_write_header(unsigned short num_strings, FILE *fd) {
325
static int svl_write_header(unsigned short num_strings, FILE *fd) {
326
  return((fwrite("SvL\x1a", 1, 4, fd) == 4) && (fwrite(&num_strings, 1, 2, fd) == 2));
326
  return((fwrite("SvL\x1a", 1, 4, fd) == 4) && (fwrite(&num_strings, 1, 2, fd) == 2));
327
}
327
}
328
 
328
 
329
 
329
 
330
static int svl_write_lang(const svl_lang_t *l, FILE *fd) {
330
static int svl_write_lang(const struct svl_lang *l, FILE *fd) {
331
  unsigned short strings_bytes = svl_strings_bytes(l);
331
  unsigned short strings_bytes = svl_strings_bytes(l);
332
 
332
 
333
  return((fwrite(&l->id, 1, 2, fd) == 2) &&
333
  return((fwrite(&l->id, 1, 2, fd) == 2) &&
334
         (fwrite(&strings_bytes, 1, 2, fd) == 2) &&
334
         (fwrite(&strings_bytes, 1, 2, fd) == 2) &&
335
         (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)) &&
336
         (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)));
337
}
337
}
338
 
338
 
339
 
339
 
340
static int svl_write_c_source(const svl_lang_t *l, const char *fn, unsigned short biggest_langsz) {
340
static int svl_write_c_source(const struct svl_lang *l, const char *fn, unsigned short biggest_langsz) {
341
  FILE *fd;
341
  FILE *fd;
342
  int i;
342
  int i;
343
  unsigned short strings_bytes = svl_strings_bytes(l);
343
  unsigned short strings_bytes = svl_strings_bytes(l);
344
  unsigned short nextnlat = 0;
344
  unsigned short nextnlat = 0;
345
  unsigned short allocsz;
345
  unsigned short allocsz;
Line 392... Line 392...
392
int main(int argc, char **argv) {
392
int main(int argc, char **argv) {
393
  FILE *fd;
393
  FILE *fd;
394
  int ecode = 0;
394
  int ecode = 0;
395
  int i;
395
  int i;
396
  unsigned short biggest_langsz = 0;
396
  unsigned short biggest_langsz = 0;
397
  svl_lang_t *lang, *reflang = NULL;
397
  struct svl_lang *lang, *reflang = NULL;
398
 
398
 
399
  if (argc < 2) {
399
  if (argc < 2) {
400
    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.");
401
    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");
402
    puts("into a single resource file (OUT.LNG).");
402
    puts("into a single resource file (OUT.LNG).");