Subversion Repositories SvarDOS

Rev

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

Rev 1296 Rev 2014
Line 18... Line 18...
18
#include "svarlang.h"
18
#include "svarlang.h"
19
 
19
 
20
#define STRINGS_CAP 65000   /* string storage size in characters */
20
#define STRINGS_CAP 65000   /* string storage size in characters */
21
#define DICT_CAP    10000   /* dictionary size in elements */
21
#define DICT_CAP    10000   /* dictionary size in elements */
22
 
22
 
-
 
23
enum {                      /* DEFLANG output format */
-
 
24
  C_OUTPUT,
-
 
25
  ASM_OUTPUT,
-
 
26
  NASM_OUTPUT
-
 
27
};
-
 
28
 
-
 
29
 
23
/* read a single line from fd and fills it into dst, returns line length
30
/* read a single line from fd and fills it into dst, returns line length
24
 * ending CR/LF is trimmed, as well as any trailing spaces */
31
 * ending CR/LF is trimmed, as well as any trailing spaces */
25
static unsigned short readl(char *dst, size_t dstsz, FILE *fd) {
32
static unsigned short readl(char *dst, size_t dstsz, FILE *fd) {
26
  unsigned short l, lastnonspace = 0;
33
  unsigned short l, lastnonspace = 0;
27
 
34
 
Line 282... Line 289...
282
 
289
 
283
    /* add the string contained in current line, if conditions are met */
290
    /* add the string contained in current line, if conditions are met */
284
    if (!svl_find(l, id)) {
291
    if (!svl_find(l, id)) {
285
      if ((refl == NULL) || (svl_find(refl, id))) {
292
      if ((refl == NULL) || (svl_find(refl, id))) {
286
        if (!svl_add_str(l, id, ptr)) {
293
        if (!svl_add_str(l, id, ptr)) {
287
          printf("ERROR: %s[#%u] output size limit exceeded\r\n", fname, linecount);
294
          fprintf(stderr, "ERROR: %s[#%u] output size limit exceeded\r\n", fname, linecount);
288
          fclose(fd);
295
          fclose(fd);
289
          return(0);
296
          return(0);
290
        }
297
        }
291
        if (id >= maxid) {
298
        if (id >= maxid) {
292
          maxid = id;
299
          maxid = id;
Line 309... Line 316...
309
    for (i = 0; i < refl->num_strings; i++) {
316
    for (i = 0; i < refl->num_strings; i++) {
310
      id = refl->dict[i].id;
317
      id = refl->dict[i].id;
311
      if (!svl_find(l, id)) {
318
      if (!svl_find(l, id)) {
312
        printf("WARNING: %s is missing string %u.%u (pulled from ref lang)\r\n", fname, id >> 8, id & 0xff);
319
        printf("WARNING: %s is missing string %u.%u (pulled from ref lang)\r\n", fname, id >> 8, id & 0xff);
313
        if (!svl_add_str(l, id, refl->strings + refl->dict[i].offset)) {
320
        if (!svl_add_str(l, id, refl->strings + refl->dict[i].offset)) {
314
          printf("ERROR: %s[#%u] output size limit exceeded\r\n", fname, linecount);
321
          fprintf(stderr, "ERROR: %s[#%u] output size limit exceeded\r\n", fname, linecount);
315
          return(0);
322
          return(0);
316
        }
323
        }
317
      }
324
      }
318
    }
325
    }
319
  }
326
  }
Line 344... Line 351...
344
  unsigned short nextnlat = 0;
351
  unsigned short nextnlat = 0;
345
  unsigned short allocsz;
352
  unsigned short allocsz;
346
 
353
 
347
  fd = fopen(fn, "wb");
354
  fd = fopen(fn, "wb");
348
  if (fd == NULL) {
355
  if (fd == NULL) {
349
    puts("ERROR: FAILED TO OPEN OR CREATE DEFLANG.C");
-
 
350
    return(0);
356
    return(0);
351
  }
357
  }
352
 
358
 
353
  allocsz = biggest_langsz + (biggest_langsz / 20);
359
  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);
360
  printf("biggest lang block is %u bytes -> allocating a %u bytes buffer (5%% safety margin)\n", biggest_langsz, allocsz);
Line 387... Line 393...
387
 
393
 
388
  return(1);
394
  return(1);
389
}
395
}
390
 
396
 
391
 
397
 
-
 
398
static int svl_write_asm_source(const struct svl_lang *l, const char *fn, unsigned short biggest_langsz, int format) {
-
 
399
  FILE *fd;
-
 
400
  int i;
-
 
401
  unsigned short strings_bytes = svl_strings_bytes(l);
-
 
402
  unsigned short nextnlat = 0;
-
 
403
  unsigned short allocsz;
-
 
404
 
-
 
405
  const char *public = (format == ASM_OUTPUT) ? "public" : "global";
-
 
406
 
-
 
407
  fd = fopen(fn, "wb");
-
 
408
  if (fd == NULL) {
-
 
409
    return(0);
-
 
410
  }
-
 
411
 
-
 
412
  allocsz = biggest_langsz + (biggest_langsz / 20);
-
 
413
  printf("biggest lang block is %u bytes -> allocating a %u bytes buffer (5%% safety margin)\n", biggest_langsz, allocsz);
-
 
414
  fprintf(fd, "; THIS FILE HAS BEEN GENERATED BY TLUMACZ (PART OF THE SVARLANG LIBRARY)\r\n");
-
 
415
  fprintf(fd, "%s svarlang_memsz\r\n", public);
-
 
416
  fprintf(fd, "svarlang_memsz dw %u\r\n", allocsz);
-
 
417
  fprintf(fd, "%s svarlang_string_count\r\n", public);
-
 
418
  fprintf(fd, "svarlang_string_count dw %u\r\n\r\n", l->num_strings);
-
 
419
  fprintf(fd, "%s svarlang_mem\r\n", public);
-
 
420
  fprintf(fd, "svarlang_mem:\r\n");
-
 
421
 
-
 
422
  if (strings_bytes > 0) fprintf(fd, "db ");
-
 
423
 
-
 
424
  for (i = 0; i < strings_bytes; i++) {
-
 
425
    if (!fprintf(fd, "%02xh", l->strings[i])) {
-
 
426
      fclose(fd);
-
 
427
      return(0);
-
 
428
    }
-
 
429
 
-
 
430
    nextnlat++;
-
 
431
    if (l->strings[i] == '\0' || nextnlat == 16) {
-
 
432
      fprintf(fd, "\r\n");
-
 
433
      if (i + 1 < strings_bytes ) fprintf(fd, "db ");
-
 
434
      nextnlat = 0;
-
 
435
    }
-
 
436
    else {
-
 
437
      fprintf(fd, ",");
-
 
438
    }
-
 
439
  }
-
 
440
 
-
 
441
  fprintf(fd, "\r\n%s svarlang_dict\r\n", public);
-
 
442
  fprintf(fd, "svarlang_dict:\r\n");
-
 
443
  for (i = 0; i < l->num_strings; i++) {
-
 
444
    if (!fprintf(fd, "dw %04xh,%04xh\r\n", l->dict[i].id, l->dict[i].offset)) {
-
 
445
      fclose(fd);
-
 
446
      return(0);
-
 
447
    }
-
 
448
  }
-
 
449
 
-
 
450
  fclose(fd);
-
 
451
 
-
 
452
  return(1);
-
 
453
}
-
 
454
 
-
 
455
 
392
int main(int argc, char **argv) {
456
int main(int argc, char **argv) {
393
  FILE *fd;
457
  FILE *fd;
394
  int ecode = 0;
458
  int ecode = 0;
395
  int i;
459
  int i, output_format = C_OUTPUT;
396
  unsigned short biggest_langsz = 0;
460
  unsigned short biggest_langsz = 0;
397
  struct svl_lang *lang, *reflang = NULL;
461
  struct svl_lang *lang, *reflang = NULL;
398
 
462
 
399
  if (argc < 2) {
463
  if (argc < 2) {
400
    puts("tlumacz ver " SVARLANGVER " - this tool is part of the SvarLANG project.");
464
    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");
465
    puts("converts a set of CATS-style translations in files EN.TXT, PL.TXT, etc");
402
    puts("into a single resource file (OUT.LNG).");
466
    puts("into a single resource file (OUT.LNG).");
403
    puts("");
467
    puts("");
404
    puts("usage: tlumacz en fr pl ...");
468
    puts("usage: tlumacz [/c|/asm|/nasm] en fr pl ...");
405
    return(1);
469
    return(1);
406
  }
470
  }
407
 
471
 
408
  fd = fopen("out.lng", "wb");
472
  fd = fopen("out.lng", "wb");
409
  if (fd == NULL) {
473
  if (fd == NULL) {
410
    puts("ERR: failed to open or create OUT.LNG");
474
    fprintf(stderr, "ERROR: FAILED TO CREATE OR OPEN OUT.LNG");
411
    return(1);
475
    return(1);
412
  }
476
  }
413
 
477
 
414
  /* write lang blocks */
478
  /* write lang blocks */
415
  for (i = 1; i < argc; i++) {
479
  for (i = 1; i < argc; i++) {
416
    unsigned short sz;
480
    unsigned short sz;
417
    char id[3];
481
    char id[3];
418
 
482
 
-
 
483
    if (!strcmp(argv[i], "/c")) {
-
 
484
      output_format = C_OUTPUT;
-
 
485
      continue;
-
 
486
    }
-
 
487
    else if (!strcmp(argv[i], "/asm")) {
-
 
488
      output_format = ASM_OUTPUT;
-
 
489
      continue;
-
 
490
    } else if(!strcmp(argv[i], "/nasm")) {
-
 
491
      output_format = NASM_OUTPUT;
-
 
492
      continue;
-
 
493
    }
-
 
494
 
419
    if (strlen(argv[i]) != 2) {
495
    if (strlen(argv[i]) != 2) {
420
      printf("INVALID LANG SPECIFIED: %s\r\n", argv[i]);
496
      fprintf(stderr, "INVALID LANG SPECIFIED: %s\r\n", argv[i]);
421
      ecode = 1;
497
      ecode = 1;
422
      break;
498
      break;
423
    }
499
    }
424
    id[0] = argv[i][0];
500
    id[0] = argv[i][0];
425
    id[1] = argv[i][1];
501
    id[1] = argv[i][1];
426
    id[2] = 0;
502
    id[2] = 0;
427
 
503
 
428
    if ((lang = svl_lang_new(id, DICT_CAP, STRINGS_CAP)) == NULL) {
504
    if ((lang = svl_lang_new(id, DICT_CAP, STRINGS_CAP)) == NULL) {
429
      printf("OUT OF MEMORY\r\n");
505
      fprintf(stderr, "OUT OF MEMORY\r\n");
430
      return(1);
506
      return(1);
431
    }
507
    }
432
 
508
 
433
    sz = svl_lang_from_cats_file(lang, reflang);
509
    sz = svl_lang_from_cats_file(lang, reflang);
434
    if (sz == 0) {
510
    if (sz == 0) {
435
      printf("ERROR COMPUTING LANG '%s'\r\n", id);
511
      fprintf(stderr, "ERROR COMPUTING LANG '%s'\r\n", id);
436
      ecode = 1;
512
      ecode = 1;
437
      break;
513
      break;
438
    } else {
514
    } else {
439
      printf("computed %s lang block of %u bytes\r\n", id, sz);
515
      printf("computed %s lang block of %u bytes\r\n", id, sz);
440
      if (sz > biggest_langsz) biggest_langsz = sz;
516
      if (sz > biggest_langsz) biggest_langsz = sz;
Line 442... Line 518...
442
    svl_compact_lang(lang);
518
    svl_compact_lang(lang);
443
 
519
 
444
    /* write header if first (reference) language */
520
    /* write header if first (reference) language */
445
    if (i == 1) {
521
    if (i == 1) {
446
      if (!svl_write_header(lang->num_strings, fd)) {
522
      if (!svl_write_header(lang->num_strings, fd)) {
447
        printf("ERROR WRITING TO OUTPUT FILE\r\n");
523
        fprintf(stderr, "ERROR WRITING TO OUTPUT FILE\r\n");
448
        ecode = 1;
524
        ecode = 1;
449
        break;
525
        break;
450
      }
526
      }
451
    }
527
    }
452
 
528
 
453
    /* write lang ID to file, followed string table size, and then
529
    /* write lang ID to file, followed string table size, and then
454
       the dictionary and string table for current language */
530
       the dictionary and string table for current language */
455
    if (!svl_write_lang(lang, fd)) {
531
    if (!svl_write_lang(lang, fd)) {
456
      printf("ERROR WRITING TO OUTPUT FILE\r\n");
532
      fprintf(stderr, "ERROR WRITING TO OUTPUT FILE\r\n");
457
      ecode = 1;
533
      ecode = 1;
458
      break;
534
      break;
459
    }
535
    }
460
 
536
 
461
    /* remember reference data for other languages */
537
    /* remember reference data for other languages */
462
    if (i == 1) {
538
    if (!reflang) {
463
      reflang = lang;
539
      reflang = lang;
464
    } else {
540
    } else {
465
      svl_lang_free(lang);
541
      svl_lang_free(lang);
466
      lang = NULL;
542
      lang = NULL;
467
    }
543
    }
468
  }
544
  }
469
 
545
 
470
  /* compute the deflang.c file containing a dump of the reference block */
546
  if (ecode) return ecode;
-
 
547
 
471
  if (!svl_write_c_source(reflang, "deflang.c", biggest_langsz)) {
548
  if (!reflang) {
472
    puts("ERROR: FAILED TO OPEN OR CREATE DEFLANG.C");
549
    fprintf(stderr, "ERROR: NO LANGUAGE GIVEN\r\n");
473
    ecode = 1;
550
    return 1;
474
  }
551
  }
475
 
552
 
-
 
553
  /* compute the deflang file containing a dump of the reference block */
-
 
554
  if (output_format == C_OUTPUT) {
-
 
555
    if (!svl_write_c_source(reflang, "deflang.c", biggest_langsz)) {
-
 
556
      fprintf(stderr, "ERROR: FAILED TO OPEN OR CREATE DEFLANG.C\r\n");
476
  /* clean up */
557
      ecode = 1;
-
 
558
    }    
477
  if (reflang) {
559
  } else {
478
    svl_lang_free(reflang);
560
    if (!svl_write_asm_source(reflang, "deflang.inc", biggest_langsz, output_format)) {
-
 
561
      fprintf(stderr, "ERROR: FAILED TO OPEN OR CREATE DEFLANG.INC\r\n");
479
    reflang = NULL;
562
      ecode = 1;
-
 
563
    }
480
  }
564
  }
481
 
565
 
-
 
566
  svl_lang_free(reflang);
-
 
567
  reflang = NULL;
-
 
568
 
482
  return(ecode);
569
  return(ecode);
483
}
570
}