/[zanavi_public1]/navit/navit/tools/gpx2navit_txt/src/parser.c
ZANavi

Contents of /navit/navit/tools/gpx2navit_txt/src/parser.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Oct 28 21:19:04 2011 UTC (12 years, 5 months ago) by zoff99
File MIME type: text/plain
File size: 5020 byte(s)
import files
1 /**
2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "gpx2navit_txt.h"
21
22 void charHandle(void *userdata, const XML_Char * data, int length);
23 void startElement(void *userdata, const char *element, const char **attr);
24 void endElement(void *userdata, const char *element);
25 void parseMain(g2sprop * prop);
26
27 /**
28 * a handler to parse charctor data on expat
29 */
30 void charHandle(void *userdata, const XML_Char * data, int length)
31 {
32 static int bufsize = DATABUFSIZE;
33 static int string_length = 0;
34 int new_length;
35 static int begin_copy = 0;
36 int i;
37 parsedata *pdata = (parsedata *) userdata;
38 if (pdata->bufptr == NULL) {
39 //start of buffer -->pdata->bufptr set to 0 at endelement
40 string_length = 0;
41 begin_copy = 0; //begin to copy after first space
42 pdata->bufptr= pdata->databuf;
43 }
44 new_length = string_length + length + 1; //additonal 0
45 if (bufsize < new_length) {
46 pdata->databuf =
47 realloc(pdata->databuf, new_length);
48 bufsize = new_length;
49 //because of realloc the pointer may have changed
50 pdata->bufptr = pdata->databuf + string_length;
51 }
52 // because expat calls this routine several times on special chars
53 // we need to do following
54 // --concat strings until reset (bufptr set to NULL)
55 // --filter out blank chars at begin of string
56 for (i=0; i<length;i++) {
57 if (begin_copy || !isspace(data[i])) {
58 *pdata->bufptr = data[i];
59 pdata->bufptr++;
60 string_length ++;
61 begin_copy = 1;
62 if (DEBUG) fprintf(stderr,"%c",data[i]);
63 }
64 }
65 *pdata->bufptr = '\0';
66 }
67
68 /**
69 * a handler when a element starts
70 */
71 void startElement(void *userdata, const char *element, const char **attr)
72 {
73 parsedata *pdata = (parsedata *) userdata;
74 pdata->parent = pdata->current;
75 pdata->current = malloc(sizeof(parent));
76 pdata->current->name = malloc(sizeof(char) * (strlen(element) + 1));
77 strcpy(pdata->current->name, element);
78 pdata->current->parentptr = pdata->parent;
79 startElementControl(pdata, element, attr);
80 if (pdata->prop->verbose) {
81 int i;
82 for (i = 0; i < pdata->depth; i++)
83 printf(" ");
84 printf("<%s>: ", element);
85 for (i = 0; attr[i]; i += 2) {
86 printf(" %s='%s'", attr[i], attr[i + 1]);
87 }
88 printf("\n");
89 }
90 pdata->depth++;
91 }
92
93 /**
94 * a handler when a element ends
95 */
96 void endElement(void *userdata, const char *element)
97 {
98 parsedata *pdata = (parsedata *) userdata;
99 endElementControl(pdata, element);
100 pdata->depth--;
101 if (pdata->prop->verbose) {
102 int i;
103 for (i = 0; i < pdata->depth; i++)
104 printf(" ");
105 printf("</%s>:%s\n ", element,pdata->parent->name);
106 }
107 free(pdata->current->name);
108 free(pdata->current);
109 pdata->current = pdata->parent;
110 pdata->parent = pdata->parent->parentptr;
111 }
112
113 void parseMain(g2sprop * prop)
114 {
115 FILE *fp;
116 char buff[BUFFSIZE];
117 XML_Parser parser;
118 parsedata *pdata;
119 fp = fopen(prop->sourcefile, "r");
120 if (fp == NULL) {
121 fprintf(stderr, "Cannot open gpx file: %s\n", prop->sourcefile);
122 exit(ERR_CANNOTOPEN);
123 }
124 parser = XML_ParserCreate(NULL);
125 if (!parser) {
126 fprintf(stderr, "Couldn't allocate memory for parser\n");
127 exit(ERR_OUTOFMEMORY);
128 }
129 pdata = createParsedata(parser, prop);
130
131 char *output_wpt =
132 (char *) malloc(sizeof(char) * (strlen(pdata->prop->output) + 9));
133 strcpy(output_wpt, pdata->prop->output);
134 strcat(output_wpt, "_nav.txt");
135 pdata->fp = fopen(output_wpt,"w");
136 if (pdata->fp == NULL)
137 {
138 //todo
139 fprintf(stderr,"Failure opening File %s for writing",output_wpt);
140 exit(1);
141 }
142 free(output_wpt);
143 XML_SetUserData(parser, pdata);
144 XML_SetElementHandler(parser, startElement, endElement);
145 XML_SetCharacterDataHandler(parser, charHandle);
146 for (;;) {
147 int done;
148 int len;
149 fgets(buff, BUFFSIZE, fp);
150 len = (int) strlen(buff);
151 if (ferror(fp)) {
152 fprintf(stderr, "Read error file: %s\n", prop->sourcefile);
153 exit(ERR_READERROR);
154 }
155 done = feof(fp);
156 if (done)
157 break;
158 if (!XML_Parse(parser, buff, len, done)) {
159 fprintf(stderr, "Parse error at line %d:\n%s\n",
160 XML_GetCurrentLineNumber(parser),
161 XML_ErrorString(XML_GetErrorCode(parser)));
162 exit(ERR_PARSEERROR);
163 }
164 }
165 fclose(pdata->fp); //close out file
166 closeParsedata(pdata);
167 }

   
Visit the ZANavi Wiki