File: contacts.c

package info (click to toggle)
flickcurl 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,508 kB
  • ctags: 3,340
  • sloc: ansic: 26,020; sh: 11,915; xml: 1,025; makefile: 343
file content (177 lines) | stat: -rw-r--r-- 5,231 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* -*- Mode: c; c-basic-offset: 2 -*-
 *
 * contacts.c - Flickcurl method contact functions
 *
 * Copyright (C) 2007-2009, David Beckett http://www.dajobe.org/
 * Copyright (C) 2007 Vanilla I. Shu 
 *   (flickcurl_free_contact, flickcurl_free_contacts,
 *    flickcurl_build_contacts)
 * 
 * This file is licensed under the following three licenses as alternatives:
 *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
 *   2. GNU General Public License (GPL) V2 or any newer version
 *   3. Apache License, V2.0 or any newer version
 * 
 * You may not use this file except in compliance with at least one of
 * the above three licenses.
 * 
 * See LICENSE.html or LICENSE.txt at the top of this package for the
 * complete terms and further detail along with the license texts for
 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
 * 
 */

#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef WIN32
#include <win32_flickcurl_config.h>
#endif

#include <flickcurl.h>
#include <flickcurl_internal.h>


/**
 * flickcurl_free_contact:
 * @contact_object: contact object
 *
 * Destructor for contact object
 */
void
flickcurl_free_contact(flickcurl_contact *contact_object)
{
  FLICKCURL_ASSERT_OBJECT_POINTER_RETURN(contact_object, flickcurl_contact);

  if(contact_object->nsid)
    free(contact_object->nsid);
  if(contact_object->username)
    free(contact_object->username);
  if(contact_object->realname)
    free(contact_object->realname);

  free(contact_object);
}


/**
 * flickcurl_free_contacts:
 * @contacts_object: contact object array
 *
 * Destructor for array of contact object
 */
void
flickcurl_free_contacts(flickcurl_contact **contacts_object)
{
  int i;
  
  FLICKCURL_ASSERT_OBJECT_POINTER_RETURN(contacts_object, flickcurl_contact);

  for(i = 0; contacts_object[i]; i++)
    flickcurl_free_contact(contacts_object[i]);
  
  free(contacts_object);
}


flickcurl_contact**
flickcurl_build_contacts(flickcurl* fc, 
                         xmlXPathContextPtr xpathCtx, const xmlChar* xpathExpr,
                         int* contact_count_p)
{
  flickcurl_contact** contacts = NULL;
  int nodes_count;
  int contact_count;
  int i;
  xmlXPathObjectPtr xpathObj = NULL;
  xmlNodeSetPtr nodes;
  
  /* Now do contacts */
  xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
  if(!xpathObj) {
    flickcurl_error(fc, "Unable to evaluate XPath expression \"%s\"", 
                    xpathExpr);
    fc->failed = 1;
    goto tidy;
  }
  
  nodes = xpathObj->nodesetval;
  /* This is a max size - it can include nodes that are CDATA */
  nodes_count = xmlXPathNodeSetGetLength(nodes);
  contacts = (flickcurl_contact**)calloc(sizeof(flickcurl_contact*), nodes_count+1);
  
  for(i = 0, contact_count = 0; i < nodes_count; i++) {
    xmlNodePtr node = nodes->nodeTab[i];
    xmlAttr* attr;
    flickcurl_contact* contact_object;
    
    if(node->type != XML_ELEMENT_NODE) {
      flickcurl_error(fc, "Got unexpected node type %d", node->type);
      fc->failed = 1;
      break;
    }
    
    contact_object = (flickcurl_contact*)calloc(sizeof(flickcurl_contact), 1);
    
    for(attr = node->properties; attr; attr = attr->next) {
      size_t attr_len = strlen((const char*)attr->children->content);
      const char *attr_name = (const char*)attr->name;
      char *attr_value;
      
      attr_value = (char*)malloc(attr_len + 1);
      memcpy(attr_value, attr->children->content, attr_len + 1);
      
      if(!strcmp(attr_name, "nsid"))
        contact_object->nsid = attr_value;
      else if(!strcmp(attr_name, "username"))
        contact_object->username = attr_value;
      else if(!strcmp(attr_name, "iconserver")) {
        contact_object->iconserver = atoi((const char*)attr_value);
        free(attr_value);
      } else if(!strcmp(attr_name, "realname"))
        contact_object->realname = attr_value;
      else if(!strcmp(attr_name, "friend")) {
        contact_object->is_friend = atoi((const char*)attr_value);
        free(attr_value);
      } else if(!strcmp(attr_name, "family")) {
        contact_object->is_family = atoi((const char*)attr_value);
        free(attr_value);
      } else if(!strcmp(attr_name, "ignored")) {
        contact_object->ignored = atoi((const char*)attr_value);
        free(attr_value);
      } else if(!strcmp(attr_name, "uploaded")) {
        contact_object->uploaded = atoi((const char*)attr_value);
        free(attr_value);
      } else
        free(attr_value);
    }

#if FLICKCURL_DEBUG > 1
    fprintf(stderr, "contact: NSID %s username %s iconserver %d realname %s friend %d family %d ignored %d uploaded %d\n",
            contact_object->nsid,
            contact_object->username,
            contact_object->iconserver,
            contact_object->realname,
            contact_object->is_friend,
            contact_object->is_family,
            contact_object->ignored,
            contact_object->uploaded);
#endif
    
    contacts[contact_count++] = contact_object;
  } /* for nodes */

  if(contact_count_p)
    *contact_count_p = contact_count;
  
 tidy:
  if(xpathObj)
    xmlXPathFreeObject(xpathObj);

  return contacts;
}