modules_k/presence_dialoginfo/pidf.c

Go to the documentation of this file.
00001 /*
00002  * $Id: pidf.c 1953 2007-04-04 08:50:33Z anca_vamanu $
00003  *
00004  * presence module - presence server implementation
00005  *
00006  * Copyright (C) 2006 Voice Sistem S.R.L.
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * --------
00026  *  2007-04-14  initial version (anca)
00027  */
00028 
00039 #ifdef __OS_linux
00040         #define _XOPEN_SOURCE 600          /* glibc2 on linux */
00041         #define _BSD_SOURCE 1                           /* needed on linux to "fix" the effect
00042                                                                                   of the above define on
00043                                                                                   features.h/unistd.h syscall() */
00044 #elif defined __OS_solaris
00045         #define _XOPEN_SOURCE_EXTENDED 1   /* solaris */
00046 #endif
00047 
00048 #include <time.h>
00049 
00050 #undef _XOPEN_SOURCE
00051 #undef _XOPEN_SOURCE_EXTENDED
00052 
00053 #include <string.h>
00054 #include <stdlib.h>
00055 #include <libxml/parser.h>
00056 #include "../../dprint.h"
00057 #include "../../sr_module.h"
00058 #include "pidf.h"
00059 
00060 xmlAttrPtr xmlNodeGetAttrByName(xmlNodePtr node, const char *name)
00061 {
00062         xmlAttrPtr attr = node->properties;
00063         while (attr) {
00064                 if (xmlStrcasecmp(attr->name, (unsigned char*)name) == 0)
00065                         return attr;
00066                 attr = attr->next;
00067         }
00068         return NULL;
00069 }
00070 
00071 char *xmlNodeGetAttrContentByName(xmlNodePtr node, const char *name)
00072 {
00073         xmlAttrPtr attr = xmlNodeGetAttrByName(node, name);
00074         if (attr)
00075                 return (char*)xmlNodeGetContent(attr->children);
00076         else
00077                 return NULL;
00078 }
00079 
00080 xmlNodePtr xmlNodeGetChildByName(xmlNodePtr node, const char *name)
00081 {
00082         xmlNodePtr cur = node->children;
00083         while (cur) {
00084                 if (xmlStrcasecmp(cur->name, (unsigned char*)name) == 0)
00085                         return cur;
00086                 cur = cur->next;
00087         }
00088         return NULL;
00089 }
00090 
00091 xmlNodePtr xmlNodeGetNodeByName(xmlNodePtr node, const char *name,
00092                                                                                                                         const char *ns)
00093 {
00094         xmlNodePtr cur = node;
00095         while (cur) {
00096                 xmlNodePtr match = NULL;
00097                 if (xmlStrcasecmp(cur->name, (unsigned char*)name) == 0) {
00098                         if (!ns || (cur->ns && xmlStrcasecmp(cur->ns->prefix,
00099                                                         (unsigned char*)ns) == 0))
00100                                 return cur;
00101                 }
00102                 match = xmlNodeGetNodeByName(cur->children, name, ns);
00103                 if (match)
00104                         return match;
00105                 cur = cur->next;
00106         }
00107         return NULL;
00108 }
00109 
00110 char *xmlNodeGetNodeContentByName(xmlNodePtr root, const char *name,
00111                 const char *ns)
00112 {
00113         xmlNodePtr node = xmlNodeGetNodeByName(root, name, ns);
00114         if (node)
00115                 return (char*)xmlNodeGetContent(node->children);
00116         else
00117                 return NULL;
00118 }
00119 
00120 xmlNodePtr xmlDocGetNodeByName(xmlDocPtr doc, const char *name, const char *ns)
00121 {
00122         xmlNodePtr cur = doc->children;
00123         return xmlNodeGetNodeByName(cur, name, ns);
00124 }
00125 
00126 char *xmlDocGetNodeContentByName(xmlDocPtr doc, const char *name, 
00127                 const char *ns)
00128 {
00129         xmlNodePtr node = xmlDocGetNodeByName(doc, name, ns);
00130         if (node)
00131                 return (char*)xmlNodeGetContent(node->children);
00132         else
00133                 return NULL;
00134 }
00135 
00136 time_t xml_parse_dateTime(char* xml_time_str)
00137 {
00138         struct tm tm;
00139         char * p;
00140         int h, m;
00141         char h1, h2, m1, m2;
00142         int sign= 1;
00143         signed int timezone_diff= 0;
00144 
00145         p= strptime(xml_time_str, "%F", &tm);
00146         if(p== NULL)
00147         {
00148                 printf("error: failed to parse time\n");
00149                 return 0;
00150         }
00151         p++;
00152         p= strptime(p, "%T", &tm);
00153         if(p== NULL)
00154         {
00155                 printf("error: failed to parse time\n");
00156                 return 0;
00157         }
00158         
00159         if(*p== '\0')
00160                 goto done;
00161 
00162         if(*p== '.')
00163         {
00164                 p++;
00165                 /* read the fractionar part of the seconds*/
00166                 while(*p!= '\0' && *p>= '0' && *p<= '9')
00167                 {
00168                         p++;
00169                 }
00170         }
00171 
00172         if(*p== '\0')
00173                 goto done;
00174 
00175         
00176         /* read time zone */
00177 
00178         if(*p== 'Z')
00179         {
00180                 goto done;
00181         }
00182 
00183         if(*p== '+')
00184                 sign= -1;
00185 
00186         p++;
00187 
00188         if(sscanf(p, "%c%c:%c%c", &h1, &h2, &m1, &m2) < 0) {
00189                 printf("error: failed to parse time\n");
00190                 return 0;
00191         }
00192         
00193         h= (h1- '0')*10+ h2- '0';
00194         m= (m1- '0')*10+ m2- '0';
00195 
00196         timezone_diff= sign* ((m+ h* 60)* 60);
00197 
00198 done:
00199         return (mktime(&tm) + timezone_diff);   
00200 }
00201 
00202