#if 0 ctime -begin ${0%.*}.ctm gcc -g -Wall -fsanitize=address -std=c99 -pipe $0 -o ${0%.*} hmml.a ctime -end ${0%.*}.ctm exit #endif #define DEBUG 0 typedef unsigned int bool; #define TRUE 1 #define FALSE 0 #include // NOTE(matt): varargs #include // NOTE(matt): printf, sprintf, vsprintf, fprintf, perror #include // NOTE(matt): calloc, malloc, free #include "hmmlib.h" #include // NOTE(matt): getopts //#include "config.h" // TODO(matt): Implement config.h #define Kilobytes(Bytes) Bytes << 10 #define Megabytes(Bytes) Bytes << 20 enum { EDITION_SINGLE = 0, EDITION_PROJECT = 1, EDITION_NETWORK = 2 } EDITION; typedef struct { char *Location; char *Ptr; char *ID; int Size; } buffer; // TODO(matt): Consider putting the ref_info and quote_info into linked lists on the heap, just to avoid all the hardcoded sizes typedef struct { char Date[32]; char Text[512]; } quote_info; typedef struct { char Timecode[8]; int Identifier; } identifier; #define REF_MAX_IDENTIFIER 64 typedef struct { char RefTitle[620]; char ID[512]; char URL[512]; char Source[256]; identifier Identifier[REF_MAX_IDENTIFIER]; int IdentifierCount; } ref_info; typedef struct { char Marker[32]; char WrittenText[32]; } category_info; // TODO(matt): Parse this stuff out of a config file char *Credentials[ ][5] = { { "Miblo", "Matt Mascarenhas", "http://miblodelcarpio.co.uk", "cinera_icon_patreon.png", "https://patreon.com/miblo"}, { "miotatsu", "Mio Iwakura", "http://riscy.tv/", "cinera_icon_patreon.png", "https://patreon.com/miotatsu"}, { "nothings", "Sean Barrett", "https://nothings.org/", "", ""}, { "cmuratori", "Casey Muratori", "https://handmadehero.org", "cinera_icon_patreon.png", "https://patreon.com/cmuratori"}, { "fierydrake", "Mike Tunnicliffe", "", "", ""}, { "abnercoimbre", "Abner Coimbre", "https://handmade.network/m/abnercoimbre", "cinera_icon_patreon.png", "https://patreon.com/handmade_dev"}, }; #define EDITION EDITION_SINGLE #define ArrayCount(A) sizeof(A)/sizeof(*(A)) #define ClaimBuffer(MemoryArena, ClaimedMemory, Buffer, ID, Size) if(__ClaimBuffer(MemoryArena, ClaimedMemory, Buffer, ID, Size))\ {\ fprintf(stderr, "%s:%d: MemoryArena cannot contain %s of size %d\n", __FILE__, __LINE__, ID, Size);\ hmml_free(&HMML);\ FreeBuffer(MemoryArena);\ return 1;\ }; void FreeBuffer(buffer *Buffer) { free(Buffer->Location); } int __ClaimBuffer(buffer *MemoryArena, int *ClaimedMemory, buffer *Buffer, char *ID, int Size) { if(*ClaimedMemory + Size > MemoryArena->Size) { return 1; } Buffer->Location = MemoryArena->Location + *ClaimedMemory; Buffer->Size = Size; Buffer->ID = ID; *ClaimedMemory += Buffer->Size; *Buffer->Location = '\0'; Buffer->Ptr = Buffer->Location; #if DEBUG printf(" Claimed: %s: %d\n" " Total ClaimedMemory: %d\n\n", Buffer->ID, Buffer->Size, *ClaimedMemory); #endif return 0; } void DeclaimBuffer(buffer *Buffer, int *ClaimedMemory) { *Buffer->Location = '\0'; *ClaimedMemory -= Buffer->Size; #if DEBUG printf("Declaimed: %s\n" " Total ClaimedMemory: %d\n\n", Buffer->ID, *ClaimedMemory); #endif } int TimecodeToSeconds(char *Timecode) { int HMS[3] = { 0, 0, 0 }; // 0 == Seconds; 1 == Minutes; 2 == Hours int Colons = 0; while(*Timecode) { //if((*Timecode < '0' || *Timecode > '9') && *Timecode != ':') { return FALSE; } if(*Timecode == ':') { ++Colons; //if(Colons > 2) { return FALSE; } for(int i = 0; i < Colons; ++i) { HMS[Colons - i] = HMS[Colons - (i + 1)]; } HMS[0] = 0; } else { HMS[0] = HMS[0] * 10 + *Timecode - '0'; } ++Timecode; } //if(HMS[0] > 59 || HMS[1] > 59 || Timecode[-1] == ':') { return FALSE; } return HMS[2] * 60 * 60 + HMS[1] * 60 + HMS[0]; } void CopyBuffer(buffer *Dest, buffer *Src) { Src->Ptr = Src->Location; while(*Src->Ptr) { // TODO(matt) { if(Dest->Ptr - Dest->Location >= Dest->Size) { fprintf(stderr, "CopyBuffer: %s cannot accommodate %s\n", Dest->ID, Src->ID); __asm__("int3"); } } *Dest->Ptr++ = *Src->Ptr++; } } __attribute__ ((format (printf, 2, 3))) void CopyString(char Dest[], char *Format, ...) { va_list Args; va_start(Args, Format); vsprintf(Dest, Format, Args); va_end(Args); } int CopyStringNoFormat(char *Dest, char *String) { int Length = 0; while(*String) { *Dest++ = *String++; ++Length; } *Dest = '\0'; return Length; } int StringLength(char *String) { int i = 0; while(String[i]) { ++i; } return i; } __attribute__ ((format (printf, 2, 3))) void CopyStringToBuffer(buffer *Dest, char *Format, ...) { va_list Args; va_start(Args, Format); int Length = vsnprintf(Dest->Ptr, Dest->Size - (Dest->Ptr - Dest->Location), Format, Args); va_end(Args); // TODO(matt): { if(Length + (Dest->Ptr - Dest->Location) >= Dest->Size) { fprintf(stderr, "CopyStringToBuffer: %s cannot accommodate %d-character string:\n" "\n" "%s\n", Dest->ID, Length, Format); __asm__("int3"); } } Dest->Ptr += Length; } void CopyStringToBufferHTMLSafe(buffer *Dest, char *String) { while(*String) { if(Dest->Ptr - Dest->Location >= Dest->Size) { fprintf(stderr, "CopyStringToBufferHTMLSafe: %s cannot accommodate %d-character string\n", Dest->ID, StringLength(String)); __asm__("int3"); } switch(*String) { case '<': CopyStringToBuffer(Dest, "<"); String++; break; case '>': CopyStringToBuffer(Dest, ">"); String++; break; case '&': CopyStringToBuffer(Dest, "&"); String++; break; case '\"': CopyStringToBuffer(Dest, """); String++; break; case '\'': CopyStringToBuffer(Dest, "'"); String++; break; default: *Dest->Ptr++ = *String++; break; } } } void CopyStringToBufferCSVSafe(buffer *Dest, char *String) { while(*String) { if(Dest->Ptr - Dest->Location >= Dest->Size) { fprintf(stderr, "CopyStringToBufferHTMLSafe: %s cannot accommodate %d-character string\n", Dest->ID, StringLength(String)); __asm__("int3"); } switch(*String) { case '<': CopyStringToBuffer(Dest, "<"); String++; break; case '>': CopyStringToBuffer(Dest, ">"); String++; break; case '&': CopyStringToBuffer(Dest, "&"); String++; break; case '\"': CopyStringToBuffer(Dest, """); String += 2; break; case '\'': CopyStringToBuffer(Dest, "'"); String++; break; default: *Dest->Ptr++ = *String++; break; } } } int StringsDiffer(char *A, char *B) // NOTE(matt): Two null-terminated strings { while(*A && *B && *A == *B) { ++A, ++B; } return *A - *B; } bool StringsDifferT(char *A, // NOTE(matt): Null-terminated string char *B, // NOTE(matt): Not null-terminated string (e.g. one mid-buffer) char Terminator // NOTE(matt): Caller definable terminator. Pass 0 to only match on the extent of A ) { int ALength = StringLength(A); int i = 0; while(i < ALength && A[i] && A[i] == B[i]) { ++i; } if((!Terminator && !A[i] && ALength == i) || (!A[i] && ALength == i && (B[i] == Terminator))) { return FALSE; } else { return TRUE; } } typedef struct { unsigned int Hue:16; unsigned int Saturation:8; unsigned int Lightness:8; } hsl_colour; hsl_colour CharToColour(char Char) { hsl_colour Colour; if(Char >= 'a' && Char <= 'z') { Colour.Hue = (((float)Char - 'a') / ('z' - 'a') * 360); Colour.Saturation = (((float)Char - 'a') / ('z' - 'a') * 26 + 74); } else if(Char >= 'A' && Char <= 'Z') { Colour.Hue = (((float)Char - 'A') / ('Z' - 'A') * 360); Colour.Saturation = (((float)Char - 'A') / ('Z' - 'A') * 26 + 74); } else if(Char >= '0' && Char <= '9') { Colour.Hue = (((float)Char - '0') / ('9' - '0') * 360); Colour.Saturation = (((float)Char - '0') / ('9' - '0') * 26 + 74); } else { Colour.Hue = 180; Colour.Saturation = 50; } return Colour; } hsl_colour * StringToColourHash(hsl_colour *Colour, char *String) { Colour->Hue = 0; Colour->Saturation = 0; Colour->Lightness = 26; int i; for(i = 0; String[i]; ++i) { Colour->Hue += CharToColour(String[i]).Hue; Colour->Saturation += CharToColour(String[i]).Saturation; } Colour->Hue = Colour->Hue % 360; Colour->Saturation = Colour->Saturation % 26 + 74; return(Colour); } char * SanitisePunctuation(char *String) { char *Ptr = String; while(*Ptr) { if(*Ptr == ' ') { *Ptr = '_'; } if((*Ptr < '0' || *Ptr > '9') && (*Ptr < 'a' || *Ptr > 'z') && (*Ptr < 'A' || *Ptr > 'Z')) { *Ptr = '-'; } ++Ptr; } return String; } int BuildCredits(buffer *CreditsMenu, buffer *HostInfo, buffer *AnnotatorInfo, bool *HasCreditsMenu, char *ImagesDir, char *Host, char *Annotator) { // TODO(matt): Handle co-hosts and guests bool FoundHost = FALSE; bool FoundAnnotator = FALSE; for(int CredentialIndex = 0; CredentialIndex < ArrayCount(Credentials); ++CredentialIndex) { if(!StringsDiffer(Host, Credentials[CredentialIndex][0])) { FoundHost = TRUE; // TODO(matt): Check if this is actually necessary... CopyStringToBuffer(HostInfo, " \n"); if(*Credentials[CredentialIndex][2]) { CopyStringToBuffer(HostInfo, " \n" "
Host
\n" "
%s
\n" "
\n", Credentials[CredentialIndex][2], Credentials[CredentialIndex][1]); } else { CopyStringToBuffer(HostInfo, "
\n" "
Host
\n" "
%s
\n" "
\n", Credentials[CredentialIndex][1]); } if(*Credentials[CredentialIndex][4] && *Credentials[CredentialIndex][3]) { CopyStringToBuffer(HostInfo, " \n", Credentials[CredentialIndex][4], ImagesDir, Credentials[CredentialIndex][3]); } CopyStringToBuffer(HostInfo, "
\n"); } if(!StringsDiffer(Annotator, Credentials[CredentialIndex][0])) { FoundAnnotator = TRUE; // TODO(matt): Check if this is actually necessary... CopyStringToBuffer(AnnotatorInfo, " \n"); if(*Credentials[CredentialIndex][2]) { CopyStringToBuffer(AnnotatorInfo, " \n" "
Annotator
\n" "
%s
\n" "
\n", Credentials[CredentialIndex][2], Credentials[CredentialIndex][1]); } else { CopyStringToBuffer(AnnotatorInfo, "
\n" "
Annotator
\n" "
%s
\n" "
\n", Credentials[CredentialIndex][1]); } if(*Credentials[CredentialIndex][4] && *Credentials[CredentialIndex][3]) { CopyStringToBuffer(AnnotatorInfo, " \n", Credentials[CredentialIndex][4], ImagesDir, Credentials[CredentialIndex][3]); } CopyStringToBuffer(AnnotatorInfo, "
\n"); } } if(FoundHost || FoundAnnotator) { CopyStringToBuffer(CreditsMenu, "
\n" "
\n" " Credits\n" "
\n"); if(FoundHost) { CopyBuffer(CreditsMenu, HostInfo); } if(FoundAnnotator) { CopyBuffer(CreditsMenu, AnnotatorInfo); } CopyStringToBuffer(CreditsMenu, "
\n" "
\n"); } else { return 1; } *HasCreditsMenu = TRUE; return 0; } int BuildReference(ref_info *ReferencesArray, int RefIdentifier, int UniqueRefs, HMML_Reference Ref, HMML_Annotation Anno) { #define REF_SITE (1 << 0) #define REF_PAGE (1 << 1) #define REF_URL (1 << 2) #define REF_TITLE (1 << 3) #define REF_ARTICLE (1 << 4) #define REF_AUTHOR (1 << 5) #define REF_EDITOR (1 << 6) #define REF_PUBLISHER (1 << 7) #define REF_ISBN (1 << 8) int Mask = 0; if(Ref.site) { Mask |= REF_SITE; } if(Ref.page) { Mask |= REF_PAGE; } if(Ref.url) { Mask |= REF_URL; } if(Ref.title) { Mask |= REF_TITLE; } if(Ref.article) { Mask |= REF_ARTICLE; } if(Ref.author) { Mask |= REF_AUTHOR; } if(Ref.editor) { Mask |= REF_EDITOR; } if(Ref.publisher) { Mask |= REF_PUBLISHER; } if(Ref.isbn) { Mask |= REF_ISBN; } /* * NOTE(matt) * * Mask * Loop over the reference attributes, adding them to a mask if they are set * Then my cases will just be that mask tested against the attributes OR'd with each other * */ if((REF_URL | REF_TITLE | REF_AUTHOR | REF_PUBLISHER | REF_ISBN) == Mask) { CopyString(ReferencesArray[UniqueRefs].ID, Ref.isbn); CopyString(ReferencesArray[UniqueRefs].Source, "%s (%s)", Ref.author, Ref.publisher); CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, Ref.title); CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, Ref.url); } else if((REF_AUTHOR | REF_SITE | REF_PAGE | REF_URL) == Mask) { CopyStringNoFormat(ReferencesArray[UniqueRefs].ID, Ref.url); CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, Ref.site); CopyString(ReferencesArray[UniqueRefs].RefTitle, "%s: \"%s\"", Ref.author, Ref.page); CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, Ref.url); } else if((REF_PAGE | REF_URL | REF_TITLE) == Mask) { CopyStringNoFormat(ReferencesArray[UniqueRefs].ID, Ref.url); CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, Ref.title); CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, Ref.page); CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, Ref.url); } else if((REF_SITE | REF_PAGE | REF_URL) == Mask) { CopyStringNoFormat(ReferencesArray[UniqueRefs].ID, Ref.url); CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, Ref.site); CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, Ref.page); CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, Ref.url); } else if((REF_SITE | REF_URL | REF_TITLE) == Mask) { CopyStringNoFormat(ReferencesArray[UniqueRefs].ID, Ref.url); CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, Ref.site); CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, Ref.title); CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, Ref.url); } else if((REF_TITLE | REF_AUTHOR | REF_ISBN) == Mask) { CopyString(ReferencesArray[UniqueRefs].ID, Ref.isbn); CopyString(ReferencesArray[UniqueRefs].Source, Ref.author); CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, Ref.title); //TODO(matt): Look into finding the best ISBN searcher the web has to offer CopyString(ReferencesArray[UniqueRefs].URL, "http://www.openisbn.com/isbn/%s", Ref.isbn); } else if((REF_URL | REF_ARTICLE | REF_AUTHOR) == Mask) { CopyStringNoFormat(ReferencesArray[UniqueRefs].ID, Ref.url); CopyString(ReferencesArray[UniqueRefs].Source, Ref.author); CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, Ref.article); CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, Ref.url); } else if((REF_URL | REF_TITLE | REF_AUTHOR) == Mask) { CopyStringNoFormat(ReferencesArray[UniqueRefs].ID, Ref.url); CopyString(ReferencesArray[UniqueRefs].Source, Ref.author); CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, Ref.title); CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, Ref.url); } else if((REF_URL | REF_TITLE) == Mask) { CopyStringNoFormat(ReferencesArray[UniqueRefs].ID, Ref.url); CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, Ref.title); CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, Ref.url); } else if((REF_SITE | REF_URL) == Mask) { CopyStringNoFormat(ReferencesArray[UniqueRefs].ID, Ref.url); CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, Ref.site); CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, Ref.url); } else { return 1; } CopyString(ReferencesArray[UniqueRefs].Identifier[ReferencesArray[UniqueRefs].IdentifierCount].Timecode, Anno.time); ReferencesArray[UniqueRefs].Identifier[ReferencesArray[UniqueRefs].IdentifierCount].Identifier = RefIdentifier; return 0; } char *CategoryMedium[][3] = { // medium icon written name { "afk", "…" , "Away from Keyboard"}, // TODO(matt): Filter this out by default { "authored", "🗪", "Chat Comment"}, // TODO(matt): Conditionally handle Chat vs Guest Comments { "blackboard", "🖌", "Blackboard"}, { "default", "🖮", "Programming"}, // TODO(matt): Potentially make this configurable per project { "experience", "🍷", "Experience"}, { "owl", "🦉", "Owl of Shame"}, { "rant", "💢", "Rant"}, { "research", "📖", "Research"}, { "run", "🏃", "In-Game"}, // TODO(matt): Potentially make this configurable per project { "trivia", "🎲", "Trivia"}, }; void BuildFilter(category_info *TopicsArray, int *UniqueTopics, category_info *MediaArray, int *UniqueMedia, char *Marker) { bool IsMedium = FALSE; int i = 0; for(i = 0; i < ArrayCount(CategoryMedium); ++i) { if(!StringsDiffer(CategoryMedium[i][0], Marker)) { IsMedium = TRUE; break; } } int Offset; if(IsMedium) { int j = 0; for(j = 0; j < *UniqueMedia; ++j) { if(!StringsDiffer(CategoryMedium[i][0], MediaArray[j].Marker)) { return; } if((Offset = StringsDiffer(CategoryMedium[i][2], MediaArray[j].WrittenText)) < 0) { int k; for(k = *UniqueMedia; k > j; --k) { CopyString(MediaArray[k].Marker, MediaArray[k-1].Marker); CopyString(MediaArray[k].WrittenText, MediaArray[k-1].WrittenText); } CopyString(MediaArray[k].Marker, CategoryMedium[i][0]); CopyString(MediaArray[k].WrittenText, CategoryMedium[i][2]); break; } } if(j == *UniqueMedia) { CopyString(MediaArray[j].Marker, CategoryMedium[i][0]); CopyString(MediaArray[j].WrittenText, CategoryMedium[i][2]); } ++*UniqueMedia; return; } else { int i = 0; for(i = 0; i < *UniqueTopics; ++i) { if(!StringsDiffer(Marker, TopicsArray[i].Marker)) { return; } if((Offset = StringsDiffer(Marker, TopicsArray[i].Marker)) < 0) { int j; for(j = *UniqueTopics; j > i; --j) { CopyString(TopicsArray[j].Marker, TopicsArray[j-1].Marker); } CopyString(TopicsArray[j].Marker, Marker); break; } } if(i == *UniqueTopics) { CopyString(TopicsArray[i].Marker, Marker); } ++*UniqueTopics; return; } } void BuildCategories(buffer *AnnotationClass, buffer *Category, int *MarkerIndex, bool *HasCategory, bool *HasMedium, char *Marker) { // NOTE(matt): This guy could also sort, so that the dots appear in a consistent order in the annotations // If so, the Category buffer would have to only contain the category names and no more until collation time // BuildCategories() would have to parse the Category.Location out to an array, sort that array and write it back in // The code in the "annotation loop" would then have to write both the head and tail of the category stuff for(int i = 0; i < ArrayCount(CategoryMedium); ++i) { if(!StringsDiffer(CategoryMedium[i][0], Marker)) { CopyStringToBuffer(AnnotationClass, " %s", SanitisePunctuation(Marker)); *HasMedium = TRUE; ++*MarkerIndex; return; } } if(*HasCategory == FALSE) { CopyStringToBuffer(Category, ""); *HasCategory = TRUE; } // NOTE(matt): Iterate through the Category->Location looking for "Marker", and bail if we find it char *Ptr = Category->Location; bool Found = FALSE; while(*Ptr) { if(*Ptr == '\"') { ++Ptr; if(!StringsDifferT(SanitisePunctuation(Marker), Ptr, ' ')) { Found = TRUE; break; } else { while(*Ptr != '\"') { ++Ptr; } } } ++Ptr; } if(Found == FALSE) { CopyStringToBuffer(Category, "
", SanitisePunctuation(Marker), SanitisePunctuation(Marker)); CopyStringToBuffer(AnnotationClass, " cat_%s", SanitisePunctuation(Marker)); } ++*MarkerIndex; return; } int StringToInt(char *String) { int Result = 0; while(*String) { Result = Result * 10 + (*String - '0'); ++String; } return Result; } int BuildQuote(quote_info *Info, char *Speaker, int ID, char *QuoteDir) { char Path[255] = {0}; sprintf(Path, "%s/#%s", QuoteDir, Speaker); FILE *File; if(!(File = fopen(Path, "r"))) { perror(Path); return 1; } fseek(File, 0, SEEK_END); int Length = ftell(File); fseek(File, 0, SEEK_SET); char *Buffer; if(!(Buffer = malloc(Length))) { perror("BuildQuote"); } fread(Buffer, Length, 1, File); fclose(File); // TODO(matt): Search the quote store in reverse char *InPtr = Buffer; while(InPtr - Buffer < Length) { char InID[4] = { 0 }; char *OutPtr = InID; while(*InPtr != ',') { *OutPtr++ = *InPtr++; } *OutPtr = '\0'; if(StringToInt(InID) == ID) { InPtr += 2; OutPtr = Info->Date; while(*InPtr != '"') { *OutPtr++ = *InPtr++; } *OutPtr = '\0'; InPtr += 3; OutPtr = Info->Text; while(*InPtr != '\n') { if(*InPtr == '\\') { ++InPtr; } *OutPtr++ = *InPtr++; } *--OutPtr = '\0'; free(Buffer); return 0; } else { while(*InPtr != '\n') { ++InPtr; } ++InPtr; } } free(Buffer); return 1; } void GenerateTopicColours(buffer *Colour, char *Topic, char *TopicsDir) { for(int i = 0; i < ArrayCount(CategoryMedium); ++i) { if(!StringsDiffer(Topic, CategoryMedium[i][0])) { return; } } FILE *TopicsFile; char *TopicsBuffer; // TODO(matt): Consider (optionally) pulling this path from the config char TopicsPath[255]; CopyString(TopicsPath, "%s/cinera_topcs.css", TopicsDir); if((TopicsFile = fopen("cinera_topics.css", "a+"))) { fseek(TopicsFile, 0, SEEK_END); int TopicsLength = ftell(TopicsFile); fseek(TopicsFile, 0, SEEK_SET); // TODO(matt): May this not just ClaimBuffer (if I can figure out how)? if(!(TopicsBuffer = malloc(TopicsLength))) { perror("GenerateTopicColours"); return; } fread(TopicsBuffer, TopicsLength, 1, TopicsFile); char *TopicsPtr = TopicsBuffer; while(TopicsPtr - TopicsBuffer < TopicsLength) { TopicsPtr += StringLength(".category."); if(!StringsDifferT(SanitisePunctuation(Topic), TopicsPtr, ' ')) { free(TopicsBuffer); fclose(TopicsFile); return; } while(TopicsPtr - TopicsBuffer < TopicsLength && *TopicsPtr != '\n') { ++TopicsPtr; } ++TopicsPtr; } hsl_colour Colour; StringToColourHash(&Colour, Topic); fprintf(TopicsFile, ".category.%s { border: 1px solid hsl(%d, %d%%, %d%%); background: hsl(%d, %d%%, %d%%); }\n", SanitisePunctuation(Topic), Colour.Hue, Colour.Saturation, Colour.Lightness, Colour.Hue, Colour.Saturation, Colour.Lightness); fclose(TopicsFile); free(TopicsBuffer); } else { 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 void PrintUsage(char *BinaryLocation, char *DefaultCSSDir, char *DefaultImagesDir, char *DefaultJSDir, char *DefaultOutLocation, char *DefaultQuoteDir, char *DefaultTemplateLocation) { fprintf(stderr, "Usage: %s [option(s)] filename(s)\n" "\n" "Options:\n" " -c \n" " Override default CSS directory (\"%s\")\n" " -i \n" " Override default images directory (\"%s\")\n" " -j \n" " Override default JS directory (\"%s\")\n" " -o \n" " Override default output location (\"%s\")\n" " -q \n" " Override default quotes directory (\"%s\")\n" " -t