XML parsing aide

Committer:
takashikojo
Date:
Sun Jul 05 08:12:12 2015 +0000
Revision:
2:c86701a4f5b0
Parent:
1:ad5fd8c29c1f
NULL pointer protection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
takashikojo 0:369a19fba8a4 1 #include <mbed.h>
takashikojo 0:369a19fba8a4 2 #include "XMLaide.h"
takashikojo 1:ad5fd8c29c1f 3 #include <algorithm>
takashikojo 0:369a19fba8a4 4
takashikojo 0:369a19fba8a4 5 const char *XML_getTag(const char *p, const char *name)
takashikojo 0:369a19fba8a4 6 {
takashikojo 0:369a19fba8a4 7 const char *find ;
takashikojo 0:369a19fba8a4 8 #define TAG_SIZE 20
takashikojo 0:369a19fba8a4 9 char tag[TAG_SIZE] = "<" ;
takashikojo 0:369a19fba8a4 10
takashikojo 2:c86701a4f5b0 11 if(p == NULL)return NULL ;
takashikojo 0:369a19fba8a4 12 find = strstr(p, strcat(tag, name)) ;
takashikojo 0:369a19fba8a4 13 if(find) {
takashikojo 0:369a19fba8a4 14 find = strchr(find, '>') ;
takashikojo 0:369a19fba8a4 15 return find + 1 ;
takashikojo 0:369a19fba8a4 16 } else return NULL ;
takashikojo 0:369a19fba8a4 17 }
takashikojo 0:369a19fba8a4 18
takashikojo 1:ad5fd8c29c1f 19 const char *XML_getElement(const char *p, char *element, int size)
takashikojo 0:369a19fba8a4 20 {
takashikojo 0:369a19fba8a4 21 const char *find ;
takashikojo 2:c86701a4f5b0 22 if(p == NULL)return NULL ;
takashikojo 0:369a19fba8a4 23 find = strstr(p, "</") ;
takashikojo 0:369a19fba8a4 24
takashikojo 0:369a19fba8a4 25 if(find) {
takashikojo 1:ad5fd8c29c1f 26 strncpy(element, p, min((find-p), size)) ;
takashikojo 1:ad5fd8c29c1f 27 element[min((find-p), size)] = '\0' ;
takashikojo 0:369a19fba8a4 28 } else *element = '\0' ;
takashikojo 0:369a19fba8a4 29
takashikojo 0:369a19fba8a4 30 return find ;
takashikojo 0:369a19fba8a4 31 }