hmml_to_youtube.c: Add # removal printout [#9]

This commit is contained in:
Matt Mascarenhas 2017-04-11 12:34:39 +01:00
parent da4c64f5d6
commit 9c47ca398f
1 changed files with 67 additions and 37 deletions

View File

@ -24,6 +24,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
StringLength(char *String)
{
@ -57,7 +66,7 @@ StringsDiffer(char *A, char *B)
__attribute__ ((format (printf, 2, 3)))
void
int
CopyStringToBuffer(buffer *Dest, char *Format, ...)
{
va_list Args;
@ -65,6 +74,7 @@ CopyStringToBuffer(buffer *Dest, char *Format, ...)
int Length = vsprintf(Dest->Ptr, Format, Args);
va_end(Args);
Dest->Ptr += Length;
return Length;
}
int
@ -76,38 +86,42 @@ main(int ArgC, char **Args)
return 1;
}
// Init MemoryArena
int ArenaSize = 1024 * 64;
char *MemoryArena;
if(!(MemoryArena = calloc(ArenaSize, 1)))
{
perror(Args[0]);
return 1;
}
int ClaimedMemory = 0;
// Buffers and Pointers
char *InPtr; // Associated buffer is allocated by hmml_parse_file()
buffer Errors;
buffer Out;
FILE *InFile;
for(int FileIndex = 1; FileIndex < ArgC; ++FileIndex)
{
if(!(InFile = fopen(Args[FileIndex], "r")))
{
perror(Args[0]);
free(MemoryArena);
return 1;
}
// Init MemoryArena
int ArenaSize = 1024 * 32;
char *MemoryArena;
if(!(MemoryArena = calloc(ArenaSize, 1)))
{
perror(Args[0]);
return 1;
}
int ClaimedMemory = 0;
ClaimBuffer(MemoryArena, &ClaimedMemory, &Errors, 1024 * 32);
// Buffers and Pointers
char *InPtr; // Associated buffer is allocated by hmml_parse_file()
buffer Out;
char OutFilename[StringLength(Args[FileIndex]) + 5];
sprintf(OutFilename, "%s.txt", Args[FileIndex]);
//printf("Reading %s\n", Args[FileIndex]);
HMML_Output HMML = hmml_parse_file(InFile);
fclose(InFile);
if(HMML.well_formed)
{
Out.Location = MemoryArena + ClaimedMemory;
Out.Size = 1024*16;
ClaimedMemory += Out.Size;
ClaimBuffer(MemoryArena, &ClaimedMemory, &Out, 1024 * 32);
char *Member = HMML.metadata.twitch ?
HMML.metadata.twitch :
@ -117,13 +131,19 @@ main(int ArgC, char **Args)
for(int BuildPass = 0; BuildPass < 2; ++BuildPass)
{
int OutLine = 1, OutColumn = 1;
Errors.Ptr = Errors.Location;
HMML_Annotation *Anno;
#if 0
if(WritingChat == FALSE)
{
printf("%s: %ld characters over budget. Shrinking...\n", Args[FileIndex], (Out.Ptr - Out.Location) - 5000);
CopyStringToBuffer(&Errors, "%s - %ld characters over budget. Shrinking...\n", Args[FileIndex], (Out.Ptr - Out.Location) - 5000);
}
#endif
Out.Ptr = Out.Location;
HMML_Annotation *Anno;
for(int AnnotationIndex = 0; AnnotationIndex < HMML.annotation_count; ++AnnotationIndex)
{
Anno = HMML.annotations + AnnotationIndex;
@ -133,13 +153,13 @@ main(int ArgC, char **Args)
goto skip;
}
CopyStringToBuffer(&Out, "%s ", Anno->time);
OutColumn += CopyStringToBuffer(&Out, "%s ", Anno->time);
InPtr = Anno->text;
if(Anno->author)
{
CopyStringToBuffer(&Out, "Chat comment: \"");
OutColumn += CopyStringToBuffer(&Out, "Chat comment: \"");
}
int MarkerIndex = 0, RefIndex = 0, InOffset = 0;
@ -180,19 +200,20 @@ main(int ArgC, char **Args)
if(Out.Ptr[-1] != '"' && Out.Ptr[-1] != ' ')
{
*Out.Ptr++ = ' ';
OutColumn += 1;
}
if(Anno->references[RefIndex].page)
{
CopyStringToBuffer(&Out, "%s", Anno->references[RefIndex].page);
OutColumn += CopyStringToBuffer(&Out, "%s", Anno->references[RefIndex].page);
}
else if(Anno->references[RefIndex].site)
{
CopyStringToBuffer(&Out, "%s", Anno->references[RefIndex].site);
OutColumn += CopyStringToBuffer(&Out, "%s", Anno->references[RefIndex].site);
}
else if(Anno->references[RefIndex].title)
{
CopyStringToBuffer(&Out, "%s", Anno->references[RefIndex].title);
OutColumn += CopyStringToBuffer(&Out, "%s", Anno->references[RefIndex].title);
}
}
@ -204,21 +225,23 @@ main(int ArgC, char **Args)
if(Out.Ptr[-1] != ' ')
{
*Out.Ptr++ = ' ';
OutColumn += 1;
}
CopyStringToBuffer(&Out, "- %s -", Anno->references[RefIndex].url);
OutColumn += CopyStringToBuffer(&Out, "- %s -", Anno->references[RefIndex].url);
}
else
{
if(Out.Ptr[-1] == ' ')
{
--Out.Ptr;
OutColumn -= 1;
}
if(InPtr[-3] != ':')
{
CopyStringToBuffer(&Out, ": ");
OutColumn += CopyStringToBuffer(&Out, ": ");
}
CopyStringToBuffer(&Out, "%s", Anno->references[RefIndex].url);
OutColumn += CopyStringToBuffer(&Out, "%s", Anno->references[RefIndex].url);
}
}
++RefIndex;
@ -229,15 +252,17 @@ main(int ArgC, char **Args)
switch(*InPtr)
{
case '<':
CopyStringToBuffer(&Out, "&lt;");
OutColumn += CopyStringToBuffer(&Out, "&lt;");
break;
case '>':
CopyStringToBuffer(&Out, "&gt;");
OutColumn += CopyStringToBuffer(&Out, "&gt;");
break;
case '#':
CopyStringToBuffer(&Errors, "%s:%d,%d - Removed '#'. Consider editing\n", OutFilename, OutLine, OutColumn);
break;
default:
*Out.Ptr++ = *InPtr;
OutColumn += 1;
break;
}
++InPtr;
@ -252,6 +277,8 @@ main(int ArgC, char **Args)
}
*Out.Ptr++ = '\n';
++OutLine;
OutColumn = 1;
skip: {};
}
@ -270,18 +297,14 @@ skip: {};
if(Out.Ptr - Out.Location > 5000)
{
fprintf(stderr, "%s: %ld characters over budget. Requires manual shrinking!\n", Args[FileIndex], (Out.Ptr - Out.Location) - 5000);
CopyStringToBuffer(&Errors, "%s - %ld characters over budget. Requires manual shrinking!\n", OutFilename, (Out.Ptr - Out.Location) - 5000);
}
// NOTE(matt): At this point we should have filled a buffer with the stuff
hmml_free(&HMML);
// NOTE(matt): Open a file for writing out to
char Filename[StringLength(Args[FileIndex]) + 5];
sprintf(Filename, "%s.txt", Args[FileIndex]);
//printf("Writing to %s\n", Filename);
FILE *OutFile;
if(!(OutFile = fopen(Filename, "w")))
if(!(OutFile = fopen(OutFilename, "w")))
{
perror(Args[0]);
free(MemoryArena);
@ -290,15 +313,22 @@ skip: {};
fwrite(Out.Location, Out.Ptr - Out.Location, 1, OutFile);
fclose(OutFile);
ClaimedMemory -= Out.Size;
}
else
{
fprintf(stderr, "%s:%d: %s\n", Args[FileIndex], HMML.error.line, HMML.error.message);
CopyStringToBuffer(&Errors, "%s:%d: %s\n", Args[FileIndex], HMML.error.line, HMML.error.message);
hmml_free(&HMML);
}
free(MemoryArena);
if(Errors.Ptr > Errors.Location)
{
fprintf(stderr, Errors.Location);
}
ClaimedMemory -= Errors.Size;
}
free(MemoryArena);
return 0;
}