Subversion Repositories SvarDOS

Rev

Rev 102 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
102 mv_fox 1
/*
2
 * svnlschk - Svarog's NLS checker
3
 * Copyright (C) 2016 Mateusz Viste
4
 *
5
 * analyzes a Svarog386 package and outputs a single integer number:
6
 *  -1 error (returns a non-zero errorlevel, too)
7
 *   0 no NLS data
8
 *   x any positive number = number of strings for given language
9
 *
10
 * usage: svnlschk pkg.zip lang filetype
11
 *
12
 * note: this program relies on the availability of info-zip's UNZIP.
13
 */
14
 
15
#include <stdio.h>
16
#include <stdlib.h> /* system() */
17
#include <string.h>
18
#include <libgen.h> /* basename() */
19
 
20
 
21
/* trims the extension part of a filename */
22
static void trimext(char *s) {
23
  char *lastdot = NULL;
24
  /* find last dot, if any */
25
  for (;*s != 0; s++) if (*s == '.') lastdot = s;
26
  /* trim last dot, if any */
27
  if (lastdot != NULL) *lastdot = 0;
28
}
29
 
30
/* trim trailing and white spaces */
31
static void trim(char *str) {
32
  int x, y, firstchar = -1, lastchar = -1;
33
  /* trim out the UTF-8 BOM, if present */
34
  if (((unsigned char)str[0] == 0xEF) && ((unsigned char)str[1] == 0xBB) && ((unsigned char)str[2] == 0xBF)) {
35
    memmove(str, str + 3, strlen(str + 3) + 1);
36
  }
37
  /* */
38
  for (x = 0; str[x] != 0; x++) {
39
    switch (str[x]) {
40
      case ' ':
41
      case '\t':
42
      case '\n':
43
      case '\r':
44
        break;
45
      default:
46
        if (firstchar < 0) firstchar = x;
47
        lastchar = x;
48
        break;
49
    }
50
  }
51
  str[lastchar + 1] = 0; /* right trim */
52
  if (firstchar > 0) { /* left trim (shift to the left ) */
53
    y = 0;
54
    for (x = firstchar; str[x] != 0; x++) str[y++] = str[x];
55
    str[y] = 0;
56
  }
57
}
58
 
59
/* count the number of strings in a CATS-style nls file */
60
static long countnlsstrings(char *file, int filetype) {
61
  /* filetype 0 = NLS file ; 1 = COMMAND-style LNG file */
62
  long res = 0;
63
  int stringongoing = 0;
64
  char line[1024];
65
  while (*file != 0) {
66
    int i = 0;
67
    for (;; file++) {
68
      if (*file == '\r') continue;
69
      if (*file == '\n') {
70
        line[i] = 0;
71
        file++;
72
        break;
73
      }
74
      if (*file == 0) {
75
        line[i] = 0;
76
        break;
77
      }
78
      line[i] = *file;
79
      if (i < 1023) i++;
80
    }
81
    trim(line);
82
    /* skip empty lines and comments */
83
    if ((line[0] == 0) || (line[0] == '#')) continue;
84
    /* */
105 mv_fox 85
    if (filetype == 0) { /* CATS-like */
102 mv_fox 86
      res++;
105 mv_fox 87
    } else if (filetype == 1) { /* lng (FreeCOM) */
102 mv_fox 88
      if ((line[0] == ':') && (stringongoing == 0)) stringongoing = 1;
89
      if ((line[0] == '.') && (stringongoing != 0)) {
90
        res++;
91
        stringongoing = 0;
92
      }
105 mv_fox 93
    } else { /* err (FreeCOM) */
94
      res++;
102 mv_fox 95
    }
96
  }
97
  return(res);
98
}
99
 
100
int main(int argc, char **argv) {
101
  char buff[512];
102
  char *lang;
103
  char *pkgfile;
104
  char pkgshortname[64];
105
  char *file;
106
  long filelen;
107
  long result;
108
  int filetype;
109
  int popenres;
110
  #define FILEALLOC 1024*1024
111
  FILE *fd;
112
  /* read arg list */
105 mv_fox 113
  if ((argc != 4) || (argv[1][0] == '-') || (argv[3][0] > '2') || (argv[3][0] < '0')) {
102 mv_fox 114
    printf("-1\n");
105 mv_fox 115
    fprintf(stderr, "svnlschk - Svarog's NLS checker - Copyright (C) 2016 Mateusz Viste\n"
116
                    "usage: svnlschk pkg.zip lang nlstype\n"
102 mv_fox 117
                    "\n"
105 mv_fox 118
                    "where nlstype is:\n"
102 mv_fox 119
                    " 0 = standard CATS-like NLS file\n"
105 mv_fox 120
                    " 1 = FreeCOM-style LNG file\n"
121
                    " 2 = FreeCOM-style ERR file\n");
102 mv_fox 122
    return(1);
123
  }
124
  pkgfile = argv[1];
125
  lang = argv[2];
126
  filetype = atoi(argv[3]);
127
  strcpy(buff, pkgfile);
128
  snprintf(pkgshortname, sizeof(pkgshortname), basename(buff));
129
  trimext(pkgshortname);
130
  /* is this a valid zip archive? */
131
  snprintf(buff, sizeof(buff), "unzip -qq -t %s", pkgfile);
132
  if (system(buff) != 0) {
133
    printf("-1\n");
134
    fprintf(stderr, "ERROR: %s is not a valid ZIP archive!\n", pkgfile);
135
    return(1);
136
  }
137
  /* extract wanted file through popen() and read it into memory */
105 mv_fox 138
  if (filetype == 0) { /* CATS-like */
139
    snprintf(buff, sizeof(buff), "unzip -pC %s nls/%s.%s", pkgfile, pkgshortname, lang);
140
  } else if (filetype == 1) { /* lng (FreeCOM) */
141
    snprintf(buff, sizeof(buff), "unzip -pC %s source/%s/strings/%s.lng", pkgfile, pkgshortname, lang);
142
  } else { /* err (FreeCOM) */
143
    snprintf(buff, sizeof(buff), "unzip -pC %s source/%s/strings/%s.err", pkgfile, pkgshortname, lang);
144
  }
102 mv_fox 145
  /* */
146
  fd = popen(buff, "r");
147
  if (fd == NULL) {
148
    printf("-1\n");
149
    fprintf(stderr, "ERROR: popen() failed\n");
150
    return(1);
151
  }
152
  file = malloc(FILEALLOC);
153
  if (file == NULL) {
154
    printf("-1\n");
155
    fprintf(stderr, "ERROR: out of memory\n");
156
    return(1);
157
  }
158
  filelen = fread(file, 1, FILEALLOC - 1, fd);
159
  file[filelen] = 0; /* make sure to NULL-terminate */
160
  result = countnlsstrings(file, filetype);
161
  free(file);
162
  popenres = WEXITSTATUS(pclose(fd));
163
  if (popenres != 0) {
164
    printf("0\n");
165
    return(0);
166
  }
167
  printf("%ld\n", result);
168
  return(0);
169
}