hmml_to_html.c: Fix BuildReference() order [#16]

Remove dependency on string.h
Correctly free everything on every return from main()
This commit is contained in:
Matt Mascarenhas 2017-04-23 01:30:37 +01:00
parent fbdc26dfd5
commit d34ad0a03a
1 changed files with 46 additions and 26 deletions

View File

@ -13,7 +13,6 @@ typedef unsigned int bool;
#include <stdarg.h> // NOTE(matt): varargs #include <stdarg.h> // NOTE(matt): varargs
#include <stdio.h> // NOTE(matt): printf, sprintf, vsprintf, fprintf, perror #include <stdio.h> // NOTE(matt): printf, sprintf, vsprintf, fprintf, perror
#include <stdlib.h> // NOTE(matt): calloc, malloc, free #include <stdlib.h> // NOTE(matt): calloc, malloc, free
#include <string.h> // NOTE(matt): strncmp, memset
#include "hmmlib.h" #include "hmmlib.h"
typedef struct typedef struct
@ -123,6 +122,24 @@ StringsDiffer(char *A, char *B)
return *A - *B; return *A - *B;
} }
bool
StringsDifferL(char *A, char *B, int LengthofA)
{
int i = 0;
while(i < LengthofA && A[i] && A[i] == B[i])
{
++i;
}
if(!A[i] && LengthofA == i)
{
return FALSE;
}
else
{
return TRUE;
}
}
typedef struct typedef struct
{ {
unsigned int Hue:16; unsigned int Hue:16;
@ -222,19 +239,20 @@ char *CategoryMedium[] =
int int
BuildReference(ref_info *ReferencesArray, int RefIdentifier, int UniqueRefs, HMML_Reference Ref, HMML_Annotation Anno) BuildReference(ref_info *ReferencesArray, int RefIdentifier, int UniqueRefs, HMML_Reference Ref, HMML_Annotation Anno)
{ {
if(Ref.page && Ref.url && Ref.title) if(Ref.url && Ref.title && Ref.author && Ref.publisher && Ref.isbn)
{
CopyString(ReferencesArray[UniqueRefs].ID, Ref.isbn);
CopyString(ReferencesArray[UniqueRefs].Source, "%s (%s)", Ref.author, Ref.publisher);
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.title);
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
}
else if(Ref.page && Ref.url && Ref.title)
{ {
CopyString(ReferencesArray[UniqueRefs].ID, Ref.url); CopyString(ReferencesArray[UniqueRefs].ID, Ref.url);
CopyString(ReferencesArray[UniqueRefs].Source, Ref.title); CopyString(ReferencesArray[UniqueRefs].Source, Ref.title);
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.page); CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.page);
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url); CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
} }
else if(Ref.url && Ref.title)
{
CopyString(ReferencesArray[UniqueRefs].ID, Ref.url);
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.title);
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
}
else if(Ref.site && Ref.page && Ref.url) else if(Ref.site && Ref.page && Ref.url)
{ {
CopyString(ReferencesArray[UniqueRefs].ID, Ref.url); CopyString(ReferencesArray[UniqueRefs].ID, Ref.url);
@ -242,12 +260,6 @@ BuildReference(ref_info *ReferencesArray, int RefIdentifier, int UniqueRefs, HMM
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.page); CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.page);
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url); CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
} }
else if(Ref.site && Ref.url)
{
CopyString(ReferencesArray[UniqueRefs].ID, Ref.url);
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.site);
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
}
else if(Ref.site && Ref.url && Ref.title) else if(Ref.site && Ref.url && Ref.title)
{ {
CopyString(ReferencesArray[UniqueRefs].ID, Ref.url); CopyString(ReferencesArray[UniqueRefs].ID, Ref.url);
@ -255,13 +267,6 @@ BuildReference(ref_info *ReferencesArray, int RefIdentifier, int UniqueRefs, HMM
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.title); CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.title);
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url); CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
} }
else if(Ref.url && Ref.title && Ref.author && Ref.publisher && Ref.isbn)
{
CopyString(ReferencesArray[UniqueRefs].ID, Ref.isbn);
CopyString(ReferencesArray[UniqueRefs].Source, "%s (%s)", Ref.author, Ref.publisher);
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.title);
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
}
else if(Ref.title && Ref.author && Ref.isbn) else if(Ref.title && Ref.author && Ref.isbn)
{ {
CopyString(ReferencesArray[UniqueRefs].ID, Ref.isbn); CopyString(ReferencesArray[UniqueRefs].ID, Ref.isbn);
@ -283,6 +288,18 @@ BuildReference(ref_info *ReferencesArray, int RefIdentifier, int UniqueRefs, HMM
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.title); CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.title);
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url); CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
} }
else if(Ref.url && Ref.title)
{
CopyString(ReferencesArray[UniqueRefs].ID, Ref.url);
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.title);
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
}
else if(Ref.site && Ref.url)
{
CopyString(ReferencesArray[UniqueRefs].ID, Ref.url);
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.site);
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
}
else else
{ {
return 1; return 1;
@ -352,7 +369,7 @@ GenerateTopicColours(buffer *Colour, char *Topic)
while(TopicsPtr - TopicsBuffer < TopicsLength) while(TopicsPtr - TopicsBuffer < TopicsLength)
{ {
TopicsPtr += 39; TopicsPtr += 39;
if(!strncmp(SanitisePunctuation(Topic), TopicsPtr, StringLength(Topic))) if(!StringsDifferL(SanitisePunctuation(Topic), TopicsPtr, StringLength(Topic)))
{ {
free(TopicsBuffer); free(TopicsBuffer);
fclose(TopicsFile); fclose(TopicsFile);
@ -434,8 +451,7 @@ main(int ArgC, char **Args)
if(HMML.well_formed) if(HMML.well_formed)
{ {
ClaimBuffer(MemoryArena, &ClaimedMemory, &Title, 1024 * 16); ClaimBuffer(MemoryArena, &ClaimedMemory, &Title, 1024 * 16);
ref_info ReferencesArray[200]; ref_info ReferencesArray[200] = { 0 };
memset(ReferencesArray, 0, sizeof(ReferencesArray));
ClaimBuffer(MemoryArena, &ClaimedMemory, &Player, 1024 * 256); ClaimBuffer(MemoryArena, &ClaimedMemory, &Player, 1024 * 256);
bool HasQuoteMenu = FALSE; bool HasQuoteMenu = FALSE;
@ -576,6 +592,8 @@ Readable);
else else
{ {
fprintf(stderr, "%s:%d: Reference must have an ISBN or URL\n", Args[FileIndex], Anno->line); fprintf(stderr, "%s:%d: Reference must have an ISBN or URL\n", Args[FileIndex], Anno->line);
hmml_free(&HMML);
free(MemoryArena);
return 1; return 1;
} }
} }
@ -605,8 +623,8 @@ AppendedIdentifier:
else else
{ {
fprintf(stderr, "%s:%d: Reference must have an ISBN or URL\n", Args[FileIndex], Anno->line); fprintf(stderr, "%s:%d: Reference must have an ISBN or URL\n", Args[FileIndex], Anno->line);
free(MemoryArena);
hmml_free(&HMML); hmml_free(&HMML);
free(MemoryArena);
return 1; return 1;
} }
@ -625,8 +643,8 @@ AppendedIdentifier:
else else
{ {
fprintf(stderr, "%s:%d: Reference must have an ISBN or URL", Args[FileIndex], Anno->line); fprintf(stderr, "%s:%d: Reference must have an ISBN or URL", Args[FileIndex], Anno->line);
free(MemoryArena);
hmml_free(&HMML); hmml_free(&HMML);
free(MemoryArena);
return 1; return 1;
} }
} }
@ -935,6 +953,8 @@ HMML.metadata.annotator);
if(!(OutFile = fopen("out.html", "w"))) if(!(OutFile = fopen("out.html", "w")))
{ {
perror(Args[0]); perror(Args[0]);
hmml_free(&HMML);
free(MemoryArena);
return 1; return 1;
} }
fwrite(Master.Location, Master.Ptr - Master.Location, 1, OutFile); fwrite(Master.Location, Master.Ptr - Master.Location, 1, OutFile);