-
Notifications
You must be signed in to change notification settings - Fork 16
/
photoset.c
217 lines (176 loc) · 5.86 KB
/
photoset.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* -*- Mode: c; c-basic-offset: 2 -*-
*
* photoset.c - Flickcurl photoset functions
*
* Copyright (C) 2007-2008, David Beckett https://www.dajobe.org/
*
* 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>
#include <stdlib.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_photoset:
* @photoset: photoset object
*
* Destructor for photoset object
*/
void
flickcurl_free_photoset(flickcurl_photoset *photoset)
{
FLICKCURL_ASSERT_OBJECT_POINTER_RETURN(photoset, flickcurl_photoset);
if(photoset->id)
free(photoset->id);
if(photoset->primary)
free(photoset->primary);
if(photoset->secret)
free(photoset->secret);
if(photoset->title)
free(photoset->title);
if(photoset->description)
free(photoset->description);
if(photoset->owner)
free(photoset->owner);
free(photoset);
}
/**
* flickcurl_free_photosets:
* @photosets_object: photoset object array
*
* Destructor for array of photoset object
*/
void
flickcurl_free_photosets(flickcurl_photoset **photosets_object)
{
int i;
FLICKCURL_ASSERT_OBJECT_POINTER_RETURN(photosets_object, flickcurl_photoset_array);
for(i = 0; photosets_object[i]; i++)
flickcurl_free_photoset(photosets_object[i]);
free(photosets_object);
}
flickcurl_photoset**
flickcurl_build_photosets(flickcurl* fc, xmlXPathContextPtr xpathCtx,
const xmlChar* xpathExpr, int* photoset_count_p)
{
flickcurl_photoset** photosets = NULL;
int nodes_count;
int photoset_count;
int i;
xmlXPathObjectPtr xpathObj = NULL;
xmlNodeSetPtr nodes;
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);
photosets = (flickcurl_photoset**)calloc(nodes_count+1, sizeof(flickcurl_photoset*));
for(i = 0, photoset_count = 0; i < nodes_count; i++) {
xmlNodePtr node = nodes->nodeTab[i];
xmlAttr* attr;
flickcurl_photoset* ps;
xmlNodePtr chnode;
if(node->type != XML_ELEMENT_NODE) {
flickcurl_error(fc, "Got unexpected node type %d", node->type);
fc->failed = 1;
break;
}
ps = (flickcurl_photoset*)calloc(1, sizeof(flickcurl_photoset));
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, "id"))
ps->id = attr_value;
else if(!strcmp(attr_name, "primary"))
ps->primary = attr_value;
else if(!strcmp(attr_name, "secret")) {
ps->secret = attr_value;
} else if(!strcmp(attr_name, "server")) {
ps->server = atoi(attr_value);
free(attr_value);
} else if(!strcmp(attr_name, "farm")) {
ps->farm = atoi(attr_value);
free(attr_value);
} else if(!strcmp(attr_name, "photos")) {
ps->photos_count = atoi(attr_value);
free(attr_value);
} else if(!strcmp(attr_name, "owner"))
ps->owner = attr_value;
else
free(attr_value);
}
/* Walk children nodes for <title> or <description> elements */
for(chnode = node->children; chnode; chnode = chnode->next) {
const char *chnode_name = (const char*)chnode->name;
if(chnode->type == XML_ELEMENT_NODE) {
if(!strcmp(chnode_name, "title")) {
if(chnode->children) {
size_t len = strlen((const char*)chnode->children->content);
ps->title = (char*)malloc(len + 1);
memcpy(ps->title, chnode->children->content, len + 1);
}
} else if(!strcmp(chnode_name, "description")) {
if(chnode->children) {
size_t len = strlen((const char*)chnode->children->content);
ps->description = (char*)malloc(len + 1);
memcpy(ps->description, chnode->children->content, len + 1);
}
}
}
}
#if FLICKCURL_DEBUG > 1
fprintf(stderr, "photoset: id %s primary '%s' secret %s server %d farm %d photos count %d title '%s' description '%s'\n",
ps->id, ps->primary, ps->secret, ps->server, ps->farm,
ps->photos_count,
ps->title,
(ps->description ? ps->description : "(No description)"));
#endif
photosets[photoset_count++] = ps;
} /* for nodes */
if(photoset_count_p)
*photoset_count_p = photoset_count;
tidy:
if(xpathObj)
xmlXPathFreeObject(xpathObj);
return photosets;
}
flickcurl_photoset*
flickcurl_build_photoset(flickcurl* fc, xmlXPathContextPtr xpathCtx)
{
flickcurl_photoset** photosets;
flickcurl_photoset* result = NULL;
photosets = flickcurl_build_photosets(fc, xpathCtx,
(const xmlChar*)"/rsp/photoset", NULL);
if(photosets) {
result = photosets[0];
free(photosets);
}
return result;
}