Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/src/translations.c
Parent Directory
|
Revision Log
Revision 373 -
(show annotations)
(download)
(as text)
Fri Apr 23 18:41:20 2010 UTC (14 years, 10 months ago) by amb
File MIME type: text/x-csrc
File size: 17927 byte(s)
Fri Apr 23 18:41:20 2010 UTC (14 years, 10 months ago) by amb
File MIME type: text/x-csrc
File size: 17927 byte(s)
Pass the tag name to the tag function.
1 | /*************************************** |
2 | $Header: /home/amb/CVS/routino/src/translations.c,v 1.5 2010-04-23 18:41:09 amb Exp $ |
3 | |
4 | Load the translations from a file and the functions for handling them. |
5 | |
6 | Part of the Routino routing software. |
7 | ******************/ /****************** |
8 | This file Copyright 2010 Andrew M. Bishop |
9 | |
10 | This program is free software: you can redistribute it and/or modify |
11 | it under the terms of the GNU Affero General Public License as published by |
12 | the Free Software Foundation, either version 3 of the License, or |
13 | (at your option) any later version. |
14 | |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public License |
21 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
22 | ***************************************/ |
23 | |
24 | |
25 | #include <stdio.h> |
26 | #include <string.h> |
27 | #include <stdlib.h> |
28 | |
29 | #include "functions.h" |
30 | #include "translations.h" |
31 | #include "xmlparse.h" |
32 | |
33 | |
34 | /* Global variables - default English values */ |
35 | |
36 | char *translate_heading[9] ={"South","South-West","West","North-West","North","North-East","East","South-East","South"}; |
37 | char *translate_turn[9] ={"Very sharp left","Sharp left","Left","Slight left","Straight on","Slight right","Right","Sharp right","Very sharp right"}; |
38 | |
39 | char *translate_gpx_desc ="%s between 'start' and 'finish' waypoints"; |
40 | char *translate_gpx_name ="%s Route"; |
41 | char *translate_gpx_step ="%s on '%s' for %.3f km, %.1 min"; |
42 | char *translate_gpx_final="Total Journey %.1f km, %d minutes"; |
43 | |
44 | char *translate_gpx_shortest="Shortest"; |
45 | char *translate_gpx_quickest="Quickest"; |
46 | |
47 | char *translate_gpx_start="START"; |
48 | char *translate_gpx_inter="INTER"; |
49 | char *translate_gpx_trip="TRIP"; |
50 | char *translate_gpx_finish="FINISH"; |
51 | |
52 | |
53 | /* Local variables */ |
54 | |
55 | /*+ The language that is to be stored. +*/ |
56 | static const char *store_lang=NULL; |
57 | |
58 | /*+ This current language is to be stored. +*/ |
59 | static int store=0; |
60 | |
61 | /*+ The chosen language has been stored. +*/ |
62 | static int stored=0; |
63 | |
64 | |
65 | /* The XML tag processing function prototypes */ |
66 | |
67 | //static int xmlDeclaration_function(const char *_tag_,int _type_,const char *version,const char *encoding); |
68 | //static int RoutinoTranslationsType_function(const char *_tag_,int _type_); |
69 | static int languageType_function(const char *_tag_,int _type_,const char *lang); |
70 | //static int GPXType_function(const char *_tag_,int _type_); |
71 | static int GPXFinalType_function(const char *_tag_,int _type_,const char *text); |
72 | static int GPXStepType_function(const char *_tag_,int _type_,const char *text); |
73 | static int GPXNameType_function(const char *_tag_,int _type_,const char *text); |
74 | static int GPXDescType_function(const char *_tag_,int _type_,const char *text); |
75 | static int GPXWaypointType_function(const char *_tag_,int _type_,const char *type,const char *string); |
76 | static int GPXRouteType_function(const char *_tag_,int _type_,const char *type,const char *string); |
77 | //static int HTMLType_function(const char *_tag_,int _type_); |
78 | static int HeadingType_function(const char *_tag_,int _type_,const char *direction,const char *string); |
79 | static int TurnType_function(const char *_tag_,int _type_,const char *direction,const char *string); |
80 | |
81 | |
82 | /* The XML tag definitions */ |
83 | |
84 | /*+ The TurnType type tag. +*/ |
85 | static xmltag TurnType_tag= |
86 | {"turn", |
87 | 2, {"direction","string"}, |
88 | TurnType_function, |
89 | {NULL}}; |
90 | |
91 | /*+ The HeadingType type tag. +*/ |
92 | static xmltag HeadingType_tag= |
93 | {"heading", |
94 | 2, {"direction","string"}, |
95 | HeadingType_function, |
96 | {NULL}}; |
97 | |
98 | /*+ The HTMLType type tag. +*/ |
99 | static xmltag HTMLType_tag= |
100 | {"output-html", |
101 | 0, {NULL}, |
102 | NULL, |
103 | {NULL}}; |
104 | |
105 | /*+ The GPXRouteType type tag. +*/ |
106 | static xmltag GPXRouteType_tag= |
107 | {"route", |
108 | 2, {"type","string"}, |
109 | GPXRouteType_function, |
110 | {NULL}}; |
111 | |
112 | /*+ The GPXWaypointType type tag. +*/ |
113 | static xmltag GPXWaypointType_tag= |
114 | {"waypoint", |
115 | 2, {"type","string"}, |
116 | GPXWaypointType_function, |
117 | {NULL}}; |
118 | |
119 | /*+ The GPXDescType type tag. +*/ |
120 | static xmltag GPXDescType_tag= |
121 | {"desc", |
122 | 1, {"text"}, |
123 | GPXDescType_function, |
124 | {NULL}}; |
125 | |
126 | /*+ The GPXNameType type tag. +*/ |
127 | static xmltag GPXNameType_tag= |
128 | {"name", |
129 | 1, {"text"}, |
130 | GPXNameType_function, |
131 | {NULL}}; |
132 | |
133 | /*+ The GPXStepType type tag. +*/ |
134 | static xmltag GPXStepType_tag= |
135 | {"step", |
136 | 1, {"text"}, |
137 | GPXStepType_function, |
138 | {NULL}}; |
139 | |
140 | /*+ The GPXFinalType type tag. +*/ |
141 | static xmltag GPXFinalType_tag= |
142 | {"final", |
143 | 1, {"text"}, |
144 | GPXFinalType_function, |
145 | {NULL}}; |
146 | |
147 | /*+ The GPXType type tag. +*/ |
148 | static xmltag GPXType_tag= |
149 | {"output-gpx", |
150 | 0, {NULL}, |
151 | NULL, |
152 | {&GPXRouteType_tag,&GPXWaypointType_tag,&GPXDescType_tag,&GPXNameType_tag,&GPXStepType_tag,&GPXFinalType_tag,NULL}}; |
153 | |
154 | /*+ The languageType type tag. +*/ |
155 | static xmltag languageType_tag= |
156 | {"language", |
157 | 1, {"lang"}, |
158 | languageType_function, |
159 | {&TurnType_tag,&HeadingType_tag,&HTMLType_tag,&GPXType_tag,NULL}}; |
160 | |
161 | /*+ The RoutinoTranslationsType type tag. +*/ |
162 | static xmltag RoutinoTranslationsType_tag= |
163 | {"routino-translations", |
164 | 0, {NULL}, |
165 | NULL, |
166 | {&languageType_tag,NULL}}; |
167 | |
168 | /*+ The xmlDeclaration type tag. +*/ |
169 | static xmltag xmlDeclaration_tag= |
170 | {"xml", |
171 | 2, {"version","encoding"}, |
172 | NULL, |
173 | {NULL}}; |
174 | |
175 | |
176 | /*+ The complete set of tags at the top level. +*/ |
177 | static xmltag *xml_toplevel_tags[]={&xmlDeclaration_tag,&RoutinoTranslationsType_tag,NULL}; |
178 | |
179 | |
180 | /* The XML tag processing functions */ |
181 | |
182 | |
183 | /*++++++++++++++++++++++++++++++++++++++ |
184 | The function that is called when the TurnType XSD type is seen |
185 | |
186 | int TurnType_function Returns 0 if no error occured or something else otherwise. |
187 | |
188 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
189 | |
190 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
191 | |
192 | const char *direction The contents of the 'direction' attribute (or NULL if not defined). |
193 | |
194 | const char *string The contents of the 'string' attribute (or NULL if not defined). |
195 | ++++++++++++++++++++++++++++++++++++++*/ |
196 | |
197 | static int TurnType_function(const char *_tag_,int _type_,const char *direction,const char *string) |
198 | { |
199 | if(_type_&XMLPARSE_TAG_START && store) |
200 | { |
201 | int d; |
202 | |
203 | XMLPARSE_ASSERT_INTEGER(_tag_,direction,d); |
204 | XMLPARSE_ASSERT_STRING(_tag_,string); |
205 | |
206 | d+=4; |
207 | |
208 | if(d<0 || d>8) |
209 | XMLPARSE_INVALID(_tag_,direction); |
210 | |
211 | translate_turn[d]=strcpy(malloc(strlen(string)+1),string); |
212 | } |
213 | |
214 | return(0); |
215 | } |
216 | |
217 | |
218 | /*++++++++++++++++++++++++++++++++++++++ |
219 | The function that is called when the HeadingType XSD type is seen |
220 | |
221 | int HeadingType_function Returns 0 if no error occured or something else otherwise. |
222 | |
223 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
224 | |
225 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
226 | |
227 | const char *direction The contents of the 'direction' attribute (or NULL if not defined). |
228 | |
229 | const char *string The contents of the 'string' attribute (or NULL if not defined). |
230 | ++++++++++++++++++++++++++++++++++++++*/ |
231 | |
232 | static int HeadingType_function(const char *_tag_,int _type_,const char *direction,const char *string) |
233 | { |
234 | if(_type_&XMLPARSE_TAG_START && store) |
235 | { |
236 | int d; |
237 | |
238 | XMLPARSE_ASSERT_INTEGER(_tag_,direction,d); |
239 | XMLPARSE_ASSERT_STRING(_tag_,string); |
240 | |
241 | d+=4; |
242 | |
243 | if(d<0 || d>8) |
244 | XMLPARSE_INVALID(_tag_,direction); |
245 | |
246 | translate_heading[d]=strcpy(malloc(strlen(string)+1),string); |
247 | } |
248 | |
249 | return(0); |
250 | } |
251 | |
252 | |
253 | /*++++++++++++++++++++++++++++++++++++++ |
254 | The function that is called when the HTMLType XSD type is seen |
255 | |
256 | int HTMLType_function Returns 0 if no error occured or something else otherwise. |
257 | |
258 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
259 | |
260 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
261 | ++++++++++++++++++++++++++++++++++++++*/ |
262 | |
263 | //static int HTMLType_function(const char *_tag_,int _type_) |
264 | //{ |
265 | //} |
266 | |
267 | |
268 | /*++++++++++++++++++++++++++++++++++++++ |
269 | The function that is called when the GPXRouteType XSD type is seen |
270 | |
271 | int GPXRouteType_function Returns 0 if no error occured or something else otherwise. |
272 | |
273 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
274 | |
275 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
276 | |
277 | const char *type The contents of the 'type' attribute (or NULL if not defined). |
278 | |
279 | const char *string The contents of the 'string' attribute (or NULL if not defined). |
280 | ++++++++++++++++++++++++++++++++++++++*/ |
281 | |
282 | static int GPXRouteType_function(const char *_tag_,int _type_,const char *type,const char *string) |
283 | { |
284 | if(_type_&XMLPARSE_TAG_START && store) |
285 | { |
286 | XMLPARSE_ASSERT_STRING(_tag_,type); |
287 | XMLPARSE_ASSERT_STRING(_tag_,string); |
288 | |
289 | if(!strcmp(type,"shortest")) |
290 | translate_gpx_shortest=strcpy(malloc(strlen(string)+1),string); |
291 | else if(!strcmp(type,"quickest")) |
292 | translate_gpx_quickest=strcpy(malloc(strlen(string)+1),string); |
293 | else |
294 | XMLPARSE_INVALID(_tag_,type); |
295 | } |
296 | |
297 | return(0); |
298 | } |
299 | |
300 | |
301 | /*++++++++++++++++++++++++++++++++++++++ |
302 | The function that is called when the GPXWaypointType XSD type is seen |
303 | |
304 | int GPXWaypointType_function Returns 0 if no error occured or something else otherwise. |
305 | |
306 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
307 | |
308 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
309 | |
310 | const char *type The contents of the 'type' attribute (or NULL if not defined). |
311 | |
312 | const char *string The contents of the 'string' attribute (or NULL if not defined). |
313 | ++++++++++++++++++++++++++++++++++++++*/ |
314 | |
315 | static int GPXWaypointType_function(const char *_tag_,int _type_,const char *type,const char *string) |
316 | { |
317 | if(_type_&XMLPARSE_TAG_START && store) |
318 | { |
319 | XMLPARSE_ASSERT_STRING(_tag_,type); |
320 | XMLPARSE_ASSERT_STRING(_tag_,string); |
321 | |
322 | if(!strcmp(type,"start")) |
323 | translate_gpx_start=strcpy(malloc(strlen(string)+1),string); |
324 | else if(!strcmp(type,"inter")) |
325 | translate_gpx_inter=strcpy(malloc(strlen(string)+1),string); |
326 | else if(!strcmp(type,"trip")) |
327 | translate_gpx_trip=strcpy(malloc(strlen(string)+1),string); |
328 | else if(!strcmp(type,"finish")) |
329 | translate_gpx_finish=strcpy(malloc(strlen(string)+1),string); |
330 | else |
331 | XMLPARSE_INVALID(_tag_,type); |
332 | } |
333 | |
334 | return(0); |
335 | } |
336 | |
337 | |
338 | /*++++++++++++++++++++++++++++++++++++++ |
339 | The function that is called when the GPXDescType XSD type is seen |
340 | |
341 | int GPXDescType_function Returns 0 if no error occured or something else otherwise. |
342 | |
343 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
344 | |
345 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
346 | |
347 | const char *text The contents of the 'text' attribute (or NULL if not defined). |
348 | ++++++++++++++++++++++++++++++++++++++*/ |
349 | |
350 | static int GPXDescType_function(const char *_tag_,int _type_,const char *text) |
351 | { |
352 | if(_type_&XMLPARSE_TAG_START && store) |
353 | { |
354 | XMLPARSE_ASSERT_STRING(_tag_,text); |
355 | |
356 | translate_gpx_desc=strcpy(malloc(strlen(text)+1),text); |
357 | } |
358 | |
359 | return(0); |
360 | } |
361 | |
362 | |
363 | /*++++++++++++++++++++++++++++++++++++++ |
364 | The function that is called when the GPXNameType XSD type is seen |
365 | |
366 | int GPXNameType_function Returns 0 if no error occured or something else otherwise. |
367 | |
368 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
369 | |
370 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
371 | |
372 | const char *text The contents of the 'text' attribute (or NULL if not defined). |
373 | ++++++++++++++++++++++++++++++++++++++*/ |
374 | |
375 | static int GPXNameType_function(const char *_tag_,int _type_,const char *text) |
376 | { |
377 | if(_type_&XMLPARSE_TAG_START && store) |
378 | { |
379 | XMLPARSE_ASSERT_STRING(_tag_,text); |
380 | |
381 | translate_gpx_name=strcpy(malloc(strlen(text)+1),text); |
382 | } |
383 | |
384 | return(0); |
385 | } |
386 | |
387 | |
388 | /*++++++++++++++++++++++++++++++++++++++ |
389 | The function that is called when the GPXStepType XSD type is seen |
390 | |
391 | int GPXStepType_function Returns 0 if no error occured or something else otherwise. |
392 | |
393 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
394 | |
395 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
396 | |
397 | const char *text The contents of the 'text' attribute (or NULL if not defined). |
398 | ++++++++++++++++++++++++++++++++++++++*/ |
399 | |
400 | static int GPXStepType_function(const char *_tag_,int _type_,const char *text) |
401 | { |
402 | if(_type_&XMLPARSE_TAG_START && store) |
403 | { |
404 | XMLPARSE_ASSERT_STRING(_tag_,text); |
405 | |
406 | translate_gpx_step=strcpy(malloc(strlen(text)+1),text); |
407 | } |
408 | |
409 | return(0); |
410 | } |
411 | |
412 | |
413 | /*++++++++++++++++++++++++++++++++++++++ |
414 | The function that is called when the GPXFinalType XSD type is seen |
415 | |
416 | int GPXFinalType_function Returns 0 if no error occured or something else otherwise. |
417 | |
418 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
419 | |
420 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
421 | |
422 | const char *text The contents of the 'text' attribute (or NULL if not defined). |
423 | ++++++++++++++++++++++++++++++++++++++*/ |
424 | |
425 | static int GPXFinalType_function(const char *_tag_,int _type_,const char *text) |
426 | { |
427 | if(_type_&XMLPARSE_TAG_START && store) |
428 | { |
429 | XMLPARSE_ASSERT_STRING(_tag_,text); |
430 | |
431 | translate_gpx_final=strcpy(malloc(strlen(text)+1),text); |
432 | } |
433 | |
434 | return(0); |
435 | } |
436 | |
437 | |
438 | /*++++++++++++++++++++++++++++++++++++++ |
439 | The function that is called when the GPXType XSD type is seen |
440 | |
441 | int GPXType_function Returns 0 if no error occured or something else otherwise. |
442 | |
443 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
444 | |
445 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
446 | ++++++++++++++++++++++++++++++++++++++*/ |
447 | |
448 | //static int GPXType_function(const char *_tag_,int _type_) |
449 | //{ |
450 | //} |
451 | |
452 | |
453 | /*++++++++++++++++++++++++++++++++++++++ |
454 | The function that is called when the languageType XSD type is seen |
455 | |
456 | int languageType_function Returns 0 if no error occured or something else otherwise. |
457 | |
458 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
459 | |
460 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
461 | |
462 | const char *lang The contents of the 'lang' attribute (or NULL if not defined). |
463 | ++++++++++++++++++++++++++++++++++++++*/ |
464 | |
465 | static int languageType_function(const char *_tag_,int _type_,const char *lang) |
466 | { |
467 | static int first=1; |
468 | |
469 | if(_type_&XMLPARSE_TAG_START) |
470 | { |
471 | XMLPARSE_ASSERT_STRING(_tag_,lang); |
472 | |
473 | if(!store_lang && first) |
474 | store=1; |
475 | else if(!strcmp(store_lang,lang)) |
476 | store=1; |
477 | else |
478 | store=0; |
479 | |
480 | first=0; |
481 | } |
482 | |
483 | if(_type_&XMLPARSE_TAG_END && store) |
484 | { |
485 | store=0; |
486 | stored=1; |
487 | } |
488 | |
489 | return(0); |
490 | } |
491 | |
492 | |
493 | /*++++++++++++++++++++++++++++++++++++++ |
494 | The function that is called when the RoutinoTranslationsType XSD type is seen |
495 | |
496 | int RoutinoTranslationsType_function Returns 0 if no error occured or something else otherwise. |
497 | |
498 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
499 | |
500 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
501 | ++++++++++++++++++++++++++++++++++++++*/ |
502 | |
503 | //static int RoutinoTranslationsType_function(const char *_tag_,int _type_) |
504 | //{ |
505 | //} |
506 | |
507 | |
508 | /*++++++++++++++++++++++++++++++++++++++ |
509 | The function that is called when the XML declaration is seen |
510 | |
511 | int xmlDeclaration_function Returns 0 if no error occured or something else otherwise. |
512 | |
513 | const char *_tag_ Set to the name of the element tag that triggered this function call. |
514 | |
515 | int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag. |
516 | |
517 | const char *version The contents of the 'version' attribute (or NULL if not defined). |
518 | |
519 | const char *encoding The contents of the 'encoding' attribute (or NULL if not defined). |
520 | ++++++++++++++++++++++++++++++++++++++*/ |
521 | |
522 | //static int xmlDeclaration_function(const char *_tag_,int _type_,const char *version,const char *encoding) |
523 | //{ |
524 | //} |
525 | |
526 | |
527 | /*++++++++++++++++++++++++++++++++++++++ |
528 | The XML translation parser. |
529 | |
530 | int ParseXMLTranslations Returns 0 if OK or something else in case of an error. |
531 | |
532 | const char *filename The name of the file to read. |
533 | |
534 | const char *language The language to search for (NULL means first in file). |
535 | ++++++++++++++++++++++++++++++++++++++*/ |
536 | |
537 | int ParseXMLTranslations(const char *filename,const char *language) |
538 | { |
539 | int retval; |
540 | |
541 | store_lang=language; |
542 | |
543 | if(!ExistsFile(filename)) |
544 | { |
545 | fprintf(stderr,"Error: Specified translations file '%s' does not exist.\n",filename); |
546 | return(1); |
547 | } |
548 | |
549 | FILE *file=fopen(filename,"r"); |
550 | |
551 | if(!file) |
552 | { |
553 | fprintf(stderr,"Error: Cannot open translations file '%s' for reading.\n",filename); |
554 | return(1); |
555 | } |
556 | |
557 | retval=ParseXML(file,xml_toplevel_tags,XMLPARSE_UNKNOWN_ATTR_ERRNONAME); |
558 | |
559 | fclose(file); |
560 | |
561 | if(retval) |
562 | return(1); |
563 | |
564 | if(language && !stored) |
565 | fprintf(stderr,"Warning: Cannot find translations for language '%s' using English instead.\n",language); |
566 | |
567 | return(0); |
568 | } |
Properties
Name | Value |
---|---|
cvs:description | File containing translation parsing functions. |