Commit d34ad0a0 authored by Matt Mascarenhas's avatar Matt Mascarenhas

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

Remove dependency on string.h
Correctly free everything on every return from main()
parent fbdc26df
......@@ -13,7 +13,6 @@ typedef unsigned int bool;
#include <stdarg.h> // NOTE(matt): varargs
#include <stdio.h> // NOTE(matt): printf, sprintf, vsprintf, fprintf, perror
#include <stdlib.h> // NOTE(matt): calloc, malloc, free
#include <string.h> // NOTE(matt): strncmp, memset
#include "hmmlib.h"
typedef struct
......@@ -123,6 +122,24 @@ StringsDiffer(char *A, char *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
{
unsigned int Hue:16;
......@@ -222,17 +239,18 @@ char *CategoryMedium[] =
int
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.url);
CopyString(ReferencesArray[UniqueRefs].Source, Ref.title);
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.page);
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.url && Ref.title)
else if(Ref.page && Ref.url && Ref.title)
{
CopyString(ReferencesArray[UniqueRefs].ID, Ref.url);
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.title);
CopyString(ReferencesArray[UniqueRefs].Source, Ref.title);
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.page);
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
}
else if(Ref.site && Ref.page && Ref.url)
......@@ -242,12 +260,6 @@ BuildReference(ref_info *ReferencesArray, int RefIdentifier, int UniqueRefs, HMM
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.page);
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)
{
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].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)
{
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].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
{
return 1;
......@@ -352,7 +369,7 @@ GenerateTopicColours(buffer *Colour, char *Topic)
while(TopicsPtr - TopicsBuffer < TopicsLength)
{
TopicsPtr += 39;
if(!strncmp(SanitisePunctuation(Topic), TopicsPtr, StringLength(Topic)))
if(!StringsDifferL(SanitisePunctuation(Topic), TopicsPtr, StringLength(Topic)))
{
free(TopicsBuffer);
fclose(TopicsFile);
......@@ -434,8 +451,7 @@ main(int ArgC, char **Args)
if(HMML.well_formed)
{
ClaimBuffer(MemoryArena, &ClaimedMemory, &Title, 1024 * 16);
ref_info ReferencesArray[200];
memset(ReferencesArray, 0, sizeof(ReferencesArray));
ref_info ReferencesArray[200] = { 0 };
ClaimBuffer(MemoryArena, &ClaimedMemory, &Player, 1024 * 256);
bool HasQuoteMenu = FALSE;
......@@ -576,6 +592,8 @@ Readable);
else
{
fprintf(stderr, "%s:%d: Reference must have an ISBN or URL\n", Args[FileIndex], Anno->line);
hmml_free(&HMML);
free(MemoryArena);
return 1;
}
}
......@@ -605,8 +623,8 @@ AppendedIdentifier:
else
{
fprintf(stderr, "%s:%d: Reference must have an ISBN or URL\n", Args[FileIndex], Anno->line);
free(MemoryArena);
hmml_free(&HMML);
free(MemoryArena);
return 1;
}
......@@ -625,8 +643,8 @@ AppendedIdentifier:
else
{
fprintf(stderr, "%s:%d: Reference must have an ISBN or URL", Args[FileIndex], Anno->line);
free(MemoryArena);
hmml_free(&HMML);
free(MemoryArena);
return 1;
}
}
......@@ -935,6 +953,8 @@ HMML.metadata.annotator);
if(!(OutFile = fopen("out.html", "w")))
{
perror(Args[0]);
hmml_free(&HMML);
free(MemoryArena);
return 1;
}
fwrite(Master.Location, Master.Ptr - Master.Location, 1, OutFile);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment