presence_xml/pidf.c
Go to the documentation of this file.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 #ifndef __OS_solaris
00040 #define _XOPEN_SOURCE 600
00041 #else
00042 #define _XOPEN_SOURCE_EXTENDED 1
00043 #endif
00044
00045 #include <time.h>
00046
00047 #undef _XOPEN_SOURCE
00048 #undef _XOPEN_SOURCE_EXTENDED
00049
00050 #include <string.h>
00051 #include <stdlib.h>
00052 #include <libxml/parser.h>
00053 #include "../../dprint.h"
00054 #include "../../sr_module.h"
00055 #include "pidf.h"
00056
00057 xmlAttrPtr xmlNodeGetAttrByName(xmlNodePtr node, const char *name)
00058 {
00059 xmlAttrPtr attr = node->properties;
00060 while (attr) {
00061 if (xmlStrcasecmp(attr->name, (unsigned char*)name) == 0)
00062 return attr;
00063 attr = attr->next;
00064 }
00065 return NULL;
00066 }
00067
00068 char *xmlNodeGetAttrContentByName(xmlNodePtr node, const char *name)
00069 {
00070 xmlAttrPtr attr = xmlNodeGetAttrByName(node, name);
00071 if (attr)
00072 return (char*)xmlNodeGetContent(attr->children);
00073 else
00074 return NULL;
00075 }
00076
00077 xmlNodePtr xmlNodeGetChildByName(xmlNodePtr node, const char *name)
00078 {
00079 xmlNodePtr cur = node->children;
00080 while (cur) {
00081 if (xmlStrcasecmp(cur->name, (unsigned char*)name) == 0)
00082 return cur;
00083 cur = cur->next;
00084 }
00085 return NULL;
00086 }
00087
00088 xmlNodePtr xmlNodeGetNodeByName(xmlNodePtr node, const char *name,
00089 const char *ns)
00090 {
00091 xmlNodePtr cur = node;
00092 while (cur) {
00093 xmlNodePtr match = NULL;
00094 if (xmlStrcasecmp(cur->name, (unsigned char*)name) == 0) {
00095 if (!ns || (cur->ns && xmlStrcasecmp(cur->ns->prefix,
00096 (unsigned char*)ns) == 0))
00097 return cur;
00098 }
00099 match = xmlNodeGetNodeByName(cur->children, name, ns);
00100 if (match)
00101 return match;
00102 cur = cur->next;
00103 }
00104 return NULL;
00105 }
00106
00107 char *xmlNodeGetNodeContentByName(xmlNodePtr root, const char *name,
00108 const char *ns)
00109 {
00110 xmlNodePtr node = xmlNodeGetNodeByName(root, name, ns);
00111 if (node)
00112 return (char*)xmlNodeGetContent(node->children);
00113 else
00114 return NULL;
00115 }
00116
00117 xmlNodePtr xmlDocGetNodeByName(xmlDocPtr doc, const char *name, const char *ns)
00118 {
00119 xmlNodePtr cur = doc->children;
00120 return xmlNodeGetNodeByName(cur, name, ns);
00121 }
00122
00123 char *xmlDocGetNodeContentByName(xmlDocPtr doc, const char *name,
00124 const char *ns)
00125 {
00126 xmlNodePtr node = xmlDocGetNodeByName(doc, name, ns);
00127 if (node)
00128 return (char*)xmlNodeGetContent(node->children);
00129 else
00130 return NULL;
00131 }
00132
00133 time_t xml_parse_dateTime(char* xml_time_str)
00134 {
00135 struct tm tm;
00136 char * p;
00137 int h, m;
00138 char h1, h2, m1, m2;
00139 int sign= 1;
00140 signed int timezone_diff= 0;
00141
00142 p= strptime(xml_time_str, "%F", &tm);
00143 if(p== NULL)
00144 {
00145 printf("error: failed to parse time\n");
00146 return 0;
00147 }
00148 p++;
00149 p= strptime(p, "%T", &tm);
00150 if(p== NULL)
00151 {
00152 printf("error: failed to parse time\n");
00153 return 0;
00154 }
00155
00156 if(*p== '\0')
00157 goto done;
00158
00159 if(*p== '.')
00160 {
00161 p++;
00162
00163 while(*p!= '\0' && *p>= '0' && *p<= '9')
00164 {
00165 p++;
00166 }
00167 }
00168
00169 if(*p== '\0')
00170 goto done;
00171
00172
00173
00174
00175 if(*p== 'Z')
00176 {
00177 goto done;
00178 }
00179
00180 if(*p== '+')
00181 sign= -1;
00182
00183 p++;
00184
00185 if(sscanf(p, "%c%c:%c%c", &h1, &h2, &m1, &m2) < 0) {
00186 printf("error: failed to parse time\n");
00187 return 0;
00188 }
00189 h= (h1- '0')*10+ h2- '0';
00190 m= (m1- '0')*10+ m2- '0';
00191
00192 timezone_diff= sign* ((m+ h* 60)* 60);
00193
00194 done:
00195 return (mktime(&tm) + timezone_diff);
00196 }
00197
00198