summaryrefslogtreecommitdiff
path: root/app/i18n/stripmsg.c
blob: 1e3dc363dd07b7b6531aac22d381950b537aa3eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2008 Mikko Nissinen
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/*
 * Input file types:
 *  XTR = Marco file. Parse MESSAGE...END blocks. 
 *  XTQ = Configuration file. Parse DEMOGROUP and DEMO lines.
 *  TIP = Tip of the day file.
 */
typedef enum { MODE_XTR, MODE_XTQ, MODE_TIP } mode_e;


/* Process the given input file. */
void process( mode_e mode, FILE * inFile )
{
	char line[4096];
	char * cp;
	int len;
	int offset;
	int i;

	while ( fgets( line, sizeof(line), inFile ) != NULL )
	{
		offset = 0;

		switch (mode)
		{
		case MODE_XTR:
			if (strncmp( line, "MESSAGE", 7 ) == 0)
			{
				while ( ( fgets( line, sizeof(line), inFile ) ) != NULL ) {
					if ( strncmp(line, "END", 3) == 0)
						/* End of message block */
						break;
					else if (strncmp(line, "__________", 10) == 0
							|| strncmp(line, "==========", 10) == 0)
						/* Skip */
						continue;

					len = (int)strlen( line );
					if (len > 0 && line[len-1] == '\n' ) len--;
					if (len > 0 && line[len-1] == '\r' ) len--;
					line[len] = '\0';
					if (len > 0)
					{
						if (strchr(line, '"'))
						{
							printf("N_(\"");
							for (i = 0; i < len; i++)
							{
								/* Escape double quotation marks */
								if (line[i] == '"')
									putchar('\\');
								putchar(line[i]);
							}
							printf("\\n\");\n");
						}
						else
						{
							printf("N_(\"%s\\n\");\n", line);
						}
					}
				} // while (in msg block)
			}
			break; // case MODE_XTR:

		case MODE_XTQ:
			if ( strncmp( line, "DEMOGROUP ", 10 ) == 0 )
			{
				offset = 10;
			}
			else if ( strncmp( line, "DEMO ", 5 ) == 0 )
			{
				offset = 6;
				if (line[5] != '"')
					break;
				cp = line+offset;
				while (*cp && *cp != '"') cp++;
				if ( !*cp )
					break;
				*cp++ = '\0';
				while (*cp && *cp == ' ') cp++;
				if ( strlen(cp)==0 )
					break;
			}
			if (offset > 0)
			{
				len = (int)strlen( line );
				if (line[len-1] == '\n' ) len--;
				if (line[len-1] == '\r' ) len--;
				line[len] = '\0';
				if (len == 0)
					break;
				printf("N_(\"%s\");\n", line+offset);
			}
			break; // case MODE_XTQ:

		case MODE_TIP:
			/* lines starting with hash sign are ignored (comments) */
			if (line[0] == '#')
				continue;

			/* remove CRs and LFs at end of line */				
			cp = line+strlen(line)-1;
			if (*cp=='\n') cp--;
			if (*cp=='\r') cp--;

			/* get next line if the line was empty */
			if (cp < line)
				continue;

			cp[1] = '\0';

			/* if line ended with a continuation sign, get the rest */
			while (*cp=='\\') {
				*cp++ = '\\';
				*cp++ = 'n';

				/* read a line */
				if (!fgets( cp, (sizeof(line)) - (cp-line), inFile )) {
					return;
				}

				/* lines starting with hash sign are ignored (comments) */
				if (*cp=='#')
					continue;

				/* remove CRs and LFs at end of line */				
				cp += strlen(cp)-1;
				if (*cp=='\n') cp--;
				if (*cp=='\r') cp--;
				cp[1] = '\0';
			}

			if (strchr(line, '"'))
			{
				printf("N_(\"");
				len = strlen(line);
				for (i = 0; i < len; i++)
				{
					/* Escape double quotation marks */
					if (line[i] == '"')
						putchar('\\');
					putchar(line[i]);
				}
				printf("\");\n");
			}
			else
			{
				printf("N_(\"%s\");\n", line);
			}
			break; // case MODE_TIP:
		} // switch (mode)
	} // while (...)
}


int main ( int argc, char * argv[] )
{
	FILE * inFile;
	mode_e mode;
	char *ch;
	int i;
	int files = 0;
	int xtrFiles = 0;
	int xtqFiles = 0;
	int tipFiles = 0;
	int errors = 0;

	if ( argc < 2 ) {
		fprintf( stderr,
				"Usage: %s files ...\n"
				"       Where \'files\' is a list of files to be parsed. Program\n"
				"       automatically detects the file type from the file extension.\n"
				"       Supported file types are:\n"
				"          .xtr, .xtq, .tip\n",
				argv[0] );
		exit(1);
	}

	/* Print header info */
	printf("/* ----------------------------------------------------------*\n"
		   " * These strings are generated from the XTrkCad macro and\n"
		   " * Tip of the day files by %s. The strings are\n"
		   " * formatted so that the xgettext can extract them into\n"
		   " * .pot file for translation.\n"
		   " * ----------------------------------------------------------*/\n",
		   argv[0]);

	for (i = 1; i < argc; i++)
	{
		/* Set operating mode according to the file name extension */
		ch = strrchr(argv[i], '.');
		if (ch == NULL)
		{
			errors++;
			fprintf( stderr, "WARNING: No file name extension in file \"%s\"\n", argv[i]);
			continue;
		}
		ch++;
		if ( strcmp( ch, "xtq" ) == 0 )
			mode = MODE_XTQ;
		else if ( strcmp( ch, "xtr" ) == 0 )
			mode = MODE_XTR;
		else if ( strcmp( ch, "tip" ) == 0 )
			mode = MODE_TIP;
		else
		{
			errors++;
			fprintf( stderr, "WARNING: Unknown file name extension in file \"%s\"\n", argv[i]);
			continue;
		}

		/* Open file */
		inFile = fopen( argv[i], "r" );
		if (inFile == NULL) {
			errors++;
			perror( argv[i] );
			continue;
		}

		/* Process file */
		process( mode, inFile );

		/* Close  file */
		files++;
		switch (mode)
		{
		case MODE_XTQ:
			xtqFiles++;
			break;
		case MODE_XTR:
			xtrFiles++;
			break;
		case MODE_TIP:
			tipFiles++;
			break;
		}
		fclose(inFile);
		inFile = NULL;
	}

	/* Print out the results */
	printf("/* ----------------------------------------------------------*\n"
		   " * Input files:   %d\n"
		   " * Files handled: %d (xtq: %d, xtr: %d, tip: %d)\n"
		   " * Errors:        %d\n"
		   " * ----------------------------------------------------------*/\n",
		   argc-1,
		   files, xtqFiles, xtrFiles, tipFiles,
		   errors);

	exit(0);
}