diff --git a/hmml_to_html/hmml_to_html.c b/hmml_to_html/hmml_to_html.c
index ba18259..00197dd 100644
--- a/hmml_to_html/hmml_to_html.c
+++ b/hmml_to_html/hmml_to_html.c
@@ -23,6 +23,15 @@ typedef struct
int Size;
} buffer;
+void
+ClaimBuffer(char *MemoryArena, int *ClaimedMemory, buffer *Buffer, int Size)
+{
+ Buffer->Location = MemoryArena + *ClaimedMemory;
+ Buffer->Size = Size;
+ *ClaimedMemory += Buffer->Size;
+ Buffer->Ptr = Buffer->Location;
+}
+
int
TimecodeToSeconds(char *Timecode)
{
@@ -55,6 +64,25 @@ TimecodeToSeconds(char *Timecode)
return HMS[2] * 60 * 60 + HMS[1] * 60 + HMS[0];
}
+void
+CopyBuffer(buffer *Src, buffer *Dest)
+{
+ Src->Ptr = Src->Location;
+ while(*Src->Ptr)
+ {
+ *Dest->Ptr++ = *Src->Ptr++;
+ }
+}
+
+void
+CopyStringToBuffer(char *Src, buffer *Dest)
+{
+ while(*Src)
+ {
+ *Dest->Ptr++ = *Src++;
+ }
+}
+
int
main(int ArgC, char **Args)
{
@@ -66,8 +94,7 @@ main(int ArgC, char **Args)
// NOTE(matt): Init MemoryArena
char *MemoryArena;
- // TODO(matt): Actually calculate how much memory I'll need
- int ArenaSize = 1024 * 512;
+ int ArenaSize = 1024 * 1024;
if(!(MemoryArena = calloc(ArenaSize, 1)))
{
perror(Args[0]);
@@ -81,10 +108,8 @@ main(int ArgC, char **Args)
buffer Text;
buffer Out;
- Out.Location = MemoryArena + ClaimedMemory;
- Out.Size = 1024 * 200;
- ClaimedMemory += Out.Size;
-
+ ClaimBuffer(MemoryArena, &ClaimedMemory, &Out, 1024 * 512);
+
for(int FileIndex = 1; FileIndex < ArgC; ++FileIndex)
{
FILE *InFile;
@@ -100,9 +125,7 @@ main(int ArgC, char **Args)
if(HMML.well_formed)
{
- Working.Location = MemoryArena + ClaimedMemory;
- Working.Size = 1024 * 1;
- ClaimedMemory += Working.Size;
+ ClaimBuffer(MemoryArena, &ClaimedMemory, &Working, 1024);
sprintf(Working.Location,
"\n"
@@ -117,13 +140,9 @@ main(int ArgC, char **Args)
"
\n"
"
%s\n", HMML.metadata.title);
- Working.Ptr = Working.Location;
Out.Ptr = Out.Location;
- while(*Working.Ptr)
- {
- *Out.Ptr++ = *Working.Ptr++;
- }
+ CopyBuffer(&Working, &Out);
int AnnotationIndex = 0;
int ReferenceIndex = 1;
@@ -137,11 +156,7 @@ main(int ArgC, char **Args)
"
\n"
"
\n");
- Working.Ptr = Working.Location;
- while(*Working.Ptr)
- {
- *Out.Ptr++ = *Working.Ptr++;
- }
+ CopyBuffer(&Working, &Out);
while(AnnotationIndex < HMML.annotation_count)
{
@@ -169,11 +184,7 @@ TimecodeToSeconds(HMML.annotations[AnnotationIndex].time),
ReferenceIndex,
HMML.annotations[AnnotationIndex].time);
- Working.Ptr = Working.Location;
- while(*Working.Ptr)
- {
- *Out.Ptr++ = *Working.Ptr++;
- }
+ CopyBuffer(&Working, &Out);
++ReferenceIndex;
}
++AnnotationIndex;
@@ -191,12 +202,7 @@ HMML.annotations[AnnotationIndex].time);
"
\n"
"
\n", HMML.metadata.annotator, HMML.metadata.id);
- Working.Ptr = Working.Location;
-
- while(*Working.Ptr)
- {
- *Out.Ptr++ = *Working.Ptr++;
- }
+ CopyBuffer(&Working, &Out);
int DataRef = 1;
@@ -204,10 +210,7 @@ HMML.annotations[AnnotationIndex].time);
{
InPtr = HMML.annotations[AnnotationIndex].text;
- Text.Location = MemoryArena + ClaimedMemory;
- Text.Size = 256;
- ClaimedMemory += Text.Size;
- Text.Ptr = Text.Location;
+ ClaimBuffer(MemoryArena, &ClaimedMemory, &Text, 256);
if(HMML.annotations[AnnotationIndex].reference_count) // || HMML.annotations[AnnotationIndex].is_quote)
{
@@ -257,28 +260,18 @@ HMML.annotations[AnnotationIndex].time);
Text.Ptr += Inc;
++DataRef;
}
- while(*InPtr)
- {
- *Text.Ptr++ = *InPtr++;
- }
+ CopyStringToBuffer(InPtr, &Text);
}
else
{
sprintf(Working.Location,
"
\n", TimecodeToSeconds(HMML.annotations[AnnotationIndex].time));
- while(*InPtr)
- {
- *Text.Ptr++ = *InPtr++;
- }
+ CopyStringToBuffer(InPtr, &Text);
}
*Text.Ptr = '\0';
- Text.Ptr = Text.Location;
- Working.Ptr = Working.Location;
- while(*Working.Ptr)
- {
- *Out.Ptr++ = *Working.Ptr++;
- }
+
+ CopyBuffer(&Working, &Out);
sprintf(Working.Location,
"
%s%s
\n"
@@ -295,14 +288,10 @@ HMML.annotations[AnnotationIndex].time);
Text.Location,
HMML.annotations[AnnotationIndex].time,
Text.Location);
- Working.Ptr = Working.Location;
ClaimedMemory -= Text.Size;
- while(*Working.Ptr)
- {
- *Out.Ptr++ = *Working.Ptr++;
- }
+ CopyBuffer(&Working, &Out);
}
sprintf(Working.Location,
@@ -376,11 +365,7 @@ HMML.annotations[AnnotationIndex].time);
" \n"
"