hmml_to_html.c: Clean up buffer claiming

This commit is contained in:
Matt Mascarenhas 2017-05-22 16:33:53 +01:00
parent 3c32b5904b
commit 4ec635831a
1 changed files with 84 additions and 33 deletions

View File

@ -158,7 +158,6 @@ CopyStringToBuffer(buffer *Dest, char *Format, ...)
{ {
printf("Too big! Too big!\n"); printf("Too big! Too big!\n");
__asm__("int3"); __asm__("int3");
} }
} }
va_list Args; va_list Args;
@ -171,7 +170,6 @@ CopyStringToBuffer(buffer *Dest, char *Format, ...)
{ {
printf("Too big! Too big!\n"); printf("Too big! Too big!\n");
__asm__("int3"); __asm__("int3");
} }
} }
Dest->Ptr += Length; Dest->Ptr += Length;
@ -549,6 +547,7 @@ GenerateTopicColours(buffer *Colour, char *Topic)
int TopicsLength = ftell(TopicsFile); int TopicsLength = ftell(TopicsFile);
fseek(TopicsFile, 0, SEEK_SET); fseek(TopicsFile, 0, SEEK_SET);
// TODO(matt): May this not just ClaimBuffer?
if(!(TopicsBuffer = malloc(TopicsLength))) if(!(TopicsBuffer = malloc(TopicsLength)))
{ {
perror("GenerateTopicColours"); perror("GenerateTopicColours");
@ -880,6 +879,26 @@ main(int ArgC, char **Args)
buffer Config; buffer Config;
#endif #endif
// NOTE(matt): Tree structure of buffer dependencies
// Master
// Title
// QuoteMenu
// ReferenceMenu
// FilterMenu
// FilterTopics
// FilterMedia
// Player
// Colour
// Annotation
// AnnotationHeader
// AnnotationClass
// AnnotationData
// Text
// Category
// FilterState
buffer Master;
buffer Title; buffer Title;
buffer QuoteMenu; buffer QuoteMenu;
buffer ReferenceMenu; buffer ReferenceMenu;
@ -888,23 +907,18 @@ main(int ArgC, char **Args)
buffer FilterMedia; buffer FilterMedia;
buffer Player; buffer Player;
buffer Colour;
buffer Annotation; buffer Annotation;
buffer AnnotationHeader; buffer AnnotationHeader;
buffer AnnotationClass; buffer AnnotationClass;
buffer AnnotationData; buffer AnnotationData;
buffer Text; buffer Text;
buffer Category; buffer Category;
buffer Colour;
buffer FilterState; buffer FilterState;
buffer Master;
for(int FileIndex = 1; FileIndex < ArgC; ++FileIndex) for(int FileIndex = 1; FileIndex < ArgC; ++FileIndex)
{ {
// TODO(matt): Maybe look into this further. It works, but there may be
// bugs lurking
ClaimedMemory = 0;
FILE *InFile; FILE *InFile;
if(!(InFile = fopen(Args[FileIndex], "r"))) if(!(InFile = fopen(Args[FileIndex], "r")))
{ {
@ -922,16 +936,33 @@ main(int ArgC, char **Args)
if(HMML.well_formed) if(HMML.well_formed)
{ {
// NOTE(matt): Tree structure of "global" buffer dependencies
// Master
// Title
// QuoteMenu
// ReferenceMenu
// FilterMenu
// FilterTopics
// FilterMedia
// Player
// Colour
// Annotation
// FilterState
ClaimBuffer(MemoryArena, &ClaimedMemory, &Master, "Master", 1024 * 512); ClaimBuffer(MemoryArena, &ClaimedMemory, &Master, "Master", 1024 * 512);
ClaimBuffer(MemoryArena, &ClaimedMemory, &Player, "Player", 1024 * 256);
ClaimBuffer(MemoryArena, &ClaimedMemory, &Title, "Title", 1024 * 16); ClaimBuffer(MemoryArena, &ClaimedMemory, &Title, "Title", 1024 * 16);
ClaimBuffer(MemoryArena, &ClaimedMemory, &QuoteMenu, "QuoteMenu", 1024 * 16);
ClaimBuffer(MemoryArena, &ClaimedMemory, &ReferenceMenu, "ReferenceMenu", 1024 * 16);
ClaimBuffer(MemoryArena, &ClaimedMemory, &FilterMenu, "FilterMenu", 1024 * 16); ClaimBuffer(MemoryArena, &ClaimedMemory, &FilterMenu, "FilterMenu", 1024 * 16);
ClaimBuffer(MemoryArena, &ClaimedMemory, &FilterTopics, "FilterTopics", 1024 * 8); ClaimBuffer(MemoryArena, &ClaimedMemory, &FilterTopics, "FilterTopics", 1024 * 8);
ClaimBuffer(MemoryArena, &ClaimedMemory, &FilterMedia, "FilterMedia", 1024 * 8); ClaimBuffer(MemoryArena, &ClaimedMemory, &FilterMedia, "FilterMedia", 1024 * 8);
ClaimBuffer(MemoryArena, &ClaimedMemory, &Player, "Player", 1024 * 256);
ClaimBuffer(MemoryArena, &ClaimedMemory, &Colour, "Colour", 32);
ClaimBuffer(MemoryArena, &ClaimedMemory, &Annotation, "Annotation", 1024 * 8);
ClaimBuffer(MemoryArena, &ClaimedMemory, &FilterState, "FilterState", 1024 * 4); ClaimBuffer(MemoryArena, &ClaimedMemory, &FilterState, "FilterState", 1024 * 4);
ClaimBuffer(MemoryArena, &ClaimedMemory, &ReferenceMenu, "ReferenceMenu", 1024 * 16);
ClaimBuffer(MemoryArena, &ClaimedMemory, &QuoteMenu, "QuoteMenu", 1024 * 16);
ref_info ReferencesArray[200] = { 0 }; ref_info ReferencesArray[200] = { 0 };
category_info CategoriesArray[64] = { 0 }; category_info CategoriesArray[64] = { 0 };
@ -954,6 +985,10 @@ main(int ArgC, char **Args)
" <div class=\"video_container\" data-videoId=\"%s\"></div>\n" " <div class=\"video_container\" data-videoId=\"%s\"></div>\n"
" <div class=\"markers_container %s\">\n", HMML.metadata.id, HMML.metadata.project); " <div class=\"markers_container %s\">\n", HMML.metadata.id, HMML.metadata.project);
#if DEBUG
printf(" --- Entering Annotations Loop ---\n");
#endif
for(int AnnotationIndex = 0; AnnotationIndex < HMML.annotation_count; ++AnnotationIndex) for(int AnnotationIndex = 0; AnnotationIndex < HMML.annotation_count; ++AnnotationIndex)
{ {
#if DEBUG #if DEBUG
@ -966,10 +1001,18 @@ main(int ArgC, char **Args)
quote_info QuoteInfo = { 0 }; quote_info QuoteInfo = { 0 };
// NOTE(matt): Tree structure of "annotation local" buffer dependencies
// AnnotationHeader
// AnnotationClass
// AnnotationData
// Text
// Category
ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationHeader, "AnnotationHeader", 512); ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationHeader, "AnnotationHeader", 512);
ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationClass, "AnnotationClass", 256); ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationClass, "AnnotationClass", 256);
ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationData, "AnnotationData", 128);
ClaimBuffer(MemoryArena, &ClaimedMemory, &Text, "Text", 1024 * 4); ClaimBuffer(MemoryArena, &ClaimedMemory, &Text, "Text", 1024 * 4);
ClaimBuffer(MemoryArena, &ClaimedMemory, &Colour, "Colour", 32); ClaimBuffer(MemoryArena, &ClaimedMemory, &Category, "Category", 256);
CopyStringToBuffer(&AnnotationHeader, CopyStringToBuffer(&AnnotationHeader,
" <div data-timestamp=\"%d\"", " <div data-timestamp=\"%d\"",
@ -1036,10 +1079,6 @@ Readable);
HasFilterMenu = TRUE; HasFilterMenu = TRUE;
} }
BuildFilter(CategoriesArray, &UniqueCategories, Anno->markers[MarkerIndex].marker); BuildFilter(CategoriesArray, &UniqueCategories, Anno->markers[MarkerIndex].marker);
if(!HasCategory)
{
ClaimBuffer(MemoryArena, &ClaimedMemory, &Category, "Category", 256);
}
BuildCategories(&AnnotationClass, &Category, &MarkerIndex, &HasCategory, Anno->markers[MarkerIndex].marker); BuildCategories(&AnnotationClass, &Category, &MarkerIndex, &HasCategory, Anno->markers[MarkerIndex].marker);
} }
} }
@ -1114,7 +1153,6 @@ Readable);
AppendedIdentifier: AppendedIdentifier:
if(!HasReference) if(!HasReference)
{ {
ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationData, "AnnotationData", 128);
if(CurrentRef->isbn) if(CurrentRef->isbn)
{ {
CopyStringToBuffer(&AnnotationData, " data-ref=\"%s", CurrentRef->isbn); CopyStringToBuffer(&AnnotationData, " data-ref=\"%s", CurrentRef->isbn);
@ -1179,7 +1217,6 @@ AppendedIdentifier:
if(!HasReference) if(!HasReference)
{ {
ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationData, "AnnotationData", 128);
CopyStringToBuffer(&AnnotationData, " data-ref=\"&#%d;", QuoteIdentifier); CopyStringToBuffer(&AnnotationData, " data-ref=\"&#%d;", QuoteIdentifier);
} }
else else
@ -1241,10 +1278,6 @@ Anno->time);
{ {
BuildFilter(CategoriesArray, &UniqueCategories, Anno->markers[MarkerIndex].marker); BuildFilter(CategoriesArray, &UniqueCategories, Anno->markers[MarkerIndex].marker);
} }
if(!HasCategory)
{
ClaimBuffer(MemoryArena, &ClaimedMemory, &Category, "Category", 256);
}
BuildCategories(&AnnotationClass, &Category, &MarkerIndex, &HasCategory, Anno->markers[MarkerIndex].marker); BuildCategories(&AnnotationClass, &Category, &MarkerIndex, &HasCategory, Anno->markers[MarkerIndex].marker);
} }
@ -1255,12 +1288,9 @@ Anno->time);
{ {
CopyStringToBuffer(&AnnotationData, "\""); CopyStringToBuffer(&AnnotationData, "\"");
CopyBuffer(&AnnotationHeader, &AnnotationData); CopyBuffer(&AnnotationHeader, &AnnotationData);
DeclaimBuffer(&AnnotationData, &ClaimedMemory);
} }
CopyStringToBuffer(&AnnotationHeader, ">\n"); CopyStringToBuffer(&AnnotationHeader, ">\n");
ClaimBuffer(MemoryArena, &ClaimedMemory, &Annotation, "Annotation", 1024 * 8);
CopyBuffer(&Annotation, &AnnotationHeader); CopyBuffer(&Annotation, &AnnotationHeader);
CopyStringToBuffer(&Annotation, CopyStringToBuffer(&Annotation,
" <div class=\"content\"><span class=\"timecode\">%s</span>", " <div class=\"content\"><span class=\"timecode\">%s</span>",
@ -1270,7 +1300,6 @@ Anno->time);
// TODO(matt): Handle special-cases, i.e. default media, and possibly other things // TODO(matt): Handle special-cases, i.e. default media, and possibly other things
if(!HasCategory) if(!HasCategory)
{ {
ClaimBuffer(MemoryArena, &ClaimedMemory, &Category, "Category", 256);
BuildFilter(CategoriesArray, &UniqueCategories, "default"); BuildFilter(CategoriesArray, &UniqueCategories, "default");
BuildCategories(&AnnotationClass, &Category, &MarkerIndex, &HasCategory, "default"); BuildCategories(&AnnotationClass, &Category, &MarkerIndex, &HasCategory, "default");
} }
@ -1280,7 +1309,6 @@ Anno->time);
{ {
CopyStringToBuffer(&Category, "</span>"); CopyStringToBuffer(&Category, "</span>");
CopyBuffer(&Text, &Category); CopyBuffer(&Text, &Category);
DeclaimBuffer(&Category, &ClaimedMemory);
} }
*Text.Ptr = '\0'; *Text.Ptr = '\0';
@ -1308,15 +1336,23 @@ Anno->time);
CopyBuffer(&Player, &Annotation); CopyBuffer(&Player, &Annotation);
DeclaimBuffer(&Annotation, &ClaimedMemory); // NOTE(matt): Tree structure of "annotation local" buffer dependencies
DeclaimBuffer(&Colour, &ClaimedMemory); // Category
// Text
// AnnotationData
// AnnotationClass
// AnnotationHeader
DeclaimBuffer(&Category, &ClaimedMemory);
DeclaimBuffer(&Text, &ClaimedMemory); DeclaimBuffer(&Text, &ClaimedMemory);
DeclaimBuffer(&AnnotationData, &ClaimedMemory);
DeclaimBuffer(&AnnotationClass, &ClaimedMemory); DeclaimBuffer(&AnnotationClass, &ClaimedMemory);
DeclaimBuffer(&AnnotationHeader, &ClaimedMemory); DeclaimBuffer(&AnnotationHeader, &ClaimedMemory);
Annotation.Ptr = Annotation.Location;
} }
#if DEBUG #if DEBUG
printf("EOA\n\n"); printf(" --- End of Annotations Loop ---\n\n");
#endif #endif
if(HasQuoteMenu) if(HasQuoteMenu)
{ {
@ -1558,14 +1594,29 @@ HMML.metadata.annotator);
CopyBuffer(&Master, &FilterState); CopyBuffer(&Master, &FilterState);
} }
DeclaimBuffer(&QuoteMenu, &ClaimedMemory); // NOTE(matt): Tree structure of "global" buffer dependencies
DeclaimBuffer(&ReferenceMenu, &ClaimedMemory); // FilterState
// Annotation
// Colour
// Player
// FilterMedia
// FilterTopics
// FilterMenu
// ReferenceMenu
// QuoteMenu
// Title
DeclaimBuffer(&FilterState, &ClaimedMemory); DeclaimBuffer(&FilterState, &ClaimedMemory);
DeclaimBuffer(&Annotation, &ClaimedMemory);
DeclaimBuffer(&Colour, &ClaimedMemory);
DeclaimBuffer(&Player, &ClaimedMemory);
DeclaimBuffer(&FilterMedia, &ClaimedMemory); DeclaimBuffer(&FilterMedia, &ClaimedMemory);
DeclaimBuffer(&FilterTopics, &ClaimedMemory); DeclaimBuffer(&FilterTopics, &ClaimedMemory);
DeclaimBuffer(&FilterMenu, &ClaimedMemory); DeclaimBuffer(&FilterMenu, &ClaimedMemory);
DeclaimBuffer(&ReferenceMenu, &ClaimedMemory);
DeclaimBuffer(&QuoteMenu, &ClaimedMemory);
DeclaimBuffer(&Title, &ClaimedMemory); DeclaimBuffer(&Title, &ClaimedMemory);
DeclaimBuffer(&Player, &ClaimedMemory);
CopyStringToBuffer(&Master, CopyStringToBuffer(&Master,
"// Filter Mode Toggle\n" "// Filter Mode Toggle\n"