00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <stdlib.h>
00043 #include <string.h>
00044 #include <stdio.h>
00045 #include "xml_parse_lib.h"
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 void Xml_Next_Word( char *line, char *word, int maxlen, char *delim )
00057 {
00058 int i=0, j=0, m=0, flag=1;
00059
00060 while ((line[i]!='\0') && (flag))
00061 {
00062 j = 0;
00063 while ((delim[j]!='\0') && (line[i]!=delim[j])) j = j + 1;
00064 if (line[i]==delim[j]) i++; else flag = 0;
00065 }
00066 maxlen--;
00067 while ((line[i]!='\0') && (m < maxlen) && (!flag))
00068 {
00069 word[m++] = line[i++];
00070 if (line[i]!='\0')
00071 {
00072 j = 0;
00073 while ((delim[j]!='\0') && (line[i]!=delim[j])) j = j + 1;
00074 if (line[i]==delim[j]) flag = 1;
00075 }
00076 }
00077 j = 0;
00078 while (line[i]!='\0') line[j++] = line[i++];
00079 line[j] = '\0';
00080 word[m] = '\0';
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 void xml_strncpy( char *dst, const char *src, int maxlen )
00094 {
00095 int j=0, oneless;
00096 oneless = maxlen - 1;
00097 while ((j < oneless) && (src[j] != '\0')) { dst[j] = src[j]; j++; }
00098 dst[j] = '\0';
00099 }
00100
00101
00102 void xml_remove_leading_trailing_spaces( char *word )
00103 {
00104 int i=0, j=0;
00105 while ((word[i]!='\0') && ((word[i]==' ') || (word[i]=='\t') || (word[i]=='\n') || (word[i]=='\r')))
00106 i = i + 1;
00107 do { word[j++] = word[i++]; } while (word[i-1]!='\0');
00108 j = j - 2;
00109 while ((j>=0) && ((word[j]==' ') || (word[j]=='\t') || (word[j]=='\n') || (word[j]=='\r')))
00110 j = j - 1;
00111 word[j+1] = '\0';
00112 }
00113
00114
00115 void xml_escape_symbols( char *phrase, int maxlen )
00116 {
00117 int j=0, k, m, n;
00118 n = strlen(phrase);
00119 do
00120 {
00121 if (phrase[j]=='&')
00122 {
00123 k = n + 4; m = n; n = n + 4;
00124 if (n > maxlen) {printf("xml_Parse: MaxStrLen %d exceeded.\n",maxlen); return;}
00125 do phrase[k--] = phrase[m--]; while (m > j);
00126 j++; phrase[j++] = 'a'; phrase[j++] = 'm'; phrase[j++] = 'p'; phrase[j++] = ';';
00127 } else
00128 if (phrase[j]=='"')
00129 {
00130 k = n + 5; m = n; n = n + 5;
00131 if (n > maxlen) {printf("xml_Parse: MaxStrLen %d exceeded.\n",maxlen); return;}
00132 do phrase[k--] = phrase[m--]; while (m > j);
00133 phrase[j++] = '&'; phrase[j++] = 'q'; phrase[j++] = 'u'; phrase[j++] = 'o'; phrase[j++] = 't'; phrase[j++] = ';';
00134 } else
00135 if (phrase[j]=='<')
00136 {
00137 k = n + 3; m = n; n = n + 3;
00138 if (n > maxlen) {printf("xml_Parse: MaxStrLen %d exceeded.\n",maxlen); return;}
00139 do phrase[k--] = phrase[m--]; while (m > j);
00140 phrase[j++] = '&'; phrase[j++] = 'l'; phrase[j++] = 't'; phrase[j++] = ';';
00141 } else
00142 if (phrase[j]=='>')
00143 {
00144 k = n + 3; m = n; n = n + 3;
00145 if (n > maxlen) {printf("xml_Parse: MaxStrLen %d exceeded.\n",maxlen); return;}
00146 do phrase[k--] = phrase[m--]; while (m > j);
00147 phrase[j++] = '&'; phrase[j++] = 'g'; phrase[j++] = 't'; phrase[j++] = ';';
00148 } else j++;
00149 }
00150 while (phrase[j] != '\0');
00151 }
00152
00153
00154 int xml_ishexadecimal( char ch, int *hex, int *sum )
00155 {
00156 if (ch < '0') return 0;
00157 if (*hex) *sum = 16 * *sum; else *sum = 10 * *sum;
00158 if (ch <= '9') { *sum = *sum + ch - 48; return 1; }
00159 if (ch < 'A') return 0;
00160 if ((*hex) && (ch <= 'F')) { *sum = *sum + ch - 55; return 1; }
00161 if ((ch == 'X') && (*hex != 1) && (*sum == 0)) { *hex = 1; return 1; }
00162 if (ch < 'a') return 0;
00163 if ((*hex) && (ch <= 'f')) { *sum = *sum + ch - 87; return 1; }
00164 if ((ch == 'x') && (*hex != 1) && (*sum == 0)) { *hex = 1; return 1; } else return 0;
00165 }
00166
00167
00168 void xml_restore_escapes( char *phrase )
00169 {
00170 int j=0, k, m, n;
00171
00172 n = strlen(phrase);
00173 if (n == 0) return;
00174 do
00175 {
00176 if (phrase[j]=='&')
00177 {
00178 switch (phrase[j+1])
00179 {
00180 case 'a':
00181 j++; m = j; k = j + 4;
00182 if (k > n) {printf("xml_Parse: String ends prematurely after ampersand '%s'.\n",phrase); return;}
00183
00184 n = n - 4;
00185 do phrase[m++] = phrase[k++]; while (phrase[k-1] != '\0');
00186 break;
00187 case 'q':
00188 phrase[j++] = '"';
00189 m = j; k = j + 5;
00190 if (k > n) {printf("xml_Parse: String ends prematurely after ampersand '%s'.\n",phrase); return;}
00191
00192 n = n - 5;
00193 do phrase[m++] = phrase[k++]; while (phrase[k-1] != '\0');
00194 break;
00195 case 'l':
00196 phrase[j++] = '<';
00197 m = j; k = j + 3;
00198 if (k > n) {printf("xml_Parse: String ends prematurely after ampersand '%s'.\n",phrase); return;}
00199
00200 n = n - 3;
00201 do phrase[m++] = phrase[k++]; while (phrase[k-1] != '\0');
00202 break;
00203 case 'g':
00204 phrase[j++] = '>';
00205 m = j; k = j + 3;
00206 if (k > n) {printf("xml_Parse: String ends prematurely after ampersand '%s'.\n",phrase); return;}
00207
00208 n = n - 3;
00209 do phrase[m++] = phrase[k++]; while (phrase[k-1] != '\0');
00210 break;
00211 case '#':
00212 { int hex=0, sum = 0;
00213 k = j + 2;
00214 while ((k < j + 6) && (k < n) && (phrase[k] != ';') && (xml_ishexadecimal( phrase[k], &hex, &sum ))) k++;
00215 if ((k > n) || (phrase[k] != ';'))
00216 {printf("xml_Parse: String ends prematurely after ampersand '%s'.\n",phrase); return;}
00217 phrase[j++] = sum; m = j; k++;
00218 do phrase[m++] = phrase[k++]; while (phrase[k-1] != '\0');
00219 }
00220 break;
00221 default: printf("xml_Parse: Unexpected char (%c) follows ampersand (&) in xml. (phrase='%s')\n", phrase[j+1], phrase ); j++;
00222 }
00223 } else j++;
00224 }
00225 while (phrase[j] != '\0');
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 void xml_grab_tag_name( char *tag, char *name, int maxlen )
00243 {
00244 int j;
00245 Xml_Next_Word( tag, name, maxlen, " \t\n\r");
00246 j = strlen(name);
00247 if ((j > 1) && (name[j-1] == '/'))
00248 {
00249 name[j-1] = '\0';
00250 j = 0; do { tag[j+1] = tag[j]; j++; } while (tag[j-1] != '\0');
00251 tag[0] = '/';
00252 }
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 void xml_grab_attrib( char *tag, char *name, char *value, int maxlen )
00274 {
00275 int j=0, k=0, m;
00276
00277 Xml_Next_Word( tag, name, maxlen, " \t=\n\r");
00278
00279
00280 while ((tag[j]!='\0') && (tag[j]!='\"'))
00281 {
00282 if ((tag[j]!=' ') && (tag[j]!='\t') && (tag[j]!='\n') && (tag[j]!='\r') && (tag[j]!='='))
00283 printf("xml error: unexpected char before attribute value quote '%s'\n", tag);
00284 j++;
00285 }
00286 if (tag[j]=='\0') { value[0] = '\0'; tag[0] = '\0'; return; }
00287 if (tag[j++]!='\"')
00288 { printf("xml error: missing attribute value quote '%s'\n", tag); tag[0] = '\0'; value[0] = '\0'; return;}
00289 while ((tag[j]!='\0') && (tag[j]!='\"')) { value[k++] = tag[j++]; }
00290 value[k] = '\0';
00291 if (tag[j]!='\"') printf("xml error: unclosed attribute value quote '%s'\n", tag); else j++;
00292 xml_restore_escapes( value );
00293
00294 k = 0;
00295 do tag[k++] = tag[j++]; while (tag[k-1] != '\0');
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317 void xml_parse( FILE *fileptr, char *tag, char *content, int maxlen, int *lnn )
00318 {
00319 int i; char ch;
00320
00321
00322 do { ch = getc(fileptr); if (ch=='\n') (*lnn)++; } while ((!feof(fileptr)) && (ch != '<'));
00323
00324 i = 0;
00325 do
00326 { do { tag[i] = getc(fileptr); if (tag[i]=='\n') tag[i] = ' '; }
00327 while ((tag[i]=='\r') && (!feof(fileptr))); i=i+1;
00328 if ((i==3) && (tag[0]=='!') && (tag[1]=='-') && (tag[2]=='-'))
00329 {
00330 i = 0;
00331 do { ch = getc(fileptr); if (ch=='-') i = i + 1; else if ((ch!='>') || (i==1)) i = 0; }
00332 while ((!feof(fileptr)) && ((i<2) || (ch!='>')));
00333 do { ch = getc(fileptr); if (ch=='\n') (*lnn)++; } while ((!feof(fileptr)) && (ch != '<'));
00334 i = 0;
00335 }
00336 } while ((!feof(fileptr)) && (i < maxlen) && (tag[i-1] != '>'));
00337 if (i==0) i = 1;
00338 tag[i-1] = '\0';
00339
00340 i = 0;
00341 do
00342 { do content[i] = getc(fileptr); while ((content[i]=='\r') && (!feof(fileptr)));
00343 if (content[i]==10) (*lnn)++; i=i+1;
00344 }
00345 while ((!feof(fileptr)) && (i < maxlen) && (content[i-1] != '<'));
00346 ungetc( content[i-1], fileptr );
00347 if (i==0) i = 1;
00348 content[i-1] = '\0';
00349
00350
00351 xml_remove_leading_trailing_spaces( tag );
00352 xml_remove_leading_trailing_spaces( content );
00353 xml_restore_escapes( content );
00354 }
00355
00356
00357
00358 void xml_parse_tag_only( FILE *fileptr, char *tag, int maxlen, int *lnn )
00359 {
00360 int i; char ch;
00361
00362
00363 do { ch = getc(fileptr); if (ch=='\n') (*lnn)++; } while ((!feof(fileptr)) && (ch != '<'));
00364
00365 i = 0;
00366 do
00367 { do { tag[i] = getc(fileptr); if (tag[i]=='\n') tag[i] = ' '; }
00368 while ((tag[i]=='\r') && (!feof(fileptr))); i=i+1;
00369 if ((i==3) && (tag[0]=='!') && (tag[1]=='-') && (tag[2]=='-'))
00370 {
00371 i = 0;
00372 do { ch = getc(fileptr); if (ch=='-') i = i + 1; else if ((ch!='>') || (i==1)) i = 0; }
00373 while ((!feof(fileptr)) && ((i<2) || (ch!='>')));
00374 do { ch = getc(fileptr); if (ch=='\n') (*lnn)++; } while ((!feof(fileptr)) && (ch != '<'));
00375 i = 0;
00376 }
00377 } while ((!feof(fileptr)) && (i < maxlen) && (tag[i-1] != '>'));
00378 if (i==0) i = 1;
00379 tag[i-1] = '\0';
00380
00381 xml_remove_leading_trailing_spaces( tag );
00382
00383 }
00384
00385
00386
00387
00388