From 955f119a188e4338f1407af07eea9e4226128ca8 Mon Sep 17 00:00:00 2001 From: Matt Mascarenhas Date: Sat, 13 May 2017 16:38:10 +0100 Subject: [PATCH] hmml_to_html.c: The beginnings of config [#25] May in all likelihood back this stuff out --- hmml_to_html/hmml_to_html.c | 290 +++++++++++++++++++++++++++++++++++- 1 file changed, 285 insertions(+), 5 deletions(-) diff --git a/hmml_to_html/hmml_to_html.c b/hmml_to_html/hmml_to_html.c index 365ad4c..bfac585 100644 --- a/hmml_to_html/hmml_to_html.c +++ b/hmml_to_html/hmml_to_html.c @@ -28,13 +28,15 @@ typedef struct int Identifier; } identifier; +#define REF_MAX_IDENTIFIER 32 + typedef struct { char RefTitle[620]; char ID[512]; char URL[512]; char Source[256]; - identifier Identifier[12]; + identifier Identifier[REF_MAX_IDENTIFIER]; int IdentifierCount; } ref_info; @@ -456,7 +458,7 @@ GenerateTopicColours(buffer *Colour, char *Topic) if(!(TopicsBuffer = malloc(TopicsLength))) { - perror("hmml_to_html"); + perror("GenerateTopicColours"); return; } @@ -488,11 +490,277 @@ GenerateTopicColours(buffer *Colour, char *Topic) } else { - perror("hmml_to_html"); + perror("GenerateTopicColours"); return; } } +#define CONFIG 0 + +#if CONFIG +typedef struct +{ + char *Username; + char *Display_Name; + char *Homepage; + char *Funding_Platform; + char *Funding_Username; + unsigned int Index; +} credentials; + +typedef struct +{ + credentials Credentials; +} config; + +int +ParseConfig(buffer *Buffer, char *Username) +{ + /* + Essentially, I want to pass a Username to this, and have it write the credentials into the Config buffer + Let's start by just grabbing the stuff and printing it out + */ + + // TODO(matt): Actually figure out the "standard" config location + char Config_Location[255]; + if(getenv("XDG_CONFIG_HOME")) + { + sprintf(Config_Location, "%s/hmml.conf", getenv("XDG_CONFIG_HOME")); + } + else if(getenv("HOME")) + { + sprintf(Config_Location, "%s/.config/hmml.conf", getenv("HOME")); + } + else + { + fprintf(stderr, "Config file location not set"); + return 1; + } + + FILE *InFile; + if(!(InFile = fopen(Config_Location, "r"))) + { + perror(Config_Location); + return 2; + } + + printf("Reading: %s\n", Config_Location); + + fseek(InFile, 0, SEEK_END); + int InSize = ftell(InFile); + fseek(InFile, 0, SEEK_SET); + + char *InBuffer; + //config Config = { 0 }; + + if(!(InBuffer = malloc(InSize))) + { + perror("ParseConfig"); + return 3; + } + + fread(InBuffer, InSize, 1, InFile); + fclose(InFile); + + char *InPtr = InBuffer; + char OutBuffer[256]; + //char *OutPtr = Config.Credentials.Display_Name; + char *OutPtr = OutBuffer; + bool Quoted = FALSE; + bool FoundCredentials, ParsingUsername = FALSE; + unsigned int ScopeDepth = 0; + + while(InPtr - InBuffer < InSize) + { + switch(*InPtr) + { + case '#': + { + if(!Quoted) + { + printf(" We are commenting\n"); + while(InPtr - InBuffer < InSize && *InPtr != '\n') + { + ++InPtr; + } + ++InPtr; + while(InPtr - InBuffer < InSize && (*InPtr == ' ' || *InPtr == '\n')) + { + ++InPtr; + } + } + else + { + *OutPtr++ = *InPtr++; + } + break; + } + case '{': + { + if(!Quoted) + { + ++ScopeDepth; + ++InPtr; + while(*InPtr == '\n' || *InPtr == ' ') + { + ++InPtr; + } + printf(" We have entered a scope\n"); + } + else + { + *OutPtr++ = *InPtr++; + } + break; + } + case '}': + { + if(!Quoted) + { + --ScopeDepth; + ++InPtr; + printf(" We have left a scope\n"); + } + else + { + *OutPtr++ = *InPtr++; + } + break; + } +#if 1 + case ' ': + { + if(!Quoted) + { + ++InPtr; + *OutPtr = '\0'; + OutPtr = OutBuffer; + printf("%s\n", OutBuffer); + + // TODO(matt): Switch on the OutBuffer? I have a feeling that isn't actually possible, though + if(!StringsDiffer("credentials", OutBuffer)) + { + FoundCredentials = TRUE; + printf(" We have found the credentials block\n"); + } + if(ParsingUsername) + { + printf(" The username is %s\n", OutBuffer); + ParsingUsername = FALSE; + } + if(FoundCredentials && (!StringsDiffer("username", OutBuffer))) + { + ParsingUsername = TRUE; + printf(" We have found the username\n"); + } + } + else + { + *OutPtr++ = *InPtr++; + } + break; + } +#endif + case '"': + { + if(!Quoted) + { + Quoted = TRUE; + printf(" We are quoting!\n"); + } + else + { + Quoted = FALSE; + printf(" We are no longer quoting!\n"); + } + ++InPtr; + break; + } + case ';': + { + if(!Quoted) + { + printf(" We have reached the end of a setting\n"); + ++InPtr; + } + else + { + *OutPtr++ = *InPtr++; + } + } + case '\n': + { + if(!Quoted) + { + if(InPtr - InBuffer < InSize) + { + *OutPtr = '\0'; + OutPtr = OutBuffer; + // TODO(matt) + if(!StringsDiffer("credentials", OutBuffer)) + { + FoundCredentials = TRUE; + printf(" We have found the credentials block\n"); + } + if(ParsingUsername) + { + printf(" The username is %s\n", OutBuffer); + ParsingUsername = FALSE; + } + if(FoundCredentials && (!StringsDiffer("username", OutBuffer))) + { + ParsingUsername = TRUE; + printf(" We have found the username\n"); + } + printf("%s\n", OutBuffer); + ++InPtr; + while(InPtr - InBuffer < InSize && *InPtr == ' ') // NOTE(matt): Skip indentation whitespace + { + ++InPtr; + } + } + } + else + { + *OutPtr++ = *InPtr++; + } + + if(InPtr - InBuffer == InSize) + { + printf(" We have reached the EOF\n"); + } + break; + } + default: + { + *OutPtr++ = *InPtr++; + break; + } + } + } + + +#if 0 + while(InPtr - InBuffer < InSize) + { + while(*InPtr != '\n') + { + *OutPtr++ = *InPtr++; + } + *OutPtr = '\0'; + printf("%s\n", Config.Credentials.Display_Name); + } +#endif + + free(InBuffer); + + // Reading from the config file, parsing it inline (on the stack) and writing into the buffer *Config + + return 0; +} + +#endif + int main(int ArgC, char **Args) { @@ -515,6 +783,10 @@ main(int ArgC, char **Args) // NOTE(matt): Setup buffers and ptrs char *InPtr; +#if CONFIG + buffer Config; +#endif + buffer Title; buffer QuoteMenu; buffer ReferenceMenu; @@ -543,6 +815,10 @@ main(int ArgC, char **Args) return 1; } +#if CONFIG + ClaimBuffer(MemoryArena, &ClaimedMemory, &Config, 1024); +#endif + HMML_Output HMML = hmml_parse_file(InFile); fclose(InFile); @@ -648,7 +924,7 @@ Readable); ClaimBuffer(MemoryArena, &ClaimedMemory, &ReferenceMenu, 1024 * 16); CopyStringToBuffer(&ReferenceMenu, -"
\n" +"
\n" " References ▼\n" "
\n" "
\n"); @@ -767,7 +1043,7 @@ AppendedIdentifier: { ClaimBuffer(MemoryArena, &ClaimedMemory, &QuoteMenu, 1024 * 16); CopyStringToBuffer(&QuoteMenu, -"
\n" +"
\n" " Quotes ▼\n" "
\n" "
\n"); @@ -953,6 +1229,10 @@ ReferencesArray[i].Identifier[j].Timecode); ClaimedMemory -= ReferenceMenu.Size; } +#if CONFIG + // TODO(matt): Here is where I test ParseConfig + ParseConfig(&Config, HMML.metadata.annotator); +#endif CopyStringToBuffer(&Title, " Annotator: %s\n" "
\n",