From 5f21a9544d3f7eb5870443c9719d69a9d11bfb2c Mon Sep 17 00:00:00 2001 From: Matt Mascarenhas Date: Wed, 9 Aug 2017 01:57:09 +0100 Subject: [PATCH] hmml_to_html.c: Pull from insobot's quote store #25 Also fix the forced integration --- hmml_to_html/hmml_to_html.c | 167 ++++++++++++++++++++---------------- 1 file changed, 95 insertions(+), 72 deletions(-) diff --git a/hmml_to_html/hmml_to_html.c b/hmml_to_html/hmml_to_html.c index 8187049..3243da1 100644 --- a/hmml_to_html/hmml_to_html.c +++ b/hmml_to_html/hmml_to_html.c @@ -1,6 +1,6 @@ #if 0 ctime -begin ${0%.*}.ctm -gcc -g -no-pie -fsanitize=address -Wall -std=c99 -pipe $0 -o ${0%.*} hmml.a +gcc -g -no-pie -fsanitize=address -Wall -std=c99 -pipe $0 -o ${0%.*} hmml.a -lcurl ctime -end ${0%.*}.ctm exit #endif @@ -18,6 +18,7 @@ typedef unsigned int bool; #include "hmmlib.h" #include // NOTE(matt): getopts //#include "config.h" // TODO(matt): Implement config.h +#include #define Kilobytes(Bytes) Bytes << 10 #define Megabytes(Bytes) Bytes << 20 @@ -809,78 +810,98 @@ StringToInt(char *String) return Result; } -int -BuildQuote(quote_info *Info, char *Speaker, int ID, char *QuoteDir) +size_t +CurlIntoBuffer(char *InPtr, size_t CharLength, size_t Chars, char **OutputPtr) { - char Path[255] = {0}; - sprintf(Path, "%s/#%s", QuoteDir, Speaker); - FILE *File; - if(!(File = fopen(Path, "r"))) + int Length = CharLength * Chars; + int i; + for(i = 0; InPtr[i] && i < Length; ++i) { - perror(Path); - return 1; + *((*OutputPtr)++) = InPtr[i]; } + **OutputPtr = '\0'; + return Length; +}; - fseek(File, 0, SEEK_END); - int Length = ftell(File); - fseek(File, 0, SEEK_SET); - char *Buffer; - if(!(Buffer = malloc(Length))) +#include + +int +BuildQuote(quote_info *Info, char *Speaker, int ID) +{ + char QuotesURL[256]; + CopyString(QuotesURL, "https://dev.abaines.me.uk/quotes/%s.raw", Speaker); + + buffer QuoteStaging; + QuoteStaging.ID = "QuoteStaging"; + QuoteStaging.Size = Kilobytes(256); + if(!(QuoteStaging.Location = malloc(QuoteStaging.Size))) { - perror("BuildQuote"); + perror("malloc"); + } + QuoteStaging.Ptr = QuoteStaging.Location; + + CURL *curl = curl_easy_init(); + if(curl) { + CURLcode res; + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &QuoteStaging.Ptr); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlIntoBuffer); + curl_easy_setopt(curl, CURLOPT_URL, QuotesURL); + if((res = curl_easy_perform(curl))) + { + fprintf(stderr, "%s", curl_easy_strerror(res)); + } + curl_easy_cleanup(curl); } - fread(Buffer, Length, 1, File); - fclose(File); // TODO(matt): Search the quote store in reverse - char *InPtr = Buffer; + QuoteStaging.Ptr = QuoteStaging.Location; - while(InPtr - Buffer < Length) + while(*QuoteStaging.Ptr) { char InID[4] = { 0 }; + char InTime[16] = { 0 }; char *OutPtr = InID; - while(*InPtr != ',') + while(*QuoteStaging.Ptr != ',') { - *OutPtr++ = *InPtr++; + *OutPtr++ = *QuoteStaging.Ptr++; } *OutPtr = '\0'; if(StringToInt(InID) == ID) { - InPtr += 2; - OutPtr = Info->Date; - while(*InPtr != '"') + QuoteStaging.Ptr += 1; + OutPtr = InTime; + while(*QuoteStaging.Ptr != ',') { - *OutPtr++ = *InPtr++; + *OutPtr++ = *QuoteStaging.Ptr++; } *OutPtr = '\0'; - InPtr += 3; + long int Time = StringToInt(InTime); + strftime(Info->Date, 32, "%d %B, %Y", gmtime(&Time)); + + QuoteStaging.Ptr += 1; OutPtr = Info->Text; - while(*InPtr != '\n') + while(*QuoteStaging.Ptr != '\n') { - if(*InPtr == '\\') - { - ++InPtr; - } - *OutPtr++ = *InPtr++; + *OutPtr++ = *QuoteStaging.Ptr++; } *--OutPtr = '\0'; - free(Buffer); + FreeBuffer(&QuoteStaging); return 0; } else { - while(*InPtr != '\n') + while(*QuoteStaging.Ptr != '\n') { - ++InPtr; + ++QuoteStaging.Ptr; } - ++InPtr; + ++QuoteStaging.Ptr; } } - free(Buffer); + FreeBuffer(&QuoteStaging); return 1; } @@ -1215,7 +1236,7 @@ ParseConfig(buffer *Buffer, char *Username) #endif void -PrintUsage(char *BinaryLocation, char *DefaultCSSDir, char *DefaultImagesDir, char *DefaultJSDir, char *DefaultDefaultMedium, char *DefaultOutLocation, char *DefaultQuoteDir, char *DefaultTemplateLocation) +PrintUsage(char *BinaryLocation, char *DefaultCSSDir, char *DefaultImagesDir, char *DefaultJSDir, char *DefaultDefaultMedium, char *DefaultOutLocation, char *DefaultTemplateLocation) { fprintf(stderr, "Usage: %s [option(s)] filename(s)\n" "\n" @@ -1232,8 +1253,6 @@ PrintUsage(char *BinaryLocation, char *DefaultCSSDir, char *DefaultImagesDir, ch " Override default default medium (\"%s\")\n" " -o \n" " Override default output location (\"%s\")\n" - " -q \n" - " Override default quotes directory (\"%s\")\n" " -t