From e91ec0d87aa31cb465fe8cf934d405ad56342eba Mon Sep 17 00:00:00 2001 From: Nick White Date: Wed, 19 May 2010 17:15:41 +0100 Subject: Switched to simpler build system, and fixed bugs Now there's a proper build system in place, which is actually simple enough to understand. I also fixed plenty of warnings about the code (reminding me how badly I knew C when I wrote this). Hinduism today aren't indexing their new issues using the index file I was sourcing any more, so I don't expect to fix any more bugs or improve this much. --- src/.gitignore | 5 - src/Makefile.am | 7 -- src/cli.c | 79 ---------------- src/config.c | 172 ----------------------------------- src/download.c | 276 -------------------------------------------------------- src/getht.c | 223 --------------------------------------------- src/getht.h | 52 ----------- src/issue.h | 59 ------------ src/issuemem.c | 148 ------------------------------ src/tocxml.c | 259 ---------------------------------------------------- src/xml.c | 60 ------------ 11 files changed, 1340 deletions(-) delete mode 100644 src/.gitignore delete mode 100644 src/Makefile.am delete mode 100644 src/cli.c delete mode 100644 src/config.c delete mode 100644 src/download.c delete mode 100644 src/getht.c delete mode 100644 src/getht.h delete mode 100644 src/issue.h delete mode 100644 src/issuemem.c delete mode 100644 src/tocxml.c delete mode 100644 src/xml.c (limited to 'src') diff --git a/src/.gitignore b/src/.gitignore deleted file mode 100644 index 546ae33..0000000 --- a/src/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -# ignore autotools creations -.deps - -# ignore built binary -getht diff --git a/src/Makefile.am b/src/Makefile.am deleted file mode 100644 index e6b2bc1..0000000 --- a/src/Makefile.am +++ /dev/null @@ -1,7 +0,0 @@ -## A simple file to process with Automake, to produce Makefile.in - -bin_PROGRAMS = getht - -getht_SOURCES = config.c cli.c download.c getht.c issuemem.c \ - tocxml.c xml.c \ - getht.h issue.h version.h diff --git a/src/cli.c b/src/cli.c deleted file mode 100644 index e1bedcf..0000000 --- a/src/cli.c +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2006,2008 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 3 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, see . - * - */ - -#include -#include - -#include "issue.h" -#include "getht.h" - -void list_issues(iss ** issue, int no_of_issues, int verbose) -{ - int iss_no, sec_no, it_no; - for(iss_no=0;iss_no<=no_of_issues;iss_no++) - { - printf("%2i: %s\n", iss_no, issue[iss_no]->title); - if(verbose >= 2) - printf(" Year: %i; Months: %i - %i\n",issue[iss_no]->date.year,issue[iss_no]->date.firstmonth,issue[iss_no]->date.lastmonth); - if(verbose >= 1) - { - for(sec_no=0; sec_no<=issue[iss_no]->no_of_sections; sec_no++) - { - printf(" %2i: %s\n", issue[iss_no]->section[sec_no]->number, - issue[iss_no]->section[sec_no]->title); - printf(" %s\n", issue[iss_no]->section[sec_no]->uri); - if(verbose >= 2) - { - for(it_no=0; - it_no<=issue[iss_no]->section[sec_no]->no_of_items; - it_no++) - { - printf(" pp %2i - %2i: %s\n", - issue[iss_no]->section[sec_no]->item[it_no]->firstpage, - issue[iss_no]->section[sec_no]->item[it_no]->lastpage, - issue[iss_no]->section[sec_no]->item[it_no]->title); - } - } - } - } - } -} - -void showusage() -{ - printf("Usage: getht [options] -a\n"); - printf(" or: getht [options] -d issno [-s secno]\n"); - printf(" or: getht [options] -l\n"); - printf("Downloads issue(s) of Hinduism Today\n\n"); - printf("Download options:\n"); - printf(" -a, --download-all Download all issues\n"); - printf(" -d, --download-issue issno Download issue number issno\n"); - printf(" -s, --download-section secno Download section number secno\n"); - printf(" -f, --force Force re-download of existing files\n"); - printf("Discovery options:\n"); - printf(" -l, --list-issues List available issues\n"); - printf(" -u, --update Update contents files\n"); - printf("Global options:\n"); - printf(" -q, --quiet Make output less verbose\n"); - printf(" -v, --verbose Make output more verbose\n"); - printf("Other options:\n"); - printf(" -h, --help Print this help message\n"); - printf(" -V, --version Print version information\n"); -} diff --git a/src/config.c b/src/config.c deleted file mode 100644 index b22e158..0000000 --- a/src/config.c +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright 2006,2008 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 3 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, see . - * - */ - -#include -#include - -#include "getht.h" - -#include - -int loadconfig(char * getht_path, struct config * options) -/* Loads variables from config file to extern and passed - * variables. */ -{ - FILE * config_file; - char filepath[STR_MAX]; - - snprintf(filepath, STR_MAX, "%s/config.ini", getht_path); - - if((config_file = fopen(filepath,"r")) == NULL) - { - fprintf(stderr,"Cannot open file %s for reading.\n",filepath); - return 1; - } - - char parameter[80], option[80]; - while(!feof(config_file)) - { - fscanf(config_file, "%s = %s", option, parameter); - - if(option[0] == '#'); /* ignore lines beginning with a hash */ - else if(!strcmp(option, "savepath")) - strncpy(options->save_path, parameter, STR_MAX); - else if(!strcmp(option, "startup_check")) - options->startup_check = atoi(parameter); - else if(!strcmp(option, "toc_uri")) - strncpy(options->issue_uri, parameter, STR_MAX); - else if(!strcmp(option, "proxy_type")) - { - if(!strcmp(parameter, "http")) - options->proxy.type = CURLPROXY_HTTP; - else if(!strcmp(parameter, "socks4")) - options->proxy.type = CURLPROXY_SOCKS4; - else if(!strcmp(parameter, "socks5")) - options->proxy.type = CURLPROXY_SOCKS5; - else - fprintf(stderr, - "Proxy type %s not known, please use either http, socks4 or socks5\n", - parameter); - } - else if(!strcmp(option, "proxy_address")) - strncpy(options->proxy.address, parameter, STR_MAX); - else if(!strcmp(option, "proxy_port")) - options->proxy.port = (long) atoi(parameter); - else if(!strcmp(option, "proxy_auth")) - { - if(!strcmp(parameter, "basic")) - options->proxy.auth = CURLAUTH_BASIC; - else if(!strcmp(parameter, "digest")) - options->proxy.auth = CURLAUTH_DIGEST; - else if(!strcmp(parameter, "ntlm")) - options->proxy.auth = CURLAUTH_NTLM; - else - fprintf(stderr, - "config.ini: Proxy authentication method %s not known, please use basic, digest or ntlm", - parameter); - } - else if(!strcmp(option, "proxy_user")) - strncpy(options->proxy.user, parameter, STR_MAX); - else if(!strcmp(option, "proxy_pass")) - strncpy(options->proxy.pass, parameter, STR_MAX); - else - fprintf(stderr, "config.ini: Option '%s' not recognised, ignoring\n", option); - } - - fclose(config_file); - - return 0; -} - -int writefreshconfig(char * getht_path, struct config * options) -/* Write a new config file according to extern and passed variables. */ -{ - FILE * config_file; - char filepath[STR_MAX]; - - snprintf(filepath, STR_MAX, "%s/config.ini", getht_path); - - if((config_file = fopen(filepath,"w")) == NULL) - { - fprintf(stderr,"Cannot open file %s for writing.\n",filepath); - return 1; - } - else - fprintf(stdout,"Writing a fresh config file to %s.\n",filepath); - - if(options->save_path[0]) - fprintf(config_file, "%s = %s\n", "savepath", options->save_path); - if(options->startup_check) - fprintf(config_file, "%s = %i\n", "startup_check", options->startup_check); - if(options->issue_uri[0]) - fprintf(config_file, "%s = %s\n", "toc_uri", options->issue_uri); - if(options->proxy.type) - { - if(options->proxy.type == CURLPROXY_HTTP) - fprintf(config_file, "%s = %s\n", "proxy_type", "http"); - else if(options->proxy.type == CURLPROXY_SOCKS4) - fprintf(config_file, "%s = %s\n", "proxy_type", "socks4"); - else if(options->proxy.type == CURLPROXY_SOCKS5) - fprintf(config_file, "%s = %s\n", "proxy_type", "socks5"); - } - if(options->proxy.address[0]) - fprintf(config_file, "%s = %s\n", "proxy_address", options->proxy.address); - if(options->proxy.port) - fprintf(config_file, "%s = %i\n", "proxy_port", options->proxy.port); - if(options->proxy.auth) - { - if(options->proxy.auth = CURLAUTH_BASIC) - fprintf(config_file, "%s = %s\n", "proxy_auth", "basic"); - else if(options->proxy.auth = CURLAUTH_DIGEST) - fprintf(config_file, "%s = %s\n", "proxy_auth", "digest"); - else if(options->proxy.auth = CURLAUTH_NTLM) - fprintf(config_file, "%s = %s\n", "proxy_auth", "ntlm"); - } - if(options->proxy.user[0]) - fprintf(config_file, "%s = %s\n", "proxy_user", options->proxy.user); - if(options->proxy.pass[0]) - fprintf(config_file, "%s = %s\n", "proxy_pass", options->proxy.pass); - - fclose(config_file); - - return 0; -} - -int updateconfig(char * getht_path, struct config * options) -/* Read existing config file, and rewrite any variables which differ - * in memory. */ -{ - FILE * config_file; - char filepath[STR_MAX]; - - snprintf(filepath, STR_MAX, "%s/config.ini", getht_path); - - if((config_file = fopen(filepath,"rw")) == NULL) - { - fprintf(stderr,"Cannot open file %s for reading/writing.\n",filepath); - return 1; - } - - /* Not yet implemented */ - - fclose(config_file); - - return 1; -} diff --git a/src/download.c b/src/download.c deleted file mode 100644 index a6dcc49..0000000 --- a/src/download.c +++ /dev/null @@ -1,276 +0,0 @@ -/* - * Copyright 2006,2008 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 3 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, see . - * - */ - -#include -#include -#include -#include -#include -#include - -#include "getht.h" -#include "issue.h" - -int read_func(void *ptr, size_t size, size_t nmemb, FILE *stream) - { return fread(ptr, size, nmemb, stream); } -int write_func(void *ptr, size_t size, size_t nmemb, FILE *stream) - { return fwrite(ptr, size, nmemb, stream); } -int update_progress(void *data, double dltotal, double dlnow, - double ultotal, double ulnow); - -int save_file(char *uri, char *filepath, char *filetitle, long resume_offset, struct config * options) -/* Save the file *uri to *filepath */ -{ - if(!options->quiet) - { - printf("Downloading %s ",filetitle); - fflush(stdout); - } - - if(options->curl_handle) { - FILE *file; - file = fopen(filepath, resume_offset?"a":"w"); - if(!file) - { - fprintf(stderr,"Error: cannot open file %s for writing.\n",filepath); - return 1; - } - - curl_easy_setopt(options->curl_handle, CURLOPT_URL, uri); - curl_easy_setopt(options->curl_handle, CURLOPT_READFUNCTION, read_func); - curl_easy_setopt(options->curl_handle, CURLOPT_WRITEFUNCTION, write_func); - curl_easy_setopt(options->curl_handle, CURLOPT_WRITEDATA, file); - - if(options->proxy.type) - { - curl_easy_setopt(options->curl_handle, CURLOPT_PROXYTYPE, options->proxy.type); - curl_easy_setopt(options->curl_handle, CURLOPT_PROXY, options->proxy.address); - if(options->proxy.port) - curl_easy_setopt(options->curl_handle, CURLOPT_PROXYPORT, options->proxy.port); - if(options->proxy.auth) - curl_easy_setopt(options->curl_handle, CURLOPT_PROXYAUTH, options->proxy.auth); - if(options->proxy.user[0] && options->proxy.pass[0]) - { - char userpass[STR_MAX]; - snprintf(userpass, STR_MAX, "%s:%s", options->proxy.user, options->proxy.pass); - curl_easy_setopt(options->curl_handle, CURLOPT_PROXYUSERPWD, userpass); - } - } - - if(!options->quiet) - { - curl_easy_setopt(options->curl_handle, CURLOPT_NOPROGRESS, 0); - curl_easy_setopt(options->curl_handle, CURLOPT_PROGRESSFUNCTION, update_progress); - curl_easy_setopt(options->curl_handle, CURLOPT_PROGRESSDATA, &resume_offset); - } - else - curl_easy_setopt(options->curl_handle, CURLOPT_NOPROGRESS, 1); - - curl_easy_setopt(options->curl_handle, CURLOPT_RESUME_FROM, resume_offset); - - /* create a buffer to hold any curl errors */ - char errorinfo[CURL_ERROR_SIZE]; - curl_easy_setopt(options->curl_handle, CURLOPT_ERRORBUFFER, errorinfo); - - if(curl_easy_perform(options->curl_handle)) - { - remove(filepath); - fprintf(stderr,"\nError, could not download %s: %s\n",uri, errorinfo); - return 1; - } - - fclose(file); - - if(!options->quiet) - printf("\rDownloaded %s \n",filetitle); - } - else { - fprintf(stderr,"Error: curl failed to initialise.\n"); - printf("Could not download %s\n",uri); - return 1; - } - return 0; -} - -int update_progress(void *data, double dltotal, double dlnow, - double ultotal, double ulnow) -/* Print status information */ -{ - double frac; - long *startsize = NULL; - - startsize = (long *)data; - long cur = (long) dlnow + *startsize; - long total = (long) dltotal + *startsize; - - if(cur > 0) - frac = 100 * (double) cur / (double) total; - else - frac = 0; - - printf("\b\b\b\b\b\b\b: %3.0lf%% ", frac); - fflush(stdout); - - return 0; -} - -double getremotefilesize(char *uri, struct config * options) -{ - double filesize; - - if(options->curl_handle) { - - curl_easy_setopt(options->curl_handle, CURLOPT_URL, uri); - curl_easy_setopt(options->curl_handle, CURLOPT_READFUNCTION, read_func); - - /* don't download or return either body or header */ - curl_easy_setopt(options->curl_handle, CURLOPT_NOBODY, 1); - curl_easy_setopt(options->curl_handle, CURLOPT_HEADER, 0); - - if(options->proxy.type) - { - curl_easy_setopt(options->curl_handle, CURLOPT_PROXYTYPE, options->proxy.type); - curl_easy_setopt(options->curl_handle, CURLOPT_PROXY, options->proxy.address); - if(options->proxy.port) - curl_easy_setopt(options->curl_handle, CURLOPT_PROXYPORT, options->proxy.port); - if(options->proxy.auth) - curl_easy_setopt(options->curl_handle, CURLOPT_PROXYAUTH, options->proxy.auth); - if(options->proxy.user[0] && options->proxy.pass[0]) - { - char userpass[STR_MAX]; - snprintf(userpass, STR_MAX, "%s:%s", options->proxy.user, options->proxy.pass); - curl_easy_setopt(options->curl_handle, CURLOPT_PROXYUSERPWD, userpass); - } - } - - curl_easy_setopt(options->curl_handle, CURLOPT_NOPROGRESS, 1); - - if(curl_easy_perform(options->curl_handle)) - filesize = -1; - - curl_easy_getinfo(options->curl_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &filesize); - - } - else - filesize = -1; - - curl_easy_reset(options->curl_handle); - return filesize; -} - -char * checkdir(char * dir) -/* Checks that dir is writable. If necessary prompt - * the user for a new directory and return it. */ -{ - int dirchanged = 0; - - if(!opendir(dir)) - while(mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) - { - fprintf(stderr,"Cannot create directory %s\n", dir); - printf("Please enter the path of a save directory: "); - scanf("%s", dir); - dirchanged = 1; - } - - if(dirchanged) - return dir; - else - return 0; -} - -char * getissuedir(struct config * options, iss * issue) -/* Returns and prepares download directory */ -{ - char * newsavedir; - char * newdir; - - newsavedir = malloc(STR_MAX); - newdir = malloc(STR_MAX); - - /* Check that main save path is ok */ - newsavedir = checkdir(options->save_path); - /* Save the new save path if it changed */ - if(newsavedir) - { - char getht_path[STR_MAX]; - snprintf(getht_path,STR_MAX,"%s/.getht",getenv("HOME")); - updateconfig(getht_path, &options); - } - - snprintf(newdir,STR_MAX,"%s/%i_%i-%i",options->save_path, - issue->date.year,issue->date.firstmonth,issue->date.lastmonth); - - /* Check that specific issue path is ok */ - checkdir(newdir); - - return newdir; -} - -void downloadsection(struct config * options, sec * section, char * downdir, int force) -/* Download section pointed to */ -{ - char filename[STR_MAX]; - char filepath[STR_MAX]; - - snprintf(filename,STR_MAX,"section_%i.pdf", section->number); - snprintf(filepath,STR_MAX,"%s/%s", downdir, filename); - - if(!force){ - struct stat fileinfo; - /* see if local file exists */ - if(stat(filepath, &fileinfo)) - save_file(section->uri, filepath, filename, 0, options); - else - { - /* get size of local file */ - long localsize = 0; - localsize = (long) fileinfo.st_size; - - /* get size of remote file */ - long remotesize = 0; - remotesize = (long) getremotefilesize(section->uri, options); - - /* if size of local file != size of remote file, resume */ - if(remotesize > 0 && localsize < remotesize) - save_file(section->uri, filepath, filename, localsize, options); - else - { - if(!options->quiet) - printf("Skipping download of completed section %i\n", section->number); - } - } - } - else - save_file(section->uri, filepath, filename, 0, options); -} - -void downloadissue(struct config * options, iss * issue, int force) -/* Download issue pointed to */ -{ - char downdir[STR_MAX]; - strncpy(downdir, getissuedir(options, issue), STR_MAX); - - printf("Downloading %s to %s\n",issue->title, downdir); - - int count; - for(count = 0; count <= issue->no_of_sections; count++) - downloadsection(options, issue->section[count], downdir, force); -} diff --git a/src/getht.c b/src/getht.c deleted file mode 100644 index de72384..0000000 --- a/src/getht.c +++ /dev/null @@ -1,223 +0,0 @@ -/* - * Copyright 2006,2008 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 3 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, see . - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "issue.h" -#include "getht.h" - -int update_contents_files(struct config * options); - -int main(int argc, char *argv[]) -{ - struct config options; - char getht_path[STR_MAX]; - - /* initialise appropriate options vars */ - options.startup_check = 0; - options.quiet = 0; - options.verbose = 0; - options.proxy.type = 0; - options.proxy.auth = 0; - options.proxy.address[0] = '\0'; - options.proxy.port = 0; - options.proxy.user[0] = '\0'; - options.proxy.pass[0] = '\0'; - - /* Define & set up paths */ - snprintf(getht_path,STR_MAX,"%s/.getht",getenv("HOME")); - - if(!opendir(getht_path)) - if(mkdir(getht_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) - { - fprintf(stderr,"Cannot open/create directory %s\n",getht_path); - printf("Please enter the path of a directory to save settings in: "); - scanf("%s", getht_path); - } - - snprintf(options.toc_xml,STR_MAX,"%s/%s",getht_path,ISS_XML_FILE); - - strncpy(options.issue_uri,XML_TOC_URI,STR_MAX); - - snprintf(options.save_path,STR_MAX,"%s/hinduism_today",getenv("HOME")); - - int downall = 0; - int downissue = 0, downissueno = -1; - int downsection = 0, downsectionno = -1; - int listissues = 0; - int force = 0; - int option = 0; - - if(loadconfig(getht_path, &options) != 0) - writefreshconfig(getht_path, &options); - - /* Parse command line options */ - char c; - static struct option long_opts[] = - { - {"download-all", no_argument, 0, 'a'}, - {"download-issue", required_argument, 0, 'd'}, - {"download-section", required_argument, 0, 's'}, - {"force", no_argument, 0, 'f'}, - {"list-issues", no_argument, 0, 'l'}, - {"quiet", no_argument, 0, 'q'}, - {"update", no_argument, 0, 'u'}, - {"help", no_argument, 0, 'h'}, - {"verbose", no_argument, 0, 'v'}, - {"version", no_argument, 0, 'V'}, - {0, 0, 0, 0} - }; - while((c = getopt_long(argc, argv, "ad:fs:hluqvV", long_opts, NULL)) != -1) { - switch(c) { - case 'a': - downall = 1; - downissue = 1; - option = 1; - break; - case 'd': - downissue = 1; - downissueno = atoi(optarg); - option = 1; - break; - case 's': - downsection = 1; - downsectionno = atoi(optarg); - option = 1; - break; - case 'l': - listissues = 1; - option = 1; - break; - case 'f': - force = 1; - option = 1; - break; - case 'u': - options.startup_check = 1; - option = 1; - break; - case 'h': - showusage(); - return 0; - break; - case 'q': - options.quiet = 1; - option = 1; - break; - case 'v': - options.verbose++; - option = 1; - break; - case 'V': - printf("%s version: %s\n",PACKAGE_NAME, PACKAGE_VERSION); - option = 1; - return 0; - break; - } - } - - if(!option) - { - showusage(); - return 0; - } - - options.curl_handle = curl_easy_init(); - - if(options.startup_check) - { - if(update_contents_files(&options)) - fprintf(stderr,"Could not update contents files\n"); - } - - /* Parse TOC, filling issue structure */ - - iss **issue; - int no_of_issues = -1; - int i; - - if(downissue || listissues) - { - issue = parsetoc(options.toc_xml, &no_of_issues); - - if(!issue) - { - if(!options.startup_check) - { - printf("Cannot open contents file, trying to update contents\n"); - if(update_contents_files(&options)) - return 1; - issue = parsetoc(options.toc_xml, &no_of_issues); - } - else - { - printf("Cannot open contents file, try running `getht --update`\n"); - return 1; - } - } - - if(downall) - { - for(i = 0; i < no_of_issues; i++) - downloadissue(&options, issue[i], force); - } - else if(downissueno >= 0 && downissueno <= no_of_issues) - { - if(downsection && downsectionno >= 0 && downsectionno <= issue[downissueno]->no_of_sections) - { - char downdir[STR_MAX]; - sec * cursec; - strncpy(downdir, (char *) getissuedir(&options, issue[downissueno]), STR_MAX); - cursec = issue[downissueno]->section[downsectionno]; - - printf("Downloading %s to %s\n", cursec->title, downdir); - - downloadsection(&options, cursec, &downdir, force); - } - else - downloadissue(&options, issue[downissueno], force); - } - } - - - if(listissues) - list_issues(issue, no_of_issues, options.verbose); - - /* Ensure curl cleans itself up */ - curl_easy_cleanup(options.curl_handle); - - return 0; -} - -int update_contents_files(struct config * options) -/* Returns 0 on success, 1 on failure */ -{ - if(save_file(options->issue_uri, options->toc_xml, "contents", 0, options)) - return 1; - else - return 0; -} diff --git a/src/getht.h b/src/getht.h deleted file mode 100644 index 6a64b6e..0000000 --- a/src/getht.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2006,2008 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 3 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, see . - * - */ - -#define XML_TOC_URI "http://www.hinduismtoday.com/digital/htde_toc.xml" - -#define ISS_XML_FILE "htde_toc.xml" - -#define STR_MAX 512 - -#include - -struct proxy_options { - char type; - char auth; - char address[STR_MAX]; - long port; - char user[STR_MAX]; - char pass[STR_MAX]; -}; - -struct config { - char toc_xml[STR_MAX]; - char issue_uri[STR_MAX]; - - char save_path[STR_MAX]; - - struct proxy_options proxy; - - int startup_check; - - int verbose; - int quiet; - - CURL *curl_handle; -}; diff --git a/src/issue.h b/src/issue.h deleted file mode 100644 index 44f2601..0000000 --- a/src/issue.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2006,2008 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 3 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, see . - * - */ - -typedef struct -{ - int year; - int firstmonth; - int lastmonth; -} issdates; - -typedef struct -{ - int firstpage; - int lastpage; - char * title; -} it; - -typedef struct -{ - char uri[512]; - char title[512]; - int number; - int size; - it ** item; - int no_of_items; -} sec; - -typedef struct -{ - char preview_uri[512]; - char title[512]; - int size; - issdates date; - sec ** section; - int no_of_sections; -} iss; - -iss ** parsetoc(char *filepath, int * iss_no); - -iss ** assignnew_iss(iss ** issue, int *no_of_issues); -sec ** assignnew_sec(sec ** section, int * no_of_sections); -it ** assignnew_it(it ** item, int * no_of_items); diff --git a/src/issuemem.c b/src/issuemem.c deleted file mode 100644 index 354c7d9..0000000 --- a/src/issuemem.c +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright 2006,2008 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 3 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, see . - * - */ - -#include -#include - -#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(iss ** issue, int *no_of_issues) -/* 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(sec ** section, int *no_of_sections) -/* 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(it ** item, int * no_of_items) -{ - 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; -} - -int issuesort(iss ** issue, int no_of_issues) -/* does a basic bubble sort, by date, returning sorted issue */ -{ - int sortindex[no_of_issues]; - - int count1, count2, temp; - - for(count1 = 0; count1 <= no_of_issues; count1++) - sortindex[count1] = count1; - - /* find correct order of issues using a bubble sort */ - for(count1 = 0; count1 <=no_of_issues; count1++) - { - for(count2 = 0; count2 < no_of_issues; count2++) - { - if(issue[sortindex[count2]]->date.year < issue[sortindex[count2+1]]->date.year) - { - temp = sortindex[count2]; - sortindex[count2] = sortindex[count2+1]; - sortindex[count2+1] = temp; - } - else if((issue[sortindex[count2]]->date.year == issue[sortindex[count2+1]]->date.year) && - (issue[sortindex[count2]]->date.firstmonth < issue[sortindex[count2+1]]->date.firstmonth)) - { - temp = sortindex[count2]; - sortindex[count2] = sortindex[count2+1]; - sortindex[count2+1] = temp; - } - } - } - - iss * sortedissue[no_of_issues]; - - for(count1 = 0; count1 <= no_of_issues; count1++) - sortedissue[count1] = issue[sortindex[count1]]; - - for(count1 = 0; count1 <= no_of_issues; count1++) - issue[count1] = sortedissue[count1]; - - return 0; -} diff --git a/src/tocxml.c b/src/tocxml.c deleted file mode 100644 index 60ef31b..0000000 --- a/src/tocxml.c +++ /dev/null @@ -1,259 +0,0 @@ -/* - * Copyright 2006,2008 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 3 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, see . - * - */ - -#include -#include -#include -#include -#include - -#include "issue.h" -#include "getht.h" - -iss ** parsetoc(char *filepath, int * iss_no); -int parseissue(xmlDocPtr file, xmlNodePtr node, iss * cur_issue); -void parsesection(xmlDocPtr file, xmlNodePtr node, sec * cur_section); - -void tokenise_hyphons(char to_token[10], int * first, int * last); - -int no_of_issues; - -iss ** parsetoc(char *filepath, int * iss_no) -/* starts parsing of xml to issue structure */ -{ - xmlDocPtr file; - xmlNodePtr node; - - if(ready_xml(filepath, "issues", &file, &node)) - return NULL; - - *node = *node->xmlChildrenNode; - - no_of_issues = -1; - - iss ** issue = NULL; - - int year; - - 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)) - { - /* assign memory for the new issue */ - issue = assignnew_iss(issue, &no_of_issues); - - /* setup issue globals */ - 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]); - } - cnode = cnode->next; - } - } - node = node->next; - } - - xmlFreeDoc(file); - - *iss_no = no_of_issues; - - issuesort(issue, no_of_issues); - - return issue; -} - -int parseissue(xmlDocPtr file, xmlNodePtr node, iss * cur_issue) -/* parses issue from xml, saving in cur_issue structure */ -{ - strncpy(cur_issue->title, (char *) xmlGetProp(node, "title"), STR_MAX); - strncpy(cur_issue->preview_uri, (char *) xmlGetProp(node, "coverlink"), STR_MAX); - - node = node->xmlChildrenNode; - - while(node != NULL){ - if(!xmlStrncmp(node->name, (char *) "section",7) || - !xmlStrcmp(node->name, (const xmlChar *) "cover")) - { - /* assign memory for new section */ - cur_issue->section = - assignnew_sec(cur_issue->section, &(cur_issue->no_of_sections)); - - /* setup section globals */ - cur_issue->section[cur_issue->no_of_sections]->no_of_items = -1; - - /* parse the section */ - parsesection(file, node, cur_issue->section[cur_issue->no_of_sections]); - } - node = node->next; - } - - return 0; -} - -void parsesection(xmlDocPtr file, xmlNodePtr node, sec * cur_section) -/* parses section xml, filling cur_section structure */ -{ - it * cur_item = NULL; - - 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)) ); - - node = node->xmlChildrenNode; - - char * pagenums; - - it ** tmp = NULL; - - while(node != NULL) - { - if(!xmlStrcmp(node->name, (const xmlChar *) "item")) - { - if(xmlNodeListGetString(file, node->xmlChildrenNode, 1) != NULL) - /* ignore items without titles */ - { - /* assign memory for new item */ - cur_section->item = - assignnew_it( cur_section->item, &(cur_section->no_of_items)); - - 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; - } - } - } - node = node->next; - } -} - -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 = xmlReadFile(filepath, "ISO-8859-1", 0)) == 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; -} diff --git a/src/xml.c b/src/xml.c deleted file mode 100644 index 9d59cf4..0000000 --- a/src/xml.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2006,2008 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 3 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, see . - * - */ - -#include -#include -#include -#include - -#include "getht.h" -#include "issue.h" - -int ready_xml(char * filepath, char * rootnode, xmlDocPtr * file, xmlNodePtr * node) -/* Opens filepath, filling nec pointers with essential information. */ -{ - if(access(filepath, R_OK) != 0) - return 1; - - if((*file = xmlReadFile(filepath, "ISO-8859-1", 0)) == NULL) - { - fprintf(stderr, "Error: cannot open xml file %s\n", filepath); - return 1; - } - - *node = xmlDocGetRootElement(*file); - - if(*node == NULL) - { - fprintf(stderr,"Error: xml file %s has no root element",filepath); - xmlFreeDoc(*file); - return 1; - } - - char * test; - test = (char *) (*node)->name; - - if(xmlStrcmp((*node)->name, (const xmlChar *) rootnode)) - { - fprintf(stderr,"Document of the wrong type, root node is '%s' (expected '%s').\n",(char *) (*node)->name, rootnode); - fprintf(stderr,"Continuing regardless...\n"); - } - - return 0; -} -- cgit v1.2.3