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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/*
* Copyright 2006 Nick White
*
* This file is part of GetHT
*
* GetHT is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GetHT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GetHT; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include "issue.h"
#include "getht.h"
iss ** parsetoc(char *filepath, int * iss_no, int * latest);
iss ** parseyear(xmlDocPtr file, xmlNodePtr node, iss ** issue, int * latest);
int parseissue(xmlDocPtr file, xmlNodePtr node, iss * cur_issue, int * latest);
void parsesection(xmlDocPtr file, xmlNodePtr node, sec * cur_section);
void tokenise_hyphons(char to_token[10], int * first, int * last);
int no_of_issues;
void nogo_mem()
/* called if memory assignation fails
TODO: handle freeing of memory to avoid leaks */
{
fprintf(stderr, "Could not assign memory, exitting\n");
exit(1);
}
iss ** parsetoc(char *filepath, int * iss_no, int * latest)
/* starts parsing of xml to issue structure
TODO: combine with parseyear */
{
xmlDocPtr file;
xmlNodePtr node;
if(ready_xml(filepath, "issues", &file, &node))
return NULL;
*node = *node->xmlChildrenNode;
no_of_issues = -1;
iss ** issue;
int year;
iss ** tmp;
xmlNodePtr cnode;
while(node != NULL)
{
if(!xmlStrncmp(node->name,(char *) "year",4))
{
cnode = node->children;
while(cnode != NULL)
{
if(!xmlStrncmp(cnode->name,(char *) "issue",5))
{
if(no_of_issues < 0)
{ /* make issue** a new array of issue pointers */
if( (tmp = malloc(sizeof(iss *))) == NULL )
nogo_mem();
}
else
{ /* add a new pointer to issue pointer list */
if( (tmp = realloc(issue, sizeof(iss *) + ((no_of_issues+1) * sizeof(iss *)))) == NULL )
nogo_mem();
}
no_of_issues++;
/* make new array item a pointer to issue */
if( (tmp[no_of_issues] = malloc(sizeof(iss))) == NULL )
nogo_mem();
issue = tmp;
issue[no_of_issues]->no_of_media = -1; /* set default no of media */
issue[no_of_issues]->date.year = atoi( (const char *)(xmlStrsub(node->name,5,4)) );
tokenise_hyphons(xmlStrsub(cnode->name,6,5), &(issue[no_of_issues]->date.firstmonth), &(issue[no_of_issues]->date.lastmonth));
issue[no_of_issues]->no_of_sections = parseissue(file, cnode, issue[no_of_issues], latest);
}
cnode = cnode->next;
}
}
node = node->next;
}
xmlFreeDoc(file);
*iss_no = no_of_issues;
return issue;
}
int parseissue(xmlDocPtr file, xmlNodePtr node, iss * cur_issue, int * latest)
/* parses issue from xml, saving in cur_issue structure */
{
int no_of_sections = -1;
strncpy(cur_issue->title, (char *) xmlGetProp(node, "title"), STR_MAX);
strncpy(cur_issue->preview_uri, (char *) xmlGetProp(node, "coverlink"), STR_MAX);
if(xmlGetProp(node, "current") && *latest==-1)
*latest = no_of_issues;
sec * cur_section = NULL;
node = node->xmlChildrenNode;
while(node != NULL){
if(!xmlStrncmp(node->name, (char *) "section",7) ||
!xmlStrcmp(node->name, (const xmlChar *) "cover"))
{
no_of_sections++;
cur_section = &(cur_issue->section[no_of_sections]);
parsesection(file, node, cur_section);
}
node = node->next;
}
return no_of_sections;
}
void parsesection(xmlDocPtr file, xmlNodePtr node, sec * cur_section)
/* parses section xml, filling cur_section structure */
{
it * cur_item;
strncpy(cur_section->uri, (char *) xmlGetProp(node, "pdflink"), STR_MAX);
strncpy(cur_section->title, (char *) xmlGetProp(node, "title"), STR_MAX);
if(!xmlStrcmp(node->name, (const xmlChar *) "cover"))
cur_section->number = 0;
else
cur_section->number = atoi( (const char *)(xmlStrsub(node->name,8,1)) );
cur_item = cur_section->item;
cur_section->no_of_items = 0;
node = node->xmlChildrenNode;
char * pagenums;
while(node != NULL)
{
if(!xmlStrcmp(node->name, (const xmlChar *) "item"))
{
cur_section->no_of_items++;
cur_item->title = xmlNodeListGetString(file, node->xmlChildrenNode, 1);
if(pagenums = (char *) xmlGetProp(node, "pages"))
tokenise_hyphons(pagenums, &(cur_item->firstpage), &(cur_item->lastpage));
else
{
cur_item->firstpage = 0;
cur_item->lastpage = 0;
}
cur_item++;
}
node = node->next;
}
cur_item = 0;
}
void tokenise_hyphons(char to_token[10], int * first, int * last)
/* splits string to_token, filling positions passed */
{
char token[10];
char * tmp;
tmp = token;
while(*to_token != '-' && *to_token)
{
*tmp = *to_token;
to_token++; tmp++;
}
*first = atoi(token);
if(!*to_token)
*last = *first;
else
{
to_token++; /* advance past '-' */
tmp = token; /* reset tmp */
while(*to_token)
{
*tmp = *to_token;
tmp++; to_token++;
}
*last = atoi(token);
}
}
int cur_identifiers(char * filepath, char * title, issdates * date)
/* parses xml file to ascertain current issue title and date */
{
xmlDocPtr file;
if((file = xmlParseFile(filepath)) == NULL)
{
return 1;
}
xmlNodePtr node,cnode;
node = xmlDocGetRootElement(file);
if(node == NULL)
{
fprintf(stderr,"Error: xml file %s has no root element",filepath);
xmlFreeDoc(file);
return 1;
}
if(xmlStrcmp(node->name, (const xmlChar *) "issues"))
{
fprintf(stderr,"Document of the wrong type, root node is '%s' (expected 'issues').\n",(char *) node->name);
fprintf(stderr,"Continuing regardless...\n");
}
/* Now that's all sorted, let's do some work */
node = node->xmlChildrenNode;
xmlChar *temp;
while(node != NULL)
{
if(!xmlStrncmp(node->name,(char *) "year",4))
{
cnode = node->children;
while(cnode != NULL)
{
if(!xmlStrncmp(cnode->name,(char *) "issue",5))
{
temp = xmlGetProp(cnode, "current");
if(temp)
{
strncpy(title, (char *) xmlGetProp(cnode, "title"), STR_MAX);
date->year = atoi( (const char *)(xmlStrsub(node->name,5,4)) );
tokenise_hyphons(xmlStrsub(cnode->name,6,5), &(date->firstmonth), &(date->lastmonth));
return 0;
}
}
cnode = cnode->next;
}
}
node = node->next;
}
return 0;
}
|