Subversion Repositories SvarDOS

Rev

Go to most recent revision | Details | 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
    /* */
85
    if (filetype == 0) {
86
      res++;
87
    } else {
88
      if ((line[0] == ':') && (stringongoing == 0)) stringongoing = 1;
89
      if ((line[0] == '.') && (stringongoing != 0)) {
90
        res++;
91
        stringongoing = 0;
92
      }
93
    }
94
  }
95
  return(res);
96
}
97
 
98
int main(int argc, char **argv) {
99
  char buff[512];
100
  char *lang;
101
  char *pkgfile;
102
  char pkgshortname[64];
103
  char *file;
104
  long filelen;
105
  long result;
106
  int filetype;
107
  int popenres;
108
  #define FILEALLOC 1024*1024
109
  FILE *fd;
110
  /* read arg list */
111
  if ((argc != 4) || (argv[1][0] == '-') || (atoi(argv[3]) > 1) || (atoi(argv[3]) < 0)) {
112
    printf("-1\n");
113
    fprintf(stderr, "svnlschk - Svarog's NLS checker\n"
114
                    "usage: svnlschk pkg.zip lang filetype\n"
115
                    "\n"
116
                    "where filetype is:\n"
117
                    " 0 = standard CATS-like NLS file\n"
118
                    " 1 = FreeCOM-style LNG file\n");
119
    return(1);
120
  }
121
  pkgfile = argv[1];
122
  lang = argv[2];
123
  filetype = atoi(argv[3]);
124
  strcpy(buff, pkgfile);
125
  snprintf(pkgshortname, sizeof(pkgshortname), basename(buff));
126
  trimext(pkgshortname);
127
  /* is this a valid zip archive? */
128
  snprintf(buff, sizeof(buff), "unzip -qq -t %s", pkgfile);
129
  if (system(buff) != 0) {
130
    printf("-1\n");
131
    fprintf(stderr, "ERROR: %s is not a valid ZIP archive!\n", pkgfile);
132
    return(1);
133
  }
134
  /* extract wanted file through popen() and read it into memory */
135
  snprintf(buff, sizeof(buff), "unzip -pC %s nls/%s.%s", pkgfile, pkgshortname, lang);
136
  /* */
137
  fd = popen(buff, "r");
138
  if (fd == NULL) {
139
    printf("-1\n");
140
    fprintf(stderr, "ERROR: popen() failed\n");
141
    return(1);
142
  }
143
  file = malloc(FILEALLOC);
144
  if (file == NULL) {
145
    printf("-1\n");
146
    fprintf(stderr, "ERROR: out of memory\n");
147
    return(1);
148
  }
149
  filelen = fread(file, 1, FILEALLOC - 1, fd);
150
  file[filelen] = 0; /* make sure to NULL-terminate */
151
  result = countnlsstrings(file, filetype);
152
  free(file);
153
  popenres = WEXITSTATUS(pclose(fd));
154
  if (popenres != 0) {
155
    printf("0\n");
156
    return(0);
157
  }
158
  printf("%ld\n", result);
159
  return(0);
160
}