diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | download.c | 6 | ||||
-rw-r--r-- | getht.c | 70 | ||||
-rw-r--r-- | issue.h | 13 | ||||
-rw-r--r-- | issuemem.c | 107 | ||||
-rw-r--r-- | mediaxml.c | 35 | ||||
-rw-r--r-- | tocxml.c | 106 |
7 files changed, 234 insertions, 105 deletions
@@ -5,7 +5,7 @@ LDFLAGS+=`curl-config --libs` `xml2-config --libs` all: getht -getht: download.o config.o tocxml.o mediarev.o mediaxml.o xml.o +getht: download.o config.o issuemem.o tocxml.o mediarev.o mediaxml.o xml.o tocxml.o mediaxml.o: xml.o @@ -162,10 +162,10 @@ void downloadissue(CURL *curl_handle, char * directory, iss * issue, int force) } int count; - for(cur_section = issue->section, count = 0; - count <= issue->no_of_sections; - cur_section++, count++) + for(count = 0; count <= issue->no_of_sections; count++) { + cur_section = issue->section[count]; + snprintf(filename,STR_MAX,"%s/section_%i.pdf", newdir, cur_section->number); if(!force){ testfile = fopen(filename, "r"); @@ -208,9 +208,6 @@ int main(int argc, char *argv[]) } } - if(showstr) - show_iss_struct(issue, no_of_issues); - if(latest_index == -1) { fprintf(stderr, "Error: Cannot ascertain latest issue. "); @@ -228,9 +225,8 @@ int main(int argc, char *argv[]) downloadissue(NULL, save_path, issue[latest_index], force); } - if(downmedia) + if(downmedia || showstr) { - med * cur_media; int newest; issue = parsemedia(media_xml, issue, &no_of_issues); @@ -255,7 +251,7 @@ int main(int argc, char *argv[]) { newest = findnewestiss(issue, no_of_issues); for(i = 0; i <= issue[newest]->no_of_media; i++) - downloadmedia(NULL, save_path, &(issue[newest]->media[i]), force); + downloadmedia(NULL, save_path, issue[newest]->media[i], force); } if(downallmedia) @@ -263,17 +259,13 @@ int main(int argc, char *argv[]) for(i = 0; i <= no_of_issues; i++) { for(newest = 0; newest <= issue[i]->no_of_media; newest++) - downloadmedia(NULL, save_path, &(issue[i]->media[newest]), force); + downloadmedia(NULL, save_path, issue[i]->media[newest], force); } } } - if(no_of_issues > -1) - { - for(i = 0; i < no_of_issues; i++) - free(issue[i]); - free(issue); - } + if(showstr) + show_iss_struct(issue, no_of_issues); /* Ensure curl cleans itself up */ curl_easy_cleanup(main_curl_handle); @@ -331,7 +323,7 @@ int update_contents_files() void show_iss_struct(iss ** issue, int no_of_issues) /* Prints issue information */ { - int iss_no, sec_no, it_no; + int iss_no, sec_no, med_no, it_no; printf("%i Issues\n",no_of_issues); for(iss_no=0;iss_no<no_of_issues;iss_no++) { @@ -345,20 +337,32 @@ void show_iss_struct(iss ** issue, int no_of_issues) for(sec_no=0; sec_no <= (issue[iss_no]->no_of_sections); sec_no++) { printf("\t-Section %i-\n", (sec_no)); - printf("\tTitle:\t'%s'\n", issue[iss_no]->section[sec_no].title); - printf("\tURI:\t'%s'\n", issue[iss_no]->section[sec_no].uri); - printf("\tNo. of Items:\t'%i'\n", issue[iss_no]->section[sec_no].no_of_items); + printf("\tTitle:\t'%s'\n", issue[iss_no]->section[sec_no]->title); + printf("\tURI:\t'%s'\n", issue[iss_no]->section[sec_no]->uri); + printf("\tNo. of Items:\t'%i'\n", issue[iss_no]->section[sec_no]->no_of_items); - for(it_no=0; it_no < issue[iss_no]->section[sec_no].no_of_items; it_no++) + for(it_no=0; it_no <= issue[iss_no]->section[sec_no]->no_of_items; it_no++) { printf("\t\t-Item-\n"); - printf("\t\tTitle:\t'%s'\n",issue[iss_no]->section[sec_no].item[it_no].title); - printf("\t\tFirst page:\t'%i'",issue[iss_no]->section[sec_no].item[it_no].firstpage); - printf("\tLast page:\t'%i'\n",issue[iss_no]->section[sec_no].item[it_no].lastpage); + printf("\t\tTitle:\t'%s'\n",issue[iss_no]->section[sec_no]->item[it_no]->title); + printf("\t\tFirst page:\t'%i'",issue[iss_no]->section[sec_no]->item[it_no]->firstpage); + printf("\tLast page:\t'%i'\n",issue[iss_no]->section[sec_no]->item[it_no]->lastpage); } it_no = 0; } sec_no = 0; + + printf("Number of Media:\t'%i'\n",issue[iss_no]->no_of_media); + + for(med_no=0; med_no <= (issue[iss_no]->no_of_media); med_no++) + { + printf("\t-Media %i-\n", (med_no)); + printf("\tTitle:\t'%s'\n", issue[iss_no]->media[med_no]->title); + printf("\tURI:\t'%s'\n", issue[iss_no]->media[med_no]->uri); + printf("\tComment:\t'%s'\n", issue[iss_no]->media[med_no]->comment); + printf("\tPreview URI:\t'%s'\n", issue[iss_no]->media[med_no]->preview_uri); + } + med_no = 0; } } @@ -389,24 +393,22 @@ void cleariss(iss * cur_issue) cur_issue->preview_uri[0] = '\0'; cur_issue->title[0] = '\0'; cur_issue->size = 0; - cur_issue->no_of_sections = 0; - cur_issue->no_of_media = 0; - clearmed(cur_issue->media); + cur_issue->no_of_sections = -1; + cur_issue->section = NULL; + cur_issue->no_of_media = -1; + cur_issue->media = NULL; + //clearmed(cur_issue->media); } } void clearmed(med * cur_media) -/* clears all members of media arrays */ +/* clears the members of a media array */ { - int tmp; - for(tmp=0; tmp<=MED_NO; cur_media++,tmp++) - { - cur_media->uri[0] = '\0'; - cur_media->title[0] = '\0'; - cur_media->comment[0] = '\0'; - cur_media->preview_uri[0] = '\0'; - cur_media->size = 0; - } + cur_media->uri[0] = '\0'; + cur_media->title[0] = '\0'; + cur_media->comment[0] = '\0'; + cur_media->preview_uri[0] = '\0'; + cur_media->size = 0; } void clearsec(sec * cur_section) @@ -45,7 +45,7 @@ typedef struct char title[512]; int number; int size; - it item[ITEM_NO]; + it ** item; int no_of_items; } sec; @@ -64,8 +64,15 @@ typedef struct char title[512]; int size; issdates date; - sec section[SEC_NO]; + sec ** section; int no_of_sections; - med media[MED_NO]; + med ** media; int no_of_media; } iss; + +iss ** parsetoc(char *filepath, int * iss_no, int * latest); +iss ** parsemedia(char * filepath, iss ** issue, int * no_of_issues); + +iss ** assignnew_iss(int *no_of_issues, iss ** issue); +sec ** assignnew_sec(int *no_of_sections, sec ** section); +it ** assignnew_it(int * no_of_items, it ** item); diff --git a/issuemem.c b/issuemem.c new file mode 100644 index 0000000..f915de7 --- /dev/null +++ b/issuemem.c @@ -0,0 +1,107 @@ +/* + * 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 "issue.h" + +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 ** assignnew_iss(int *no_of_issues, iss ** issue) +/* assign memory for new issue */ +{ + iss ** tmp = NULL; + + 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(); + + return tmp; +} + +sec ** assignnew_sec(int *no_of_sections, sec ** section) +/* assign memory for new section */ +{ + sec ** tmp = NULL; + + if(*no_of_sections < 0) + { /* make **section a new array of section pointers */ + if( (tmp = malloc(sizeof(sec *))) == NULL ) + nogo_mem(); + } + else + { /* add a new pointer to section pointer list */ + if( (tmp = realloc(section, sizeof(sec *) + (((*no_of_sections)+1) * sizeof(sec *)))) == NULL ) + nogo_mem(); + } + + (*no_of_sections)++; + + /* make new array item a pointer to issue */ + if( (tmp[*no_of_sections] = malloc(sizeof(sec))) == NULL ) + nogo_mem(); + + return tmp; +} + +it ** assignnew_it(int * no_of_items, it ** item) +{ + it ** tmp = NULL; + + if(*no_of_items < 0) + { /* make **item a new array of item pointers */ + if( (tmp = malloc(sizeof(it *))) == NULL ) + nogo_mem(); + } + else + { /* add a new pointer to item pointer list */ + if( (tmp = realloc(item, sizeof(it *) + (((*no_of_items)+1) * sizeof(it *)))) == NULL ) + nogo_mem(); + } + + (*no_of_items)++; + + /* make new array item a pointer to item */ + if( (tmp[*no_of_items] = malloc(sizeof(it))) == NULL ) + nogo_mem(); + + return tmp; +} @@ -165,7 +165,7 @@ iss ** parsemedia(char * filepath, iss ** issue, int * no_of_issues) if(!issue_there) { /* advance to the next free issue */ - iss ** tmpiss; + iss ** tmpiss = NULL; if(*no_of_issues == -1) { /* make issue** a new array of issue pointers */ if( (tmpiss = malloc(sizeof(iss *))) == NULL ) @@ -194,9 +194,11 @@ iss ** parsemedia(char * filepath, iss ** issue, int * no_of_issues) tmp = *no_of_issues; } + iss * cur_issue = issue[tmp]; + issue[tmp]->no_of_media = -1; - cur_media = issue[tmp]->media; + med ** tmpmed = NULL; itnode = node->xmlChildrenNode; @@ -205,6 +207,31 @@ iss ** parsemedia(char * filepath, iss ** issue, int * no_of_issues) if(!xmlStrcmp(itnode->name,(char *) "item")) { + /* assign memory for new media */ + if(cur_issue->no_of_media < 0) + { /* make **section a new array of section pointers */ + if( (tmpmed = malloc(sizeof(med *))) == NULL ) + nogo_mem(); + } + else + { /* add a new pointer to media pointer list */ + if( (tmpmed = realloc(cur_issue->media, sizeof(med *) + ((cur_issue->no_of_media+1) * sizeof(med *)))) == NULL ) + nogo_mem(); + } + + cur_issue->no_of_media++; + + /* make new array item a pointer to issue */ + if( (tmpmed[cur_issue->no_of_media] = malloc(sizeof(med))) == NULL ) + nogo_mem(); + + cur_issue->media = tmpmed; + /* memory for seoction all dealt with */ + + cur_media = cur_issue->media[cur_issue->no_of_media]; + + clearmed(cur_media); + /* add media info to cur_media */ if(xmlGetProp(itnode, "uri")) strncpy(cur_media->uri, (char *) xmlGetProp(itnode, "uri"), STR_MAX); @@ -216,10 +243,6 @@ iss ** parsemedia(char * filepath, iss ** issue, int * no_of_issues) strncpy(cur_media->preview_uri, (char *) xmlGetProp(itnode, "preview_uri"), STR_MAX); strncpy(cur_media->title, (char *) xmlNodeListGetString(media_file, itnode->xmlChildrenNode, 1), STR_MAX); - - issue[tmp]->no_of_media++; - - cur_media++; } itnode = itnode->next; @@ -29,7 +29,6 @@ #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); @@ -37,17 +36,8 @@ 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 */ +/* starts parsing of xml to issue structure */ { xmlDocPtr file; xmlNodePtr node; @@ -59,10 +49,10 @@ iss ** parsetoc(char *filepath, int * iss_no, int * latest) no_of_issues = -1; - iss ** issue; + iss ** issue = NULL; + //iss ** tmp = NULL; int year; - iss ** tmp; xmlNodePtr cnode; @@ -75,29 +65,21 @@ iss ** parsetoc(char *filepath, int * iss_no, int * latest) { 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); + /* assign memory for the new issue */ + issue = assignnew_iss(&no_of_issues, issue); + + /* setup issue globals */ + issue[no_of_issues]->no_of_media = -1; + issue[no_of_issues]->no_of_sections = -1; + 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)); + + /* parse the issue */ + parseissue(file, cnode, issue[no_of_issues], latest); } cnode = cnode->next; } @@ -113,39 +95,40 @@ iss ** parsetoc(char *filepath, int * iss_no, int * latest) } int parseissue(xmlDocPtr file, xmlNodePtr node, iss * cur_issue, int * latest) -/* parses issue from xml, saving in cur_issue structure */ +/* 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]); + /* assign memory for new section */ + cur_issue->section = + assignnew_sec(&(cur_issue->no_of_sections), cur_issue->section); + + /* setup section globals */ + cur_issue->section[cur_issue->no_of_sections]->no_of_items = -1; - parsesection(file, node, cur_section); + /* parse the section */ + parsesection(file, node, cur_issue->section[cur_issue->no_of_sections]); } node = node->next; } - return no_of_sections; + return 0; } void parsesection(xmlDocPtr file, xmlNodePtr node, sec * cur_section) /* parses section xml, filling cur_section structure */ { - it * cur_item; + it * cur_item = NULL; strncpy(cur_section->uri, (char *) xmlGetProp(node, "pdflink"), STR_MAX); strncpy(cur_section->title, (char *) xmlGetProp(node, "title"), STR_MAX); @@ -155,31 +138,38 @@ void parsesection(xmlDocPtr file, xmlNodePtr node, sec * cur_section) 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; + it ** tmp = NULL; + 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 + if(xmlNodeListGetString(file, node->xmlChildrenNode, 1) != NULL) + /* ignore items without titles */ { - cur_item->firstpage = 0; - cur_item->lastpage = 0; + /* assign memory for new item */ + cur_section->item = + assignnew_it( &(cur_section->no_of_items), cur_section->item); + + cur_item = cur_section->item[cur_section->no_of_items]; + + /* parse item */ + 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) |