"Fossies" - the Fresh Open Source Software Archive

Member "opensips-3.0.1/modules/presence_xml/pidf.c" (16 Apr 2019, 3737 Bytes) of package /linux/misc/opensips-3.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * presence module - presence server implementation
    3  *
    4  * Copyright (C) 2006 Voice Sistem S.R.L.
    5  *
    6  * This file is part of opensips, a free SIP server.
    7  *
    8  * opensips is free software; you can redistribute it and/or modify
    9  * it under the terms of the GNU General Public License as published by
   10  * the Free Software Foundation; either version 2 of the License, or
   11  * (at your option) any later version
   12  *
   13  * opensips is distributed in the hope that it will be useful,
   14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16  * GNU General Public License for more details.
   17  *
   18  * You should have received a copy of the GNU General Public License
   19  * along with this program; if not, write to the Free Software
   20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
   21  *
   22  * History:
   23  * --------
   24  *  2007-04-14  initial version (anca)
   25  */
   26 #define _XOPEN_SOURCE
   27 
   28 #include <time.h>
   29 #include <string.h>
   30 #include <stdlib.h>
   31 #include <libxml/parser.h>
   32 #include "../../dprint.h"
   33 #include "../../sr_module.h"
   34 #include "pidf.h"
   35 
   36 xmlAttrPtr xmlNodeGetAttrByName(xmlNodePtr node, const char *name)
   37 {
   38     xmlAttrPtr attr = node->properties;
   39     while (attr) {
   40         if (xmlStrcasecmp(attr->name, (unsigned char*)name) == 0)
   41             return attr;
   42         attr = attr->next;
   43     }
   44     return NULL;
   45 }
   46 
   47 char *xmlNodeGetAttrContentByName(xmlNodePtr node, const char *name)
   48 {
   49     xmlAttrPtr attr = xmlNodeGetAttrByName(node, name);
   50     if (attr)
   51         return (char*)xmlNodeGetContent(attr->children);
   52     else
   53         return NULL;
   54 }
   55 
   56 xmlNodePtr xmlNodeGetChildByName(xmlNodePtr node, const char *name)
   57 {
   58     xmlNodePtr cur = node->children;
   59     while (cur) {
   60         if (xmlStrcasecmp(cur->name, (unsigned char*)name) == 0)
   61             return cur;
   62         cur = cur->next;
   63     }
   64     return NULL;
   65 }
   66 
   67 xmlNodePtr xmlNodeGetNodeByName(xmlNodePtr node, const char *name,
   68                                                             const char *ns)
   69 {
   70     xmlNodePtr cur = node;
   71     while (cur) {
   72         xmlNodePtr match = NULL;
   73         if (xmlStrcasecmp(cur->name, (unsigned char*)name) == 0) {
   74             if (!ns || (cur->ns && xmlStrcasecmp(cur->ns->prefix,
   75                             (unsigned char*)ns) == 0))
   76                 return cur;
   77         }
   78         match = xmlNodeGetNodeByName(cur->children, name, ns);
   79         if (match)
   80             return match;
   81         cur = cur->next;
   82     }
   83     return NULL;
   84 }
   85 
   86 char *xmlNodeGetNodeContentByName(xmlNodePtr root, const char *name,
   87         const char *ns)
   88 {
   89     xmlNodePtr node = xmlNodeGetNodeByName(root, name, ns);
   90     if (node)
   91         return (char*)xmlNodeGetContent(node->children);
   92     else
   93         return NULL;
   94 }
   95 
   96 xmlNodePtr xmlDocGetNodeByName(xmlDocPtr doc, const char *name, const char *ns)
   97 {
   98     xmlNodePtr cur = doc->children;
   99     return xmlNodeGetNodeByName(cur, name, ns);
  100 }
  101 
  102 char *xmlDocGetNodeContentByName(xmlDocPtr doc, const char *name,
  103         const char *ns)
  104 {
  105     xmlNodePtr node = xmlDocGetNodeByName(doc, name, ns);
  106     if (node)
  107         return (char*)xmlNodeGetContent(node->children);
  108     else
  109         return NULL;
  110 }
  111 
  112 time_t xml_parse_dateTime(char* xml_time_str)
  113 {
  114     struct tm tm;
  115     char * p;
  116     int h, m;
  117     char h1, h2, m1, m2;
  118     int sign= 1;
  119     signed int timezone_diff= 0;
  120 
  121     p= strptime(xml_time_str, "%F", &tm);
  122     if(p== NULL)
  123     {
  124         printf("error: failed to parse time\n");
  125         return 0;
  126     }
  127     p++;
  128     p= strptime(p, "%T", &tm);
  129     if(p== NULL)
  130     {
  131         printf("error: failed to parse time\n");
  132         return 0;
  133     }
  134 
  135     if(*p== '\0')
  136         goto done;
  137 
  138     if(*p== '.')
  139     {
  140         p++;
  141         /* read the fractionar part of the seconds*/
  142         while(*p!= '\0' && *p>= '0' && *p<= '9')
  143         {
  144             p++;
  145         }
  146     }
  147 
  148     if(*p== '\0')
  149         goto done;
  150 
  151 
  152     /* read time zone */
  153 
  154     if(*p== 'Z')
  155     {
  156         goto done;
  157     }
  158 
  159     if(*p== '+')
  160         sign= -1;
  161 
  162     p++;
  163 
  164     sscanf(p, "%c%c:%c%c", &h1, &h2, &m1, &m2);
  165 
  166     h= (h1- '0')*10+ h2- '0';
  167     m= (m1- '0')*10+ m2- '0';
  168 
  169     timezone_diff= sign* ((m+ h* 60)* 60);
  170 
  171 done:
  172     return (mktime(&tm) + timezone_diff);
  173 }
  174 
  175