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