From be119d5922d9d7783032f2a4b34d0bcd2f630202 Mon Sep 17 00:00:00 2001 From: Justin Reichardt Date: Thu, 2 Sep 2021 15:17:49 -0500 Subject: [PATCH] Cleans Download Currently removes comments and empty lines from downloaded file. Later will verify contents, convert to ipv4 and ipv6, and remove duplicates --- src/download.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/src/download.c b/src/download.c index 002717c..05720b1 100644 --- a/src/download.c +++ b/src/download.c @@ -22,6 +22,9 @@ #include "download.h" #endif +int clean_download(); +int skip_to_next_line(FILE **fp); + int download_entries(struct entry **entries){ int i = (*entries)[0].entrytype; int rc = 0; @@ -47,7 +50,7 @@ int download_entries(struct entry **entries){ fflush(tmpf); if (rc == EOF){ printf("Failed to write to %s\n", \ - TMPDOWNLOADLOCATION); + TMPLOCATION); fclose(tmpdf); fclose(tmpf); return 1; @@ -85,24 +88,26 @@ int download_libcurl(char *e){ copy_old_download(e); return 1; } + clean_download(); curl_easy_cleanup(curl); } curl_global_cleanup(); + remove(TMPDOWNLOADLOCATION); return 0; } int parse_download(char *buff, size_t size, size_t nmemb){ - FILE *tmpf; + FILE *tmpdf; int i=0; int rc=0; - tmpf = fopen(TMPLOCATION, "a"); + tmpdf = fopen(TMPDOWNLOADLOCATION, "a"); for(i=0;i MAXSTRSIZE){ + printf("String too long when cleaning download: %s\n",buff); + return 1; + } + if (c!=EOF) // Later needs to check for NULL + strncat(buff,&c,1); + if (buffsize==2){ + if (buff[0]=='#' || buff[0]=='\n'){ + skip_to_next_line(&tmpdf); + buff[0] = '\0'; + buffsize=1; + } + } + if (c == '\n' || c == EOF){ + fputs(buff,tmpf); + buff[0]='\0'; + buffsize=1; + } + }while (c!=EOF); + fflush(tmpf); + fclose(tmpf); + fclose(tmpdf); + return 0; +} +int skip_to_next_line(FILE **fp){ + char c; + do{ + c = fgetc(*fp); + }while(c != '\n' && c != EOF); + return 0; +}