presence_xml/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 
00029 /*! \file
00030  * \brief Kamailio Presence_XML ::  PIDF handling
00031  * \ingroup presence_xml
00032  */
00033 
00034 /**
00035  * make strptime available
00036  * use 600 for 'Single UNIX Specification, Version 3'
00037  * _XOPEN_SOURCE creates conflict in header definitions in Solaris
00038  */
00039 #ifndef __OS_solaris
00040    #define _XOPEN_SOURCE 600          /* glibc2 on linux, bsd */
00041 #else
00042    #define _XOPEN_SOURCE_EXTENDED 1   /* solaris */
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       /* read the fractionar part of the seconds*/
00163       while(*p!= '\0' && *p>= '0' && *p<= '9')
00164       {
00165          p++;
00166       }
00167    }
00168 
00169    if(*p== '\0')
00170       goto done;
00171 
00172    
00173    /* read time zone */
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 

Generated on Thu May 24 08:00:52 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6