diff --git a/cinera/cinera.c b/cinera/cinera.c index dba3f9f..27d60db 100644 --- a/cinera/cinera.c +++ b/cinera/cinera.c @@ -16,8 +16,8 @@ typedef struct version CINERA_APP_VERSION = { .Major = 0, - .Minor = 6, - .Patch = 8 + .Minor = 7, + .Patch = 0 }; #include // NOTE(matt): varargs @@ -25,7 +25,6 @@ version CINERA_APP_VERSION = { #include // NOTE(matt): calloc, malloc, free #include "hmmlib.h" #include // NOTE(matt): getopts -//#include "config.h" // TODO(matt): Implement config.h #include #include #include @@ -34,6 +33,7 @@ version CINERA_APP_VERSION = { #include // NOTE(matt): strerror #include //NOTE(matt): errno #include // NOTE(matt): inotify +#include // NOTE(matt): ioctl and TIOCGWINSZ #include #define __USE_XOPEN2K8 // NOTE(matt): O_NOFOLLOW @@ -41,7 +41,15 @@ version CINERA_APP_VERSION = { #define __USE_XOPEN2K // NOTE(matt): readlink() #include // NOTE(matt): sleep() -typedef unsigned int bool; +#define STB_IMAGE_IMPLEMENTATION +#define STBI_NO_LINEAR +#define STBI_NO_HDR +#define STBI_ONLY_GIF +#define STBI_ONLY_JPEG +#define STBI_ONLY_PNG +#include "stb_image.h" + +typedef uint64_t bool; #define TRUE 1 #define FALSE 0 @@ -49,9 +57,19 @@ typedef unsigned int bool; #define enum16(type) int16_t #define enum32(type) int32_t +// DEBUG SWITCHES + +#define DEBUG_MEMORY_LEAKAGE 0 +#define DEBUG_MEMORY_LEAKAGE_LOOPED 0 +#define DEBUG_PRINT_FUNCTION_NAMES 0 +#define DEBUG_PROJECT_INDICES 0 + #define DEBUG 0 #define DEBUG_MEM 0 +// +//// + bool PROFILING = 0; clock_t TIMING_START; #define START_TIMING_BLOCK(...) if(PROFILING) { printf(__VA_ARGS__); TIMING_START = clock(); } @@ -60,83 +78,185 @@ clock_t TIMING_START; #define Kilobytes(Bytes) Bytes << 10 #define Megabytes(Bytes) Bytes << 20 -#define MAX_PROJECT_ID_LENGTH 31 -#define MAX_PROJECT_NAME_LENGTH 63 -#define MAX_BASE_DIR_LENGTH 127 -#define MAX_BASE_URL_LENGTH 127 -#define MAX_RELATIVE_PAGE_LOCATION_LENGTH 31 -#define MAX_PLAYER_URL_PREFIX_LENGTH 15 +#define MAX_UNIT_LENGTH 16 +#define MAX_PROJECT_ID_LENGTH 32 +#define MAX_THEME_LENGTH MAX_PROJECT_ID_LENGTH +#define MAX_PROJECT_NAME_LENGTH 64 +#define MAX_BASE_DIR_LENGTH 128 +#define MAX_BASE_URL_LENGTH 128 +#define MAX_RELATIVE_PAGE_LOCATION_LENGTH 32 +#define MAX_VOD_ID_LENGTH 32 -#define MAX_ROOT_DIR_LENGTH 127 -#define MAX_ROOT_URL_LENGTH 127 -#define MAX_RELATIVE_ASSET_LOCATION_LENGTH 31 +#define MAX_ROOT_DIR_LENGTH 128 +#define MAX_ROOT_URL_LENGTH 128 +#define MAX_RELATIVE_ASSET_LOCATION_LENGTH 32 -#define MAX_BASE_FILENAME_LENGTH 31 -#define MAX_TITLE_LENGTH 128 - (MAX_BASE_FILENAME_LENGTH + 1) - (int)sizeof(link_insertion_offsets) - (int)sizeof(unsigned short int) - 1 // NOTE(matt): We size this such that db_entry is 128 bytes total +#define MAX_BASE_FILENAME_LENGTH 32 +#define MAX_ENTRY_OUTPUT_LENGTH MAX_BASE_FILENAME_LENGTH +#define MAX_TITLE_LENGTH 128 -#define MAX_ASSET_FILENAME_LENGTH 63 +#define MAX_ASSET_FILENAME_LENGTH 64 // TODO(matt): Stop distinguishing between short / long and lift the size limit once we're on the LUT -#define MAX_CUSTOM_SNIPPET_SHORT_LENGTH 255 -#define MAX_CUSTOM_SNIPPET_LONG_LENGTH 1023 +#define MAX_CUSTOM_SNIPPET_SHORT_LENGTH 256 +#define MAX_CUSTOM_SNIPPET_LONG_LENGTH 1024 + +#define INDENT_WIDTH 4 #define ArrayCount(A) sizeof(A)/sizeof(*(A)) -#define Assert(Expression) if(!(Expression)) { printf("l.%d: \e[1;31mAssertion failure\e[0m\n", __LINE__); __asm__("int3"); } +#define Assert(Expression) do { if(!(Expression)){ fprintf(stderr, "l.%d: \e[1;31mAssertion failure\e[0m\n", __LINE__); __asm__("int3"); } } while(0) +#define BreakHere() fprintf(stderr, "[%i] BreakHere\n", __LINE__) #define FOURCC(String) ((uint32_t)(String[0] << 0) | (uint32_t)(String[1] << 8) | (uint32_t)(String[2] << 16) | (uint32_t)(String[3] << 24)) -enum -{ - EDITION_SINGLE, - EDITION_PROJECT, - EDITION_NETWORK -} editions; +#define Free(M) free(M); M = 0; +#define FreeAndResetCount(M, Count) { Free(M); Count = 0; } +#define FreeAndResetCountAndCapacity(M, Count, Capacity) { FreeAndResetCount(M, Count); Capacity = 0; } -enum -{ - // NOTE(matt): https://tools.ietf.org/html/rfc5424#section-6.2.1 - LOG_EMERGENCY, - LOG_ALERT, - LOG_CRITICAL, - LOG_ERROR, - LOG_WARNING, - LOG_NOTICE, - LOG_INFORMATIONAL, - LOG_DEBUG -} log_levels; +#define MIN(A, B) A < B ? A : B -enum +void +Clear(void *V, uint64_t Size) { - MODE_FORCEINTEGRATION = 1 << 0, - MODE_ONESHOT = 1 << 1, - MODE_EXAMINE = 1 << 2, - MODE_NOCACHE = 1 << 3, - MODE_NOPRIVACY = 1 << 4, - MODE_SINGLETAB = 1 << 5, - MODE_NOREVVEDRESOURCE = 1 << 6 -} modes; + char *Ptr = (char *)V; + for(int i = 0; i < Size; ++i) + { + *Ptr++ = 0; + } +} -enum +void * +Fit(void *Base, uint16_t WidthInBytes, uint64_t ItemCount, uint32_t ItemsPerBlock, bool ZeroInitialise) +{ + if(ItemCount % ItemsPerBlock == 0) + { + Base = realloc(Base, (ItemCount + ItemsPerBlock) * WidthInBytes); + if(ZeroInitialise) + { + Clear(Base + WidthInBytes * ItemCount, WidthInBytes * ItemsPerBlock); + } + } + return Base; +} + + +void * +FitShrinkable(void *Base, uint16_t WidthInBytes, uint64_t ItemCount, uint64_t *Capacity, uint32_t ItemsPerBlock, bool ZeroInitialise) +{ + if(ItemCount == *Capacity) + { + Base = realloc(Base, (ItemCount + ItemsPerBlock) * WidthInBytes); + if(ZeroInitialise) + { + Clear(Base + WidthInBytes * ItemCount, WidthInBytes * ItemsPerBlock); + } + *Capacity += ItemsPerBlock; + } + return Base; +} + + +// NOTE(matt): Memory testing +#include +int +GetUsage(void) +{ + struct rusage Usage; + getrusage(RUSAGE_SELF, &Usage); + return Usage.ru_maxrss; +} + +// NOTE(matt): Mem loop testing +// +// Let the usual work function happen, then: +// +// Call MEM_LOOP_PRE_FREE("Thing you're testing") +// Call the freeing function +// Call MEM_LOOP_PRE_WORK() +// Call the function whose work the freeing function is meant to free +// Call MEM_LOOP_POST("Thing you're testing") +// +#if DEBUG_MEMORY_LEAKAGE_LOOPED +#define MEM_LOOP_PRE_FREE(String) \ + fprintf(stderr, "Testing %s freeing\n", String);\ + int MemLoopInitial = GetUsage();\ + int MemLoopOld = MemLoopInitial;\ + int MemLoopNew;\ + for(int i = 0; i < 1 << 16; ++i)\ + { + +#define MEM_LOOP_PRE_WORK() \ + MemLoopNew = GetUsage();\ + if(1 /*MemLoopNew > MemLoopOld*/) { fprintf(stderr, "iter %2i: %i (%s%i)\n", i, MemLoopNew, MemLoopNew > MemLoopOld ? "+" : "-", MemLoopNew - MemLoopOld); sleep(1); }\ + +#define MEM_LOOP_POST(String) \ + MemLoopOld = MemLoopNew;\ + }\ + fprintf(stderr, "Done (%s): ", String);\ + Colourise(MemLoopNew > MemLoopInitial ? CS_RED : CS_GREEN);\ + fprintf(stderr, "%s%i\n", MemLoopNew > MemLoopInitial ? "+" : "-", MemLoopNew - MemLoopInitial);\ + Colourise(CS_END);\ + sleep(4); +#else +#define MEM_LOOP_PRE_FREE(String) +#define MEM_LOOP_PRE_WORK() +#define MEM_LOOP_POST(String) +#endif +// +//// + +// NOTE(matt): One-time memory testing +// +#if DEBUG_MEMORY_LEAKAGE +#define MEM_TEST_INITIAL() int Initial = GetUsage() +#define MEM_TEST_MID(String) if(GetUsage() > Initial) fprintf(stderr, "%s: %i kb\n", String, GetUsage() - Initial) +#define MEM_TEST_AFTER(String) int Current = GetUsage();\ + Colourise(Current > Initial ? CS_RED : CS_BLUE);\ + fprintf(stderr, "%s leaks %i kb\n", String, Current - Initial);\ + Colourise(CS_END); +#else +#define MEM_TEST_INITIAL() +#define MEM_TEST_MID(String) +#define MEM_TEST_AFTER(String) +#endif +// +//// + +typedef enum +{ + //MODE_FORCEINTEGRATION = 1 << 0, + MODE_EXAMINE = 1 << 0, + MODE_DRYRUN = 1 << 1, +} mode; + +typedef enum { RC_ARENA_FULL, + RC_ERROR_CAPACITY, RC_ERROR_DIRECTORY, RC_ERROR_FATAL, RC_ERROR_FILE, RC_ERROR_HMML, RC_ERROR_MAX_REFS, RC_ERROR_MEMORY, + RC_ERROR_MISSING_PARAMETER, RC_ERROR_PARSING, + RC_ERROR_PARSING_UNCLOSED_QUOTED_STRING, RC_ERROR_PROJECT, RC_ERROR_QUOTE, RC_ERROR_SEEK, RC_FOUND, - RC_UNFOUND, + RC_INVALID_IDENTIFIER, RC_INVALID_REFERENCE, RC_INVALID_TEMPLATE, - RC_PRIVATE_VIDEO, RC_NOOP, + RC_PRIVATE_VIDEO, RC_RIP, + RC_SCHEME_MIXTURE, + RC_SYNTAX_ERROR, + RC_UNFOUND, + RC_FAILURE, RC_SUCCESS -} returns; +} rc; typedef struct { @@ -146,64 +266,1538 @@ typedef struct int Size; } arena; -typedef struct +char *BufferIDStrings[] = { - // Universal - char CacheDir[256]; - enum8(editions) Edition; - enum8(log_levels) LogLevel; - enum8(modes) Mode; - int UpdateInterval; + "BID_NULL", + "BID_CHECKSUM", + "BID_COLLATION_BUFFERS_CUSTOM0", + "BID_COLLATION_BUFFERS_CUSTOM1", + "BID_COLLATION_BUFFERS_CUSTOM2", + "BID_COLLATION_BUFFERS_CUSTOM3", + "BID_COLLATION_BUFFERS_CUSTOM4", + "BID_COLLATION_BUFFERS_CUSTOM5", + "BID_COLLATION_BUFFERS_CUSTOM6", + "BID_COLLATION_BUFFERS_CUSTOM7", + "BID_COLLATION_BUFFERS_CUSTOM8", + "BID_COLLATION_BUFFERS_CUSTOM9", + "BID_COLLATION_BUFFERS_CUSTOM10", + "BID_COLLATION_BUFFERS_CUSTOM11", + "BID_COLLATION_BUFFERS_CUSTOM12", + "BID_COLLATION_BUFFERS_CUSTOM13", + "BID_COLLATION_BUFFERS_CUSTOM14", + "BID_COLLATION_BUFFERS_CUSTOM15", + "BID_COLLATION_BUFFERS_INCLUDES_PLAYER", + "BID_COLLATION_BUFFERS_INCLUDES_SEARCH", + "BID_COLLATION_BUFFERS_PLAYER", + "BID_COLLATION_BUFFERS_SEARCH", // malloc'd + "BID_COLLATION_BUFFERS_SEARCH_ENTRY", + "BID_COLLATION_BUFFERS_TITLE", + "BID_COLLATION_BUFFERS_URL_PLAYER", + "BID_COLLATION_BUFFERS_URL_SEARCH", + "BID_COLLATION_BUFFERS_VIDEO_ID", + "BID_COLLATION_BUFFERS_VOD_PLATFORM", + "BID_DATABASE", + "BID_ERRORS", + "BID_FILTER", + "BID_INDEX", + "BID_INDEX_BUFFERS_CATEGORY_ICONS", + "BID_INDEX_BUFFERS_CLASS", + "BID_INDEX_BUFFERS_DATA", + "BID_INDEX_BUFFERS_HEADER", + "BID_INDEX_BUFFERS_MASTER", + "BID_INDEX_BUFFERS_TEXT", + "BID_INOTIFY_EVENTS", + "BID_LINK", + "BID_MASTER", + "BID_MENU_BUFFERS_CREDITS", + "BID_MENU_BUFFERS_FILTER", + "BID_MENU_BUFFERS_FILTER_MEDIA", + "BID_MENU_BUFFERS_FILTER_TOPICS", + "BID_MENU_BUFFERS_QUOTE", + "BID_MENU_BUFFERS_REFERENCE", + "BID_NEXT_PLAYER_URL", + "BID_PLAYER_BUFFERS_MAIN", + "BID_PLAYER_BUFFERS_MENUS", + "BID_PLAYER_BUFFERS_SCRIPT", + "BID_PREVIOUS_PLAYER_URL", + "BID_PWD_STRIPPED_PATH", + "BID_QUOTE_STAGING", + "BID_RESOLVED_PATH", + "BID_SCRIPT", // malloc'd + "BID_TOPICS", + "BID_TO_PLAYER_URL", + "BID_URL", + "BID_URL_ASSET", + "BID_URL_PLAYER", + "BID_URL_SEARCH", + "BID_VIDEO_API_RESPONSE", +}; - // Advisedly universal, although could be per-project - char *RootDir; // Absolute - char *RootURL; - char *CSSDir; // Relative to Root{Dir,URL} - char *ImagesDir; // Relative to Root{Dir,URL} - char *JSDir; // Relative to Root{Dir,URL} - char *QueryString; - - // Per Project - char *ProjectID; - char *Theme; - char *DefaultMedium; - - // Per Project - Input - char *ProjectDir; // Absolute - char *TemplatesDir; // Absolute - char *TemplateSearchLocation; // Relative to TemplatesDir ??? - char *TemplatePlayerLocation; // Relative to TemplatesDir ??? - - // Per Project - Output - char *BaseDir; // Absolute - char *BaseURL; - char *SearchLocation; // Relative to Base{Dir,URL} - char *PlayerLocation; // Relative to Base{Dir,URL} - char *PlayerURLPrefix; /* NOTE(matt): This will become a full blown customisable output URL. - For now it simply replaces the ProjectID */ - // Single Edition - Input - char SingleHMMLFilePath[256]; - - // Single Edition - Output - char *OutLocation; - char *OutIntegratedLocation; -} config; +// TODO(matt): Note which buffers malloc +typedef enum +{ + BID_NULL, + BID_CHECKSUM, + BID_COLLATION_BUFFERS_CUSTOM0, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM1, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM2, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM3, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM4, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM5, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM6, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM7, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM8, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM9, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM10, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM11, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM12, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM13, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM14, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_CUSTOM15, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_INCLUDES_PLAYER, + BID_COLLATION_BUFFERS_INCLUDES_SEARCH, + BID_COLLATION_BUFFERS_PLAYER, + BID_COLLATION_BUFFERS_SEARCH, // malloc'd + BID_COLLATION_BUFFERS_SEARCH_ENTRY, + BID_COLLATION_BUFFERS_TITLE, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_URL_PLAYER, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_URL_SEARCH, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_VIDEO_ID, // NOTE(matt): Not a buffer + BID_COLLATION_BUFFERS_VOD_PLATFORM, // NOTE(matt): Not a buffer + BID_DATABASE, // File.Buffer, malloc'd + BID_ERRORS, + BID_FILTER, // malloc'd + BID_INDEX, // malloc'd + BID_INDEX_BUFFERS_CATEGORY_ICONS, + BID_INDEX_BUFFERS_CLASS, + BID_INDEX_BUFFERS_DATA, + BID_INDEX_BUFFERS_HEADER, + BID_INDEX_BUFFERS_MASTER, + BID_INDEX_BUFFERS_TEXT, + BID_INOTIFY_EVENTS, + BID_LINK, + BID_MASTER, // NOTE(matt): This shall only ever be the destination, never the source. It is also malloc'd + BID_MENU_BUFFERS_CREDITS, + BID_MENU_BUFFERS_FILTER, + BID_MENU_BUFFERS_FILTER_MEDIA, + BID_MENU_BUFFERS_FILTER_TOPICS, + BID_MENU_BUFFERS_QUOTE, + BID_MENU_BUFFERS_REFERENCE, + BID_NEXT_PLAYER_URL, + BID_PLAYER_BUFFERS_MAIN, + BID_PLAYER_BUFFERS_MENUS, + BID_PLAYER_BUFFERS_SCRIPT, + BID_PREVIOUS_PLAYER_URL, + BID_PWD_STRIPPED_PATH, + BID_QUOTE_STAGING, // malloc'd + BID_RESOLVED_PATH, + BID_SCRIPT, // malloc'd + BID_TOPICS, // File.Buffer, malloc'd + BID_TO_PLAYER_URL, + BID_URL, + BID_URL_ASSET, + BID_URL_PLAYER, + BID_URL_SEARCH, + BID_VIDEO_API_RESPONSE, + BID_COUNT, +} buffer_id; typedef struct { char *Location; char *Ptr; - char *ID; - int Size; + buffer_id ID; + uint64_t Size; + uint32_t IndentLevel; } buffer; +typedef struct +{ + char *Base; + uint64_t Length; +} string; + +int +CopyStringToBarePtr(char *Dest, string Src) +{ + for(int i = 0; i < Src.Length; ++i) + { + *Dest++ = Src.Base[i]; + } + return Src.Length; +} + +typedef struct +{ + char *Base; + char *Ptr; +} memory_page; + +typedef struct memory_pen_location +{ + memory_page *Page; + string String; + struct memory_pen_location *Prev; + struct memory_pen_location *Next; +} memory_pen_location; + +typedef enum +{ + MBT_NONE, + MBT_ASSET, + MBT_STRING, +} memory_book_type; + +typedef struct +{ + int64_t PageCount; + uint64_t PageSize; + uint64_t DataWidthInBytes; + memory_page *Pages; + memory_pen_location *Pen; + memory_book_type Type; +} memory_book; + +void +InitBook(memory_book *M, uint64_t DataWidthInBytes, uint64_t ItemsPerPage, memory_book_type Type) +{ + M->PageSize = ItemsPerPage * DataWidthInBytes; + M->Type = Type; + M->DataWidthInBytes = DataWidthInBytes; +} + +memory_page * +AddPage(memory_book *M) +{ + M->Pages = realloc(M->Pages, (M->PageCount + 1) * sizeof(memory_page)); + M->Pages[M->PageCount].Base = calloc(1, M->PageSize); + M->Pages[M->PageCount].Ptr = M->Pages[M->PageCount].Base; + ++M->PageCount; + return &M->Pages[M->PageCount - 1]; +} + +void +FreePage(memory_page *P) +{ + Free(P->Base); + P->Ptr = 0; +} + +void +FreeBook(memory_book *M) +{ + for(int i = 0; i < M->PageCount; ++i) + { + FreePage(&M->Pages[i]); + } + FreeAndResetCount(M->Pages, M->PageCount); + + if(M->Type == MBT_STRING) + { + memory_pen_location *This = M->Pen; + while(This) + { + M->Pen = M->Pen->Prev; + Free(This); + This = M->Pen; + } + } + + memory_book Zero = {}; + *M = Zero; +} + +void +FreeAndReinitialiseBook(memory_book *M) +{ + int PageSize = M->PageSize; + int Type = M->Type; + FreeBook(M); + M->PageSize = PageSize; + M->Type = Type; +} + +memory_page * +GetOrAddPageForString(memory_book *M, string S) +{ + for(int i = 0; i < M->PageCount; ++i) + { + if((M->Pages[i].Ptr - M->Pages[i].Base) + S.Length < M->PageSize) + { + return &M->Pages[i]; + } + } + return AddPage(M); +} + +memory_page * +GetOrAddPageForSize(memory_book *M, uint64_t Size) +{ + for(int i = 0; i < M->PageCount; ++i) + { + if((M->Pages[i].Ptr - M->Pages[i].Base) + Size < M->PageSize) + { + return &M->Pages[i]; + } + } + return AddPage(M); +} + +string +WriteStringInBook(memory_book *M, string S) +{ + string Result = {}; + memory_page *Page = GetOrAddPageForString(M, S); + + memory_pen_location *Pen = malloc(sizeof(memory_pen_location)); + Pen->Page = Page; + Pen->String.Base = Page->Ptr; + Pen->String.Length = S.Length; + + Pen->Prev = M->Pen; + Pen->Next = 0; + + if(M->Pen) + { + M->Pen->Next = Pen; + } + + M->Pen = Pen; + + Result.Base = Page->Ptr; + Result.Length = S.Length; + Page->Ptr += CopyStringToBarePtr(Page->Ptr, S); + return Result; +} + +typedef struct +{ + int64_t Page; + uint64_t Line; +} book_position; + +book_position +GetBookPositionFromIndex(memory_book *M, int Index) +{ + book_position Result = {}; + int ItemsPerPage = M->PageSize / M->DataWidthInBytes; + Result.Line = Index % ItemsPerPage; + Index -= Result.Line; + Result.Page = Index / ItemsPerPage; + return Result; +} + +void * +GetPlaceInBook(memory_book *M, int Index) +{ + void *Result = 0; + + book_position Position = GetBookPositionFromIndex(M, Index); + + memory_page *Page = M->Pages + Position.Page; + while((Position.Page + 1) > M->PageCount) + { + Page = AddPage(M); + } + + char *Ptr = Page->Base; + Ptr += M->DataWidthInBytes * Position.Line; + Result = Ptr; + + return Result; +} + +string +ExtendStringInBook(memory_book *M, string S) +{ + string Result = {}; + if(M->Pen) + { + Result.Length = M->Pen->String.Length + S.Length; + if(M->Pen->String.Base - M->Pen->Page->Base + Result.Length < M->PageSize) + { + M->Pen->Page->Ptr += CopyStringToBarePtr(M->Pen->Page->Ptr, S); + } + else + { + memory_pen_location Pen = *M->Pen; + M->Pen->Page->Ptr = M->Pen->String.Base; + + memory_page *Page = GetOrAddPageForString(M, Result); + M->Pen->Page = Page; + M->Pen->String.Base = Page->Ptr; + Page->Ptr += CopyStringToBarePtr(Page->Ptr, Pen.String); + Page->Ptr += CopyStringToBarePtr(Page->Ptr, S); + } + Result.Base = M->Pen->String.Base; + M->Pen->String.Length = Result.Length; + } + else + { + Result = WriteStringInBook(M, S); + } + return Result; +} + +void +ResetPen(memory_book *M) +{ + if(M->Pen) + { + if(M->Pen->Page) + { + M->Pen->String.Base = M->Pen->Page->Ptr; + } + else + { + M->Pen->Page = AddPage(M); + M->Pen->String.Base = M->Pen->Page->Base; + } + M->Pen->String.Length = 0; + } +} + +void +EraseCurrentStringFromBook(memory_book *M) +{ + M->Pen->Page->Ptr -= M->Pen->String.Length; + + if(M->Pen->Prev) + { + M->Pen->Prev->Next = M->Pen->Next; + } + if(M->Pen->Next) + { + M->Pen->Next->Prev = M->Pen->Prev; + } + + memory_pen_location *This = M->Pen; + M->Pen = M->Pen->Prev; + Free(This); +} + +void +PrintPage(memory_page *P) +{ + fprintf(stderr, "%.*s\n\n", (int)(P->Ptr - P->Base), P->Base); +} + +void +PrintBook(memory_book *M) +{ + switch(M->Type) + { + case MBT_STRING: + { + for(int i = 0; i < M->PageCount; ++i) + { + PrintPage(&M->Pages[i]); + } + } break; + case MBT_ASSET: Assert(0); break; + case MBT_NONE: Assert(0); break; + } +} + +int64_t +StringToInt(string S) +{ + int Result = 0; + for(int i = 0; i < S.Length; ++i) + { + Result = Result * 10 + S.Base[i] - '0'; + } + return Result; +} + +uint64_t +StringLength(char *String) +{ + uint64_t i = 0; + if(String) + { + while(String[i]) + { + ++i; + } + } + return i; +} + +int64_t +StringsDiffer(string A, string B) +{ + int i = 0; + while(i < A.Length && i < B.Length && A.Base[i] == B.Base[i]) { ++i; } + if(i == A.Length && i == B.Length) + { + return 0; + } + else if(i == A.Length) + { + return -1; + } + else if(i == B.Length) + { + return 1; + } + return A.Base[i] - B.Base[i]; +} + +int64_t +StringsDifferLv0(string A, char *B) +{ + if(B) + { + int i = 0; + while(i < A.Length && B[i] && A.Base[i] == B[i]) { ++i; } + if(i == A.Length && !B[i]) + { + return 0; + } + else if(i == A.Length) + { + return -1; + } + return A.Base[i] - B[i]; + } + else + { + return A.Length; + } +} + +int64_t +StringsDifferS(char *NullTerminated, buffer *NotNullTerminated) +{ + char *Ptr = NotNullTerminated->Ptr; + uint64_t Length = StringLength(NullTerminated); + int i = 0; + while(i < Length && Ptr - NotNullTerminated->Location < NotNullTerminated->Size && *NullTerminated == *Ptr) + { + ++i, ++NullTerminated, ++Ptr; + } + if(i == Length) + { + return 0; + } + bool WithinBounds = Ptr - NotNullTerminated->Location < NotNullTerminated->Size; + return WithinBounds ? *NullTerminated - *Ptr : *NullTerminated; +} + +int64_t +StringsDiffer0(char *A, char *B) +{ + //fprintf(stderr, "%c vs %c\n", *A, *B); + while(*A && *B && *A == *B) + { + //fprintf(stderr, "%c vs %c\n", *A, *B); + //fprintf(stderr, "[%d] %c\n", Location, *A); + ++A, ++B; + } + return *A - *B; +} + +int +StringsDifferCaseInsensitive(string A, string B) // NOTE(matt): Two null-terminated strings +{ + int i = 0; + while(i < A.Length && i < B.Length && + ((A.Base[i] >= 'A' && A.Base[i] <= 'Z') ? A.Base[i] + ('a' - 'A') : A.Base[i]) == + ((B.Base[i] >= 'A' && B.Base[i] <= 'Z') ? B.Base[i] + ('a' - 'A') : B.Base[i]) + ) { ++i; } + if(i == A.Length && i == B.Length) + { + return 0; + } + else if(i == A.Length) + { + return -1; + } + else if(i == B.Length) + { + return 1; + } + return ((A.Base[i] >= 'A' && A.Base[i] <= 'Z') ? A.Base[i] + ('a' - 'A') : A.Base[i]) - + ((B.Base[i] >= 'A' && B.Base[i] <= 'Z') ? B.Base[i] + ('a' - 'A') : B.Base[i]); +} + +int +StringsDiffer0CaseInsensitive(char *A, char *B) // NOTE(matt): Two null-terminated strings +{ + while(*A && *B && + ((*A >= 'A' && *A <= 'Z') ? *A + ('a' - 'A') : *A) == + ((*B >= 'A' && *B <= 'Z') ? *B + ('a' - 'A') : *B)) + { + ++A, ++B; + } + + return ((*A >= 'A' && *A <= 'Z') ? *A + ('a' - 'A') : *A) - + ((*B >= 'A' && *B <= 'Z') ? *B + ('a' - 'A') : *B); +} + +bool +StringsDifferT(char *A, // NOTE(matt): Null-terminated string + char *B, // NOTE(matt): Not null-terminated string (e.g. one mid-buffer) + char Terminator // NOTE(matt): Caller definable terminator. Pass 0 to only match on the extent of A + ) +{ + // TODO(matt): Make sure this can't crash upon reaching the end of B's buffer + int ALength = StringLength(A); + int i = 0; + while(i < ALength && A[i] && A[i] == B[i]) + { + ++i; + } + if((!Terminator && !A[i] && ALength == i) || + (!A[i] && ALength == i && (B[i] == Terminator))) + { + return FALSE; + } + else + { + return TRUE; + } +} + +bool +StringsMatch(string A, string B) +{ + int i = 0; + while(i < A.Length && i < B.Length && A.Base[i] == B.Base[i]) { ++i; } + if(i == A.Length && i == B.Length) + { + return TRUE; + } + return FALSE; +} + +bool +StringsMatchCaseInsensitive(string A, string B) +{ + int i = 0; + while(i < A.Length && i < B.Length && + ((A.Base[i] >= 'A' && A.Base[i] <= 'Z') ? A.Base[i] + ('a' - 'A') : A.Base[i]) == + ((B.Base[i] >= 'A' && B.Base[i] <= 'Z') ? B.Base[i] + ('a' - 'A') : B.Base[i])) + { + ++i; + } + if(i == A.Length && i == B.Length) + { + return TRUE; + } + return FALSE; +} + +string ExtensionStrings[] = +{ + {}, + { ".gif", 4 }, + { ".hmml", 5 }, + { ".index", 6 }, + { ".jpeg", 5 }, + { ".jpg", 4 }, + { ".png", 4 }, +}; + +typedef enum +{ + EXT_NULL, + EXT_GIF, + EXT_HMML, + EXT_INDEX, + EXT_JPEG, + EXT_JPG, + EXT_PNG, +} extension_id; + +bool +ExtensionMatches(string Path, extension_id Extension) // NOTE(matt): Extension includes the preceding "." +{ + bool Result = FALSE; + if(Path.Length >= ExtensionStrings[Extension].Length) + { + string Test = { .Base = Path.Base + Path.Length - ExtensionStrings[Extension].Length, .Length = ExtensionStrings[Extension].Length }; + if(StringsMatchCaseInsensitive(Test, ExtensionStrings[Extension])) + { + Result = TRUE; + } + } + return Result; +} + +string +Wrap0(char *String) +{ + string Result = {}; + Result.Base = String; + Result.Length = StringLength(String); + return Result; +} + +string +Wrap0i(char *S, uint64_t MaxSize) +{ + string Result = {}; + Result.Base = S; + Result.Length = MaxSize; + while(Result.Length > 0 && Result.Base[Result.Length - 1] == '\0') + { + --Result.Length; + } + return Result; +} + +void +ExtendString0(char **Dest, string Src) +{ + if(Src.Length > 0) + { + uint64_t OriginalLength = 0; + if(*Dest) { OriginalLength = StringLength(*Dest); } + uint64_t RequiredBytes = OriginalLength + Src.Length + 1; + *Dest = realloc(*Dest, RequiredBytes); + char *Ptr = *Dest + OriginalLength; + for(int i = 0; i < Src.Length; ++i) + { + *Ptr++ = Src.Base[i]; + } + *Ptr = '\0'; + } +} + +char *ColourStrings[] = +{ + "\e[0m", + "\e[0;30m", "\e[0;31m", "\e[0;32m", "\e[0;33m", "\e[0;34m", "\e[0;35m", "\e[0;36m", "\e[0;37m", + "\e[1;30m", "\e[1;31m", "\e[1;32m", "\e[1;33m", "\e[1;34m", "\e[1;35m", "\e[1;36m", "\e[1;37m", +}; + +typedef enum +{ + CS_END, + CS_BLACK, CS_RED, CS_GREEN, CS_YELLOW, CS_BLUE, CS_MAGENTA, CS_CYAN, CS_WHITE, + CS_BLACK_BOLD, CS_RED_BOLD, CS_GREEN_BOLD, CS_YELLOW_BOLD, CS_BLUE_BOLD, CS_MAGENTA_BOLD, CS_CYAN_BOLD, CS_WHITE_BOLD, +} colour_code; + +#define CS_SUCCESS CS_GREEN_BOLD +#define CS_ADDITION CS_GREEN_BOLD +#define CS_REINSERTION CS_YELLOW_BOLD +#define CS_FAILURE CS_RED_BOLD +#define CS_ERROR CS_RED_BOLD +#define CS_WARNING CS_YELLOW +#define CS_PRIVATE CS_BLUE +#define CS_ONGOING CS_MAGENTA +#define CS_COMMENT CS_BLACK_BOLD +#define CS_DELETION CS_BLACK_BOLD + +void +Colourise(colour_code C) +{ + fprintf(stderr, "%s", ColourStrings[C]); +} + +void +PrintString(string S) +{ + //fprintf(stderr, "PrintString()\n"); + fprintf(stderr, "%.*s", (int)S.Length, S.Base); +} + +void +PrintStringN(string S) +{ + //fprintf(stderr, "PrintString()\n"); + fprintf(stderr, "\n%.*s", (int)S.Length, S.Base); +} + +void +PrintStringI(string S, uint64_t Indentation) +{ + for(int i = 0; i < Indentation; ++i) + { + fprintf(stderr, " "); + } + PrintString(S); +} + +void +PrintStringC(colour_code Colour, string String) +{ + Colourise(Colour); + PrintString(String); + Colourise(CS_END); +} + +void +PrintC(colour_code Colour, char *String) +{ + Colourise(Colour); + fprintf(stderr, "%s", String); + Colourise(CS_END); +} + +#if DEBUG_PRINT_FUNCTION_NAMES +void +PrintFunctionName(char *N) +{ + PrintC(CS_MAGENTA, N); + fprintf(stderr, "\n"); +} + +void +PrintLinedFunctionName(int LineNumber, char *N) +{ + Colourise(CS_BLACK_BOLD); + fprintf(stderr, "[%i] ", LineNumber); + Colourise(CS_END); + PrintFunctionName(N); +} +#else +#define PrintFunctionName(S); +#define PrintLinedFunctionName(L, N); +#endif + +typedef enum +{ + TOKEN_NULL, + TOKEN_COMMENT_SINGLE, + TOKEN_COMMENT_MULTI_OPEN, + TOKEN_COMMENT_MULTI_CLOSE, + TOKEN_ASSIGN, + TOKEN_SEMICOLON, + TOKEN_OPEN_BRACE, + TOKEN_CLOSE_BRACE, + TOKEN_DOUBLEQUOTE, + TOKEN_NEWLINE, + + TOKEN_COMMENT, + TOKEN_STRING, // Double-quoted alphanumeric, may contain spaces + TOKEN_IDENTIFIER, // Alphanumeric + TOKEN_NUMBER, // Numeric + //TOKEN_UNHANDLED, // TODO(matt): Consider the need for this +} token_type; + +char *TokenTypeStrings[] = +{ + "", + "COMMENT_SINGLE", + "COMMENT_MULTI_OPEN", + "COMMENT_MULTI_CLOSE", + "ASSIGN", + "SEMICOLON", + "OPEN_BRACE", + "CLOSE_BRACE", + "DOUBLEQUOTE", + "NEWLINE", + + "COMMENT", + "STRING", // Double-quoted alphanumeric, may contain spaces + "IDENTIFIER", // Alphanumeric + "NUMBER", // Numeric +}; + +char *TokenStrings[] = +{ + "", + "//", + "/*", + "*/", + "=", + ";", + "{", + "}", + "\"", + "\n", + + "Comment", + "String", // Double-quoted alphanumeric, may contain spaces + "Identifier", // Alphanumeric + "Number", // Numeric +}; + +#define ArrayCount(A) sizeof(A)/sizeof(*(A)) + typedef struct { buffer Buffer; - FILE *Handle; - char Path[256]; // NOTE(matt): Could this just be a char *? - int FileSize; -} file_buffer; + char *Path; + FILE *Handle; +} file; + +typedef enum +{ + FID_NULL, + FID_METADATA, +} file_id; + +typedef struct +{ + uint64_t BytePosition; + int64_t Size; +} file_edit; + +typedef enum +{ + T_db_header_project, + T_db_entry, + T_db_block_assets, +} type_id; + +typedef struct +{ + type_id ID; + void *Ptr; + uint64_t Byte; +} file_signpost; + +typedef struct +{ + file_id ID; + file_edit Edit; + + file_signpost ProjectsBlock; + file_signpost ProjectParent; + file_signpost ProjectHeader; + file_signpost Prev; + file_signpost This; + file_signpost Next; + file_signpost AssetsBlock; +} file_signposts; + +typedef struct +{ + file File; + file_signposts Signposts; +} file_signposted; + +void +WriteFromByteToPointer(file *F, uint64_t *BytesThroughBuffer, void *Pointer) +{ + uint64_t Byte = (char *)Pointer - F->Buffer.Location; + fwrite(F->Buffer.Location + *BytesThroughBuffer, Byte - *BytesThroughBuffer, 1, F->Handle); + *BytesThroughBuffer += Byte - *BytesThroughBuffer; +} + +uint64_t +WriteFromPointerToPointer(file *F, void *From, void *To, uint64_t *BytesWritten) +{ + uint64_t BytesToWrite = (char *)To - (char *)From; + fwrite(From, BytesToWrite, 1, F->Handle); + *BytesWritten += BytesToWrite; + return BytesToWrite; +} + +uint64_t +WriteFromByteToEnd(file *F, uint64_t Byte) +{ + fwrite(F->Buffer.Location + Byte, F->Buffer.Size - Byte, 1, F->Handle); + return F->Buffer.Size - Byte; +} + +typedef struct +{ + char *String; + char *IdentifierDescription; + char *IdentifierDescription_Medium; + char *LocalVariableDescription; + + bool IdentifierDescriptionDisplayed; + bool IdentifierDescription_MediumDisplayed; + bool LocalVariableDescriptionDisplayed; +} config_identifier; + +config_identifier ConfigIdentifiers[] = +{ + { "" }, + { "allow", +"An include-rule string of the forms: \"identifier\", \"type.member\" or \"type.member.member\", etc. For example:\n\nallow = \"project.default_medium\";\n\nAdding an \"allow\" / \"deny\" rule makes the inclusion prohibitive or permissive, respectively, \ +and they cannot be mixed (i.e. only \"allow\" rules, or only \"deny\" rules)." + }, + { "art" }, + { "art_variants" }, + { "assets_root_dir", +"Absolute directory path, from where the locations of CSS, JavaScript and image files are derived (see also css_path, images_path and js_path). Setting this correctly allows Cinera to read in the \ +files for the purpose of hashing them." + }, + { "assets_root_url", +"URL from where the locations of CSS, JavaScript and image files are derived (see also css_path, images_path and js_path). Setting this correctly allows HTTP requests for these files to be fulfilled." + }, + { "base_dir", "Absolute directory path, where Cinera will generate the search page (see also search_location) and directories for the player pages (see also player_location)." }, + { "base_url", "URL where the search page (see also search_location) and player pages (see also player_location) will be located." }, + { "cache_dir", "Internal directory (i.e. no access from the wider internet) where we store errors.log and quotes retrieved from insobot." }, + { "cohost", +"The ID of a person (see also person) who cohosts a project. They will then appear in the credits menu of each entry in the project. Note that setting a cohost in the configuration \ +file credits this person for the entire project. There is no way to \"uncredit\" people in a HMML file. If a person ought not be credited for the whole project, just add them in \ +video node of the entries for which they should be credited." + }, + { "css_path", "Path relative to assets_root_dir and assets_root_url where CSS files are located." }, + { "db_location", "Absolute file path where the database file resides. If you run multiple instances of Cinera on the same machine, please ensure this db_location differs between them." }, + { "default_medium", "The ID of a medium (see also medium) which will be the default for the project. May be overridden by setting the medium in the video node of an HMML file." }, + { "deny", "See allow." }, + { "genre", "This is a setting for the future. We currently only support the \"video\" genre, which is the default." }, + { "global_search_dir", "Absolute directory path, where the global search page will be located, collating the search pages of all projects in the Cinera instance." }, + { "global_search_template", "Path of a HTML template file relative to the global_templates_dir from which the global search page will be generated." }, + { "global_search_url", "URL where the global search page will be located, collating the search pages of all projects in the Cinera instance." }, + { "global_templates_dir", "Absolute directory path, from where the global_search_template path may be derived." }, + { "global_theme", "The theme used for global pages, e.g. search page. As with all themes, its name forms the file path of a CSS file containing the style as follows: cinera__${theme}.css" }, + { "guest", +"The ID of a person (see also person) who guests in a project. They will then appear in the credits menu of each entry in the project. Note that setting a guest in the configuration \ +file credits this person for the entire project. There is no way to \"uncredit\" people in a HMML file. If a person ought not be credited for the whole project, just add them in \ +video node of the entries for which they should be credited." + }, + { "hidden", "Hidden media are filtered off by default in the player. For example, we may create an \"afk\" medium to tag portions of videos where the host is \"Away from \ +Keyboard\". These portions, filtered off, will be skipped to save the viewer sitting through them." }, + { "hmml_dir", "The input directory where Cinera will look for the project's .hmml files." }, + { "homepage", "The URL where visitors will find the person's homepage." }, + { "html_title", "The HTML \"enhanced\" version of title, which may employ HTML tags." }, + { "icon", +"The file path of an image that will appear in the credits menu beside the name of a person who may be supported via this support platform. The image shall be a 32×16 pixel sprite, with the left side \ +used when the image is not focused, and the right side when it is focused.", +"A HTML character entity used to denote the medium in the filter menu and list of indices." + }, + { "icon_disabled" }, + { "icon_focused" }, + { "icon_normal" }, + { "icon_type" }, + { "icon_variants" }, + { "ignore_privacy", +"Creators may be able to set the publication status of their content. By default Cinera respects this status by not processing any content that is private. If a creator only publishes \ +publicly, then we can set ignore_privacy to \"true\" and save the resources otherwise spent checking the privacy status."}, + { "images_path", "Path relative to assets_root_dir and assets_root_url where image files are located.", 0 }, + { "include", "The path - either absolute or relative to the containing config file - of a file to be included in the configuration." }, + { "indexer", +"The ID of a person (see also person) who indexes a project. They will then appear in the credits menu of each entry in the project. Note that setting an indexer in the configuration \ +file credits this person for the entire project. There is no way to \"uncredit\" people in a HMML file. If a person ought not be credited for the whole project, just add them in \ +video node of the entries for which they should be credited." + }, + { "js_path", "Path relative to assets_root_dir and assets_root_url where JavaScript files are located." }, + { "lineage", 0, 0, "A slash-separated string of all project IDs from the top of the family tree to the present project" }, + { "lineage_without_origin", 0, 0, "Same as the $lineage, without the first component (the $origin)" }, + { "log_level", "Possible log levels, from least to most verbose: \"emergency\", \"alert\", \"critical\", \"error\", \"warning\", \"notice\", \"informational\", \"debug\""}, + { "medium", +"In HMML an indexer may prefix a word with a colon, to mark it as a category. This category will then appear in the filter menu, for viewers to toggle on / off. A category may be either a topic (by \ +default categories are assumed to be topics) or a medium, and both use the same colon prefix. Configuring a medium is our way of stating that a category is indeed a medium, not a topic." + }, + { "name", +"The person's name as it appears in the credits menu.", +"The name of the medium as it appears in the filter menu and in a tooltip when hovering on the medium's icon in an index item." + }, + { "numbering_scheme", +"Possible numbering schemes: \"calendrical\", \"linear\", \"seasonal\". Only \"linear\" (the default) is treated specially. We assume that .hmml file names take the form: \ +\"$project$episode_number.hmml\". Under the \"linear\" scheme, Cinera tries to derive each entry's number in its project by skipping past the project ID, then replacing all underscores with full \ +stops. This derived number is then used in the search results to denote the entry." + }, + { "origin", 0, 0, "The ID of the project in our branch of the family tree which has no parent" }, + { "owner", +"The ID of the person (see also person) who owns the project. There may only be one owner, and they will appear in the credits menu as the host of each entry.", +0, +"The ID of the project's owner" + }, + { "person", +"This is someone who played a role in the projects, for which they deserve credit (see also: owner, cohost, guest, indexer).", + 0, +"The ID of the person within whose scope the variable occurs" + }, + { "player_location", "The location of the project's player pages relative to base_dir and base_url" }, + { "player_template", "Path of a HTML template file relative to the templates_dir from which the project's player pages will be generated." }, + { "privacy_check_interval", "In minutes, this sets how often to check if the privacy status of private entries has changed to \"public\"." }, + { "project", +"The work horse of the whole configuration. A config file lacking project scopes will produce no output. Notably project scopes may themselves contain project scopes.", +0, +"The ID of the project within which scope the variable occurs" + }, + { "query_string", "This string (default \"r\") enables web browsers to cache asset files. We hash those files to produce a number, which we then write to HTML files in hexadecimal format, e.g. \ +?r=a59bb130. Hashing may be disabled by setting query_string = \"\";" + }, + { "search_location", "The location of the project's search page relative to base_dir and base_url" }, + { "search_template", "Path of a HTML template file relative to the templates_dir from which the project's search page will be generated." }, + { "single_browser_tab", "Setting this to \"true\" (default \"false\") makes the search page open player pages in its own tab." }, + { "stream_platform", "This is a setting for the future. We currently only support \"twitch\" but not in any meaningful way." }, + { "stream_username", +"We use this username to retrieve quotes from insobot. If it is not set, we use the host's ID when contacting insobot. The purpose of this setting is to let us identify project owners in one way, \ +perhaps to automatically construct paths, while recognising that same person when they stream under a different username." + }, + { "support", +"Information detailing where a person may be supported, to be cited in the credits menu.", +0, +"The ID of the support platform within which scope the variable occurs" + }, + { "templates_dir", "Absolute directory path, from where the player_template and search_template path may be derived." }, + { "theme", "The theme used to style all the project's pages. As with all themes, its name forms the file path of a CSS file containing the style as follows: cinera__${theme}.css" }, + { "title", "The full name of the project" }, + { "title_list_delimiter", "Currently not implemented, probably to be removed." }, + { "title_list_end", "Currently not implemented, probably to be removed." }, + { "title_list_prefix", "Currently not implemented, probably to be removed." }, + { "title_list_suffix", "Currently not implemented, probably to be removed."} , + { "title_suffix", "Currently not implemented, probably to be removed." }, + { "unit", "This works in conjunction with the numbering_scheme. It is a freely-configurable string - e.g. \"Day\", \"Session\", \"Episode\", \"Meeting\" - which is written on the search page, \ +preceding the derived number of each entry. If the unit is not set, then the entries will not be numbered." }, + { "vod_platform", "This is a setting more for the future. We currently only support \"youtube\" for the purposes of generating the player page. But an additional use of the vod_platform is as \ +a template tag (see also Templating)." }, + { "url", "The URL where viewers may support the person, e.g. their page on a crowd funding site, the \"pledge\" page on their own website." }, +}; + +typedef enum +{ + IDENT_NULL, + IDENT_ALLOW, + IDENT_ART, + IDENT_ART_VARIANTS, + IDENT_ASSETS_ROOT_DIR, + IDENT_ASSETS_ROOT_URL, + IDENT_BASE_DIR, + IDENT_BASE_URL, + IDENT_CACHE_DIR, + IDENT_COHOST, + IDENT_CSS_PATH, + IDENT_DB_LOCATION, + IDENT_DEFAULT_MEDIUM, + IDENT_DENY, + IDENT_GENRE, + IDENT_GLOBAL_SEARCH_DIR, + IDENT_GLOBAL_SEARCH_TEMPLATE, + IDENT_GLOBAL_SEARCH_URL, + IDENT_GLOBAL_TEMPLATES_DIR, + IDENT_GLOBAL_THEME, + IDENT_GUEST, + IDENT_HIDDEN, + IDENT_HMML_DIR, + IDENT_HOMEPAGE, + IDENT_HTML_TITLE, + IDENT_ICON, + IDENT_ICON_DISABLED, + IDENT_ICON_FOCUSED, + IDENT_ICON_NORMAL, + IDENT_ICON_TYPE, + IDENT_ICON_VARIANTS, + IDENT_IGNORE_PRIVACY, + IDENT_IMAGES_PATH, + IDENT_INCLUDE, + IDENT_INDEXER, + IDENT_JS_PATH, + IDENT_LINEAGE, + IDENT_LINEAGE_WITHOUT_ORIGIN, + IDENT_LOG_LEVEL, + IDENT_MEDIUM, + IDENT_NAME, + IDENT_NUMBERING_SCHEME, + IDENT_ORIGIN, + IDENT_OWNER, + IDENT_PERSON, + IDENT_PLAYER_LOCATION, + IDENT_PLAYER_TEMPLATE, + IDENT_PRIVACY_CHECK_INTERVAL, + IDENT_PROJECT, + IDENT_QUERY_STRING, + IDENT_SEARCH_LOCATION, + IDENT_SEARCH_TEMPLATE, + IDENT_SINGLE_BROWSER_TAB, + IDENT_STREAM_PLATFORM, + IDENT_STREAM_USERNAME, + IDENT_SUPPORT, + IDENT_TEMPLATES_DIR, + IDENT_THEME, + IDENT_TITLE, + IDENT_TITLE_LIST_DELIMITER, + IDENT_TITLE_LIST_END, + IDENT_TITLE_LIST_PREFIX, + IDENT_TITLE_LIST_SUFFIX, + IDENT_TITLE_SUFFIX, + IDENT_UNIT, + IDENT_VOD_PLATFORM, + IDENT_URL, + IDENT_COUNT, +} config_identifier_id; + +void +Indent(uint64_t Indent) +{ + for(int i = 0; i < INDENT_WIDTH * Indent; ++i) + { + fprintf(stderr, " "); + } +} + +void +IndentedCarriageReturn(int IndentationLevel) +{ + fprintf(stderr, "\n"); + Indent(IndentationLevel); +} + +void +NewParagraph(int IndentationLevel) +{ + fprintf(stderr, "\n"); + IndentedCarriageReturn(IndentationLevel); +} + +void +NewSection(char *Title, int *IndentationLevel) +{ + IndentedCarriageReturn(*IndentationLevel); + if(Title) + { + fprintf(stderr, "%s:", Title); + } + ++*IndentationLevel; + IndentedCarriageReturn(*IndentationLevel); +} + +void +EndSection(int *IndentationLevel) +{ + --*IndentationLevel; +} + +int +GetTerminalColumns(void) +{ + struct winsize TermDim; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &TermDim); + return TermDim.ws_col; +} + +void +ClearTerminal(void) +{ + fprintf(stderr, "\033[2J"); +} + +void +TypesetString(int CurrentColumn, string String) +{ + if(String.Length > 0) + { + int TermCols = GetTerminalColumns(); + int AvailableCharacters = TermCols - CurrentColumn; + int CharactersToWrite = String.Length; + string Pen = { .Base = String.Base + String.Length - CharactersToWrite, + .Length = MIN(String.Length, AvailableCharacters) }; + + int Length = Pen.Length; + while(Length > 0 && Length < String.Length - (String.Length - CharactersToWrite) && Pen.Base[Length] != ' ') + { + --Length; + } + bool SplitAtWhitespace = FALSE; + if(Length > 0) + { + if(Length < String.Length - (String.Length - CharactersToWrite) && Pen.Base[Length] == ' ') + { + SplitAtWhitespace = TRUE; + } + Pen.Length = Length; + } + + for(int i = 0; i < Pen.Length; ++i) + { + if(Pen.Base[i] == '\n') + { + Pen.Length = i; + SplitAtWhitespace = TRUE; + break; + } + } + PrintString(Pen); + CharactersToWrite -= Pen.Length; + if(SplitAtWhitespace) + { + --CharactersToWrite; + } + + while(CharactersToWrite > 0) + { + fprintf(stderr, "\n"); + for(int i = 0; i < CurrentColumn; ++i) + { + fprintf(stderr, " "); + } + + string Pen = { .Base = String.Base + String.Length - CharactersToWrite, + .Length = MIN(CharactersToWrite, AvailableCharacters) }; + + Length = Pen.Length; + while(Length > 0 && Length < String.Length - (String.Length - CharactersToWrite) && Pen.Base[Length] != ' ') + { + --Length; + } + SplitAtWhitespace = FALSE; + if(Length > 0) + { + if(Length < String.Length - (String.Length - CharactersToWrite) && Pen.Base[Length] == ' ') + { + SplitAtWhitespace = TRUE; + } + Pen.Length = Length; + } + + for(int i = 0; i < Pen.Length; ++i) + { + if(Pen.Base[i] == '\n') + { + Pen.Length = i; + SplitAtWhitespace = TRUE; + break; + } + } + PrintString(Pen); + CharactersToWrite -= Pen.Length; + if(SplitAtWhitespace) + { + --CharactersToWrite; + } + } + } +} + +typedef struct +{ + token_type Type; + string Content; + int64_t int64_t; + uint64_t LineNumber; +} token; + +typedef struct +{ + file File; + uint64_t Count; + token *Token; + uint64_t CurrentIndex; + uint64_t CurrentLine; +} tokens; + +typedef struct +{ + tokens *Tokens; + uint64_t Count; +} tokens_list; + +char *ArtVariantStrings[] = +{ + "light_normal", + "light_focused", + "light_disabled", + "dark_normal", + "dark_focused", + "dark_disabled", +}; + +typedef enum +{ + AVS_LIGHT_NORMAL, + AVS_LIGHT_FOCUSED, + AVS_LIGHT_DISABLED, + AVS_DARK_NORMAL, + AVS_DARK_FOCUSED, + AVS_DARK_DISABLED, + AVS_COUNT, +} art_variant_shifters; + +typedef struct +{ + char *String; + uint64_t Mapping:63; + uint64_t Unused:1; +} config_art_variant; + +config_art_variant ConfigArtVariants[] = +{ + { }, + { "light_normal", 1 << AVS_LIGHT_NORMAL }, + { "light_focused", 1 << AVS_LIGHT_FOCUSED }, + { "light_disabled", 1 << AVS_LIGHT_DISABLED }, + { "light", ((1 << AVS_LIGHT_NORMAL) | (1 << AVS_LIGHT_FOCUSED) | (1 << AVS_LIGHT_DISABLED)) }, + { "dark_normal", 1 << AVS_DARK_NORMAL }, + { "dark_focused", 1 << AVS_DARK_FOCUSED }, + { "dark_disabled", 1 << AVS_DARK_DISABLED }, + { "dark", ((1 << AVS_DARK_NORMAL) | (1 << AVS_DARK_FOCUSED) | (1 << AVS_DARK_DISABLED)) }, + { "normal", ((1 << AVS_LIGHT_NORMAL) | (1 << AVS_DARK_NORMAL)) }, + { "focused", ((1 << AVS_LIGHT_FOCUSED) | (1 << AVS_DARK_FOCUSED)) }, + { "disabled", ((1 << AVS_LIGHT_NORMAL) | (1 << AVS_DARK_NORMAL)) }, + { "all", ((1 << AVS_LIGHT_NORMAL) | (1 << AVS_LIGHT_FOCUSED) | (1 << AVS_LIGHT_DISABLED) | (1 << AVS_DARK_NORMAL) | (1 << AVS_DARK_FOCUSED) | (1 << AVS_DARK_DISABLED)) }, +}; + +typedef enum +{ + CAV_DEFAULT_UNSET, + CAV_LIGHT_NORMAL, + CAV_LIGHT_FOCUSED, + CAV_LIGHT_DISABLED, + CAV_LIGHT, + CAV_DARK_NORMAL, + CAV_DARK_FOCUSED, + CAV_DARK_DISABLED, + CAV_DARK, + CAV_NORMAL, + CAV_FOCUSED, + CAV_DISABLED, + CAV_ALL, + CAV_COUNT, +} config_art_variant_id; + +typedef enum +{ + S_ERROR, + S_WARNING, +} severity; + +void +ConfigErrorFilenameAndLineNumber(char *Filename, uint64_t LineNumber, severity Severity) +{ + fprintf(stderr, "\n" + "┌─ "); + switch(Severity) + { + case S_ERROR: PrintC(CS_ERROR, "Config error"); break; + case S_WARNING: PrintC(CS_WARNING, "Config warning"); break; + } + if(Filename) + { + fprintf(stderr, " on line "); + Colourise(CS_BLUE_BOLD); + fprintf(stderr, "%lu", LineNumber); + Colourise(CS_END); + fprintf(stderr, " of "); + PrintC(CS_CYAN, Filename); + } + fprintf(stderr, "\n" + "└─╼ "); +} + +// TODO(matt): Get more errors going through Error() + +void +ConfigError(char *Filename, uint64_t LineNumber, severity Severity, char *Message, string *Received) +{ + ConfigErrorFilenameAndLineNumber(Filename, LineNumber, Severity); + fprintf(stderr, + "%s", Message); + if(Received) + { + PrintStringC(CS_MAGENTA_BOLD, *Received); + } + fprintf(stderr, "\n"); +} + +void +ConfigErrorUnset(config_identifier_id FieldID) +{ + ConfigErrorFilenameAndLineNumber(0, 0, S_ERROR); + fprintf(stderr, + "Unset %s\n", ConfigIdentifiers[FieldID].String); +} + +void +ConfigFileIncludeError(char *Filename, uint64_t LineNumber, string Path) +{ + ConfigErrorFilenameAndLineNumber(Filename, LineNumber, S_WARNING); + fprintf(stderr, + "Included file could not be opened (%s): ", strerror(errno)); + PrintStringC(CS_MAGENTA_BOLD, Path); + fprintf(stderr, "\n"); +} + +void +ConfigErrorSizing(char *Filename, uint64_t LineNumber, config_identifier_id FieldID, string *Received, uint64_t MaxSize) +{ + ConfigErrorFilenameAndLineNumber(Filename, LineNumber, S_ERROR); + fprintf(stderr, "%s value is too long (%lu/%lu characters): ", ConfigIdentifiers[FieldID].String, Received->Length, MaxSize); + PrintStringC(CS_MAGENTA_BOLD, *Received); + fprintf(stderr, "\n"); +} + +void +ConfigErrorInt(char *Filename, uint64_t LineNumber, severity Severity, char *Message, uint64_t Number) +{ + ConfigErrorFilenameAndLineNumber(Filename, LineNumber, Severity); + fprintf(stderr, + "%s%s%lu%s\n", Message, ColourStrings[CS_BLUE_BOLD], Number, ColourStrings[CS_END]); +} + +void +ConfigErrorExpectation(tokens *T, token_type GreaterExpectation, token_type LesserExpectation) +{ + ConfigErrorFilenameAndLineNumber(T->File.Path, T->Token[T->CurrentIndex].LineNumber, S_ERROR); + fprintf(stderr, + "Syntax error: Received "); + if(T->Token[T->CurrentIndex].Content.Base) + { + PrintStringC(CS_RED, T->Token[T->CurrentIndex].Content); + } + else + { + fprintf(stderr, "%s%ld%s", ColourStrings[CS_BLUE_BOLD], T->Token[T->CurrentIndex].int64_t, ColourStrings[CS_END]); + } + + fprintf(stderr, + " but Expected "); + PrintStringC(CS_GREEN, Wrap0(TokenStrings[GreaterExpectation])); + if(LesserExpectation) + { + fprintf(stderr, + " or "); + PrintStringC(CS_GREEN, Wrap0(TokenStrings[LesserExpectation])); + } + fprintf(stderr, + "\n"); +} + +typedef enum +{ + // NOTE(matt): https://tools.ietf.org/html/rfc5424#section-6.2.1 + LOG_EMERGENCY, + LOG_ALERT, + LOG_CRITICAL, + LOG_ERROR, + LOG_WARNING, + LOG_NOTICE, + LOG_INFORMATIONAL, + LOG_DEBUG, + LOG_COUNT, +} log_level; + +char *LogLevelStrings[] = +{ + "emergency", + "alert", + "critical", + "error", + "warning", + "notice", + "informational", + "debug", +}; + +log_level +GetLogLevelFromString(char *Filename, token *T) +{ + for(int i = 0; i < LOG_COUNT; ++i) + { + if(!StringsDifferLv0(T->Content, LogLevelStrings[i])) { return i; } + } + + ConfigError(Filename, T->LineNumber, S_ERROR, "Unknown log level: ", &T->Content); + fprintf(stderr, " Valid log levels:\n"); + for(int i = 0; i < LOG_COUNT; ++i) + { + fprintf(stderr, " %s\n", LogLevelStrings[i]); + } + + return LOG_COUNT; +} + +void +FreeBuffer(buffer *Buffer) +{ + Free(Buffer->Location); + Buffer->Ptr = 0; + Buffer->Size = 0; + //Buffer->ID = 0; + Buffer->IndentLevel = 0; +} char *AssetTypeNames[] = { @@ -213,94 +1807,285 @@ char *AssetTypeNames[] = "JavaScript" }; -enum +typedef enum { ASSET_GENERIC, ASSET_CSS, ASSET_IMG, ASSET_JS, ASSET_TYPE_COUNT -} asset_types; +} asset_type; -typedef struct asset +char *GenreStrings[] = { - int32_t Hash; - enum8(asset_types) Type; - char Filename[MAX_ASSET_FILENAME_LENGTH + 1]; - int32_t FilenameAt:29; - int32_t Known:1; - int32_t OffsetLandmarks:1; - int32_t DeferredUpdate:1; - uint32_t SearchLandmarkCapacity; - uint32_t SearchLandmarkCount; - uint32_t *SearchLandmark; - uint32_t PlayerLandmarkCapacity; - uint32_t PlayerLandmarkCount; - uint32_t *PlayerLandmark; -} asset; - -asset BuiltinAssets[] = -{ - { 0, ASSET_CSS, "cinera.css" }, - { 0, ASSET_CSS }, // NOTE(matt): .Filename set by InitBuiltinAssets() - { 0, ASSET_CSS, "cinera_topics.css" }, - { 0, ASSET_IMG, "cinera_icon_filter.png" }, - { 0, ASSET_JS, "cinera_search.js" }, - { 0, ASSET_JS, "cinera_player_pre.js" }, - { 0, ASSET_JS, "cinera_player_post.js" }, + "video", }; -enum +typedef enum { - ASSET_CSS_CINERA, - ASSET_CSS_THEME, - ASSET_CSS_TOPICS, - ASSET_IMG_FILTER, - ASSET_JS_SEARCH, - ASSET_JS_PLAYER_PRE, - ASSET_JS_PLAYER_POST, - BUILTIN_ASSETS_COUNT, -} builtin_assets; + GENRE_VIDEO, + GENRE_COUNT, +} genre; -typedef struct +genre +GetGenreFromString(char *Filename, token *T) { - int Count; - int Capacity; - asset *Asset; -} assets; + for(int i = 0; i < GENRE_COUNT; ++i) + { + if(!StringsDifferLv0(T->Content, GenreStrings[i])) { return i; } + } -enum -{ - WT_HMML, - WT_ASSET -} watch_types; + ConfigError(Filename, T->LineNumber, S_ERROR, "Unknown genre: ", &T->Content); + fprintf(stderr, " Valid genres:\n"); + for(int i = 0; i < GENRE_COUNT; ++i) + { + fprintf(stderr, " %s\n", GenreStrings[i]); + } -typedef struct -{ - int Descriptor; - enum8(watch_types) Type; - char Path[MAX_ROOT_DIR_LENGTH + 1 + MAX_RELATIVE_ASSET_LOCATION_LENGTH]; -} watch_handle; + return GENRE_COUNT; +} -typedef struct +char *NumberingSchemeStrings[] = { - int Count; - int Capacity; - watch_handle *Handle; -} watch_handles; + "calendrical", + "linear", + "seasonal", +}; + +typedef enum +{ + NS_CALENDRICAL, + NS_LINEAR, + NS_SEASONAL, + NS_COUNT, +} numbering_scheme; + +numbering_scheme +GetNumberingSchemeFromString(char *Filename, token *T) +{ + for(int i = 0; i < NS_COUNT; ++i) + { + if(!StringsDifferLv0(T->Content, NumberingSchemeStrings[i])) { return i; } + } + + ConfigError(Filename, T->LineNumber, S_ERROR, "Unknown numbering scheme: ", &T->Content); + fprintf(stderr, " Valid numbering schemes:\n"); + for(int i = 0; i < NS_COUNT; ++i) + { + fprintf(stderr, " %s\n", NumberingSchemeStrings[i]); + } + + return NS_COUNT; +} + +bool +GetBoolFromString(char *Filename, token *T) +{ + if(!StringsDifferLv0(T->Content, "true") || + !StringsDifferLv0(T->Content, "True") || + !StringsDifferLv0(T->Content, "TRUE") || + !StringsDifferLv0(T->Content, "yes")) + { + return TRUE; + } + else if(!StringsDifferLv0(T->Content, "false") || + !StringsDifferLv0(T->Content, "False") || + !StringsDifferLv0(T->Content, "FALSE") || + !StringsDifferLv0(T->Content, "no")) + { + return FALSE; + } + else + { + ConfigError(Filename, T->LineNumber, S_ERROR, "Unknown boolean: ", &T->Content); + fprintf(stderr, " Valid booleans:\n"); + PrintC(CS_GREEN, " true\n"); + PrintC(CS_GREEN, " True\n"); + PrintC(CS_GREEN, " TRUE\n"); + PrintC(CS_GREEN, " yes\n"); + PrintC(CS_RED, " false\n"); + PrintC(CS_RED, " False\n"); + PrintC(CS_RED, " FALSE\n"); + PrintC(CS_RED, " no\n"); + return -1; + } +} + +/* + // NOTE(matt): Sprite stuff + art = "riscy_sprite.png"; + art_variants = "light_normal light_focused light_disabled"; + art_variants = "light"; + art_variants = "dark_normal dark_focused dark_disabled"; + art_variants = "dark"; + art_variants = "light_normal dark_normal"; + art_variants = "normal"; + art_variants = "all"; + + //og_image = "*.png"; + + icon = ""; // all + icon_type = "textual"; // or "graphical" + icon_variants = "all"; // for "graphical" icons, same as art_variants + + icon_normal = ""; // fills the whole normal row + icon_focused = ""; // fills the whole focused row + icon_disabled = ""; // fills the whole disabled row + + // NOTE(matt): In the future, allow setting art and graphical icons in this manner, for Cinera itself to compose the sprite + art_light_normal = "*.png"; + art_light_focused = "*.png"; + art_light_disabled = "*.png"; + art_dark_normal = "*.png"; + art_dark_focused = "*.png"; + art_dark_disabled = "*.png"; + icon_light_normal = "*.png"; + icon_light_focused = "*.png"; + icon_light_disabled = "*.png"; + icon_dark_normal = "*.png"; + icon_dark_focused = "*.png"; + icon_dark_disabled = "*.png"; + // + + // NOTE(matt): icon, with icon_type ("graphical" or "textual") + // projects, media and support + // if "graphical", one file with icon_variants + // if "textual", one or many texts: icon_normal, icon_focused, icon_disabled + // art, may only be graphical + // projects and entries +*/ + +uint64_t +GetArtVariantFromString(string S) +{ + uint64_t Result = 0; + for(int i = 0; i < CAV_COUNT; ++i) + { + config_art_variant *This = ConfigArtVariants + i; + if(StringsMatch(S, Wrap0(This->String))) + { + Result = This->Mapping; + } + } + return Result; +} + +uint64_t +SkipWhitespaceS(string *S, uint64_t Position) +{ + while(Position < S->Length && S->Base[Position] && + (S->Base[Position] == ' ' || + S->Base[Position] == '\n' || + S->Base[Position] == '\t')) + { + ++Position; + } + return Position; +} + +bool +IsValidIdentifierCharacter(char C) +{ + return ((C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') || C == '_' || C == '-'); +} + +bool +IsNumber(char C) +{ + return (C >= '0' && C <= '9'); +} + +int64_t +ParseArtVariantsString(char *Filename, token *ArtVariantsString) +{ + uint64_t Result = 0; + bool Valid = TRUE; + + int i = SkipWhitespaceS(&ArtVariantsString->Content, 0); + for(; i < ArtVariantsString->Content.Length; i = SkipWhitespaceS(&ArtVariantsString->Content, i)) + { + if(IsValidIdentifierCharacter(ArtVariantsString->Content.Base[i])) + { + string S = {}; + S.Base = ArtVariantsString->Content.Base + i; + for(; i < ArtVariantsString->Content.Length && IsValidIdentifierCharacter(ArtVariantsString->Content.Base[i]); ++i) + { + ++S.Length; + } + uint64_t Variant = GetArtVariantFromString(S); + if(Variant != 0) + { + Result |= Variant; + } + else + { + ConfigError(Filename, ArtVariantsString->LineNumber, S_WARNING, "Unknown art_variant: ", &S); + Valid = FALSE; + } + } + } + + if(!Valid) + { + fprintf(stderr, " Valid variant_types:\n"); + int FirstValidVariant = 1; + for(int i = FirstValidVariant; i < CAV_COUNT; ++i) + { + fprintf(stderr, " %s\n", ConfigArtVariants[i].String); + } + Result = -1; + } + + return Result; +} + +char *IconTypeStrings[] = +{ + 0, + "graphical", + "textual", +}; + +typedef enum +{ + IT_DEFAULT_UNSET, + IT_GRAPHICAL, + IT_TEXTUAL, + IT_COUNT, +} icon_type; + +icon_type +GetIconTypeFromString(char *Filename, token *T) +{ + for(int i = 0; i < IT_COUNT; ++i) + { + if(!StringsDifferLv0(T->Content, IconTypeStrings[i])) { return i; } + } + + ConfigError(Filename, T->LineNumber, S_ERROR, "Unknown icon_type: ", &T->Content); + fprintf(stderr, " Valid icon_types:\n"); + int FirstValidIconType = 1; + for(int i = FirstValidIconType; i < IT_COUNT; ++i) + { + fprintf(stderr, " %s\n", IconTypeStrings[i]); + } + + return NS_COUNT; +} // DBVersion 1 typedef struct { unsigned int DBVersion; version AppVersion; version HMMLVersion; unsigned int EntryCount; } db_header1; typedef struct { int Size; char BaseFilename[32]; } db_entry1; -typedef struct { file_buffer File; file_buffer Metadata; db_header1 Header; db_entry1 Entry; } database1; +typedef struct { file File; file Metadata; db_header1 Header; db_entry1 Entry; } database1; // // DBVersion 2 typedef struct { unsigned int DBVersion; version AppVersion; version HMMLVersion; unsigned int EntryCount; char SearchLocation[32]; char PlayerLocation[32]; } db_header2; typedef db_entry1 db_entry2; -typedef struct { file_buffer File; file_buffer Metadata; db_header2 Header; db_entry2 Entry; } database2; +typedef struct { file File; file Metadata; db_header2 Header; db_entry2 Entry; } database2; // +#define MAX_PLAYER_URL_PREFIX_LENGTH 16 // TODO(matt): Increment CINERA_DB_VERSION! typedef struct { @@ -337,8 +2122,8 @@ typedef struct typedef struct { - file_buffer File; - file_buffer Metadata; + file File; + file Metadata; db_header3 Header; db_entry3 Entry; } database3; @@ -362,12 +2147,12 @@ typedef struct { uint32_t BlockID; // 'NTRY' uint16_t Count; - char ProjectID[MAX_PROJECT_ID_LENGTH + 1]; - char ProjectName[MAX_PROJECT_NAME_LENGTH + 1]; - char BaseURL[MAX_BASE_URL_LENGTH + 1]; - char SearchLocation[MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1]; - char PlayerLocation[MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1]; - char PlayerURLPrefix[MAX_PLAYER_URL_PREFIX_LENGTH + 1]; // TODO(matt): Replace this with the OutputPath, when we add that + char ProjectID[MAX_PROJECT_ID_LENGTH]; + char ProjectName[MAX_PROJECT_NAME_LENGTH]; + char BaseURL[MAX_BASE_URL_LENGTH]; + char SearchLocation[MAX_RELATIVE_PAGE_LOCATION_LENGTH]; + char PlayerLocation[MAX_RELATIVE_PAGE_LOCATION_LENGTH]; + char PlayerURLPrefix[MAX_PLAYER_URL_PREFIX_LENGTH]; // TODO(matt): Replace this with the OutputPath, when we add that } db_header_entries4; typedef db_entry3 db_entry4; @@ -376,19 +2161,19 @@ typedef struct { uint32_t BlockID; // 'ASET' uint16_t Count; - char RootDir[MAX_ROOT_DIR_LENGTH + 1]; - char RootURL[MAX_ROOT_URL_LENGTH + 1]; - char CSSDir[MAX_RELATIVE_ASSET_LOCATION_LENGTH + 1]; - char ImagesDir[MAX_RELATIVE_ASSET_LOCATION_LENGTH + 1]; - char JSDir[MAX_RELATIVE_ASSET_LOCATION_LENGTH + 1]; + char RootDir[MAX_ROOT_DIR_LENGTH]; + char RootURL[MAX_ROOT_URL_LENGTH]; + char CSSDir[MAX_RELATIVE_ASSET_LOCATION_LENGTH]; + char ImagesDir[MAX_RELATIVE_ASSET_LOCATION_LENGTH]; + char JSDir[MAX_RELATIVE_ASSET_LOCATION_LENGTH]; } db_header_assets4; typedef struct { int32_t Hash; uint32_t LandmarkCount; - enum8(asset_types) Type; - char Filename[MAX_ASSET_FILENAME_LENGTH + 1]; + enum8(asset_type) Type; + char Filename[MAX_ASSET_FILENAME_LENGTH]; } db_asset4; typedef struct @@ -399,8 +2184,8 @@ typedef struct typedef struct { - file_buffer File; - file_buffer Metadata; + file File; + file Metadata; db_header4 Header; db_header_entries4 EntriesHeader; @@ -409,73 +2194,208 @@ typedef struct db_asset4 Asset; db_landmark4 Landmark; } database4; -#pragma pack(pop) -#define CINERA_DB_VERSION 4 - -#define db_header db_header4 -#define db_header_entries db_header_entries4 -#define db_entry db_entry4 -#define db_header_assets db_header_assets4 -#define db_asset db_asset4 -#define db_landmark db_landmark4 -#define database database4 -// TODO(matt): Increment CINERA_DB_VERSION! - -// NOTE(matt): Globals -arena MemoryArena; -config Config; -assets Assets; -int inotifyInstance; -watch_handles WatchHandles; -database DB; -time_t LastPrivacyCheck; -time_t LastQuoteFetch; -// - -enum -{ - PAGE_TYPE_SEARCH = -1, -} page_type_indices; +// Database 5 +typedef db_header4 db_header5; typedef struct { - buffer IncludesSearch; - buffer SearchEntry; - buffer Search; // NOTE(matt): This buffer is malloc'd separately, rather than claimed from the memory_arena - buffer IncludesPlayer; - buffer Menus; - buffer Player; - buffer ScriptPlayer; + uint32_t BlockID; // 'PROJ' // NOTE(matt): This corresponds to the old NTRY block + char GlobalSearchDir[MAX_BASE_DIR_LENGTH]; + char GlobalSearchURL[MAX_BASE_URL_LENGTH]; + uint64_t Count; + char Reserved[244]; +} db_block_projects5; - char Custom0[MAX_CUSTOM_SNIPPET_SHORT_LENGTH + 1]; - char Custom1[MAX_CUSTOM_SNIPPET_SHORT_LENGTH + 1]; - char Custom2[MAX_CUSTOM_SNIPPET_SHORT_LENGTH + 1]; - char Custom3[MAX_CUSTOM_SNIPPET_SHORT_LENGTH + 1]; - char Custom4[MAX_CUSTOM_SNIPPET_SHORT_LENGTH + 1]; - char Custom5[MAX_CUSTOM_SNIPPET_SHORT_LENGTH + 1]; - char Custom6[MAX_CUSTOM_SNIPPET_SHORT_LENGTH + 1]; - char Custom7[MAX_CUSTOM_SNIPPET_SHORT_LENGTH + 1]; - char Custom8[MAX_CUSTOM_SNIPPET_SHORT_LENGTH + 1]; - char Custom9[MAX_CUSTOM_SNIPPET_SHORT_LENGTH + 1]; - char Custom10[MAX_CUSTOM_SNIPPET_SHORT_LENGTH + 1]; - char Custom11[MAX_CUSTOM_SNIPPET_SHORT_LENGTH + 1]; - char Custom12[MAX_CUSTOM_SNIPPET_LONG_LENGTH + 1]; - char Custom13[MAX_CUSTOM_SNIPPET_LONG_LENGTH + 1]; - char Custom14[MAX_CUSTOM_SNIPPET_LONG_LENGTH + 1]; - char Custom15[MAX_CUSTOM_SNIPPET_LONG_LENGTH + 1]; +char *SpecialAssetIndexStrings[] = +{ + "SAI_TEXTUAL", + "SAI_UNSET", +}; - char ProjectID[MAX_PROJECT_ID_LENGTH + 1]; - char ProjectName[MAX_PROJECT_NAME_LENGTH + 1]; - char Theme[MAX_PROJECT_NAME_LENGTH + 1]; - char Title[MAX_TITLE_LENGTH + 1]; - char URLSearch[MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1]; - char URLPlayer[MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_PLAYER_URL_PREFIX_LENGTH + MAX_BASE_FILENAME_LENGTH + 1]; - char VideoID[16]; - char VODPlatform[16]; -} buffers; +typedef enum +{ + SAI_TEXTUAL = -2, + SAI_UNSET = -1, +} special_asset_index; -enum +typedef struct +{ + char ID[MAX_PROJECT_ID_LENGTH]; + char Title[MAX_PROJECT_NAME_LENGTH]; + char BaseDir[MAX_BASE_DIR_LENGTH]; + char BaseURL[MAX_BASE_URL_LENGTH]; + char SearchLocation[MAX_RELATIVE_PAGE_LOCATION_LENGTH]; + char PlayerLocation[MAX_RELATIVE_PAGE_LOCATION_LENGTH]; + char Theme[MAX_THEME_LENGTH]; + char Unit[MAX_UNIT_LENGTH]; + enum32(special_asset_index) ArtIndex; + enum32(special_asset_index) IconIndex; + uint64_t EntryCount; + uint64_t ChildCount; + char Reserved[24]; +} db_header_project5; + +typedef struct +{ + char HMMLBaseFilename[MAX_BASE_FILENAME_LENGTH]; + char OutputLocation[MAX_ENTRY_OUTPUT_LENGTH]; + char Title[MAX_TITLE_LENGTH]; + unsigned short int Size; + link_insertion_offsets LinkOffsets; + enum32(special_asset_index) ArtIndex; + char Reserved[46]; +} db_entry5; + +typedef struct db_block_assets5 +{ + uint32_t BlockID; // 'ASET' + uint16_t Count; + char RootDir[MAX_ROOT_DIR_LENGTH]; + char RootURL[MAX_ROOT_URL_LENGTH]; + char CSSDir[MAX_RELATIVE_ASSET_LOCATION_LENGTH]; + char ImagesDir[MAX_RELATIVE_ASSET_LOCATION_LENGTH]; + char JSDir[MAX_RELATIVE_ASSET_LOCATION_LENGTH]; + char Reserved[154]; +} db_block_assets5; + +typedef struct +{ + char Filename[MAX_ASSET_FILENAME_LENGTH]; + enum8(asset_type) Type; + int32_t Hash; + uint32_t LandmarkCount; + uint64_t Associated:1; + uint64_t Variants:63; + uint32_t Width; + uint32_t Height; + char Reserved[39]; +} db_asset5; + +// TODO(matt): Consider the required sizes of these people +typedef struct +{ + int32_t Generation; + int32_t Index; +} db_project_index5; + +typedef struct +{ + db_project_index5 Project; + int32_t EntryIndex; + uint32_t Position; +} db_landmark5; + +typedef struct +{ + file File; + file_signposted Metadata; + + db_header5 Header; + db_block_projects5 ProjectsBlock; + db_header_project5 ProjectHeader; + db_entry5 Entry; + db_block_assets5 AssetsBlock; + db_asset5 Asset; + db_landmark5 Landmark; +} database5; + +// +// // +#pragma pack(pop) + +#define CINERA_DB_VERSION 5 + +#define db_header db_header5 +#define db_block_projects db_block_projects5 +#define db_header_project db_header_project5 +#define db_entry db_entry5 +#define db_block_assets db_block_assets5 +#define db_asset db_asset5 +#define db_project_index db_project_index5 +#define db_landmark db_landmark5 +#define database database5 +// TODO(matt): Increment CINERA_DB_VERSION! + +typedef struct +{ + buffer_id BufferID; + uint32_t Offset; +} landmark; + +typedef struct +{ + db_project_index ProjectIndex; + config_identifier_id Type; // IDENT_ART or IDENT_ICON +} asset_association; + +typedef struct +{ + uint32_t Width; + uint32_t Height; +} vec2; + +typedef struct +{ + vec2 TileDim; + int32_t XLight; + int32_t XDark; + int32_t YNormal; + int32_t YFocused; + int32_t YDisabled; +} sprite; + +typedef struct asset +{ + asset_type Type; + char Filename[MAX_ASSET_FILENAME_LENGTH]; + int32_t Hash; + vec2 Dimensions; + sprite Sprite; + uint64_t Variants:63; + uint64_t Associated:1; + int32_t FilenameAt:29; + int32_t Known:1; + int32_t OffsetLandmarks:1; + int32_t DeferredUpdate:1; + uint32_t SearchLandmarkCount; + landmark *Search; + uint32_t PlayerLandmarkCount; + landmark *Player; +} asset; + +asset BuiltinAssets[] = +{ + { ASSET_CSS, "cinera.css" }, + { ASSET_CSS, "cinera_topics.css" }, + { ASSET_IMG, "cinera_icon_filter.png" }, + { ASSET_JS, "cinera_pre.js" }, + { ASSET_JS, "cinera_post.js" }, + { ASSET_JS, "cinera_search.js" }, + { ASSET_JS, "cinera_player_pre.js" }, + { ASSET_JS, "cinera_player_post.js" }, +}; + +typedef enum +{ + ASSET_CSS_CINERA, + ASSET_CSS_TOPICS, + ASSET_IMG_FILTER, + ASSET_JS_CINERA_PRE, + ASSET_JS_CINERA_POST, + ASSET_JS_SEARCH, + ASSET_JS_PLAYER_PRE, + ASSET_JS_PLAYER_POST, + BUILTIN_ASSETS_COUNT, +} builtin_asset_id; + +typedef struct +{ + int Count; + memory_book Asset; +} assets; + +typedef enum { // Contents and Player Pages Mandatory TAG_INCLUDES, @@ -484,9 +2404,7 @@ enum TAG_SEARCH, // Player Page Mandatory - TAG_MENUS, TAG_PLAYER, - TAG_SCRIPT, // Player Page Optional TAG_CUSTOM0, @@ -516,22 +2434,22 @@ enum TAG_CSS, TAG_IMAGE, TAG_JS, + TAG_NAV, TAG_PROJECT, TAG_PROJECT_ID, + TAG_PROJECT_PLAIN, TAG_SEARCH_URL, TAG_THEME, TAG_URL, TEMPLATE_TAG_COUNT, -} template_tag_codes; +} template_tag_code; char *TemplateTags[] = { "__CINERA_INCLUDES__", "__CINERA_SEARCH__", - "__CINERA_MENUS__", "__CINERA_PLAYER__", - "__CINERA_SCRIPT__", "__CINERA_CUSTOM0__", "__CINERA_CUSTOM1__", @@ -559,34 +2477,423 @@ char *TemplateTags[] = { "__CINERA_CSS__", "__CINERA_IMAGE__", "__CINERA_JS__", + "__CINERA_NAV__", "__CINERA_PROJECT__", "__CINERA_PROJECT_ID__", + "__CINERA_PROJECT_PLAIN__", "__CINERA_SEARCH_URL__", "__CINERA_THEME__", "__CINERA_URL__", }; +char *NavigationTypes[] = +{ + 0, + "dropdown", + "horizontal", + "plain", +}; + +typedef enum +{ + NT_NULL, + NT_DROPDOWN, + NT_HORIZONTAL, + NT_PLAIN, + NT_COUNT, +} navigation_type; + typedef struct { int Offset; - uint32_t AssetIndex; + asset *Asset; + navigation_type NavigationType; enum8(template_tag_codes) TagCode; } tag_offset; +typedef enum +{ + TEMPLATE_GLOBAL_SEARCH, + TEMPLATE_SEARCH, + TEMPLATE_PLAYER, + TEMPLATE_BESPOKE +} template_type; + typedef struct { int Validity; // NOTE(matt): Bitmask describing which page the template is valid for, i.e. contents and / or player page - int TagCapacity; - int TagCount; + template_type Type; + bool RequiresCineraJS; + uint64_t TagCount; tag_offset *Tags; } template_metadata; typedef struct { - file_buffer File; + file File; template_metadata Metadata; } template; +void +ClearTemplateMetadata(template *Template) +{ + Template->Metadata.TagCount = 0; + Template->Metadata.Validity = 0; +} + +void +FreeFile(file *F) +{ + FreeBuffer(&F->Buffer); + Free(F->Path); + F->Handle = 0; +} + +void +FreeSignpostedFile(file_signposted *F) +{ + FreeFile(&F->File); + file_signposts Zero = {}; + F->Signposts = Zero; +} + +void +FreeTemplate(template *Template) +{ + FreeFile(&Template->File); + + FreeAndResetCount(Template->Metadata.Tags, Template->Metadata.TagCount); + ClearTemplateMetadata(Template); +} + +string +StripComponentFromPath(string Path) +{ + string Result = Path; + while(Result.Length > 0 && Result.Base[Result.Length - 1] != '/') + { + --Result.Length; + } + if(Result.Length > 1) + { + --Result.Length; + } + return Result; +} + +int +StripComponentFromPath0(char *Path) +{ + // TODO(matt): Beware that this may leak, perhaps? I dunno. + char *Ptr = Path + StringLength(Path) - 1; + if(Ptr < Path) { return RC_ERROR_DIRECTORY; } + while(Ptr > Path && *Ptr != '/') + { + --Ptr; + } + *Ptr = '\0'; + return RC_SUCCESS; +} + +int +FinalPathComponentPosition(string Path) +{ + char *Ptr = Path.Base + Path.Length - 1; + while(Ptr > Path.Base && *Ptr != '/') + { + --Ptr; + } + if(*Ptr == '/') + { + ++Ptr; + } + return Ptr - Path.Base; +} + +string +GetFinalComponent(string Path) +{ + int NewPos = FinalPathComponentPosition(Path); + string Result = { .Base = Path.Base + NewPos, .Length = Path.Length - NewPos }; + return Result; +} + +string +GetBaseFilename(string Filepath, + extension_id Extension // Pass EXT_NULL (or 0) to retain the whole file path, only without its parent directories + ) +{ + string Result = GetFinalComponent(Filepath); + if(Extension != EXT_NULL) + { + Result.Length -= ExtensionStrings[Extension].Length; + } + return Result; +} + +char *WatchTypeStrings[] = +{ + "WT_HMML", + "WT_ASSET", + "WT_CONFIG", +}; + +typedef enum +{ + WT_HMML, + WT_ASSET, + WT_CONFIG, +} watch_type; + +#include "cinera_config.c" + +typedef struct +{ + watch_type Type; + string Path; + extension_id Extension; + project *Project; + asset *Asset; +} watch_file; + +typedef struct +{ + int Descriptor; + string WatchedPath; + string TargetPath; + uint64_t FileCount; + uint64_t FileCapacity; + watch_file *Files; +} watch_handle; + +typedef struct +{ + uint64_t Count; + uint64_t Capacity; + watch_handle *Handles; + memory_book Paths; + uint32_t DefaultEventsMask; +} watch_handles; + +#define AFD 1 +#define AFE 0 + +config *Config; +project *CurrentProject; + +// NOTE(matt): Globals +mode Mode; +arena MemoryArena; +//config *Config; +assets Assets; +int inotifyInstance; +watch_handles WatchHandles; +database DB; +time_t LastPrivacyCheck; +time_t LastQuoteFetch; +#define GLOBAL_UPDATE_INTERVAL 1 +// + +typedef struct +{ + db_header_project *Project; + db_entry *Prev; + db_entry *This; + db_entry *Next; + + db_entry WorkingThis; + uint32_t PreLinkPrevOffsetTotal, PreLinkThisOffsetTotal, PreLinkNextOffsetTotal; + uint32_t PrevOffsetModifier, ThisOffsetModifier, NextOffsetModifier; + bool FormerIsFirst, LatterIsFinal; + bool DeletedEntryWasFirst, DeletedEntryWasFinal; + int16_t PrevIndex, PreDeletionThisIndex, ThisIndex, NextIndex; +} neighbourhood; + +void +LogUsage(buffer *Buffer) +{ +#if DEBUG + // NOTE(matt): Stack-string + char LogPath[256]; + CopyString(LogPath, "%s/%s", CurrentProject->CacheDir, "buffers.log"); + FILE *LogFile; + if(!(LogFile = fopen(LogPath, "a+"))) + { + MakeDir(CurrentProject->CacheDir); + if(!(LogFile = fopen(LogPath, "a+"))) + { + perror("LogUsage"); + return; + } + } + + fprintf(LogFile, "%s,%ld,%d\n", + Buffer->ID, + Buffer->Ptr - Buffer->Location, + Buffer->Size); + fclose(LogFile); +#endif +} + +bool MakeDir(string Path); + +__attribute__ ((format (printf, 2, 3))) +void +LogError(int LogLevel, char *Format, ...) +{ + if(Config->LogLevel >= LogLevel) + { + char *LogPath = MakeString0("ls", &Config->CacheDir, "/errors.log"); + FILE *LogFile; + if(!(LogFile = fopen(LogPath, "a+"))) + { + MakeDir(Config->CacheDir); + if(!(LogFile = fopen(LogPath, "a+"))) + { + perror("LogUsage"); + Free(LogPath); + return; + } + } + + va_list Args; + va_start(Args, Format); + vfprintf(LogFile, Format, Args); + va_end(Args); + // TODO(matt): Include the LogLevel "string" and the current wall time + fprintf(LogFile, "\n"); + fclose(LogFile); + Free(LogPath); + } +} + +typedef struct +{ + buffer IncludesSearch; + buffer SearchEntry; + buffer Search; // NOTE(matt): This buffer is malloc'd separately, rather than claimed from the memory_arena + buffer IncludesPlayer; + buffer Player; + + char Custom0[MAX_CUSTOM_SNIPPET_SHORT_LENGTH]; + char Custom1[MAX_CUSTOM_SNIPPET_SHORT_LENGTH]; + char Custom2[MAX_CUSTOM_SNIPPET_SHORT_LENGTH]; + char Custom3[MAX_CUSTOM_SNIPPET_SHORT_LENGTH]; + char Custom4[MAX_CUSTOM_SNIPPET_SHORT_LENGTH]; + char Custom5[MAX_CUSTOM_SNIPPET_SHORT_LENGTH]; + char Custom6[MAX_CUSTOM_SNIPPET_SHORT_LENGTH]; + char Custom7[MAX_CUSTOM_SNIPPET_SHORT_LENGTH]; + char Custom8[MAX_CUSTOM_SNIPPET_SHORT_LENGTH]; + char Custom9[MAX_CUSTOM_SNIPPET_SHORT_LENGTH]; + char Custom10[MAX_CUSTOM_SNIPPET_SHORT_LENGTH]; + char Custom11[MAX_CUSTOM_SNIPPET_SHORT_LENGTH]; + char Custom12[MAX_CUSTOM_SNIPPET_LONG_LENGTH]; + char Custom13[MAX_CUSTOM_SNIPPET_LONG_LENGTH]; + char Custom14[MAX_CUSTOM_SNIPPET_LONG_LENGTH]; + char Custom15[MAX_CUSTOM_SNIPPET_LONG_LENGTH]; + + char Title[MAX_TITLE_LENGTH]; + char URLSearch[MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH]; + char URLPlayer[MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_BASE_FILENAME_LENGTH]; + char VideoID[MAX_VOD_ID_LENGTH]; + char VODPlatform[16]; +} buffers; + +int +ClaimBuffer(buffer *Buffer, buffer_id ID, int Size) +{ + if(MemoryArena.Ptr - MemoryArena.Location + Size > MemoryArena.Size) + { + return RC_ARENA_FULL; + } + Buffer->Location = (char *)MemoryArena.Ptr; + Buffer->Size = Size; + Buffer->ID = ID; + MemoryArena.Ptr += Buffer->Size; + *Buffer->Location = '\0'; + Buffer->Ptr = Buffer->Location; +#if DEBUG + float PercentageUsed = (float)(MemoryArena.Ptr - MemoryArena.Location) / MemoryArena.Size * 100; + printf(" ClaimBuffer(%s): %d\n" + " Total ClaimedMemory: %ld (%.2f%%, leaving %ld free)\n\n", Buffer->ID, Buffer->Size, MemoryArena.Ptr - MemoryArena.Location, PercentageUsed, MemoryArena.Size - (MemoryArena.Ptr - MemoryArena.Location)); +#endif + return RC_SUCCESS; +} + +void +DeclaimBuffer(buffer *Buffer) +{ + if(Buffer->Size > 0) + { + float PercentageUsed = (float)(Buffer->Ptr - Buffer->Location) / Buffer->Size * 100; +#if DEBUG + printf("DeclaimBuffer(%s)\n" + " Used: %ld / %d (%.2f%%)\n" + "\n" + " Total ClaimedMemory: %ld\n\n", + Buffer->ID, + Buffer->Ptr - Buffer->Location, + Buffer->Size, + PercentageUsed, + MemoryArena.Ptr - MemoryArena.Location); +#endif + LogUsage(Buffer); + if(PercentageUsed >= 95.0f) + { + // TODO(matt): Implement either dynamically growing buffers, or phoning home to miblodelcarpio@gmail.com + LogError(LOG_ERROR, "%s used %.2f%% of its allotted memory\n", BufferIDStrings[Buffer->ID], PercentageUsed); + fprintf(stderr, "%sWarning%s: %s used %.2f%% of its allotted memory\n", + ColourStrings[CS_ERROR], ColourStrings[CS_END], + BufferIDStrings[Buffer->ID], PercentageUsed); + } + else if(PercentageUsed >= 80.0f) + { + // TODO(matt): Implement either dynamically growing buffers, or phoning home to miblodelcarpio@gmail.com + LogError(LOG_ERROR, "%s used %.2f%% of its allotted memory\n", BufferIDStrings[Buffer->ID], PercentageUsed); + fprintf(stderr, "%sWarning%s: %s used %.2f%% of its allotted memory\n", + ColourStrings[CS_WARNING], ColourStrings[CS_END], + BufferIDStrings[Buffer->ID], PercentageUsed); + } + *Buffer->Location = '\0'; + Buffer->Ptr = Buffer->Location; + MemoryArena.Ptr -= Buffer->Size; + Buffer->Size = 0; + //Buffer->ID = 0; + } +} + +void +RewindBuffer(buffer *Buffer) +{ +#if DEBUG + float PercentageUsed = (float)(Buffer->Ptr - Buffer->Location) / Buffer->Size * 100; + printf("Rewinding %s\n" + " Used: %ld / %d (%.2f%%)\n\n", + Buffer->ID, + Buffer->Ptr - Buffer->Location, + Buffer->Size, + PercentageUsed); +#endif + Buffer->Ptr = Buffer->Location; +} + +void +RewindCollationBuffers(buffers *CollationBuffers) +{ + RewindBuffer(&CollationBuffers->IncludesPlayer); + RewindBuffer(&CollationBuffers->Player); + + RewindBuffer(&CollationBuffers->IncludesSearch); + RewindBuffer(&CollationBuffers->SearchEntry); +} + +// NOTE(matt): Special-purposes indices, for project generation-index and entry index +db_project_index GLOBAL_SEARCH_PAGE_INDEX = { -1, 0 }; +typedef enum +{ + SP_SEARCH = -1, +} special_page_id; + // TODO(matt): Consider putting the ref_info and quote_info into linked lists on the heap, just to avoid all the hardcoded sizes typedef struct @@ -624,185 +2931,20 @@ typedef struct int Count; } categories; -char *SupportIcons[] = -{ - "cinera_sprite_patreon.png", - "cinera_sprite_sendowl.png", -}; - -typedef enum -{ - ICON_PATREON = BUILTIN_ASSETS_COUNT, - ICON_SENDOWL, - SUPPORT_ICON_COUNT, -} support_icons; - -// TODO(matt): Parse this stuff out of a config file -typedef struct -{ - char *Username; - char *CreditedName; - char *HomepageURL; - enum8(support_icons) SupportIconIndex; - char *SupportURL; -} credential_info; - -credential_info Credentials[] = -{ - { "a_waterman", "Andrew Waterman", "https://www.linkedin.com/in/andrew-waterman-76805788" }, - { "y_lee", "Yunsup Lee", "https://www.linkedin.com/in/yunsup-lee-385b692b/" }, - { "AndrewJDR", "Andrew Johnson" }, - { "AsafGartner", "Asaf Gartner" }, - { "BretHudson", "Bret Hudson", "http://www.brethudson.com/", ICON_PATREON, "https://www.patreon.com/indieFunction"}, - { "ChronalDragon", "Andrew Chronister", "http://chronal.net/" }, - { "Kelimion", "Jeroen van Rijn", "https://handmade.network/home" }, - { "Mannilie", "Emmanuel Vaccaro", "http://emmanuelvaccaro.com/" }, - { "Miblo", "Matt Mascarenhas", "https://miblodelcarpio.co.uk/", ICON_SENDOWL, "https://miblodelcarpio.co.uk/cinera#pledge"}, - { "Mr4thDimention", "Allen Webster", "http://www.4coder.net/" }, - { "Pseudonym73", "Andrew Bromage", "https://twitter.com/deguerre" }, - { "Quel_Solaar", "Eskil Steenberg", "http://quelsolaar.com/" }, - { "ZedZull", "Jay Waggle" }, - { "abnercoimbre", "Abner Coimbre", "https://handmade.network/m/abnercoimbre" }, - { "brianwill", "Brian Will", "http://brianwill.net/blog/" }, - { "cbloom", "Charles Bloom", "http://cbloomrants.blogspot.co.uk/" }, - { "cmuratori", "Casey Muratori", "https://handmadehero.org", ICON_SENDOWL, "https://handmadehero.org/fund"}, - { "csnover", "Colin Snover", "https://zetafleet.com/" }, - { "debiatan", "Miguel Lechón", "http://blog.debiatan.net/" }, - { "dspecht", "Dustin Specht" }, - { "effect0r", "Cory Henderlite" }, - { "ffsjs", "ffsjs" }, - { "fierydrake", "Mike Tunnicliffe" }, - { "garlandobloom", "Matthew VanDevander", "https://lowtideproductions.com/", ICON_PATREON, "https://www.patreon.com/mv"}, - { "ikerms", "Iker Murga" }, - { "insofaras", "Alex Baines", "https://abaines.me.uk/" }, - { "jacebennett", "Jace Bennett" }, - { "jon", "Jonathan Blow", "http://the-witness.net/news/" }, - { "jpike", "Jacob Pike" }, - { "martincohen", "Martin Cohen", "http://blog.coh.io/" }, - { "miotatsu", "Mio Iwakura", "http://riscy.tv/", ICON_PATREON, "https://patreon.com/miotatsu"}, - { "nothings", "Sean Barrett", "https://nothings.org/" }, - { "pervognsen", "Per Vognsen", "https://github.com/pervognsen/bitwise/" }, - { "philipbuuck", "Philip Buuck", "http://philipbuuck.com/" }, - { "powerc9000", "Clay Murray", "http://claymurray.website/" }, - { "rygorous", "Fabian Giesen", "https://fgiesen.wordpress.com/" }, - { "schme", "Kasper Sauramo" }, - { "sssmcgrath", "Shawn McGrath", "http://www.dyadgame.com/" }, - { "thehappiecat", "Anne", "https://www.youtube.com/c/TheHappieCat", ICON_PATREON, "https://www.patreon.com/thehappiecat"}, - { "theinternetftw", "Ben Craddock" }, - { "wheatdog", "Tim Liou", "http://stringbulbs.com/" }, - { "williamchyr", "William Chyr", "http://williamchyr.com/" }, - { "wonchun", "Won Chun", "https://twitter.com/won3d" }, -}; - -typedef struct -{ - char *Medium; - char *Icon; - char *WrittenName; -} category_medium; - -category_medium CategoryMedium[] = -{ - // medium icon written name - { "admin", "🗹", "Administrivia"}, - { "afk", "…" , "Away from Keyboard"}, - { "authored", "🗪", "Chat Comment"}, - { "blackboard", "🖌", "Blackboard"}, - { "drawing", "🎨", "Drawing"}, - { "experience", "🍷", "Experience"}, - { "hat", "🎩", "Hat"}, - { "multimedia", "🎬", "Media Clip"}, - { "owl", "🦉", "Owl of Shame"}, - { "programming", "🖮", "Programming"}, - { "rant", "💢", "Rant"}, - { "research", "📖", "Research"}, - { "run", "🏃", "In-Game"}, // TODO(matt): Potentially make this written name configurable per project - { "speech", "🗩", "Speech"}, - { "trivia", "🎲", "Trivia"}, -}; - -enum -{ - NS_CALENDRICAL, - NS_LINEAR, - NS_SEASONAL, -} numbering_schemes; - -typedef struct -{ - char *ProjectID; - char *FullName; - char *Unit; // e.g. Day, Episode, Session - enum8(numbering_schemes) NumberingScheme; // numbering_schemes - char *Medium; - char *AltURLPrefix; // NOTE(matt): This currently just straight up replaces the ProjectID in the player - // pages' output directories -} project_info; - -project_info ProjectInfo[] = -{ - { "bitwise", "Bitwise", "Day", NS_LINEAR, "programming", "" }, - - { "book", "Book Club", "Day", NS_LINEAR, "research", "" }, - { "coad", "Computer Organization and Design", "", NS_LINEAR, "research", "" }, - { "reader", "RISC-V Reader", "", NS_LINEAR, "research", "" }, - { "riscy", "RISCY BUSINESS", "Day", NS_LINEAR, "programming", "" }, - { "risc", "RISCellaneous", "", NS_CALENDRICAL, "speech", "" }, - - { "chat", "Handmade Chat", "Day", NS_LINEAR, "speech", "" }, - { "code", "Handmade Hero", "Day", NS_LINEAR, "programming", "day" }, - { "intro-to-c", "Intro to C on Windows", "Day", NS_LINEAR, "programming", "day" }, - { "misc", "Handmade Miscellany", "", NS_LINEAR, "admin", "" }, - { "ray", "Handmade Ray", "Day", NS_LINEAR, "programming", "" }, - - { "hmdshow", "HandmadeDev Show", "", NS_SEASONAL, "speech", "ep" }, - { "lecture", "Abner Talks", "", NS_SEASONAL, "speech", "" }, - { "stream", "Abner Programs", "", NS_SEASONAL, "programming", "" }, - { "special", "Abner Show Special", "", NS_SEASONAL, "programming", "" }, - - { "obbg", "Open Block Building Game", "Episode", NS_LINEAR, "programming", "" }, - - { "sysadmin", "SysAdmin", "Session", NS_LINEAR, "admin", "" }, -}; - -char *ColourStrings[] = -{ - "\e[0m", - "\e[0;33m", - "\e[0;34m", - "\e[0;35m", "\e[0;35m", - "\e[1;30m", "\e[1;30m", - "\e[1;31m", - "\e[1;32m", "\e[1;32m", - "\e[1;33m", -}; - -enum -{ - CS_END, - CS_WARNING, - CS_PRIVATE, - CS_ONGOING, CS_PURPLE, - CS_COMMENT, CS_DELETION, - CS_ERROR, - CS_ADDITION, CS_SUCCESS, - CS_REINSERTION, -} colour_strings; - typedef struct { char *Name; - enum8(colour_strings) Colour; + colour_code Colour; } edit_type; -enum +typedef enum { EDIT_INSERTION, EDIT_APPEND, EDIT_REINSERTION, EDIT_DELETION, EDIT_ADDITION, -} edit_types; +} edit_type_id; edit_type EditTypes[] = { @@ -813,26 +2955,6 @@ edit_type EditTypes[] = { "Added", CS_ADDITION } }; -void -Clear(char *String, int Size) -{ - for(int i = 0; i < Size; ++i) - { - String[i] = 0; - } -} - -int -StringLength(char *String) -{ - int i = 0; - while(String[i]) - { - ++i; - } - return i; -} - #define CopyString(Dest, DestSize, Format, ...) CopyString_(__LINE__, (Dest), (DestSize), (Format), ##__VA_ARGS__) __attribute__ ((format (printf, 4, 5))) int @@ -851,47 +2973,61 @@ CopyString_(int LineNumber, char Dest[], int DestSize, char *Format, ...) return Length; } -#define CopyStringNoFormat(Dest, DestSize, String) CopyStringNoFormat_(__LINE__, Dest, DestSize, String) +#define ClearCopyString(Dest, DestSize, Format, ...) ClearCopyString_(__LINE__, (Dest), (DestSize), (Format), ##__VA_ARGS__) +__attribute__ ((format (printf, 4, 5))) int -CopyStringNoFormat_(int LineNumber, char *Dest, int DestSize, char *String) +ClearCopyString_(int LineNumber, char Dest[], int DestSize, char *Format, ...) { + Clear(Dest, DestSize); int Length = 0; - char *Start = String; - while(*String) - { - *Dest++ = *String++; - ++Length; - } + va_list Args; + va_start(Args, Format); + Length = vsnprintf(Dest, DestSize, Format, Args); if(Length >= DestSize) { - printf("CopyStringNoFormat() call on line %d has been passed a buffer too small (%d bytes) to contain null-terminated %d(+1)-character string:\n" - "%s\n", LineNumber, DestSize, Length, Start); + printf("CopyString() call on line %d has been passed a buffer too small (%d bytes) to contain null-terminated %d(+1)-character string\n", LineNumber, DestSize, Length); __asm__("int3"); } - *Dest = '\0'; + va_end(Args); return Length; } +#define CopyStringNoFormat(Dest, DestSize, String) CopyStringNoFormat_(__LINE__, Dest, DestSize, String) +int +CopyStringNoFormat_(int LineNumber, char *Dest, int DestSize, string String) +{ + if(String.Length + 1 > DestSize) + { + printf("CopyStringNoFormat() call on line %d has been passed a buffer too small (%d bytes) to contain null-terminated %ld(+1)-character string:\n" + "%.*s\n", LineNumber, DestSize, String.Length, (int)String.Length, String.Base); + __asm__("int3"); + } + for(int i = 0; i < String.Length; ++i) + { + *Dest++ = String.Base[i]; + } + *Dest = '\0'; + return String.Length; +} + #define ClearCopyStringNoFormat(Dest, DestSize, String) ClearCopyStringNoFormat_(__LINE__, Dest, DestSize, String) int -ClearCopyStringNoFormat_(int LineNumber, char *Dest, int DestSize, char *String) +ClearCopyStringNoFormat_(int LineNumber, char *Dest, int DestSize, string String) { - Clear(Dest, DestSize); - int Length = 0; - char *Start = String; - while(*String) + if(String.Length + 1 > DestSize) { - *Dest++ = *String++; - ++Length; - } - if(Length >= DestSize) - { - printf("ClearCopyStringNoFormat() call on line %d has been passed a buffer too small (%d bytes) to contain null-terminated %d(+1)-character string:\n" - "%s\n", LineNumber, DestSize, Length, Start); + printf("ClearCopyStringNoFormat() call on line %d has been passed a buffer too small (%d bytes) to contain null-terminated %ld(+1)-character string:\n" + "%.*s\n", LineNumber, DestSize, String.Length, (int)String.Length, String.Base); __asm__("int3"); } + + Clear(Dest, DestSize); + for(int i = 0; i < String.Length; ++i) + { + *Dest++ = String.Base[i]; + } *Dest = '\0'; - return Length; + return String.Length; } // TODO(matt): Maybe do a version of this that takes a string as a Terminator @@ -928,7 +3064,7 @@ CopyStringToBuffer_(int LineNumber, buffer *Dest, char *Format, ...) if(Length + (Dest->Ptr - Dest->Location) >= Dest->Size) { fprintf(stderr, "CopyStringToBuffer(%s) call on line %d cannot accommodate null-terminated %d(+1)-character string:\n" - "%s\n", Dest->ID, LineNumber, Length, Format); + "%s\n", BufferIDStrings[Dest->ID], LineNumber, Length, Format); __asm__("int3"); } Dest->Ptr += Length; @@ -936,19 +3072,18 @@ CopyStringToBuffer_(int LineNumber, buffer *Dest, char *Format, ...) #define CopyStringToBufferNoFormat(Dest, String) CopyStringToBufferNoFormat_(__LINE__, Dest, String) void -CopyStringToBufferNoFormat_(int LineNumber, buffer *Dest, char *String) +CopyStringToBufferNoFormat_(int LineNumber, buffer *Dest, string String) { - char *Start = String; - while(*String) + if(String.Length + 1 > Dest->Size - (Dest->Ptr - Dest->Location)) { - *Dest->Ptr++ = *String++; - } - if(Dest->Ptr - Dest->Location >= Dest->Size) - { - fprintf(stderr, "CopyStringToBufferNoFormat(%s) call on line %d cannot accommodate %d-character string:\n" - "%s\n", Dest->ID, LineNumber, StringLength(Start), Start); + fprintf(stderr, "CopyStringToBufferNoFormat(%s) call on line %d cannot accommodate %ld-character string:\n" + "%.*s\n", BufferIDStrings[Dest->ID], LineNumber, String.Length, (int)String.Length, String.Base); __asm__("int3"); } + for(int i = 0; i < String.Length; ++i) + { + *Dest->Ptr++ = String.Base[i]; + } *Dest->Ptr = '\0'; } @@ -963,8 +3098,8 @@ CopyStringToBufferNoFormatL_(int LineNumber, buffer *Dest, int Length, char *Str } if(Dest->Ptr - Dest->Location >= Dest->Size) { - fprintf(stderr, "CopyStringToBufferNoFormat(%s) call on line %d cannot accommodate %d-character string:\n" - "%s\n", Dest->ID, LineNumber, StringLength(Start), Start); + fprintf(stderr, "CopyStringToBufferNoFormat(%s) call on line %d cannot accommodate %ld-character string:\n" + "%s\n", BufferIDStrings[Dest->ID], LineNumber, StringLength(Start), Start); __asm__("int3"); } *Dest->Ptr = '\0'; @@ -972,28 +3107,38 @@ CopyStringToBufferNoFormatL_(int LineNumber, buffer *Dest, int Length, char *Str #define CopyStringToBufferHTMLSafe(Dest, String) CopyStringToBufferHTMLSafe_(__LINE__, Dest, String) void -CopyStringToBufferHTMLSafe_(int LineNumber, buffer *Dest, char *String) +CopyStringToBufferHTMLSafe_(int LineNumber, buffer *Dest, string String) { - char *Start = String; - int Length = StringLength(String); - while(*String) + int Length = String.Length; + for(int i = 0; i < String.Length; ++i) { - switch(*String) + switch(String.Base[i]) + { + case '<': case '>': Length += 3; break; + case '&': case '\'': Length += 4; break; + case '\"': Length += 5; break; + default: break; + } + } + + if((Dest->Ptr - Dest->Location) + Length >= Dest->Size) + { + fprintf(stderr, "CopyStringToBufferHTMLSafe(%s) call on line %d cannot accommodate %d(+1)-character HTML-sanitised string:\n" + "%.*s\n", BufferIDStrings[Dest->ID], LineNumber, Length, (int)String.Length, String.Base); + __asm__("int3"); + } + + for(int i = 0; i < String.Length; ++i) + { + switch(String.Base[i]) { case '<': *Dest->Ptr++ = '&'; *Dest->Ptr++ = 'l'; *Dest->Ptr++ = 't'; *Dest->Ptr++ = ';'; Length += 3; break; case '>': *Dest->Ptr++ = '&'; *Dest->Ptr++ = 'g'; *Dest->Ptr++ = 't'; *Dest->Ptr++ = ';'; Length += 3; break; case '&': *Dest->Ptr++ = '&'; *Dest->Ptr++ = 'a'; *Dest->Ptr++ = 'm'; *Dest->Ptr++ = 'p'; *Dest->Ptr++ = ';'; Length += 4; break; case '\"': *Dest->Ptr++ = '&'; *Dest->Ptr++ = 'q'; *Dest->Ptr++ = 'u'; *Dest->Ptr++ = 'o'; *Dest->Ptr++ = 't'; *Dest->Ptr++ = ';'; Length += 5; break; case '\'': *Dest->Ptr++ = '&'; *Dest->Ptr++ = '#'; *Dest->Ptr++ = '3'; *Dest->Ptr++ = '9'; *Dest->Ptr++ = ';'; Length += 4; break; - default: *Dest->Ptr++ = *String; break; + default: *Dest->Ptr++ = String.Base[i]; break; } - ++String; - } - if(Dest->Ptr - Dest->Location >= Dest->Size) - { - fprintf(stderr, "CopyStringToBufferHTMLSafe(%s) call on line %d cannot accommodate %d(+1)-character HTML-sanitised string:\n" - "%s\n", Dest->ID, LineNumber, Length, Start); - __asm__("int3"); } *Dest->Ptr = '\0'; } @@ -1021,7 +3166,7 @@ CopyStringToBufferHTMLSafeBreakingOnSlash_(int LineNumber, buffer *Dest, char *S if(Dest->Ptr - Dest->Location >= Dest->Size) { fprintf(stderr, "CopyStringToBufferHTMLSafeBreakingOnSlash(%s) call on line %d cannot accommodate %d(+1)-character HTML-sanitised string:\n" - "%s\n", Dest->ID, LineNumber, Length, Start); + "%s\n", BufferIDStrings[Dest->ID], LineNumber, Length, Start); __asm__("int3"); } *Dest->Ptr = '\0'; @@ -1029,13 +3174,40 @@ CopyStringToBufferHTMLSafeBreakingOnSlash_(int LineNumber, buffer *Dest, char *S #define CopyStringToBufferHTMLPercentEncoded(Dest, String) CopyStringToBufferHTMLPercentEncoded_(__LINE__, Dest, String) void -CopyStringToBufferHTMLPercentEncoded_(int LineNumber, buffer *Dest, char *String) +CopyStringToBufferHTMLPercentEncoded_(int LineNumber, buffer *Dest, string String) { - char *Start = String; - int Length = StringLength(String); - while(*String) + int Length = String.Length; + for(int i = 0; i < String.Length; ++ i) { - switch(*String) + switch(String.Base[i]) + { + case ' ': + case '\"': + case '%': + case '&': + case '<': + case '>': + case '?': + case '\\': + case '^': + case '`': + case '{': + case '|': + case '}': Length += 2; break; + default: break; + } + } + + if((Dest->Ptr - Dest->Location) + Length >= Dest->Size) + { + fprintf(stderr, "CopyStringToBufferHTMLPercentEncodedL(%s) call on line %d cannot accommodate %d(+1)-character percent-encoded string:\n" + "%.*s\n", BufferIDStrings[Dest->ID], LineNumber, Length, (int)String.Length, String.Base); + __asm__("int3"); + } + + for(int i = 0; i < String.Length; ++ i) + { + switch(String.Base[i]) { case ' ': *Dest->Ptr++ = '%'; *Dest->Ptr++ = '2'; *Dest->Ptr++ = '0'; Length += 2; break; case '\"': *Dest->Ptr++ = '%'; *Dest->Ptr++ = '2'; *Dest->Ptr++ = '2'; Length += 2; break; @@ -1050,15 +3222,8 @@ CopyStringToBufferHTMLPercentEncoded_(int LineNumber, buffer *Dest, char *String case '{': *Dest->Ptr++ = '%'; *Dest->Ptr++ = '7'; *Dest->Ptr++ = 'B'; Length += 2; break; case '|': *Dest->Ptr++ = '%'; *Dest->Ptr++ = '7'; *Dest->Ptr++ = 'C'; Length += 2; break; case '}': *Dest->Ptr++ = '%'; *Dest->Ptr++ = '7'; *Dest->Ptr++ = 'D'; Length += 2; break; - default: *Dest->Ptr++ = *String; break; + default: *Dest->Ptr++ = String.Base[i]; break; } - ++String; - } - if(Dest->Ptr - Dest->Location >= Dest->Size) - { - fprintf(stderr, "CopyStringToBufferHTMLPercentEncodedL(%s) call on line %d cannot accommodate %d(+1)-character percent-encoded string:\n" - "%s\n", Dest->ID, LineNumber, Length, Start); - __asm__("int3"); } *Dest->Ptr = '\0'; } @@ -1067,79 +3232,181 @@ CopyStringToBufferHTMLPercentEncoded_(int LineNumber, buffer *Dest, char *String void CopyBuffer_(int LineNumber, buffer *Dest, buffer *Src) { - Src->Ptr = Src->Location; - while(*Src->Ptr) + if((Dest->Ptr - Dest->Location + Src->Ptr - Src->Location) >= Dest->Size) { - *Dest->Ptr++ = *Src->Ptr++; - } - if(Dest->Ptr - Dest->Location >= Dest->Size) - { - fprintf(stderr, "CopyBuffer(%s) call on line %d cannot accommodate %d(+1)-character %s\n", Dest->ID, LineNumber, StringLength(Src->Location), Src->ID); + fprintf(stderr, "CopyBuffer(%s) call on line %d cannot accommodate %ld(+1)-character %s\n", BufferIDStrings[Dest->ID], LineNumber, StringLength(Src->Location), BufferIDStrings[Src->ID]); __asm__("int3"); } + for(int i = 0; i < Src->Ptr - Src->Location; ++i) + { + *Dest->Ptr++ = Src->Location[i]; + } + *Dest->Ptr = '\0'; +} + +typedef enum +{ + PAGE_PLAYER = 1 << 0, + PAGE_SEARCH = 1 << 1 +} page_type; + +// NOTE(matt): Perhaps this OffsetLandmarks() could be made redundant once we're on the LUT +void +OffsetLandmarks(buffer *Dest, buffer *Src, page_type PageType) +{ + if(Config->QueryString.Length > 0) + { + for(int i = 0; i < Assets.Count; ++i) + { + asset *Asset = GetPlaceInBook(&Assets.Asset, i); + if(PageType == PAGE_PLAYER) + { + for(int LandmarkIndex = 0; LandmarkIndex < Asset->PlayerLandmarkCount; ++LandmarkIndex) + { + if(Asset->Player[LandmarkIndex].BufferID == Src->ID) + { + Asset->Player[LandmarkIndex].Offset += Dest->Ptr - Dest->Location; + Asset->Player[LandmarkIndex].BufferID = Dest->ID; + } + } + } + else + { + for(int LandmarkIndex = 0; LandmarkIndex < Asset->SearchLandmarkCount; ++LandmarkIndex) + { + if(Asset->Search[LandmarkIndex].BufferID == Src->ID) + { + Asset->Search[LandmarkIndex].Offset += Dest->Ptr - Dest->Location; + Asset->Search[LandmarkIndex].BufferID = Dest->ID; + } + } + } + } + } +} + +#define CopyLandmarkedBuffer(Dest, Src, PrevStartLinkOffset, PageType) CopyLandmarkedBuffer_(__LINE__, Dest, Src, PrevStartLinkOffset, PageType) +void +CopyLandmarkedBuffer_(int LineNumber, buffer *Dest, buffer *Src, uint32_t *PrevStartLinkOffset, page_type PageType) +{ + if((Dest->Ptr - Dest->Location + Src->Ptr - Src->Location) >= Dest->Size) + { + fprintf(stderr, "CopyLandmarkedBuffer(%s) call on line %d cannot accommodate %ld(+1)-character %s\n", BufferIDStrings[Dest->ID], LineNumber, StringLength(Src->Location), BufferIDStrings[Src->ID]); + __asm__("int3"); + } + + if(PrevStartLinkOffset) { *PrevStartLinkOffset += Dest->Ptr - Dest->Location; } + OffsetLandmarks(Dest, Src, PageType); + + for(int i = 0; i < Src->Ptr - Src->Location; ++i) + { + *Dest->Ptr++ = Src->Location[i]; + } *Dest->Ptr = '\0'; } #define CopyBufferSized(Dest, Src, Size) CopyBufferSized_(__LINE__, Dest, Src, Size) void -CopyBufferSized_(int LineNumber, buffer *Dest, buffer *Src, int Size) +CopyBufferSized_(int LineNumber, buffer *Dest, buffer *Src, uint64_t Size) { // NOTE(matt): Similar to CopyBuffer(), just without null-terminating - Src->Ptr = Src->Location; - while(Src->Ptr - Src->Location < Size) + if((Dest->Ptr - Dest->Location + Size) > Dest->Size) { - *Dest->Ptr++ = *Src->Ptr++; - } - if(Dest->Ptr - Dest->Location >= Dest->Size) - { - fprintf(stderr, "CopyBufferNoNull(%s) call on line %d cannot accommodate %d(+1)-character %s\n", Dest->ID, LineNumber, StringLength(Src->Location), Src->ID); + fprintf(stderr, "CopyBufferSized(%s) call on line %d cannot accommodate %ld-character %s\n", BufferIDStrings[Dest->ID], LineNumber, Size, BufferIDStrings[Src->ID]); __asm__("int3"); } + for(int i = 0; i < Size; ++i) + { + *Dest->Ptr++ = Src->Location[i]; + } } -int -StringsDiffer(char *A, char *B) // NOTE(matt): Two null-terminated strings +void +AppendBuffer(buffer *Dest, buffer *Src) { - while(*A && *B && *A == *B) - { - ++A, ++B; - } - return *A - *B; + uint64_t BytePosition = Dest->Ptr - Dest->Location; + Dest->Size = Dest->Ptr - Dest->Location + Src->Ptr - Src->Location + 1; + Dest->Location = realloc(Dest->Location, Dest->Size); + Dest->Ptr = Dest->Location + BytePosition; + CopyBuffer(Dest, Src); } -int -StringsDifferCaseInsensitive(char *A, char *B) // NOTE(matt): Two null-terminated strings +void +AppendLandmarkedBuffer(buffer *Dest, buffer *Src, page_type PageType) { - while(*A && *B && - ((*A >= 'A' && *A <= 'Z') ? *A + ('a' - 'A') : *A) == - ((*B >= 'A' && *B <= 'Z') ? *B + ('a' - 'A') : *B)) - { - ++A, ++B; - } - return *A - *B; + OffsetLandmarks(Dest, Src, PageType); + AppendBuffer(Dest, Src); } -bool -StringsDifferT(char *A, // NOTE(matt): Null-terminated string - char *B, // NOTE(matt): Not null-terminated string (e.g. one mid-buffer) - char Terminator // NOTE(matt): Caller definable terminator. Pass 0 to only match on the extent of A - ) +void +AppendStringToBuffer(buffer *B, string S) { - // TODO(matt): Make sure this can't crash upon reaching the end of B's buffer - int ALength = StringLength(A); - int i = 0; - while(i < ALength && A[i] && A[i] == B[i]) + uint64_t BytePosition = B->Ptr - B->Location; + B->Size = B->Ptr - B->Location + S.Length + 1; + B->Location = realloc(B->Location, B->Size); + B->Ptr = B->Location + BytePosition; + CopyStringToBufferNoFormat(B, S); +} + +void +AppendInt32ToBuffer(buffer *B, int32_t I) +{ + int Digits = DigitsInInt(&I); + uint64_t BytePosition = B->Ptr - B->Location; + B->Size = B->Ptr - B->Location + Digits + 1; + B->Location = realloc(B->Location, B->Size); + B->Ptr = B->Location + BytePosition; + char Temp[Digits + 1]; + sprintf(Temp, "%i", I); + CopyStringToBufferNoFormat(B, Wrap0(Temp)); +} + +void +AppendUint32ToBuffer(buffer *B, uint32_t I) +{ + int Digits = DigitsInUint(&I); + uint64_t BytePosition = B->Ptr - B->Location; + B->Size = B->Ptr - B->Location + Digits + 1; + B->Location = realloc(B->Location, B->Size); + B->Ptr = B->Location + BytePosition; + char Temp[Digits + 1]; + sprintf(Temp, "%i", I); + CopyStringToBufferNoFormat(B, Wrap0(Temp)); +} + +uint64_t +AmpersandEncodedStringLength(string S) +{ + uint64_t Result = S.Length; + for(int i = 0; i < S.Length; ++i) { - ++i; + switch(S.Base[i]) + { + case '<': case '>': Result += 3; break; + case '&': case '\'': Result += 4; break; + case '\"': Result += 5; break; + default: break; + } } - if((!Terminator && !A[i] && ALength == i) || - (!A[i] && ALength == i && (B[i] == Terminator))) + return Result; +} + +void +AppendStringToBufferHTMLSafe(buffer *B, string S) +{ + uint64_t BytePosition = B->Ptr - B->Location; + B->Size = B->Ptr - B->Location + AmpersandEncodedStringLength(S) + 1; + B->Location = realloc(B->Location, B->Size); + B->Ptr = B->Location + BytePosition; + CopyStringToBufferHTMLSafe(B, S); +} + +void +IndentBuffer(buffer *B, uint64_t IndentationLevel) +{ + for(int i = 0; i < INDENT_WIDTH * IndentationLevel; ++i) { - return FALSE; - } - else - { - return TRUE; + AppendStringToBuffer(B, Wrap0(" ")); } } @@ -1224,28 +3491,12 @@ SeekBufferForString(buffer *Buffer, char *String, return RC_SUCCESS; } -int -StripComponentFromPath(char *Path) -{ - char *Ptr = Path + StringLength(Path) - 1; - if(Ptr < Path) { return RC_ERROR_DIRECTORY; } - while(Ptr > Path && *Ptr != '/') - { - --Ptr; - } - *Ptr = '\0'; - return RC_SUCCESS; -} - -int ClaimBuffer(buffer *Buffer, char *ID, int Size); -void DeclaimBuffer(buffer *Buffer); - -int -ResolvePath(char *Path) +void +ResolvePath(char **Path) { buffer B; - ClaimBuffer(&B, "ResolvedPath", StringLength(Path) + 1); - CopyStringToBufferNoFormat(&B, Path); + ClaimBuffer(&B, BID_RESOLVED_PATH, StringLength(*Path) + 1); + CopyStringToBufferNoFormat(&B, Wrap0(*Path)); B.Ptr = B.Location; while(SeekBufferForString(&B, "/../", C_SEEK_FORWARDS, C_SEEK_END) == RC_SUCCESS) @@ -1255,226 +3506,80 @@ ResolvePath(char *Path) --B.Ptr; SeekBufferForString(&B, "/", C_SEEK_BACKWARDS, C_SEEK_BEFORE); SeekBufferForString(&B, "/", C_SEEK_BACKWARDS, C_SEEK_START); - CopyStringToBufferNoFormat(&B, NextComponentHead); + CopyStringToBufferNoFormat(&B, Wrap0(NextComponentHead)); Clear(B.Ptr, B.Size - (B.Ptr - B.Location)); B.Ptr -= RemainingChars; } - CopyStringNoFormat(Path, StringLength(Path) + 1, B.Location); - + Free(*Path); + ExtendString0(Path, Wrap0(B.Location)); DeclaimBuffer(&B); - return RC_SUCCESS; } -int -MakeDir(char *Path) +char * +ExpandPath(string Path, string *RelativeToFile) { - // TODO(matt): Correctly check for permissions - int Ancestors = 0; - while(mkdir(Path, 00755) == -1) - { - if(errno == EACCES) - { - return RC_ERROR_DIRECTORY; - } - if(StripComponentFromPath(Path) == RC_ERROR_DIRECTORY) { return RC_ERROR_DIRECTORY; } - ++Ancestors; - } - int i = StringLength(Path); - while(Ancestors > 0) - { - while(Path[i] != '\0') - { - ++i; - } - Path[i] = '/'; - --Ancestors; - if((mkdir(Path, 00755)) == -1) - { - return RC_ERROR_DIRECTORY; - } - } - return RC_SUCCESS; -} + int Flags = WRDE_NOCMD | WRDE_UNDEF | WRDE_APPEND; + wordexp_t Expansions = {}; -void -LogUsage(buffer *Buffer) -{ -#if DEBUG - char LogPath[256]; - CopyString(LogPath, "%s/%s", Config.CacheDir, "buffers.log"); - FILE *LogFile; - if(!(LogFile = fopen(LogPath, "a+"))) - { - MakeDir(Config.CacheDir); - if(!(LogFile = fopen(LogPath, "a+"))) - { - perror("LogUsage"); - return; - } - } + char *WorkingPath = MakeString0("l", &Path); + wordexp(WorkingPath, &Expansions, Flags); + Free(WorkingPath); - fprintf(LogFile, "%s,%ld,%d\n", - Buffer->ID, - Buffer->Ptr - Buffer->Location, - Buffer->Size); - fclose(LogFile); -#endif -} - -__attribute__ ((format (printf, 2, 3))) -void -LogError(int LogLevel, char *Format, ...) -{ - if(Config.LogLevel >= LogLevel) + char *Result = 0; + if(Expansions.we_wordc > 0) { - char LogPath[256]; - CopyString(LogPath, sizeof(LogPath), "%s/%s", Config.CacheDir, "errors.log"); - FILE *LogFile; - if(!(LogFile = fopen(LogPath, "a+"))) + if(Expansions.we_wordv[0][0] != '/') { - MakeDir(Config.CacheDir); - if(!(LogFile = fopen(LogPath, "a+"))) + if(RelativeToFile) { - perror("LogUsage"); - return; + string RelativeDir = StripComponentFromPath(*RelativeToFile); + Result = MakeString0("lss", &RelativeDir, "/", Expansions.we_wordv[0]); + } + else + { + Result = MakeString0("sss", getenv("PWD"), "/", Expansions.we_wordv[0]); } } - - va_list Args; - va_start(Args, Format); - vfprintf(LogFile, Format, Args); - va_end(Args); - // TODO(matt): Include the LogLevel "string" and the current wall time - fprintf(LogFile, "\n"); - fclose(LogFile); + else + { + Result = MakeString0("s", Expansions.we_wordv[0]); + } } + + wordfree(&Expansions); + + ResolvePath(&Result); + return Result; } -int -ReadFileIntoBuffer(file_buffer *File, int BufferPadding) +bool +MakeDir(string Path) { - if(!(File->Handle = fopen(File->Path, "r"))) // TODO(matt): Fuller error handling + for(int i = 1; i < Path.Length; ++i) { - return RC_ERROR_FILE; + if(Path.Base[i] == '/') + { + Path.Base[i] = '\0'; + int ReturnCode = mkdir(Path.Base, 00755); + Path.Base[i] = '/'; + if(ReturnCode == -1 && errno == EACCES) + { + return FALSE; + } + } } - fseek(File->Handle, 0, SEEK_END); - File->FileSize = ftell(File->Handle); - File->Buffer.Size = File->FileSize + 1 + BufferPadding; // NOTE(matt): +1 to accommodate a NULL terminator - fseek(File->Handle, 0, SEEK_SET); - - // TODO(matt): Consider using the MemoryArena? Maybe have separate ReadFileIntoMemory() and ReadFileIntoArena() - if(!(File->Buffer.Location = malloc(File->Buffer.Size))) + char *FullPath = MakeString0("l", &Path); + int ReturnCode = mkdir(FullPath, 00755); + Free(FullPath); + if(ReturnCode == -1 && errno == EACCES) { - fclose(File->Handle); - return RC_ERROR_MEMORY; + return FALSE; } - File->Buffer.Ptr = File->Buffer.Location; - - fread(File->Buffer.Location, File->FileSize, 1, File->Handle); - File->Buffer.Location[File->FileSize] = '\0'; - fclose(File->Handle); - File->Buffer.ID = File->Path; - return RC_SUCCESS; + return TRUE; } -void -FreeBuffer(buffer *Buffer) -{ - free(Buffer->Location); - Buffer->Location = 0; - Buffer->Ptr = 0; - Buffer->Size = 0; -#if DEBUG_MEM - FILE *MemLog = fopen("/home/matt/cinera_mem", "a+"); - fprintf(MemLog, " Freed %s\n", Buffer->ID); - fclose(MemLog); - printf(" Freed %s\n", Buffer->ID); -#endif - Buffer->ID = 0; -} - -int -ClaimBuffer(buffer *Buffer, char *ID, int Size) -{ - if(MemoryArena.Ptr - MemoryArena.Location + Size > MemoryArena.Size) - { - return RC_ARENA_FULL; - } - Buffer->Location = (char *)MemoryArena.Ptr; - Buffer->Size = Size; - Buffer->ID = ID; - MemoryArena.Ptr += Buffer->Size; - *Buffer->Location = '\0'; - Buffer->Ptr = Buffer->Location; -#if DEBUG - float PercentageUsed = (float)(MemoryArena.Ptr - MemoryArena.Location) / MemoryArena.Size * 100; - printf(" ClaimBuffer(%s): %d\n" - " Total ClaimedMemory: %ld (%.2f%%, leaving %ld free)\n\n", Buffer->ID, Buffer->Size, MemoryArena.Ptr - MemoryArena.Location, PercentageUsed, MemoryArena.Size - (MemoryArena.Ptr - MemoryArena.Location)); -#endif - return RC_SUCCESS; -} - -void -DeclaimBuffer(buffer *Buffer) -{ - *Buffer->Location = '\0'; - MemoryArena.Ptr -= Buffer->Size; - float PercentageUsed = (float)(Buffer->Ptr - Buffer->Location) / Buffer->Size * 100; -#if DEBUG - printf("DeclaimBuffer(%s)\n" - " Used: %ld / %d (%.2f%%)\n" - "\n" - " Total ClaimedMemory: %ld\n\n", - Buffer->ID, - Buffer->Ptr - Buffer->Location, - Buffer->Size, - PercentageUsed, - MemoryArena.Ptr - MemoryArena.Location); -#endif - LogUsage(Buffer); - if(PercentageUsed >= 95.0f) - { - // TODO(matt): Implement either dynamically growing buffers, or phoning home to miblodelcarpio@gmail.com - LogError(LOG_ERROR, "%s used %.2f%% of its allotted memory\n", Buffer->ID, PercentageUsed); - fprintf(stderr, "%sWarning%s: %s used %.2f%% of its allotted memory\n", - ColourStrings[CS_ERROR], ColourStrings[CS_END], - Buffer->ID, PercentageUsed); - } - else if(PercentageUsed >= 80.0f) - { - // TODO(matt): Implement either dynamically growing buffers, or phoning home to miblodelcarpio@gmail.com - LogError(LOG_ERROR, "%s used %.2f%% of its allotted memory\n", Buffer->ID, PercentageUsed); - fprintf(stderr, "%sWarning%s: %s used %.2f%% of its allotted memory\n", - ColourStrings[CS_WARNING], ColourStrings[CS_END], - Buffer->ID, PercentageUsed); - } - Buffer->Size = 0; -} - -void -RewindBuffer(buffer *Buffer) -{ -#if DEBUG - float PercentageUsed = (float)(Buffer->Ptr - Buffer->Location) / Buffer->Size * 100; - printf("Rewinding %s\n" - " Used: %ld / %d (%.2f%%)\n\n", - Buffer->ID, - Buffer->Ptr - Buffer->Location, - Buffer->Size, - PercentageUsed); -#endif - Buffer->Ptr = Buffer->Location; -} - -enum -{ - TEMPLATE_SEARCH, - TEMPLATE_PLAYER, - TEMPLATE_BESPOKE -} template_types; - char * GetDirectoryPath(char *Filepath) { @@ -1491,112 +3596,38 @@ GetDirectoryPath(char *Filepath) return Filepath; } -char * -GetBaseFilename(char *Filepath, - char *Extension // Including the "." - // Pass 0 to retain the whole file path, only without its parent directories - ) -{ - char *BaseFilename = Filepath + StringLength(Filepath) - 1; - while(BaseFilename > Filepath && *BaseFilename != '/') - { - --BaseFilename; - } - if(*BaseFilename == '/') - { - ++BaseFilename; - } - BaseFilename[StringLength(BaseFilename) - StringLength(Extension)] = '\0'; - return BaseFilename; -} - void -ConstructTemplatePath(template *Template, enum8(template_types) Type) +PushTemplateTag(template *Template, int Offset, enum8(template_tag_types) TagType, asset *Asset, navigation_type NavigationType) { - // NOTE(matt): Bespoke template paths are set relative to: - // in Project Edition: ProjectDir - // in Single Edition: Parent directory of .hmml file - - if(Template->File.Path[0] != '/') - { - char Temp[256]; - CopyString(Temp, sizeof(Temp), "%s", Template->File.Path); - char *Ptr = Template->File.Path; - char *End = Template->File.Path + sizeof(Template->File.Path); - if(Type == TEMPLATE_BESPOKE) - { - if(Config.Edition == EDITION_SINGLE) - { - Ptr += CopyString(Ptr, End - Ptr, "%s/", GetDirectoryPath(Config.SingleHMMLFilePath)); - } - else - { - Ptr += CopyString(Ptr, End - Ptr, "%s/", Config.ProjectDir); - } - } - else - { - Ptr += CopyString(Ptr, End - Ptr, "%s/", Config.TemplatesDir); - } - CopyString(Ptr, End - Ptr, "%s", Temp); - } -} - -void -FitTemplateTag(template *Template) -{ - int BlockSize = 16; - if(Template->Metadata.TagCount == Template->Metadata.TagCapacity) - { - Template->Metadata.TagCapacity += BlockSize; - if(Template->Metadata.Tags) - { - Template->Metadata.Tags = realloc(Template->Metadata.Tags, Template->Metadata.TagCapacity * sizeof(*Template->Metadata.Tags)); - } - else - { - Template->Metadata.Tags = calloc(Template->Metadata.TagCapacity, sizeof(*Template->Metadata.Tags)); - } - } -} - -void -PushTemplateTag(template *Template, int Offset, enum8(template_tag_types) TagType, int AssetIndex) -{ - FitTemplateTag(Template); + Template->Metadata.Tags = Fit(Template->Metadata.Tags, sizeof(*Template->Metadata.Tags), Template->Metadata.TagCount, 16, FALSE); Template->Metadata.Tags[Template->Metadata.TagCount].Offset = Offset; Template->Metadata.Tags[Template->Metadata.TagCount].TagCode = TagType; - Template->Metadata.Tags[Template->Metadata.TagCount].AssetIndex = AssetIndex; + Template->Metadata.Tags[Template->Metadata.TagCount].Asset = Asset; + Template->Metadata.Tags[Template->Metadata.TagCount].NavigationType = NavigationType; ++Template->Metadata.TagCount; } void -ClearTemplateMetadata(template *Template) +InitTemplate(template *Template, string Location, template_type Type) { - Template->Metadata.TagCapacity = 0; - Template->Metadata.TagCount = 0; - Template->Metadata.Validity = 0; -} - -void -InitTemplate(template *Template, char *Location, enum8(template_types) Type) -{ - CopyStringNoFormat(Template->File.Path, sizeof(Template->File.Path), Location); - ConstructTemplatePath(Template, Type); + Template->Metadata.Type = Type; + if(Type == TEMPLATE_GLOBAL_SEARCH) + { + Template->File = InitFile(&Config->GlobalTemplatesDir, &Config->GlobalSearchTemplatePath, EXT_NULL); + } + else + { + if(Location.Base[0] == '/') + { + Template->File = InitFile(0, &Location, EXT_NULL); + } + else + { + Template->File = InitFile(Type == TEMPLATE_BESPOKE ? &CurrentProject->HMMLDir : &CurrentProject->TemplatesDir, &Location, EXT_NULL); + } + } printf("%sPacking%s template: %s\n", ColourStrings[CS_ONGOING], ColourStrings[CS_END], Template->File.Path); - ReadFileIntoBuffer(&Template->File, 0); - ClearTemplateMetadata(Template); -} - -void -FreeTemplate(template *Template) -{ - FreeBuffer(&Template->File.Buffer); - Clear(Template->File.Path, sizeof(Template->File.Path)); - Template->File.FileSize = 0; - - free(Template->Metadata.Tags); - Template->Metadata.Tags = 0; + ReadFileIntoBuffer(&Template->File); ClearTemplateMetadata(Template); } @@ -1668,17 +3699,16 @@ CharToColour(char Char) } void -StringToColourHash(hsl_colour *Colour, char *String) +StringToColourHash(hsl_colour *Colour, string String) { Colour->Hue = 0; Colour->Saturation = 0; Colour->Lightness = 74; - int i; - for(i = 0; String[i]; ++i) + for(int i = 0; i < String.Length; ++i) { - Colour->Hue += CharToColour(String[i]).Hue; - Colour->Saturation += CharToColour(String[i]).Saturation; + Colour->Hue += CharToColour(String.Base[i]).Hue; + Colour->Saturation += CharToColour(String.Base[i]).Saturation; } Colour->Hue = Colour->Hue % 360; @@ -1706,77 +3736,72 @@ SanitisePunctuation(char *String) return String; } -enum -{ - PAGE_PLAYER = 1 << 0, - PAGE_SEARCH = 1 << 1 -} pages; - +// TODO(matt): Use ExtendString0() +// AFD void -ConstructURLPrefix(buffer *URLPrefix, enum8(asset_types) AssetType, enum8(pages) PageType) +ConstructURLPrefix(buffer *URLPrefix, asset_type AssetType, enum8(pages) PageType) { RewindBuffer(URLPrefix); - if(StringsDiffer(Config.RootURL, "")) + if(Config->AssetsRootURL.Length > 0) { - CopyStringToBufferHTMLPercentEncoded(URLPrefix, Config.RootURL); + CopyStringToBufferHTMLPercentEncoded(URLPrefix, Config->AssetsRootURL); CopyStringToBuffer(URLPrefix, "/"); } else { - if(Config.Edition == EDITION_PROJECT) + if(PageType == PAGE_PLAYER) { - if(PageType == PAGE_PLAYER) - { - CopyStringToBuffer(URLPrefix, "../"); - } CopyStringToBuffer(URLPrefix, "../"); } + CopyStringToBuffer(URLPrefix, "../"); } switch(AssetType) { case ASSET_CSS: - if(StringsDiffer(Config.CSSDir, "")) + if(Config->CSSDir.Length > 0) { - CopyStringToBufferHTMLPercentEncoded(URLPrefix, Config.CSSDir); + CopyStringToBufferHTMLPercentEncoded(URLPrefix, Config->CSSDir); CopyStringToBuffer(URLPrefix, "/"); } break; case ASSET_IMG: - if(StringsDiffer(Config.ImagesDir, "")) + if(Config->ImagesDir.Length > 0 ) { - CopyStringToBufferHTMLPercentEncoded(URLPrefix, Config.ImagesDir); + CopyStringToBufferHTMLPercentEncoded(URLPrefix, Config->ImagesDir); CopyStringToBuffer(URLPrefix, "/"); } break; case ASSET_JS: - if(StringsDiffer(Config.JSDir, "")) + if(Config->JSDir.Length > 0) { - CopyStringToBufferHTMLPercentEncoded(URLPrefix, Config.JSDir); + CopyStringToBufferHTMLPercentEncoded(URLPrefix, Config->JSDir); CopyStringToBuffer(URLPrefix, "/"); } break; + default: break; } } +// TODO(matt): Do a PushSpeaker() type of thing typedef struct { - char Abbreviation[32]; hsl_colour Colour; - credential_info *Credential; + person *Person; + char *Abbreviation; bool Seen; } speaker; typedef struct { - speaker Speaker[16]; - int Count; + speaker *Speaker; + uint64_t Count; } speakers; enum { CreditsError_NoHost, - CreditsError_NoAnnotator, + CreditsError_NoIndexer, CreditsError_NoCredentials } credits_errors; @@ -1808,31 +3833,37 @@ StringToFletcher32(const char *Data, size_t Length) return (c1 << 16 | c0); } -void -ConstructAssetPath(file_buffer *AssetFile, char *Filename, int Type) +char * +ConstructAssetPath(file *AssetFile, string Filename, asset_type Type) { - buffer Path; - ClaimBuffer(&Path, "Path", Kilobytes(4)); - if(StringsDiffer(Config.RootDir, "")) + char *Result = 0; + if(Config->AssetsRootDir.Length > 0) { - CopyStringToBuffer(&Path, "%s", Config.RootDir); + ExtendString0(&Result, Config->AssetsRootDir); } - char *AssetDir = 0; + string AssetDir = {}; switch(Type) { - case ASSET_CSS: AssetDir = Config.CSSDir; break; - case ASSET_IMG: AssetDir = Config.ImagesDir; break; - case ASSET_JS: AssetDir = Config.JSDir; break; + case ASSET_CSS: AssetDir = Config->CSSDir; break; + case ASSET_IMG: AssetDir = Config->ImagesDir; break; + case ASSET_JS: AssetDir = Config->JSDir; break; + default: break; } - if(AssetDir && StringsDiffer(AssetDir, "")) { CopyStringToBuffer(&Path, "/%s", AssetDir); } - if(Filename) { CopyStringToBuffer(&Path, "/%s", Filename); } - - CopyString(AssetFile->Path, sizeof(AssetFile->Path), Path.Location); - DeclaimBuffer(&Path); + if(AssetDir.Length > 0) + { + ExtendString0(&Result, Wrap0("/")); + ExtendString0(&Result, AssetDir); + } + if(Filename.Length > 0) + { + ExtendString0(&Result, Wrap0("/")); + ExtendString0(&Result, Filename); + } + return Result; } void -CycleFile(file_buffer *File) +CycleFile(file *File) { fclose(File->Handle); // TODO(matt): Rather than freeing the buffer, why not just realloc it to fit the new file? Couldn't that work easily? @@ -1840,76 +3871,66 @@ CycleFile(file_buffer *File) // If we switch the whole database over to use linked lists - the database being the only file we actually cycle // at the moment - then we may actually obviate the need to cycle at all FreeBuffer(&File->Buffer); - ReadFileIntoBuffer(File, 0); + ReadFileIntoBuffer(File); } void -ConstructDirectoryPath(buffer *DirectoryPath, int PageType, char *PageLocation, char *BaseFilename) +SetSignpostOffset(buffer *B, file_edit *E, file_signpost *S) { - RewindBuffer(DirectoryPath); - CopyStringToBuffer(DirectoryPath, "%s", Config.BaseDir); - switch(PageType) + S->Byte = (char *)S->Ptr - B->Location; + if(S->Byte >= E->BytePosition) { - case PAGE_SEARCH: - if(StringsDiffer(PageLocation, "")) - { - CopyStringToBuffer(DirectoryPath, "/%s", PageLocation); - } - break; - case PAGE_PLAYER: - if(StringsDiffer(PageLocation, "")) - { - CopyStringToBuffer(DirectoryPath, "/%s", PageLocation); - } - if(BaseFilename) - { - if(StringsDiffer(Config.PlayerURLPrefix, "")) - { - char *Ptr = BaseFilename + StringLength(Config.ProjectID); - CopyStringToBuffer(DirectoryPath, "/%s%s", Config.PlayerURLPrefix, Ptr); - } - else - { - CopyStringToBuffer(DirectoryPath, "/%s", BaseFilename); - } - } - break; + S->Byte += E->Size; } } -int -ReadSearchPageIntoBuffer(file_buffer *File) -{ - buffer SearchPagePath; - ClaimBuffer(&SearchPagePath, "SearchPagePath", MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + 10); - - ConstructDirectoryPath(&SearchPagePath, PAGE_SEARCH, Config.SearchLocation, 0); - CopyString(File->Path, sizeof(File->Path), "%s/index.html", SearchPagePath.Location); - DeclaimBuffer(&SearchPagePath); - return(ReadFileIntoBuffer(File, 0)); -} - -int -ReadPlayerPageIntoBuffer(file_buffer *File, db_entry *Entry) -{ - buffer PlayerPagePath; - ClaimBuffer(&PlayerPagePath, "PlayerPagePath", MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_PLAYER_URL_PREFIX_LENGTH + MAX_BASE_FILENAME_LENGTH + 1 + 10); - - ConstructDirectoryPath(&PlayerPagePath, PAGE_PLAYER, Config.PlayerLocation, Entry->BaseFilename); - CopyString(File->Path, sizeof(File->Path), "%s/index.html", PlayerPagePath.Location); - DeclaimBuffer(&PlayerPagePath); - return(ReadFileIntoBuffer(File, 0)); -} - void -ClearTerminalRow(int Length) +CycleSignpostedFile(file_signposted *File) { - fprintf(stderr, "\r"); - for(int i = 0; i < Length; ++i) + // NOTE(matt) This file_signposted struct is totally hardcoded to the Metadata file, but may suffice for now + // + // TODO(matt): This is probably insufficient. We likely need to offset the pointers any time we add stuff to the database + fclose(File->File.Handle); + // TODO(matt): Rather than freeing the buffer, why not just realloc it to fit the new file? Couldn't that work easily? + // The reason we Free / Reread is to save us having to shuffle the buffer contents around, basically + // If we switch the whole database over to use linked lists - the database being the only file we actually cycle + // at the moment - then we may actually obviate the need to cycle at all + file_signposts *S = &File->Signposts; + + buffer *B = &File->File.Buffer; + + uint64_t PtrByte = B->Ptr - B->Location; + if(S->ProjectsBlock.Ptr) { SetSignpostOffset(B, &S->Edit, &S->ProjectsBlock); } + if(S->ProjectParent.Ptr) { SetSignpostOffset(B, &S->Edit, &S->ProjectParent); } + if(S->ProjectHeader.Ptr) { SetSignpostOffset(B, &S->Edit, &S->ProjectHeader); } + if(S->Prev.Ptr) { SetSignpostOffset(B, &S->Edit, &S->Prev); } + if(S->This.Ptr) { SetSignpostOffset(B, &S->Edit, &S->This); } + if(S->Next.Ptr) { SetSignpostOffset(B, &S->Edit, &S->Next); } + if(S->AssetsBlock.Ptr) { SetSignpostOffset(B, &S->Edit, &S->AssetsBlock); } + + FreeBuffer(B); + ReadFileIntoBuffer(&File->File); + + B->Ptr = B->Location + PtrByte; + if(S->ProjectsBlock.Ptr) { S->ProjectsBlock.Ptr = B->Location + S->ProjectsBlock.Byte; } + if(S->ProjectParent.Ptr) { S->ProjectParent.Ptr = B->Location + S->ProjectParent.Byte; } + if(S->ProjectHeader.Ptr) { S->ProjectHeader.Ptr = B->Location + S->ProjectHeader.Byte; } + if(S->Prev.Ptr) { S->Prev.Ptr = B->Location + S->Prev.Byte; } + if(S->This.Ptr) { S->This.Ptr = B->Location + S->This.Byte; } + if(S->Next.Ptr) { S->Next.Ptr = B->Location + S->Next.Byte; } + if(S->AssetsBlock.Ptr) { S->AssetsBlock.Ptr = B->Location + S->AssetsBlock.Byte; } + + S->Edit.BytePosition = 0; + S->Edit.Size = 0; +} + +void +ClearTerminalRow(uint64_t Length) +{ + for(uint64_t i = 0; i < Length; ++i) { - fprintf(stderr, " "); + fprintf(stderr, "\b \b"); } - fprintf(stderr, "\r"); } typedef struct @@ -1918,96 +3939,134 @@ typedef struct uint32_t Length; } landmark_range; +bool +ProjectIndicesMatch(db_project_index A, db_project_index B) +{ + return (A.Generation == B.Generation && A.Index == B.Index); +} + +db_landmark * +LocateFirstLandmark(db_asset *A) +{ + char *Ptr = (char *)A; + Ptr += sizeof(*A); + return (db_landmark *)Ptr; +} + +db_landmark * +LocateLandmark(db_asset *A, uint64_t Index) +{ + char *Ptr = (char *)A; + Ptr += sizeof(*A) + sizeof(db_landmark) * Index; + return (db_landmark *)Ptr; +} + uint32_t -GetIndexRangeLength(void *FirstLandmark, int EntryIndex, int LandmarkIndex, int LandmarkCount) +GetIndexRangeLength(db_asset *A, landmark_range ProjectRange, uint64_t LandmarkIndex) { - uint32_t Result = 1; - db_landmark Landmark = *(db_landmark*)(FirstLandmark + sizeof(Landmark) * LandmarkIndex); - while(EntryIndex == Landmark.EntryIndex && LandmarkIndex < LandmarkCount - 1) + uint32_t Result = 0; + db_landmark *FirstLandmarkOfRange = LocateLandmark(A, LandmarkIndex); + db_landmark *Landmark = FirstLandmarkOfRange; + while(LandmarkIndex < (ProjectRange.First + ProjectRange.Length) && ProjectIndicesMatch(FirstLandmarkOfRange->Project, Landmark->Project) && FirstLandmarkOfRange->EntryIndex == Landmark->EntryIndex) { + ++Result; ++LandmarkIndex; - Landmark = *(db_landmark*)(FirstLandmark + sizeof(Landmark) * LandmarkIndex); - if(EntryIndex == Landmark.EntryIndex) - { - ++Result; - } + ++Landmark; } + return Result; } landmark_range -GetIndexRange(void *FirstLandmark, int EntryIndex, int LandmarkIndex, int LandmarkCount) +GetIndexRange(db_asset *A, landmark_range ProjectRange, uint64_t LandmarkIndex) { landmark_range Result = {}; - db_landmark Landmark = *(db_landmark*)(FirstLandmark + sizeof(Landmark) * LandmarkIndex); - while(EntryIndex == Landmark.EntryIndex && LandmarkIndex > 0) - { - --LandmarkIndex; - Landmark = *(db_landmark*)(FirstLandmark + sizeof(Landmark) * LandmarkIndex); - } - if(Landmark.EntryIndex != EntryIndex) - { - ++LandmarkIndex; - } - - Landmark = *(db_landmark*)(FirstLandmark + sizeof(Landmark) * LandmarkIndex); - Result.First = LandmarkIndex; - Result.Length = GetIndexRangeLength(FirstLandmark, EntryIndex, LandmarkIndex, LandmarkCount); + db_landmark *LandmarkInRange = LocateLandmark(A, LandmarkIndex); + db_landmark *Landmark = LandmarkInRange; + + while(Result.First > ProjectRange.First && ProjectIndicesMatch(LandmarkInRange->Project, Landmark->Project) && LandmarkInRange->EntryIndex == Landmark->EntryIndex) + { + --Result.First; + --Landmark; + } + + if(LandmarkInRange->EntryIndex != Landmark->EntryIndex) + { + ++Result.First; + } + + Result.Length = GetIndexRangeLength(A, ProjectRange, Result.First); return Result; } -landmark_range -BinarySearchForMetadataLandmark(void *FirstLandmark, int EntryIndex, int LandmarkCount) +int +ProjectIndicesDiffer(db_project_index A, db_project_index B) { - // NOTE(matt): Depends on FirstLandmark being positioned after an Asset "header" - landmark_range Result = {}; - if(LandmarkCount > 0) + if(A.Generation < B.Generation || (A.Generation == B.Generation && A.Index < B.Index)) { - int Lower = 0; - db_landmark *LowerLandmark = (db_landmark*)(FirstLandmark + sizeof(LowerLandmark) * Lower); + return -1; + } + else if(A.Generation > B.Generation || (A.Generation == B.Generation && A.Index > B.Index)) + { + return 1; + } + else + { + return 0; + } +} + +landmark_range +BinarySearchForMetadataLandmark(db_asset *Asset, landmark_range ProjectRange, int EntryIndex) +{ + landmark_range Result = ProjectRange; + if(ProjectRange.Length > 0) + { + db_landmark *FirstLandmark = LocateFirstLandmark(Asset); + + uint64_t Lower = ProjectRange.First; + db_landmark *LowerLandmark = FirstLandmark + Lower; + + uint64_t Upper = Lower + ProjectRange.Length - 1; + db_landmark *UpperLandmark = FirstLandmark + Upper; + if(EntryIndex < LowerLandmark->EntryIndex) { - Result.First = 0; + Result.First = Lower; Result.Length = 0; return Result; } - int Upper = LandmarkCount - 1; - db_landmark *UpperLandmark; // TODO(matt): Is there a slicker way of doing this? - if(Upper >= 0) + if(EntryIndex > UpperLandmark->EntryIndex) { - UpperLandmark = (db_landmark*)(FirstLandmark + sizeof(db_landmark) * Upper); - if(EntryIndex > UpperLandmark->EntryIndex) - { - Result.First = LandmarkCount; - Result.Length = 0; - return Result; - } + Result.First = Upper + 1; + Result.Length = 0; + return Result; } int Pivot = Upper - ((Upper - Lower) >> 1); db_landmark *PivotLandmark; do { - LowerLandmark = (db_landmark*)(FirstLandmark + sizeof(db_landmark) * Lower); - PivotLandmark = (db_landmark*)(FirstLandmark + sizeof(db_landmark) * Pivot); - UpperLandmark = (db_landmark*)(FirstLandmark + sizeof(db_landmark) * Upper); + LowerLandmark = FirstLandmark + Lower; + PivotLandmark = FirstLandmark + Pivot; + UpperLandmark = FirstLandmark + Upper; if(EntryIndex == LowerLandmark->EntryIndex) { - return GetIndexRange(FirstLandmark, EntryIndex, Lower, LandmarkCount); + return GetIndexRange(Asset, ProjectRange, Lower); } if(EntryIndex == PivotLandmark->EntryIndex) { - return GetIndexRange(FirstLandmark, EntryIndex, Pivot, LandmarkCount); + return GetIndexRange(Asset, ProjectRange, Pivot); } if(EntryIndex == UpperLandmark->EntryIndex) { - return GetIndexRange(FirstLandmark, EntryIndex, Upper, LandmarkCount); + return GetIndexRange(Asset, ProjectRange, Upper); } if(EntryIndex < PivotLandmark->EntryIndex) { Upper = Pivot; } @@ -2022,58 +4081,680 @@ BinarySearchForMetadataLandmark(void *FirstLandmark, int EntryIndex, int Landmar } void -SnipeChecksumAndCloseFile(file_buffer *File, void *FirstLandmark, int LandmarksInFile, buffer *Checksum, int *RunningLandmarkIndex) +PrintEntryIndex(db_project_index Project, int64_t EntryIndex) { + fprintf(stderr, "%s%i:%i%s %s%3li%s", + ColourStrings[CS_MAGENTA], Project.Generation, Project.Index, ColourStrings[CS_END], + ColourStrings[CS_BLUE_BOLD], EntryIndex, ColourStrings[CS_END]); +} + +void +PrintLandmark(db_landmark *L) +{ + PrintEntryIndex(L->Project, L->EntryIndex); + fprintf(stderr, " %6u", L->Position); +} + +void +PrintAsset(db_asset *A, uint16_t *Index) +{ + if(Index) + { + Colourise(CS_BLACK_BOLD); + fprintf(stderr, "[%i]", *Index); + Colourise(CS_END); + } + string FilenameL = Wrap0i(A->Filename, sizeof(A->Filename)); + fprintf(stderr, " %s asset: %.*s [%8x]\n", + AssetTypeNames[A->Type], (int)FilenameL.Length, FilenameL.Base, A->Hash); + + if(A->Type == ASSET_IMG) + { + bool Associated = A->Associated; + uint64_t Variants = A->Variants; + if(Index) + { + int SpacesRequired = 1 + 1 + DigitsInInt(Index); + for(int i = 0; i < SpacesRequired; ++i) + { + fprintf(stderr, " "); + } + } + fprintf(stderr, " Associated: "); + if(Associated) { PrintC(CS_GREEN, "true"); } + else { PrintC(CS_RED, "false"); } + fprintf(stderr, " • Variants: "); + Colourise(CS_BLUE_BOLD); + fprintf(stderr, "%lu", Variants); + Colourise(CS_END); + fprintf(stderr, " • Dimensions: "); + Colourise(CS_BLUE_BOLD); + fprintf(stderr, "%u", A->Width); + fprintf(stderr, "×"); + fprintf(stderr, "%u", A->Height); + Colourise(CS_END); + fprintf(stderr, "\n"); + } +} + +void +PrintAssetAndLandmarks(db_asset *A, uint16_t *Index) +{ + fprintf(stderr, "\n" + "\n"); + PrintAsset(A, Index); + db_landmark *FirstLandmark = LocateFirstLandmark(A); + //Colourise(CS_BLACK_BOLD); fprintf(stderr, "%4u ", 0); Colourise(CS_END); + //PrintLandmark(FirstLandmark); + for(int i = 0; i < A->LandmarkCount; ++i) + { + db_landmark *This = FirstLandmark + i; + if((i % 8) == 0) + { + fprintf(stderr, "\n"); + } + else + { + PrintC(CS_BLACK_BOLD, " │ "); + } + Colourise(CS_BLACK_BOLD); fprintf(stderr, "%4u ", i); Colourise(CS_END); + PrintLandmark(This); + } + fprintf(stderr, "\n"); +} + +// TODO(matt): Almost definitely redo this using Locate*() functions... +void +SnipeChecksumAndCloseFile(file *HTMLFile, db_asset *Asset, int LandmarksInFile, buffer *Checksum, int64_t *RunningLandmarkIndex) +{ + db_landmark *FirstLandmark = LocateFirstLandmark(Asset); for(int j = 0; j < LandmarksInFile; ++j, ++*RunningLandmarkIndex) { - db_landmark Landmark = *(db_landmark *)(FirstLandmark + sizeof(DB.Landmark) * *RunningLandmarkIndex); + db_landmark *Landmark = FirstLandmark + *RunningLandmarkIndex; - File->Buffer.Ptr = File->Buffer.Location + Landmark.Position; - CopyBufferSized(&File->Buffer, Checksum, Checksum->Ptr - Checksum->Location); + HTMLFile->Buffer.Ptr = HTMLFile->Buffer.Location + Landmark->Position; + CopyBufferSized(&HTMLFile->Buffer, Checksum, Checksum->Ptr - Checksum->Location); } - File->Handle = fopen(File->Path, "w"); - fwrite(File->Buffer.Location, File->FileSize, 1, File->Handle); - fclose(File->Handle); - FreeBuffer(&File->Buffer); + HTMLFile->Handle = fopen(HTMLFile->Path, "w"); + fwrite(HTMLFile->Buffer.Location, HTMLFile->Buffer.Size, 1, HTMLFile->Handle); + fclose(HTMLFile->Handle); + HTMLFile->Handle = 0; + FreeFile(HTMLFile); +} + +// TODO(matt): Bounds-check the Metadata.File.Buffer +void * +SkipAsset(db_asset *Asset) +{ + char *Ptr = (char *)Asset; + Ptr += sizeof(db_asset) + sizeof(db_landmark) * Asset->LandmarkCount; + return Ptr; +} + +void * +SkipAssetsBlock(db_block_assets *Block) +{ + char *Ptr = (char *)Block; + Ptr += sizeof(db_block_assets); + for(int i = 0; i < Block->Count; ++i) + { + db_asset *Asset = (db_asset *)Ptr; + Ptr = SkipAsset(Asset); + } + return Ptr; +} + +typedef struct +{ + uint64_t CurrentGeneration; + uint64_t Count; + uint32_t *EntriesInGeneration; +} project_generations; + +void +PushGeneration(project_generations *G) +{ + G->EntriesInGeneration = Fit(G->EntriesInGeneration, sizeof(*G->EntriesInGeneration), G->Count, 4, TRUE); + ++G->Count; +} + +db_project_index +GetCurrentProjectIndex(project_generations *G) +{ + db_project_index Result = {}; + Result.Generation = G->CurrentGeneration; + Result.Index = G->EntriesInGeneration[G->CurrentGeneration]; + return Result; } void -SnipeChecksumIntoHTML(void *FirstLandmark, buffer *Checksum) +AddEntryToGeneration(project_generations *G, project *P) { - landmark_range SearchRange = BinarySearchForMetadataLandmark(FirstLandmark, PAGE_TYPE_SEARCH, DB.Asset.LandmarkCount); - int RunningLandmarkIndex = 0; - if(SearchRange.Length > 0) + if(G) { - file_buffer HTML; - ReadSearchPageIntoBuffer(&HTML); - SnipeChecksumAndCloseFile(&HTML, FirstLandmark, SearchRange.Length, Checksum, &RunningLandmarkIndex); - } - - for(; RunningLandmarkIndex < DB.Asset.LandmarkCount;) - { - db_landmark Landmark = *(db_landmark *)(FirstLandmark + sizeof(Landmark) * RunningLandmarkIndex); - - db_entry Entry = *(db_entry *)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * Landmark.EntryIndex); - int Length = GetIndexRangeLength(FirstLandmark, Landmark.EntryIndex, RunningLandmarkIndex, DB.Asset.LandmarkCount); - file_buffer HTML; - ReadPlayerPageIntoBuffer(&HTML, &Entry); - SnipeChecksumAndCloseFile(&HTML, FirstLandmark, Length, Checksum, &RunningLandmarkIndex); + if(G->Count <= G->CurrentGeneration) + { + PushGeneration(G); + } + if(P) + { + P->Index = GetCurrentProjectIndex(G); + } + ++G->EntriesInGeneration[G->CurrentGeneration]; } } void -PrintWatchHandles(void) +IncrementCurrentGeneration(project_generations *G) { - printf("\n" - "PrintWatchHandles()\n"); + if(G) { ++G->CurrentGeneration; } +} + +void +DecrementCurrentGeneration(project_generations *G) +{ + if(G) { --G->CurrentGeneration; } +} + +void +PrintGenerations(project_generations *G, bool IndicateCurrentGeneration) +{ + for(uint64_t i = 0; i < G->Count; ++i) + { + fprintf(stderr, "%lu: %u%s\n", i, G->EntriesInGeneration[i], IndicateCurrentGeneration && i == G->CurrentGeneration ? " [Current Generation]" : ""); + } +} + +void +FreeGenerations(project_generations *G) +{ + FreeAndResetCount(G->EntriesInGeneration, G->Count); +} + +void * +SkipProject(db_header_project *Project) +{ + char *Ptr = (char *)Project; + Ptr += sizeof(db_header_project) + sizeof(db_entry) * Project->EntryCount; + return Ptr; +} + +void * +SkipProjectAndChildren(db_header_project *Project) +{ + db_header_project *Ptr = SkipProject(Project); + for(int i = 0; i < Project->ChildCount; ++i) + { + Ptr = SkipProjectAndChildren(Ptr); + } + return Ptr; +} + +void * +SkipProjectsBlock(db_block_projects *Block) +{ + char *Ptr = (char *)Block; + Ptr += sizeof(db_block_projects); + for(int i = 0; i < Block->Count; ++i) + { + db_header_project *Project = (db_header_project *)Ptr; + Ptr = SkipProjectAndChildren(Project); + } + return Ptr; +} + +typedef enum +{ + B_ASET, + B_PROJ, +} block_id; + +uint32_t +GetFourFromBlockID(block_id ID) +{ + switch(ID) + { + case B_ASET: return FOURCC("ASET"); + case B_PROJ: return FOURCC("PROJ"); + } + return 0; +} + +void * +SkipBlock(void *Block) +{ + void *Result = 0; + + uint32_t FirstInt = *(uint32_t *)Block; + if(FirstInt == GetFourFromBlockID(B_PROJ)) + { + Result = SkipProjectsBlock(Block); + } + else if(FirstInt == GetFourFromBlockID(B_ASET)) + { + Result = SkipAssetsBlock(Block); + } + Assert(Result); + return Result; +} + +void * +LocateBlock(block_id BlockID) +{ + void *Result = 0; + + db_header *Header = (db_header *)DB.Metadata.File.Buffer.Location; + char *Ptr = (char *)Header; + Ptr += sizeof(db_header); + for(int i = 0; i < Header->BlockCount; ++i) + { + uint32_t FirstInt = *(uint32_t *)Ptr; + if(FirstInt == GetFourFromBlockID(BlockID)) + { + Result = Ptr; + break; + } + else + { + Ptr = SkipBlock(Ptr); + } + } + return Result; +} + +db_header_project * +LocateFirstChildProjectOfBlock(db_block_projects *P) +{ + char *Ptr = (char *)P; + Ptr += sizeof(*P); + return (db_header_project *)Ptr; +} + +db_header_project * +LocateFirstChildProject(db_header_project *P) +{ + char *Ptr = (char *)P; + Ptr += sizeof(*P) + sizeof(db_entry) * P->EntryCount; + return (db_header_project *)Ptr; +} + +// TODO(matt): Test this project index accumulation stuff, for goodness' sake! +db_header_project * +LocateProjectRecursively(db_header_project *Header, db_project_index *DesiredProject, db_project_index *Accumulator) +{ + db_header_project *Child = LocateFirstChildProject(Header); + + for(int i = 0; i < Header->ChildCount; ++i) + { + if(Accumulator->Generation == DesiredProject->Generation) + { + if(Accumulator->Index == DesiredProject->Index) + { + return Child; + } + else + { + ++Accumulator->Index; + } + } + + ++Accumulator->Generation; + db_header_project *Test = LocateProjectRecursively(Child, DesiredProject, Accumulator); + if(Test) + { + return Test; + } + --Accumulator->Generation; + Child = SkipProjectAndChildren(Child); + } + return 0; +} + +db_header_project * +LocateProject(db_project_index Project) +{ + if(Project.Generation != -1) + { + db_block_projects *ProjectsBlock = DB.Metadata.Signposts.ProjectsBlock.Ptr ? DB.Metadata.Signposts.ProjectsBlock.Ptr : LocateBlock(B_PROJ); + db_header_project *Child = LocateFirstChildProjectOfBlock(ProjectsBlock); + + db_project_index Accumulator = {}; + if(ProjectsBlock) + { + if(Project.Generation == Accumulator.Generation) + { + for(int i = 0; i < ProjectsBlock->Count; ++i) + { + if(Project.Index == Accumulator.Index) + { + return Child; + } + Child = SkipProjectAndChildren(Child); + ++Accumulator.Index; + } + } + else + { + ++Accumulator.Generation; + for(int i = 0; i < ProjectsBlock->Count; ++i) + { + db_header_project *Test = LocateProjectRecursively(Child, &Project, &Accumulator); + if(Test) + { + return Test; + } + Child = SkipProjectAndChildren(Child); + } + --Accumulator.Generation; + } + } + } + return 0; +} + +db_entry * +LocateFirstEntry(db_header_project *P) +{ + char *Ptr = (char *)P; + Ptr += sizeof(db_header_project); + return (db_entry *)Ptr; +} + +db_entry * +LocateEntry(db_project_index DBProjectIndex, int32_t EntryIndex) +{ + db_header_project *Project = LocateProject(DBProjectIndex); + if(Project && EntryIndex < Project->EntryCount) + { + char *Ptr = (char *)Project; + Ptr += sizeof(db_header_project) + sizeof(db_entry) * EntryIndex; + db_entry *Result = (db_entry *)Ptr; + return Result; + } + return 0; +} + +void +SetFileEditPosition(file_signposted *File) +{ + File->Signposts.Edit.BytePosition = ftell(File->File.Handle); + File->Signposts.Edit.Size = 0; +} + +void +AccumulateFileEditSize(file_signposted *File, int64_t Bytes) +{ + File->Signposts.Edit.Size += Bytes; +} + +// TODO(matt): Consider enforcing an order of these blocks, basically putting the easy-to-skip ones first... +void * +InitBlock(block_id ID) +{ + db_header *Header = (db_header *)DB.Metadata.File.Buffer.Location; + ++Header->BlockCount; + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + fwrite(DB.Metadata.File.Buffer.Location, DB.Metadata.File.Buffer.Size, 1, DB.Metadata.File.Handle); + + SetFileEditPosition(&DB.Metadata); + switch(ID) + { + case B_ASET: + { + db_block_assets Block = {}; + fwrite(&Block, sizeof(Block), 1, DB.Metadata.File.Handle); + AccumulateFileEditSize(&DB.Metadata, sizeof(Block)); + break; + } + case B_PROJ: + { + db_block_projects Block = {}; + fwrite(&Block, sizeof(Block), 1, DB.Metadata.File.Handle); + AccumulateFileEditSize(&DB.Metadata, sizeof(Block)); + break; + } + } + uint32_t BytesIntoFile = DB.Metadata.File.Buffer.Size; + CycleSignpostedFile(&DB.Metadata); + return &DB.Metadata.File.Buffer.Location + BytesIntoFile; +} + +db_asset * +LocateFirstAsset(db_block_assets *B) +{ + char *Ptr = (char *)B; + Ptr += sizeof(*B); + return (db_asset *)Ptr; +} + +#define PrintAssetsBlock(B) PrintAssetsBlock_(B, __LINE__) +void * +PrintAssetsBlock_(db_block_assets *B, int LineNumber) +{ +#if 0 + fprintf(stderr, "[%d] ", LineNumber); + PrintFunctionName("PrintAssetsBlock()"); +#endif + if(!B) { B = LocateBlock(B_ASET); } + PrintC(CS_BLUE_BOLD, "\n" + "\n" + "Assets Block (ASET)"); + db_asset *A = LocateFirstAsset(B); + for(uint16_t i = 0; i < B->Count; ++i) + { + PrintAssetAndLandmarks(A, &i); + A = SkipAsset(A); + } + return A; +} + +char * +ConstructDirectoryPath(db_header_project *P, string *PageLocation, string *EntryOutput) +{ + char *Result = 0; + if(P) + { + ExtendString0(&Result, Wrap0i(P->BaseDir, sizeof(P->BaseDir))); + } + if(PageLocation && PageLocation->Length > 0) + { + if(P) + { + ExtendString0(&Result, Wrap0("/")); + } + ExtendString0(&Result, *PageLocation); + } + + if(EntryOutput) + { + ExtendString0(&Result, Wrap0("/")); + ExtendString0(&Result, *EntryOutput); + } + return Result; +} + +char * +ConstructHTMLIndexFilePath(db_header_project *P, string *PageLocation, string *EntryOutput) +{ + char *Result = ConstructDirectoryPath(P, PageLocation, EntryOutput); + ExtendString0(&Result, Wrap0("/index.html")); + return Result; +} + +void +ReadSearchPageIntoBuffer(db_header_project *P, file *File) +{ + string SearchLocationL = Wrap0i(P->SearchLocation, sizeof(P->SearchLocation)); + File->Path = ConstructHTMLIndexFilePath(P, &SearchLocationL, 0); + ReadFileIntoBuffer(File); +} + +void +ReadGlobalSearchPageIntoBuffer(file *File) +{ + db_block_projects *ProjectsBlock = DB.Metadata.Signposts.ProjectsBlock.Ptr ? DB.Metadata.Signposts.ProjectsBlock.Ptr : LocateBlock(B_PROJ); + string SearchLocationL = Wrap0i(ProjectsBlock->GlobalSearchDir, sizeof(ProjectsBlock->GlobalSearchDir)); + File->Path = ConstructHTMLIndexFilePath(0, &SearchLocationL, 0); + ReadFileIntoBuffer(File); +} + +void +ReadPlayerPageIntoBuffer(db_header_project *P, file *File, db_entry *Entry) +{ + string EntryOutput = Wrap0i(Entry->OutputLocation, sizeof(Entry->OutputLocation)); + string PlayerLocationL = Wrap0i(P->PlayerLocation, sizeof(P->PlayerLocation)); + File->Path = ConstructHTMLIndexFilePath(P, &PlayerLocationL, &EntryOutput); + ReadFileIntoBuffer(File); +} + +rc +SnipeChecksumIntoHTML(db_asset *Asset, buffer *Checksum) +{ + db_landmark *FirstLandmark = LocateFirstLandmark(Asset); + + rc Result = RC_SUCCESS; + + for(int64_t RunningLandmarkIndex = 0; RunningLandmarkIndex < Asset->LandmarkCount;) + { + db_landmark *Landmark = FirstLandmark + RunningLandmarkIndex; + // TODO(matt): Do the Next vs Current Project check to see whether we even need to do LocateProject() + db_header_project *P = LocateProject(Landmark->Project); + + file HTML = {}; + if(Landmark->EntryIndex >= 0) + { + db_entry *Entry = LocateEntry(Landmark->Project, Landmark->EntryIndex); + if(Entry) + { + ReadPlayerPageIntoBuffer(P, &HTML, Entry); + } + else + { + PrintC(CS_ERROR, "\nInvalid landmark (aborting update): "); + PrintAsset(Asset, 0); + PrintLandmark(Landmark); + Result = RC_FAILURE; + break; + } + } + else + { + switch(Landmark->EntryIndex) + { + case SP_SEARCH: + { + if(P) + { + ReadSearchPageIntoBuffer(P, &HTML); + } + else + { + ReadGlobalSearchPageIntoBuffer(&HTML); + } + } break; + default: + { + Colourise(CS_RED); + fprintf(stderr, "SnipeChecksumIntoHTML() does not know about special page with index %i\n", Landmark->EntryIndex); + Colourise(CS_END); + Assert(0); + } break; + } + } + + landmark_range Range = {}; + Range.First = RunningLandmarkIndex; + Range.Length = Asset->LandmarkCount - Range.First; + int Length = GetIndexRangeLength(Asset, Range, RunningLandmarkIndex); + SnipeChecksumAndCloseFile(&HTML, Asset, Length, Checksum, &RunningLandmarkIndex); + } + + return Result; +} + +void +PrintWatchFile(watch_file *W) +{ + fprintf(stderr, "%s", WatchTypeStrings[W->Type]); + fprintf(stderr, " • "); + if(W->Extension != EXT_NULL) + { + fprintf(stderr, "*"); + PrintString(ExtensionStrings[W->Extension]); + } + else + { + PrintString(W->Path); + } + + if(W->Project) + { + fprintf(stderr, " • "); + PrintLineage(W->Project->Lineage, FALSE); + } + + if(W->Asset) + { + fprintf(stderr, " • "); + fprintf(stderr, "%s", AssetTypeNames[W->Asset->Type]); + } +} + +void +PrintWatchHandle(watch_handle *W) +{ + fprintf(stderr, "%4d", W->Descriptor); + + fprintf(stderr, " • "); + if(StringsDiffer(W->TargetPath, W->WatchedPath)) + { + PrintStringC(CS_MAGENTA, W->WatchedPath); + fprintf(stderr, "\n "); + PrintStringC(CS_RED_BOLD, W->TargetPath); + } + else + { + PrintStringC(CS_GREEN_BOLD, W->TargetPath); + } + + for(int i = 0; i < W->FileCount; ++i) + { + watch_file *This = W->Files + i; + fprintf(stderr, "\n" + " "); + PrintWatchFile(This); + } +} + +#define PrintWatchHandles() PrintWatchHandles_(__LINE__) +void +PrintWatchHandles_(int LineNumber) +{ + typography T = + { + .UpperLeftCorner = "┌", + .UpperLeft = "╾", + .Horizontal = "─", + .UpperRight = "╼", + .Vertical = "│", + .LowerLeftCorner = "└", + .LowerLeft = "╽", + .Margin = " ", + .Delimiter = ": ", + .Separator = "•", + }; + fprintf(stderr, "\n" + "%s%s%s [%i] PrintWatchHandles()", T.UpperLeftCorner, T.Horizontal, T.UpperRight, LineNumber); for(int i = 0; i < WatchHandles.Count; ++i) { - printf(" %d • %s • %s\n", - WatchHandles.Handle[i].Descriptor, - WatchHandles.Handle[i].Type == WT_HMML ? "WT_HMML " : "WT_ASSET", - WatchHandles.Handle[i].Path); + watch_handle *This = WatchHandles.Handles + i; + fprintf(stderr, "\n"); + PrintWatchHandle(This); } + + fprintf(stderr, "\n" + "%s%s%s", T.LowerLeftCorner, T.Horizontal, T.UpperRight); } bool @@ -2086,224 +4767,532 @@ IsSymlink(char *Filepath) } void -FitWatchHandle(void) +PushWatchFileUniquely(watch_handle *Handle, string Filepath, extension_id Extension, watch_type Type, project *Project, asset *Asset) { - int BlockSize = 8; - if(WatchHandles.Count == WatchHandles.Capacity) + bool Required = TRUE; + for(int i = 0; i < Handle->FileCount; ++i) { - WatchHandles.Capacity += BlockSize; - if(WatchHandles.Handle) + watch_file *This = Handle->Files + i; + if(This->Type == Type) { - WatchHandles.Handle = realloc(WatchHandles.Handle, WatchHandles.Capacity * sizeof(*WatchHandles.Handle)); + if(Extension != EXT_NULL) + { + if(This->Extension == Extension) + { + Required = FALSE; + break; + } + } + else + { + if(StringsMatch(This->Path, Filepath)) + { + Required = FALSE; + EraseCurrentStringFromBook(&WatchHandles.Paths); + break; + } + } + } + + } + + if(Required) + { + Handle->Files = FitShrinkable(Handle->Files, sizeof(*Handle->Files), Handle->FileCount, &Handle->FileCapacity, 16, TRUE); + watch_file *New = Handle->Files + Handle->FileCount; + if(Extension != EXT_NULL) + { + New->Extension = Extension; } else { - WatchHandles.Handle = calloc(WatchHandles.Capacity, sizeof(*WatchHandles.Handle)); + New->Path = Filepath; } + New->Type = Type; + New->Project = Project; + New->Asset = Asset; + ++Handle->FileCount; } } -void -PushHMMLWatchHandle(void) +bool +FileExists(string Path) { - FitWatchHandle(); - CopyString(WatchHandles.Handle[WatchHandles.Count].Path, sizeof(WatchHandles.Handle[0].Path), Config.ProjectDir); - WatchHandles.Handle[WatchHandles.Count].Descriptor = inotify_add_watch(inotifyInstance, Config.ProjectDir, IN_MOVED_TO | IN_CLOSE_WRITE | IN_DELETE); - WatchHandles.Handle[WatchHandles.Count].Type = WT_HMML; - ++WatchHandles.Count; -} + // TODO(matt): Whenever we need to produce a 0-terminated string like this, we may as well do: + // WriteString0InBook() [...whatever stuff...] EraseCurrentStringFromBook() + // NOTE(matt): Stack-string + char Path0[Path.Length + 1]; + CopyStringNoFormat(Path0, sizeof(Path0), Path); -void -PushAssetWatchHandle(file_buffer *AssetFile, uint32_t AssetIndex) -{ - if(IsSymlink(AssetFile->Path)) + bool Result = TRUE; + int File = open(Path0, O_RDONLY); + if(File == -1) { - char ResolvedSymlinkPath[4096] = {}; - readlink(AssetFile->Path, ResolvedSymlinkPath, 4096); - Clear(AssetFile->Path, sizeof(AssetFile->Path)); - CopyString(AssetFile->Path, sizeof(AssetFile->Path), ResolvedSymlinkPath); + Result = FALSE; + } + close(File); + return Result; +} + +typedef struct +{ + string A; + string B; +} string_2x; + +string_2x +WriteFilenameThenTargetPathIntoBook(memory_book *M, string Path, watch_type Type) // NOTE(matt): Follows symlinks +{ + // NOTE(matt): The order in which we write the strings into the memory_book is deliberate and permits duplicate TargetPath + // or Filename to be erased by PushWatchHandle() and PushWatchFileUniquely() + + string_2x Result = {}; + // NOTE(matt): Stack-string + char OriginalPath0[Path.Length + 1]; + CopyStringNoFormat(OriginalPath0, sizeof(OriginalPath0), Path); + // NOTE(matt): Stack-string + char ResolvedSymlinkPath[4096] = {}; + string FullPath = {}; + + if(IsSymlink(OriginalPath0)) + { + fprintf(stderr, "%s\n", OriginalPath0); + int PathLength = readlink(OriginalPath0, ResolvedSymlinkPath, 4096); + FullPath = Wrap0i(ResolvedSymlinkPath, PathLength); + PrintString(FullPath); + fprintf(stderr, "\n"); + } + else + { + FullPath = Path; } - ResolvePath(AssetFile->Path); - StripComponentFromPath(AssetFile->Path); + if(Type == WT_HMML) + { + Result.B = WriteStringInBook(M, FullPath); + } + else + { + Result.A = WriteStringInBook(M, GetFinalComponent(FullPath)); + Result.B = WriteStringInBook(M, StripComponentFromPath(FullPath)); + } + return Result; +} + +string +GetNearestExistingPath(string Path) +{ + string Result = Path; + while(!FileExists(Result)) + { + Result = StripComponentFromPath(Result); + } + return Result; +} + +void +PushWatchHandle(string Path, extension_id Extension, watch_type Type, project *Project, asset *Asset) +{ + // NOTE(matt): This function influences RemoveAndFreeAllButFirstWatchFile(). If we change to write different strings into + // the W->Paths memory_book, we must reflect that change over to RemoveAndFreeAllButFirstWatchFile() + + // NOTE(matt): Path types: + // WT_ASSET: File, but we seem to strip the file component from the path, leaving the parent directory + // WT_CONFIG: Directory of -c config, then File of existing config files + // WT_HMML: Directory + // + // So do we need to watch Files and Directories differently? + // + // We could surely detect the type of Path we've been passed, whether DIR or FILE + // + // Rationale: + // WT_HMML: We must watch the directory, so that we can actually pick up new .hmml files + // HMML filenames are supposed to end .hmml, so we may use that to make sure we pick out the + // watch handle + // And we point to the Project so that we may switch to the correct CurrentProject + // + // For HMMLDir that don't exist, they may come into existence after the initial Sync. + // How can we handle this? Must we watch the nearest existing ancestor directory, and keep + // progressively swapping that out until we're watching the HMMLDir itself? + // + // Do we augment watch_handle with "NearestAncestorPath", in addition to TargetPath? + // So if the TargetPath doesn't exist, we instead watch the NearestAncestorPath. The all watch + // handles have a valid WatchDescriptor (i.e. not -1). + // + // WT_CONFIG: We must watch the -c directory, to enable us to handle the absence of a config + // But thereafter, we may (and probably should) watch the files directly, as we are doing + // The reason to watch at all is to enable live-reloading + // WT_ASSET: We have a finite set of assets, right? So shouldn't that mean that we could just watch the + // files directly? + // + // We have BuiltinAssets, which we push onto the Assets whether or not the file exists, but we + // only push their watch handle if the file does exist. Maybe any time we create an asset file, + // e.g. cinera_topics.css, or we see an asset file referred to in a template, we try pushing on + // its watch handle. + // + // Q: Why watch at all? + // A: So that we can rehash the file and snipe its new checksum into the html files + // So wouldn't it be worth pointing to the Asset directly, so that we have immediate access to + // this data? + // + // Permitting multiple "types" at the same path? + // + // This could only possibly happen for directories, which excludes WT_ASSET. + // WT_HMML + // We determine it to be a WT_HMML if the filepath ends .hmml + // WT_CONFIG + // The only WT_CONFIG directory is the parent of the -c, whose path we always know + // + // + // Side issue: Why bother storing the whole path, when the Event->name only contains the BaseFilename? + // Is it so we can distinguish between paths when pushing / getting watch handles? + // + // I reckon we store the Target and the Watched paths, both of which must surely be the full thing + // + // Symlinks: What do we watch? The symlink, or the path to which the symlink points? + // + // Okay, I reckon what we do is the following: + // + // watch_handle + // { + // char *TargetDirectory + // char *WatchedDirectory + // int FileCount + // char **Files + // } + // + // The Target and Files remain the same always + // The WatchedDirectory may change according to which directories exist + // + // Watch the target directory + // For each watch_handle, list out the specific files that we're watching + // + // We only need to write anything into the book once + // + // WrittenPath: + // /home/matt/tmp/file + // Filename: file + // TargetPath: /home/matt/tmp + // + // If we write the WrittenPath, then realise that the TargetPath is already in there, we'd inadvertently + // erase the whole WrittenPath + // + // If we write the Filename then the TargetPath, then realise the TargetPath is already there, we'd erase + // the TargetPath just fine. But then if we later realise we have the Filename too, we have no way to erase + // that. While suggests that we should straight up make the memory_book Current be part of a linked list, + // so we could erase multiple things if needed. + // + // But, write the Filename, then the TargetPath: + // TargetPath doesn't already exist + // Filename, couldn't already exist + // + // TargetPath does exist, so may be erased + // Filename + // does exist, so may be erase + // doesn't exist, so must remain + // + // Seems like we really want to "resolve" the whole path somewhere temporary (on the stack), then: + // string Filename = GetFinalComponent(Wrap0(Temp)); + // string TargetPath = StripComponentFromPath(Wrap0(Temp)); + // WriteStringInBook(Filename); + // WriteStringInBook(TargetPath); + + string_2x FilenameAndDir = WriteFilenameThenTargetPathIntoBook(&WatchHandles.Paths, Path, Type); + + string Filename = FilenameAndDir.A; + string TargetPath = FilenameAndDir.B; + + watch_handle *Watch = 0; for(int i = 0; i < WatchHandles.Count; ++i) { - if(!StringsDiffer(WatchHandles.Handle[i].Path, AssetFile->Path)) + if(StringsMatch(TargetPath, WatchHandles.Handles[i].TargetPath)) { - return; - } - } - - FitWatchHandle(); - WatchHandles.Handle[WatchHandles.Count].Type = WT_ASSET; - CopyString(WatchHandles.Handle[WatchHandles.Count].Path, sizeof(WatchHandles.Handle[0].Path), AssetFile->Path); - WatchHandles.Handle[WatchHandles.Count].Descriptor = inotify_add_watch(inotifyInstance, AssetFile->Path, IN_CLOSE_WRITE); - ++WatchHandles.Count; -} - -void -UpdateAssetInDB(int AssetIndex) -{ - DB.Header = *(db_header *)DB.Metadata.Buffer.Location; - if(DB.Header.HexSignature != FOURCC("CNRA")) - { - printf("line %d: Malformed .metadata file. HexSignature not in expected location\n", __LINE__); - exit(1); - } - DB.Metadata.Buffer.Ptr = DB.Metadata.Buffer.Location + sizeof(DB.Header); - - DB.EntriesHeader = *(db_header_entries *)DB.Metadata.Buffer.Ptr; - if(DB.EntriesHeader.BlockID != FOURCC("NTRY")) - { - printf("line %d: Malformed .metadata file. Entries BlockID not in expected location\n", __LINE__); - exit(1); - } - - DB.Metadata.Buffer.Ptr += sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * DB.EntriesHeader.Count; - - DB.AssetsHeader = *(db_header_assets *)DB.Metadata.Buffer.Ptr; - if(DB.AssetsHeader.BlockID != FOURCC("ASET")) - { - printf("line %d: Malformed .metadata file. Assets BlockID not in expected location\n", __LINE__); - exit(1); - } - int AssetsHeaderLocation = DB.Metadata.Buffer.Ptr - DB.Metadata.Buffer.Location; - DB.Metadata.Buffer.Ptr += sizeof(DB.AssetsHeader); - bool Found = FALSE; - for(int i = 0; i < DB.AssetsHeader.Count; ++i) - { - DB.Asset = *(db_asset *)DB.Metadata.Buffer.Ptr; - if(!StringsDiffer(DB.Asset.Filename, Assets.Asset[AssetIndex].Filename) && DB.Asset.Type == Assets.Asset[AssetIndex].Type) - { - Found = TRUE; + Watch = WatchHandles.Handles + i; + EraseCurrentStringFromBook(&WatchHandles.Paths); break; } - DB.Metadata.Buffer.Ptr += sizeof(DB.Asset) + sizeof(DB.Landmark) * DB.Asset.LandmarkCount; } - if(Found) + if(!Watch) { - if(DB.Asset.Hash != Assets.Asset[AssetIndex].Hash) + WatchHandles.Handles = FitShrinkable(WatchHandles.Handles, sizeof(*WatchHandles.Handles), WatchHandles.Count, &WatchHandles.Capacity, 8, TRUE); + Watch = WatchHandles.Handles + WatchHandles.Count; + + Watch->TargetPath = TargetPath; + Watch->WatchedPath = GetNearestExistingPath(TargetPath); + + // NOTE(matt): Stack-string + char WatchablePath0[Watch->WatchedPath.Length + 1]; + CopyStringNoFormat(WatchablePath0, sizeof(WatchablePath0), Watch->WatchedPath); + + Watch->Descriptor = inotify_add_watch(inotifyInstance, WatchablePath0, WatchHandles.DefaultEventsMask); + ++WatchHandles.Count; + //PrintWatchHandles(); + } + + PushWatchFileUniquely(Watch, Filename, Extension, Type, Project, Asset); +} + +bool +DescriptorIsRedundant(int Descriptor) +{ + bool Result = TRUE; + for(int i = 0; i < WatchHandles.Count; ++i) + { + watch_handle *This = WatchHandles.Handles + i; + if(Descriptor == This->Descriptor) { - DB.Asset.Hash = Assets.Asset[AssetIndex].Hash; - *(db_asset *)DB.Metadata.Buffer.Ptr = DB.Asset; - DB.Metadata.Buffer.Ptr += sizeof(DB.Asset); - - buffer Checksum; - ClaimBuffer(&Checksum, "Checksum", 16); - CopyStringToBuffer(&Checksum, "%08x", DB.Asset.Hash); - - file_buffer AssetFile; - ConstructAssetPath(&AssetFile, DB.Asset.Filename, DB.Asset.Type); - ResolvePath(AssetFile.Path); - - char Message[256] = { }; - CopyString(Message, sizeof(Message), "%sUpdating%s checksum %s of %s in HTML files", ColourStrings[CS_ONGOING], ColourStrings[CS_END], Checksum.Location, AssetFile.Path); - fprintf(stderr, Message); - - SnipeChecksumIntoHTML(DB.Metadata.Buffer.Ptr, &Checksum); - - ClearTerminalRow(StringLength(Message)); - fprintf(stderr, "%sUpdated%s checksum %s of %s\n", ColourStrings[CS_REINSERTION], ColourStrings[CS_END], Checksum.Location, AssetFile.Path); - - DeclaimBuffer(&Checksum); - - DB.Metadata.Handle = fopen(DB.Metadata.Path, "w"); - fwrite(DB.Metadata.Buffer.Location, DB.Metadata.FileSize, 1, DB.Metadata.Handle); - CycleFile(&DB.Metadata); - Assets.Asset[AssetIndex].DeferredUpdate = FALSE; + string WatchablePath = GetNearestExistingPath(This->TargetPath); + if(StringsMatch(This->WatchedPath, WatchablePath)) + { + Result = FALSE; + break; + } } } - else + return Result; +} + +int +GetExistingWatchDescriptor(string Path) +{ + int Result = -1; + for(int i = 0; i < WatchHandles.Count; ++i) { - // Append new asset, not bothering to insertion sort because there likely won't be many - ++DB.AssetsHeader.Count; - - DB.Metadata.Handle = fopen(DB.Metadata.Path, "w"); - - fwrite(DB.Metadata.Buffer.Location, AssetsHeaderLocation, 1, DB.Metadata.Handle); - fwrite(&DB.AssetsHeader, sizeof(DB.AssetsHeader), 1, DB.Metadata.Handle); - fwrite(DB.Metadata.Buffer.Location + AssetsHeaderLocation + sizeof(DB.AssetsHeader), - DB.Metadata.FileSize - AssetsHeaderLocation - sizeof(DB.AssetsHeader), - 1, - DB.Metadata.Handle); - - db_asset Asset = {}; - Asset.Hash = Assets.Asset[AssetIndex].Hash; - Asset.Type = Assets.Asset[AssetIndex].Type; - ClearCopyStringNoFormat(Asset.Filename, sizeof(Asset.Filename), Assets.Asset[AssetIndex].Filename); - fwrite(&Asset, sizeof(Asset), 1, DB.Metadata.Handle); - printf("%sAppended%s %s asset: %s [%08x]\n", ColourStrings[CS_ADDITION], ColourStrings[CS_END], AssetTypeNames[Asset.Type], Asset.Filename, Asset.Hash); - - CycleFile(&DB.Metadata); - } - if(!Assets.Asset[AssetIndex].Known) - { - Assets.Asset[AssetIndex].Known = TRUE; + watch_handle *This = WatchHandles.Handles + i; + if(StringsMatch(Path, This->WatchedPath)) + { + Result = This->Descriptor; + break; + } } + return Result; } void -FitAssetLandmark(enum8(builtin_assets + support_icons) AssetIndex, int PageType) +UpdateWatchHandles(int Descriptor) { - int BlockSize = 2; - if(PageType == PAGE_PLAYER) + for(int i = 0; i < WatchHandles.Count; ++i) { - if(Assets.Asset[AssetIndex].PlayerLandmarkCount == Assets.Asset[AssetIndex].PlayerLandmarkCapacity) + watch_handle *This = WatchHandles.Handles + i; + if(Descriptor == This->Descriptor) { - Assets.Asset[AssetIndex].PlayerLandmarkCapacity += BlockSize; - if(Assets.Asset[AssetIndex].PlayerLandmark) + string WatchablePath = GetNearestExistingPath(This->TargetPath); + if(StringsDiffer(This->WatchedPath, WatchablePath)) { - Assets.Asset[AssetIndex].PlayerLandmark = - realloc(Assets.Asset[AssetIndex].PlayerLandmark, - Assets.Asset[AssetIndex].PlayerLandmarkCapacity * sizeof(*Assets.Asset[AssetIndex].PlayerLandmark)); - } - else - { - Assets.Asset[AssetIndex].PlayerLandmark = - calloc(Assets.Asset[AssetIndex].PlayerLandmarkCapacity, sizeof(*Assets.Asset[AssetIndex].PlayerLandmark)); - } - } - } - else - { - if(Assets.Asset[AssetIndex].SearchLandmarkCount == Assets.Asset[AssetIndex].SearchLandmarkCapacity) - { - Assets.Asset[AssetIndex].SearchLandmarkCapacity += BlockSize; - if(Assets.Asset[AssetIndex].SearchLandmark) - { - Assets.Asset[AssetIndex].SearchLandmark = - realloc(Assets.Asset[AssetIndex].SearchLandmark, - Assets.Asset[AssetIndex].SearchLandmarkCapacity * sizeof(*Assets.Asset[AssetIndex].SearchLandmark)); - } - else - { - Assets.Asset[AssetIndex].SearchLandmark = - calloc(Assets.Asset[AssetIndex].SearchLandmarkCapacity, sizeof(*Assets.Asset[AssetIndex].SearchLandmark)); + if(DescriptorIsRedundant(Descriptor)) + { + inotify_rm_watch(inotifyInstance, This->Descriptor); + } + int NewDescriptor = GetExistingWatchDescriptor(WatchablePath); + if(NewDescriptor == -1) + { + int NullTerminationBytes = 1; + char WatchablePath0[WatchablePath.Length + NullTerminationBytes]; + CopyStringNoFormat(WatchablePath0, sizeof(WatchablePath0), WatchablePath); + + NewDescriptor = inotify_add_watch(inotifyInstance, WatchablePath0, WatchHandles.DefaultEventsMask); + } + This->Descriptor = NewDescriptor; + This->WatchedPath = WatchablePath; } } } } -void -PushAssetLandmark(buffer *Dest, int AssetIndex, int PageType) +db_asset * +LocateAsset(db_block_assets *Block, asset *Asset, int *Index) { - if(!(Config.Mode & MODE_NOREVVEDRESOURCE)) + db_asset *Result = 0; + *Index = SAI_UNSET; + if(!Block) { - FitAssetLandmark(AssetIndex, PageType); - CopyStringToBuffer(Dest, "?%s=", Config.QueryString); - if(PageType == PAGE_PLAYER) + Block = InitBlock(B_ASET); + } + + char *Ptr = (char *)Block; + Ptr += sizeof(*Block); + for(int i = 0; i < Block->Count; ++i) + { + db_asset *StoredAsset = (db_asset *)Ptr; + string FilenameInDB = Wrap0i(StoredAsset->Filename, sizeof(StoredAsset->Filename)); + string FilenameInMemory = Wrap0i(Asset->Filename, sizeof(Asset->Filename)); + if(StringsMatch(FilenameInDB, FilenameInMemory) && StoredAsset->Type == Asset->Type) { - Assets.Asset[AssetIndex].PlayerLandmark[Assets.Asset[AssetIndex].PlayerLandmarkCount] = Dest->Ptr - Dest->Location; - ++Assets.Asset[AssetIndex].PlayerLandmarkCount; + *Index = i; + Result = StoredAsset; + break; + } + Ptr = SkipAsset(StoredAsset); + } + return Result; +} + +void * +LocateEndOfAssetsBlock(db_block_assets *Block) +{ + char *Ptr = (char *)Block; + Ptr += sizeof(*Block); + for(int i = 0; i < Block->Count; ++i) + { + db_asset *Asset = (db_asset *)Ptr; + Ptr += sizeof(*Asset) + sizeof(db_landmark) * Asset->LandmarkCount; + } + return Ptr; +} + +void +UpdateNeighbourhoodPointers(neighbourhood *N, file_signposts *S) +{ + N->Project = S->ProjectHeader.Ptr; + N->Prev = S->Prev.Ptr; + N->This = S->This.Ptr; + N->Next = S->Next.Ptr; +} + +int +UpdateAssetInDB(asset *Asset) +{ + int AssetIndexInDB = SAI_UNSET; + if(Config->QueryString.Length > 0) + { + db_block_assets *AssetsBlock = LocateBlock(B_ASET); + if(!AssetsBlock) + { + AssetsBlock = InitBlock(B_ASET); + } + + DB.Metadata.Signposts.AssetsBlock.Ptr = AssetsBlock; + + db_asset *StoredAsset = LocateAsset(AssetsBlock, Asset, &AssetIndexInDB); + + if(StoredAsset) + { + StoredAsset->Associated = Asset->Associated; + StoredAsset->Variants = Asset->Variants; + StoredAsset->Width = Asset->Dimensions.Width; + StoredAsset->Height = Asset->Dimensions.Height; + + if(StoredAsset->Hash != Asset->Hash) + { + StoredAsset->Hash = Asset->Hash; + char *Ptr = (char *)Asset; + Ptr += sizeof(*Asset); + + buffer Checksum = {}; + ClaimBuffer(&Checksum, BID_CHECKSUM, 16); + CopyStringToBuffer(&Checksum, "%08x", StoredAsset->Hash); + + file AssetFile = {}; + AssetFile.Path = ConstructAssetPath(&AssetFile, Wrap0i(StoredAsset->Filename, sizeof(StoredAsset->Filename)), StoredAsset->Type); + ResolvePath(&AssetFile.Path); + + string ChecksumL = Wrap0i(Checksum.Location, Checksum.Ptr - Checksum.Location); + string Message = MakeString("sssslsss", + ColourStrings[CS_ONGOING], "Updating", ColourStrings[CS_END], " checksum ", &ChecksumL, " of ", AssetFile.Path, " in HTML files"); + fprintf(stderr, "%.*s", (int)Message.Length, Message.Base); + uint64_t MessageLength = Message.Length; + FreeString(&Message); + + if(SnipeChecksumIntoHTML(StoredAsset, &Checksum) == RC_SUCCESS) + { + ClearTerminalRow(MessageLength); + fprintf(stderr, "%sUpdated%s checksum %.*s of %s\n", ColourStrings[CS_REINSERTION], ColourStrings[CS_END], (int)ChecksumL.Length, ChecksumL.Base, AssetFile.Path); + } + + DeclaimBuffer(&Checksum); + + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + fwrite(DB.Metadata.File.Buffer.Location, DB.Metadata.File.Buffer.Size, 1, DB.Metadata.File.Handle); + SetFileEditPosition(&DB.Metadata); + + CycleSignpostedFile(&DB.Metadata); + Asset->DeferredUpdate = FALSE; + } } else { - Assets.Asset[AssetIndex].SearchLandmark[Assets.Asset[AssetIndex].SearchLandmarkCount] = Dest->Ptr - Dest->Location; - ++Assets.Asset[AssetIndex].SearchLandmarkCount; + // Append new asset, not bothering to insertion sort because there likely won't be many + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + char *InsertionPoint = LocateEndOfAssetsBlock(AssetsBlock); + + AssetIndexInDB = AssetsBlock->Count; + ++AssetsBlock->Count; + + uint64_t BytesIntoFile = InsertionPoint - DB.Metadata.File.Buffer.Location; + fwrite(DB.Metadata.File.Buffer.Location, BytesIntoFile, 1, DB.Metadata.File.Handle); + SetFileEditPosition(&DB.Metadata); + + db_asset StoredAsset = {}; + StoredAsset.Hash = Asset->Hash; + StoredAsset.Type = Asset->Type; + StoredAsset.Variants = Asset->Variants; + StoredAsset.Width = Asset->Dimensions.Width; + StoredAsset.Height = Asset->Dimensions.Height; + + StoredAsset.Associated = Asset->Associated; + ClearCopyStringNoFormat(StoredAsset.Filename, sizeof(StoredAsset.Filename), Wrap0i(Asset->Filename, sizeof(Asset->Filename))); + + fwrite(&StoredAsset, sizeof(StoredAsset), 1, DB.Metadata.File.Handle); + AccumulateFileEditSize(&DB.Metadata, sizeof(StoredAsset)); + + printf("%sAppended%s %s asset: %s [%08x]\n", ColourStrings[CS_ADDITION], ColourStrings[CS_END], AssetTypeNames[StoredAsset.Type], StoredAsset.Filename, StoredAsset.Hash); + + fwrite(DB.Metadata.File.Buffer.Location + BytesIntoFile, DB.Metadata.File.Buffer.Size - BytesIntoFile, 1, DB.Metadata.File.Handle); + + CycleSignpostedFile(&DB.Metadata); + } + if(!Asset->Known) + { + Asset->Known = TRUE; + } + } + return AssetIndexInDB; +} + +void +PushAssetLandmark(buffer *Dest, asset *Asset, int PageType, bool GrowableBuffer) +{ + if(Config->QueryString.Length > 0) + { + if(GrowableBuffer) + { + AppendStringToBuffer(Dest, Wrap0("?")); + AppendStringToBuffer(Dest, Config->QueryString); + AppendStringToBuffer(Dest, Wrap0("=")); + } + else + { + CopyStringToBuffer(Dest, "?%.*s=", (int)Config->QueryString.Length, Config->QueryString.Base); + } + + if(PageType == PAGE_PLAYER) + { + Asset->Player = Fit(Asset->Player, sizeof(*Asset->Player), Asset->PlayerLandmarkCount, 8, TRUE); + Asset->Player[Asset->PlayerLandmarkCount].Offset = Dest->Ptr - Dest->Location; + Asset->Player[Asset->PlayerLandmarkCount].BufferID = Dest->ID; + ++Asset->PlayerLandmarkCount; + } + else + { + Asset->Search = Fit(Asset->Search, sizeof(*Asset->Search), Asset->SearchLandmarkCount, 8, TRUE); + Asset->Search[Asset->SearchLandmarkCount].Offset = Dest->Ptr - Dest->Location; + Asset->Search[Asset->SearchLandmarkCount].BufferID = Dest->ID; + ++Asset->SearchLandmarkCount; + } + + if(GrowableBuffer) + { + // NOTE(matt): Stack-string + char Hash[16] = {}; + CopyString(Hash, sizeof(Hash), "%08x", Asset->Hash); + AppendStringToBuffer(Dest, Wrap0(Hash)); + } + else + { + CopyStringToBuffer(Dest, "%08x", Asset->Hash); } - CopyStringToBuffer(Dest, "%08x", Assets.Asset[AssetIndex].Hash); } } @@ -2312,254 +5301,217 @@ ResetAssetLandmarks(void) { for(int AssetIndex = 0; AssetIndex < Assets.Count; ++AssetIndex) { - for(int LandmarkIndex = 0; LandmarkIndex < Assets.Asset[AssetIndex].PlayerLandmarkCount; ++LandmarkIndex) - { - Assets.Asset[AssetIndex].PlayerLandmark[LandmarkIndex] = 0; - } - Assets.Asset[AssetIndex].PlayerLandmarkCount = 0; - - for(int LandmarkIndex = 0; LandmarkIndex < Assets.Asset[AssetIndex].SearchLandmarkCount; ++LandmarkIndex) - { - Assets.Asset[AssetIndex].SearchLandmark[LandmarkIndex] = 0; - } - Assets.Asset[AssetIndex].SearchLandmarkCount = 0; - Assets.Asset[AssetIndex].OffsetLandmarks = FALSE; + asset *A = GetPlaceInBook(&Assets.Asset, AssetIndex); + FreeAndResetCount(A->Player, A->PlayerLandmarkCount); + FreeAndResetCount(A->Search, A->SearchLandmarkCount); + A->OffsetLandmarks = FALSE; } } -void -FitAsset(void) +vec2 +GetImageDimensions(buffer *B) { - int BlockSize = 16; - if(Assets.Count == Assets.Capacity) - { - Assets.Capacity += BlockSize; - if(Assets.Asset) - { - Assets.Asset = realloc(Assets.Asset, Assets.Capacity * sizeof(*Assets.Asset)); - } - else - { - Assets.Asset = calloc(Assets.Capacity, sizeof(*Assets.Asset)); - } - } + vec2 Result = {}; + stbi_info_from_memory(&*(stbi_uc *)B->Location, B->Size, (int *)&Result.Width, (int *)&Result.Height, 0); + return Result; } -void -UpdateAsset(uint32_t AssetIndex, bool Defer) +int32_t +UpdateAsset(asset *Asset, bool Defer) { - file_buffer File; - ConstructAssetPath(&File, Assets.Asset[AssetIndex].Filename, Assets.Asset[AssetIndex].Type); - if(ReadFileIntoBuffer(&File, 0) == RC_SUCCESS) + int32_t AssetIndexInDB = SAI_UNSET; + file File = {}; + File.Path = ConstructAssetPath(&File, Wrap0i(Asset->Filename, sizeof(Asset->Filename)), Asset->Type); + ReadFileIntoBuffer(&File); + + if(File.Buffer.Location) { - Assets.Asset[AssetIndex].Hash = StringToFletcher32(File.Buffer.Location, File.FileSize); + Asset->Hash = StringToFletcher32(File.Buffer.Location, File.Buffer.Size); + if(Asset->Type == ASSET_IMG) + { + Asset->Dimensions = GetImageDimensions(&File.Buffer); + } if(!Defer) { - UpdateAssetInDB(AssetIndex); + AssetIndexInDB = UpdateAssetInDB(Asset); } else { - Assets.Asset[AssetIndex].DeferredUpdate = TRUE; + Asset->DeferredUpdate = TRUE; } - FreeBuffer(&File.Buffer); + } + else if(Asset->Associated) + { + AssetIndexInDB = UpdateAssetInDB(Asset); + } + FreeFile(&File); + return AssetIndexInDB; +} + +//typedef struct +//{ +// vec2 TileDim; +// int32_t XLight; +// int32_t XDark; +// int32_t YNormal; +// int32_t YFocused; +// int32_t YDisabled; +//} sprite; + +void +ComputeSpriteData(asset *A) +{ + if(A->Variants) + { + sprite *S = &A->Sprite; + + bool HasLight = (A->Variants & (1 << AVS_LIGHT_NORMAL) || A->Variants & (1 << AVS_LIGHT_FOCUSED) || A->Variants & (1 << AVS_LIGHT_DISABLED)); + bool HasDark = (A->Variants & (1 << AVS_DARK_NORMAL) || A->Variants & (1 << AVS_DARK_FOCUSED) || A->Variants & (1 << AVS_DARK_DISABLED)); + int TileCountX = HasLight + HasDark; + + + bool HasNormal = (A->Variants & (1 << AVS_LIGHT_NORMAL) || A->Variants & (1 << AVS_DARK_NORMAL)); + bool HasFocused = (A->Variants & (1 << AVS_LIGHT_FOCUSED) || A->Variants & (1 << AVS_DARK_FOCUSED)); + bool HasDisabled = (A->Variants & (1 << AVS_LIGHT_DISABLED) || A->Variants & (1 << AVS_DARK_DISABLED)); + int TileCountY = HasNormal + HasFocused + HasDisabled; + + S->TileDim.Width = A->Dimensions.Width / TileCountX; + S->TileDim.Height = A->Dimensions.Height / TileCountY; + + S->XLight = 0; + S->XDark = 0; + if(HasDark) { S->XDark += S->TileDim.Width * HasLight; } + + A->Sprite.YNormal = 0; + A->Sprite.YFocused = 0; + A->Sprite.YDisabled = 0; + if(HasFocused) { S->YFocused -= S->TileDim.Height * HasNormal; } + if(HasDisabled) { S->YDisabled -= S->TileDim.Height * (HasNormal + HasFocused); } + if(!HasNormal && HasDisabled) { S->YNormal = S->YDisabled; } } } -int -FinalPathComponentPosition(char *Path) +asset * +PlaceAsset(string Filename, asset_type Type, uint64_t Variants, bool Associated, int Position) { - char *Ptr = Path + StringLength(Path) - 1; - while(Ptr > Path && *Ptr != '/') - { - --Ptr; - } - if(*Ptr == '/') - { - ++Ptr; - } - return Ptr - Path; -} + asset *This = GetPlaceInBook(&Assets.Asset, Position); -int -PlaceAsset(char *Filename, int Type, int Position) -{ - FitAsset(); - Assets.Asset[Position].Type = Type; - CopyString(Assets.Asset[Position].Filename, sizeof(Assets.Asset[0].Filename), Filename); - Assets.Asset[Position].FilenameAt = FinalPathComponentPosition(Filename); - if(Position == Assets.Count && !Assets.Asset[Position].Known) { ++Assets.Count; } + This->Type = Type; + This->Variants = Variants; + This->Associated = Associated; + ClearCopyString(This->Filename, sizeof(This->Filename), "%.*s", (int)Filename.Length, Filename.Base); + This->FilenameAt = FinalPathComponentPosition(Filename); + if(Position == Assets.Count && !This->Known) { ++Assets.Count; } - file_buffer File; - ConstructAssetPath(&File, Filename, Type); - if(ReadFileIntoBuffer(&File, 0) == RC_SUCCESS) + file File = {}; + File.Path = ConstructAssetPath(&File, Filename, Type); + ReadFileIntoBuffer(&File); + if(File.Buffer.Location) { - Assets.Asset[Position].Hash = StringToFletcher32(File.Buffer.Location, File.FileSize); - FreeBuffer(&File.Buffer); - PushAssetWatchHandle(&File, Position); - return RC_SUCCESS; + This->Hash = StringToFletcher32(File.Buffer.Location, File.Buffer.Size); + if(This->Type == ASSET_IMG) + { + This->Dimensions = GetImageDimensions(&File.Buffer); + ComputeSpriteData(This); + } + PushWatchHandle(Wrap0(File.Path), EXT_NULL, WT_ASSET, 0, This); } else { - ResolvePath(File.Path); + ResolvePath(&File.Path); printf("%sNonexistent%s %s asset: %s\n", ColourStrings[CS_WARNING], ColourStrings[CS_END], AssetTypeNames[Type], File.Path); - return RC_ERROR_FILE; } + + FreeFile(&File); + return This; } -int -PushAsset(char *Filename, int Type, uint32_t *AssetIndexPtr) +asset * +PushAsset(string Filename, asset_type Type, uint64_t Variants, bool Associated) { - for(*AssetIndexPtr = 0; *AssetIndexPtr < Assets.Count; ++*AssetIndexPtr) + int i; + for(i = 0; i < Assets.Count; ++i) { - if(!StringsDiffer(Filename, Assets.Asset[*AssetIndexPtr].Filename) && Type == Assets.Asset[*AssetIndexPtr].Type) + asset *Asset = GetPlaceInBook(&Assets.Asset, i); + if(!StringsDifferLv0(Filename, Asset->Filename) && Type == Asset->Type) { break; } } - return PlaceAsset(Filename, Type, *AssetIndexPtr); + return PlaceAsset(Filename, Type, Variants, Associated, i); +} + +void +FreeAssets(assets *A) +{ + for(int i = 0; i < A->Count; ++i) + { + asset *Asset = GetPlaceInBook(&Assets.Asset, i); + FreeAndResetCount(Asset->Search, Asset->SearchLandmarkCount); + FreeAndResetCount(Asset->Player, Asset->PlayerLandmarkCount); + } + FreeBook(&A->Asset); + A->Count = 0; } void InitBuiltinAssets(void) { + // NOTE(matt): This places assets in their own known slots, letting people like GenerateTopicColours() index them directly Assert(BUILTIN_ASSETS_COUNT == ArrayCount(BuiltinAssets)); - CopyString(BuiltinAssets[ASSET_CSS_THEME].Filename, sizeof(BuiltinAssets[0].Filename), "cinera__%s.css", Config.Theme); for(int AssetIndex = 0; AssetIndex < BUILTIN_ASSETS_COUNT; ++AssetIndex) { - if(PlaceAsset(BuiltinAssets[AssetIndex].Filename, BuiltinAssets[AssetIndex].Type, AssetIndex) == RC_ERROR_FILE && AssetIndex == ASSET_CSS_TOPICS) + asset *This = BuiltinAssets + AssetIndex; + if(!(PlaceAsset(Wrap0(This->Filename), This->Type, This->Variants, This->Associated, AssetIndex)) && AssetIndex == ASSET_CSS_TOPICS) { printf( " %s└───────┴────┴─── Don't worry about this one. We'll generate it if needed%s\n", ColourStrings[CS_COMMENT], ColourStrings[CS_END]); } } Assets.Count = BUILTIN_ASSETS_COUNT; + // TODO(matt): Think deeply about how and when we push these support icon assets on +#if 0 Assert(SUPPORT_ICON_COUNT - BUILTIN_ASSETS_COUNT == ArrayCount(SupportIcons)); for(int SupportIconIndex = BUILTIN_ASSETS_COUNT; SupportIconIndex < SUPPORT_ICON_COUNT; ++SupportIconIndex) { - PlaceAsset(SupportIcons[SupportIconIndex - BUILTIN_ASSETS_COUNT], ASSET_IMG, SupportIconIndex); + PlaceAsset(Wrap0(SupportIcons[SupportIconIndex - BUILTIN_ASSETS_COUNT]), ASSET_IMG, SupportIconIndex); } -} - -void -SkipEntriesBlock(void *EntriesHeaderLocation) -{ - DB.EntriesHeader = *(db_header_entries *)EntriesHeaderLocation; - DB.Metadata.Buffer.Ptr = EntriesHeaderLocation + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * DB.EntriesHeader.Count; -} - -void -LocateAssetsBlock(void) -{ - DB.Header = *(db_header *)DB.Metadata.Buffer.Location; - DB.Metadata.Buffer.Ptr = DB.Metadata.Buffer.Location + sizeof(DB.Header); - for(int BlockIndex = 0; BlockIndex < DB.Header.BlockCount; ++BlockIndex) - { - uint32_t FirstInt = *(uint32_t *)DB.Metadata.Buffer.Ptr; - if(FirstInt == FOURCC("NTRY")) - { - SkipEntriesBlock(DB.Metadata.Buffer.Ptr); - } - else if(FirstInt == FOURCC("ASET")) - { - return; - } - } - +#endif } void InitAssets(void) { + InitBook(&Assets.Asset, sizeof(asset), 16, MBT_ASSET); InitBuiltinAssets(); - LocateAssetsBlock(); - DB.AssetsHeader = *(db_header_assets *)DB.Metadata.Buffer.Ptr; - DB.Metadata.Buffer.Ptr += sizeof(DB.AssetsHeader); - for(int AssetIndex = 0; AssetIndex < DB.AssetsHeader.Count; ++AssetIndex) + db_block_assets *AssetsBlock = LocateBlock(B_ASET); + if(AssetsBlock) { - DB.Asset = *(db_asset *)DB.Metadata.Buffer.Ptr; - uint32_t AI; - PushAsset(DB.Asset.Filename, DB.Asset.Type, &AI); - DB.Metadata.Buffer.Ptr += sizeof(DB.Asset) + sizeof(DB.Landmark) * DB.Asset.LandmarkCount; + DB.Metadata.Signposts.AssetsBlock.Ptr = AssetsBlock; + db_asset *Asset = LocateFirstAsset(AssetsBlock); + + for(int i = 0; i < AssetsBlock->Count; ++i) + { + PushAsset(Wrap0i(Asset->Filename, sizeof(Asset->Filename)), Asset->Type, Asset->Variants, Asset->Associated); + Asset = SkipAsset(Asset); + } } } void -ConstructResolvedAssetURL(buffer *Buffer, uint32_t AssetIndex, enum8(pages) PageType) +ConstructResolvedAssetURL(buffer *Buffer, asset *Asset, enum8(pages) PageType) { - ClaimBuffer(Buffer, "URL", (MAX_ROOT_URL_LENGTH + 1 + MAX_RELATIVE_ASSET_LOCATION_LENGTH + 1) * 2); - ConstructURLPrefix(Buffer, Assets.Asset[AssetIndex].Type, PageType); - CopyStringToBufferHTMLPercentEncoded(Buffer, Assets.Asset[AssetIndex].Filename); - ResolvePath(Buffer->Location); -} - -int -SearchCredentials(buffer *CreditsMenu, bool *HasCreditsMenu, char *Person, char *Role, speakers *Speakers) -{ - bool Found = FALSE; - for(int CredentialIndex = 0; CredentialIndex < ArrayCount(Credentials); ++CredentialIndex) - { - if(!StringsDiffer(Person, Credentials[CredentialIndex].Username)) - { - if(Speakers) - { - Speakers->Speaker[Speakers->Count].Credential = &Credentials[CredentialIndex]; - ++Speakers->Count; - } - Found = TRUE; - if(*HasCreditsMenu == FALSE) - { - CopyStringToBuffer(CreditsMenu, - "
\n" - " Credits\n" - "
\n"); - - *HasCreditsMenu = TRUE; - } - - CopyStringToBuffer(CreditsMenu, - " \n"); - if(Credentials[CredentialIndex].HomepageURL) - { - CopyStringToBuffer(CreditsMenu, - " \n" - "
%s
\n" - "
%s
\n" - "
\n", - Credentials[CredentialIndex].HomepageURL, - Role, - Credentials[CredentialIndex].CreditedName); - } - else - { - CopyStringToBuffer(CreditsMenu, - "
\n" - "
%s
\n" - "
%s
\n" - "
\n", - Role, - Credentials[CredentialIndex].CreditedName); - } - - if(Credentials[CredentialIndex].SupportURL) - { - buffer URL; - ConstructResolvedAssetURL(&URL, Credentials[CredentialIndex].SupportIconIndex, PAGE_PLAYER); - CopyStringToBuffer(CreditsMenu, - "
\n"); - } - - CopyStringToBuffer(CreditsMenu, - "
\n"); - } - } - return Found ? RC_SUCCESS : CreditsError_NoCredentials; + ClaimBuffer(Buffer, BID_URL_ASSET, (MAX_ROOT_URL_LENGTH + 1 + MAX_RELATIVE_ASSET_LOCATION_LENGTH + 1) * 2); + ConstructURLPrefix(Buffer, Asset->Type, PageType); + CopyStringToBufferHTMLPercentEncoded(Buffer, Wrap0i(Asset->Filename, sizeof(Asset->Filename))); + string BufferL = {}; + BufferL.Base = Buffer->Location; + BufferL.Length = Buffer->Ptr - Buffer->Location; + char *ResolvablePath = MakeString0("l", &BufferL); + ResolvePath(&ResolvablePath); + RewindBuffer(Buffer); + CopyStringToBuffer(Buffer, "%s", ResolvablePath); + Free(ResolvablePath); } void @@ -2571,72 +5523,80 @@ ClearNullTerminatedString(char *String) } } -void -InitialString(char *Dest, char *Src) +char * +InitialString(char *Dest, string Src) { - ClearNullTerminatedString(Dest); - *Dest++ = *Src++; - while(*Src++) + Free(Dest); + string Char = Wrap0i(Src.Base, 1); + + ExtendString0(&Dest, Char); + for(int i = 1; i < Src.Length; ++i) { - if(*Src == ' ') + if(Src.Base[i] == ' ' && i < Src.Length) { - ++Src; - if(*Src) + ++i; + Char = Wrap0i(Src.Base + i, 1); + ExtendString0(&Dest, Char); + } + } + return Dest; +} + +char * +GetFirstSubstring(char *Dest, string Src) +{ + Free(Dest); + string Substring = {}; + Substring.Base = Src.Base; + for(int i = 0; i < Src.Length; ++i, ++Substring.Length) + { + if(Src.Base[i] == ' ') + { + ExtendString0(&Dest, Substring); + return Dest; + } + } + ExtendString0(&Dest, Substring); + return Dest; +} + +char * +InitialAndGetFinalString(char *Dest, string Src) +{ + Free(Dest); + int FinalStringBase; + for(FinalStringBase = Src.Length; FinalStringBase > 0; --FinalStringBase) + { + if(Src.Base[FinalStringBase - 1] == ' ') { break; } + } + + if(FinalStringBase > 0 && Src.Base[FinalStringBase] == ' ' && FinalStringBase < Src.Length) + { + ++FinalStringBase; + } + + string FinalString = Wrap0i(Src.Base + FinalStringBase, Src.Length - FinalStringBase); + + if(FinalStringBase > 0) + { + string Initial = Wrap0i(Src.Base, 1); + ExtendString0(&Dest, Initial); + ExtendString0(&Dest, Wrap0(". ")); + + for(int i = 0; i < FinalStringBase; ++i) + { + if(Src.Base[i] == ' ' && i + 1 < FinalStringBase) { - *Dest++ = *Src; + ++i; + string Initial = Wrap0i(Src.Base + i, 1); + ExtendString0(&Dest, Initial); + ExtendString0(&Dest, Wrap0(". ")); } } } -} -void -GetFirstSubstring(char *Dest, char *Src) -{ - ClearNullTerminatedString(Dest); - while(*Src && *Src != ' ') - { - *Dest++ = *Src++; - } -} - -void -InitialAndGetFinalString(char *Dest, int DestSize, char *Src) -{ - ClearNullTerminatedString(Dest); - int SrcLength = StringLength(Src); - char *SrcPtr = Src + SrcLength - 1; - while(SrcPtr > Src && *SrcPtr != ' ') - { - --SrcPtr; - } - if(*SrcPtr == ' ' && SrcPtr - Src < SrcLength - 1) - { - ++SrcPtr; - } - - if(Src < SrcPtr) - { - *Dest++ = *Src++; - *Dest++ = '.'; - *Dest++ = ' '; - - while(Src < SrcPtr - 1) - { - if(*Src == ' ') - { - ++Src; - if(*Src) - { - *Dest++ = *Src; - *Dest++ = '.'; - *Dest++ = ' '; - } - } - ++Src; - } - } - - CopyString(Dest, DestSize, "%s", SrcPtr); + ExtendString0(&Dest, FinalString); + return Dest; } bool @@ -2646,7 +5606,7 @@ AbbreviationsClash(speakers *Speakers) { for(int j = i + 1; j < Speakers->Count; ++j) { - if(!StringsDiffer(Speakers->Speaker[i].Abbreviation, Speakers->Speaker[j].Abbreviation)) + if(!StringsDiffer0(Speakers->Speaker[i].Abbreviation, Speakers->Speaker[j].Abbreviation)) { return TRUE; } @@ -2658,15 +5618,17 @@ AbbreviationsClash(speakers *Speakers) void SortAndAbbreviateSpeakers(speakers *Speakers) { + // TODO(matt): Handle Abbreviation in its new form as a char *, rather than a fixed-sized char[], so probably doing + // MakeString0() or ExpandString0() or something for(int i = 0; i < Speakers->Count; ++i) { for(int j = i + 1; j < Speakers->Count; ++j) { - if(StringsDiffer(Speakers->Speaker[i].Credential->Username, Speakers->Speaker[j].Credential->Username) > 0) + if(StringsDiffer(Speakers->Speaker[i].Person->ID, Speakers->Speaker[j].Person->ID) > 0) { - credential_info *Temp = Speakers->Speaker[j].Credential; - Speakers->Speaker[j].Credential = Speakers->Speaker[i].Credential; - Speakers->Speaker[i].Credential = Temp; + person *Temp = Speakers->Speaker[j].Person; + Speakers->Speaker[j].Person = Speakers->Speaker[i].Person; + Speakers->Speaker[i].Person = Temp; break; } } @@ -2674,8 +5636,8 @@ SortAndAbbreviateSpeakers(speakers *Speakers) for(int i = 0; i < Speakers->Count; ++i) { - StringToColourHash(&Speakers->Speaker[i].Colour, Speakers->Speaker[i].Credential->Username); - InitialString(Speakers->Speaker[i].Abbreviation, Speakers->Speaker[i].Credential->CreditedName); + StringToColourHash(&Speakers->Speaker[i].Colour, Speakers->Speaker[i].Person->ID); + Speakers->Speaker[i].Abbreviation = InitialString(Speakers->Speaker[i].Abbreviation, Speakers->Speaker[i].Person->Name); } int Attempt = 0; @@ -2685,55 +5647,497 @@ SortAndAbbreviateSpeakers(speakers *Speakers) { switch(Attempt) { - case 0: GetFirstSubstring(Speakers->Speaker[i].Abbreviation, Speakers->Speaker[i].Credential->CreditedName); break; - case 1: InitialAndGetFinalString(Speakers->Speaker[i].Abbreviation, sizeof(Speakers->Speaker[i].Abbreviation), Speakers->Speaker[i].Credential->CreditedName); break; - case 2: ClearCopyStringNoFormat(Speakers->Speaker[i].Abbreviation, sizeof(Speakers->Speaker[i].Abbreviation), Speakers->Speaker[i].Credential->Username); break; + case 0: Speakers->Speaker[i].Abbreviation = GetFirstSubstring(Speakers->Speaker[i].Abbreviation, Speakers->Speaker[i].Person->Name); break; + case 1: Speakers->Speaker[i].Abbreviation = InitialAndGetFinalString(Speakers->Speaker[i].Abbreviation, Speakers->Speaker[i].Person->Name); break; + case 2: Free(Speakers->Speaker[i].Abbreviation); ExtendString0(&Speakers->Speaker[i].Abbreviation, Speakers->Speaker[i].Person->Name); break; } } ++Attempt; } } -int -BuildCredits(buffer *CreditsMenu, bool *HasCreditsMenu, HMML_VideoMetaData *Metadata, speakers *Speakers) -// TODO(matt): Make this take the Credentials, once we are parsing them from a config +person * +GetPersonFromConfig(string Person) { - if(SearchCredentials(CreditsMenu, HasCreditsMenu, Metadata->member, "Host", Speakers) == CreditsError_NoCredentials) + for(int i = 0; i < Config->PersonCount; ++i) { - printf("No credentials for member %s. Please contact miblodelcarpio@gmail.com with their:\n" - " Full name\n" - " Homepage URL (optional)\n" - " Financial support info, e.g. Patreon URL (optional)\n", Metadata->member); - return CreditsError_NoCredentials; - } - - if(Metadata->co_host_count > 0) - { - for(int i = 0; i < Metadata->co_host_count; ++i) + if(!StringsDifferCaseInsensitive(Config->Person[i].ID, Person)) { - if(SearchCredentials(CreditsMenu, HasCreditsMenu, Metadata->co_hosts[i], "Co-host", Speakers) == CreditsError_NoCredentials) + return &Config->Person[i]; + } + } + return 0; +} + +asset * +GetAsset(string Filename, asset_type AssetType) +{ + asset *Result = 0; + for(int i = 0; i < Assets.Count; ++i) + { + asset *This = GetPlaceInBook(&Assets.Asset, i); + if(StringsMatch(Filename, Wrap0i(This->Filename, sizeof(This->Filename))) + && AssetType == This->Type) + { + Result = This; + break; + } + } + return Result; +} + +void +PushSupportIconAssets() +{ + for(int i = 0; i < Config->PersonCount; ++i) + { + person *Person = Config->Person + i; + for(int j = 0; j < Person->SupportCount; ++j) + { + support *Support = Person->Support + j; + if(Support->IconType == IT_GRAPHICAL) { - printf("No credentials for co-host %s. Please contact miblodelcarpio@gmail.com with their:\n" - " Full name\n" - " Homepage URL (optional)\n" - " Financial support info, e.g. Patreon URL (optional)\n", Metadata->co_hosts[i]); - return CreditsError_NoCredentials; + asset *IconAsset = GetAsset(Support->Icon, ASSET_IMG); + if(!IconAsset) + { + IconAsset = PushAsset(Support->Icon, ASSET_IMG, Support->IconVariants, FALSE); + } + Support->IconAsset = IconAsset; } } } +} - if(Metadata->guest_count > 0) +typedef enum +{ + AI_PROJECT_ART, + AI_PROJECT_ICON, + AI_ENTRY_ART, +} associable_identifier; + +void * +ConfirmAssociationsOfProject(db_header_project *Project, asset *Asset, db_asset *AssetInDB, int Index) +{ + if(Project->ArtIndex == Index) { - for(int i = 0; i < Metadata->guest_count; ++i) + Asset->Associated = TRUE; + AssetInDB->Associated = TRUE; + } + if(Project->IconIndex == Index) + { + Asset->Associated = TRUE; + AssetInDB->Associated = TRUE; + } + + db_entry *Entry = LocateFirstEntry(Project); + for(int j = 0; j < Project->EntryCount; ++j, ++Entry) + { + if(Entry->ArtIndex == Index) { - if(SearchCredentials(CreditsMenu, HasCreditsMenu, Metadata->guests[i], "Guest", Speakers) == CreditsError_NoCredentials) + Asset->Associated = TRUE; + AssetInDB->Associated = TRUE; + } + } + + db_header_project *Child = LocateFirstChildProject(Project); + for(int ChildIndex = 0; ChildIndex < Project->ChildCount; ++ChildIndex) + { + Child = ConfirmAssociationsOfProject(Child, Asset, AssetInDB, Index); + } + + return SkipProjectAndChildren(Project); +} + +void +ConfirmAssociations(asset *Asset, db_asset *AssetInDB, int Index) +{ + Asset->Associated = FALSE; + AssetInDB->Associated = FALSE; + + db_block_projects *ProjectsBlock = LocateBlock(B_PROJ); + db_header_project *Project = LocateFirstChildProjectOfBlock(ProjectsBlock); + for(int i = 0; i < ProjectsBlock->Count; ++i) + { + Project = ConfirmAssociationsOfProject(Project, Asset, AssetInDB, Index); + } +} + +db_asset * +LocateAssetByIndex(uint16_t Index) +{ + db_asset *Result = 0; + db_block_assets *AssetsBlock = LocateBlock(B_ASET); + db_asset *This = LocateFirstAsset(AssetsBlock); + for(int i = 0; i < AssetsBlock->Count; ++i) + { + if(Index == i) + { + Result = This; + break; + } + This = SkipAsset(This); + } + return Result; +} + +asset * +SyncAssetAssociation(string Path, uint64_t Variants, db_project_index Index, associable_identifier Type) +{ + asset *Asset = GetAsset(Path, ASSET_IMG); + if(!Asset) + { + Asset = PushAsset(Path, ASSET_IMG, Variants, TRUE); + } + + Asset->Associated = TRUE; + DB.Metadata.Signposts.ProjectHeader.Ptr = LocateProject(Index); + int NewIndex = UpdateAsset(Asset, FALSE); + db_header_project *ProjectInDB = DB.Metadata.Signposts.ProjectHeader.Ptr; + int32_t *StoredIndex = 0; + switch(Type) + { + case AI_PROJECT_ART: { StoredIndex = &ProjectInDB->ArtIndex; } break; + case AI_PROJECT_ICON: { StoredIndex = &ProjectInDB->IconIndex; } break; + default: Assert(0); break; + + } + + int OldIndex = *StoredIndex; + *StoredIndex = NewIndex; + if(OldIndex >= 0 && OldIndex != NewIndex) + { + db_asset *OldAssetInDB = LocateAssetByIndex(OldIndex); + asset *OldAsset = GetAsset(Wrap0i(OldAssetInDB->Filename, sizeof(OldAssetInDB->Filename)), OldAssetInDB->Type); + ConfirmAssociations(OldAsset, OldAssetInDB, OldIndex); + //DeleteStaleAssets(); // TODO(matt): Maybe do this later? + } + return Asset; +} + +void +PushMediumIconAsset(medium *Medium) +{ + if(Medium->IconType == IT_GRAPHICAL) + { + asset *Asset = GetAsset(Medium->Icon, ASSET_IMG); + if(!Asset) + { + Asset = PushAsset(Medium->Icon, ASSET_IMG, Medium->IconVariants, FALSE); + } + Medium->IconAsset = Asset; + } +} + +void +PushProjectAssets(project *P) +{ + string Theme = MakeString("sls", "cinera__", &P->Theme, ".css"); + if(!GetAsset(Theme, ASSET_CSS)) + { + PushAsset(Theme, ASSET_CSS, CAV_DEFAULT_UNSET, FALSE); // NOTE(matt): This may want to be associated, if we change to + // storing the Theme by index, rather than a string + } + + if(P->Art.Length) + { + P->ArtAsset = SyncAssetAssociation(P->Art, P->ArtVariants, P->Index, AI_PROJECT_ART); + } + + if(P->Icon.Length) + { + P->IconAsset = SyncAssetAssociation(P->Icon, P->IconVariants, P->Index, AI_PROJECT_ICON); + } + + for(int i = 0; i < P->MediumCount; ++i) + { + PushMediumIconAsset(P->Medium + i); + } + + for(int i = 0; i < P->ChildCount; ++i) + { + PushProjectAssets(&P->Child[i]); + } + FreeString(&Theme); +} + +void +PushThemeAssets() +{ + string GlobalTheme = MakeString("sls", "cinera__", &Config->GlobalTheme, ".css"); + if(!GetAsset(GlobalTheme, ASSET_CSS)) + { + PushAsset(GlobalTheme, ASSET_CSS, CAV_DEFAULT_UNSET, FALSE); // NOTE(matt): This may want to be associated if we change to + // storing the Theme by index rather than a string + } + FreeString(&GlobalTheme); + for(int i = 0; i < Config->ProjectCount; ++i) + { + PushProjectAssets(&Config->Project[i]); + } +} + +void +PushConfiguredAssets() +{ + PushSupportIconAssets(); + PushThemeAssets(); +} + +char *RoleStrings[] = +{ + "Cohost", + "Guest", + "Host", + "Indexer", +}; + +typedef enum +{ + R_COHOST, + R_GUEST, + R_HOST, + R_INDEXER, +} role; + +void +PushIcon(buffer *Buffer, bool GrowableBuffer, icon_type IconType, string IconString, asset *IconAsset, uint64_t Variants, page_type PageType, bool *RequiresCineraJS) +{ + if(IconType == IT_GRAPHICAL) + { + buffer AssetURL = {}; + ConstructResolvedAssetURL(&AssetURL, IconAsset, PageType); + if(Variants) + { + sprite *S = &IconAsset->Sprite; + *RequiresCineraJS = TRUE; + + if(GrowableBuffer) { - printf("No credentials for guest %s. Please contact miblodelcarpio@gmail.com with their:\n" - " Full name\n" - " Homepage URL (optional)\n" - " Financial support info, e.g. Patreon URL (optional)\n", Metadata->guests[i]); - return CreditsError_NoCredentials; + AppendStringToBuffer(Buffer, Wrap0("
Dimensions.Width, + IconAsset->Dimensions.Height, + S->TileDim.Width, + S->TileDim.Height, + S->XLight, + S->XDark, + S->YNormal, + S->YFocused, + S->YDisabled, + AssetURL.Location); + } + + PushAssetLandmark(Buffer, IconAsset, PageType, GrowableBuffer); + + if(GrowableBuffer) + { + AppendStringToBuffer(Buffer, Wrap0("\">
")); + } + else + { + CopyStringToBuffer(Buffer, "\">
"); + } + } + else + { + if(GrowableBuffer) + { + AppendStringToBuffer(Buffer, Wrap0("")); + } + else + { + CopyStringToBuffer(Buffer, "\">"); + } + } + DeclaimBuffer(&AssetURL); + } + else + { + if(GrowableBuffer) + { + AppendStringToBuffer(Buffer, IconString); + } + else + { + CopyStringToBuffer(Buffer, "%.*s", (int)IconString.Length, IconString.Base); + } + } +} + +void +PushCredentials(buffer *CreditsMenu, speakers *Speakers, person *Actor, role Role, bool *RequiresCineraJS) +{ + if(Role != R_INDEXER) + { + Speakers->Speaker = Fit(Speakers->Speaker, sizeof(*Speakers->Speaker), Speakers->Count, 4, TRUE); + Speakers->Speaker[Speakers->Count].Person = Actor; + ++Speakers->Count; + } + + if(CreditsMenu->Ptr == CreditsMenu->Location) + { + CopyStringToBuffer(CreditsMenu, + "
\n" + " Credits\n" + "
\n"); + + } + + CopyStringToBuffer(CreditsMenu, + " \n"); + if(Actor->Homepage.Length) + { + CopyStringToBuffer(CreditsMenu, + " \n" + "
%s
\n" + "
%.*s
\n" + "
\n", + (int)Actor->Homepage.Length, Actor->Homepage.Base, + RoleStrings[Role], + (int)Actor->Name.Length, Actor->Name.Base); + } + else + { + CopyStringToBuffer(CreditsMenu, + "
\n" + "
%s
\n" + "
%.*s
\n" + "
\n", + RoleStrings[Role], + (int)Actor->Name.Length, Actor->Name.Base); + } + + // TODO(matt): Handle multiple support platforms! + // AFD + if(Actor->Support) + { + support *Support = Actor->Support; + CopyStringToBuffer(CreditsMenu, + " ", + (int)Support->URL.Length, Support->URL.Base); + + PushIcon(CreditsMenu, FALSE, Support->IconType, Support->Icon, Support->IconAsset, Support->IconVariants, PAGE_PLAYER, RequiresCineraJS); + CopyStringToBuffer(CreditsMenu, "\n"); + } + + CopyStringToBuffer(CreditsMenu, + "
\n"); +} + +void +FreeCredentials(speakers *S) +{ + FreeAndResetCount(S->Speaker, S->Count); +} + +void +WaitForInput() +{ + fprintf(stderr, "Press Enter to continue...\n"); + getchar(); +} + +void +ErrorCredentials(string Actor, role Role) +{ + Colourise(CS_ERROR); + fprintf(stderr, "No credentials for %s %.*s\n", RoleStrings[Role], (int)Actor.Length, Actor.Base); + Colourise(CS_END); + fprintf(stderr, "Perhaps you'd like to add a new person to your config file, e.g.:\n" + " person = \"%.*s\"\n" + " {\n" + " name = \"Jane Doe\";\n" + " homepage = \"https://example.com/\";\n" + " support = \"their_support_platform\";\n" + " }\n", (int)Actor.Length, Actor.Base); + WaitForInput(); +} + +int +BuildCredits(buffer *CreditsMenu, HMML_VideoMetaData *Metadata, speakers *Speakers, bool *RequiresCineraJS) +{ + person *Host = GetPersonFromConfig(Wrap0(Metadata->member)); + if(Host) + { + PushCredentials(CreditsMenu, Speakers, Host, R_HOST, RequiresCineraJS); + } + else + { + ErrorCredentials(Wrap0(Metadata->member), R_HOST); + return CreditsError_NoCredentials; + } + + for(int i = 0; i < Metadata->co_host_count; ++i) + { + person *CoHost = GetPersonFromConfig(Wrap0(Metadata->co_hosts[i])); + if(CoHost) + { + PushCredentials(CreditsMenu, Speakers, CoHost, R_COHOST, RequiresCineraJS); + } + else + { + ErrorCredentials(Wrap0(Metadata->co_hosts[i]), R_COHOST); + return CreditsError_NoCredentials; + } + } + + for(int i = 0; i < Metadata->guest_count; ++i) + { + person *Guest = GetPersonFromConfig(Wrap0(Metadata->guests[i])); + if(Guest) + { + PushCredentials(CreditsMenu, Speakers, Guest, R_GUEST, RequiresCineraJS); + } + else + { + ErrorCredentials(Wrap0(Metadata->guests[i]), R_GUEST); + return CreditsError_NoCredentials; } } @@ -2746,29 +6150,31 @@ BuildCredits(buffer *CreditsMenu, bool *HasCreditsMenu, HMML_VideoMetaData *Meta { for(int i = 0; i < Metadata->annotator_count; ++i) { - if(SearchCredentials(CreditsMenu, HasCreditsMenu, Metadata->annotators[i], "Annotator", 0) == CreditsError_NoCredentials) + person *Indexer = GetPersonFromConfig(Wrap0(Metadata->annotators[i])); + if(Indexer) { - printf("No credentials for annotator %s. Please contact miblodelcarpio@gmail.com with their:\n" - " Full name\n" - " Homepage URL (optional)\n" - " Financial support info, e.g. Patreon URL (optional)\n", Metadata->annotators[i]); + PushCredentials(CreditsMenu, Speakers, Indexer, R_INDEXER, RequiresCineraJS); + } + else + { + ErrorCredentials(Wrap0(Metadata->annotators[i]), R_INDEXER); return CreditsError_NoCredentials; } } } else { - if(*HasCreditsMenu == TRUE) + if(CreditsMenu->Ptr > CreditsMenu->Location) { CopyStringToBuffer(CreditsMenu, "
\n" "
\n"); } - fprintf(stderr, "Missing \"annotator\" in the [video] node\n"); - return CreditsError_NoAnnotator; + fprintf(stderr, "Missing \"indexer\" in the [video] node\n"); + return CreditsError_NoIndexer; } - if(*HasCreditsMenu == TRUE) + if(CreditsMenu->Ptr > CreditsMenu->Location) { CopyStringToBuffer(CreditsMenu, "
\n" @@ -2798,12 +6204,12 @@ BuildReference(ref_info *ReferencesArray, int RefIdentifier, int UniqueRefs, HMM { CopyString(ReferencesArray[UniqueRefs].ID, sizeof(ReferencesArray[UniqueRefs].ID), "%s", Ref->isbn); if(!Ref->url) { CopyString(ReferencesArray[UniqueRefs].URL, sizeof(ReferencesArray[UniqueRefs].URL), "https://isbndb.com/book/%s", Ref->isbn); } - else { CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, sizeof(ReferencesArray[UniqueRefs].URL), Ref->url); } + else { CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, sizeof(ReferencesArray[UniqueRefs].URL), Wrap0(Ref->url)); } } else if(Ref->url) { - CopyStringNoFormat(ReferencesArray[UniqueRefs].ID, sizeof(ReferencesArray[UniqueRefs].ID), Ref->url); - CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, sizeof(ReferencesArray[UniqueRefs].URL), Ref->url); + CopyStringNoFormat(ReferencesArray[UniqueRefs].ID, sizeof(ReferencesArray[UniqueRefs].ID), Wrap0(Ref->url)); + CopyStringNoFormat(ReferencesArray[UniqueRefs].URL, sizeof(ReferencesArray[UniqueRefs].URL), Wrap0(Ref->url)); } else { return RC_INVALID_REFERENCE; } @@ -2823,50 +6229,50 @@ BuildReference(ref_info *ReferencesArray, int RefIdentifier, int UniqueRefs, HMM case (REF_TITLE | REF_AUTHOR | REF_PUBLISHER): { CopyString(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), "%s (%s)", Ref->author, Ref->publisher); - CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Ref->title); + CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Wrap0(Ref->title)); } break; case (REF_AUTHOR | REF_SITE | REF_PAGE): { - CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Ref->site); + CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Wrap0(Ref->site)); CopyString(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), "%s: \"%s\"", Ref->author, Ref->page); } break; case (REF_PAGE | REF_TITLE): { - CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Ref->title); - CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Ref->page); + CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Wrap0(Ref->title)); + CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Wrap0(Ref->page)); } break; case (REF_SITE | REF_PAGE): { - CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Ref->site); - CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Ref->page); + CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Wrap0(Ref->site)); + CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Wrap0(Ref->page)); } break; case (REF_SITE | REF_TITLE): { - CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Ref->site); - CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Ref->title); + CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Wrap0(Ref->site)); + CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Wrap0(Ref->title)); } break; case (REF_TITLE | REF_AUTHOR): { - CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Ref->author); - CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Ref->title); + CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Wrap0(Ref->author)); + CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Wrap0(Ref->title)); } break; case (REF_ARTICLE | REF_AUTHOR): { - CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Ref->author); - CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Ref->article); + CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Wrap0(Ref->author)); + CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Wrap0(Ref->article)); } break; case (REF_TITLE | REF_PUBLISHER): { - CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Ref->publisher); - CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Ref->title); + CopyStringNoFormat(ReferencesArray[UniqueRefs].Source, sizeof(ReferencesArray[UniqueRefs].Source), Wrap0(Ref->publisher)); + CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Wrap0(Ref->title)); } break; case REF_TITLE: { - CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Ref->title); + CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Wrap0(Ref->title)); } break; case REF_SITE: { - CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Ref->site); + CopyStringNoFormat(ReferencesArray[UniqueRefs].RefTitle, sizeof(ReferencesArray[UniqueRefs].RefTitle), Wrap0(Ref->site)); } break; default: return RC_INVALID_REFERENCE; break; } @@ -2877,77 +6283,67 @@ BuildReference(ref_info *ReferencesArray, int RefIdentifier, int UniqueRefs, HMM } void -InsertCategory(categories *GlobalTopics, categories *LocalTopics, categories *GlobalMedia, categories *LocalMedia, char *Marker) +InsertCategory(categories *GlobalTopics, categories *LocalTopics, categories *GlobalMedia, categories *LocalMedia, string Marker) { - bool IsMedium = FALSE; + medium *Medium = GetMediumFromProject(CurrentProject, Marker); - int CategoryMediumIndex; - for(CategoryMediumIndex = 0; CategoryMediumIndex < ArrayCount(CategoryMedium); ++CategoryMediumIndex) - { - if(!StringsDiffer(CategoryMedium[CategoryMediumIndex].Medium, Marker)) - { - IsMedium = TRUE; - break; - } - } - - if(IsMedium) + if(Medium) { int MediumIndex; for(MediumIndex = 0; MediumIndex < LocalMedia->Count; ++MediumIndex) { - if(!StringsDiffer(CategoryMedium[CategoryMediumIndex].Medium, LocalMedia->Category[MediumIndex].Marker)) + if(!StringsDifferLv0(Medium->ID, LocalMedia->Category[MediumIndex].Marker)) { return; } - if((StringsDiffer(CategoryMedium[CategoryMediumIndex].WrittenName, LocalMedia->Category[MediumIndex].WrittenText)) < 0) + if((StringsDifferLv0(Medium->Name, LocalMedia->Category[MediumIndex].WrittenText)) < 0) { int CategoryCount; for(CategoryCount = LocalMedia->Count; CategoryCount > MediumIndex; --CategoryCount) { - CopyString(LocalMedia->Category[CategoryCount].Marker, sizeof(LocalMedia->Category[CategoryCount].Marker), "%s", LocalMedia->Category[CategoryCount-1].Marker); - CopyString(LocalMedia->Category[CategoryCount].WrittenText, sizeof(LocalMedia->Category[CategoryCount].WrittenText), "%s", LocalMedia->Category[CategoryCount-1].WrittenText); + ClearCopyString(LocalMedia->Category[CategoryCount].Marker, sizeof(LocalMedia->Category[CategoryCount].Marker), "%s", LocalMedia->Category[CategoryCount-1].Marker); + ClearCopyString(LocalMedia->Category[CategoryCount].WrittenText, sizeof(LocalMedia->Category[CategoryCount].WrittenText), "%s", LocalMedia->Category[CategoryCount-1].WrittenText); } - CopyString(LocalMedia->Category[CategoryCount].Marker, sizeof(LocalMedia->Category[CategoryCount].Marker), "%s", CategoryMedium[CategoryMediumIndex].Medium); - CopyString(LocalMedia->Category[CategoryCount].WrittenText, sizeof(LocalMedia->Category[CategoryCount].WrittenText), "%s", CategoryMedium[CategoryMediumIndex].WrittenName); + ClearCopyString(LocalMedia->Category[CategoryCount].Marker, sizeof(LocalMedia->Category[CategoryCount].Marker), "%.*s", (int)Medium->ID.Length, Medium->ID.Base); + ClearCopyString(LocalMedia->Category[CategoryCount].WrittenText, sizeof(LocalMedia->Category[CategoryCount].WrittenText), "%.*s", (int)Medium->Name.Length, Medium->Name.Base); break; } } if(MediumIndex == LocalMedia->Count) { - CopyString(LocalMedia->Category[MediumIndex].Marker, sizeof(LocalMedia->Category[MediumIndex].Marker), "%s", CategoryMedium[CategoryMediumIndex].Medium); - CopyString(LocalMedia->Category[MediumIndex].WrittenText, sizeof(LocalMedia->Category[MediumIndex].WrittenText), "%s", CategoryMedium[CategoryMediumIndex].WrittenName); + CopyString(LocalMedia->Category[MediumIndex].Marker, sizeof(LocalMedia->Category[MediumIndex].Marker), "%.*s", (int)Medium->ID.Length, Medium->ID.Base); + CopyString(LocalMedia->Category[MediumIndex].WrittenText, sizeof(LocalMedia->Category[MediumIndex].WrittenText), "%.*s", (int)Medium->Name.Length, Medium->Name.Base); } ++LocalMedia->Count; for(MediumIndex = 0; MediumIndex < GlobalMedia->Count; ++MediumIndex) { - if(!StringsDiffer(CategoryMedium[CategoryMediumIndex].Medium, GlobalMedia->Category[MediumIndex].Marker)) + if(!StringsDifferLv0(Medium->ID, GlobalMedia->Category[MediumIndex].Marker)) { return; } - if((StringsDiffer(CategoryMedium[CategoryMediumIndex].WrittenName, GlobalMedia->Category[MediumIndex].WrittenText)) < 0) + if((StringsDifferLv0(Medium->Name, GlobalMedia->Category[MediumIndex].WrittenText)) < 0) { int CategoryCount; for(CategoryCount = GlobalMedia->Count; CategoryCount > MediumIndex; --CategoryCount) { - CopyString(GlobalMedia->Category[CategoryCount].Marker, sizeof(GlobalMedia->Category[CategoryCount].Marker), "%s", GlobalMedia->Category[CategoryCount-1].Marker); - CopyString(GlobalMedia->Category[CategoryCount].WrittenText, sizeof(GlobalMedia->Category[CategoryCount].WrittenText), "%s", GlobalMedia->Category[CategoryCount-1].WrittenText); + ClearCopyString(GlobalMedia->Category[CategoryCount].Marker, sizeof(GlobalMedia->Category[CategoryCount].Marker), "%s", GlobalMedia->Category[CategoryCount-1].Marker); + ClearCopyString(GlobalMedia->Category[CategoryCount].WrittenText, sizeof(GlobalMedia->Category[CategoryCount].WrittenText), "%s", GlobalMedia->Category[CategoryCount-1].WrittenText); } - CopyString(GlobalMedia->Category[CategoryCount].Marker, sizeof(GlobalMedia->Category[CategoryCount].Marker), "%s", CategoryMedium[CategoryMediumIndex].Medium); - CopyString(GlobalMedia->Category[CategoryCount].WrittenText, sizeof(GlobalMedia->Category[CategoryCount].WrittenText), "%s", CategoryMedium[CategoryMediumIndex].WrittenName); + ClearCopyString(GlobalMedia->Category[CategoryCount].Marker, sizeof(GlobalMedia->Category[CategoryCount].Marker), "%.*s", (int)Medium->ID.Length, Medium->ID.Base); + ClearCopyString(GlobalMedia->Category[CategoryCount].WrittenText, sizeof(GlobalMedia->Category[CategoryCount].WrittenText), "%.*s", (int)Medium->Name.Length, Medium->Name.Base); break; } } if(MediumIndex == GlobalMedia->Count) { - CopyString(GlobalMedia->Category[MediumIndex].Marker, sizeof(GlobalMedia->Category[MediumIndex].Marker), "%s", CategoryMedium[CategoryMediumIndex].Medium); - CopyString(GlobalMedia->Category[MediumIndex].WrittenText, sizeof(GlobalMedia->Category[MediumIndex].WrittenText), "%s", CategoryMedium[CategoryMediumIndex].WrittenName); + CopyString(GlobalMedia->Category[MediumIndex].Marker, sizeof(GlobalMedia->Category[MediumIndex].Marker), "%.*s", (int)Medium->ID.Length, Medium->ID.Base); + CopyString(GlobalMedia->Category[MediumIndex].WrittenText, sizeof(GlobalMedia->Category[MediumIndex].WrittenText), "%.*s", (int)Medium->Name.Length, Medium->Name.Base); } ++GlobalMedia->Count; @@ -2957,49 +6353,49 @@ InsertCategory(categories *GlobalTopics, categories *LocalTopics, categories *Gl int TopicIndex; for(TopicIndex = 0; TopicIndex < LocalTopics->Count; ++TopicIndex) { - if(!StringsDiffer(Marker, LocalTopics->Category[TopicIndex].Marker)) + if(!StringsDifferLv0(Marker, LocalTopics->Category[TopicIndex].Marker)) { return; } - if((StringsDiffer(Marker, LocalTopics->Category[TopicIndex].Marker)) < 0) + if((StringsDifferLv0(Marker, LocalTopics->Category[TopicIndex].Marker)) < 0) { int CategoryCount; for(CategoryCount = LocalTopics->Count; CategoryCount > TopicIndex; --CategoryCount) { - CopyString(LocalTopics->Category[CategoryCount].Marker, sizeof(LocalTopics->Category[CategoryCount].Marker), "%s", LocalTopics->Category[CategoryCount-1].Marker); + ClearCopyString(LocalTopics->Category[CategoryCount].Marker, sizeof(LocalTopics->Category[CategoryCount].Marker), "%s", LocalTopics->Category[CategoryCount-1].Marker); } - CopyString(LocalTopics->Category[CategoryCount].Marker, sizeof(LocalTopics->Category[CategoryCount].Marker), "%s", Marker); + ClearCopyString(LocalTopics->Category[CategoryCount].Marker, sizeof(LocalTopics->Category[CategoryCount].Marker), "%.*s", (int)Marker.Length, Marker.Base); break; } } if(TopicIndex == LocalTopics->Count) { - CopyString(LocalTopics->Category[TopicIndex].Marker, sizeof(LocalTopics->Category[TopicIndex].Marker), "%s", Marker); + CopyString(LocalTopics->Category[TopicIndex].Marker, sizeof(LocalTopics->Category[TopicIndex].Marker), "%.*s", (int)Marker.Length, Marker.Base); } ++LocalTopics->Count; for(TopicIndex = 0; TopicIndex < GlobalTopics->Count; ++TopicIndex) { - if(!StringsDiffer(Marker, GlobalTopics->Category[TopicIndex].Marker)) + if(!StringsDifferLv0(Marker, GlobalTopics->Category[TopicIndex].Marker)) { return; } // NOTE(matt): This successfully sorts "nullTopic" at the end, but maybe figure out a more general way to force the // order of stuff, perhaps blocks of dudes that should sort to the start / end - if(((StringsDiffer(Marker, GlobalTopics->Category[TopicIndex].Marker)) < 0 || !StringsDiffer(GlobalTopics->Category[TopicIndex].Marker, "nullTopic"))) + if(((StringsDifferLv0(Marker, GlobalTopics->Category[TopicIndex].Marker)) < 0 || !StringsDiffer0(GlobalTopics->Category[TopicIndex].Marker, "nullTopic"))) { - if(StringsDiffer(Marker, "nullTopic")) // NOTE(matt): This test (with the above || condition) forces nullTopic never to be inserted, only appended + if(StringsDifferLv0(Marker, "nullTopic")) // NOTE(matt): This test (with the above || condition) forces nullTopic never to be inserted, only appended { int CategoryCount; for(CategoryCount = GlobalTopics->Count; CategoryCount > TopicIndex; --CategoryCount) { - CopyString(GlobalTopics->Category[CategoryCount].Marker, sizeof(GlobalTopics->Category[CategoryCount].Marker), "%s", GlobalTopics->Category[CategoryCount-1].Marker); + ClearCopyString(GlobalTopics->Category[CategoryCount].Marker, sizeof(GlobalTopics->Category[CategoryCount].Marker), "%s", GlobalTopics->Category[CategoryCount-1].Marker); } - CopyString(GlobalTopics->Category[CategoryCount].Marker, sizeof(GlobalTopics->Category[CategoryCount].Marker), "%s", Marker); + ClearCopyString(GlobalTopics->Category[CategoryCount].Marker, sizeof(GlobalTopics->Category[CategoryCount].Marker), "%.*s", (int)Marker.Length, Marker.Base); break; } } @@ -3007,7 +6403,7 @@ InsertCategory(categories *GlobalTopics, categories *LocalTopics, categories *Gl if(TopicIndex == GlobalTopics->Count) { - CopyString(GlobalTopics->Category[TopicIndex].Marker, sizeof(GlobalTopics->Category[TopicIndex].Marker), "%s", Marker); + CopyString(GlobalTopics->Category[TopicIndex].Marker, sizeof(GlobalTopics->Category[TopicIndex].Marker), "%.*s", (int)Marker.Length, Marker.Base); } ++GlobalTopics->Count; @@ -3015,27 +6411,81 @@ InsertCategory(categories *GlobalTopics, categories *LocalTopics, categories *Gl } void -BuildCategories(buffer *AnnotationClass, buffer *CategoryIcons, categories *LocalTopics, categories *LocalMedia, int *MarkerIndex, char *DefaultMedium) +BuildTimestampClass(buffer *TimestampClass, categories *LocalTopics, categories *LocalMedia, string DefaultMedium) { - bool CategoriesSpan = FALSE; - if(!(LocalTopics->Count == 1 && !StringsDiffer(LocalTopics->Category[0].Marker, "nullTopic") - && LocalMedia->Count == 1 && !StringsDiffer(LocalMedia->Category[0].Marker, DefaultMedium))) - { - CategoriesSpan = TRUE; - CopyStringToBuffer(CategoryIcons, ""); - } - - if(LocalTopics->Count == 1 && !StringsDiffer(LocalTopics->Category[0].Marker, "nullTopic")) + if(LocalTopics->Count == 1 && !StringsDiffer0(LocalTopics->Category[0].Marker, "nullTopic")) { + // NOTE(matt): Stack-string char SanitisedMarker[StringLength(LocalTopics->Category[0].Marker) + 1]; CopyString(SanitisedMarker, sizeof(SanitisedMarker), "%s", LocalTopics->Category[0].Marker); SanitisePunctuation(SanitisedMarker); - CopyStringToBuffer(AnnotationClass, " cat_%s", SanitisedMarker); + CopyStringToBuffer(TimestampClass, " cat_%s", SanitisedMarker); } else { for(int i = 0; i < LocalTopics->Count; ++i) { + // NOTE(matt): Stack-string + char SanitisedMarker[StringLength(LocalTopics->Category[i].Marker) + 1]; + CopyString(SanitisedMarker, sizeof(SanitisedMarker), "%s", LocalTopics->Category[i].Marker); + SanitisePunctuation(SanitisedMarker); + + CopyStringToBuffer(TimestampClass, " cat_%s", + SanitisedMarker); + } + } + + if(LocalMedia->Count == 1 && !StringsDifferLv0(DefaultMedium, LocalMedia->Category[0].Marker)) + { + // NOTE(matt): Stack-string + char SanitisedMarker[StringLength(LocalMedia->Category[0].Marker) + 1]; + CopyString(SanitisedMarker, sizeof(SanitisedMarker), "%s", LocalMedia->Category[0].Marker); + SanitisePunctuation(SanitisedMarker); + CopyStringToBuffer(TimestampClass, " %s", SanitisedMarker); + } + else + { + for(int i = 0; i < LocalMedia->Count; ++i) + { + // NOTE(matt): Stack-string + char SanitisedMarker[StringLength(LocalMedia->Category[i].Marker) + 1]; + CopyString(SanitisedMarker, sizeof(SanitisedMarker), "%s", LocalMedia->Category[i].Marker); + SanitisePunctuation(SanitisedMarker); + + medium *Medium = GetMediumFromProject(CurrentProject, Wrap0i(LocalMedia->Category[i].Marker, sizeof(LocalMedia->Category[i].Marker))); + if(Medium) + { + if(Medium->Hidden) + { + CopyStringToBuffer(TimestampClass, " off_%s skip", SanitisedMarker); + } + else + { + CopyStringToBuffer(TimestampClass, " %s", SanitisedMarker); + } + } + } + } + + CopyStringToBuffer(TimestampClass, "\""); +} + +void +BuildCategoryIcons(buffer *CategoryIcons, categories *LocalTopics, categories *LocalMedia, string DefaultMedium, bool *RequiresCineraJS) +{ + bool CategoriesSpan = FALSE; + if(!(LocalTopics->Count == 1 && !StringsDiffer0(LocalTopics->Category[0].Marker, "nullTopic") + && LocalMedia->Count == 1 && !StringsDifferLv0(DefaultMedium, LocalMedia->Category[0].Marker))) + { + CategoriesSpan = TRUE; + CopyStringToBuffer(CategoryIcons, ""); + } + + if(!(LocalTopics->Count == 1 && !StringsDiffer0(LocalTopics->Category[0].Marker, "nullTopic"))) + { + for(int i = 0; i < LocalTopics->Count; ++i) + { + // NOTE(matt): Stack-string char SanitisedMarker[StringLength(LocalTopics->Category[i].Marker) + 1]; CopyString(SanitisedMarker, sizeof(SanitisedMarker), "%s", LocalTopics->Category[i].Marker); SanitisePunctuation(SanitisedMarker); @@ -3043,44 +6493,26 @@ BuildCategories(buffer *AnnotationClass, buffer *CategoryIcons, categories *Loca CopyStringToBuffer(CategoryIcons, "
", LocalTopics->Category[i].Marker, SanitisedMarker); - - CopyStringToBuffer(AnnotationClass, " cat_%s", - SanitisedMarker); } } - if(LocalMedia->Count == 1 && !StringsDiffer(LocalMedia->Category[0].Marker, DefaultMedium)) - { - char SanitisedMarker[StringLength(LocalMedia->Category[0].Marker) + 1]; - CopyString(SanitisedMarker, sizeof(SanitisedMarker), "%s", LocalMedia->Category[0].Marker); - SanitisePunctuation(SanitisedMarker); - CopyStringToBuffer(AnnotationClass, " %s", SanitisedMarker); - } - else + if(!(LocalMedia->Count == 1 && !StringsDifferLv0(DefaultMedium, LocalMedia->Category[0].Marker))) { for(int i = 0; i < LocalMedia->Count; ++i) { + // NOTE(matt): Stack-string char SanitisedMarker[StringLength(LocalMedia->Category[i].Marker) + 1]; CopyString(SanitisedMarker, sizeof(SanitisedMarker), "%s", LocalMedia->Category[i].Marker); SanitisePunctuation(SanitisedMarker); - if(!StringsDiffer(LocalMedia->Category[i].Marker, "afk")) // TODO(matt): Initially hidden config + medium *Medium = GetMediumFromProject(CurrentProject, Wrap0i(LocalMedia->Category[i].Marker, sizeof(LocalMedia->Category[i].Marker))); + if(Medium && !Medium->Hidden) { - CopyStringToBuffer(AnnotationClass, " off_%s skip", SanitisedMarker); // TODO(matt): Bulletproof this? - } - else - { - for(int j = 0; j < ArrayCount(CategoryMedium); ++j) - { - if(!StringsDiffer(LocalMedia->Category[i].Marker, CategoryMedium[j].Medium)) - { - CopyStringToBuffer(CategoryIcons, "
%s
", - LocalMedia->Category[i].WrittenText, - LocalMedia->Category[i].Marker, - CategoryMedium[j].Icon); - CopyStringToBuffer(AnnotationClass, " %s", SanitisedMarker); - } - } + CopyStringToBuffer(CategoryIcons, "
", LocalMedia->Category[i].WrittenText, LocalMedia->Category[i].Marker); + + PushIcon(CategoryIcons, FALSE, Medium->IconType, Medium->Icon, Medium->IconAsset, Medium->IconVariants, PAGE_PLAYER, RequiresCineraJS); + + CopyStringToBuffer(CategoryIcons, "
"); } } } @@ -3089,14 +6521,12 @@ BuildCategories(buffer *AnnotationClass, buffer *CategoryIcons, categories *Loca { CopyStringToBuffer(CategoryIcons, "
"); } - - CopyStringToBuffer(AnnotationClass, "\""); } -int -StringToInt(char *String) +int64_t +String0ToInt(char *String) { - int Result = 0; + int64_t Result = 0; while(*String) { Result = Result * 10 + (*String - '0'); @@ -3118,56 +6548,77 @@ CurlIntoBuffer(char *InPtr, size_t CharLength, size_t Chars, char **OutputPtr) return Length; }; -void +CURLcode CurlQuotes(buffer *QuoteStaging, char *QuotesURL) { fprintf(stderr, "%sFetching%s quotes: %s\n", ColourStrings[CS_ONGOING], ColourStrings[CS_END], QuotesURL); + CURLcode Result = CURLE_FAILED_INIT; + CURL *curl = curl_easy_init(); if(curl) { - CURLcode CurlReturnCode; curl_easy_setopt(curl, CURLOPT_WRITEDATA, &QuoteStaging->Ptr); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlIntoBuffer); curl_easy_setopt(curl, CURLOPT_URL, QuotesURL); - if((CurlReturnCode = curl_easy_perform(curl))) + if((Result = curl_easy_perform(curl))) { - fprintf(stderr, "%s\n", curl_easy_strerror(CurlReturnCode)); + fprintf(stderr, "%s\n", curl_easy_strerror(Result)); } curl_easy_cleanup(curl); } + return Result; } -int +string +GetStringFromBufferT(buffer *B, char Terminator) +{ + // NOTE(matt): This just straight up assumes success + // We may want to make it report a failure if, e.g. B->Ptr != Terminator after the loop + string Result = { .Base = B->Ptr }; + char *Ptr = B->Ptr; + while(Ptr - B->Location < B->Size && *Ptr != Terminator) + { + ++Ptr; + ++Result.Length; + } + return Result; +} + +rc SearchQuotes(buffer *QuoteStaging, int CacheSize, quote_info *Info, int ID) { + rc Result = RC_UNFOUND; QuoteStaging->Ptr = QuoteStaging->Location; while(QuoteStaging->Ptr - QuoteStaging->Location < CacheSize) { - char InID[8] = { 0 }; - char InTime[16] = { 0 }; - QuoteStaging->Ptr += CopyStringNoFormatT(InID, sizeof(InID), QuoteStaging->Ptr, ',') + 1; // Skip past the ',' + string InID = GetStringFromBufferT(QuoteStaging, ','); + QuoteStaging->Ptr += InID.Length + 1; // Skip past the ',' if(StringToInt(InID) == ID) { - QuoteStaging->Ptr += CopyStringNoFormatT(InTime, sizeof(InTime), QuoteStaging->Ptr, ',') + 1; // Skip past the ',' + string InTime = GetStringFromBufferT(QuoteStaging, ','); + QuoteStaging->Ptr += InTime.Length + 1; // Skip past the ',' long int Time = StringToInt(InTime); + // NOTE(matt): Stack-string char DayString[3] = { 0 }; strftime(DayString, 3, "%d", gmtime(&Time)); - int Day = StringToInt(DayString); + int Day = String0ToInt(DayString); + // NOTE(matt): Stack-string char DaySuffix[3]; if(DayString[1] == '1' && Day != 11) { CopyString(DaySuffix, sizeof(DaySuffix), "st"); } else if(DayString[1] == '2' && Day != 12) { CopyString(DaySuffix, sizeof(DaySuffix), "nd"); } else if(DayString[1] == '3' && Day != 13) { CopyString(DaySuffix, sizeof(DaySuffix), "rd"); } else { CopyString(DaySuffix, sizeof(DaySuffix), "th"); } + // NOTE(matt): Stack-string char MonthYear[32]; strftime(MonthYear, 32, "%B, %Y", gmtime(&Time)); CopyString(Info->Date, sizeof(Info->Date), "%d%s %s", Day, DaySuffix, MonthYear); CopyStringNoFormatT(Info->Text, sizeof(Info->Text), QuoteStaging->Ptr, '\n'); - FreeBuffer(QuoteStaging); - return RC_FOUND; + Result = RC_FOUND; + break; } else { @@ -3178,26 +6629,31 @@ SearchQuotes(buffer *QuoteStaging, int CacheSize, quote_info *Info, int ID) ++QuoteStaging->Ptr; } } - return RC_UNFOUND; + return Result; } -int -BuildQuote(quote_info *Info, char *Speaker, int ID, bool ShouldFetchQuotes) +rc +BuildQuote(quote_info *Info, string Speaker, int ID, bool ShouldFetchQuotes) { - char QuoteCacheDir[256]; - CopyString(QuoteCacheDir, sizeof(QuoteCacheDir), "%s/quotes", Config.CacheDir); - char QuoteCachePath[256]; - CopyString(QuoteCachePath, sizeof(QuoteCachePath), "%s/%s", QuoteCacheDir, Speaker); + rc Result = RC_SUCCESS; + // TODO(matt): Generally sanitise this function, e.g. using MakeString0(), curling in to a growing buffer, etc. + // NOTE(matt): Stack-string + char QuoteCacheDir[256] = {}; + CopyString(QuoteCacheDir, sizeof(QuoteCacheDir), "%.*s/quotes", (int)Config->CacheDir.Length, Config->CacheDir.Base); + // NOTE(matt): Stack-string + char QuoteCachePath[256] = {}; + CopyString(QuoteCachePath, sizeof(QuoteCachePath), "%s/%.*s", QuoteCacheDir, (int)Speaker.Length, Speaker.Base); FILE *QuoteCache; - char QuotesURL[256]; + // NOTE(matt): Stack-string + char QuotesURL[256] = {}; // TODO(matt): Make the URL configurable and also handle the case in which the .raw isn't available - CopyString(QuotesURL, sizeof(QuotesURL), "https://dev.abaines.me.uk/quotes/%s.raw", Speaker); + CopyString(QuotesURL, sizeof(QuotesURL), "https://dev.abaines.me.uk/quotes/%.*s.raw", (int)Speaker.Length, Speaker.Base); bool CacheAvailable = FALSE; if(!(QuoteCache = fopen(QuoteCachePath, "a+"))) { - if(MakeDir(QuoteCacheDir) == RC_SUCCESS) + if(MakeDir(Wrap0i(QuoteCacheDir, sizeof(QuoteCacheDir)))) { CacheAvailable = TRUE; } @@ -3215,94 +6671,98 @@ BuildQuote(quote_info *Info, char *Speaker, int ID, bool ShouldFetchQuotes) CacheAvailable = TRUE; } - buffer QuoteStaging; - QuoteStaging.ID = "QuoteStaging"; + buffer QuoteStaging = {}; + QuoteStaging.ID = BID_QUOTE_STAGING; QuoteStaging.Size = Kilobytes(256); if(!(QuoteStaging.Location = malloc(QuoteStaging.Size))) { fclose(QuoteCache); - return RC_ERROR_MEMORY; + Result = RC_ERROR_MEMORY; } + if(Result != RC_ERROR_MEMORY) + { #if DEBUG_MEM - FILE *MemLog = fopen("/home/matt/cinera_mem", "a+"); - fprintf(MemLog, " Allocated QuoteStaging (%d)\n", QuoteStaging.Size); - fclose(MemLog); - printf(" Allocated QuoteStaging (%d)\n", QuoteStaging.Size); + FILE *MemLog = fopen("/home/matt/cinera_mem", "a+"); + fprintf(MemLog, " Allocated QuoteStaging (%ld)\n", QuoteStaging.Size); + fclose(MemLog); + printf(" Allocated QuoteStaging (%ld)\n", QuoteStaging.Size); #endif - QuoteStaging.Ptr = QuoteStaging.Location; + QuoteStaging.Ptr = QuoteStaging.Location; - if(CacheAvailable) - { - fseek(QuoteCache, 0, SEEK_END); - int FileSize = ftell(QuoteCache); - fseek(QuoteCache, 0, SEEK_SET); - - fread(QuoteStaging.Location, FileSize, 1, QuoteCache); - fclose(QuoteCache); - - if(ShouldFetchQuotes || SearchQuotes(&QuoteStaging, FileSize, Info, ID) == RC_UNFOUND) + if(CacheAvailable) { - // TODO(matt): Error handling - CurlQuotes(&QuoteStaging, QuotesURL); + fseek(QuoteCache, 0, SEEK_END); + int FileSize = ftell(QuoteCache); + fseek(QuoteCache, 0, SEEK_SET); - if(!(QuoteCache = fopen(QuoteCachePath, "w"))) - { - perror(QuoteCachePath); - } - fwrite(QuoteStaging.Location, QuoteStaging.Ptr - QuoteStaging.Location, 1, QuoteCache); + fread(QuoteStaging.Location, FileSize, 1, QuoteCache); fclose(QuoteCache); - int CacheSize = QuoteStaging.Ptr - QuoteStaging.Location; - QuoteStaging.Ptr = QuoteStaging.Location; - if(SearchQuotes(&QuoteStaging, CacheSize, Info, ID) == RC_UNFOUND) + if(ShouldFetchQuotes || SearchQuotes(&QuoteStaging, FileSize, Info, ID) == RC_UNFOUND) { - FreeBuffer(&QuoteStaging); - return RC_UNFOUND; + if(CurlQuotes(&QuoteStaging, QuotesURL) == CURLE_OK) + { + if(!(QuoteCache = fopen(QuoteCachePath, "w"))) + { + perror(QuoteCachePath); + } + fwrite(QuoteStaging.Location, QuoteStaging.Ptr - QuoteStaging.Location, 1, QuoteCache); + fclose(QuoteCache); + + int CacheSize = QuoteStaging.Ptr - QuoteStaging.Location; + QuoteStaging.Ptr = QuoteStaging.Location; + Result = SearchQuotes(&QuoteStaging, CacheSize, Info, ID); + } + else + { + Result = RC_UNFOUND; + } } } - } - else - { - CurlQuotes(&QuoteStaging, QuotesURL); - int CacheSize = QuoteStaging.Ptr - QuoteStaging.Location; - QuoteStaging.Ptr = QuoteStaging.Location; - if(SearchQuotes(&QuoteStaging, CacheSize, Info, ID) == RC_UNFOUND) + else { - FreeBuffer(&QuoteStaging); - return RC_UNFOUND; + if(CurlQuotes(&QuoteStaging, QuotesURL) == CURLE_OK) + { + int CacheSize = QuoteStaging.Ptr - QuoteStaging.Location; + QuoteStaging.Ptr = QuoteStaging.Location; + Result = SearchQuotes(&QuoteStaging, CacheSize, Info, ID); + } + else + { + Result = RC_UNFOUND; + } } + FreeBuffer(&QuoteStaging); } - - return RC_SUCCESS; + return Result; } int -GenerateTopicColours(char *Topic) +GenerateTopicColours(neighbourhood *N, string Topic) { - char SanitisedTopic[StringLength(Topic) + 1]; - CopyString(SanitisedTopic, sizeof(SanitisedTopic), "%s", Topic); + // NOTE(matt): Stack-string + char SanitisedTopic[Topic.Length + 1]; + CopyString(SanitisedTopic, sizeof(SanitisedTopic), "%.*s", (int)Topic.Length, Topic.Base); SanitisePunctuation(SanitisedTopic); - for(int i = 0; i < ArrayCount(CategoryMedium); ++i) + medium *Medium = GetMediumFromProject(CurrentProject, Topic); + if(Medium) { - if(!StringsDiffer(Topic, CategoryMedium[i].Medium)) - { - return RC_NOOP; - } + return RC_NOOP; } - file_buffer Topics; - Topics.Buffer.ID = "Topics"; + file Topics = {}; + Topics.Buffer.ID = BID_TOPICS; - if(StringsDiffer(Config.CSSDir, "")) + if(Config->CSSDir.Length > 0) { - CopyString(Topics.Path, sizeof(Topics.Path), "%s/%s/%s", Config.RootDir, Config.CSSDir, BuiltinAssets[ASSET_CSS_TOPICS].Filename); + Topics.Path = MakeString0("lslss", &Config->AssetsRootDir, "/", &Config->CSSDir, "/", BuiltinAssets[ASSET_CSS_TOPICS].Filename); } else { - CopyString(Topics.Path, sizeof(Topics.Path), "%s/%s", Config.RootDir, BuiltinAssets[ASSET_CSS_TOPICS].Filename); + Topics.Path = MakeString0("lss", &Config->AssetsRootDir, "/", BuiltinAssets[ASSET_CSS_TOPICS].Filename); } char *Ptr = Topics.Path + StringLength(Topics.Path) - 1; @@ -3314,7 +6774,7 @@ GenerateTopicColours(char *Topic) DIR *CSSDirHandle; // TODO(matt): open() if(!(CSSDirHandle = opendir(Topics.Path))) { - if(MakeDir(Topics.Path) == RC_ERROR_DIRECTORY) + if(!MakeDir(Wrap0(Topics.Path))) { LogError(LOG_ERROR, "Unable to create directory %s: %s", Topics.Path, strerror(errno)); fprintf(stderr, "Unable to create directory %s: %s\n", Topics.Path, strerror(errno)); @@ -3327,8 +6787,7 @@ GenerateTopicColours(char *Topic) if((Topics.Handle = fopen(Topics.Path, "a+"))) { fseek(Topics.Handle, 0, SEEK_END); - Topics.FileSize = ftell(Topics.Handle); - Topics.Buffer.Size = Topics.FileSize; + Topics.Buffer.Size = ftell(Topics.Handle); fseek(Topics.Handle, 0, SEEK_SET); if(!(Topics.Buffer.Location = malloc(Topics.Buffer.Size))) @@ -3338,9 +6797,9 @@ GenerateTopicColours(char *Topic) #if DEBUG_MEM FILE *MemLog = fopen("/home/matt/cinera_mem", "a+"); - fprintf(MemLog, " Allocated Topics (%d)\n", Topics.Buffer.Size); + fprintf(MemLog, " Allocated Topics (%ld)\n", Topics.Buffer.Size); fclose(MemLog); - printf(" Allocated Topics (%d)\n", Topics.Buffer.Size); + printf(" Allocated Topics (%ld)\n", Topics.Buffer.Size); #endif Topics.Buffer.Ptr = Topics.Buffer.Location; @@ -3362,7 +6821,7 @@ GenerateTopicColours(char *Topic) ++Topics.Buffer.Ptr; } - if(!StringsDiffer(Topic, "nullTopic")) + if(!StringsDifferLv0(Topic, "nullTopic")) { fprintf(Topics.Handle, ".category.%s { border: 1px solid transparent; background: transparent; }\n", SanitisedTopic); @@ -3376,14 +6835,25 @@ GenerateTopicColours(char *Topic) } fclose(Topics.Handle); +#if DEBUG_MEM + MemLog = fopen("/home/matt/cinera_mem", "a+"); + fprintf(MemLog, " Freed Topics (%ld)\n", Topics.Buffer.Size); + fclose(MemLog); + printf(" Freed Topics (%ld)\n", Topics.Buffer.Size); +#endif FreeBuffer(&Topics.Buffer); - if(Assets.Asset[ASSET_CSS_TOPICS].Known) + + asset *Asset = GetPlaceInBook(&Assets.Asset, ASSET_CSS_TOPICS); + if(Asset->Known) { - UpdateAsset(ASSET_CSS_TOPICS, TRUE); + // NOTE(matt): We may index this out directly because InitBuiltinAssets() places it in its own known slot + UpdateAsset(Asset, TRUE); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); } else { - PlaceAsset(BuiltinAssets[ASSET_CSS_TOPICS].Filename, ASSET_CSS, ASSET_CSS_TOPICS); + asset *CSSTopics = BuiltinAssets + ASSET_CSS_TOPICS; + PlaceAsset(Wrap0(CSSTopics->Filename), CSSTopics->Type, CSSTopics->Variants, CSSTopics->Associated, ASSET_CSS_TOPICS); } return RC_SUCCESS; } @@ -3395,9 +6865,189 @@ GenerateTopicColours(char *Topic) } } +/* + * NOTE(matt); + * + * Documentation structure: + * + * section + * + * option + * description + */ + void -PrintUsage(char *BinaryLocation, config *DefaultConfig) +ResetConfigIdentifierDescriptionDisplayedBools(void) { + for(int i = 0; i < IDENT_COUNT; ++i) + { + ConfigIdentifiers[i].IdentifierDescriptionDisplayed = FALSE; + ConfigIdentifiers[i].IdentifierDescription_MediumDisplayed = FALSE; + ConfigIdentifiers[i].LocalVariableDescriptionDisplayed = FALSE; + } +} + +void +PrintHelpConfig(void) +{ + ResetConfigIdentifierDescriptionDisplayedBools(); + + // Config Syntax + int IndentationLevel = 0; + NewSection("Configuration", &IndentationLevel); + NewSection("Assigning values", &IndentationLevel); + + PrintC(CS_YELLOW_BOLD, "identifier"); + fprintf(stderr, " = "); + PrintC(CS_GREEN_BOLD, "\"string\""); + fprintf(stderr, ";"); + IndentedCarriageReturn(IndentationLevel); + + PrintC(CS_YELLOW_BOLD, "identifier"); + fprintf(stderr, " = "); + PrintC(CS_BLUE_BOLD, "number"); + fprintf(stderr, ";"); + IndentedCarriageReturn(IndentationLevel); + + PrintC(CS_YELLOW_BOLD, "identifier"); + fprintf(stderr, " = "); + PrintC(CS_GREEN_BOLD, "\"boolean\""); + fprintf(stderr, ";"); + ++IndentationLevel; + IndentedCarriageReturn(IndentationLevel); + + TypesetString(INDENT_WIDTH * IndentationLevel, Wrap0("(valid booleans: true, True, TRUE, yes, false, False, FALSE, no)")); + --IndentationLevel; + IndentedCarriageReturn(IndentationLevel); + + PrintC(CS_YELLOW_BOLD, "identifier"); + fprintf(stderr, " = "); + PrintC(CS_GREEN_BOLD, "\"scope\""); + fprintf(stderr, " {"); + IndentedCarriageReturn(IndentationLevel); + + fprintf(stderr, "}\n"); + EndSection(&IndentationLevel); + + + NewSection("Identifiers", &IndentationLevel); + TypesetString(INDENT_WIDTH * IndentationLevel, Wrap0("We may set identifiers singly or multiple times in a given scope, as specified below for each identifier. \ +If we set a \"single\" identifier multiple times then each setting overwrites the previous one, and Cinera will warn us. If we set a \"multi\" identifier multiple times \ +then all of the settings will take effect.")); + NewParagraph(IndentationLevel); + TypesetString(INDENT_WIDTH * IndentationLevel, Wrap0("All settings get \"absorbed\" by child scopes, if that child scope may contain the identifier. For example, if we \ +set base_dir at the root scope (i.e. not within a project scope) then the base_dir will automatically be set in all project scopes. Similarly if we set the owner in a project \ +scope, then the owner will also be set in all child project scopes. Naturally we may need this setting to vary, while also wanting the concision of writing it once. The \ +base_dir is a prime example of this. To facilitate this variance, we may use variables, notably the $lineage variable, as described below. A variable written in a setting at the root \ +scope, which is absorbed by a project, only gets resolved as if it had been written in the project scope.")); + config_type_specs TypeSpecs = InitTypeSpecs(); + PrintTypeSpecs(&TypeSpecs, IndentationLevel); + //FreeTypeSpecs(&TypeSpecs); + EndSection(&IndentationLevel); + + fprintf(stderr, "\n"); + NewSection("Variables", &IndentationLevel); + + TypesetString(INDENT_WIDTH * IndentationLevel, Wrap0("We write variables the same way as in shell scripts like bash, zsh, etc.")); + ++IndentationLevel; + IndentedCarriageReturn(IndentationLevel); + + PrintC(CS_CYAN, "$variable_name"); + IndentedCarriageReturn(IndentationLevel); + PrintC(CS_CYAN, "${variable_name_within_valid_variable_characters}"); + --IndentationLevel; + IndentedCarriageReturn(IndentationLevel); + + NewSection("Config local variables", &IndentationLevel); + + for(int i = 0; i < IDENT_COUNT; ++i) + { + if(ConfigIdentifiers[i].LocalVariableDescription && !ConfigIdentifiers[i].LocalVariableDescriptionDisplayed) + { + IndentedCarriageReturn(IndentationLevel); + PrintStringC(CS_YELLOW_BOLD, Wrap0(ConfigIdentifiers[i].String)); + ++IndentationLevel; + IndentedCarriageReturn(IndentationLevel); + TypesetString(INDENT_WIDTH * IndentationLevel, Wrap0(ConfigIdentifiers[i].LocalVariableDescription)); + ConfigIdentifiers[i].LocalVariableDescriptionDisplayed = TRUE; + --IndentationLevel; + } + } + EndSection(&IndentationLevel); + + fprintf(stderr, "\n"); + NewSection("Environment variables", &IndentationLevel); + TypesetString(INDENT_WIDTH *IndentationLevel, Wrap0("Run `export` to see all available environment variables")); + EndSection(&IndentationLevel); + + EndSection(&IndentationLevel); // Variables + + fprintf(stderr, "\n"); + NewSection("Miscellaneous", &IndentationLevel); + TypesetString(INDENT_WIDTH * IndentationLevel, Wrap0("We may use C-style comments, single-line with // and multi-line with /* and */")); + NewParagraph(IndentationLevel); + TypesetString(INDENT_WIDTH * IndentationLevel, Wrap0("We may edit the config file(s) while Cinera is running, and it will pick up the changes.")); + NewParagraph(IndentationLevel); + TypesetString(INDENT_WIDTH * IndentationLevel, Wrap0("Tip: Since the syntax is very C-like, vim users may put the following line somewhere to \ +enable syntax highlighting:")); + + NewSection(0, &IndentationLevel); + TypesetString(INDENT_WIDTH * IndentationLevel, Wrap0("// vim:ft=c:")); + EndSection(&IndentationLevel); + + EndSection(&IndentationLevel); + + fprintf(stderr, "\n"); +#if 1 + // numbering_scheme + // linear + // 1, 2, 3, ... + // calendrical + // 2020-02-07, 2020-03-08, 2020-12-25, ... + // seasonal + // S01E01 + + // Miscellaneous + // + // Defaults + // + NewSection("Defaults", &IndentationLevel); + scope_tree *ScopeTree = calloc(1, sizeof(scope_tree)); + SetTypeSpec(ScopeTree, &TypeSpecs); + SetDefaults(ScopeTree, &TypeSpecs); + PrintScopeTree(ScopeTree, IndentationLevel); + FreeScopeTree(ScopeTree); + FreeTypeSpecs(&TypeSpecs); +#endif + fprintf(stderr, "\n"); +} + +void +PrintHelp(char *BinaryLocation) +{ + // Options + fprintf(stderr, + "Usage: %s [option(s)]\n" + "\n" + "Options:\n" + " -c \n" + " Set the main config file path.\n" + " -0\n" + " Dry-run mode. Parse and print the config, but do not modify the filesystem.\n" + " -e\n" + " Display (examine) database and exit\n" + " -v\n" + " Display version and exit\n" + " -h\n" + " Display this help\n", BinaryLocation); + + PrintHelpConfig(); +} + +void +PrintHelp_(char *BinaryLocation) +{ +#if AFE fprintf(stderr, "Usage: %s [option(s)] filename(s)\n" "\n" @@ -3587,7 +7237,7 @@ PrintUsage(char *BinaryLocation, config *DefaultConfig) " https://git.handmade.network/Annotation-Pushers/Annotation-System/wikis/hmmlspec\n", ColourStrings[CS_END], - DefaultConfig->ProjectDir, + DefaultConfig->HMMLDir, DefaultConfig->TemplatesDir, DefaultConfig->BaseDir, @@ -3609,6 +7259,7 @@ PrintUsage(char *BinaryLocation, config *DefaultConfig) ColourStrings[CS_COMMENT], ColourStrings[CS_END], ColourStrings[CS_COMMENT], ColourStrings[CS_END], ColourStrings[CS_COMMENT], ColourStrings[CS_END]); +#endif } void @@ -3658,94 +7309,29 @@ StripSurroundingSlashes(char *String) // NOTE(matt): For relative paths return Ptr; } -void -ConsumeWhitespace(buffer *Buffer, char **Ptr) +bool +IsWhitespace(char C) { - while(*Ptr - Buffer->Location < Buffer->Size) - { - switch(**Ptr) - { - case ' ': - case '\t': - case '\n': - ++*Ptr; break; - default: return; - } - } -} - -int -ParseQuotedString(buffer *Dest, buffer *Src, char **Ptr, enum8(template_tag_codes) TagIndex) -{ - char *End; - bool MissingPath = FALSE; - bool MissingClosingQuote = FALSE; - if(**Ptr == '\"') - { - ++*Ptr; - End = *Ptr; - while(End - Src->Location < Src->Size && End - *Ptr < Dest->Size) - { - switch(*End) - { - case '-': - { - if((End + 2) - Src->Location < Src->Size && End[1] == '-' && End[2] == '>') - { - MissingClosingQuote = TRUE; - goto Finalise; - } - } - case '\"': goto Finalise; - case '\\': ++End; - default: *Dest->Ptr++ = *End++; break; - } - } - } - else - { - End = *Ptr; - if((End + 3) - Src->Location < Src->Size && End[0] == '-' && End[1] == '-' && End[2] == '>') - { - MissingPath = TRUE; - goto Finalise; - } - while(End - Src->Location < Src->Size && *End != ' ' && Dest->Ptr - Dest->Location < Dest->Size) - { - *Dest->Ptr++ = *End++; - } - } - *Dest->Ptr = '\0'; - -Finalise: - if(MissingClosingQuote) - { - printf("%sQuoted string%s seems to be missing its closing quote mark:\n" - " %.*s\n", ColourStrings[CS_ERROR], ColourStrings[CS_END], (int)(End - *Ptr), *Ptr); - return RC_ERROR_PARSING; - } - - if(MissingPath || StringLength(Dest->Location) == 0) - { - printf("%s%s tag%s seems to be missing a file path\n" - " %.*s\n", ColourStrings[CS_ERROR], TemplateTags[TagIndex], ColourStrings[CS_END], (int)(End - *Ptr), *Ptr); - return RC_ERROR_PARSING; - } - - if(End - *Ptr == Dest->Size) - { - printf("%sAsset file path%s is too long (we support paths up to %d characters):\n" - " %.*s...\n", ColourStrings[CS_ERROR], ColourStrings[CS_END], MAX_ASSET_FILENAME_LENGTH, (int)(End - *Ptr), *Ptr); - return RC_ERROR_PARSING; - } - return RC_SUCCESS; + return (C == ' ' || C == '\t' || C == '\n'); } void -StripPWDIndicators(char *Path) +ConsumeWhitespace(buffer *B) { - buffer B; - ClaimBuffer(&B, "PWDStrippedPtr", (StringLength(Path) + 1) * 2); + while(B->Ptr - B->Location < B->Size && IsWhitespace(*B->Ptr)) + { + ++B->Ptr; + } +} + +string +StripPWDIndicators(string Path) +{ + // NOTE(matt): Must modify the string content, so performs MakeString() and returns an allocated string + // + // Please call FreeString() afterwards + buffer B = {}; + ClaimBuffer(&B, BID_PWD_STRIPPED_PATH, (Path.Length + 1) * 2); CopyStringToBufferNoFormat(&B, Path); B.Ptr = B.Location; @@ -3753,7 +7339,7 @@ StripPWDIndicators(char *Path) { char *NextComponentHead = B.Ptr + 2; int RemainingChars = StringLength(NextComponentHead); - CopyStringToBufferNoFormat(&B, NextComponentHead); + CopyStringToBufferNoFormat(&B, Wrap0(NextComponentHead)); Clear(B.Ptr, B.Size - (B.Ptr - B.Location)); B.Ptr -= RemainingChars; } @@ -3764,46 +7350,213 @@ StripPWDIndicators(char *Path) int RemainingChars = StringLength(NextComponentHead); --B.Ptr; SeekBufferForString(&B, "/", C_SEEK_BACKWARDS, C_SEEK_START); - CopyStringToBufferNoFormat(&B, NextComponentHead); + CopyStringToBufferNoFormat(&B, Wrap0(NextComponentHead)); Clear(B.Ptr, B.Size - (B.Ptr - B.Location)); B.Ptr -= RemainingChars; } - CopyStringNoFormat(Path, StringLength(Path) + 1, B.Location); + string BufferedResult = Wrap0(B.Location); + string Result = MakeString("l", &BufferedResult); DeclaimBuffer(&B); + return Result; } -int -ParseAssetString(template *Template, enum8(template_tag_codes) TagIndex, uint32_t *AssetIndexPtr) +string +ParseString(buffer *B, rc *ReturnCode) { - char *Ptr = Template->File.Buffer.Ptr + StringLength(TemplateTags[TagIndex]); - ConsumeWhitespace(&Template->File.Buffer, &Ptr); - buffer AssetString; - ClaimBuffer(&AssetString, "AssetString", MAX_ASSET_FILENAME_LENGTH); - if(ParseQuotedString(&AssetString, &Template->File.Buffer, &Ptr, TagIndex) == RC_ERROR_PARSING) + *ReturnCode = RC_SUCCESS; + char *Ptr = B->Ptr; + bool Quoted = FALSE; + string Result = {}; + if(*Ptr == '\"') { - DeclaimBuffer(&AssetString); - return RC_ERROR_PARSING; - } - int AssetType = 0; - switch(TagIndex) - { - case TAG_ASSET: AssetType = ASSET_GENERIC; break; - case TAG_CSS: AssetType = ASSET_CSS; break; - case TAG_IMAGE: AssetType = ASSET_IMG; break; - case TAG_JS: AssetType = ASSET_JS; break; + ++Ptr; + Quoted = TRUE; } - AssetString.Ptr = StripSurroundingSlashes(AssetString.Location); - StripPWDIndicators(AssetString.Ptr); + Result.Base = Ptr; - PushAsset(AssetString.Ptr, AssetType, AssetIndexPtr); - DeclaimBuffer(&AssetString); - return RC_SUCCESS; + while(Ptr - B->Location < B->Size && (Quoted ? *Ptr != '\"' : !IsWhitespace(*Ptr))) + { + ++Ptr; + ++Result.Length; + } + + if(Quoted && Ptr - B->Location == B->Size) + { + *ReturnCode = RC_ERROR_PARSING_UNCLOSED_QUOTED_STRING; + } + + return Result; +} + +bool +AtHTMLCommentCloser(buffer *B) +{ + bool Result = FALSE; + if((B->Ptr + 3) - B->Location < B->Size && B->Ptr[0] == '-' && B->Ptr[1] == '-' && B->Ptr[2] == '>') + { + Result = TRUE; + } + return Result; +} + +asset * +ParseAssetString(template *Template, enum8(template_tag_codes) TagIndex, rc *ReturnCode) +{ + asset *Result = 0; + buffer *B = &Template->File.Buffer; + B->Ptr += StringLength(TemplateTags[TagIndex]); + ConsumeWhitespace(B); + if(AtHTMLCommentCloser(B)) + { + *ReturnCode = RC_ERROR_MISSING_PARAMETER; + fprintf(stderr, "\n" + "┌─ "); + PrintC(CS_ERROR, "Template error"); + fprintf(stderr, " in "); + PrintC(CS_CYAN, Template->File.Path); + fprintf(stderr, "\n" + "└─╼ "); + fprintf(stderr, "%s is missing a file path\n", TemplateTags[TagIndex]); + } + else + { + string AssetString = ParseString(B, ReturnCode); + if(*ReturnCode == RC_SUCCESS) + { + if(AssetString.Length > MAX_ASSET_FILENAME_LENGTH) + { + fprintf(stderr, "\n" + "┌─ "); + PrintC(CS_ERROR, "Template error"); + fprintf(stderr, " in "); + PrintC(CS_CYAN, Template->File.Path); + fprintf(stderr, "\n" + "└─╼ "); + fprintf(stderr, "Asset file path is too long (we support paths up to %d characters):\n" + " ", MAX_ASSET_FILENAME_LENGTH); + PrintStringC(CS_MAGENTA_BOLD, AssetString); + fprintf(stderr, "\n"); + *ReturnCode = RC_ERROR_CAPACITY; + } + else + { + int AssetType = 0; + switch(TagIndex) + { + case TAG_ASSET: AssetType = ASSET_GENERIC; break; + case TAG_CSS: AssetType = ASSET_CSS; break; + case TAG_IMAGE: AssetType = ASSET_IMG; break; + case TAG_JS: AssetType = ASSET_JS; break; + } + + string FinalAssetString = StripPWDIndicators(StripSlashes(AssetString, P_REL)); + + if(!(Result = GetAsset(FinalAssetString, AssetType))) + { + // TODO(matt): If we add a __CINERA_SPRITE__ template tag, we must pass the actual Variants here + + // NOTE(matt): Associating this basically prepares us for when we're storing templates in the db + Result = PushAsset(FinalAssetString, AssetType, CAV_DEFAULT_UNSET, TRUE); + } + FreeString(&FinalAssetString); + } + } + else + { + switch(*ReturnCode) + { + case RC_ERROR_PARSING_UNCLOSED_QUOTED_STRING: + { + fprintf(stderr, "\n" + "┌─ "); + PrintC(CS_ERROR, "Template error"); + fprintf(stderr, " in "); + PrintC(CS_CYAN, Template->File.Path); + fprintf(stderr, "\n" + "└─╼ "); + fprintf(stderr, "Unclosed asset quoted string: \""); + string UnclosedString = { .Base = AssetString.Base, .Length = MIN(16, AssetString.Length) }; + PrintStringC(CS_MAGENTA_BOLD, UnclosedString); + fprintf(stderr, "…\"\n"); + } break; + default: break; + } + } + } + + return Result; +} + +navigation_type +ParseNavigationTag(template *Template, template_tag_code TagIndex) +{ + navigation_type Result = NT_NULL; + buffer *B = &Template->File.Buffer; + B->Ptr += StringLength(TemplateTags[TagIndex]); + ConsumeWhitespace(B); + if(AtHTMLCommentCloser(B)) + { + Result = NT_PLAIN; + } + else + { + rc ReturnCode = RC_SUCCESS; + string NavigationString = ParseString(B, &ReturnCode); + if(ReturnCode == RC_SUCCESS) + { + for(int NavigationType = 1; NavigationType < NT_COUNT; ++NavigationType) + { + if(!StringsDifferLv0(NavigationString, NavigationTypes[NavigationType])) + { + Result = NavigationType; + break; + } + } + + if(Result == NT_NULL) + { + fprintf(stderr, "\n" + "┌─ "); + PrintC(CS_ERROR, "Template error"); + fprintf(stderr, " in "); + PrintC(CS_CYAN, Template->File.Path); + fprintf(stderr, "\n" + "└─╼ "); + fprintf(stderr, "Invalid navigation type \""); + PrintStringC(CS_MAGENTA_BOLD, NavigationString); + fprintf(stderr, "\"\n"); + } + } + else + { + switch(ReturnCode) + { + case RC_ERROR_PARSING_UNCLOSED_QUOTED_STRING: + { + fprintf(stderr, "\n" + "┌─ "); + PrintC(CS_ERROR, "Template error"); + fprintf(stderr, " in "); + PrintC(CS_CYAN, Template->File.Path); + fprintf(stderr, "\n" + "└─╼ "); + fprintf(stderr, "Unclosed navigation type quoted string: \""); + string UnclosedString = { .Base = NavigationString.Base, .Length = MIN(16, NavigationString.Length) }; + PrintStringC(CS_MAGENTA_BOLD, UnclosedString); + fprintf(stderr, "…\"\n"); + } break; + default: break; + } + } + } + + return Result; } int -PackTemplate(template *Template, char *Location, enum8(template_types) Type) +PackTemplate(template *Template, string Location, template_type Type) { // TODO(matt): Record line numbers and contextual information: // @@ -3814,15 +7567,15 @@ PackTemplate(template *Template, char *Location, enum8(template_types) Type) InitTemplate(Template, Location, Type); buffer Errors; - if(ClaimBuffer(&Errors, "Errors", Kilobytes(1)) == RC_ARENA_FULL) { FreeTemplate(Template); return RC_ARENA_FULL; }; + if(ClaimBuffer(&Errors, BID_ERRORS, Kilobytes(1)) == RC_ARENA_FULL) { FreeTemplate(Template); return RC_ARENA_FULL; }; bool HaveErrors = FALSE; bool HaveAssetParsingErrors = FALSE; + bool HaveNavParsingErrors = FALSE; + bool HaveGlobalValidityErrors = FALSE; bool FoundIncludes = FALSE; - bool FoundMenus = FALSE; bool FoundPlayer = FALSE; - bool FoundScript = FALSE; bool FoundSearch = FALSE; char *Previous = Template->File.Buffer.Location; @@ -3852,66 +7605,82 @@ NextTagSearch: * */ - uint32_t AssetIndex = 0; + asset *Asset = 0; + navigation_type NavigationType = NT_NULL; switch(TagIndex) { case TAG_SEARCH: FoundSearch = TRUE; goto RecordTag; +#if AFD case TAG_INCLUDES: - if(!(Config.Mode & MODE_FORCEINTEGRATION) && FoundIncludes == TRUE) + if( +#if 0 + !(CurrentProject->Mode & MODE_FORCEINTEGRATION) && +#endif + FoundIncludes == TRUE) { CopyStringToBuffer(&Errors, "Template contains more than one tag\n", TemplateTags[TagIndex]); HaveErrors = TRUE; } FoundIncludes = TRUE; goto RecordTag; - case TAG_MENUS: - if(!(Config.Mode & MODE_FORCEINTEGRATION) && FoundMenus == TRUE) - { - CopyStringToBuffer(&Errors, "Template contains more than one tag\n", TemplateTags[TagIndex]); - HaveErrors = TRUE; - } - FoundMenus = TRUE; - goto RecordTag; case TAG_PLAYER: - if(!(Config.Mode & MODE_FORCEINTEGRATION) && FoundPlayer == TRUE) + if( +#if 0 + !(CurrentProject->Mode & MODE_FORCEINTEGRATION) && +#endif + FoundPlayer == TRUE) { CopyStringToBuffer(&Errors, "Template contains more than one tag\n", TemplateTags[TagIndex]); HaveErrors = TRUE; } FoundPlayer = TRUE; goto RecordTag; - case TAG_SCRIPT: - if(!(Config.Mode & MODE_FORCEINTEGRATION) && (FoundMenus == FALSE || FoundPlayer == FALSE)) - { - CopyStringToBuffer(&Errors, " must come after and \n", TemplateTags[TagIndex]); - HaveErrors = TRUE; - } - if(!(Config.Mode & MODE_FORCEINTEGRATION) && FoundScript == TRUE) - { - CopyStringToBuffer(&Errors, "Template contains more than one tag\n", TemplateTags[TagIndex]); - HaveErrors = TRUE; - } - FoundScript = TRUE; - goto RecordTag; +#endif case TAG_ASSET: case TAG_CSS: case TAG_IMAGE: case TAG_JS: { - if(ParseAssetString(Template, TagIndex, &AssetIndex) == RC_ERROR_PARSING) + rc ReturnCode; + Asset = ParseAssetString(Template, TagIndex, &ReturnCode); + if(ReturnCode != RC_SUCCESS) { HaveErrors = TRUE; HaveAssetParsingErrors = TRUE; } } goto RecordTag; + case TAG_NAV: + { + NavigationType = ParseNavigationTag(Template, TagIndex); + if(NavigationType == NT_NULL) + { + HaveErrors = TRUE; + HaveNavParsingErrors = TRUE; + } + else if(NavigationType == NT_DROPDOWN) + { + Template->Metadata.RequiresCineraJS = TRUE; + } + } goto RecordTag; + case TAG_PROJECT: + case TAG_PROJECT_ID: + case TAG_PROJECT_PLAIN: + { + if(Type == TEMPLATE_GLOBAL_SEARCH) + { + CopyStringToBuffer(&Errors, "Global search template may not contain tags\n", TemplateTags[TagIndex]); + HaveErrors = TRUE; + HaveGlobalValidityErrors = TRUE; + } + } // NOTE(matt): Intentional fall-through, for project templates default: // NOTE(matt): All freely usable tags should hit this case RecordTag: { int Offset = CommentStart - Previous; - PushTemplateTag(Template, Offset, TagIndex, AssetIndex); + PushTemplateTag(Template, Offset, TagIndex, Asset, NavigationType); DepartComment(&Template->File.Buffer); Previous = Template->File.Buffer.Ptr; goto NextTagSearch; @@ -3928,7 +7697,7 @@ RecordTag: } } - if(HaveAssetParsingErrors) + if(HaveAssetParsingErrors || HaveNavParsingErrors) { DeclaimBuffer(&Errors); FreeTemplate(Template); @@ -3940,97 +7709,123 @@ RecordTag: Template->Metadata.Validity |= PAGE_SEARCH; } - if(!HaveErrors && FoundIncludes && FoundMenus && FoundPlayer && FoundScript) + if(!HaveErrors && FoundIncludes && FoundPlayer) { Template->Metadata.Validity |= PAGE_PLAYER; } - if(!(Config.Mode & MODE_FORCEINTEGRATION)) +#if 0 + if(!(CurrentProject->Mode & MODE_FORCEINTEGRATION)) { - if(Type == TEMPLATE_SEARCH && !(Template->Metadata.Validity & PAGE_SEARCH)) +#endif + if((Type == TEMPLATE_GLOBAL_SEARCH || Type == TEMPLATE_SEARCH) && !(Template->Metadata.Validity & PAGE_SEARCH)) { + Colourise(CS_ERROR); if(!FoundIncludes) { CopyStringToBuffer(&Errors, "%sSearch template%s must include one tag\n", ColourStrings[CS_ERROR], ColourStrings[CS_END]); }; if(!FoundSearch) { CopyStringToBuffer(&Errors, "%sSearch template%s must include one tag\n", ColourStrings[CS_ERROR], ColourStrings[CS_END]); }; fprintf(stderr, "%s", Errors.Location); DeclaimBuffer(&Errors); FreeTemplate(Template); + Colourise(CS_END); + WaitForInput(); return RC_INVALID_TEMPLATE; } else if((Type == TEMPLATE_PLAYER || Type == TEMPLATE_BESPOKE) && !(Template->Metadata.Validity & PAGE_PLAYER)) { + Colourise(CS_ERROR); if(!FoundIncludes){ CopyStringToBuffer(&Errors, "%s%slayer template%s must include one tag\n", ColourStrings[CS_ERROR], Type == TEMPLATE_BESPOKE ? "Bespoke p" : "P", ColourStrings[CS_END]); }; - if(!FoundMenus){ CopyStringToBuffer(&Errors, "%s%slayer template%s must include one tag\n", ColourStrings[CS_ERROR], Type == TEMPLATE_BESPOKE ? "Bespoke p" : "P", ColourStrings[CS_END]); }; if(!FoundPlayer){ CopyStringToBuffer(&Errors, "%s%slayer template%s must include one tag\n", ColourStrings[CS_ERROR], Type == TEMPLATE_BESPOKE ? "Bespoke p" : "P", ColourStrings[CS_END]); }; - if(!FoundScript){ CopyStringToBuffer(&Errors, "%s%slayer template%s must include one tag\n", ColourStrings[CS_ERROR], Type == TEMPLATE_BESPOKE ? "Bespoke p" : "P", ColourStrings[CS_END]); }; fprintf(stderr, "%s", Errors.Location); DeclaimBuffer(&Errors); FreeTemplate(Template); + Colourise(CS_END); + WaitForInput(); return RC_INVALID_TEMPLATE; } + else if(Type == TEMPLATE_GLOBAL_SEARCH && HaveGlobalValidityErrors) + { + Colourise(CS_ERROR); + fprintf(stderr, "%s", Errors.Location); + DeclaimBuffer(&Errors); + FreeTemplate(Template); + Colourise(CS_END); + WaitForInput(); + return RC_INVALID_TEMPLATE; + } +#if 0 } +#endif DeclaimBuffer(&Errors); return RC_SUCCESS; } void -ConstructSearchURL(buffer *SearchURL) +ConstructSearchURL(buffer *SearchURL, project *P) { RewindBuffer(SearchURL); - if(StringsDiffer(Config.BaseURL, "")) + if(P->BaseURL.Length > 0) { - CopyStringToBuffer(SearchURL, "%s/", Config.BaseURL); - if(StringsDiffer(Config.SearchLocation, "")) + CopyStringToBuffer(SearchURL, "%.*s/", (int)P->BaseURL.Length, P->BaseURL.Base); + if(P->SearchLocation.Length > 0) { - CopyStringToBuffer(SearchURL, "%s/", Config.SearchLocation); + CopyStringToBuffer(SearchURL, "%.*s/", (int)P->SearchLocation.Length, P->SearchLocation.Base); } } } void -ConstructPlayerURL(buffer *PlayerURL, char *BaseFilename) +ConstructPlayerURL(buffer *PlayerURL, db_header_project *P, string EntryOutput) { RewindBuffer(PlayerURL); - if(StringsDiffer(Config.BaseURL, "")) + string BaseURL = Wrap0i(P->BaseURL, sizeof(P->BaseURL)); + string PlayerLocation = Wrap0i(P->PlayerLocation, sizeof(P->PlayerLocation)); + if(BaseURL.Length > 0) { - CopyStringToBuffer(PlayerURL, "%s/", Config.BaseURL); - if(StringsDiffer(Config.PlayerLocation, "")) + CopyStringToBuffer(PlayerURL, "%.*s/", (int)BaseURL.Length, BaseURL.Base); + if(PlayerLocation.Length > 0) { - CopyStringToBuffer(PlayerURL, "%s/", Config.PlayerLocation); - } - } - if(StringsDiffer(BaseFilename, "")) - { - if(StringsDiffer(Config.PlayerURLPrefix, "")) - { - char *Ptr = BaseFilename + StringLength(Config.ProjectID); - CopyStringToBuffer(PlayerURL, "%s%s/", Config.PlayerURLPrefix, Ptr); - } - else - { - CopyStringToBuffer(PlayerURL, "%s/", BaseFilename); + CopyStringToBuffer(PlayerURL, "%.*s/", (int)PlayerLocation.Length, PlayerLocation.Base); } } + + CopyStringToBuffer(PlayerURL, "%.*s/", (int)EntryOutput.Length, EntryOutput.Base); } -bool -MediumExists(char *Medium) +medium * +MediumExists(string Medium) { - for(int i = 0; i < ArrayCount(CategoryMedium); ++i) + medium *ParsedMedium = GetMediumFromProject(CurrentProject, Medium); + if(ParsedMedium) { - if(!StringsDiffer(Medium, CategoryMedium[i].Medium)) - { - return TRUE; - } + return ParsedMedium; } - fprintf(stderr, "Specified default medium \"%s\" not available. Valid media are:\n", Medium); - for(int i = 0; i < ArrayCount(CategoryMedium); ++i) + fprintf(stderr, "Specified default medium \"%.*s\" not available. Valid media are:\n", (int)Medium.Length, Medium.Base); + for(int i = 0; i < CurrentProject->MediumCount; ++i) { - fprintf(stderr, " %s\n", CategoryMedium[i].Medium); + typography Typography = + { + .UpperLeftCorner = "┌", + .UpperLeft = "╾", + .Horizontal = "─", + .UpperRight = "╼", + .Vertical = "", + .LowerLeftCorner = "└", + .LowerLeft = "╽", + .Margin = "", + .Delimiter = ": ", + .Separator = "•", + }; + PrintMedium(&Typography, &CurrentProject->Medium[i], ": ", 2, TRUE); } - fprintf(stderr, "To have \"%s\" added to the list, contact miblodelcarpio@gmail.com\n", Medium); - return FALSE; + fprintf(stderr, "Perhaps you'd like to add a new medium to your config file, e.g.:\n" + " medium = \"%.*s\"\n" + " {\n" + " name = \"Name of Medium\";\n" + " icon = \"🧪\";\n" + " }\n", (int)Medium.Length, Medium.Base); + return 0; } typedef struct @@ -4046,7 +7841,7 @@ typedef struct } neighbours; void -ExamineDB1(file_buffer File) +ExamineDB1(file File) { database1 LocalDB; LocalDB.Header = *(db_header1 *)File.Buffer.Location; @@ -4076,7 +7871,7 @@ ExamineDB1(file_buffer File) } void -ExamineDB2(file_buffer File) +ExamineDB2(file File) { database2 LocalDB; LocalDB.Header = *(db_header2 *)File.Buffer.Location; @@ -4112,7 +7907,7 @@ ExamineDB2(file_buffer File) } void -ExamineDB3(file_buffer File) +ExamineDB3(file File) { database3 LocalDB; LocalDB.Header = *(db_header3 *)File.Buffer.Location; @@ -4173,8 +7968,9 @@ ExamineDB3(file_buffer File) } void -ExamineDB4(file_buffer File) +ExamineDB4(file File) { +#if AFE database4 LocalDB; LocalDB.Header = *(db_header4 *)File.Buffer.Location; @@ -4210,8 +8006,8 @@ ExamineDB4(file_buffer File) LocalDB.Header.InitialAppVersion.Major, LocalDB.Header.InitialAppVersion.Minor, LocalDB.Header.InitialAppVersion.Patch, LocalDB.Header.InitialHMMLVersion.Major, LocalDB.Header.InitialHMMLVersion.Minor, LocalDB.Header.InitialHMMLVersion.Patch, - Config.BaseDir, Config.ProjectID, - Config.BaseDir, Config.ProjectID, + CurrentProject->BaseDir, CurrentProject->ID, + CurrentProject->BaseDir, CurrentProject->ID, LocalDB.EntriesHeader.ProjectID, LocalDB.EntriesHeader.ProjectName, @@ -4259,7 +8055,7 @@ ExamineDB4(file_buffer File) StringsDiffer(LocalDB.AssetsHeader.JSDir, "") ? LocalDB.AssetsHeader.JSDir : "(same as root)", LocalDB.AssetsHeader.Count, ColourStrings[CS_COMMENT], ColourStrings[CS_END], - ColourStrings[CS_PURPLE], ColourStrings[CS_END], + ColourStrings[CS_MAGENTA], ColourStrings[CS_END], ColourStrings[CS_COMMENT], ColourStrings[CS_END], ColourStrings[CS_COMMENT], ColourStrings[CS_END]); @@ -4281,79 +8077,283 @@ ExamineDB4(file_buffer File) { LocalDB.Landmark = *(db_landmark *)File.Buffer.Ptr; File.Buffer.Ptr += sizeof(LocalDB.Landmark); - printf(" %d•%s%d%s", LocalDB.Landmark.EntryIndex, ColourStrings[CS_PURPLE], LocalDB.Landmark.Position, ColourStrings[CS_END]); + printf(" %d•%s%d%s", LocalDB.Landmark.EntryIndex, ColourStrings[CS_MAGENTA], LocalDB.Landmark.Position, ColourStrings[CS_END]); } printf("\n"); } +#endif } -int -ExamineDB(void) +char * +GetAssetStringFromIndex(special_asset_index Index) { - file_buffer DBFile; - DBFile.Buffer.ID = "DBFile"; - CopyString(DBFile.Path, sizeof(DBFile.Path), "%s/%s.metadata", Config.BaseDir, Config.ProjectID); - int DBFileReadCode = ReadFileIntoBuffer(&DBFile, 0); - - switch(DBFileReadCode) + char *Result = 0; + switch(Index) { - case RC_ERROR_MEMORY: - return RC_ERROR_MEMORY; - case RC_ERROR_FILE: - fprintf(stderr, "Unable to open metadata file %s: %s\n", DBFile.Path, strerror(errno)); - return RC_ERROR_FILE; - case RC_SUCCESS: - break; + case SAI_TEXTUAL: + case SAI_UNSET: + { + Result = SpecialAssetIndexStrings[Index + 2]; + } break; + } + return Result; +} + +void +PrintAssetIndex(int32_t Index) +{ + Colourise(CS_BLUE_BOLD); + if(Index >= 0) + { + fprintf(stderr, "%i", Index); + } + else + { + fprintf(stderr, "%s", GetAssetStringFromIndex(Index)); + } + Colourise(CS_END); +} + +void * +PrintProjectAndChildren(db_header_project *P, typography T) +{ + string ID = Wrap0i(P->ID, sizeof(P->ID)); + string Title = Wrap0i(P->Title, sizeof(P->Title)); + string BaseDir = Wrap0i(P->BaseDir, sizeof(P->BaseDir)); + string BaseURL = Wrap0i(P->BaseURL, sizeof(P->BaseURL)); + string SearchLocation = Wrap0i(P->SearchLocation, sizeof(P->SearchLocation)); + string PlayerLocation = Wrap0i(P->PlayerLocation, sizeof(P->PlayerLocation)); + string Theme = Wrap0i(P->Theme, sizeof(P->Theme)); + string Unit = Wrap0i(P->Unit, sizeof(P->Unit)); + + fprintf(stderr, "\n"); + PrintC(CS_YELLOW_BOLD, "\nID"); fprintf(stderr, ": "); PrintStringC(CS_GREEN_BOLD, ID); + PrintC(CS_YELLOW_BOLD, "\nTitle"); fprintf(stderr, ": "); PrintStringC(CS_GREEN_BOLD, Title); + PrintC(CS_YELLOW_BOLD, "\nBaseDir"); fprintf(stderr, ": "); PrintStringC(CS_GREEN_BOLD, BaseDir); + PrintC(CS_YELLOW_BOLD, "\nBaseURL"); fprintf(stderr, ": "); PrintStringC(CS_GREEN_BOLD, BaseURL); + PrintC(CS_YELLOW_BOLD, "\nSearchLocation"); fprintf(stderr, ": "); PrintStringC(CS_GREEN_BOLD, SearchLocation); + PrintC(CS_YELLOW_BOLD, "\nPlayerLocation"); fprintf(stderr, ": "); PrintStringC(CS_GREEN_BOLD, PlayerLocation); + PrintC(CS_YELLOW_BOLD, "\nTheme"); fprintf(stderr, ": "); PrintStringC(CS_GREEN_BOLD, Theme); + PrintC(CS_YELLOW_BOLD, "\nArt asset index"); fprintf(stderr, ": "); PrintAssetIndex(P->ArtIndex); + PrintC(CS_YELLOW_BOLD, "\nIcon asset index"); fprintf(stderr, ": "); PrintAssetIndex(P->IconIndex); + PrintC(CS_YELLOW_BOLD, "\nUnit"); fprintf(stderr, ": "); PrintStringC(CS_GREEN_BOLD, Unit); + + char *Ptr = (char *)P; + Ptr += sizeof(db_header_project); + + PrintC(CS_YELLOW_BOLD, "\nEntries"); + fprintf(stderr, " ("); + Colourise(CS_BLUE_BOLD); + fprintf(stderr, "%ld", P->EntryCount); + Colourise(CS_END); + fprintf(stderr, "):"); + + for(int EntryIndex = 0; EntryIndex < P->EntryCount; ++EntryIndex) + { + db_entry *E = (db_entry *)Ptr; + + string EntryHMMLBaseFilename = Wrap0i(E->HMMLBaseFilename, sizeof(E->HMMLBaseFilename)); + string EntryOutputLocation = Wrap0i(E->OutputLocation, sizeof(E->OutputLocation)); + string EntryTitle = Wrap0i(E->Title, sizeof(E->Title)); + + fprintf(stderr, "\n" + "%s%s%s%s%s ", T.UpperLeftCorner, + T.Horizontal, T.Horizontal, T.Horizontal, + T.UpperRight); + + Colourise(CS_BLACK_BOLD); fprintf(stderr, "[%d] ", EntryIndex); Colourise(CS_END); + PrintStringC(CS_GREEN_BOLD, EntryHMMLBaseFilename); + fprintf(stderr, "\n%s%s", T.Vertical, T.Margin); PrintC(CS_YELLOW_BOLD, "OutputLocation"); fprintf(stderr, ": "); PrintStringC(CS_GREEN_BOLD, EntryOutputLocation); + fprintf(stderr, "\n%s%s", T.Vertical, T.Margin); PrintC(CS_YELLOW_BOLD, "Title"); fprintf(stderr, ": "); PrintStringC(CS_GREEN_BOLD, EntryTitle); + + fprintf(stderr, "\n%s%s", T.Vertical, T.Margin); PrintC(CS_YELLOW_BOLD, "Size"); fprintf(stderr, ": "); + Colourise(CS_BLUE_BOLD); + fprintf(stderr, "%d", E->Size); + Colourise(CS_END); + + fprintf(stderr, "\n%s%s", T.Vertical, T.Margin); PrintC(CS_YELLOW_BOLD, "Link offsets"); fprintf(stderr, ": "); + Colourise(CS_BLUE_BOLD); + fprintf(stderr, "%d\t%d\t%d\t%d", + E->LinkOffsets.PrevStart, + E->LinkOffsets.PrevEnd, + E->LinkOffsets.NextStart, + E->LinkOffsets.NextEnd); + Colourise(CS_END); + + fprintf(stderr, "\n%s%s", T.LowerLeft, T.Margin); PrintC(CS_YELLOW_BOLD, "Art asset index"); fprintf(stderr, ": "); PrintAssetIndex(E->ArtIndex); + + Ptr += sizeof(db_entry); } - uint32_t FirstInt = *(uint32_t *)DBFile.Buffer.Location; - if(FirstInt != FOURCC("CNRA")) + if(P->EntryCount > 0) { fprintf(stderr, "\n"); } + PrintC(CS_YELLOW_BOLD, "\nChildren"); + fprintf(stderr, " ("); + Colourise(CS_BLUE_BOLD); + fprintf(stderr, "%ld", P->ChildCount); + Colourise(CS_END); + fprintf(stderr, "):"); + + for(int ChildIndex = 0; ChildIndex < P->ChildCount; ++ChildIndex) { - switch(FirstInt) + db_header_project *Child = (db_header_project *)Ptr; + Ptr = PrintProjectAndChildren(Child, T); + } + + return Ptr; +} + +void * +PrintProjectsBlock(db_block_projects *B) +{ + if(!B) + { + B = LocateBlock(B_PROJ); + } + fprintf(stderr, "\n" + "\n"); + PrintC(CS_BLUE_BOLD, "Projects Block (PROJ)"); + + typography Typography = + { + .UpperLeftCorner = "┌", + .UpperLeft = "╾", + .Horizontal = "─", + .UpperRight = "╼", + .Vertical = "│", + .LowerLeftCorner = "└", + .LowerLeft = "╽", + .Margin = " ", + .Delimiter = ": ", + .Separator = "•", + }; + + char *Ptr = (char *)B; + Ptr += sizeof(db_block_projects); + db_header_project *P = (db_header_project *)Ptr; + for(int i = 0; i < B->Count; ++i) + { + P = PrintProjectAndChildren(P, Typography); + } + return P; +} + +void +ExamineDB5(file File) +{ + File.Buffer.Ptr = File.Buffer.Location; + + database5 LocalDB; + if(File.Buffer.Size >= sizeof(LocalDB.Header)) + { + LocalDB.Header = *(db_header5 *)File.Buffer.Ptr; + fprintf(stderr, "\n"); + PrintC(CS_BLUE_BOLD, "Versions"); + fprintf(stderr, + "\n" + "\n" + " Current:\n" + " Cinera: %d.%d.%d\n" + " Database: %d\n" + " hmmlib: %d.%d.%d\n" + "\n" + " Initial:\n" + " Cinera: %d.%d.%d\n" + " Database: %d\n" + " hmmlib: %d.%d.%d", + LocalDB.Header.CurrentAppVersion.Major, LocalDB.Header.CurrentAppVersion.Minor, LocalDB.Header.CurrentAppVersion.Patch, + LocalDB.Header.CurrentDBVersion, + LocalDB.Header.CurrentHMMLVersion.Major, LocalDB.Header.CurrentHMMLVersion.Minor, LocalDB.Header.CurrentHMMLVersion.Patch, + + LocalDB.Header.InitialAppVersion.Major, LocalDB.Header.InitialAppVersion.Minor, LocalDB.Header.InitialAppVersion.Patch, + LocalDB.Header.InitialDBVersion, + LocalDB.Header.InitialHMMLVersion.Major, LocalDB.Header.InitialHMMLVersion.Minor, LocalDB.Header.InitialHMMLVersion.Patch); + File.Buffer.Ptr += sizeof(db_header5); + for(int i = 0; i < LocalDB.Header.BlockCount; ++i) { - case 1: ExamineDB1(DBFile); break; - case 2: ExamineDB2(DBFile); break; - case 3: ExamineDB3(DBFile); break; - default: printf("Invalid metadata file: %s\n", DBFile.Path); break; + int Four = *(int *)File.Buffer.Ptr; + if(Four == FOURCC("ASET")) + { + db_block_assets *AssetsBlock = (db_block_assets *)File.Buffer.Ptr; + File.Buffer.Ptr = PrintAssetsBlock(AssetsBlock); + } + else if(Four == FOURCC("PROJ")) + { + db_block_projects *ProjectsBlock = (db_block_projects *)File.Buffer.Ptr; + File.Buffer.Ptr = PrintProjectsBlock(ProjectsBlock); + } + else + { + fprintf(stderr, "\n" + "Invalid database file: %s", File.Path); + break; + } + } + + fprintf(stderr, "\n"); + } +} + +void +ExamineDB(void) +{ + DB.Metadata.File.Buffer.ID = BID_DATABASE; + DB.Metadata.File = InitFile(0, &Config->DatabaseLocation, EXT_NULL); + ReadFileIntoBuffer(&DB.Metadata.File); // NOTE(matt): Could we actually catch errors (permissions?) here and bail? + + if(DB.Metadata.File.Buffer.Location) + { + uint32_t FirstInt = *(uint32_t *)DB.Metadata.File.Buffer.Location; + if(FirstInt != FOURCC("CNRA")) + { + switch(FirstInt) + { + case 1: ExamineDB1(DB.Metadata.File); break; + case 2: ExamineDB2(DB.Metadata.File); break; + case 3: ExamineDB3(DB.Metadata.File); break; + default: printf("Invalid database file: %s\n", DB.Metadata.File.Path); break; + } + } + else + { + uint32_t SecondInt = *(uint32_t *)(DB.Metadata.File.Buffer.Location + sizeof(uint32_t)); + switch(SecondInt) + { + case 4: ExamineDB4(DB.Metadata.File); break; + case 5: ExamineDB5(DB.Metadata.File); break; + default: printf("Invalid database file: %s\n", DB.Metadata.File.Path); break; + } } } else { - uint32_t SecondInt = *(uint32_t *)(DBFile.Buffer.Location + sizeof(uint32_t)); - switch(SecondInt) - { - case 4: ExamineDB4(DBFile); break; - default: printf("Invalid metadata file: %s\n", DBFile.Path); break; - } + fprintf(stderr, "Unable to open database file %s: %s\n", DB.Metadata.File.Path, strerror(errno)); } - FreeBuffer(&DBFile.Buffer); - return RC_SUCCESS; + FreeFile(&DB.Metadata.File); } #define HMMLCleanup() \ - DeclaimBuffer(&CreditsMenu); \ - DeclaimBuffer(&FilterMedia); \ - DeclaimBuffer(&FilterTopics); \ - DeclaimBuffer(&FilterMenu); \ - DeclaimBuffer(&ReferenceMenu); \ - DeclaimBuffer(&QuoteMenu); \ - hmml_free(&HMML); + DeclaimPlayerBuffers(&PlayerBuffers); \ + DeclaimIndexBuffers(&IndexBuffers); \ + DeclaimMenuBuffers(&MenuBuffers); \ + hmml_free(&HMML) bool VideoIsPrivate(char *VideoID) { // NOTE(matt): Currently only supports YouTube + // NOTE(matt): Stack-string char Message[128]; CopyString(Message, sizeof(Message), "%sChecking%s privacy status of: https://youtube.com/watch?v=%s", ColourStrings[CS_ONGOING], ColourStrings[CS_END], VideoID); fprintf(stderr, "%s", Message); int MessageLength = StringLength(Message); buffer VideoAPIResponse; - ClaimBuffer(&VideoAPIResponse, "VideoAPIResponse", Kilobytes(1)); + ClaimBuffer(&VideoAPIResponse, BID_VIDEO_API_RESPONSE, Kilobytes(1)); CURL *curl = curl_easy_init(); if(curl) { LastPrivacyCheck = time(0); #define APIKey "AIzaSyAdV2U8ivPk8PHMaPMId0gynksw_gdzr9k" + // NOTE(matt): Stack-string char URL[1024] = {0}; CopyString(URL, sizeof(URL), "https://www.googleapis.com/youtube/v3/videos?key=%s&part=status&id=%s", APIKey, VideoID); CURLcode CurlReturnCode; @@ -4367,6 +8367,7 @@ VideoIsPrivate(char *VideoID) curl_easy_cleanup(curl); VideoAPIResponse.Ptr = VideoAPIResponse.Location; + // TODO(matt): Parse this JSON SeekBufferForString(&VideoAPIResponse, "{", C_SEEK_FORWARDS, C_SEEK_AFTER); SeekBufferForString(&VideoAPIResponse, "\"totalResults\": ", C_SEEK_FORWARDS, C_SEEK_AFTER); if(*VideoAPIResponse.Ptr == '0') @@ -4378,9 +8379,10 @@ VideoIsPrivate(char *VideoID) } SeekBufferForString(&VideoAPIResponse, "{", C_SEEK_FORWARDS, C_SEEK_AFTER); SeekBufferForString(&VideoAPIResponse, "\"privacyStatus\": \"", C_SEEK_FORWARDS, C_SEEK_AFTER); + // NOTE(matt): Stack-string char Status[16]; CopyStringNoFormatT(Status, sizeof(Status), VideoAPIResponse.Ptr, '\"'); - if(!StringsDiffer(Status, "public")) + if(!StringsDiffer0(Status, "public")) { DeclaimBuffer(&VideoAPIResponse); ClearTerminalRow(MessageLength); @@ -4393,35 +8395,25 @@ VideoIsPrivate(char *VideoID) return TRUE; } -typedef struct -{ - db_entry Prev, This, Next; - uint32_t PreLinkPrevOffsetTotal, PreLinkThisOffsetTotal, PreLinkNextOffsetTotal; - uint32_t PrevOffsetModifier, ThisOffsetModifier, NextOffsetModifier; - bool FormerIsFirst, LatterIsFinal; - bool DeletedEntryWasFirst, DeletedEntryWasFinal; - short int PrevIndex, PreDeletionThisIndex, ThisIndex, NextIndex; -} neighbourhood; - -int -LinearSearchForSpeaker(speakers Speakers, char *Username) +speaker * +GetSpeaker(speakers Speakers, string Username) { for(int i = 0; i < Speakers.Count; ++i) { - if(!StringsDifferCaseInsensitive(Speakers.Speaker[i].Credential->Username, Username)) + if(!StringsDifferCaseInsensitive(Speakers.Speaker[i].Person->ID, Username)) { - return i; + return &Speakers.Speaker[i]; } } - return -1; + return 0; } bool -IsCategorisedAFK(HMML_Annotation Anno) +IsCategorisedAFK(HMML_Annotation *Anno) { - for(int i = 0; i < Anno.marker_count; ++i) + for(int i = 0; i < Anno->marker_count; ++i) { - if(!StringsDiffer(Anno.markers[i].marker, "afk")) + if(!StringsDiffer0(Anno->markers[i].marker, "afk")) { return TRUE; } @@ -4429,114 +8421,249 @@ IsCategorisedAFK(HMML_Annotation Anno) return FALSE; } -// NOTE(matt): Perhaps these OffsetLandmarks* could be made redundant / generalised once we're on the LUT -void -OffsetLandmarks(buffer *Src, int AssetIndex, int PageType) +bool +IsCategorisedAuthored(HMML_Annotation *Anno) { - if(!(Config.Mode & MODE_NOREVVEDRESOURCE)) + for(int i = 0; i < Anno->marker_count; ++i) { - if(PageType == PAGE_PLAYER) + if(!StringsDiffer0(Anno->markers[i].marker, "authored")) { - for(int LandmarkIndex = 0; LandmarkIndex < Assets.Asset[AssetIndex].PlayerLandmarkCount; ++LandmarkIndex) - { - Assets.Asset[AssetIndex].PlayerLandmark[LandmarkIndex] += Src->Ptr - Src->Location; - } + return TRUE; } - else + } + return FALSE; +} + +typedef struct +{ + buffer Quote; + buffer Reference; + buffer Filter; + buffer FilterTopics; + buffer FilterMedia; + buffer Credits; +} menu_buffers; + +void +DeclaimMenuBuffers(menu_buffers *B) +{ + DeclaimBuffer(&B->Credits); + DeclaimBuffer(&B->FilterMedia); + DeclaimBuffer(&B->FilterTopics); + DeclaimBuffer(&B->Filter); + DeclaimBuffer(&B->Reference); + DeclaimBuffer(&B->Quote); +} + +typedef struct +{ + buffer Master; + buffer Header; + buffer Class; + buffer Data; + buffer Text; + buffer CategoryIcons; +} index_buffers; + +void +DeclaimIndexBuffers(index_buffers *B) +{ + DeclaimBuffer(&B->CategoryIcons); + DeclaimBuffer(&B->Text); + DeclaimBuffer(&B->Data); + DeclaimBuffer(&B->Class); + DeclaimBuffer(&B->Header); + DeclaimBuffer(&B->Master); +} + +typedef struct +{ + buffer Menus; + buffer Main; + buffer Script; +} player_buffers; + +void +DeclaimPlayerBuffers(player_buffers *B) +{ + DeclaimBuffer(&B->Script); + DeclaimBuffer(&B->Main); + DeclaimBuffer(&B->Menus); +} + +char * +ConstructIndexFilePath(db_header_project *Project) +{ + string SearchLocation = Wrap0i(Project->SearchLocation, sizeof(Project->SearchLocation)); + char *Result = ConstructDirectoryPath(Project, &SearchLocation, 0); + ExtendString0(&Result, Wrap0("/")); + ExtendString0(&Result, Wrap0i(Project->ID, sizeof(Project->ID))); + ExtendString0(&Result, ExtensionStrings[EXT_INDEX]); + return Result; +} + +void +DeleteSearchPageFromFilesystem(db_header_project *Project) // NOTE(matt): Do we need to handle relocating, like the PlayerPage function? +{ + string SearchLocationL = Wrap0i(Project->SearchLocation, sizeof(Project->SearchLocation)); + char *SearchPagePath = ConstructHTMLIndexFilePath(Project, &SearchLocationL, 0); + remove(SearchPagePath); + Free(SearchPagePath); + + char *IndexFilePath = ConstructIndexFilePath(Project); + remove(IndexFilePath); + Free(IndexFilePath); + // TODO(matt): Consider the correctness of this + FreeFile(&DB.File); + + char *SearchDirectory = ConstructDirectoryPath(Project, &SearchLocationL, 0); + remove(SearchDirectory); + Free(SearchDirectory); +} + +void +DeleteGlobalSearchPageFromFilesystem(char *Location) +{ + string LocationL = Wrap0(Location); + char *SearchPagePath = ConstructHTMLIndexFilePath(0, &LocationL, 0); + remove(SearchPagePath); + Free(SearchPagePath); + + remove(Location); +} + +void +PrintLineageAndEntryID(string Lineage, string EntryID, bool AppendNewline) +{ + PrintLineage(Lineage, FALSE); + fprintf(stderr, "/");//%.*s - %s\n", + PrintStringC(CS_MAGENTA, EntryID); + if(AppendNewline) { fprintf(stderr, "\n"); } +} + +void +PrintLineageAndEntry(string Lineage, string EntryID, string EntryTitle, bool AppendNewline) +{ + PrintLineageAndEntryID(Lineage, EntryID, FALSE); + Colourise(CS_MAGENTA); + fprintf(stderr, " - "); + PrintString(EntryTitle); + Colourise(CS_END); + if(AppendNewline) { fprintf(stderr, "\n"); } +} + +rc +DeletePlayerPageFromFilesystem(db_header_project *P, string EntryOutput, bool Relocating, bool Echo) +{ + string PlayerLocation = Wrap0i(P->PlayerLocation, sizeof(P->PlayerLocation)); + char *OutputDirectoryPath = ConstructDirectoryPath(P, &PlayerLocation, &EntryOutput); + DIR *PlayerDir; + + if((PlayerDir = opendir(OutputDirectoryPath))) // There is a directory for the Player, which there probably should be if not for manual intervention + { + char *PlayerPagePath = MakeString0("ss", OutputDirectoryPath, "/index.html"); + FILE *PlayerPage; + if((PlayerPage = fopen(PlayerPagePath, "r"))) { - for(int LandmarkIndex = 0; LandmarkIndex < Assets.Asset[AssetIndex].SearchLandmarkCount; ++LandmarkIndex) + fclose(PlayerPage); + remove(PlayerPagePath); + } + Free(PlayerPagePath); + + closedir(PlayerDir); + int64_t RemovalSuccess = remove(OutputDirectoryPath); + if(Echo) + { + if(RemovalSuccess == -1) { - Assets.Asset[AssetIndex].SearchLandmark[LandmarkIndex] += Src->Ptr - Src->Location; + LogError(LOG_NOTICE, "Mostly deleted %.*s/%.*s. Unable to remove directory %s: %s", (int)CurrentProject->Lineage.Length, CurrentProject->Lineage.Base, (int)EntryOutput.Length, EntryOutput.Base, OutputDirectoryPath, strerror(errno)); + + fprintf(stderr, "%sMostly deleted%s ", ColourStrings[EditTypes[EDIT_DELETION].Colour], ColourStrings[CS_END]); + PrintLineageAndEntryID(CurrentProject->Lineage, EntryOutput, TRUE); + fprintf(stderr, " %sUnable to remove directory%s %s: %s\n", ColourStrings[CS_ERROR], ColourStrings[CS_END], OutputDirectoryPath, strerror(errno)); + } + else + { + if(!Relocating) + { + LogError(LOG_INFORMATIONAL, "Deleted %.*s/%.*s", (int)CurrentProject->Lineage.Length, CurrentProject->Lineage.Base, (int)EntryOutput.Length, EntryOutput.Base); + fprintf(stderr, "%s%s%s ", ColourStrings[EditTypes[EDIT_DELETION].Colour], EditTypes[EDIT_DELETION].Name, ColourStrings[CS_END]); + PrintLineageAndEntryID(CurrentProject->Lineage, EntryOutput, TRUE); + } + } } } + Free(OutputDirectoryPath); + return RC_SUCCESS; } -void -OffsetLandmarksIncludes(buffer *Src, int PageType) -{ - OffsetLandmarks(Src, ASSET_CSS_CINERA, PageType); - OffsetLandmarks(Src, ASSET_CSS_THEME, PageType); - OffsetLandmarks(Src, ASSET_CSS_TOPICS, PageType); - if(PageType == PAGE_PLAYER) - { - OffsetLandmarks(Src, ASSET_JS_PLAYER_PRE, PageType); - } -} - -void -OffsetLandmarksCredits(buffer *Src) -{ - OffsetLandmarks(Src, ICON_SENDOWL, PAGE_PLAYER); - OffsetLandmarks(Src, ICON_PATREON, PAGE_PLAYER); -} - -void -OffsetLandmarksMenus(buffer *Src) -{ - OffsetLandmarks(Src, ASSET_IMG_FILTER, PAGE_PLAYER); - OffsetLandmarksCredits(Src); -} -// - int -HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filename, neighbourhood *N) +HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, string BaseFilename, neighbourhood *N) { - RewindBuffer(&CollationBuffers->IncludesPlayer); - RewindBuffer(&CollationBuffers->Menus); - RewindBuffer(&CollationBuffers->Player); - RewindBuffer(&CollationBuffers->ScriptPlayer); - RewindBuffer(&CollationBuffers->SearchEntry); - *CollationBuffers->Custom0 = '\0'; - *CollationBuffers->Custom1 = '\0'; - *CollationBuffers->Custom2 = '\0'; - *CollationBuffers->Custom3 = '\0'; - *CollationBuffers->Custom4 = '\0'; - *CollationBuffers->Custom5 = '\0'; - *CollationBuffers->Custom6 = '\0'; - *CollationBuffers->Custom7 = '\0'; - *CollationBuffers->Custom8 = '\0'; - *CollationBuffers->Custom9 = '\0'; - *CollationBuffers->Custom10 = '\0'; - *CollationBuffers->Custom11 = '\0'; - *CollationBuffers->Custom12 = '\0'; - *CollationBuffers->Custom13 = '\0'; - *CollationBuffers->Custom14 = '\0'; - *CollationBuffers->Custom15 = '\0'; - *CollationBuffers->Title = '\0'; - *CollationBuffers->URLPlayer = '\0'; - *CollationBuffers->URLSearch = '\0'; - *CollationBuffers->VODPlatform = '\0'; + RewindCollationBuffers(CollationBuffers); + Clear(CollationBuffers->Custom0, sizeof(CollationBuffers->Custom0)); + Clear(CollationBuffers->Custom1, sizeof(CollationBuffers->Custom1)); + Clear(CollationBuffers->Custom2, sizeof(CollationBuffers->Custom2)); + Clear(CollationBuffers->Custom3, sizeof(CollationBuffers->Custom3)); + Clear(CollationBuffers->Custom4, sizeof(CollationBuffers->Custom4)); + Clear(CollationBuffers->Custom5, sizeof(CollationBuffers->Custom5)); + Clear(CollationBuffers->Custom6, sizeof(CollationBuffers->Custom6)); + Clear(CollationBuffers->Custom7, sizeof(CollationBuffers->Custom7)); + Clear(CollationBuffers->Custom8, sizeof(CollationBuffers->Custom8)); + Clear(CollationBuffers->Custom9, sizeof(CollationBuffers->Custom9)); + Clear(CollationBuffers->Custom10, sizeof(CollationBuffers->Custom10)); + Clear(CollationBuffers->Custom11, sizeof(CollationBuffers->Custom11)); + Clear(CollationBuffers->Custom12, sizeof(CollationBuffers->Custom12)); + Clear(CollationBuffers->Custom13, sizeof(CollationBuffers->Custom13)); + Clear(CollationBuffers->Custom14, sizeof(CollationBuffers->Custom14)); + Clear(CollationBuffers->Custom15, sizeof(CollationBuffers->Custom15)); + Clear(CollationBuffers->Title, sizeof(CollationBuffers->Title)); + Clear(CollationBuffers->URLPlayer, sizeof(CollationBuffers->URLPlayer)); + Clear(CollationBuffers->URLSearch, sizeof(CollationBuffers->URLSearch)); + Clear(CollationBuffers->VODPlatform, sizeof(CollationBuffers->VODPlatform)); - char Filepath[256]; - if(Config.Edition == EDITION_PROJECT) - { - CopyString(Filepath, sizeof(Filepath), "%s/%s", Config.ProjectDir, Filename); - } - else - { - CopyString(Filepath, sizeof(Filepath), "%s", Filename); - } + // TODO(matt): Throughout this function dispense with Filename, in favour of the passed in BaseFilename, or Filepath? + // + // TODO(matt): A "MakeString0OnStack()" sort of function? + // NOTE(matt): Stack-string + int NullTerminationBytes = 1; + char Filename[BaseFilename.Length + ExtensionStrings[EXT_HMML].Length + NullTerminationBytes]; + char *P = Filename; + P += CopyStringToBarePtr(P, BaseFilename); + P += CopyStringToBarePtr(P, ExtensionStrings[EXT_HMML]); + *P = '\0'; - FILE *InFile; - if(!(InFile = fopen(Filepath, "r"))) + // NOTE(matt): Stack-string + char Filepath[CurrentProject->HMMLDir.Length + StringLength("/") + BaseFilename.Length + ExtensionStrings[EXT_HMML].Length + NullTerminationBytes]; + P = Filepath; + P += CopyStringToBarePtr(P, CurrentProject->HMMLDir); + P += CopyStringToBarePtr(P, Wrap0("/")); + P += CopyStringToBarePtr(P, Wrap0(Filename)); + *P = '\0'; + + FILE *InFile = fopen(Filepath, "r"); + if(!InFile) { - LogError(LOG_ERROR, "Unable to open (annotations file) %s: %s", Filename, strerror(errno)); - fprintf(stderr, "Unable to open (annotations file) %s: %s\n", Filename, strerror(errno)); + LogError(LOG_ERROR, "Unable to open %s: %s", Filename, strerror(errno)); + fprintf(stderr, "Unable to open %s: %s\n", Filename, strerror(errno)); return RC_ERROR_FILE; } HMML_Output HMML = hmml_parse_file(InFile); fclose(InFile); - char *BaseFilename = GetBaseFilename(Filename, ".hmml"); + // TODO(matt): Use the HMML.output here if it is set, else do the GetBaseFilename0() thing + // + // Once we have the idea of an HMML.output, exhaustively make sure we're using the right thing in the right + // place, i.e. BaseFilename vs Output if(HMML.well_formed) { bool HaveErrors = FALSE; - if(StringLength(BaseFilename) > MAX_BASE_FILENAME_LENGTH) + if(BaseFilename.Length > MAX_BASE_FILENAME_LENGTH) { - fprintf(stderr, "%sBase filename \"%s\" is too long (%d/%d characters)%s\n", ColourStrings[CS_ERROR], BaseFilename, StringLength(BaseFilename), MAX_BASE_FILENAME_LENGTH, ColourStrings[CS_END]); + fprintf(stderr, "%sBase filename \"%.*s\" is too long (%ld/%d characters)%s\n", ColourStrings[CS_ERROR], (int)BaseFilename.Length, BaseFilename.Base, BaseFilename.Length, MAX_BASE_FILENAME_LENGTH, ColourStrings[CS_END]); HaveErrors = TRUE; } @@ -4547,12 +8674,12 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena } else if(StringLength(HMML.metadata.title) > MAX_TITLE_LENGTH) { - fprintf(stderr, "%sVideo title \"%s\" is too long (%d/%d characters)%s\n", ColourStrings[CS_ERROR], HMML.metadata.title, StringLength(HMML.metadata.title), MAX_TITLE_LENGTH, ColourStrings[CS_END]); + fprintf(stderr, "%sVideo title \"%s\" is too long (%ld/%d characters)%s\n", ColourStrings[CS_ERROR], HMML.metadata.title, StringLength(HMML.metadata.title), MAX_TITLE_LENGTH, ColourStrings[CS_END]); HaveErrors = TRUE; } else { - CopyString(CollationBuffers->Title, sizeof(CollationBuffers->Title), "%s", HMML.metadata.title); + ClearCopyStringNoFormat(CollationBuffers->Title, sizeof(CollationBuffers->Title), Wrap0(HMML.metadata.title)); } if(!HMML.metadata.member) @@ -4560,23 +8687,10 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena fprintf(stderr, "Please set the member attribute in the [video] node of your .hmml file\n"); HaveErrors = TRUE; } - - if(!StringsDiffer(CollationBuffers->Theme, "")) + else if(!GetPersonFromConfig(Wrap0(HMML.metadata.member))) { - if(HMML.metadata.project) - { - CopyStringNoFormat(CollationBuffers->Theme, sizeof(CollationBuffers->Theme), HMML.metadata.project); - } - else - { - fprintf(stderr, "Unable to determine which theme to apply to the HTML\n" - "Please set at least one of:\n" - "\t1. project attribute in the [video] node of your .hmml file\n" - "\t2. ProjectID on the command line with -p\n" - "\t3. Style on the command line with -s\n" - ); - HaveErrors = TRUE; - } + ErrorCredentials(Wrap0(HMML.metadata.member), R_HOST); + HaveErrors = TRUE; } if(!HMML.metadata.id) @@ -4589,9 +8703,19 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena CopyString(CollationBuffers->VideoID, sizeof(CollationBuffers->VideoID), "%s", HMML.metadata.id); } - if(!HMML.metadata.vod_platform) + string VODPlatform = {}; + if(HMML.metadata.vod_platform) { - fprintf(stderr, "Please set the vod_platform attribute in the [video] node of your .hmml file\n"); + VODPlatform = Wrap0(HMML.metadata.vod_platform); + } + else if(CurrentProject->VODPlatform.Length > 0) + { + VODPlatform = CurrentProject->VODPlatform; + } + + if(VODPlatform.Length == 0) + { + fprintf(stderr, "Please configure or set the vod_platform attribute in the [video] node of your .hmml file\n"); HaveErrors = TRUE; } else @@ -4600,38 +8724,37 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena } buffer URLPlayer; - ClaimBuffer(&URLPlayer, "URLPlayer", MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_PLAYER_URL_PREFIX_LENGTH + MAX_BASE_FILENAME_LENGTH); - ConstructPlayerURL(&URLPlayer, BaseFilename); + ClaimBuffer(&URLPlayer, BID_URL_PLAYER, MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_BASE_FILENAME_LENGTH); + ConstructPlayerURL(&URLPlayer, N->Project, HMML.metadata.output ? Wrap0(HMML.metadata.output) : BaseFilename); CopyString(CollationBuffers->URLPlayer, sizeof(CollationBuffers->URLPlayer), "%s", URLPlayer.Location); DeclaimBuffer(&URLPlayer); - if(HMML.metadata.project && (!StringsDiffer(CollationBuffers->ProjectID, "") || !StringsDiffer(CollationBuffers->ProjectName, ""))) + medium *DefaultMedium = CurrentProject->DefaultMedium; + if(HMML.metadata.medium && !(DefaultMedium = MediumExists(Wrap0(HMML.metadata.medium)))) { - for(int ProjectIndex = 0; ProjectIndex < ArrayCount(ProjectInfo); ++ProjectIndex) - { - if(!StringsDiffer(ProjectInfo[ProjectIndex].ProjectID, - HMML.metadata.project)) - { - CopyStringNoFormat(CollationBuffers->ProjectID, sizeof(CollationBuffers->ProjectID), HMML.metadata.project); - CopyStringNoFormat(CollationBuffers->ProjectName, sizeof(CollationBuffers->ProjectName), ProjectInfo[ProjectIndex].FullName); - break; - } - } + HaveErrors = TRUE; + } + if(!DefaultMedium) + { + fprintf(stderr, "No default_medium set in config, or medium set in the .hmml video node\n"); + HaveErrors = TRUE; } - char *DefaultMedium = Config.DefaultMedium; - if(HMML.metadata.medium) + string ProjectTitle; + if(CurrentProject->HTMLTitle.Length) { - if(MediumExists(HMML.metadata.medium)) - { - DefaultMedium = HMML.metadata.medium; - } - else { HaveErrors = TRUE; } + ProjectTitle = CurrentProject->HTMLTitle; + } + else + { + ProjectTitle = CurrentProject->Title; } - // TODO(matt): Consider simply making these as buffers and claiming the necessary amount for them - // The nice thing about doing it this way, though, is that it encourages bespoke template use, which should - // usually be the more convenient way for people to write greater amounts of localised information + // TODO(matt): Handle the art and art_variants once .hmml supports them + + // TODO(matt): Consider simply making these as buffers and claiming the necessary amount for them + // The nice thing about doing it this way, though, is that it encourages bespoke template use, which should + // usually be the more convenient way for people to write greater amounts of localised information for(int CustomIndex = 0; CustomIndex < HMML_CUSTOM_ATTR_COUNT; ++CustomIndex) { if(HMML.metadata.custom[CustomIndex]) @@ -4656,22 +8779,22 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena { switch(CustomIndex) { - case 0: CopyStringNoFormat(CollationBuffers->Custom0, sizeof(CollationBuffers->Custom0), HMML.metadata.custom[CustomIndex]); break; - case 1: CopyStringNoFormat(CollationBuffers->Custom1, sizeof(CollationBuffers->Custom1), HMML.metadata.custom[CustomIndex]); break; - case 2: CopyStringNoFormat(CollationBuffers->Custom2, sizeof(CollationBuffers->Custom2), HMML.metadata.custom[CustomIndex]); break; - case 3: CopyStringNoFormat(CollationBuffers->Custom3, sizeof(CollationBuffers->Custom3), HMML.metadata.custom[CustomIndex]); break; - case 4: CopyStringNoFormat(CollationBuffers->Custom4, sizeof(CollationBuffers->Custom4), HMML.metadata.custom[CustomIndex]); break; - case 5: CopyStringNoFormat(CollationBuffers->Custom5, sizeof(CollationBuffers->Custom5), HMML.metadata.custom[CustomIndex]); break; - case 6: CopyStringNoFormat(CollationBuffers->Custom6, sizeof(CollationBuffers->Custom6), HMML.metadata.custom[CustomIndex]); break; - case 7: CopyStringNoFormat(CollationBuffers->Custom7, sizeof(CollationBuffers->Custom7), HMML.metadata.custom[CustomIndex]); break; - case 8: CopyStringNoFormat(CollationBuffers->Custom8, sizeof(CollationBuffers->Custom8), HMML.metadata.custom[CustomIndex]); break; - case 9: CopyStringNoFormat(CollationBuffers->Custom9, sizeof(CollationBuffers->Custom9), HMML.metadata.custom[CustomIndex]); break; - case 10: CopyStringNoFormat(CollationBuffers->Custom10, sizeof(CollationBuffers->Custom10), HMML.metadata.custom[CustomIndex]); break; - case 11: CopyStringNoFormat(CollationBuffers->Custom11, sizeof(CollationBuffers->Custom11), HMML.metadata.custom[CustomIndex]); break; - case 12: CopyStringNoFormat(CollationBuffers->Custom12, sizeof(CollationBuffers->Custom12), HMML.metadata.custom[CustomIndex]); break; - case 13: CopyStringNoFormat(CollationBuffers->Custom13, sizeof(CollationBuffers->Custom13), HMML.metadata.custom[CustomIndex]); break; - case 14: CopyStringNoFormat(CollationBuffers->Custom14, sizeof(CollationBuffers->Custom14), HMML.metadata.custom[CustomIndex]); break; - case 15: CopyStringNoFormat(CollationBuffers->Custom15, sizeof(CollationBuffers->Custom15), HMML.metadata.custom[CustomIndex]); break; + case 0: CopyStringNoFormat(CollationBuffers->Custom0, sizeof(CollationBuffers->Custom0), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 1: CopyStringNoFormat(CollationBuffers->Custom1, sizeof(CollationBuffers->Custom1), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 2: CopyStringNoFormat(CollationBuffers->Custom2, sizeof(CollationBuffers->Custom2), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 3: CopyStringNoFormat(CollationBuffers->Custom3, sizeof(CollationBuffers->Custom3), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 4: CopyStringNoFormat(CollationBuffers->Custom4, sizeof(CollationBuffers->Custom4), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 5: CopyStringNoFormat(CollationBuffers->Custom5, sizeof(CollationBuffers->Custom5), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 6: CopyStringNoFormat(CollationBuffers->Custom6, sizeof(CollationBuffers->Custom6), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 7: CopyStringNoFormat(CollationBuffers->Custom7, sizeof(CollationBuffers->Custom7), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 8: CopyStringNoFormat(CollationBuffers->Custom8, sizeof(CollationBuffers->Custom8), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 9: CopyStringNoFormat(CollationBuffers->Custom9, sizeof(CollationBuffers->Custom9), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 10: CopyStringNoFormat(CollationBuffers->Custom10, sizeof(CollationBuffers->Custom10), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 11: CopyStringNoFormat(CollationBuffers->Custom11, sizeof(CollationBuffers->Custom11), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 12: CopyStringNoFormat(CollationBuffers->Custom12, sizeof(CollationBuffers->Custom12), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 13: CopyStringNoFormat(CollationBuffers->Custom13, sizeof(CollationBuffers->Custom13), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 14: CopyStringNoFormat(CollationBuffers->Custom14, sizeof(CollationBuffers->Custom14), Wrap0(HMML.metadata.custom[CustomIndex])); break; + case 15: CopyStringNoFormat(CollationBuffers->Custom15, sizeof(CollationBuffers->Custom15), Wrap0(HMML.metadata.custom[CustomIndex])); break; } } } @@ -4681,7 +8804,7 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena { if(HMML.metadata.template) { - switch(PackTemplate(BespokeTemplate, HMML.metadata.template, TEMPLATE_BESPOKE)) + switch(PackTemplate(BespokeTemplate, Wrap0(HMML.metadata.template), TEMPLATE_BESPOKE)) { case RC_ARENA_FULL: case RC_INVALID_TEMPLATE: // Invalid template @@ -4696,21 +8819,36 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena if(HaveErrors) { - fprintf(stderr, "%sSkipping%s %s", ColourStrings[CS_ERROR], ColourStrings[CS_END], BaseFilename); + fprintf(stderr, "%sSkipping%s %.*s", ColourStrings[CS_ERROR], ColourStrings[CS_END], (int)BaseFilename.Length, BaseFilename.Base); if(HMML.metadata.title) { fprintf(stderr, " - %s", HMML.metadata.title); } fprintf(stderr, "\n"); hmml_free(&HMML); return RC_ERROR_HMML; } - if(N) + string OutputLocation = {}; + if(HMML.metadata.output) { - N->This.LinkOffsets.PrevStart = 0; - N->This.LinkOffsets.PrevEnd = 0; - N->This.LinkOffsets.NextStart = 0; - N->This.LinkOffsets.NextEnd = 0; + OutputLocation = Wrap0(HMML.metadata.output); + } + else + { + OutputLocation = BaseFilename; } + ClearCopyStringNoFormat(N->WorkingThis.OutputLocation, sizeof(N->WorkingThis.OutputLocation), OutputLocation); + + if(N->This) + { + string OldOutputLocation = Wrap0i(N->This->OutputLocation, sizeof(N->This->OutputLocation)); + string NewOutputLocation = Wrap0i(N->WorkingThis.OutputLocation, sizeof(N->WorkingThis.OutputLocation)); + if(StringsDiffer(OldOutputLocation, NewOutputLocation)) + { + DeletePlayerPageFromFilesystem(N->Project, OldOutputLocation, FALSE, TRUE); + } + } + + // TODO(matt): Handle art and art_variants, once .hmml supports them #if DEBUG printf( "================================================================================\n" @@ -4722,35 +8860,45 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena // Master // IncludesPlayer // Menus - // QuoteMenu - // ReferenceMenu - // FilterMenu - // FilterTopics - // FilterMedia - // CreditsMenu + // MenuBuffers->Quote + // MenuBuffers->Reference + // MenuBuffers->Filter + // MenuBuffers->FilterTopics + // MenuBuffers->FilterMedia + // MenuBuffers->Credits // Player // Script - buffer QuoteMenu; - buffer ReferenceMenu; - buffer FilterMenu; - buffer FilterTopics; - buffer FilterMedia; - buffer CreditsMenu; - buffer Annotation; - buffer AnnotationHeader; - buffer AnnotationClass; - buffer AnnotationData; - buffer Text; - buffer CategoryIcons; + menu_buffers MenuBuffers = {}; + index_buffers IndexBuffers = {}; + player_buffers PlayerBuffers = {}; - if(ClaimBuffer(&QuoteMenu, "QuoteMenu", Kilobytes(32)) == RC_ARENA_FULL) { hmml_free(&HMML); return RC_ARENA_FULL; }; - if(ClaimBuffer(&ReferenceMenu, "ReferenceMenu", Kilobytes(32)) == RC_ARENA_FULL) { hmml_free(&HMML); return RC_ARENA_FULL; }; - if(ClaimBuffer(&FilterMenu, "FilterMenu", Kilobytes(16)) == RC_ARENA_FULL) { hmml_free(&HMML); return RC_ARENA_FULL; }; - if(ClaimBuffer(&FilterTopics, "FilterTopics", Kilobytes(8)) == RC_ARENA_FULL) { hmml_free(&HMML); return RC_ARENA_FULL; }; - if(ClaimBuffer(&FilterMedia, "FilterMedia", Kilobytes(8)) == RC_ARENA_FULL) { hmml_free(&HMML); return RC_ARENA_FULL; }; - if(ClaimBuffer(&CreditsMenu, "CreditsMenu", Kilobytes(8)) == RC_ARENA_FULL) { hmml_free(&HMML); return RC_ARENA_FULL; }; + if(ClaimBuffer(&MenuBuffers.Quote, BID_MENU_BUFFERS_QUOTE, Kilobytes(32)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + if(ClaimBuffer(&MenuBuffers.Reference, BID_MENU_BUFFERS_REFERENCE, Kilobytes(32)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + if(ClaimBuffer(&MenuBuffers.Filter, BID_MENU_BUFFERS_FILTER, Kilobytes(16)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + if(ClaimBuffer(&MenuBuffers.FilterTopics, BID_MENU_BUFFERS_FILTER_TOPICS, Kilobytes(8)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + if(ClaimBuffer(&MenuBuffers.FilterMedia, BID_MENU_BUFFERS_FILTER_MEDIA, Kilobytes(8)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + if(ClaimBuffer(&MenuBuffers.Credits, BID_MENU_BUFFERS_CREDITS, Kilobytes(8)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + + // NOTE(matt): Tree structure of IndexBuffers dependencies + // Master + // Header + // Class + // Data + // Text + // CategoryIcons + + if(ClaimBuffer(&IndexBuffers.Master, BID_INDEX_BUFFERS_MASTER, Kilobytes(8)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + if(ClaimBuffer(&IndexBuffers.Header, BID_INDEX_BUFFERS_HEADER, Kilobytes(1)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + if(ClaimBuffer(&IndexBuffers.Class, BID_INDEX_BUFFERS_CLASS, 256) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + if(ClaimBuffer(&IndexBuffers.Data, BID_INDEX_BUFFERS_DATA, Kilobytes(1)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + if(ClaimBuffer(&IndexBuffers.Text, BID_INDEX_BUFFERS_TEXT, Kilobytes(4)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + if(ClaimBuffer(&IndexBuffers.CategoryIcons, BID_INDEX_BUFFERS_CATEGORY_ICONS, Kilobytes(1)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + + if(ClaimBuffer(&PlayerBuffers.Menus, BID_PLAYER_BUFFERS_MENUS, Kilobytes(32)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + if(ClaimBuffer(&PlayerBuffers.Main, BID_PLAYER_BUFFERS_MAIN, Kilobytes(512)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; + if(ClaimBuffer(&PlayerBuffers.Script, BID_PLAYER_BUFFERS_SCRIPT, Kilobytes(8)) == RC_ARENA_FULL) { HMMLCleanup(); return RC_ARENA_FULL; }; ref_info ReferencesArray[200] = { }; categories Topics = { }; @@ -4759,117 +8907,109 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena bool HasQuoteMenu = FALSE; bool HasReferenceMenu = FALSE; bool HasFilterMenu = FALSE; - bool HasCreditsMenu = FALSE; int QuoteIdentifier = 0x3b1; int RefIdentifier = 1; int UniqueRefs = 0; - CopyStringToBuffer(&CollationBuffers->Menus, - "
\n" - " ", StringsDiffer(Config.Theme, "") ? Config.Theme : HMML.metadata.project); - CopyStringToBufferHTMLSafe(&CollationBuffers->Menus, HMML.metadata.title); - CopyStringToBuffer(&CollationBuffers->Menus, "\n"); + CopyStringToBuffer(&PlayerBuffers.Menus, + "
\n" + " ", (int)CurrentProject->Theme.Length, CurrentProject->Theme.Base); + CopyStringToBufferHTMLSafe(&PlayerBuffers.Menus, Wrap0(HMML.metadata.title)); + CopyStringToBuffer(&PlayerBuffers.Menus, "\n"); - CopyStringToBuffer(&CollationBuffers->Player, + CopyStringToBuffer(&PlayerBuffers.Main, "
\n" "
\n" - "
\n", HMML.metadata.id, StringsDiffer(Config.Theme, "") ? Config.Theme : HMML.metadata.project); + "
\n", HMML.metadata.id, (int)CurrentProject->Theme.Length, CurrentProject->Theme.Base); if(N) { - N->This.LinkOffsets.PrevStart = (CollationBuffers->Player.Ptr - CollationBuffers->Player.Location); - if(N->Prev.Size || N->Next.Size) + N->WorkingThis.LinkOffsets.PrevStart = (PlayerBuffers.Main.Ptr - PlayerBuffers.Main.Location); + if((N->Prev && N->Prev->Size) || (N->Next && N->Next->Size)) { - if(N->Prev.Size) + if(N->Prev && N->Prev->Size) { // TODO(matt): Once we have a more rigorous notion of "Day Numbers", perhaps also use them here buffer PreviousPlayerURL; - ClaimBuffer(&PreviousPlayerURL, "PreviousPlayerURL", MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_PLAYER_URL_PREFIX_LENGTH + MAX_BASE_FILENAME_LENGTH); - ConstructPlayerURL(&PreviousPlayerURL, N->Prev.BaseFilename); - CopyStringToBuffer(&CollationBuffers->Player, + ClaimBuffer(&PreviousPlayerURL, BID_PREVIOUS_PLAYER_URL, MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_BASE_FILENAME_LENGTH); + ConstructPlayerURL(&PreviousPlayerURL, N->Project, Wrap0i(N->Prev->OutputLocation, sizeof(N->Prev->OutputLocation))); + CopyStringToBuffer(&PlayerBuffers.Main, "
Previous: '%s'
\n", PreviousPlayerURL.Location, - N->Prev.Title); + N->Prev->Title); DeclaimBuffer(&PreviousPlayerURL); } else { - CopyStringToBuffer(&CollationBuffers->Player, - "
Welcome to %s
\n", CollationBuffers->ProjectName); + CopyStringToBuffer(&PlayerBuffers.Main, + "
Welcome to %.*s
\n", (int)ProjectTitle.Length, ProjectTitle.Base); } } - N->This.LinkOffsets.PrevEnd = (CollationBuffers->Player.Ptr - CollationBuffers->Player.Location - N->This.LinkOffsets.PrevStart); + N->WorkingThis.LinkOffsets.PrevEnd = (PlayerBuffers.Main.Ptr - PlayerBuffers.Main.Location - N->WorkingThis.LinkOffsets.PrevStart); } - CopyStringToBuffer(&CollationBuffers->Player, + CopyStringToBuffer(&PlayerBuffers.Main, "
\n"); speakers Speakers = { }; - switch(BuildCredits(&CreditsMenu, &HasCreditsMenu, &HMML.metadata, &Speakers)) + bool RequiresCineraJS = FALSE; + switch(BuildCredits(&MenuBuffers.Credits, &HMML.metadata, &Speakers, &RequiresCineraJS)) { case CreditsError_NoHost: - case CreditsError_NoAnnotator: + case CreditsError_NoIndexer: case CreditsError_NoCredentials: - fprintf(stderr, "%sSkipping%s %s - %s\n", ColourStrings[CS_ERROR], ColourStrings[CS_END], BaseFilename, HMML.metadata.title); + fprintf(stderr, "%sSkipping%s %.*s - %s\n", ColourStrings[CS_ERROR], ColourStrings[CS_END], (int)BaseFilename.Length, BaseFilename.Base, HMML.metadata.title); HMMLCleanup(); return RC_ERROR_HMML; } bool PrivateVideo = FALSE; - if(Config.Edition != EDITION_SINGLE && N->This.Size == 0 && !(Config.Mode & MODE_NOPRIVACY)) + if(N->WorkingThis.Size == 0 && !CurrentProject->IgnorePrivacy) { if(VideoIsPrivate(HMML.metadata.id)) { // TODO(matt): Actually generate these guys, just putting them in a secret location - N->This.LinkOffsets.PrevStart = 0; - N->This.LinkOffsets.PrevEnd = 0; + N->WorkingThis.LinkOffsets.PrevStart = 0; + N->WorkingThis.LinkOffsets.PrevEnd = 0; PrivateVideo = TRUE; HMMLCleanup(); return RC_PRIVATE_VIDEO; } } - if(Config.Edition != EDITION_SINGLE && !PrivateVideo) + if(!PrivateVideo) { CopyStringToBuffer(&CollationBuffers->SearchEntry, "name: \""); - if(StringsDiffer(Config.PlayerURLPrefix, "")) - { - char *Ptr = BaseFilename + StringLength(Config.ProjectID); - CopyStringToBuffer(&CollationBuffers->SearchEntry, "%s%s", Config.PlayerURLPrefix, Ptr); - } - else - { - CopyStringToBuffer(&CollationBuffers->SearchEntry, "%s", BaseFilename); - } + CopyStringToBuffer(&CollationBuffers->SearchEntry, "%.*s", (int)OutputLocation.Length, OutputLocation.Base); CopyStringToBuffer(&CollationBuffers->SearchEntry, "\"\n" "title: \""); - CopyStringToBufferNoFormat(&CollationBuffers->SearchEntry, HMML.metadata.title); + CopyStringToBufferNoFormat(&CollationBuffers->SearchEntry, Wrap0(HMML.metadata.title)); CopyStringToBuffer(&CollationBuffers->SearchEntry, "\"\n" "markers:\n"); } #if DEBUG - printf("\n\n --- Entering Annotations Loop ---\n\n\n\n"); + printf("\n\n --- Entering Timestamps Loop ---\n\n\n\n"); #endif int PreviousTimecode = 0; - for(int AnnotationIndex = 0; AnnotationIndex < HMML.annotation_count; ++AnnotationIndex) + for(int TimestampIndex = 0; TimestampIndex < HMML.annotation_count; ++TimestampIndex) { #if DEBUG - printf("%d\n", AnnotationIndex); + printf("%d\n", TimestampIndex); #endif - HMML_Annotation *Anno = HMML.annotations + AnnotationIndex; + HMML_Annotation *Anno = HMML.annotations + TimestampIndex; if(TimecodeToSeconds(Anno->time) < PreviousTimecode) { fprintf(stderr, "%s:%d: Timecode %s is chronologically before previous timecode\n" - "%sSkipping%s %s - %s\n", + "%sSkipping%s %.*s - %s\n", Filename, Anno->line, Anno->time, - ColourStrings[CS_ERROR], ColourStrings[CS_END], BaseFilename, HMML.metadata.title); + ColourStrings[CS_ERROR], ColourStrings[CS_END], (int)BaseFilename.Length, BaseFilename.Base, HMML.metadata.title); HMMLCleanup(); return RC_ERROR_HMML; } @@ -4882,63 +9022,51 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena quote_info QuoteInfo = { }; - // NOTE(matt): Tree structure of "annotation local" buffer dependencies - // Annotation - // AnnotationHeader - // AnnotationClass - // AnnotationData - // Text - // CategoryIcons + RewindBuffer(&IndexBuffers.Master); + RewindBuffer(&IndexBuffers.Header); + RewindBuffer(&IndexBuffers.Class); + RewindBuffer(&IndexBuffers.Data); + RewindBuffer(&IndexBuffers.Text); + RewindBuffer(&IndexBuffers.CategoryIcons); - // TODO(matt): Shouldn't all of these guys DeclaimBuffer() before returning? - if(ClaimBuffer(&Annotation, "Annotation", Kilobytes(8)) == RC_ARENA_FULL) { hmml_free(&HMML); return RC_ARENA_FULL; }; - if(ClaimBuffer(&AnnotationHeader, "AnnotationHeader", Kilobytes(1)) == RC_ARENA_FULL) { hmml_free(&HMML); return RC_ARENA_FULL; }; - if(ClaimBuffer(&AnnotationClass, "AnnotationClass", 256) == RC_ARENA_FULL) { hmml_free(&HMML); return RC_ARENA_FULL; }; - if(ClaimBuffer(&AnnotationData, "AnnotationData", 512) == RC_ARENA_FULL) { hmml_free(&HMML); return RC_ARENA_FULL; }; - if(ClaimBuffer(&Text, "Text", Kilobytes(4)) == RC_ARENA_FULL) { hmml_free(&HMML); return RC_ARENA_FULL; }; - if(ClaimBuffer(&CategoryIcons, "CategoryIcons", Kilobytes(1)) == RC_ARENA_FULL) { hmml_free(&HMML); return RC_ARENA_FULL; }; - - CopyStringToBuffer(&AnnotationHeader, + CopyStringToBuffer(&IndexBuffers.Header, "
time)); - CopyStringToBuffer(&AnnotationClass, + CopyStringToBuffer(&IndexBuffers.Class, " class=\"marker"); - if((Anno->author || Speakers.Count > 1) && !IsCategorisedAFK(*Anno)) + speaker *Speaker = GetSpeaker(Speakers, Anno->author ? Wrap0(Anno->author) : CurrentProject->StreamUsername.Length > 0 ? CurrentProject->StreamUsername : CurrentProject->Owner->ID); + if(!IsCategorisedAFK(Anno)) { - int SpeakerIndex; - if(Anno->author && (SpeakerIndex = LinearSearchForSpeaker(Speakers, Anno->author)) == -1) + if(Speakers.Count > 1 && Speaker && !IsCategorisedAuthored(Anno)) + { + string DisplayName = !Speaker->Seen ? Speaker->Person->Name : Wrap0(Speaker->Abbreviation); + + CopyStringToBuffer(&IndexBuffers.Text, + "%.*s: ", + Speaker->Colour.Hue, + Speaker->Colour.Saturation, + + (int)DisplayName.Length, DisplayName.Base); + + Speaker->Seen = TRUE; + } + else if(Anno->author) { if(!HasFilterMenu) { HasFilterMenu = TRUE; } - InsertCategory(&Topics, &LocalTopics, &Media, &LocalMedia, "authored"); + InsertCategory(&Topics, &LocalTopics, &Media, &LocalMedia, Wrap0("authored")); hsl_colour AuthorColour; - StringToColourHash(&AuthorColour, Anno->author); + StringToColourHash(&AuthorColour, Wrap0(Anno->author)); // TODO(matt): That EDITION_NETWORK site database API-polling stuff - CopyStringToBuffer(&Text, + CopyStringToBuffer(&IndexBuffers.Text, "%s ", AuthorColour.Hue, AuthorColour.Saturation, Anno->author); } - else - { - if(!Anno->author) - { - SpeakerIndex = LinearSearchForSpeaker(Speakers, HMML.metadata.member); - } - - CopyStringToBuffer(&Text, - "%s: ", - Speakers.Speaker[SpeakerIndex].Colour.Hue, - Speakers.Speaker[SpeakerIndex].Colour.Saturation, - - Speakers.Speaker[SpeakerIndex].Seen == FALSE ? Speakers.Speaker[SpeakerIndex].Credential->CreditedName : Speakers.Speaker[SpeakerIndex].Abbreviation); - - Speakers.Speaker[SpeakerIndex].Seen = TRUE; - } } char *InPtr = Anno->text; @@ -4959,32 +9087,32 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena { // TODO(matt): That EDITION_NETWORK site database API-polling stuff hsl_colour Colour; - StringToColourHash(&Colour, Anno->markers[MarkerIndex].marker); - CopyStringToBuffer(&Text, + StringToColourHash(&Colour, Wrap0(Anno->markers[MarkerIndex].marker)); + CopyStringToBuffer(&IndexBuffers.Text, "%.*s", Anno->markers[MarkerIndex].type == HMML_MEMBER ? "member" : "project", Colour.Hue, Colour.Saturation, - StringLength(Readable), InPtr); + (int)StringLength(Readable), InPtr); } break; case HMML_CATEGORY: { - switch(GenerateTopicColours(Anno->markers[MarkerIndex].marker)) + switch(GenerateTopicColours(N, Wrap0(Anno->markers[MarkerIndex].marker))) { case RC_SUCCESS: case RC_NOOP: break; case RC_ERROR_FILE: case RC_ERROR_MEMORY: - hmml_free(&HMML); + HMMLCleanup(); return RC_ERROR_FATAL; }; if(!HasFilterMenu) { HasFilterMenu = TRUE; } - InsertCategory(&Topics, &LocalTopics, &Media, &LocalMedia, Anno->markers[MarkerIndex].marker); - CopyStringToBuffer(&Text, "%.*s", StringLength(Readable), InPtr); + InsertCategory(&Topics, &LocalTopics, &Media, &LocalMedia, Wrap0(Anno->markers[MarkerIndex].marker)); + CopyStringToBuffer(&IndexBuffers.Text, "%.*s", (int)StringLength(Readable), InPtr); } break; case HMML_MARKER_COUNT: break; } @@ -4998,7 +9126,7 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena HMML_Reference *CurrentRef = Anno->references + RefIndex; if(!HasReferenceMenu) { - CopyStringToBuffer(&ReferenceMenu, + CopyStringToBuffer(&MenuBuffers.Reference, "
\n" " References ▼\n" "
\n"); @@ -5008,10 +9136,10 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena LogError(LOG_ERROR, "Reference combination processing failed: %s:%d", Filename, Anno->line); fprintf(stderr, "%s:%d: Cannot process new combination of reference info\n" "\n" - "Either tweak your annotation, or contact miblodelcarpio@gmail.com\n" + "Either tweak your timestamp, or contact miblodelcarpio@gmail.com\n" "mentioning the ref node you want to write and how you want it to\n" "appear in the references menu\n", Filename, Anno->line); - hmml_free(&HMML); + HMMLCleanup(); return RC_INVALID_REFERENCE; } ++ReferencesArray[RefIdentifier - 1].IdentifierCount; @@ -5027,12 +9155,12 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena { LogError(LOG_EMERGENCY, "REF_MAX_IDENTIFIER (%d) reached. Contact miblodelcarpio@gmail.com", MAX_REF_IDENTIFIER_COUNT); fprintf(stderr, "%s:%d: Too many timecodes associated with one reference (increase REF_MAX_IDENTIFIER)\n", Filename, Anno->line); - hmml_free(&HMML); + HMMLCleanup(); return RC_ERROR_MAX_REFS; } if(CurrentRef->isbn) { - if(!StringsDiffer(CurrentRef->isbn, ReferencesArray[i].ID)) + if(!StringsDiffer0(CurrentRef->isbn, ReferencesArray[i].ID)) { CopyString(ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Timecode, sizeof(ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Timecode), "%s", Anno->time); ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Identifier = RefIdentifier; @@ -5042,7 +9170,7 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena } else if(CurrentRef->url) { - if(!StringsDiffer(CurrentRef->url, ReferencesArray[i].ID)) + if(!StringsDiffer0(CurrentRef->url, ReferencesArray[i].ID)) { CopyString(ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Timecode, sizeof(ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Timecode), "%s", Anno->time); ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Identifier = RefIdentifier; @@ -5054,7 +9182,7 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena { LogError(LOG_ERROR, "Reference missing ISBN or URL: %s:%d", Filename, Anno->line); fprintf(stderr, "%s:%d: Reference must have an ISBN or URL\n", Filename, Anno->line); - hmml_free(&HMML); + HMMLCleanup(); return RC_INVALID_REFERENCE; } } @@ -5064,10 +9192,10 @@ HMMLToBuffers(buffers *CollationBuffers, template *BespokeTemplate, char *Filena LogError(LOG_ERROR, "Reference combination processing failed: %s:%d", Filename, Anno->line); fprintf(stderr, "%s:%d: Cannot process new combination of reference info\n" "\n" - "Either tweak your annotation, or contact miblodelcarpio@gmail.com\n" + "Either tweak your timestamp, or contact miblodelcarpio@gmail.com\n" "mentioning the ref node you want to write and how you want it to\n" "appear in the references menu\n", Filename, Anno->line); - hmml_free(&HMML); + HMMLCleanup(); return RC_INVALID_REFERENCE; } ++ReferencesArray[UniqueRefs].IdentifierCount; @@ -5078,17 +9206,17 @@ AppendedIdentifier: { if(CurrentRef->isbn) { - CopyStringToBuffer(&AnnotationData, " data-ref=\"%s", CurrentRef->isbn); + CopyStringToBuffer(&IndexBuffers.Data, " data-ref=\"%s", CurrentRef->isbn); } else if(CurrentRef->url) { - CopyStringToBuffer(&AnnotationData, " data-ref=\"%s", CurrentRef->url); + CopyStringToBuffer(&IndexBuffers.Data, " data-ref=\"%s", CurrentRef->url); } else { LogError(LOG_ERROR, "Reference missing ISBN or URL: %s:%d", Filename, Anno->line); fprintf(stderr, "%s:%d: Reference must have an ISBN or URL\n", Filename, Anno->line); - hmml_free(&HMML); + HMMLCleanup(); return RC_INVALID_REFERENCE; } @@ -5098,22 +9226,22 @@ AppendedIdentifier: { if(CurrentRef->isbn) { - CopyStringToBuffer(&AnnotationData, ",%s", CurrentRef->isbn); + CopyStringToBuffer(&IndexBuffers.Data, ",%s", CurrentRef->isbn); } else if(CurrentRef->url) { - CopyStringToBuffer(&AnnotationData, ",%s", CurrentRef->url); + CopyStringToBuffer(&IndexBuffers.Data, ",%s", CurrentRef->url); } else { LogError(LOG_ERROR, "Reference missing ISBN or URL: %s:%d", Filename, Anno->line); fprintf(stderr, "%s:%d: Reference must have an ISBN or URL", Filename, Anno->line); - hmml_free(&HMML); + HMMLCleanup(); return RC_INVALID_REFERENCE; } } - CopyStringToBuffer(&Text, "%s%d", + CopyStringToBuffer(&IndexBuffers.Text, "%s%d", Anno->references[RefIndex].offset == Anno->references[RefIndex-1].offset ? "," : "", RefIdentifier); @@ -5126,23 +9254,23 @@ AppendedIdentifier: switch(*InPtr) { case '<': - CopyStringToBuffer(&Text, "<"); + CopyStringToBuffer(&IndexBuffers.Text, "<"); break; case '>': - CopyStringToBuffer(&Text, ">"); + CopyStringToBuffer(&IndexBuffers.Text, ">"); break; case '&': - CopyStringToBuffer(&Text, "&"); + CopyStringToBuffer(&IndexBuffers.Text, "&"); break; case '\"': - CopyStringToBuffer(&Text, """); + CopyStringToBuffer(&IndexBuffers.Text, """); break; case '\'': - CopyStringToBuffer(&Text, "'"); + CopyStringToBuffer(&IndexBuffers.Text, "'"); break; default: - *Text.Ptr++ = *InPtr; - *Text.Ptr = '\0'; + *IndexBuffers.Text.Ptr++ = *InPtr; + *IndexBuffers.Text.Ptr = '\0'; break; } ++InPtr; @@ -5153,7 +9281,7 @@ AppendedIdentifier: { if(!HasQuoteMenu) { - CopyStringToBuffer(&QuoteMenu, + CopyStringToBuffer(&MenuBuffers.Quote, "
\n" " Quotes ▼\n" "
\n"); @@ -5163,220 +9291,215 @@ AppendedIdentifier: if(!HasReference) { - CopyStringToBuffer(&AnnotationData, " data-ref=\"&#%d;", QuoteIdentifier); + CopyStringToBuffer(&IndexBuffers.Data, " data-ref=\"&#%d;", QuoteIdentifier); } else { - CopyStringToBuffer(&AnnotationData, ",&#%d;", QuoteIdentifier); + CopyStringToBuffer(&IndexBuffers.Data, ",&#%d;", QuoteIdentifier); } HasQuote = TRUE; - char *Speaker = Anno->quote.author ? Anno->quote.author : HMML.metadata.stream_username ? HMML.metadata.stream_username : HMML.metadata.member; + string Speaker = {}; + if(Anno->quote.author) { Speaker = Wrap0(Anno->quote.author); } + else if(HMML.metadata.stream_username) { Speaker = Wrap0(HMML.metadata.stream_username); } + else if(CurrentProject->StreamUsername.Length > 0) { Speaker = CurrentProject->StreamUsername; } + else { Speaker = CurrentProject->Owner->ID; } + bool ShouldFetchQuotes = FALSE; - if(Config.Mode & MODE_NOCACHE || (Config.Edition != EDITION_SINGLE && time(0) - LastQuoteFetch > 60*60)) + if(Config->CacheDir.Length == 0 || time(0) - LastQuoteFetch > 60*60) { ShouldFetchQuotes = TRUE; LastQuoteFetch = time(0); } + if(BuildQuote(&QuoteInfo, Speaker, Anno->quote.id, ShouldFetchQuotes) == RC_UNFOUND) { - LogError(LOG_ERROR, "Quote #%s %d not found: %s:%d", Speaker, Anno->quote.id, Filename, Anno->line); - Filename[StringLength(Filename) - StringLength(".hmml")] = '\0'; + LogError(LOG_ERROR, "Quote #%.*s %d not found: %s:%d", (int)Speaker.Length, Speaker.Base, Anno->quote.id, Filename, Anno->line); - fprintf(stderr, "Quote #%s %d not found\n" - "%sSkipping%s %s - %s\n", - Speaker, Anno->quote.id, - ColourStrings[CS_ERROR], ColourStrings[CS_END], BaseFilename, HMML.metadata.title); - hmml_free(&HMML); + fprintf(stderr, "Quote #%.*s %d not found\n" + "%sSkipping%s %.*s - %s\n", + (int)Speaker.Length, Speaker.Base, Anno->quote.id, + ColourStrings[CS_ERROR], ColourStrings[CS_END], (int)BaseFilename.Length, BaseFilename.Base, HMML.metadata.title); + HMMLCleanup(); return RC_ERROR_QUOTE; } - CopyStringToBuffer(&QuoteMenu, - " \n" + CopyStringToBuffer(&MenuBuffers.Quote, + " \n" " \n" " \n" "
Quote %d
\n" "
", - Speaker, + (int)Speaker.Length, Speaker.Base, Anno->quote.id, QuoteIdentifier, Anno->quote.id); - CopyStringToBufferHTMLSafe(&QuoteMenu, QuoteInfo.Text); + CopyStringToBufferHTMLSafe(&MenuBuffers.Quote, Wrap0i(QuoteInfo.Text, sizeof(QuoteInfo.Text))); - CopyStringToBuffer(&QuoteMenu, "
\n" - " \n" + CopyStringToBuffer(&MenuBuffers.Quote, "
\n" + " \n" " \n" "
\n" " [&#%d;]%s\n" "
\n" " \n" "
\n", - Speaker, + (int)Speaker.Length, Speaker.Base, QuoteInfo.Date, TimecodeToSeconds(Anno->time), QuoteIdentifier, Anno->time); if(!Anno->text[0]) { - CopyStringToBuffer(&Text, "“"); - CopyStringToBufferHTMLSafe(&Text, QuoteInfo.Text); - CopyStringToBuffer(&Text, "”"); + CopyStringToBuffer(&IndexBuffers.Text, "“"); + CopyStringToBufferHTMLSafe(&IndexBuffers.Text, Wrap0i(QuoteInfo.Text, sizeof(QuoteInfo.Text))); + CopyStringToBuffer(&IndexBuffers.Text, "”"); } - CopyStringToBuffer(&Text, "&#%d;", QuoteIdentifier); + CopyStringToBuffer(&IndexBuffers.Text, "&#%d;", QuoteIdentifier); ++QuoteIdentifier; } - if(Config.Edition != EDITION_SINGLE && !PrivateVideo) + if(!PrivateVideo) { CopyStringToBuffer(&CollationBuffers->SearchEntry, "\"%d\": \"", TimecodeToSeconds(Anno->time)); if(Anno->is_quote && !Anno->text[0]) { CopyStringToBuffer(&CollationBuffers->SearchEntry, "\u201C"); - CopyStringToBufferNoFormat(&CollationBuffers->SearchEntry, QuoteInfo.Text); + CopyStringToBufferNoFormat(&CollationBuffers->SearchEntry, Wrap0i(QuoteInfo.Text, sizeof(QuoteInfo.Text))); CopyStringToBuffer(&CollationBuffers->SearchEntry, "\u201D"); } else { - CopyStringToBufferNoFormat(&CollationBuffers->SearchEntry, Anno->text); + CopyStringToBufferNoFormat(&CollationBuffers->SearchEntry, Wrap0(Anno->text)); } CopyStringToBuffer(&CollationBuffers->SearchEntry, "\"\n"); } while(MarkerIndex < Anno->marker_count) { - switch(GenerateTopicColours(Anno->markers[MarkerIndex].marker)) + switch(GenerateTopicColours(N, Wrap0(Anno->markers[MarkerIndex].marker))) { case RC_SUCCESS: case RC_NOOP: break; case RC_ERROR_FILE: case RC_ERROR_MEMORY: - hmml_free(&HMML); + HMMLCleanup(); return RC_ERROR_FATAL; } if(!HasFilterMenu) { HasFilterMenu = TRUE; } - InsertCategory(&Topics, &LocalTopics, &Media, &LocalMedia, Anno->markers[MarkerIndex].marker); + InsertCategory(&Topics, &LocalTopics, &Media, &LocalMedia, Wrap0(Anno->markers[MarkerIndex].marker)); ++MarkerIndex; } if(LocalTopics.Count == 0) { - switch(GenerateTopicColours("nullTopic")) + switch(GenerateTopicColours(N, Wrap0("nullTopic"))) { case RC_SUCCESS: case RC_NOOP: break; case RC_ERROR_FILE: case RC_ERROR_MEMORY: - hmml_free(&HMML); + HMMLCleanup(); return RC_ERROR_FATAL; }; - InsertCategory(&Topics, &LocalTopics, &Media, &LocalMedia, "nullTopic"); + InsertCategory(&Topics, &LocalTopics, &Media, &LocalMedia, Wrap0("nullTopic")); } if(LocalMedia.Count == 0) { - InsertCategory(&Topics, &LocalTopics, &Media, &LocalMedia, DefaultMedium); + InsertCategory(&Topics, &LocalTopics, &Media, &LocalMedia, DefaultMedium->ID); } - BuildCategories(&AnnotationClass, &CategoryIcons, &LocalTopics, &LocalMedia, &MarkerIndex, DefaultMedium); - CopyBuffer(&AnnotationHeader, &AnnotationClass); + BuildTimestampClass(&IndexBuffers.Class, &LocalTopics, &LocalMedia, DefaultMedium->ID); + CopyLandmarkedBuffer(&IndexBuffers.Header, &IndexBuffers.Class, 0, PAGE_PLAYER); if(HasQuote || HasReference) { - CopyStringToBuffer(&AnnotationData, "\""); - CopyBuffer(&AnnotationHeader, &AnnotationData); + CopyStringToBuffer(&IndexBuffers.Data, "\""); + CopyLandmarkedBuffer(&IndexBuffers.Header, &IndexBuffers.Data, 0, PAGE_PLAYER); } - CopyStringToBuffer(&AnnotationHeader, ">\n"); + CopyStringToBuffer(&IndexBuffers.Header, ">\n"); - CopyBuffer(&Annotation, &AnnotationHeader); - CopyStringToBuffer(&Annotation, + CopyLandmarkedBuffer(&IndexBuffers.Master, &IndexBuffers.Header, 0, PAGE_PLAYER); + CopyStringToBuffer(&IndexBuffers.Master, "
%s", Anno->time); - CopyBuffer(&Annotation, &Text); + CopyLandmarkedBuffer(&IndexBuffers.Master, &IndexBuffers.Text, 0, PAGE_PLAYER); if(LocalTopics.Count > 0) { - CopyBuffer(&Annotation, &CategoryIcons); + BuildCategoryIcons(&IndexBuffers.Master, &LocalTopics, &LocalMedia, DefaultMedium->ID, &RequiresCineraJS); } - CopyStringToBuffer(&Annotation, "
\n" + CopyStringToBuffer(&IndexBuffers.Master, "
\n" "
\n" "
%s", Anno->time); - CopyBuffer(&Annotation, &Text); + CopyLandmarkedBuffer(&IndexBuffers.Master, &IndexBuffers.Text, 0, PAGE_PLAYER); if(LocalTopics.Count > 0) { - CopyBuffer(&Annotation, &CategoryIcons); + BuildCategoryIcons(&IndexBuffers.Master, &LocalTopics, &LocalMedia, DefaultMedium->ID, &RequiresCineraJS); } - CopyStringToBuffer(&Annotation, "
\n" + CopyStringToBuffer(&IndexBuffers.Master, "
\n" "
\n" "
\n" "
%s", Anno->time); - CopyBuffer(&Annotation, &Text); + CopyLandmarkedBuffer(&IndexBuffers.Master, &IndexBuffers.Text, 0, PAGE_PLAYER); if(LocalTopics.Count > 0) { - CopyBuffer(&Annotation, &CategoryIcons); + //CopyLandmarkedBuffer(&IndexBuffers.Master, &IndexBuffers.CategoryIcons, PAGE_PLAYER); + BuildCategoryIcons(&IndexBuffers.Master, &LocalTopics, &LocalMedia, DefaultMedium->ID, &RequiresCineraJS); } - CopyStringToBuffer(&Annotation, "
\n" + CopyStringToBuffer(&IndexBuffers.Master, "
\n" "
\n" "
\n"); - CopyBuffer(&CollationBuffers->Player, &Annotation); - - // NOTE(matt): Tree structure of "annotation local" buffer dependencies - // CategoryIcons - // Text - // AnnotationData - // AnnotationClass - // AnnotationHeader - // Annotation - - DeclaimBuffer(&CategoryIcons); - DeclaimBuffer(&Text); - DeclaimBuffer(&AnnotationData); - DeclaimBuffer(&AnnotationClass); - DeclaimBuffer(&AnnotationHeader); - DeclaimBuffer(&Annotation); + CopyLandmarkedBuffer(&PlayerBuffers.Main, &IndexBuffers.Master, 0, PAGE_PLAYER); } - if(Config.Edition != EDITION_SINGLE && !PrivateVideo) + DeclaimIndexBuffers(&IndexBuffers); + + FreeCredentials(&Speakers); + + if(!PrivateVideo) { CopyStringToBuffer(&CollationBuffers->SearchEntry, "---\n"); - N->This.Size = CollationBuffers->SearchEntry.Ptr - CollationBuffers->SearchEntry.Location; + N->WorkingThis.Size = CollationBuffers->SearchEntry.Ptr - CollationBuffers->SearchEntry.Location; } #if DEBUG - printf("\n\n --- End of Annotations Loop ---\n\n\n\n"); + printf("\n\n --- End of Timestamps Loop ---\n\n\n\n"); #endif if(HasQuoteMenu) { - CopyStringToBuffer(&QuoteMenu, + CopyStringToBuffer(&MenuBuffers.Quote, "
\n" "
\n"); - CopyBuffer(&CollationBuffers->Menus, &QuoteMenu); + CopyLandmarkedBuffer(&PlayerBuffers.Menus, &MenuBuffers.Quote, 0, PAGE_PLAYER); } if(HasReferenceMenu) { for(int i = 0; i < UniqueRefs; ++i) { - CopyStringToBuffer(&ReferenceMenu, + CopyStringToBuffer(&MenuBuffers.Reference, " \n" " \n", ReferencesArray[i].ID, @@ -5384,63 +9507,64 @@ AppendedIdentifier: if(*ReferencesArray[i].Source) { - CopyStringToBuffer(&ReferenceMenu, + CopyStringToBuffer(&MenuBuffers.Reference, "
"); - CopyStringToBufferHTMLSafeBreakingOnSlash(&ReferenceMenu, ReferencesArray[i].Source); - CopyStringToBuffer(&ReferenceMenu, "
\n" + CopyStringToBufferHTMLSafeBreakingOnSlash(&MenuBuffers.Reference, ReferencesArray[i].Source); + CopyStringToBuffer(&MenuBuffers.Reference, "
\n" "
"); - CopyStringToBufferHTMLSafeBreakingOnSlash(&ReferenceMenu, ReferencesArray[i].RefTitle); - CopyStringToBuffer(&ReferenceMenu, "
\n"); + CopyStringToBufferHTMLSafeBreakingOnSlash(&MenuBuffers.Reference, ReferencesArray[i].RefTitle); + CopyStringToBuffer(&MenuBuffers.Reference, "
\n"); } else { - CopyStringToBuffer(&ReferenceMenu, + CopyStringToBuffer(&MenuBuffers.Reference, "
"); - CopyStringToBufferHTMLSafeBreakingOnSlash(&ReferenceMenu, ReferencesArray[i].RefTitle); - CopyStringToBuffer(&ReferenceMenu, "
\n"); + CopyStringToBufferHTMLSafeBreakingOnSlash(&MenuBuffers.Reference, ReferencesArray[i].RefTitle); + CopyStringToBuffer(&MenuBuffers.Reference, "
\n"); } - CopyStringToBuffer(&ReferenceMenu, + CopyStringToBuffer(&MenuBuffers.Reference, " \n"); for(int j = 0; j < ReferencesArray[i].IdentifierCount;) { - CopyStringToBuffer(&ReferenceMenu, + CopyStringToBuffer(&MenuBuffers.Reference, "
\n "); for(int k = 0; k < 3 && j < ReferencesArray[i].IdentifierCount; ++k, ++j) { - CopyStringToBuffer(&ReferenceMenu, + CopyStringToBuffer(&MenuBuffers.Reference, "[%d]%s", TimecodeToSeconds(ReferencesArray[i].Identifier[j].Timecode), ReferencesArray[i].Identifier[j].Identifier, ReferencesArray[i].Identifier[j].Timecode); } - CopyStringToBuffer(&ReferenceMenu, "\n" + CopyStringToBuffer(&MenuBuffers.Reference, "\n" "
\n"); } - CopyStringToBuffer(&ReferenceMenu, + CopyStringToBuffer(&MenuBuffers.Reference, "
\n"); } - CopyStringToBuffer(&ReferenceMenu, + CopyStringToBuffer(&MenuBuffers.Reference, "
\n" " \n"); - CopyBuffer(&CollationBuffers->Menus, &ReferenceMenu); + CopyLandmarkedBuffer(&PlayerBuffers.Menus, &MenuBuffers.Reference, 0, PAGE_PLAYER); } if(HasFilterMenu) { buffer URL; - ConstructResolvedAssetURL(&URL, ASSET_IMG_FILTER, PAGE_PLAYER); - CopyStringToBuffer(&FilterMenu, + asset *FilterImage = GetAsset(Wrap0(BuiltinAssets[ASSET_IMG_FILTER].Filename), ASSET_IMG); + ConstructResolvedAssetURL(&URL, FilterImage, PAGE_PLAYER); + CopyStringToBuffer(&MenuBuffers.Filter, "
\n" " \n" "
\n" "
Filter mode:
\n" @@ -5448,92 +9572,74 @@ AppendedIdentifier: if(Topics.Count > 0) { - CopyStringToBuffer(&FilterMenu, + CopyStringToBuffer(&MenuBuffers.Filter, "
\n" "
Topics
\n"); for(int i = 0; i < Topics.Count; ++i) { + // NOTE(matt): Stack-string char SanitisedMarker[StringLength(Topics.Category[i].Marker) + 1]; CopyString(SanitisedMarker, sizeof(SanitisedMarker), "%s", Topics.Category[i].Marker); SanitisePunctuation(SanitisedMarker); - bool NullTopic = !StringsDiffer(Topics.Category[i].Marker, "nullTopic"); - CopyStringToBuffer(&FilterTopics, + bool NullTopic = !StringsDiffer0(Topics.Category[i].Marker, "nullTopic"); + CopyStringToBuffer(&MenuBuffers.FilterTopics, "
\n" " %s\n" "
\n", - NullTopic ? "title=\"Annotations that don't fit into the above topic(s) may be filtered using this pseudo-topic\" " : "", + NullTopic ? "title=\"Timestamps that don't fit into the above topic(s) may be filtered using this pseudo-topic\" " : "", SanitisedMarker, SanitisedMarker, NullTopic ? "(null topic)" : Topics.Category[i].Marker); } - CopyStringToBuffer(&FilterTopics, + CopyStringToBuffer(&MenuBuffers.FilterTopics, "
\n"); - CopyBuffer(&FilterMenu, &FilterTopics); + CopyLandmarkedBuffer(&MenuBuffers.Filter, &MenuBuffers.FilterTopics, 0, PAGE_PLAYER); } if(Media.Count > 0) { - CopyStringToBuffer(&FilterMedia, + CopyStringToBuffer(&MenuBuffers.FilterMedia, "
\n" "
Media
\n"); for(int i = 0; i < Media.Count; ++i) { + // NOTE(matt): Stack-string char SanitisedMarker[StringLength(Media.Category[i].Marker) + 1]; CopyString(SanitisedMarker, sizeof(SanitisedMarker), "%s", Media.Category[i].Marker); SanitisePunctuation(SanitisedMarker); - int j; - for(j = 0; j < ArrayCount(CategoryMedium); ++j) - { - if(!StringsDiffer(Media.Category[i].Marker, CategoryMedium[j].Medium)) - { - break; - } - } + medium *Medium = GetMediumFromProject(CurrentProject, Wrap0i(Media.Category[i].Marker, sizeof(Media.Category[i].Marker))); + CopyStringToBuffer(&MenuBuffers.FilterMedia, + "
\n" + " ", + SanitisedMarker, Medium->Hidden ? " off" : ""); - if(!StringsDiffer(Media.Category[i].Marker, "afk")) // TODO(matt): Initially hidden config - // When we do this for real, we'll probably need to loop - // over the configured media to see who should be hidden - { - CopyStringToBuffer(&FilterMedia, - "
\n" - " %s%s\n" - "
\n", - SanitisedMarker, - CategoryMedium[j].Icon, - CategoryMedium[j].WrittenName); - } - else - { - CopyStringToBuffer(&FilterMedia, - "
\n" - " %s%s%s\n" - "
\n", - SanitisedMarker, - CategoryMedium[j].Icon, - CategoryMedium[j].WrittenName, - !StringsDiffer(Media.Category[i].Marker, DefaultMedium) ? "
🟉" : ""); - } + + PushIcon(&MenuBuffers.FilterMedia, FALSE, Medium->IconType, Medium->Icon, Medium->IconAsset, Medium->IconVariants, PAGE_PLAYER, &RequiresCineraJS); + + CopyStringToBuffer(&MenuBuffers.FilterMedia, "%.*s%s\n" + "
\n", + (int)Medium->Name.Length, Medium->Name.Base, + Medium == DefaultMedium ? "🟉" : ""); } - CopyStringToBuffer(&FilterMedia, + CopyStringToBuffer(&MenuBuffers.FilterMedia, "
\n"); - CopyBuffer(&FilterMenu, &FilterMedia); + CopyLandmarkedBuffer(&MenuBuffers.Filter, &MenuBuffers.FilterMedia, 0, PAGE_PLAYER); } - CopyStringToBuffer(&FilterMenu, + CopyStringToBuffer(&MenuBuffers.Filter, "
\n" "
\n" " \n"); - OffsetLandmarks(&CollationBuffers->Menus, ASSET_IMG_FILTER, PAGE_PLAYER); - CopyBuffer(&CollationBuffers->Menus, &FilterMenu); + CopyLandmarkedBuffer(&PlayerBuffers.Menus, &MenuBuffers.Filter, 0, PAGE_PLAYER); } - CopyStringToBuffer(&CollationBuffers->Menus, + CopyStringToBuffer(&PlayerBuffers.Menus, "
\n" "
🎭
\n" "
\n" @@ -5543,18 +9649,18 @@ AppendedIdentifier: "
\n" " 🔗\n" " \n" "
\n"); + bool HasCreditsMenu = MenuBuffers.Credits.Ptr > MenuBuffers.Credits.Location; if(HasCreditsMenu) { - OffsetLandmarksCredits(&CollationBuffers->Menus); - CopyBuffer(&CollationBuffers->Menus, &CreditsMenu); + CopyLandmarkedBuffer(&PlayerBuffers.Menus, &MenuBuffers.Credits, 0, PAGE_PLAYER); } - CopyStringToBuffer(&CollationBuffers->Menus, + CopyStringToBuffer(&PlayerBuffers.Menus, "
\n" " ?\n" "
\n" @@ -5568,7 +9674,7 @@ AppendedIdentifier: HasFilterMenu ? "" : " unavailable", HasFilterMenu ? "" : " unavailable"); - CopyStringToBuffer(&CollationBuffers->Menus, + CopyStringToBuffer(&PlayerBuffers.Menus, "\n" "

Menu toggling

\n" " q Quotes\n" @@ -5582,7 +9688,7 @@ AppendedIdentifier: HasFilterMenu ? "" : " unavailable", HasFilterMenu ? "" : " unavailable", HasCreditsMenu ? "" : " unavailable", HasCreditsMenu ? "" : " unavailable"); - CopyStringToBuffer(&CollationBuffers->Menus, + CopyStringToBuffer(&PlayerBuffers.Menus, "\n" "

In-Menu Movement

\n" "
\n" @@ -5619,7 +9725,7 @@ AppendedIdentifier: "
\n" "
\n"); - CopyStringToBuffer(&CollationBuffers->Menus, + CopyStringToBuffer(&PlayerBuffers.Menus, "

%sQuotes %sand%s References%s Menus%s

\n" " Enter Jump to timecode
\n", // Q R @@ -5636,7 +9742,7 @@ AppendedIdentifier: !HasQuoteMenu && !HasReferenceMenu ? "" : "", HasQuoteMenu || HasReferenceMenu ? "" : " unavailable", HasQuoteMenu || HasReferenceMenu ? "" : " unavailable"); - CopyStringToBuffer(&CollationBuffers->Menus, + CopyStringToBuffer(&PlayerBuffers.Menus, "\n" "

%sQuotes%s,%s References %sand%s Credits%s Menus%s

" " o Open URL (in new tab)\n", @@ -5662,7 +9768,7 @@ AppendedIdentifier: HasQuoteMenu || HasReferenceMenu || HasCreditsMenu ? "" : " unavailable", HasQuoteMenu || HasReferenceMenu || HasCreditsMenu ? "" : " unavailable"); - CopyStringToBuffer(&CollationBuffers->Menus, + CopyStringToBuffer(&PlayerBuffers.Menus, "\n" "

%sFilter Menu%s

\n" " x, Space Toggle category and focus next
\n" @@ -5679,7 +9785,7 @@ AppendedIdentifier: HasFilterMenu ? "" : "", HasFilterMenu ? "" : "", HasFilterMenu ? "" : "", HasFilterMenu ? "" : ""); - CopyStringToBuffer(&CollationBuffers->Menus, + CopyStringToBuffer(&PlayerBuffers.Menus, "\n" "

%sCredits Menu%s

\n" " Enter Open URL (in new tab)
\n", @@ -5687,49 +9793,49 @@ AppendedIdentifier: HasCreditsMenu ? "" : "", HasCreditsMenu ? "" : "", HasCreditsMenu ? "" : " unavailable", HasCreditsMenu ? "" : " unavailable"); - CopyStringToBuffer(&CollationBuffers->Menus, + CopyStringToBuffer(&PlayerBuffers.Menus, "
\n" "
\n" "
"); - CopyStringToBuffer(&CollationBuffers->Player, + CopyStringToBuffer(&PlayerBuffers.Main, "
\n"); if(N) { - N->This.LinkOffsets.NextStart = (CollationBuffers->Player.Ptr - CollationBuffers->Player.Location - (N->This.LinkOffsets.PrevStart + N->This.LinkOffsets.PrevEnd)); - if(N->Prev.Size || N->Next.Size) + N->WorkingThis.LinkOffsets.NextStart = (PlayerBuffers.Main.Ptr - PlayerBuffers.Main.Location - (N->WorkingThis.LinkOffsets.PrevStart + N->WorkingThis.LinkOffsets.PrevEnd)); + if((N->Prev && N->Prev->Size) || (N->Next && N->Next->Size)) { - if(N->Next.Size > 0) + if(N->Next && N->Next->Size > 0) { // TODO(matt): Once we have a more rigorous notion of "Day Numbers", perhaps also use them here buffer NextPlayerURL; - ClaimBuffer(&NextPlayerURL, "NextPlayerURL", MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_PLAYER_URL_PREFIX_LENGTH + MAX_BASE_FILENAME_LENGTH); - ConstructPlayerURL(&NextPlayerURL, N->Next.BaseFilename); - CopyStringToBuffer(&CollationBuffers->Player, + ClaimBuffer(&NextPlayerURL, BID_NEXT_PLAYER_URL, MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_BASE_FILENAME_LENGTH); + ConstructPlayerURL(&NextPlayerURL, N->Project, Wrap0i(N->Next->OutputLocation, sizeof(N->Next->OutputLocation))); + CopyStringToBuffer(&PlayerBuffers.Main, "
Next: '%s'
\n", NextPlayerURL.Location, - N->Next.Title); + N->Next->Title); DeclaimBuffer(&NextPlayerURL); } else { - CopyStringToBuffer(&CollationBuffers->Player, - "
You have arrived at the (current) end of %s
\n", CollationBuffers->ProjectName); + CopyStringToBuffer(&PlayerBuffers.Main, + "
You have arrived at the (current) end of %.*s
\n", (int)ProjectTitle.Length, ProjectTitle.Base); } } - N->This.LinkOffsets.NextEnd = (CollationBuffers->Player.Ptr - CollationBuffers->Player.Location - (N->This.LinkOffsets.PrevStart + N->This.LinkOffsets.PrevEnd + N->This.LinkOffsets.NextStart)); + N->WorkingThis.LinkOffsets.NextEnd = (PlayerBuffers.Main.Ptr - PlayerBuffers.Main.Location - (N->WorkingThis.LinkOffsets.PrevStart + N->WorkingThis.LinkOffsets.PrevEnd + N->WorkingThis.LinkOffsets.NextStart)); } - CopyStringToBuffer(&CollationBuffers->Player, + CopyStringToBuffer(&PlayerBuffers.Main, " \n" " "); buffer URLSearch; - ClaimBuffer(&URLSearch, "URLSearch", MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1); - ConstructSearchURL(&URLSearch); + ClaimBuffer(&URLSearch, BID_URL_SEARCH, MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1); + ConstructSearchURL(&URLSearch, CurrentProject); CopyString(CollationBuffers->URLSearch, sizeof(CollationBuffers->URLSearch), "%s", URLSearch.Location); DeclaimBuffer(&URLSearch); @@ -5749,7 +9855,7 @@ AppendedIdentifier: { for(int i = 0; i < Topics.Count; ++i) { - if(StringsDiffer(Topics.Category[i].Marker, "nullTopic")) + if(StringsDiffer0(Topics.Category[i].Marker, "nullTopic")) { CopyStringToBuffer(&CollationBuffers->IncludesPlayer, "%s, ", Topics.Category[i].Marker); } @@ -5768,69 +9874,120 @@ AppendedIdentifier: CopyStringToBuffer(&CollationBuffers->IncludesPlayer, "\">\n"); } - buffer URL; + buffer URL = {}; + URL.ID = BID_URL; - ConstructResolvedAssetURL(&URL, ASSET_CSS_CINERA, PAGE_PLAYER); + asset *CSSCinera = GetAsset(Wrap0(BuiltinAssets[ASSET_CSS_CINERA].Filename), ASSET_CSS); + ConstructResolvedAssetURL(&URL, CSSCinera, PAGE_PLAYER); CopyStringToBuffer(&CollationBuffers->IncludesPlayer, "\n" " IncludesPlayer, ASSET_CSS_CINERA, PAGE_PLAYER); + PushAssetLandmark(&CollationBuffers->IncludesPlayer, CSSCinera, PAGE_PLAYER, 0); - ConstructResolvedAssetURL(&URL, ASSET_CSS_THEME, PAGE_PLAYER); + string ThemeFilename = MakeString("sls", "cinera__", &CurrentProject->Theme, ".css"); + asset *CSSTheme = GetAsset(ThemeFilename, ASSET_CSS); + FreeString(&ThemeFilename); + ConstructResolvedAssetURL(&URL, CSSTheme, PAGE_PLAYER); CopyStringToBuffer(&CollationBuffers->IncludesPlayer, "\">\n" " IncludesPlayer, ASSET_CSS_THEME, PAGE_PLAYER); + PushAssetLandmark(&CollationBuffers->IncludesPlayer, CSSTheme, PAGE_PLAYER, 0); - ConstructResolvedAssetURL(&URL, ASSET_CSS_TOPICS, PAGE_PLAYER); + asset *CSSTopics = GetAsset(Wrap0(BuiltinAssets[ASSET_CSS_TOPICS].Filename), ASSET_CSS); + ConstructResolvedAssetURL(&URL, CSSTopics, PAGE_PLAYER); CopyStringToBuffer(&CollationBuffers->IncludesPlayer, "\">\n" " IncludesPlayer, ASSET_CSS_TOPICS, PAGE_PLAYER); + PushAssetLandmark(&CollationBuffers->IncludesPlayer, CSSTopics, PAGE_PLAYER, 0); CopyStringToBuffer(&CollationBuffers->IncludesPlayer, - "\">\n"); + "\">"); - ConstructResolvedAssetURL(&URL, ASSET_JS_PLAYER_PRE, PAGE_PLAYER); + if(BespokeTemplate->Metadata.TagCount > 0) + { + if(BespokeTemplate->Metadata.RequiresCineraJS) + { + RequiresCineraJS = TRUE; + } + } + else + { + if(CurrentProject->PlayerTemplate.Metadata.RequiresCineraJS) + { + RequiresCineraJS = TRUE; + } + } + + asset *JSCineraPre = GetAsset(Wrap0(BuiltinAssets[ASSET_JS_CINERA_PRE].Filename), ASSET_JS); + ConstructResolvedAssetURL(&URL, JSCineraPre, PAGE_PLAYER); CopyStringToBuffer(&CollationBuffers->IncludesPlayer, - " "); + + if(RequiresCineraJS) + { + asset *JSCineraPost = GetAsset(Wrap0(BuiltinAssets[ASSET_JS_CINERA_POST].Filename), ASSET_JS); + ConstructResolvedAssetURL(&URL, JSCineraPost, PAGE_PLAYER); + CopyStringToBuffer(&CollationBuffers->IncludesPlayer, + "\n "); + } + + asset *JSPlayerPre = GetAsset(Wrap0(BuiltinAssets[ASSET_JS_PLAYER_PRE].Filename), ASSET_JS); + ConstructResolvedAssetURL(&URL, JSPlayerPre, PAGE_PLAYER); + CopyStringToBuffer(&CollationBuffers->IncludesPlayer, + "\n "); - ConstructResolvedAssetURL(&URL, ASSET_JS_PLAYER_POST, PAGE_PLAYER); - CopyStringToBuffer(&CollationBuffers->ScriptPlayer, + asset *JSPlayerPost = GetAsset(Wrap0(BuiltinAssets[ASSET_JS_PLAYER_POST].Filename), ASSET_JS); + ConstructResolvedAssetURL(&URL, JSPlayerPost, PAGE_PLAYER); + CopyStringToBuffer(&PlayerBuffers.Script, ""); - // NOTE(matt): Tree structure of "global" buffer dependencies - // CreditsMenu - // FilterMedia - // FilterTopics - // FilterMenu - // ReferenceMenu - // QuoteMenu + CopyLandmarkedBuffer(&CollationBuffers->Player, &PlayerBuffers.Menus, 0, PAGE_PLAYER); + CopyStringToBuffer(&CollationBuffers->Player, "\n" + " "); + CopyLandmarkedBuffer(&CollationBuffers->Player, &PlayerBuffers.Main, &N->WorkingThis.LinkOffsets.PrevStart, PAGE_PLAYER); + CopyStringToBuffer(&CollationBuffers->Player, "\n" + " "); + CopyStringToBuffer(&CollationBuffers->Player, "\n" + " "); + CopyLandmarkedBuffer(&CollationBuffers->Player, &PlayerBuffers.Script, 0, PAGE_PLAYER); - DeclaimBuffer(&CreditsMenu); - DeclaimBuffer(&FilterMedia); - DeclaimBuffer(&FilterTopics); - DeclaimBuffer(&FilterMenu); - DeclaimBuffer(&ReferenceMenu); - DeclaimBuffer(&QuoteMenu); + // NOTE(matt): Tree structure of "global" buffer dependencies + // MenuBuffers.Credits + // MenuBuffers.FilterMedia + // MenuBuffers.FilterTopics + // MenuBuffers.Filter + // MenuBuffers.Reference + // MenuBuffers.Quote + DeclaimMenuBuffers(&MenuBuffers); + DeclaimPlayerBuffers(&PlayerBuffers); } else { @@ -5844,50 +10001,65 @@ AppendedIdentifier: } int -BuffersToHTML(buffers *CollationBuffers, template *Template, char *OutputPath, int PageType, unsigned int *PlayerOffset) +BuffersToHTML(buffers *CollationBuffers, template *Template, char *OutputPath, page_type PageType, unsigned int *PlayerOffset) { + MEM_TEST_INITIAL(); #if DEBUG printf("\n\n --- Buffer Collation ---\n" - " %s\n\n\n", OutputPath ? OutputPath : Config.OutLocation); + " %s\n\n\n", OutputPath ? OutputPath : CurrentProject->OutLocation); #endif #if DEBUG_MEM FILE *MemLog = fopen("/home/matt/cinera_mem", "a+"); - fprintf(MemLog, "\nEntered BuffersToHTML(%s)\n", OutputPath ? OutputPath : Config.OutLocation); + fprintf(MemLog, "\nEntered BuffersToHTML("); + if(OutputPath) + { + fprintf(MemLog, "%s", OutputPath); + } + else + { + fprintf(MemLog, "%.*s", (int)CurrentProject->PlayerLocation.Length, CurrentProject->PlayerLocation.Base); + } + fprintf(MemLog, ")\n"); fclose(MemLog); #endif + // TODO(matt): Consider straight up enforcing that a template is set and available, and that we do not attempt to + // generate a sort of bare default .HTML file + + // TODO(matt): Redo this bit to use the AppendStringToBuffer() sort of stuff if(Template->File.Buffer.Location) { - if((Template->Metadata.Validity & PageType) || Config.Mode & MODE_FORCEINTEGRATION) +#if AFD + if((Template->Metadata.Validity & PageType))// || CurrentProject->Mode & MODE_FORCEINTEGRATION) { - buffer Output; - Output.Size = Template->File.Buffer.Size + (Kilobytes(512)); - Output.ID = "Output"; - if(!(Output.Location = malloc(Output.Size))) + buffer Master; + Master.Size = Template->File.Buffer.Size + (Kilobytes(512)); + Master.ID = BID_MASTER; + if(!(Master.Location = malloc(Master.Size))) { LogError(LOG_ERROR, "BuffersToHTML(): %s", strerror(errno)); + MEM_TEST_AFTER("BuffersToHTML"); return RC_ERROR_MEMORY; } #if DEBUG_MEM MemLog = fopen("/home/matt/cinera_mem", "a+"); - fprintf(MemLog, " Allocated Output (%d)\n", Output.Size); + fprintf(MemLog, " Allocated Master (%ld)\n", Master.Size); fclose(MemLog); - printf(" Allocated Output (%d)\n", Output.Size); + printf(" Allocated Master (%ld)\n", Master.Size); #endif - Output.Ptr = Output.Location; + Master.Ptr = Master.Location; - bool NeedPlayerOffset = PlayerOffset ? TRUE : FALSE; Template->File.Buffer.Ptr = Template->File.Buffer.Location; for(int i = 0; i < Template->Metadata.TagCount; ++i) { int j = 0; while(Template->Metadata.Tags[i].Offset > j) { - *Output.Ptr++ = *Template->File.Buffer.Ptr++; + *Master.Ptr++ = *Template->File.Buffer.Ptr++; ++j; } @@ -5895,189 +10067,219 @@ BuffersToHTML(buffers *CollationBuffers, template *Template, char *OutputPath, i // or sanitise punctuation for CSS-safety switch(Template->Metadata.Tags[i].TagCode) { - case TAG_PROJECT_ID: - if(CollationBuffers->ProjectID[0] == '\0') - { - fprintf(stderr, "Template contains a tag\n" - "Skipping just this tag, because no project ID is set\n"); - } - else - { - CopyStringToBufferNoFormat(&Output, CollationBuffers->ProjectID); // NOTE(matt): Not HTML-safe - } - break; case TAG_PROJECT: - if(CollationBuffers->ProjectName[0] == '\0') { - fprintf(stderr, "Template contains a tag\n" - "Skipping just this tag, because we do not know the project's full name\n"); - } - else - { - CopyStringToBufferHTMLSafe(&Output, CollationBuffers->ProjectName); - } - break; - case TAG_SEARCH_URL: CopyStringToBufferNoFormat(&Output, CollationBuffers->URLSearch); break; // NOTE(matt): Not HTML-safe - case TAG_THEME: CopyStringToBufferNoFormat(&Output, CollationBuffers->Theme); break; // NOTE(matt): Not HTML-safe - case TAG_TITLE: CopyStringToBufferHTMLSafe(&Output, CollationBuffers->Title); break; + if(CurrentProject->HTMLTitle.Length > 0) { CopyStringToBufferNoFormat(&Master, CurrentProject->HTMLTitle); } // NOTE(matt): Not HTML-safe + else { CopyStringToBufferHTMLSafe(&Master, CurrentProject->Title); } + } break; + case TAG_PROJECT_ID: CopyStringToBufferNoFormat(&Master, CurrentProject->ID); break; // NOTE(matt): Not HTML-safe + case TAG_PROJECT_PLAIN: CopyStringToBufferHTMLSafe(&Master, CurrentProject->Title); break; + case TAG_SEARCH_URL: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->URLSearch, sizeof(CollationBuffers->URLSearch))); break; // NOTE(matt): Not HTML-safe + case TAG_THEME: CopyStringToBufferNoFormat(&Master, CurrentProject->Theme); break; // NOTE(matt): Not HTML-safe + case TAG_TITLE: CopyStringToBufferHTMLSafe(&Master, Wrap0i(CollationBuffers->Title, sizeof(CollationBuffers->Title))); break; case TAG_URL: if(PageType == PAGE_PLAYER) { - CopyStringToBufferNoFormat(&Output, CollationBuffers->URLPlayer); + CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->URLPlayer, sizeof(CollationBuffers->URLPlayer))); } else { - CopyStringToBufferNoFormat(&Output, CollationBuffers->URLSearch); + CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->URLSearch, sizeof(CollationBuffers->URLSearch))); } break; - case TAG_VIDEO_ID: CopyStringToBufferNoFormat(&Output, CollationBuffers->VideoID); break; - case TAG_VOD_PLATFORM: CopyStringToBufferNoFormat(&Output, CollationBuffers->VODPlatform); break; + case TAG_VIDEO_ID: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->VideoID, sizeof(CollationBuffers->VideoID))); break; + case TAG_VOD_PLATFORM: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->VODPlatform, sizeof(CollationBuffers->VODPlatform))); break; case TAG_SEARCH: - if(Config.Edition == EDITION_SINGLE) { - fprintf(stderr, "Template contains a tag\n" - "Skipping just this tag, because a search cannot be generated for inclusion in a\n" - "bespoke template in Single Edition\n"); - } - else - { - OffsetLandmarks(&Output, ASSET_JS_SEARCH, PAGE_SEARCH); - CopyBuffer(&Output, &CollationBuffers->Search); - } - break; + CopyLandmarkedBuffer(&Master, &CollationBuffers->Search, 0, PageType); + } break; case TAG_INCLUDES: if(PageType == PAGE_PLAYER) { - OffsetLandmarksIncludes(&Output, PageType); - CopyBuffer(&Output, &CollationBuffers->IncludesPlayer); + CopyLandmarkedBuffer(&Master, &CollationBuffers->IncludesPlayer, 0, PageType); } else { - OffsetLandmarksIncludes(&Output, PageType); - CopyBuffer(&Output, &CollationBuffers->IncludesSearch); + CopyLandmarkedBuffer(&Master, &CollationBuffers->IncludesSearch, 0, PageType); } break; - case TAG_MENUS: - OffsetLandmarksMenus(&Output); - CopyBuffer(&Output, &CollationBuffers->Menus); - break; case TAG_PLAYER: - if(NeedPlayerOffset) { *PlayerOffset += (Output.Ptr - Output.Location); NeedPlayerOffset = !NeedPlayerOffset; } - CopyBuffer(&Output, &CollationBuffers->Player); - break; - case TAG_SCRIPT: - OffsetLandmarks(&Output, ASSET_JS_PLAYER_POST, PAGE_PLAYER); - CopyBuffer(&Output, &CollationBuffers->ScriptPlayer); + CopyLandmarkedBuffer(&Master, &CollationBuffers->Player, PlayerOffset, PageType); break; case TAG_ASSET: { buffer URL; - ConstructResolvedAssetURL(&URL, Template->Metadata.Tags[i].AssetIndex, PageType); - CopyStringToBuffer(&Output, "%s", URL.Location); + ConstructResolvedAssetURL(&URL, Template->Metadata.Tags[i].Asset, PageType); + CopyStringToBuffer(&Master, "%s", URL.Location); DeclaimBuffer(&URL); - PushAssetLandmark(&Output, Template->Metadata.Tags[i].AssetIndex, PageType); + PushAssetLandmark(&Master, Template->Metadata.Tags[i].Asset, PageType, 0); } break; case TAG_CSS: { buffer URL; - ConstructResolvedAssetURL(&URL, Template->Metadata.Tags[i].AssetIndex, PageType); - CopyStringToBuffer(&Output, + ConstructResolvedAssetURL(&URL, Template->Metadata.Tags[i].Asset, PageType); + CopyStringToBuffer(&Master, "Metadata.Tags[i].AssetIndex, PageType); - CopyStringToBuffer(&Output, "\">"); + PushAssetLandmark(&Master, Template->Metadata.Tags[i].Asset, PageType, 0); + CopyStringToBuffer(&Master, "\">"); } break; case TAG_IMAGE: { buffer URL; - ConstructResolvedAssetURL(&URL, Template->Metadata.Tags[i].AssetIndex, PageType); - CopyStringToBuffer(&Output, "%s", URL.Location); + ConstructResolvedAssetURL(&URL, Template->Metadata.Tags[i].Asset, PageType); + CopyStringToBuffer(&Master, "%s", URL.Location); DeclaimBuffer(&URL); - PushAssetLandmark(&Output, Template->Metadata.Tags[i].AssetIndex, PageType); + PushAssetLandmark(&Master, Template->Metadata.Tags[i].Asset, PageType, 0); } break; case TAG_JS: { buffer URL; - ConstructResolvedAssetURL(&URL, Template->Metadata.Tags[i].AssetIndex, PageType); - CopyStringToBuffer(&Output, + ConstructResolvedAssetURL(&URL, Template->Metadata.Tags[i].Asset, PageType); + CopyStringToBuffer(&Master, ""); + PushAssetLandmark(&Master, Template->Metadata.Tags[i].Asset, PageType, 0); + CopyStringToBuffer(&Master, "\">"); } break; - case TAG_CUSTOM0: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom0); break; - case TAG_CUSTOM1: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom1); break; - case TAG_CUSTOM2: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom2); break; - case TAG_CUSTOM3: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom3); break; - case TAG_CUSTOM4: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom4); break; - case TAG_CUSTOM5: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom5); break; - case TAG_CUSTOM6: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom6); break; - case TAG_CUSTOM7: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom7); break; - case TAG_CUSTOM8: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom8); break; - case TAG_CUSTOM9: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom9); break; - case TAG_CUSTOM10: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom10); break; - case TAG_CUSTOM11: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom11); break; - case TAG_CUSTOM12: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom12); break; - case TAG_CUSTOM13: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom13); break; - case TAG_CUSTOM14: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom14); break; - case TAG_CUSTOM15: CopyStringToBufferNoFormat(&Output, CollationBuffers->Custom15); break; + case TAG_NAV: + { + buffer *NavDropdownPre; + buffer *NavHorizontalPre; + buffer *NavPlainPre; + buffer *NavGeneric; + buffer *NavDropdownPost; + if(Template->Metadata.Type == TEMPLATE_GLOBAL_SEARCH) + { + NavDropdownPre = &Config->NavDropdownPre; + NavHorizontalPre = &Config->NavHorizontalPre; + NavPlainPre = &Config->NavPlainPre; + NavGeneric = &Config->NavGeneric; + NavDropdownPost = &Config->NavDropdownPost; + } + else + { + NavDropdownPre = &CurrentProject->NavDropdownPre; + NavHorizontalPre = &CurrentProject->NavHorizontalPre; + NavPlainPre = &CurrentProject->NavPlainPre; + NavGeneric = &CurrentProject->NavGeneric; + NavDropdownPost = &CurrentProject->NavDropdownPost; + } + + switch(Template->Metadata.Tags[i].NavigationType) + { + case NT_DROPDOWN: + { + CopyLandmarkedBuffer(&Master, NavDropdownPre, 0, PageType); + } // NOTE(matt): Intentional fall-through + case NT_HORIZONTAL: + { + CopyLandmarkedBuffer(&Master, NavHorizontalPre, 0, PageType); + } break; + case NT_PLAIN: + { + CopyLandmarkedBuffer(&Master, NavPlainPre, 0, PageType); + } break; + default: break; + } + CopyLandmarkedBuffer(&Master, NavGeneric, 0, PageType); + if(Template->Metadata.Tags[i].NavigationType == NT_DROPDOWN) + { + CopyLandmarkedBuffer(&Master, NavDropdownPost, 0, PageType); + } + } + case TAG_CUSTOM0: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom0, sizeof(CollationBuffers->Custom0))); break; + case TAG_CUSTOM1: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom1, sizeof(CollationBuffers->Custom1))); break; + case TAG_CUSTOM2: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom2, sizeof(CollationBuffers->Custom2))); break; + case TAG_CUSTOM3: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom3, sizeof(CollationBuffers->Custom3))); break; + case TAG_CUSTOM4: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom4, sizeof(CollationBuffers->Custom4))); break; + case TAG_CUSTOM5: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom5, sizeof(CollationBuffers->Custom5))); break; + case TAG_CUSTOM6: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom6, sizeof(CollationBuffers->Custom6))); break; + case TAG_CUSTOM7: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom7, sizeof(CollationBuffers->Custom7))); break; + case TAG_CUSTOM8: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom8, sizeof(CollationBuffers->Custom8))); break; + case TAG_CUSTOM9: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom9, sizeof(CollationBuffers->Custom9))); break; + case TAG_CUSTOM10: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom10, sizeof(CollationBuffers->Custom10))); break; + case TAG_CUSTOM11: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom11, sizeof(CollationBuffers->Custom11))); break; + case TAG_CUSTOM12: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom12, sizeof(CollationBuffers->Custom12))); break; + case TAG_CUSTOM13: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom13, sizeof(CollationBuffers->Custom13))); break; + case TAG_CUSTOM14: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom14, sizeof(CollationBuffers->Custom14))); break; + case TAG_CUSTOM15: CopyStringToBufferNoFormat(&Master, Wrap0i(CollationBuffers->Custom15, sizeof(CollationBuffers->Custom15))); break; } DepartComment(&Template->File.Buffer); } while(Template->File.Buffer.Ptr - Template->File.Buffer.Location < Template->File.Buffer.Size) { - *Output.Ptr++ = *Template->File.Buffer.Ptr++; + *Master.Ptr++ = *Template->File.Buffer.Ptr++; } FILE *OutFile; - if(!(OutFile = fopen(Config.Edition == EDITION_PROJECT ? OutputPath : Config.OutIntegratedLocation, "w"))) + if(!(OutFile = fopen(OutputPath, "w"))) { - LogError(LOG_ERROR, "Unable to open output file %s: %s", Config.Edition == EDITION_PROJECT ? OutputPath : Config.OutIntegratedLocation, strerror(errno)); - FreeBuffer(&Output); + LogError(LOG_ERROR, "Unable to open output file %s: %s", OutputPath, strerror(errno)); + FreeBuffer(&Master); #if DEBUG_MEM MemLog = fopen("/home/matt/cinera_mem", "a+"); - fprintf(MemLog, " Freed Output\n"); + fprintf(MemLog, " Freed Master\n"); fclose(MemLog); - printf(" Freed Output\n"); + printf(" Freed Master\n"); #endif + MEM_TEST_AFTER("BuffersToHTML"); return RC_ERROR_FILE; } - fwrite(Output.Location, Output.Ptr - Output.Location, 1, OutFile); + fwrite(Master.Location, Master.Ptr - Master.Location, 1, OutFile); fclose(OutFile); - FreeBuffer(&Output); + FreeBuffer(&Master); #if DEBUG_MEM MemLog = fopen("/home/matt/cinera_mem", "a+"); - fprintf(MemLog, " Freed Output\n"); + fprintf(MemLog, " Freed Master\n"); fclose(MemLog); - printf(" Freed Output\n"); + printf(" Freed Master\n"); #endif + MEM_TEST_AFTER("BuffersToHTML"); return RC_SUCCESS; } else { + MEM_TEST_AFTER("BuffersToHTML"); return RC_INVALID_TEMPLATE; } +#endif // AFE + MEM_TEST_AFTER("BuffersToHTML"); + return RC_SUCCESS; // NOTE(matt): We added this simply to squash the "end of non-void function" warning } else { - buffer Master; - if(ClaimBuffer(&Master, "Master", Kilobytes(512)) == RC_ARENA_FULL) { return RC_ARENA_FULL; }; + MEM_TEST_MID("BuffersToHTML1"); + buffer Master = {}; + Master.Size = Kilobytes(512); + Master.ID = BID_MASTER; + MEM_TEST_MID("BuffersToHTML2"); + if(!(Master.Location = malloc(Master.Size))) + { + LogError(LOG_ERROR, "BuffersToHTML(): %s", + strerror(errno)); + MEM_TEST_AFTER("BuffersToHTML"); + return RC_ERROR_MEMORY; + } + MEM_TEST_MID("BuffersToHTML3"); + Master.Ptr = Master.Location; CopyStringToBuffer(&Master, "\n" " \n" " "); - OffsetLandmarksIncludes(&Master, PageType); - CopyBuffer(&Master, PageType == PAGE_PLAYER ? &CollationBuffers->IncludesPlayer : &CollationBuffers->IncludesSearch); + MEM_TEST_MID("BuffersToHTML4"); + CopyLandmarkedBuffer(&Master, PageType == PAGE_PLAYER ? &CollationBuffers->IncludesPlayer : &CollationBuffers->IncludesSearch, 0, PageType); + MEM_TEST_MID("BuffersToHTML5"); CopyStringToBuffer(&Master, "\n"); CopyStringToBuffer(&Master, @@ -6089,27 +10291,15 @@ BuffersToHTML(buffers *CollationBuffers, template *Template, char *OutputPath, i CopyStringToBuffer(&Master, "
\n" " "); - OffsetLandmarksMenus(&Master); - CopyBuffer(&Master, &CollationBuffers->Menus); - CopyStringToBuffer(&Master, "\n" - " "); - - if(PlayerOffset) { *PlayerOffset += Master.Ptr - Master.Location; } - - CopyBuffer(&Master, &CollationBuffers->Player); - CopyStringToBuffer(&Master, "\n" - " "); - CopyStringToBuffer(&Master, "
\n" - " "); - - OffsetLandmarks(&Master, ASSET_JS_PLAYER_POST, PAGE_PLAYER); - CopyBuffer(&Master, &CollationBuffers->ScriptPlayer); + CopyLandmarkedBuffer(&Master, &CollationBuffers->Player, PlayerOffset, PageType); + MEM_TEST_MID("BuffersToHTML9"); CopyStringToBuffer(&Master, "\n"); } else { - OffsetLandmarks(&Master, ASSET_JS_SEARCH, PAGE_SEARCH); - CopyBuffer(&Master, &CollationBuffers->Search); + MEM_TEST_MID("BuffersToHTML10"); + CopyLandmarkedBuffer(&Master, &CollationBuffers->Search, 0, PageType); + MEM_TEST_MID("BuffersToHTML11"); } CopyStringToBuffer(&Master, @@ -6117,70 +10307,116 @@ BuffersToHTML(buffers *CollationBuffers, template *Template, char *OutputPath, i "\n"); FILE *OutFile; - if(!(OutFile = fopen(Config.Edition == EDITION_PROJECT ? OutputPath : Config.OutLocation, "w"))) + MEM_TEST_MID("BuffersToHTML12"); + if(!(OutFile = fopen(OutputPath, "w"))) { - LogError(LOG_ERROR, "Unable to open output file %s: %s", Config.Edition == EDITION_PROJECT ? OutputPath : Config.OutLocation, strerror(errno)); + LogError(LOG_ERROR, "Unable to open output file %s: %s", OutputPath, strerror(errno)); DeclaimBuffer(&Master); + MEM_TEST_AFTER("BuffersToHTML"); return RC_ERROR_FILE; } + MEM_TEST_MID("BuffersToHTML13"); fwrite(Master.Location, Master.Ptr - Master.Location, 1, OutFile); + MEM_TEST_MID("BuffersToHTML13"); fclose(OutFile); - DeclaimBuffer(&Master); + MEM_TEST_MID("BuffersToHTML14"); + OutFile = 0; + FreeBuffer(&Master); + MEM_TEST_AFTER("BuffersToHTML"); return RC_SUCCESS; } } int -BinarySearchForMetadataEntry(db_entry **Entry, char *SearchTerm) +BinarySearchForMetadataEntry(db_header_project *Header, db_entry **Entry, string SearchTerm) { - DB.Metadata.Buffer.Ptr = DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader); + char *Ptr = (char *)Header; + Ptr += sizeof(*Header); + db_entry *FirstEntry = (db_entry *)Ptr; int Lower = 0; - db_entry *LowerEntry = (db_entry*)(DB.Metadata.Buffer.Ptr + sizeof(DB.Entry) * Lower); - if(StringsDiffer(SearchTerm, LowerEntry->BaseFilename) < 0 ) { return -1; } + db_entry *LowerEntry = FirstEntry + Lower; + if(StringsDiffer(SearchTerm, Wrap0i(LowerEntry->HMMLBaseFilename, sizeof(LowerEntry->HMMLBaseFilename))) < 0 ) { return -1; } - int Upper = DB.EntriesHeader.Count - 1; + int Upper = Header->EntryCount - 1; int Pivot = Upper - ((Upper - Lower) >> 1); db_entry *UpperEntry; db_entry *PivotEntry; do { - LowerEntry = (db_entry*)(DB.Metadata.Buffer.Ptr + sizeof(DB.Entry) * Lower); - PivotEntry = (db_entry*)(DB.Metadata.Buffer.Ptr + sizeof(DB.Entry) * Pivot); - UpperEntry = (db_entry*)(DB.Metadata.Buffer.Ptr + sizeof(DB.Entry) * Upper); + LowerEntry = FirstEntry + Lower; + PivotEntry = FirstEntry + Pivot; + UpperEntry = FirstEntry + Upper; - if(!StringsDiffer(SearchTerm, LowerEntry->BaseFilename)) { *Entry = LowerEntry; return Lower; } - if(!StringsDiffer(SearchTerm, PivotEntry->BaseFilename)) { *Entry = PivotEntry; return Pivot; } - if(!StringsDiffer(SearchTerm, UpperEntry->BaseFilename)) { *Entry = UpperEntry; return Upper; } + if(!StringsDiffer(SearchTerm, Wrap0i(LowerEntry->HMMLBaseFilename, sizeof(LowerEntry->HMMLBaseFilename)))) { *Entry = LowerEntry; return Lower; } + if(!StringsDiffer(SearchTerm, Wrap0i(PivotEntry->HMMLBaseFilename, sizeof(PivotEntry->HMMLBaseFilename)))) { *Entry = PivotEntry; return Pivot; } + if(!StringsDiffer(SearchTerm, Wrap0i(UpperEntry->HMMLBaseFilename, sizeof(UpperEntry->HMMLBaseFilename)))) { *Entry = UpperEntry; return Upper; } - if((StringsDiffer(SearchTerm, PivotEntry->BaseFilename) < 0)) { Upper = Pivot; } + if(StringsDiffer(SearchTerm, Wrap0i(PivotEntry->HMMLBaseFilename, sizeof(PivotEntry->HMMLBaseFilename))) < 0) { Upper = Pivot; } else { Lower = Pivot; } Pivot = Upper - ((Upper - Lower) >> 1); } while(Upper > Pivot); return Upper; } -int -AccumulateDBEntryInsertionOffset(int EntryIndex) +void +InitIndexFile(project *P) { - int Result = 0; - db_entry *AccEntry = { 0 }; - if(EntryIndex < DB.EntriesHeader.Count >> 1) + DB.File.Buffer.ID = BID_DATABASE; + DB.File = InitFile(&P->BaseDir, &P->ID, EXT_INDEX); + ReadFileIntoBuffer(&DB.File); // NOTE(matt): Could we actually catch errors (permissions?) here and bail? + if(!DB.File.Buffer.Location) { + char *BaseDir0 = MakeString0("l", &P->BaseDir); + DIR *OutputDirectoryHandle = opendir(BaseDir0); + if(!OutputDirectoryHandle) + { + if(!MakeDir(P->BaseDir)) + { + LogError(LOG_ERROR, "Unable to create directory %.*s: %s", (int)P->BaseDir.Length, P->BaseDir.Base, strerror(errno)); + fprintf(stderr, "Unable to create directory %.*s: %s\n", (int)P->BaseDir.Length, P->BaseDir.Base, strerror(errno)); + Free(BaseDir0); + return; + }; + } + Free(BaseDir0); + closedir(OutputDirectoryHandle); + + DB.File.Handle = fopen(DB.File.Path, "w"); + fprintf(DB.File.Handle, "---\n"); + fclose(DB.File.Handle); + ReadFileIntoBuffer(&DB.File); + } +} + +uint64_t +AccumulateDBEntryInsertionOffset(db_header_project *Header, int EntryIndex) +{ + uint64_t Result = 0; + char *Ptr = (char *)Header; + Ptr += sizeof(*Header); + db_entry *Entry = (db_entry *)Ptr + EntryIndex; + if(EntryIndex < Header->EntryCount >> 1) + { + --Entry; for(; EntryIndex > 0; --EntryIndex) { - AccEntry = (db_entry*)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * (EntryIndex - 1)); - Result += AccEntry->Size; + Result += Entry->Size; + --Entry; } Result += StringLength("---\n"); } else { - for(; EntryIndex < DB.EntriesHeader.Count; ++EntryIndex) + for(; EntryIndex < Header->EntryCount; ++EntryIndex) { - AccEntry = (db_entry*)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * EntryIndex); - Result += AccEntry->Size; + Result += Entry->Size; + ++Entry; } - Result = DB.File.FileSize - Result; + if(!DB.File.Buffer.Location) + { + InitIndexFile(CurrentProject); + } + Result = DB.File.Buffer.Size - Result; } return Result; } @@ -6188,18 +10424,24 @@ AccumulateDBEntryInsertionOffset(int EntryIndex) void ClearEntry(db_entry *Entry) { - Entry->LinkOffsets.PrevStart = 0; - Entry->LinkOffsets.NextStart = 0; - Entry->LinkOffsets.PrevEnd = 0; - Entry->LinkOffsets.NextEnd = 0; - Entry->Size = 0; - Clear(Entry->BaseFilename, sizeof(Entry->BaseFilename)); - Clear(Entry->Title, sizeof(Entry->Title)); + db_entry Zero = {}; + *Entry = Zero; + Entry->ArtIndex = SAI_UNSET; } void -InitNeighbourhood(neighbourhood *N) +ResetNeighbourhood(neighbourhood *N) { + N->Prev = 0; + N->This = 0; + N->Next = 0; + + DB.Metadata.Signposts.Prev.Ptr = N->Prev; + DB.Metadata.Signposts.This.Ptr = N->This; + DB.Metadata.Signposts.Next.Ptr = N->Next; + + ClearEntry(&N->WorkingThis); + N->PrevIndex = -1; N->PreDeletionThisIndex = N->ThisIndex = 0; N->NextIndex = -1; @@ -6208,20 +10450,94 @@ InitNeighbourhood(neighbourhood *N) N->PrevOffsetModifier = N->ThisOffsetModifier = N->NextOffsetModifier = 0; N->FormerIsFirst = N->LatterIsFinal = FALSE; N->DeletedEntryWasFirst = N->DeletedEntryWasFinal = FALSE; +} - N->Prev.LinkOffsets.PrevStart = N->Prev.LinkOffsets.PrevEnd = N->Prev.LinkOffsets.NextStart = N->Prev.LinkOffsets.NextEnd = 0; - N->This.LinkOffsets.PrevStart = N->This.LinkOffsets.PrevEnd = N->This.LinkOffsets.NextStart = N->This.LinkOffsets.NextEnd = 0; - N->Next.LinkOffsets.PrevStart = N->Next.LinkOffsets.PrevEnd = N->Next.LinkOffsets.NextStart = N->Next.LinkOffsets.NextEnd = 0; +void +InitNeighbourhood(neighbourhood *N) +{ + ResetNeighbourhood(N); + // TODO(matt): Does this whole thing actually have to happen? Wouldn't it be enough simply to set N->PrevIndex and + // N->NextIndex to -1? - N->Prev.Size = N->This.Size = N->Next.Size = 0; + // TODO(matt): This is completely bogus! Probably take the project_index, so we can get the correct neighbourhood? + // AFD - Clear(N->Prev.BaseFilename, sizeof(N->Prev.BaseFilename)); - Clear(N->This.BaseFilename, sizeof(N->This.BaseFilename)); - Clear(N->Next.BaseFilename, sizeof(N->Next.BaseFilename)); + // TODO(matt): To get us going, I think we want to make this first call LocateBlock(), and initialise a PROJ block if that + // fails. + // + // After having located / initialised that PROJ block, we should then try and locate the Project itself, in + // turn initialising one if we fail to locate it + // + // So we'll be doing some of the work of InitDB() + //PrintConfig(Config); + //CurrentProject = GetProjectFromProject(GetProjectFromProject(GetProjectFromConfig(Config, Wrap0("riscy")), Wrap0("book")), Wrap0("reader")); - Clear(N->Prev.Title, sizeof(N->Prev.Title)); - Clear(N->This.Title, sizeof(N->This.Title)); - Clear(N->Next.Title, sizeof(N->Next.Title)); + + // TODO(matt): Why is this Generation off by one? riscy/book/reader should be 2:1, but we're seeing it here as 3:1 +#if DEBUG_PROJECT_INDICES + fprintf(stderr, "\n%u: %u\n", CurrentProject->Index.Generation, CurrentProject->Index.Index); +#endif + //_exit(0); + + N->Project = LocateProject(CurrentProject->Index); + if(!N->Project) + { + Colourise(CS_ERROR); + fprintf(stderr, "Project %u:%u could not be located in DB\n", CurrentProject->Index.Generation, CurrentProject->Index.Index); + Colourise(CS_END); + _exit(1); + } + + if(StringsDiffer(CurrentProject->ID, Wrap0i(N->Project->ID, sizeof(N->Project->ID)))) + { + string StoredID = Wrap0i(N->Project->ID, sizeof(N->Project->ID)); + Colourise(CS_ERROR); + fprintf(stderr, "Project in DB located by ProjectIndex (%.*s) does not match the project got from the config (%.*s)\n", (int)StoredID.Length, StoredID.Base, (int)CurrentProject->ID.Length, CurrentProject->ID.Base); + Colourise(CS_END); + _exit(1); + } + else + { +#if DEBUG_PROJECT_INDICES + Colourise(CS_SUCCESS); + fprintf(stderr, "Project \"%.*s\" [%u:%u] found. Proceeding!\n", (int)CurrentProject->Lineage.Length, CurrentProject->Lineage.Base, CurrentProject->Index.Generation, CurrentProject->Index.Index); + Colourise(CS_END); +#endif + } + + //_exit(1); + + DB.Metadata.Signposts.ProjectHeader.Ptr = N->Project; +} + +char *NeighbourhoodMemberStrings[] = +{ + "Prev", + "This", + "Working This", + "Next" +}; + +typedef enum +{ + NM_PREV, + NM_THIS, + NM_WORKING_THIS, + NM_NEXT +} neighbourhood_member; + +void +PrintEntry(db_entry *E, int16_t EIndex, neighbourhood_member M) +{ + fprintf(stderr, + " %s [%d]: %s: %6d %6d %6d %6d\n", + NeighbourhoodMemberStrings[M], + EIndex, + E->HMMLBaseFilename, + E->LinkOffsets.PrevStart, + E->LinkOffsets.PrevEnd, + E->LinkOffsets.NextStart, + E->LinkOffsets.NextEnd); } #define PrintNeighbourhood(N) PrintNeighbourhood_(N, __LINE__) @@ -6231,40 +10547,24 @@ PrintNeighbourhood_(neighbourhood *N, int Line) printf( "\n" " Neighbourhood (line %d):\n", Line); - if(N->PrevIndex >= 0) + if(N->Prev) { - printf( - " Prev [%d]: %s: %6d %6d %6d %6d\n", - N->PrevIndex, N->Prev.BaseFilename, - N->Prev.LinkOffsets.PrevStart, - N->Prev.LinkOffsets.PrevEnd, - N->Prev.LinkOffsets.NextStart, - N->Prev.LinkOffsets.NextEnd); + PrintEntry(N->Prev, N->PrevIndex, NM_PREV); } - if(N->ThisIndex >= 0) + PrintEntry(&N->WorkingThis, N->ThisIndex, NM_WORKING_THIS); + if(N->This) { - printf( - " This [%d (pre-deletion %d)]: %s: %6d %6d %6d %6d\n", - N->ThisIndex, N->PreDeletionThisIndex, N->This.BaseFilename, - N->This.LinkOffsets.PrevStart, - N->This.LinkOffsets.PrevEnd, - N->This.LinkOffsets.NextStart, - N->This.LinkOffsets.NextEnd); + fprintf(stderr, "(Pre-deletion index %d) ", N->PreDeletionThisIndex); + PrintEntry(N->This, N->ThisIndex, NM_THIS); } - if(N->NextIndex >= 0) + if(N->Next) { - printf( - " Next [%d]: %s: %6d %6d %6d %6d\n", - N->NextIndex, N->Next.BaseFilename, - N->Next.LinkOffsets.PrevStart, - N->Next.LinkOffsets.PrevEnd, - N->Next.LinkOffsets.NextStart, - N->Next.LinkOffsets.NextEnd); + PrintEntry(N->Next, N->NextIndex, NM_NEXT); } - printf( + fprintf(stderr, " OffsetModifiers: Prev %6d • This %6d • Next %6d\n" " PreLinkOffsetTotals: Prev %6d • This %6d • Next %6d\n" " DeletedEntryWasFirst: %s\n" @@ -6286,33 +10586,29 @@ SetNeighbour(db_entry *Dest, db_entry *Src) Dest->LinkOffsets.PrevEnd = Src->LinkOffsets.PrevEnd; Dest->LinkOffsets.NextStart = Src->LinkOffsets.NextStart; Dest->LinkOffsets.NextEnd = Src->LinkOffsets.NextEnd; - CopyString(Dest->BaseFilename, sizeof(Dest->BaseFilename), "%s", Src->BaseFilename); - CopyString(Dest->Title, sizeof(Dest->Title), "%s", Src->Title); + ClearCopyString(Dest->HMMLBaseFilename, sizeof(Dest->HMMLBaseFilename), "%s", Src->HMMLBaseFilename); + ClearCopyString(Dest->OutputLocation, sizeof(Dest->OutputLocation), "%s", Src->HMMLBaseFilename); + ClearCopyString(Dest->Title, sizeof(Dest->Title), "%s", Src->Title); } void -GetNeighbourhoodForAddition(neighbourhood *N, enum8(edit_types) EditType) +GetNeighbourhoodForAddition(neighbourhood *N, edit_type_id EditType) { - db_entry Entry = { }; - - int EntryIndex; - - bool FoundPrev = FALSE; - bool FoundNext = FALSE; - N->FormerIsFirst = TRUE; - EntryIndex = N->ThisIndex - 1; + int EntryIndex = N->ThisIndex - 1; + char *Ptr = (char *)N->Project; + Ptr += sizeof(*N->Project); + db_entry *FirstEntry = (db_entry *)Ptr; for(; EntryIndex >= 0; --EntryIndex) { - Entry = *(db_entry*)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * EntryIndex); - if(Entry.Size > 0) + db_entry *Entry = FirstEntry + EntryIndex; + if(Entry->Size > 0) { - if(!FoundPrev) + if(!N->Prev) { N->PrevIndex = EntryIndex; - SetNeighbour(&N->Prev, &Entry); - FoundPrev = TRUE; + N->Prev = Entry; } else { @@ -6324,26 +10620,22 @@ GetNeighbourhoodForAddition(neighbourhood *N, enum8(edit_types) EditType) switch(EditType) { - case EDIT_REINSERTION: - EntryIndex = N->ThisIndex + 1; - break; - case EDIT_ADDITION: - EntryIndex = N->ThisIndex; - break; + case EDIT_REINSERTION: EntryIndex = N->ThisIndex + 1; break; + case EDIT_ADDITION: EntryIndex = N->ThisIndex; break; + default: break; } N->LatterIsFinal = TRUE; - for(; EntryIndex < DB.EntriesHeader.Count; + for(; EntryIndex < N->Project->EntryCount; ++EntryIndex) { - Entry = *(db_entry*)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * EntryIndex); - if(Entry.Size > 0) + db_entry *Entry = FirstEntry + EntryIndex; + if(Entry->Size > 0) { - if(!FoundNext) + if(!N->Next) { N->NextIndex = EntryIndex; - SetNeighbour(&N->Next, &Entry); - FoundNext = TRUE; + N->Next = Entry; } else { @@ -6353,7 +10645,7 @@ GetNeighbourhoodForAddition(neighbourhood *N, enum8(edit_types) EditType) } } - if(EditType == EDIT_ADDITION && FoundNext) + if(EditType == EDIT_ADDITION && N->Next) { ++N->NextIndex; } @@ -6362,28 +10654,29 @@ GetNeighbourhoodForAddition(neighbourhood *N, enum8(edit_types) EditType) void GetNeighbourhoodForDeletion(neighbourhood *N) { - db_entry Entry = { }; - Entry = *(db_entry *)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * N->ThisIndex); + char *Ptr = (char *)N->Project; + Ptr += sizeof(*N->Project); + db_entry *FirstEntry = (db_entry *)Ptr; + db_entry *Entry = FirstEntry + N->ThisIndex; + N->PreDeletionThisIndex = N->ThisIndex; - SetNeighbour(&N->This, &Entry); + N->This = Entry; int EntryIndex; N->DeletedEntryWasFirst = TRUE; N->FormerIsFirst = TRUE; - bool FoundPrev = FALSE; for(EntryIndex = N->PreDeletionThisIndex - 1; EntryIndex >= 0; --EntryIndex) { - Entry = *(db_entry *)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * EntryIndex); - if(Entry.Size > 0) + Entry = FirstEntry + EntryIndex; + if(Entry->Size > 0) { - if(!FoundPrev) + if(!N->Prev) { - FoundPrev = TRUE; N->DeletedEntryWasFirst = FALSE; N->PrevIndex = EntryIndex; - SetNeighbour(&N->Prev, &Entry); + N->Prev = Entry; } else { @@ -6395,19 +10688,17 @@ GetNeighbourhoodForDeletion(neighbourhood *N) N->DeletedEntryWasFinal = TRUE; N->LatterIsFinal = TRUE; - bool FoundNext = FALSE; - for(EntryIndex = N->PreDeletionThisIndex + 1; EntryIndex < DB.EntriesHeader.Count; ++EntryIndex) + for(EntryIndex = N->PreDeletionThisIndex + 1; EntryIndex < N->Project->EntryCount; ++EntryIndex) { - Entry = *(db_entry *)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * EntryIndex); - if(Entry.Size > 0) + Entry = FirstEntry + EntryIndex; + if(Entry->Size > 0) { - if(!FoundNext) + if(!N->Next) { - FoundNext = TRUE; N->DeletedEntryWasFinal = FALSE; N->NextIndex = EntryIndex - 1; - SetNeighbour(&N->Next, &Entry); + N->Next = Entry; } else { @@ -6419,8 +10710,9 @@ GetNeighbourhoodForDeletion(neighbourhood *N) } void -GetNeighbourhood(neighbourhood *N, enum8(edit_types) EditType) +GetNeighbourhood(neighbourhood *N, edit_type_id EditType) { + // TODO(matt): We could probably get rid of the N->ThisIndex and friends entirely, in favour of the pointers if(EditType == EDIT_DELETION) { GetNeighbourhoodForDeletion(N); @@ -6429,38 +10721,41 @@ GetNeighbourhood(neighbourhood *N, enum8(edit_types) EditType) { GetNeighbourhoodForAddition(N, EditType); } + + DB.Metadata.Signposts.Prev.Ptr = N->Prev; + DB.Metadata.Signposts.This.Ptr = N->This; + DB.Metadata.Signposts.Next.Ptr = N->Next; } -void -SnipeEntryIntoMetadataBuffer(db_entry *Entry, int EntryIndex) +db_entry * +InsertIntoDB(neighbourhood *N, buffers *CollationBuffers, template *BespokeTemplate, string BaseFilename, bool RecheckingPrivacy, bool *Reinserting) { - *(db_entry *)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * EntryIndex) = *Entry; -} - -int -InsertIntoDB(neighbourhood *N, buffers *CollationBuffers, template *BespokeTemplate, char *BaseFilename, bool RecheckingPrivacy, bool *Reinserting) -{ - enum8(edit_types) EditType = EDIT_APPEND; + MEM_TEST_INITIAL(); + //MEM_TEST_MID("InsertIntoDB1"); + ResetNeighbourhood(N); + edit_type_id EditType = EDIT_APPEND; int EntryInsertionStart = StringLength("---\n"); - int EntryInsertionEnd; + int EntryInsertionEnd = 0; - if(DB.EntriesHeader.Count > 0) + if(N->Project->EntryCount > 0) { - db_entry *Entry = { 0 }; - N->ThisIndex = BinarySearchForMetadataEntry(&Entry, BaseFilename); - if(Entry) + db_entry *FirstEntry = LocateFirstEntry(N->Project); + N->ThisIndex = BinarySearchForMetadataEntry(N->Project, &N->This, BaseFilename); + if(N->This) { // Reinsert *Reinserting = TRUE; - EntryInsertionStart = AccumulateDBEntryInsertionOffset(N->ThisIndex); - EntryInsertionEnd = EntryInsertionStart + Entry->Size; + EntryInsertionStart = AccumulateDBEntryInsertionOffset(N->Project, N->ThisIndex); + EntryInsertionEnd = EntryInsertionStart + N->This->Size; EditType = EDIT_REINSERTION; + N->WorkingThis = *N->This; } else { if(N->ThisIndex == -1) { ++N->ThisIndex; } // NOTE(matt): BinarySearchForMetadataEntry returns -1 if search term precedes the set - Entry = (db_entry*)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * N->ThisIndex); - if(StringsDiffer(BaseFilename, Entry->BaseFilename) < 0) + db_entry *Test = FirstEntry + N->ThisIndex; + //N->WorkingThis = *(FirstEntry + N->ThisIndex); + if(StringsDiffer(BaseFilename, Wrap0i(Test->HMMLBaseFilename, sizeof(Test->HMMLBaseFilename))) < 0) { // Insert EditType = EDIT_INSERTION; @@ -6471,370 +10766,498 @@ InsertIntoDB(neighbourhood *N, buffers *CollationBuffers, template *BespokeTempl ++N->ThisIndex; EditType = EDIT_APPEND; } - EntryInsertionStart = AccumulateDBEntryInsertionOffset(N->ThisIndex); + EntryInsertionStart = AccumulateDBEntryInsertionOffset(N->Project, N->ThisIndex); } GetNeighbourhood(N, *Reinserting ? EDIT_REINSERTION : EDIT_ADDITION); } - char InputFile[StringLength(BaseFilename) + StringLength(".hmml") + 1]; - CopyString(InputFile, sizeof(InputFile), "%s.hmml", BaseFilename); bool VideoIsPrivate = FALSE; - switch(HMMLToBuffers(CollationBuffers, BespokeTemplate, InputFile, N)) + //MEM_TEST_MID("InsertIntoDB2"); + switch(HMMLToBuffers(CollationBuffers, BespokeTemplate, BaseFilename, N)) { // TODO(matt): Actually sort out the fatality of these cases case RC_ERROR_FILE: case RC_ERROR_FATAL: - return RC_ERROR_FATAL; case RC_ERROR_HMML: case RC_ERROR_MAX_REFS: case RC_ERROR_QUOTE: case RC_INVALID_REFERENCE: - return RC_ERROR_HMML; + return 0; case RC_PRIVATE_VIDEO: VideoIsPrivate = TRUE; case RC_SUCCESS: break; } + //MEM_TEST_MID("InsertIntoDB3"); - ClearCopyStringNoFormat(N->This.BaseFilename, sizeof(N->This.BaseFilename), BaseFilename); - if(!VideoIsPrivate) { ClearCopyStringNoFormat(N->This.Title, sizeof(N->This.Title), CollationBuffers->Title); } + ClearCopyStringNoFormat(N->WorkingThis.HMMLBaseFilename, sizeof(N->WorkingThis.HMMLBaseFilename), BaseFilename); + + if(!VideoIsPrivate) { ClearCopyStringNoFormat(N->WorkingThis.Title, sizeof(N->WorkingThis.Title), Wrap0i(CollationBuffers->Title, sizeof(CollationBuffers->Title))); } + + if(!DB.File.Buffer.Location) + { + InitIndexFile(CurrentProject); + } if(EditType == EDIT_REINSERTION) { // NOTE(matt): To save opening the DB.Metadata file, we defer sniping N->This in until InsertNeighbourLink() if(!VideoIsPrivate) { - if(!(DB.File.Handle = fopen(DB.File.Path, "w"))) { return RC_ERROR_FILE; } + *N->This = N->WorkingThis; + + if(!(DB.File.Handle = fopen(DB.File.Path, "w"))) { return 0; } fwrite(DB.File.Buffer.Location, EntryInsertionStart, 1, DB.File.Handle); - fwrite(CollationBuffers->SearchEntry.Location, N->This.Size, 1, DB.File.Handle); - fwrite(DB.File.Buffer.Location + EntryInsertionEnd, DB.File.FileSize - EntryInsertionEnd, 1, DB.File.Handle); + fwrite(CollationBuffers->SearchEntry.Location, N->This->Size, 1, DB.File.Handle); + fwrite(DB.File.Buffer.Location + EntryInsertionEnd, DB.File.Buffer.Size - EntryInsertionEnd, 1, DB.File.Handle); fclose(DB.File.Handle); - if(N->This.Size == EntryInsertionEnd - EntryInsertionStart) + if(N->This->Size == EntryInsertionEnd - EntryInsertionStart) { DB.File.Buffer.Ptr = DB.File.Buffer.Location + EntryInsertionStart; - CopyBufferSized(&DB.File.Buffer, &CollationBuffers->SearchEntry, N->This.Size); + CopyBufferSized(&DB.File.Buffer, &CollationBuffers->SearchEntry, N->This->Size); } else { FreeBuffer(&DB.File.Buffer); - ReadFileIntoBuffer(&DB.File, 0); + ReadFileIntoBuffer(&DB.File); } } } else { - int ExistingEntryCount = DB.EntriesHeader.Count; - ++DB.EntriesHeader.Count; + ++N->Project->EntryCount; - if(!(DB.Metadata.Handle = fopen(DB.Metadata.Path, "w"))) { return RC_ERROR_FILE; } - fwrite(&DB.Header, sizeof(DB.Header), 1, DB.Metadata.Handle); - fwrite(&DB.EntriesHeader, sizeof(DB.EntriesHeader), 1, DB.Metadata.Handle); + char *Ptr = (char*)N->Project; + Ptr += sizeof(*N->Project) + sizeof(db_entry) * N->ThisIndex; + uint64_t BytesIntoFile = Ptr - DB.Metadata.File.Buffer.Location; - DB.Metadata.Buffer.Ptr = DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader); - fwrite(DB.Metadata.Buffer.Ptr, - sizeof(DB.Entry), - N->ThisIndex, - DB.Metadata.Handle); - DB.Metadata.Buffer.Ptr += sizeof(DB.Entry) * N->ThisIndex; + if(!(DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"))) { return 0; } + fwrite(DB.Metadata.File.Buffer.Location, BytesIntoFile, 1, DB.Metadata.File.Handle); + SetFileEditPosition(&DB.Metadata); - fwrite(&N->This, sizeof(DB.Entry), 1, DB.Metadata.Handle); + fwrite(&N->WorkingThis, sizeof(N->WorkingThis), 1, DB.Metadata.File.Handle); + AccumulateFileEditSize(&DB.Metadata, sizeof(N->WorkingThis)); - fwrite(DB.Metadata.Buffer.Ptr, - sizeof(DB.Entry), - ExistingEntryCount - N->ThisIndex, - DB.Metadata.Handle); - DB.Metadata.Buffer.Ptr += sizeof(DB.Entry) * (ExistingEntryCount - N->ThisIndex); + fwrite(DB.Metadata.File.Buffer.Location + BytesIntoFile, DB.Metadata.File.Buffer.Size - BytesIntoFile, 1, DB.Metadata.File.Handle); + CycleSignpostedFile(&DB.Metadata); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); - fwrite(DB.Metadata.Buffer.Ptr, - DB.Metadata.FileSize - (DB.Metadata.Buffer.Ptr - DB.Metadata.Buffer.Location), - 1, - DB.Metadata.Handle); + char *EntryInDB = DB.Metadata.File.Buffer.Location + BytesIntoFile; + N->This = (db_entry *)EntryInDB; - CycleFile(&DB.Metadata); - - - if(!(DB.File.Handle = fopen(DB.File.Path, "w"))) { return RC_ERROR_FILE; } - fwrite(DB.File.Buffer.Location, - DB.File.FileSize - (DB.File.FileSize - EntryInsertionStart), - 1, DB.File.Handle); - fwrite(CollationBuffers->SearchEntry.Location, N->This.Size, 1, DB.File.Handle); - fwrite(DB.File.Buffer.Location + EntryInsertionStart, DB.File.FileSize - EntryInsertionStart, 1, DB.File.Handle); + if(!(DB.File.Handle = fopen(DB.File.Path, "w"))) { return 0; } + fwrite(DB.File.Buffer.Location, EntryInsertionStart, 1, DB.File.Handle); + fwrite(CollationBuffers->SearchEntry.Location, N->This->Size, 1, DB.File.Handle); + fwrite(DB.File.Buffer.Location + EntryInsertionStart, DB.File.Buffer.Size - EntryInsertionStart, 1, DB.File.Handle); CycleFile(&DB.File); } if(!VideoIsPrivate) { - LogError(LOG_NOTICE, "%s %s - %s", EditTypes[EditType].Name, BaseFilename, CollationBuffers->Title); - fprintf(stderr, "%s%s%s %s - %s\n", ColourStrings[EditTypes[EditType].Colour], EditTypes[EditType].Name, ColourStrings[CS_END], BaseFilename, CollationBuffers->Title); +#if 0 + LogError(LOG_NOTICE, "%s %.*s/%.*s - %s", EditTypes[EditType].Name, (int)CurrentProject->Lineage.Length, CurrentProject->Lineage.Base, (int)BaseFilename.Length, BaseFilename.Base, CollationBuffers->Title); + fprintf(stderr, "%s%s%s %.*s/%.*s - %s\n", + ColourStrings[EditTypes[EditType].Colour], EditTypes[EditType].Name, ColourStrings[CS_END], (int)CurrentProject->Lineage.Length, CurrentProject->Lineage.Base, (int)BaseFilename.Length, BaseFilename.Base, CollationBuffers->Title); +#else + LogError(LOG_NOTICE, "%s %.*s/%.*s - %s", EditTypes[EditType].Name, (int)CurrentProject->Lineage.Length, CurrentProject->Lineage.Base, (int)BaseFilename.Length, BaseFilename.Base, CollationBuffers->Title); + + fprintf(stderr, "%s%s%s ", ColourStrings[EditTypes[EditType].Colour], EditTypes[EditType].Name, ColourStrings[CS_END]); + PrintLineageAndEntry(CurrentProject->Lineage, BaseFilename, Wrap0(CollationBuffers->Title), TRUE); +#endif } else if(!RecheckingPrivacy) { - LogError(LOG_NOTICE, "Privately %s %s", EditTypes[EditType].Name, BaseFilename); - fprintf(stderr, "%sPrivately %s%s %s\n", ColourStrings[CS_PRIVATE], EditTypes[EditType].Name, ColourStrings[CS_END], BaseFilename); + LogError(LOG_NOTICE, "Privately %s %.*s/%.*s", EditTypes[EditType].Name, (int)CurrentProject->Lineage.Length, CurrentProject->Lineage.Base, (int)BaseFilename.Length, BaseFilename.Base); + fprintf(stderr, "%sPrivately %s%s ", ColourStrings[CS_PRIVATE], EditTypes[EditType].Name, ColourStrings[CS_END]); + PrintLineageAndEntry(CurrentProject->Lineage, BaseFilename, Wrap0(CollationBuffers->Title), TRUE); } // TODO(matt): Remove VideoIsPrivate in favour of generating a player page in a random location - return VideoIsPrivate ? RC_PRIVATE_VIDEO : RC_SUCCESS; + MEM_TEST_AFTER("InsertIntoDB()"); + return VideoIsPrivate ? 0 : N->This; } void WritePastAssetsHeader(void) { - DB.Metadata.Handle = fopen(DB.Metadata.Path, "w"); - fwrite(DB.Metadata.Buffer.Location, - sizeof(DB.Header) - + sizeof(DB.EntriesHeader) - + sizeof(DB.Entry) * DB.EntriesHeader.Count - + sizeof(DB.AssetsHeader), - 1, - DB.Metadata.Handle); + DB.Metadata.Signposts.AssetsBlock.Ptr = LocateBlock(B_ASET); + DB.Metadata.File.Buffer.Ptr = DB.Metadata.Signposts.AssetsBlock.Ptr; + DB.Metadata.File.Buffer.Ptr += sizeof(db_block_assets); - DB.Metadata.Buffer.Ptr = DB.Metadata.Buffer.Location - + sizeof(DB.Header) - + sizeof(DB.EntriesHeader) - + sizeof(DB.Entry) * DB.EntriesHeader.Count; - - DB.AssetsHeader = *(db_header_assets *)DB.Metadata.Buffer.Ptr; - DB.Metadata.Buffer.Ptr += sizeof(DB.AssetsHeader); + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + fwrite(DB.Metadata.File.Buffer.Location, DB.Metadata.File.Buffer.Ptr - DB.Metadata.File.Buffer.Location, 1, DB.Metadata.File.Handle); } void -PrintLandmarks(void *FirstLandmark, int LandmarkCount) +ProcessPrevLandmarks(neighbourhood *N, db_asset *Asset, landmark_range ProjectRange, landmark_range CurrentTarget, int *RunningIndex) { - printf("PrintLandmarks()\n"); - for(int i = 0; i < LandmarkCount; ++i) + db_landmark *FirstLandmark = LocateFirstLandmark(Asset); + if(N->Prev) { - db_landmark Landmark = *(db_landmark *)(FirstLandmark + sizeof(Landmark) * i); - printf(" %4d %d\n", Landmark.EntryIndex, Landmark.Position); - } -} - -void -ProcessPrevLandmarks(neighbourhood *N, void *FirstLandmark, int ExistingLandmarkCount, landmark_range *CurrentTarget, int *RunningIndex) -{ - if(N->PrevIndex >= 0) - { - landmark_range FormerTarget = BinarySearchForMetadataLandmark(FirstLandmark, N->PrevIndex, ExistingLandmarkCount); - fwrite(FirstLandmark, sizeof(db_landmark), FormerTarget.First, DB.Metadata.Handle); + //landmark_range FormerTarget = BinarySearchForMetadataLandmark(Asset, CurrentProject->Index, N->PrevIndex, ExistingLandmarkCount); + landmark_range FormerTarget = {}; + if(CurrentTarget.First > 0) + { + //db_landmark *Prior = FirstLandmark - 1; + FormerTarget = GetIndexRange(Asset, ProjectRange, CurrentTarget.First - 1); + } + fwrite(FirstLandmark, sizeof(db_landmark), FormerTarget.First, DB.Metadata.File.Handle); *RunningIndex += FormerTarget.First; for(int j = 0; j < FormerTarget.Length; ++j, ++*RunningIndex) { - db_landmark Landmark = *(db_landmark *)(FirstLandmark + sizeof(Landmark) * *RunningIndex); - if(Landmark.Position >= N->PreLinkPrevOffsetTotal) + db_landmark *Landmark = FirstLandmark + *RunningIndex; + if(Landmark->EntryIndex >= 0 && Landmark->Position >= N->PreLinkPrevOffsetTotal) { - Landmark.Position += N->PrevOffsetModifier; + Landmark->Position += N->PrevOffsetModifier; } - fwrite(&Landmark, sizeof(Landmark), 1, DB.Metadata.Handle); + fwrite(Landmark, sizeof(db_landmark), 1, DB.Metadata.File.Handle); } } else { - fwrite(FirstLandmark + sizeof(db_landmark) * *RunningIndex, sizeof(db_landmark), CurrentTarget->First, DB.Metadata.Handle); - *RunningIndex += CurrentTarget->First; + //fwrite(FirstLandmark + sizeof(db_landmark) * *RunningIndex, sizeof(db_landmark), CurrentTarget.First, DB.Metadata.File.Handle); + fwrite(FirstLandmark, sizeof(db_landmark), CurrentTarget.First, DB.Metadata.File.Handle); + *RunningIndex += CurrentTarget.First; } } void -ProcessNextLandmarks(neighbourhood *N, void *FirstLandmark, int ExistingLandmarkCount, int *RunningIndex, enum8(edit_types) EditType) +ProcessNextLandmarks(neighbourhood *N, db_asset *Asset, landmark_range ProjectRange, landmark_range CurrentTarget, + int ExistingLandmarkCount, int *RunningIndex, edit_type_id EditType) { - if(N->NextIndex >= 0 && *RunningIndex < ExistingLandmarkCount) + db_landmark *FirstLandmark = LocateFirstLandmark(Asset); + if(N->Next && *RunningIndex < ProjectRange.First + ProjectRange.Length) { - db_landmark Landmark = *(db_landmark *)(FirstLandmark + sizeof(Landmark) * *RunningIndex); - landmark_range LatterTarget = BinarySearchForMetadataLandmark(FirstLandmark, Landmark.EntryIndex, ExistingLandmarkCount); + //db_landmark *Latter = FirstLandmark + *RunningIndex; + landmark_range LatterTarget = GetIndexRange(Asset, ProjectRange, *RunningIndex); + //BinarySearchForMetadataLandmark(Asset, CurrentProject->Index, Landmark->EntryIndex, ExistingLandmarkCount); - for(int j = 0; j < LatterTarget.Length; ++j, ++*RunningIndex) + for(int j = 0; j < LatterTarget.Length; ++j) { - Landmark = *(db_landmark *)(FirstLandmark + sizeof(Landmark) * *RunningIndex); - if(Landmark.Position >= N->PreLinkNextOffsetTotal) + db_landmark *Landmark = FirstLandmark + LatterTarget.First + j; + if(Landmark->Position >= N->PreLinkNextOffsetTotal) { - Landmark.Position += N->NextOffsetModifier; + Landmark->Position += N->NextOffsetModifier; } - switch(EditType) - { - case EDIT_DELETION: --Landmark.EntryIndex; break; - case EDIT_ADDITION: ++Landmark.EntryIndex; break; - } - fwrite(&Landmark, sizeof(Landmark), 1, DB.Metadata.Handle); } - for(; *RunningIndex < ExistingLandmarkCount; ++*RunningIndex) + for(; *RunningIndex < ProjectRange.First + ProjectRange.Length; ++*RunningIndex) { - Landmark = *(db_landmark *)(FirstLandmark + sizeof(Landmark) * *RunningIndex); + db_landmark *Landmark = FirstLandmark + *RunningIndex; switch(EditType) { - case EDIT_DELETION: --Landmark.EntryIndex; break; - case EDIT_ADDITION: ++Landmark.EntryIndex; break; + case EDIT_DELETION: --Landmark->EntryIndex; break; + case EDIT_ADDITION: ++Landmark->EntryIndex; break; + default: break; } - fwrite(&Landmark, sizeof(Landmark), 1, DB.Metadata.Handle); + fwrite(Landmark, sizeof(db_landmark), 1, DB.Metadata.File.Handle); } } + + if(*RunningIndex < ExistingLandmarkCount) + { + db_landmark *Landmark = FirstLandmark + *RunningIndex; + fwrite(Landmark, sizeof(db_landmark), ExistingLandmarkCount - *RunningIndex, DB.Metadata.File.Handle); + } +} + +typedef struct +{ + uint64_t Index; + uint64_t Location; +} asset_index_and_location; + +void +OffsetAssociatedIndex(asset_index_and_location *AssetDeletionRecords, int AssetDeletionRecordCount, int32_t *Index) +{ + fprintf(stderr, "Possibly offsetting associated index\n"); + for(int i = AssetDeletionRecordCount - 1; i >= 0; --i) + { + if(*Index > AssetDeletionRecords[i].Index) + { + --*Index; + } + } +} + +void * +OffsetAssociatedIndicesOfProject(db_header_project *Project, asset_index_and_location *AssetDeletionRecords, int AssetDeletionRecordCount) +{ + if(Project->ArtIndex >= 0) + { + OffsetAssociatedIndex(AssetDeletionRecords, AssetDeletionRecordCount, &Project->ArtIndex); + } + if(Project->IconIndex >= 0) + { + OffsetAssociatedIndex(AssetDeletionRecords, AssetDeletionRecordCount, &Project->IconIndex); + } + + db_entry *Entry = LocateFirstEntry(Project); + for(int j = 0; j < Project->EntryCount; ++j, ++Entry) + { + if(Entry->ArtIndex >= 0) + { + OffsetAssociatedIndex(AssetDeletionRecords, AssetDeletionRecordCount, &Entry->ArtIndex); + } + } + + db_header_project *Child = LocateFirstChildProject(Project); + for(int ChildIndex = 0; ChildIndex < Project->ChildCount; ++ChildIndex) + { + Child = OffsetAssociatedIndicesOfProject(Child, AssetDeletionRecords, AssetDeletionRecordCount); + } + + return SkipProjectAndChildren(Project); +} + +void +OffsetAssociatedIndices(asset_index_and_location *AssetDeletionRecords, int AssetDeletionRecordCount) +{ + db_block_projects *ProjectsBlock = LocateBlock(B_PROJ); + db_header_project *Project = LocateFirstChildProjectOfBlock(ProjectsBlock); + for(int i = 0; i < ProjectsBlock->Count; ++i) + { + Project = OffsetAssociatedIndicesOfProject(Project, AssetDeletionRecords, AssetDeletionRecordCount); + } } void DeleteStaleAssets(void) { - LocateAssetsBlock(); - char *AssetsHeaderLocation = DB.Metadata.Buffer.Ptr; - DB.AssetsHeader = *(db_header_assets *)AssetsHeaderLocation; int AssetDeletionCount = 0; - int AssetDeletionLocations[DB.AssetsHeader.Count]; - DB.Metadata.Buffer.Ptr += sizeof(DB.AssetsHeader); - for(int AssetIndex = 0; AssetIndex < DB.AssetsHeader.Count; ++AssetIndex) + DB.Metadata.Signposts.AssetsBlock.Ptr = LocateBlock(B_ASET); + db_block_assets *AssetsBlock = DB.Metadata.Signposts.AssetsBlock.Ptr; + + int AssetsCount = AssetsBlock->Count; + // TODO(matt): Put this stuff on the heap. possibly once our memory situation is straightened out + asset_index_and_location AssetDeletionRecords[AssetsBlock->Count]; + db_asset *Asset = LocateFirstAsset(AssetsBlock); + for(int AssetIndex = 0; AssetIndex < AssetsCount; ++AssetIndex) { - DB.Asset = *(db_asset*)DB.Metadata.Buffer.Ptr; - if(DB.Asset.LandmarkCount == 0) + if(Asset->LandmarkCount == 0 && !Asset->Associated) { - AssetDeletionLocations[AssetDeletionCount] = DB.Metadata.Buffer.Ptr - DB.Metadata.Buffer.Location; + AssetDeletionRecords[AssetDeletionCount].Location = (char *)Asset - DB.Metadata.File.Buffer.Location; + AssetDeletionRecords[AssetDeletionCount].Index = AssetIndex; ++AssetDeletionCount; - --DB.AssetsHeader.Count; + --AssetsBlock->Count; } - DB.Metadata.Buffer.Ptr += sizeof(DB.Asset) + sizeof(DB.Landmark) * DB.Asset.LandmarkCount; + Asset = SkipAsset(Asset); } if(AssetDeletionCount > 0) { - DB.Metadata.Handle = fopen(DB.Metadata.Path, "w"); - fwrite(DB.Metadata.Buffer.Location, AssetsHeaderLocation - DB.Metadata.Buffer.Location, 1, DB.Metadata.Handle); - fwrite(&DB.AssetsHeader, sizeof(DB.AssetsHeader), 1, DB.Metadata.Handle); + OffsetAssociatedIndices(AssetDeletionRecords, AssetDeletionCount); - int WrittenBytes = (AssetsHeaderLocation - DB.Metadata.Buffer.Location) + sizeof(DB.AssetsHeader); + WritePastAssetsHeader(); + SetFileEditPosition(&DB.Metadata); + uint64_t BytesIntoFile = DB.Metadata.File.Buffer.Ptr - DB.Metadata.File.Buffer.Location; for(int DeletionIndex = 0; DeletionIndex < AssetDeletionCount; ++DeletionIndex) { - DB.Metadata.Buffer.Ptr = DB.Metadata.Buffer.Location + AssetDeletionLocations[DeletionIndex]; - fwrite(DB.Metadata.Buffer.Location + WrittenBytes, - (DB.Metadata.Buffer.Ptr - DB.Metadata.Buffer.Location) - WrittenBytes, + DB.Metadata.File.Buffer.Ptr = DB.Metadata.File.Buffer.Location + AssetDeletionRecords[DeletionIndex].Location; + fwrite(DB.Metadata.File.Buffer.Location + BytesIntoFile, + (DB.Metadata.File.Buffer.Ptr - DB.Metadata.File.Buffer.Location) - BytesIntoFile, 1, - DB.Metadata.Handle); - WrittenBytes += (DB.Metadata.Buffer.Ptr - DB.Metadata.Buffer.Location) - WrittenBytes + sizeof(DB.Asset); + DB.Metadata.File.Handle); + BytesIntoFile += (DB.Metadata.File.Buffer.Ptr - DB.Metadata.File.Buffer.Location) - BytesIntoFile + sizeof(db_asset); } - fwrite(DB.Metadata.Buffer.Location + WrittenBytes, DB.Metadata.FileSize - WrittenBytes, 1, DB.Metadata.Handle); - CycleFile(&DB.Metadata); + AccumulateFileEditSize(&DB.Metadata, -sizeof(db_asset) * AssetDeletionCount); + WriteFromByteToEnd(&DB.Metadata.File, BytesIntoFile); + CycleSignpostedFile(&DB.Metadata); } } void -DeleteStaleLandmarks(void) +DeleteAllLandmarksAndAssets(void) { - WritePastAssetsHeader(); - for(int AssetIndex = 0; AssetIndex < DB.AssetsHeader.Count; ++AssetIndex) + // TODO(matt): Test this! + uint64_t BytesThroughBuffer = 0; + + db_block_assets *AssetsBlock = LocateBlock(B_ASET); + WriteFromByteToPointer(&DB.Metadata.File, &BytesThroughBuffer, AssetsBlock); + + int AssetsCount = AssetsBlock->Count; + AssetsBlock->Count = 0; + fwrite(AssetsBlock, sizeof(db_block_assets), 1, DB.Metadata.File.Handle); + BytesThroughBuffer += sizeof(db_block_assets); + + DB.Metadata.File.Buffer.Ptr = DB.Metadata.File.Buffer.Location + BytesThroughBuffer; + SetFileEditPosition(&DB.Metadata); + + for(int AssetIndex = 0; AssetIndex < AssetsCount; ++AssetIndex) { - DB.Asset = *(db_asset *)DB.Metadata.Buffer.Ptr; - DB.Metadata.Buffer.Ptr += sizeof(DB.Asset) + sizeof(DB.Landmark) * DB.Asset.LandmarkCount; - DB.Asset.LandmarkCount = 0; - fwrite(&DB.Asset, sizeof(DB.Asset), 1, DB.Metadata.Handle); + db_asset *Asset = (db_asset *)(DB.Metadata.File.Buffer.Location + BytesThroughBuffer); + + BytesThroughBuffer += sizeof(db_asset); + AccumulateFileEditSize(&DB.Metadata, -sizeof(db_asset)); + + BytesThroughBuffer += sizeof(db_landmark) * Asset->LandmarkCount; + AccumulateFileEditSize(&DB.Metadata, -sizeof(db_landmark) * Asset->LandmarkCount); } - CycleFile(&DB.Metadata); + + WriteFromByteToEnd(&DB.Metadata.File, BytesThroughBuffer); + CycleSignpostedFile(&DB.Metadata); +} + +landmark_range +DetermineProjectLandmarksRange(db_asset *A, db_project_index I) +{ + // TODO(matt): Do it in a Binary search fashion + landmark_range Result = {}; + + db_landmark *Landmark = LocateFirstLandmark(A); + for(; Result.First < A->LandmarkCount; ++Result.First, ++Landmark) + { + if(!(ProjectIndicesDiffer(I, Landmark->Project) > 0)) + { + break; + } + } + + for(; Result.First + Result.Length < A->LandmarkCount; ++Landmark, ++Result.Length) + { + if(ProjectIndicesDiffer(I, Landmark->Project)) + { + break; + } + } + return Result; } void DeleteLandmarks(neighbourhood *N) { - for(int AssetIndex = 0; AssetIndex < DB.AssetsHeader.Count; ++AssetIndex) + SetFileEditPosition(&DB.Metadata); + db_block_assets *AssetsBlock = DB.Metadata.Signposts.AssetsBlock.Ptr; + for(int AssetIndex = 0; AssetIndex < AssetsBlock->Count; ++AssetIndex) { - DB.Asset = *(db_asset *)DB.Metadata.Buffer.Ptr; - DB.Metadata.Buffer.Ptr += sizeof(DB.Asset); - db_landmark *FirstLandmark = (db_landmark *)DB.Metadata.Buffer.Ptr; - int ExistingLandmarkCount = DB.Asset.LandmarkCount; + db_asset *Asset = (db_asset *)DB.Metadata.File.Buffer.Ptr; + DB.Metadata.File.Buffer.Ptr += sizeof(*Asset); + int ExistingLandmarkCount = Asset->LandmarkCount; int RunningIndex = 0; - landmark_range DeletionTarget = BinarySearchForMetadataLandmark(FirstLandmark, N->PreDeletionThisIndex, ExistingLandmarkCount); - DB.Asset.LandmarkCount -= DeletionTarget.Length; - fwrite(&DB.Asset, sizeof(DB.Asset), 1, DB.Metadata.Handle); + landmark_range ProjectRange = DetermineProjectLandmarksRange(Asset, CurrentProject->Index); - ProcessPrevLandmarks(N, FirstLandmark, ExistingLandmarkCount, &DeletionTarget, &RunningIndex); + landmark_range DeletionTarget = BinarySearchForMetadataLandmark(Asset, ProjectRange, N->PreDeletionThisIndex); + Asset->LandmarkCount -= DeletionTarget.Length; + AccumulateFileEditSize(&DB.Metadata, -sizeof(DB.Landmark) * DeletionTarget.Length); + fwrite(Asset, sizeof(*Asset), 1, DB.Metadata.File.Handle); + + ProcessPrevLandmarks(N, Asset, ProjectRange, DeletionTarget, &RunningIndex); RunningIndex += DeletionTarget.Length; - ProcessNextLandmarks(N, FirstLandmark, ExistingLandmarkCount, &RunningIndex, EDIT_DELETION); - DB.Metadata.Buffer.Ptr += sizeof(db_landmark) * ExistingLandmarkCount; + ProcessNextLandmarks(N, Asset, ProjectRange, DeletionTarget, ExistingLandmarkCount, &RunningIndex, EDIT_DELETION); + DB.Metadata.File.Buffer.Ptr += sizeof(DB.Landmark) * ExistingLandmarkCount; } - CycleFile(&DB.Metadata); + CycleSignpostedFile(&DB.Metadata); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); } -void UpdateLandmarksForNeighbourhood(neighbourhood *N, enum8(edit_types) EditType); +void UpdateLandmarksForNeighbourhood(neighbourhood *N, edit_type_id EditType); void -AddLandmarks(neighbourhood *N, enum8(edit_types) EditType) +AddLandmarks(neighbourhood *N, project *P, edit_type_id EditType) { for(int i = 0; i < Assets.Count; ++i) { - Assets.Asset[i].Known = FALSE; + asset *Asset = GetPlaceInBook(&Assets.Asset, i); + Asset->Known = FALSE; } - for(int StoredAssetIndex = 0; StoredAssetIndex < DB.AssetsHeader.Count; ++StoredAssetIndex) + SetFileEditPosition(&DB.Metadata); + + db_block_assets *AssetsBlock = DB.Metadata.Signposts.AssetsBlock.Ptr; + for(int StoredAssetIndex = 0; StoredAssetIndex < AssetsBlock->Count; ++StoredAssetIndex) { - DB.Asset = *(db_asset *)DB.Metadata.Buffer.Ptr; - int ExistingLandmarkCount = DB.Asset.LandmarkCount; - DB.Metadata.Buffer.Ptr += sizeof(DB.Asset); - void *FirstLandmark; - if(ExistingLandmarkCount > 0) - { - FirstLandmark = DB.Metadata.Buffer.Ptr; - } + db_asset *Asset = (db_asset *)DB.Metadata.File.Buffer.Ptr; + + //PrintAssetAndLandmarks(Asset); + + int ExistingLandmarkCount = Asset->LandmarkCount; + DB.Metadata.File.Buffer.Ptr += sizeof(*Asset); int RunningIndex = 0; for(int i = 0; i < Assets.Count; ++i) { - if(!StringsDiffer(DB.Asset.Filename, Assets.Asset[i].Filename) && DB.Asset.Type == Assets.Asset[i].Type) + asset *AssetInMemory = GetPlaceInBook(&Assets.Asset, i); + // TODO(matt): Exhaustively test BinarySearchForMetadataLandmark() and figure out why ProcessPrevLandmarks() / + // ProcessNextLandmarks() are apparently failing to write out all the existing landmarks + if(!StringsDiffer(Wrap0i(Asset->Filename, sizeof(Asset->Filename)), Wrap0i(AssetInMemory->Filename, sizeof(AssetInMemory->Filename))) && Asset->Type == AssetInMemory->Type) { - Assets.Asset[i].Known = TRUE; - if(!Assets.Asset[i].OffsetLandmarks) + AssetInMemory->Known = TRUE; + landmark_range ProjectRange = DetermineProjectLandmarksRange(Asset, P->Index); + /* + * Existing asset landmarks may all be -1, and only appropriate for a search page + * We have a N->Prev who does not contain this asset + * First batch of asset landmarks may be -1, with following ones >= 0 and appropriate for player pages + * Existing asset landmarks are all >= 0, and appropriate for player pages + * + */ + if(!AssetInMemory->OffsetLandmarks)// && Assets.Asset[i].PlayerLandmarkCount > 0) { - DB.Asset.LandmarkCount += Assets.Asset[i].PlayerLandmarkCount; - landmark_range ThisTarget; - if(ExistingLandmarkCount > 0) + Asset->LandmarkCount += AssetInMemory->PlayerLandmarkCount; + landmark_range ThisTarget = ProjectRange; + if(ProjectRange.Length > 0) { - ThisTarget = BinarySearchForMetadataLandmark(FirstLandmark, N->ThisIndex, ExistingLandmarkCount); + ThisTarget = BinarySearchForMetadataLandmark(Asset, ProjectRange, N->ThisIndex); - if(EditType == EDIT_REINSERTION) { DB.Asset.LandmarkCount -= ThisTarget.Length; } + if(EditType == EDIT_REINSERTION) { Asset->LandmarkCount -= ThisTarget.Length; } } - fwrite(&DB.Asset, sizeof(DB.Asset), 1, DB.Metadata.Handle); + fwrite(Asset, sizeof(*Asset), 1, DB.Metadata.File.Handle); if(ExistingLandmarkCount > 0) { - ProcessPrevLandmarks(N, FirstLandmark, ExistingLandmarkCount, &ThisTarget, &RunningIndex); + ProcessPrevLandmarks(N, Asset, ProjectRange, ThisTarget, &RunningIndex); } - for(int j = 0; j < Assets.Asset[i].PlayerLandmarkCount; ++j) + for(int j = 0; j < AssetInMemory->PlayerLandmarkCount; ++j) { - db_landmark Landmark; + db_landmark Landmark = {}; + // TODO(matt): Actually make sure that the correct project_index is set! + Landmark.Project = P->Index; Landmark.EntryIndex = N->ThisIndex; - Landmark.Position = Assets.Asset[i].PlayerLandmark[j]; - fwrite(&Landmark, sizeof(Landmark), 1, DB.Metadata.Handle); + Landmark.Position = AssetInMemory->Player[j].Offset; + fwrite(&Landmark, sizeof(Landmark), 1, DB.Metadata.File.Handle); } if(ExistingLandmarkCount > 0) { if(EditType == EDIT_REINSERTION) { RunningIndex += ThisTarget.Length; } - ProcessNextLandmarks(N, FirstLandmark, ExistingLandmarkCount, &RunningIndex, EditType); + ProcessNextLandmarks(N, Asset, ProjectRange, ThisTarget, ExistingLandmarkCount, &RunningIndex, EditType); } - Assets.Asset[i].OffsetLandmarks = TRUE; + AssetInMemory->OffsetLandmarks = TRUE; } else { - fwrite(&DB.Asset, sizeof(DB.Asset), 1, DB.Metadata.Handle); - fwrite(DB.Metadata.Buffer.Ptr, sizeof(db_landmark) * ExistingLandmarkCount, 1, DB.Metadata.Handle); + AssetInMemory->OffsetLandmarks = TRUE; + fwrite(Asset, sizeof(*Asset), 1, DB.Metadata.File.Handle); + fwrite(DB.Metadata.File.Buffer.Ptr, sizeof(db_landmark) * ExistingLandmarkCount, 1, DB.Metadata.File.Handle); } break; } } - DB.Metadata.Buffer.Ptr += sizeof(db_landmark) * ExistingLandmarkCount; + // TODO(matt): Is this definitely okay to do here, or should we do it before the break? + AccumulateFileEditSize(&DB.Metadata, sizeof(db_landmark) * (Asset->LandmarkCount - ExistingLandmarkCount)); + DB.Metadata.File.Buffer.Ptr += sizeof(db_landmark) * ExistingLandmarkCount; } - CycleFile(&DB.Metadata); + CycleSignpostedFile(&DB.Metadata); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); bool NewAsset = FALSE; for(int i = 0; i < Assets.Count; ++i) { - if(!Assets.Asset[i].Known && Assets.Asset[i].PlayerLandmarkCount > 0) + asset *This = GetPlaceInBook(&Assets.Asset, i); + if(!This->Known && This->PlayerLandmarkCount > 0) { - UpdateAssetInDB(i); + UpdateAssetInDB(This); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); NewAsset = TRUE; } } @@ -6845,931 +11268,93 @@ AddLandmarks(neighbourhood *N, enum8(edit_types) EditType) } void -UpdateLandmarksForNeighbourhood(neighbourhood *N, enum8(edit_types) EditType) +UpdateLandmarksForNeighbourhood(neighbourhood *N, edit_type_id EditType) { - if(!(Config.Mode & MODE_NOREVVEDRESOURCE)) + if(Config->QueryString.Length > 0) { WritePastAssetsHeader(); switch(EditType) { case EDIT_DELETION: DeleteLandmarks(N); break; - case EDIT_ADDITION: case EDIT_REINSERTION: { AddLandmarks(N, EditType); break; } + case EDIT_ADDITION: case EDIT_REINSERTION: { AddLandmarks(N, CurrentProject, EditType); break; } + default: break; } } + + //PrintAssetsBlock(); } void -DeleteLandmarksForSearch(void) +DeleteLandmarksForSearch(db_project_index ProjectIndex) { - if(!(Config.Mode & MODE_NOREVVEDRESOURCE)) + if(Config->QueryString.Length > 0) { WritePastAssetsHeader(); - for(int AssetIndex = 0; AssetIndex < DB.AssetsHeader.Count; ++AssetIndex) + SetFileEditPosition(&DB.Metadata); + + db_block_assets *AssetsBlock = DB.Metadata.Signposts.AssetsBlock.Ptr; + for(int AssetIndex = 0; AssetIndex < AssetsBlock->Count; ++AssetIndex) { - DB.Asset = *(db_asset *)DB.Metadata.Buffer.Ptr; - DB.Metadata.Buffer.Ptr += sizeof(DB.Asset); - db_landmark *FirstLandmark = (db_landmark *)DB.Metadata.Buffer.Ptr; - int ExistingLandmarkCount = DB.Asset.LandmarkCount; + db_asset *Asset = (db_asset *)DB.Metadata.File.Buffer.Ptr; + int ExistingLandmarkCount = Asset->LandmarkCount; - landmark_range DeletionTarget = BinarySearchForMetadataLandmark(FirstLandmark, PAGE_TYPE_SEARCH, ExistingLandmarkCount); - DB.Asset.LandmarkCount -= DeletionTarget.Length; - fwrite(&DB.Asset, sizeof(DB.Asset), 1, DB.Metadata.Handle); + landmark_range ProjectRange = DetermineProjectLandmarksRange(Asset, ProjectIndex); + landmark_range DeletionTarget = BinarySearchForMetadataLandmark(Asset, ProjectRange, SP_SEARCH); - DB.Metadata.Buffer.Ptr += sizeof(DB.Landmark) * DeletionTarget.Length; - fwrite(DB.Metadata.Buffer.Ptr, sizeof(DB.Landmark), ExistingLandmarkCount - DeletionTarget.Length, DB.Metadata.Handle); + Asset->LandmarkCount -= DeletionTarget.Length; - DB.Metadata.Buffer.Ptr += sizeof(DB.Landmark) * ExistingLandmarkCount - DeletionTarget.Length; + fwrite(Asset, sizeof(*Asset), 1, DB.Metadata.File.Handle); + DB.Metadata.File.Buffer.Ptr += sizeof(*Asset); + + fwrite(DB.Metadata.File.Buffer.Ptr, sizeof(db_landmark), DeletionTarget.First, DB.Metadata.File.Handle); + DB.Metadata.File.Buffer.Ptr += sizeof(db_landmark) * DeletionTarget.First; + + DB.Metadata.File.Buffer.Ptr += sizeof(db_landmark) * DeletionTarget.Length; + AccumulateFileEditSize(&DB.Metadata, -sizeof(db_landmark) * DeletionTarget.Length); + + fwrite(DB.Metadata.File.Buffer.Ptr, sizeof(db_landmark), ExistingLandmarkCount - (DeletionTarget.First + DeletionTarget.Length), DB.Metadata.File.Handle); + DB.Metadata.File.Buffer.Ptr += sizeof(db_landmark) * (ExistingLandmarkCount - (DeletionTarget.First + DeletionTarget.Length)); } - CycleFile(&DB.Metadata); + CycleSignpostedFile(&DB.Metadata); } } -void -UpdateLandmarksForSearch(void) -{ - if(!(Config.Mode & MODE_NOREVVEDRESOURCE)) - { - for(int i = 0; i < Assets.Count; ++i) - { - Assets.Asset[i].Known = FALSE; - } - - WritePastAssetsHeader(); - for(int AssetIndex = 0; AssetIndex < DB.AssetsHeader.Count; ++AssetIndex) - { - DB.Asset = *(db_asset *)DB.Metadata.Buffer.Ptr; - int ExistingLandmarkCount = DB.Asset.LandmarkCount; - DB.Metadata.Buffer.Ptr += sizeof(DB.Asset); - void *FirstLandmark = DB.Metadata.Buffer.Ptr; - - for(int i = 0; i < Assets.Count; ++i) - { - if(!StringsDiffer(DB.Asset.Filename, Assets.Asset[i].Filename) && DB.Asset.Type == Assets.Asset[i].Type) - { - Assets.Asset[i].Known = TRUE; - - landmark_range Target = BinarySearchForMetadataLandmark(FirstLandmark, PAGE_TYPE_SEARCH, DB.Asset.LandmarkCount); - - DB.Asset.LandmarkCount += Assets.Asset[i].SearchLandmarkCount - Target.Length; - - fwrite(&DB.Asset, sizeof(DB.Asset), 1, DB.Metadata.Handle); - - for(int j = 0; j < Assets.Asset[i].SearchLandmarkCount; ++j) - { - DB.Landmark.EntryIndex = PAGE_TYPE_SEARCH; - DB.Landmark.Position = Assets.Asset[i].SearchLandmark[j]; - fwrite(&DB.Landmark, sizeof(DB.Landmark), 1, DB.Metadata.Handle); - } - - DB.Metadata.Buffer.Ptr += sizeof(DB.Landmark) * (Target.First + Target.Length); - fwrite(DB.Metadata.Buffer.Ptr, sizeof(DB.Landmark), ExistingLandmarkCount - (Target.First + Target.Length), DB.Metadata.Handle); - DB.Metadata.Buffer.Ptr += sizeof(DB.Landmark) * (ExistingLandmarkCount - (Target.First + Target.Length)); - break; - } - } - } - CycleFile(&DB.Metadata); - - bool NewAsset = FALSE; - for(int InternalAssetIndex = 0; InternalAssetIndex < Assets.Count; ++InternalAssetIndex) - { - if(!Assets.Asset[InternalAssetIndex].Known && Assets.Asset[InternalAssetIndex].SearchLandmarkCount > 0) - { - NewAsset = TRUE; - UpdateAssetInDB(InternalAssetIndex); - } - } - if(NewAsset) { UpdateLandmarksForSearch(); } - } -} - -enum -{ - LINK_INCLUDE, - LINK_EXCLUDE -} link_types; - -enum -{ - LINK_FORWARDS, - LINK_BACKWARDS -} link_directions; - -int -InsertNeighbourLink(db_entry *From, int FromIndex, db_entry *To, enum8(link_directions) LinkDirection, bool FromHasOneNeighbour) -{ - file_buffer HTML; - if(ReadPlayerPageIntoBuffer(&HTML, From) == RC_SUCCESS) - { - if(!(HTML.Handle = fopen(HTML.Path, "w"))) { FreeBuffer(&HTML.Buffer); return RC_ERROR_FILE; }; - - buffer Link; - ClaimBuffer(&Link, "Link", Kilobytes(4)); - - buffer ToPlayerURL; - if(To) - { - ClaimBuffer(&ToPlayerURL, "ToPlayerURL", MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_PLAYER_URL_PREFIX_LENGTH + MAX_BASE_FILENAME_LENGTH); - ConstructPlayerURL(&ToPlayerURL, To->BaseFilename); - } - - int NewPrevEnd = 0; - int NewNextEnd = 0; - switch(LinkDirection) - { - case LINK_BACKWARDS: - { - fwrite(HTML.Buffer.Location, From->LinkOffsets.PrevStart, 1, HTML.Handle); - if(To) - { - CopyStringToBuffer(&Link, - "
Previous: '%s'
\n", - ToPlayerURL.Location, - To->Title); - } - else - { - CopyStringToBuffer(&Link, - "
Welcome to %s
\n", DB.EntriesHeader.ProjectName); - } - NewPrevEnd = Link.Ptr - Link.Location; - fwrite(Link.Location, (Link.Ptr - Link.Location), 1, HTML.Handle); - if(FromHasOneNeighbour) - { - fwrite(HTML.Buffer.Location + From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd, From->LinkOffsets.NextStart, 1, HTML.Handle); - RewindBuffer(&Link); - CopyStringToBuffer(&Link, - "
You have arrived at the (current) end of %s
\n", DB.EntriesHeader.ProjectName); - NewNextEnd = Link.Ptr - Link.Location; - fwrite(Link.Location, NewNextEnd, 1, HTML.Handle); - fwrite(HTML.Buffer.Location + From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd + From->LinkOffsets.NextStart + From->LinkOffsets.NextEnd, - HTML.FileSize - (From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd + From->LinkOffsets.NextStart + From->LinkOffsets.NextEnd), - 1, - HTML.Handle); - } - else - { - fwrite(HTML.Buffer.Location + From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd, - HTML.FileSize - (From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd), - 1, - HTML.Handle); - } - - From->LinkOffsets.PrevEnd = NewPrevEnd; - if(FromHasOneNeighbour) { From->LinkOffsets.NextEnd = NewNextEnd; } - } break; - case LINK_FORWARDS: - { - if(FromHasOneNeighbour) - { - fwrite(HTML.Buffer.Location, From->LinkOffsets.PrevStart, 1, HTML.Handle); - CopyStringToBuffer(&Link, - "
Welcome to %s
\n", DB.EntriesHeader.ProjectName); - NewPrevEnd = Link.Ptr - Link.Location; - fwrite(Link.Location, NewPrevEnd, 1, HTML.Handle); - RewindBuffer(&Link); - fwrite(HTML.Buffer.Location + From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd, - From->LinkOffsets.NextStart, 1, HTML.Handle); - } - else - { - fwrite(HTML.Buffer.Location, From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd + From->LinkOffsets.NextStart, 1, HTML.Handle); - } - - if(To) - { - CopyStringToBuffer(&Link, - "
Next: '%s'
\n", - ToPlayerURL.Location, - To->Title); - } - else - { - CopyStringToBuffer(&Link, - "
You have arrived at the (current) end of %s
\n", DB.EntriesHeader.ProjectName); - } - NewNextEnd = Link.Ptr - Link.Location; - fwrite(Link.Location, (Link.Ptr - Link.Location), 1, HTML.Handle); - - fwrite(HTML.Buffer.Location + From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd + From->LinkOffsets.NextStart + From->LinkOffsets.NextEnd, - HTML.FileSize - (From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd + From->LinkOffsets.NextStart + From->LinkOffsets.NextEnd), - 1, - HTML.Handle); - - if(FromHasOneNeighbour) { From->LinkOffsets.PrevEnd = NewPrevEnd; } - From->LinkOffsets.NextEnd = NewNextEnd; - } break; - } - - if(To) { DeclaimBuffer(&ToPlayerURL); } - DeclaimBuffer(&Link); - fclose(HTML.Handle); - FreeBuffer(&HTML.Buffer); - SnipeEntryIntoMetadataBuffer(From, FromIndex); - return RC_SUCCESS; - } - else - { - return RC_ERROR_FILE; - } -} - -int -DeleteNeighbourLinks(neighbourhood *N) -{ - db_entry *Entry; - int EntryIndex; - if(N->PrevIndex >= 0) - { - Entry = &N->Prev; - EntryIndex = N->PrevIndex; - - N->PreLinkPrevOffsetTotal = N->Prev.LinkOffsets.PrevEnd - + N->Prev.LinkOffsets.NextStart - + N->Prev.LinkOffsets.NextEnd; - } - else - { - Entry = &N->Next; - EntryIndex = N->NextIndex; - - N->PreLinkNextOffsetTotal = N->Next.LinkOffsets.PrevEnd - + N->Next.LinkOffsets.NextStart - + N->Next.LinkOffsets.NextEnd; - } - - file_buffer HTML; - if(ReadPlayerPageIntoBuffer(&HTML, Entry) == RC_SUCCESS) - { - if(!(HTML.Handle = fopen(HTML.Path, "w"))) { FreeBuffer(&HTML.Buffer); return RC_ERROR_FILE; }; - - fwrite(HTML.Buffer.Location, Entry->LinkOffsets.PrevStart, 1, HTML.Handle); - fwrite(HTML.Buffer.Location + Entry->LinkOffsets.PrevStart + Entry->LinkOffsets.PrevEnd, Entry->LinkOffsets.NextStart, 1, HTML.Handle); - fwrite(HTML.Buffer.Location + Entry->LinkOffsets.PrevStart + Entry->LinkOffsets.PrevEnd + Entry->LinkOffsets.NextStart + Entry->LinkOffsets.NextEnd, - HTML.FileSize - (Entry->LinkOffsets.PrevStart + Entry->LinkOffsets.PrevEnd + Entry->LinkOffsets.NextStart + Entry->LinkOffsets.NextEnd), - 1, - HTML.Handle); - fclose(HTML.Handle); - Entry->LinkOffsets.PrevEnd = 0; - Entry->LinkOffsets.NextEnd = 0; - if(N->PrevIndex >= 0) - { - N->PrevOffsetModifier = N->Prev.LinkOffsets.PrevEnd - + N->Prev.LinkOffsets.NextStart - + N->Prev.LinkOffsets.NextEnd - - N->PreLinkPrevOffsetTotal; - } - else - { - N->NextOffsetModifier = N->Next.LinkOffsets.PrevEnd - + N->Next.LinkOffsets.NextStart - + N->Next.LinkOffsets.NextEnd - - N->PreLinkNextOffsetTotal; - } - FreeBuffer(&HTML.Buffer); - SnipeEntryIntoMetadataBuffer(Entry, EntryIndex); - } - - return RC_SUCCESS; -} - -void -LinkToNewEntry(neighbourhood *N) -{ - N->PreLinkThisOffsetTotal = N->This.LinkOffsets.PrevEnd - + N->This.LinkOffsets.NextStart - + N->This.LinkOffsets.NextEnd; - - if(N->PrevIndex >= 0) - { - N->PreLinkPrevOffsetTotal = N->Prev.LinkOffsets.PrevEnd - + N->Prev.LinkOffsets.NextStart - + N->Prev.LinkOffsets.NextEnd; - - InsertNeighbourLink(&N->Prev, N->PrevIndex, &N->This, LINK_FORWARDS, N->FormerIsFirst); - - N->PrevOffsetModifier = N->Prev.LinkOffsets.PrevEnd - + N->Prev.LinkOffsets.NextStart - + N->Prev.LinkOffsets.NextEnd - - N->PreLinkPrevOffsetTotal; - } - - if(N->NextIndex >= 0) - { - N->PreLinkNextOffsetTotal = N->Next.LinkOffsets.PrevEnd - + N->Next.LinkOffsets.NextStart - + N->Next.LinkOffsets.NextEnd; - - InsertNeighbourLink(&N->Next, N->NextIndex, &N->This, LINK_BACKWARDS, N->LatterIsFinal); - - N->NextOffsetModifier = N->Next.LinkOffsets.PrevEnd - + N->Next.LinkOffsets.NextStart - + N->Next.LinkOffsets.NextEnd - - N->PreLinkNextOffsetTotal; - } -} - -void -MarkNextAsFirst(neighbourhood *N) -{ - file_buffer HTML; - ReadPlayerPageIntoBuffer(&HTML, &N->Next); - - buffer Link; - ClaimBuffer(&Link, "Link", Kilobytes(4)); - - HTML.Handle = fopen(HTML.Path, "w"); - - fwrite(HTML.Buffer.Location, N->Next.LinkOffsets.PrevStart, 1, HTML.Handle); - - CopyStringToBuffer(&Link, - "
Welcome to %s
\n", DB.EntriesHeader.ProjectName); - - fwrite(Link.Location, (Link.Ptr - Link.Location), 1, HTML.Handle); - - fwrite(HTML.Buffer.Location + N->Next.LinkOffsets.PrevStart + N->Next.LinkOffsets.PrevEnd, - HTML.FileSize - (N->Next.LinkOffsets.PrevStart + N->Next.LinkOffsets.PrevEnd), - 1, - HTML.Handle); - - N->Next.LinkOffsets.PrevEnd = Link.Ptr - Link.Location; - - DeclaimBuffer(&Link); - fclose(HTML.Handle); - FreeBuffer(&HTML.Buffer); - - SnipeEntryIntoMetadataBuffer(&N->Next, N->NextIndex); -} - -void -MarkPrevAsFinal(neighbourhood *N) -{ - file_buffer File; - ReadPlayerPageIntoBuffer(&File, &N->Prev); - - File.Handle = fopen(File.Path, "w"); - buffer Link; - ClaimBuffer(&Link, "Link", Kilobytes(4)); - - - fwrite(File.Buffer.Location, N->Prev.LinkOffsets.PrevStart + N->Prev.LinkOffsets.PrevEnd + N->Prev.LinkOffsets.NextStart, 1, File.Handle); - - CopyStringToBuffer(&Link, - "
You have arrived at the (current) end of %s
\n", DB.EntriesHeader.ProjectName); - - fwrite(Link.Location, (Link.Ptr - Link.Location), 1, File.Handle); - - fwrite(File.Buffer.Location + N->Prev.LinkOffsets.PrevStart + N->Prev.LinkOffsets.PrevEnd + N->Prev.LinkOffsets.NextStart + N->Prev.LinkOffsets.NextEnd, - File.FileSize - (N->Prev.LinkOffsets.PrevStart + N->Prev.LinkOffsets.PrevEnd + N->Prev.LinkOffsets.NextStart + N->Prev.LinkOffsets.NextEnd), - 1, - File.Handle); - - N->Prev.LinkOffsets.NextEnd = Link.Ptr - Link.Location; - - DeclaimBuffer(&Link); - fclose(File.Handle); - FreeBuffer(&File.Buffer); - - SnipeEntryIntoMetadataBuffer(&N->Prev, N->PrevIndex); -} - -void -LinkOverDeletedEntry(neighbourhood *N) -{ - if(DB.EntriesHeader.Count == 1) - { - DeleteNeighbourLinks(N); - } - else - { - if(N->DeletedEntryWasFirst) - { - N->PreLinkNextOffsetTotal = N->Next.LinkOffsets.PrevEnd - + N->Next.LinkOffsets.NextStart - + N->Next.LinkOffsets.NextEnd; - - MarkNextAsFirst(N); - - N->NextOffsetModifier = N->Next.LinkOffsets.PrevEnd - + N->Next.LinkOffsets.NextStart - + N->Next.LinkOffsets.NextEnd - - N->PreLinkNextOffsetTotal; - return; - } - else if(N->DeletedEntryWasFinal) - { - N->PreLinkPrevOffsetTotal = N->Prev.LinkOffsets.PrevEnd - + N->Prev.LinkOffsets.NextStart - + N->Prev.LinkOffsets.NextEnd; - - MarkPrevAsFinal(N); - - N->PrevOffsetModifier = N->Prev.LinkOffsets.PrevEnd - + N->Prev.LinkOffsets.NextStart - + N->Prev.LinkOffsets.NextEnd - - N->PreLinkPrevOffsetTotal; - return; - } - else - { - // Assert(N->PrevIndex >= 0 && N->NextIndex >= 0) - N->PreLinkPrevOffsetTotal = N->Prev.LinkOffsets.PrevEnd - + N->Prev.LinkOffsets.NextStart - + N->Prev.LinkOffsets.NextEnd; - - - N->PreLinkNextOffsetTotal = N->Next.LinkOffsets.PrevEnd - + N->Next.LinkOffsets.NextStart - + N->Next.LinkOffsets.NextEnd; - } - - InsertNeighbourLink(&N->Prev, N->PrevIndex, &N->Next, LINK_FORWARDS, N->FormerIsFirst); - InsertNeighbourLink(&N->Next, N->NextIndex, &N->Prev, LINK_BACKWARDS, N->LatterIsFinal); - - N->PrevOffsetModifier = N->Prev.LinkOffsets.PrevEnd - + N->Prev.LinkOffsets.NextStart - + N->Prev.LinkOffsets.NextEnd - - N->PreLinkPrevOffsetTotal; - - N->NextOffsetModifier = N->Next.LinkOffsets.PrevEnd - + N->Next.LinkOffsets.NextStart - + N->Next.LinkOffsets.NextEnd - - N->PreLinkNextOffsetTotal; - } -} - -void -LinkNeighbours(neighbourhood *N, enum8(link_types) LinkType) -{ - if(LinkType == LINK_INCLUDE) - { - LinkToNewEntry(N); - } - else - { - LinkOverDeletedEntry(N); - } -} - -void -DeleteSearchPageFromFilesystem() // NOTE(matt): Do we need to handle relocating, like the PlayerPage function? -{ - buffer SearchDirectory; - ClaimBuffer(&SearchDirectory, "SearchDirectory", MAX_BASE_DIR_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + 10); - ConstructDirectoryPath(&SearchDirectory, PAGE_SEARCH, Config.SearchLocation, ""); - char SearchPagePath[1024]; - CopyString(SearchPagePath, sizeof(SearchPagePath), "%s/index.html", SearchDirectory.Location); - remove(SearchPagePath); - remove(SearchDirectory.Location); - DeclaimBuffer(&SearchDirectory); -} - -int -DeletePlayerPageFromFilesystem(char *BaseFilename, char *PlayerLocation, bool Relocating) -{ - // NOTE(matt): Once we have the notion of an output filename format, we'll need to use that here - buffer OutputDirectoryPath; - ClaimBuffer(&OutputDirectoryPath, "OutputDirectoryPath", MAX_BASE_DIR_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_PLAYER_URL_PREFIX_LENGTH + MAX_BASE_FILENAME_LENGTH + 1 + 10); - ConstructDirectoryPath(&OutputDirectoryPath, PAGE_PLAYER, PlayerLocation, BaseFilename); - DIR *PlayerDir; - - if((PlayerDir = opendir(OutputDirectoryPath.Location))) // There is a directory for the Player, which there probably should be if not for manual intervention - { - char PlayerPagePath[256]; - CopyString(PlayerPagePath, sizeof(PlayerPagePath), "%s/index.html", OutputDirectoryPath.Location); - FILE *PlayerPage; - if((PlayerPage = fopen(PlayerPagePath, "r"))) - { - fclose(PlayerPage); - remove(PlayerPagePath); - } - - closedir(PlayerDir); - if((remove(OutputDirectoryPath.Location) == -1)) - { - LogError(LOG_NOTICE, "Mostly deleted %s. Unable to remove directory %s: %s", BaseFilename, OutputDirectoryPath.Location, strerror(errno)); - fprintf(stderr, "%sMostly deleted%s %s. %sUnable to remove directory%s %s: %s", ColourStrings[EditTypes[EDIT_DELETION].Colour], ColourStrings[CS_END], BaseFilename, ColourStrings[CS_ERROR], ColourStrings[CS_END], OutputDirectoryPath.Location, strerror(errno)); - } - else - { - if(!Relocating) - { - LogError(LOG_INFORMATIONAL, "Deleted %s", BaseFilename); - fprintf(stderr, "%sDeleted%s %s\n", ColourStrings[EditTypes[EDIT_DELETION].Colour], ColourStrings[CS_END], BaseFilename); - } - - } - } - DeclaimBuffer(&OutputDirectoryPath); - return RC_SUCCESS; -} - -int -DeleteFromDB(neighbourhood *N, char *BaseFilename) -{ - // TODO(matt): LogError() - DB.Header = *(db_header *)DB.Metadata.Buffer.Location; - DB.EntriesHeader = *(db_header_entries *)(DB.Metadata.Buffer.Location + sizeof(DB.Header)); - - db_entry *Entry = { 0 }; - int EntryIndex = BinarySearchForMetadataEntry(&Entry, BaseFilename); - if(Entry) - { - int DeleteFileFrom = AccumulateDBEntryInsertionOffset(EntryIndex); - int DeleteFileTo = DeleteFileFrom + Entry->Size; - N->ThisIndex = EntryIndex; - GetNeighbourhood(N, EDIT_DELETION); - --DB.EntriesHeader.Count; - - if(DB.EntriesHeader.Count == 0) - { - // TODO(matt): Handle this differently, allowing 0 entries but > 0 assets? - DeleteSearchPageFromFilesystem(); - - remove(DB.Metadata.Path); - DB.Metadata.FileSize = 0; - FreeBuffer(&DB.Metadata.Buffer); - - remove(DB.File.Path); - DB.File.FileSize = 0; - FreeBuffer(&DB.File.Buffer); - } - else - { - if(!(DB.Metadata.Handle = fopen(DB.Metadata.Path, "w"))) { FreeBuffer(&DB.Metadata.Buffer); return RC_ERROR_FILE; } - if(!(DB.File.Handle = fopen(DB.File.Path, "w"))) { FreeBuffer(&DB.File.Buffer); return RC_ERROR_FILE; } - - fwrite(&DB.Header, sizeof(DB.Header), 1, DB.Metadata.Handle); - fwrite(&DB.EntriesHeader, sizeof(DB.EntriesHeader), 1, DB.Metadata.Handle); - fwrite(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader), sizeof(DB.Entry) * EntryIndex, 1, DB.Metadata.Handle); - fwrite(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * (EntryIndex + 1), - DB.Metadata.FileSize - sizeof(DB.Header) - sizeof(DB.EntriesHeader) - sizeof(DB.Entry) * (EntryIndex + 1), - 1, DB.Metadata.Handle); - CycleFile(&DB.Metadata); - - fwrite(DB.File.Buffer.Location, DeleteFileFrom, 1, DB.File.Handle); - fwrite(DB.File.Buffer.Location + DeleteFileTo, DB.File.FileSize - DeleteFileTo, 1, DB.File.Handle); - CycleFile(&DB.File); - } - } - - return Entry ? RC_SUCCESS : RC_NOOP; -} - -int -SearchToBuffer(buffers *CollationBuffers) // NOTE(matt): This guy malloc's CollationBuffers->Search -{ - if(DB.Metadata.Buffer.Location) - { - DB.Header = *(db_header *)DB.Metadata.Buffer.Location; - DB.EntriesHeader = *(db_header_entries *)(DB.Metadata.Buffer.Location + sizeof(DB.Header)); - if(DB.EntriesHeader.Count > 0) - { - RewindBuffer(&CollationBuffers->IncludesSearch); - - buffer URL; - ConstructResolvedAssetURL(&URL, ASSET_CSS_CINERA, PAGE_SEARCH); - CopyStringToBuffer(&CollationBuffers->IncludesSearch, - "\n" - " \n" - "\n" - " IncludesSearch, ASSET_CSS_CINERA, PAGE_SEARCH); - - ConstructResolvedAssetURL(&URL, ASSET_CSS_THEME, PAGE_SEARCH); - CopyStringToBuffer(&CollationBuffers->IncludesSearch, - "\">\n" - " IncludesSearch, ASSET_CSS_THEME, PAGE_SEARCH); - - ConstructResolvedAssetURL(&URL, ASSET_CSS_TOPICS, PAGE_SEARCH); - CopyStringToBuffer(&CollationBuffers->IncludesSearch, - "\">\n" - " IncludesSearch, ASSET_CSS_TOPICS, PAGE_SEARCH); - - CopyStringToBuffer(&CollationBuffers->IncludesSearch, - "\">\n"); - - int ProjectIndex; - for(ProjectIndex = 0; ProjectIndex < ArrayCount(ProjectInfo); ++ProjectIndex) - { - if(!StringsDiffer(ProjectInfo[ProjectIndex].ProjectID, Config.ProjectID)) - { - break; - } - } - - char *Theme = StringsDiffer(Config.Theme, "") ? Config.Theme : Config.ProjectID; - int Allowance = StringLength(Theme) * 2 + StringLength(Config.ProjectID) + StringLength(Config.BaseURL) + StringLength(Config.PlayerLocation); - char queryContainer[827 + Allowance]; // NOTE(matt): Update the size if changing the string - CopyString(queryContainer, sizeof(queryContainer), - "
\n" - "
Sort: Old to New ⏶
\n" - "
\n" - " \n" - "
\n" - " \n" - "
\n" - " Downloading data...\n" - "
\n" - "
\n" - "
\n" - "
\n" - "
Found: 0 episodes, 0 markers, 0h 0m 0s total.
\n" - "
\n" - "\n" - "
\n" - "
\n", - Theme, - Config.Mode & MODE_SINGLETAB ? 1 : 0, - Theme, - Config.ProjectID, - Config.BaseURL, - Config.PlayerLocation); - - ConstructResolvedAssetURL(&URL, ASSET_JS_SEARCH, PAGE_SEARCH); - buffer Script; - ClaimBuffer(&Script, "Script", (117 + URL.Ptr - URL.Location) * 2); // NOTE(matt): Update the size if changing the string - CopyStringToBuffer(&Script, - "
\n" - "
\n" - " "); - - buffer PlayerURL; - ClaimBuffer(&PlayerURL, "PlayerURL", MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_PLAYER_URL_PREFIX_LENGTH + MAX_BASE_FILENAME_LENGTH); - ConstructPlayerURL(&PlayerURL, ""); - char *ProjectUnit = 0; - if(StringsDiffer(ProjectInfo[ProjectIndex].Unit, "")) - { - ProjectUnit = ProjectInfo[ProjectIndex].Unit; - } - char Number[16]; - db_entry *This; - char Text[(ProjectUnit ? StringLength(ProjectUnit) : 0) + sizeof(Number) + sizeof(This->Title) + 3]; - - int EntryLength = StringLength(PlayerURL.Location) + sizeof(Text) + 82; - CollationBuffers->Search.Size = StringLength(queryContainer) + (DB.EntriesHeader.Count * EntryLength) + Script.Ptr - Script.Location; - - if(!(CollationBuffers->Search.Location = malloc(CollationBuffers->Search.Size))) { return RC_ERROR_MEMORY; } - - CollationBuffers->Search.ID = "Search"; - CollationBuffers->Search.Ptr = CollationBuffers->Search.Location; - - CopyStringToBuffer(&CollationBuffers->Search, "%s", queryContainer); - - int ProjectIDLength = StringLength(Config.ProjectID); - bool SearchRequired = FALSE; - - DB.Metadata.Buffer.Ptr = DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader); - for(int EntryIndex = 0; EntryIndex < DB.EntriesHeader.Count; ++EntryIndex, DB.Metadata.Buffer.Ptr += sizeof(DB.Entry)) - { - This = (db_entry *)DB.Metadata.Buffer.Ptr; - if(This->Size > 0) - { - SearchRequired = TRUE; - CopyString(Number, sizeof(Number), "%s", This->BaseFilename + ProjectIDLength); - if(ProjectInfo[ProjectIndex].NumberingScheme == NS_LINEAR) - { - for(int i = 0; Number[i]; ++i) - { - if(Number[i] == '_') - { - Number[i] = '.'; - } - } - } - - ConstructPlayerURL(&PlayerURL, This->BaseFilename); - - if(ProjectUnit) - { - CopyStringToBuffer(&CollationBuffers->Search, - " \n"); - } - else - { - CopyStringToBuffer(&CollationBuffers->Search, - " \n"); - } - } - } - - OffsetLandmarks(&CollationBuffers->Search, ASSET_JS_SEARCH, PAGE_SEARCH); - CopyBuffer(&CollationBuffers->Search, &Script); - - DeclaimBuffer(&PlayerURL); - DeclaimBuffer(&Script); - DeclaimBuffer(&URL); - - if(!SearchRequired) { return RC_NOOP; } - else { return RC_SUCCESS; } - } - } - return RC_NOOP; -} - -int -GeneratePlayerPage(neighbourhood *N, buffers *CollationBuffers, template *PlayerTemplate, char *BaseFilename, bool Reinserting) -{ - buffer OutputDirectoryPath; - ClaimBuffer(&OutputDirectoryPath, "OutputDirectoryPath", MAX_BASE_DIR_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_PLAYER_URL_PREFIX_LENGTH + MAX_BASE_FILENAME_LENGTH + 1 + 10); - ConstructDirectoryPath(&OutputDirectoryPath, PAGE_PLAYER, Config.PlayerLocation, BaseFilename); - - DIR *OutputDirectoryHandle; - if(!(OutputDirectoryHandle = opendir(OutputDirectoryPath.Location))) // TODO(matt): open() - { - if(MakeDir(OutputDirectoryPath.Location) == RC_ERROR_DIRECTORY) - { - LogError(LOG_ERROR, "Unable to create directory %s: %s", OutputDirectoryPath.Location, strerror(errno)); - fprintf(stderr, "Unable to create directory %s: %s\n", OutputDirectoryPath.Location, strerror(errno)); - return RC_ERROR_DIRECTORY; - }; - } - closedir(OutputDirectoryHandle); - - char PlayerPagePath[1024]; - CopyString(PlayerPagePath, sizeof(PlayerPagePath), "%s/index.html", OutputDirectoryPath.Location); - DeclaimBuffer(&OutputDirectoryPath); - - bool SearchInTemplate = FALSE; - for(int TagIndex = 0; TagIndex < PlayerTemplate->Metadata.TagCount; ++TagIndex) - { - if(PlayerTemplate->Metadata.Tags[TagIndex].TagCode == TAG_SEARCH) - { - SearchInTemplate = TRUE; - SearchToBuffer(CollationBuffers); - break; - } - } - - BuffersToHTML(CollationBuffers, PlayerTemplate, PlayerPagePath, PAGE_PLAYER, &N->This.LinkOffsets.PrevStart); - // NOTE(matt): A previous InsertNeighbourLink() call will have done SnipeEntryIntoMetadataBuffer(), but we must do it here now - // that PrevStart has been adjusted by BuffersToHTML() - SnipeEntryIntoMetadataBuffer(&N->This, N->ThisIndex); - UpdateLandmarksForNeighbourhood(N, Reinserting ? EDIT_REINSERTION : EDIT_ADDITION); - - if(SearchInTemplate) - { - FreeBuffer(&CollationBuffers->Search); - } - return RC_SUCCESS; -} - -int -GenerateSearchPage(buffers *CollationBuffers, template *SearchTemplate) -{ - buffer OutputDirectoryPath; - ClaimBuffer(&OutputDirectoryPath, "OutputDirectoryPath", MAX_BASE_DIR_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + 10); - ConstructDirectoryPath(&OutputDirectoryPath, PAGE_SEARCH, Config.SearchLocation, 0); - - DIR *OutputDirectoryHandle; - if(!(OutputDirectoryHandle = opendir(OutputDirectoryPath.Location))) // TODO(matt): open() - { - if(MakeDir(OutputDirectoryPath.Location) == RC_ERROR_DIRECTORY) - { - LogError(LOG_ERROR, "Unable to create directory %s: %s", OutputDirectoryPath.Location, strerror(errno)); - fprintf(stderr, "Unable to create directory %s: %s\n", OutputDirectoryPath.Location, strerror(errno)); - return RC_ERROR_DIRECTORY; - }; - } - closedir(OutputDirectoryHandle); - - char SearchPagePath[1024]; - CopyString(SearchPagePath, sizeof(SearchPagePath), "%s/index.html", OutputDirectoryPath.Location); - DeclaimBuffer(&OutputDirectoryPath); - switch(SearchToBuffer(CollationBuffers)) - { - case RC_SUCCESS: - { - BuffersToHTML(CollationBuffers, SearchTemplate, SearchPagePath, PAGE_SEARCH, 0); - UpdateLandmarksForSearch(); - break; - } - case RC_NOOP: - { - DeleteSearchPageFromFilesystem(); - DeleteLandmarksForSearch(); - break; - } - } - FreeBuffer(&CollationBuffers->Search); - return RC_SUCCESS; -} - -int -DeleteEntry(neighbourhood *Neighbourhood, char *BaseFilename) -{ - if(DeleteFromDB(Neighbourhood, BaseFilename) == RC_SUCCESS) - { - LinkNeighbours(Neighbourhood, LINK_EXCLUDE); - DeletePlayerPageFromFilesystem(BaseFilename, Config.PlayerLocation, FALSE); - UpdateLandmarksForNeighbourhood(Neighbourhood, EDIT_DELETION); - return RC_SUCCESS; - } - return RC_NOOP; -} - -int -InsertEntry(neighbourhood *Neighbourhood, buffers *CollationBuffers, template *PlayerTemplate, template *BespokeTemplate, char *BaseFilename, bool RecheckingPrivacy) -{ - bool Reinserting = FALSE; - if(InsertIntoDB(Neighbourhood, CollationBuffers, BespokeTemplate, BaseFilename, RecheckingPrivacy, &Reinserting) == RC_SUCCESS) - { - LinkNeighbours(Neighbourhood, LINK_INCLUDE); - if(BespokeTemplate->File.Buffer.Location) - { - GeneratePlayerPage(Neighbourhood, CollationBuffers, BespokeTemplate, BaseFilename, Reinserting); - FreeTemplate(BespokeTemplate); - } - else - { - GeneratePlayerPage(Neighbourhood, CollationBuffers, PlayerTemplate, BaseFilename, Reinserting); - } - return RC_SUCCESS; - } - return RC_NOOP; -} - -int -RecheckPrivacy(buffers *CollationBuffers, template *SearchTemplate, template *PlayerTemplate, template *BespokeTemplate) -{ - if(DB.Metadata.FileSize > 0) - { - DB.Header = *(db_header *)DB.Metadata.Buffer.Location; - DB.EntriesHeader = *(db_header_entries *)(DB.Metadata.Buffer.Location + sizeof(DB.Header)); - db_entry Entry = { }; - int PrivateEntryIndex = 0; - db_entry PrivateEntries[DB.EntriesHeader.Count]; - bool Inserted = FALSE; - for(int IndexEntry = 0; IndexEntry < DB.EntriesHeader.Count; ++IndexEntry) - { - Entry = *(db_entry *)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * IndexEntry); - if(Entry.Size == 0) - { - PrivateEntries[PrivateEntryIndex] = Entry; - ++PrivateEntryIndex; - } - } - - for(int i = 0; i < PrivateEntryIndex; ++i) - { - neighbourhood Neighbourhood = { }; - InitNeighbourhood(&Neighbourhood); - ResetAssetLandmarks(); - Inserted = (InsertEntry(&Neighbourhood, CollationBuffers, PlayerTemplate, BespokeTemplate, PrivateEntries[i].BaseFilename, TRUE) == RC_SUCCESS); - } - - if(Inserted) - { - GenerateSearchPage(CollationBuffers, SearchTemplate); - DeleteStaleAssets(); - } - - LastPrivacyCheck = time(0); - } - return RC_SUCCESS; -} - #define DEBUG_LANDMARKS 0 #if DEBUG_LANDMARKS +#define VerifyLandmarks(N) VerifyLandmarks_(N, __LINE__) void -VerifyLandmarks(void) +VerifyLandmarks_(neighbourhood *N, int LineNumber) { - printf("\n" - "VerifyLandmarks()\n"); + fprintf(stderr, "%sVerifyLandmarks(%u)%s\n", ColourStrings[CS_MAGENTA], LineNumber, ColourStrings[CS_END]); + + db_block_assets *Block = LocateBlock(B_ASET); + db_asset *Asset = LocateFirstAsset(Block); + for(int i = 0; i < Block->Count; ++i) + { + db_landmark *Landmark = LocateFirstLandmark(Asset); + for(int j = 0; j < Asset->LandmarkCount; ++j, ++Landmark) + { + if(j + 1 < Asset->LandmarkCount) + { + db_landmark *Next = Landmark + 1; + if((ProjectIndicesDiffer(Next->Project, Landmark->Project) < 0) || + (ProjectIndicesMatch(Next->Project, Landmark->Project) && Next->EntryIndex < Landmark->EntryIndex)) + { + PrintC(CS_ERROR, "We fail, sadly\n"); + PrintAssetsBlock(0); + PrintLandmark(Landmark); + fprintf(stderr, " vs "); + PrintLandmark(Next); + fprintf(stderr, "\n"); + PrintNeighbourhood(N); + _exit(1); + } + } + } + Asset = SkipAsset(Asset); + } + +#if 0 DB.Metadata.Buffer.Ptr = DB.Metadata.Buffer.Location; DB.Header = *(db_header *)DB.Metadata.Buffer.Ptr; DB.Metadata.Buffer.Ptr += sizeof(DB.Header); @@ -7783,7 +11368,7 @@ VerifyLandmarks(void) char *FirstAsset = DB.Metadata.Buffer.Ptr; buffer OutputDirectoryPath; - ClaimBuffer(&OutputDirectoryPath, "OutputDirectoryPath", MAX_BASE_DIR_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_PLAYER_URL_PREFIX_LENGTH + MAX_BASE_FILENAME_LENGTH + 1 + 10); + ClaimBuffer(&OutputDirectoryPath, "OutputDirectoryPath", MAX_BASE_DIR_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_BASE_FILENAME_LENGTH + 1 + 10); bool UniversalSuccess = TRUE; @@ -7796,14 +11381,14 @@ VerifyLandmarks(void) { DB.Asset = *(db_asset *)Cursor; Cursor += sizeof(DB.Asset); - landmark_range Target = BinarySearchForMetadataLandmark(Cursor, PAGE_TYPE_SEARCH, DB.Asset.LandmarkCount); + landmark_range Target = BinarySearchForMetadataLandmark(Cursor, CurrentProject->Index, SP_SEARCH, DB.Asset.LandmarkCount); for(int k = 0; k < Target.Length; ++k) { DB.Landmark = *(db_landmark *)(Cursor + sizeof(db_landmark) * (Target.First + k)); bool Found = FALSE; for(int l = 0; l < Assets.Count; ++l) { - // TODO(matt): StringToInt (base16) + // TODO(matt): String0ToInt (base16) char HashString[9]; sprintf(HashString, "%08x", Assets.Asset[l].Hash); @@ -7829,24 +11414,24 @@ VerifyLandmarks(void) for(int i = 0; i < DB.EntriesHeader.Count; ++i) { DB.Entry = *(db_entry *)(FirstEntry + sizeof(DB.Entry) * i); - ConstructDirectoryPath(&OutputDirectoryPath, PAGE_PLAYER, Config.PlayerLocation, DB.Entry.BaseFilename); + ConstructDirectoryPath(&OutputDirectoryPath, PAGE_PLAYER, CurrentProject->PlayerLocation, DB.Entry.BaseFilename); file_buffer HTML; CopyString(HTML.Path, sizeof(HTML.Path), "%s/index.html", OutputDirectoryPath.Location); - if(ReadFileIntoBuffer(&HTML, 0) == RC_SUCCESS) + if(ReadFileIntoBuffer_OLD(&HTML, 0) == RC_SUCCESS) { char *Cursor = FirstAsset; for(int j = 0; j < DB.AssetsHeader.Count; ++j) { DB.Asset = *(db_asset *)Cursor; Cursor += sizeof(DB.Asset); - landmark_range Target = BinarySearchForMetadataLandmark(Cursor, i, DB.Asset.LandmarkCount); + landmark_range Target = BinarySearchForMetadataLandmark(Cursor, CurrentProject->Index, i, DB.Asset.LandmarkCount); for(int k = 0; k < Target.Length; ++k) { DB.Landmark = *(db_landmark *)(Cursor + sizeof(db_landmark) * (Target.First + k)); bool Found = FALSE; for(int l = 0; l < Assets.Count; ++l) { - // TODO(matt): StringToInt (base16) + // TODO(matt): String0ToInt (base16) char HashString[9]; sprintf(HashString, "%08x", Assets.Asset[l].Hash); @@ -7876,21 +11461,3193 @@ VerifyLandmarks(void) Assert(UniversalSuccess); printf("Success! All landmarks correspond to asset hashes\n"); DeclaimBuffer(&OutputDirectoryPath); -} #endif +} +#else +#define VerifyLandmarks(N) +#endif + +void +UpdateLandmarksForSearch(neighbourhood *N, db_project_index ProjectIndex) +{ + if(Config->QueryString.Length > 0) + { + for(int i = 0; i < Assets.Count; ++i) + { + asset *This = GetPlaceInBook(&Assets.Asset, i); + This->Known = FALSE; + } + + WritePastAssetsHeader(); + SetFileEditPosition(&DB.Metadata); + DB.Metadata.Signposts.AssetsBlock.Ptr = LocateBlock(B_ASET); + db_block_assets *AssetsBlock = DB.Metadata.Signposts.AssetsBlock.Ptr; + db_asset *Asset = LocateFirstAsset(AssetsBlock); + for(int AssetIndex = 0; AssetIndex < AssetsBlock->Count; ++AssetIndex) + { + db_landmark *FirstLandmark = LocateFirstLandmark(Asset); + uint64_t ExistingLandmarkCount = Asset->LandmarkCount; + + for(int i = 0; i < Assets.Count; ++i) + { + asset *This = GetPlaceInBook(&Assets.Asset, i); + if(!StringsDiffer0(Asset->Filename, This->Filename) && Asset->Type == This->Type) + { + This->Known = TRUE; + + landmark_range ProjectRange = DetermineProjectLandmarksRange(Asset, ProjectIndex); + landmark_range Target = BinarySearchForMetadataLandmark(Asset, ProjectRange, SP_SEARCH); + + db_asset NewAsset = *Asset; + NewAsset.LandmarkCount += This->SearchLandmarkCount - Target.Length; + + fwrite(&NewAsset, sizeof(db_asset), 1, DB.Metadata.File.Handle); + //AccumulateFileEditSize(&DB.Metadata, sizeof(db_asset)); + + fwrite(FirstLandmark, sizeof(db_landmark), Target.First, DB.Metadata.File.Handle); + //AccumulateFileEditSize(&DB.Metadata, sizeof(db_landmark) * Target.First); + uint64_t ToWrite = ExistingLandmarkCount - Target.First; + + for(int j = 0; j < This->SearchLandmarkCount; ++j) + { + db_landmark Landmark = {}; + Landmark.Project = ProjectIndex; + Landmark.EntryIndex = SP_SEARCH; + Landmark.Position = This->Search[j].Offset; + fwrite(&Landmark, sizeof(Landmark), 1, DB.Metadata.File.Handle); + //AccumulateFileEditSize(&DB.Metadata, sizeof(db_landmark)); + } + + db_landmark *TrailingLandmark = FirstLandmark + Target.First + Target.Length; + AccumulateFileEditSize(&DB.Metadata, sizeof(db_landmark) * (This->SearchLandmarkCount - Target.Length)); + ToWrite -= Target.Length; + fwrite(TrailingLandmark, sizeof(db_landmark), ToWrite, DB.Metadata.File.Handle); + break; + } + } + Asset = SkipAsset(Asset); + } + CycleSignpostedFile(&DB.Metadata); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + VerifyLandmarks(N); + + bool NewAsset = FALSE; + for(int InternalAssetIndex = 0; InternalAssetIndex < Assets.Count; ++InternalAssetIndex) + { + asset *This = GetPlaceInBook(&Assets.Asset, InternalAssetIndex); + if(!This->Known && This->SearchLandmarkCount > 0) + { + NewAsset = TRUE; + UpdateAssetInDB(This); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + } + } + if(NewAsset) { UpdateLandmarksForSearch(N, ProjectIndex); } + } +} + +enum +{ + LINK_INCLUDE, + LINK_EXCLUDE +} link_types; + +enum +{ + LINK_FORWARDS, + LINK_BACKWARDS +} link_directions; + +int +InsertNeighbourLink(db_header_project *P, db_entry *From, db_entry *To, enum8(link_directions) LinkDirection, bool FromHasOneNeighbour) +{ + MEM_TEST_INITIAL(); + file HTML = {}; + ReadPlayerPageIntoBuffer(P, &HTML, From); + + MEM_TEST_MID("InsertNeighbourLink1"); + if(HTML.Buffer.Location) + { + if(!(HTML.Handle = fopen(HTML.Path, "w"))) { FreeFile(&HTML); return RC_ERROR_FILE; }; + + buffer Link = {}; + ClaimBuffer(&Link, BID_LINK, Kilobytes(4)); + + buffer ToPlayerURL = {}; + if(To) + { + ClaimBuffer(&ToPlayerURL, BID_TO_PLAYER_URL, MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_BASE_FILENAME_LENGTH); + ConstructPlayerURL(&ToPlayerURL, P, Wrap0i(To->OutputLocation, sizeof(To->OutputLocation))); + } + + int NewPrevEnd = 0; + int NewNextEnd = 0; + switch(LinkDirection) + { + case LINK_BACKWARDS: + { + fwrite(HTML.Buffer.Location, From->LinkOffsets.PrevStart, 1, HTML.Handle); + if(To) + { + CopyStringToBuffer(&Link, + "
Previous: '%s'
\n", + ToPlayerURL.Location, + To->Title); + } + else + { + // TODO(matt): Be careful of this! Is this CurrentProject->Title definitely set to the right thing? + CopyStringToBuffer(&Link, + "
Welcome to %.*s
\n", (int)CurrentProject->Title.Length, CurrentProject->Title.Base); + } + NewPrevEnd = Link.Ptr - Link.Location; + fwrite(Link.Location, (Link.Ptr - Link.Location), 1, HTML.Handle); + if(FromHasOneNeighbour) + { + fwrite(HTML.Buffer.Location + From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd, From->LinkOffsets.NextStart, 1, HTML.Handle); + RewindBuffer(&Link); + CopyStringToBuffer(&Link, + "
You have arrived at the (current) end of %.*s
\n", (int)CurrentProject->Title.Length, CurrentProject->Title.Base); + NewNextEnd = Link.Ptr - Link.Location; + fwrite(Link.Location, NewNextEnd, 1, HTML.Handle); + fwrite(HTML.Buffer.Location + From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd + From->LinkOffsets.NextStart + From->LinkOffsets.NextEnd, + HTML.Buffer.Size - (From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd + From->LinkOffsets.NextStart + From->LinkOffsets.NextEnd), + 1, + HTML.Handle); + } + else + { + fwrite(HTML.Buffer.Location + From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd, + HTML.Buffer.Size - (From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd), + 1, + HTML.Handle); + } + + From->LinkOffsets.PrevEnd = NewPrevEnd; + if(FromHasOneNeighbour) { From->LinkOffsets.NextEnd = NewNextEnd; } + } break; + case LINK_FORWARDS: + { + if(FromHasOneNeighbour) + { + fwrite(HTML.Buffer.Location, From->LinkOffsets.PrevStart, 1, HTML.Handle); + CopyStringToBuffer(&Link, + "
Welcome to %.*s
\n", (int)CurrentProject->Title.Length, CurrentProject->Title.Base); + NewPrevEnd = Link.Ptr - Link.Location; + fwrite(Link.Location, NewPrevEnd, 1, HTML.Handle); + RewindBuffer(&Link); + fwrite(HTML.Buffer.Location + From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd, + From->LinkOffsets.NextStart, 1, HTML.Handle); + } + else + { + fwrite(HTML.Buffer.Location, From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd + From->LinkOffsets.NextStart, 1, HTML.Handle); + } + + if(To) + { + CopyStringToBuffer(&Link, + "
Next: '%s'
\n", + ToPlayerURL.Location, + To->Title); + } + else + { + CopyStringToBuffer(&Link, + "
You have arrived at the (current) end of %.*s
\n", (int)CurrentProject->Title.Length, CurrentProject->Title.Base); + } + NewNextEnd = Link.Ptr - Link.Location; + fwrite(Link.Location, (Link.Ptr - Link.Location), 1, HTML.Handle); + + fwrite(HTML.Buffer.Location + From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd + From->LinkOffsets.NextStart + From->LinkOffsets.NextEnd, + HTML.Buffer.Size - (From->LinkOffsets.PrevStart + From->LinkOffsets.PrevEnd + From->LinkOffsets.NextStart + From->LinkOffsets.NextEnd), + 1, + HTML.Handle); + + if(FromHasOneNeighbour) { From->LinkOffsets.PrevEnd = NewPrevEnd; } + From->LinkOffsets.NextEnd = NewNextEnd; + } break; + } + + if(To) { DeclaimBuffer(&ToPlayerURL); } + DeclaimBuffer(&Link); + fclose(HTML.Handle); + HTML.Handle = 0; + FreeFile(&HTML); + MEM_TEST_AFTER("InsertNeighbourLink()"); + return RC_SUCCESS; + } + else + { + FreeFile(&HTML); + MEM_TEST_AFTER("InsertNeighbourLink()"); + return RC_ERROR_FILE; + } +} + +int +DeleteNeighbourLinks(neighbourhood *N) +{ + // TODO(matt): Compression Oriented Programming! + db_entry *Entry; + if(N->Prev) + { + Entry = N->Prev; + + N->PreLinkPrevOffsetTotal = N->Prev->LinkOffsets.PrevEnd + + N->Prev->LinkOffsets.NextStart + + N->Prev->LinkOffsets.NextEnd; + } + else + { + Entry = N->Next; + + N->PreLinkNextOffsetTotal = N->Next->LinkOffsets.PrevEnd + + N->Next->LinkOffsets.NextStart + + N->Next->LinkOffsets.NextEnd; + } + + file HTML = {}; + ReadPlayerPageIntoBuffer(N->Project, &HTML, Entry); + if(HTML.Buffer.Location) + { + if(!(HTML.Handle = fopen(HTML.Path, "w"))) { FreeFile(&HTML); return RC_ERROR_FILE; }; + fwrite(HTML.Buffer.Location, Entry->LinkOffsets.PrevStart, 1, HTML.Handle); + fwrite(HTML.Buffer.Location + Entry->LinkOffsets.PrevStart + Entry->LinkOffsets.PrevEnd, Entry->LinkOffsets.NextStart, 1, HTML.Handle); + fwrite(HTML.Buffer.Location + Entry->LinkOffsets.PrevStart + Entry->LinkOffsets.PrevEnd + Entry->LinkOffsets.NextStart + Entry->LinkOffsets.NextEnd, + HTML.Buffer.Size - (Entry->LinkOffsets.PrevStart + Entry->LinkOffsets.PrevEnd + Entry->LinkOffsets.NextStart + Entry->LinkOffsets.NextEnd), + 1, + HTML.Handle); + fclose(HTML.Handle); + Entry->LinkOffsets.PrevEnd = 0; + Entry->LinkOffsets.NextEnd = 0; + if(N->Prev && N->PrevIndex >= 0) + { + N->PrevOffsetModifier = N->Prev->LinkOffsets.PrevEnd + + N->Prev->LinkOffsets.NextStart + + N->Prev->LinkOffsets.NextEnd + - N->PreLinkPrevOffsetTotal; + } + else + { + N->NextOffsetModifier = N->Next->LinkOffsets.PrevEnd + + N->Next->LinkOffsets.NextStart + + N->Next->LinkOffsets.NextEnd + - N->PreLinkNextOffsetTotal; + } + } + + FreeFile(&HTML); + return RC_SUCCESS; +} + +void +LinkToNewEntry(neighbourhood *N) +{ + MEM_TEST_INITIAL(); + N->PreLinkThisOffsetTotal = N->This->LinkOffsets.PrevEnd + + N->This->LinkOffsets.NextStart + + N->This->LinkOffsets.NextEnd; + + if(N->Prev) + { + N->PreLinkPrevOffsetTotal = N->Prev->LinkOffsets.PrevEnd + + N->Prev->LinkOffsets.NextStart + + N->Prev->LinkOffsets.NextEnd; + + MEM_TEST_MID("LinkToNewEntry1"); + InsertNeighbourLink(N->Project, N->Prev, N->This, LINK_FORWARDS, N->FormerIsFirst); + MEM_TEST_MID("LinkToNewEntry2"); + + N->PrevOffsetModifier = N->Prev->LinkOffsets.PrevEnd + + N->Prev->LinkOffsets.NextStart + + N->Prev->LinkOffsets.NextEnd + - N->PreLinkPrevOffsetTotal; + } + + if(N->Next) + { + N->PreLinkNextOffsetTotal = N->Next->LinkOffsets.PrevEnd + + N->Next->LinkOffsets.NextStart + + N->Next->LinkOffsets.NextEnd; + + MEM_TEST_MID("LinkToNewEntry3"); + InsertNeighbourLink(N->Project, N->Next, N->This, LINK_BACKWARDS, N->LatterIsFinal); + MEM_TEST_MID("LinkToNewEntry4"); + + N->NextOffsetModifier = N->Next->LinkOffsets.PrevEnd + + N->Next->LinkOffsets.NextStart + + N->Next->LinkOffsets.NextEnd + - N->PreLinkNextOffsetTotal; + } + MEM_TEST_AFTER("LinkToNewEntry()"); +} + +void +MarkNextAsFirst(neighbourhood *N) +{ + // TODO(matt): We added some untested logic in here. If things related to the prev / next links fail, we screwed up here + file HTML = {}; + ReadPlayerPageIntoBuffer(N->Project, &HTML, N->Next); + if(HTML.Buffer.Location) + { + buffer Link; + ClaimBuffer(&Link, BID_LINK, Kilobytes(4)); + + HTML.Handle = fopen(HTML.Path, "w"); + + fwrite(HTML.Buffer.Location, N->Next->LinkOffsets.PrevStart, 1, HTML.Handle); + + CopyStringToBuffer(&Link, + "
Welcome to %.*s
\n", (int)CurrentProject->Title.Length, CurrentProject->Title.Base); + + fwrite(Link.Location, (Link.Ptr - Link.Location), 1, HTML.Handle); + + fwrite(HTML.Buffer.Location + N->Next->LinkOffsets.PrevStart + N->Next->LinkOffsets.PrevEnd, + HTML.Buffer.Size - (N->Next->LinkOffsets.PrevStart + N->Next->LinkOffsets.PrevEnd), + 1, + HTML.Handle); + + N->Next->LinkOffsets.PrevEnd = Link.Ptr - Link.Location; + + DeclaimBuffer(&Link); + fclose(HTML.Handle); + } + else + { + link_insertion_offsets Blank = {}; + N->Next->LinkOffsets = Blank; + } + + FreeFile(&HTML); +} + +void +MarkPrevAsFinal(neighbourhood *N) +{ + file File = {}; + ReadPlayerPageIntoBuffer(N->Project, &File, N->Prev); + + if(File.Buffer.Location) + { + File.Handle = fopen(File.Path, "w"); + buffer Link; + ClaimBuffer(&Link, BID_LINK, Kilobytes(4)); + + + fwrite(File.Buffer.Location, N->Prev->LinkOffsets.PrevStart + N->Prev->LinkOffsets.PrevEnd + N->Prev->LinkOffsets.NextStart, 1, File.Handle); + + CopyStringToBuffer(&Link, + "
You have arrived at the (current) end of %.*s
\n", (int)CurrentProject->Title.Length, CurrentProject->Title.Base); + + fwrite(Link.Location, (Link.Ptr - Link.Location), 1, File.Handle); + + fwrite(File.Buffer.Location + N->Prev->LinkOffsets.PrevStart + N->Prev->LinkOffsets.PrevEnd + N->Prev->LinkOffsets.NextStart + N->Prev->LinkOffsets.NextEnd, + File.Buffer.Size - (N->Prev->LinkOffsets.PrevStart + N->Prev->LinkOffsets.PrevEnd + N->Prev->LinkOffsets.NextStart + N->Prev->LinkOffsets.NextEnd), + 1, + File.Handle); + + N->Prev->LinkOffsets.NextEnd = Link.Ptr - Link.Location; + + DeclaimBuffer(&Link); + fclose(File.Handle); + } + else + { + link_insertion_offsets Blank = {}; + N->Prev->LinkOffsets = Blank; + } + + FreeFile(&File); +} + +void +LinkOverDeletedEntry(neighbourhood *N) +{ + if(N->Project->EntryCount > 0) + { + if(N->Project->EntryCount == 1) + { + DeleteNeighbourLinks(N); + } + else + { + if(N->DeletedEntryWasFirst) + { + N->PreLinkNextOffsetTotal = N->Next->LinkOffsets.PrevEnd + + N->Next->LinkOffsets.NextStart + + N->Next->LinkOffsets.NextEnd; + + MarkNextAsFirst(N); + + N->NextOffsetModifier = N->Next->LinkOffsets.PrevEnd + + N->Next->LinkOffsets.NextStart + + N->Next->LinkOffsets.NextEnd + - N->PreLinkNextOffsetTotal; + return; + } + else if(N->DeletedEntryWasFinal) + { + N->PreLinkPrevOffsetTotal = N->Prev->LinkOffsets.PrevEnd + + N->Prev->LinkOffsets.NextStart + + N->Prev->LinkOffsets.NextEnd; + + MarkPrevAsFinal(N); + + N->PrevOffsetModifier = N->Prev->LinkOffsets.PrevEnd + + N->Prev->LinkOffsets.NextStart + + N->Prev->LinkOffsets.NextEnd + - N->PreLinkPrevOffsetTotal; + return; + } + else + { + // Assert(N->PrevIndex >= 0 && N->NextIndex >= 0) + N->PreLinkPrevOffsetTotal = N->Prev->LinkOffsets.PrevEnd + + N->Prev->LinkOffsets.NextStart + + N->Prev->LinkOffsets.NextEnd; + + + N->PreLinkNextOffsetTotal = N->Next->LinkOffsets.PrevEnd + + N->Next->LinkOffsets.NextStart + + N->Next->LinkOffsets.NextEnd; + } + + InsertNeighbourLink(N->Project, N->Prev, N->Next, LINK_FORWARDS, N->FormerIsFirst); + InsertNeighbourLink(N->Project, N->Next, N->Prev, LINK_BACKWARDS, N->LatterIsFinal); + + N->PrevOffsetModifier = N->Prev->LinkOffsets.PrevEnd + + N->Prev->LinkOffsets.NextStart + + N->Prev->LinkOffsets.NextEnd + - N->PreLinkPrevOffsetTotal; + + N->NextOffsetModifier = N->Next->LinkOffsets.PrevEnd + + N->Next->LinkOffsets.NextStart + + N->Next->LinkOffsets.NextEnd + - N->PreLinkNextOffsetTotal; + } + } +} + +void +LinkNeighbours(neighbourhood *N, enum8(link_types) LinkType) +{ + if(LinkType == LINK_INCLUDE) + { + LinkToNewEntry(N); + } + else + { + LinkOverDeletedEntry(N); + } +} + +rc +DeleteFromDB(neighbourhood *N, string BaseFilename) +{ + // TODO(matt): LogError() + db_entry *Entry = 0; + int EntryIndex = BinarySearchForMetadataEntry(N->Project, &Entry, BaseFilename); + + if(!DB.File.Buffer.Location) + { + InitIndexFile(CurrentProject); + } + + if(Entry) + { + int DeleteFileFrom = AccumulateDBEntryInsertionOffset(N->Project, EntryIndex); + int DeleteFileTo = DeleteFileFrom + Entry->Size; + N->This = Entry; + N->ThisIndex = EntryIndex; + GetNeighbourhood(N, EDIT_DELETION); + + int NewEntryCount = N->Project->EntryCount; + --NewEntryCount; + if(NewEntryCount == 0) + { + DeleteSearchPageFromFilesystem(N->Project); + DeleteLandmarksForSearch(CurrentProject->Index); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + } + N->Project->EntryCount = NewEntryCount; + + if(!(DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"))) { FreeBuffer(&DB.Metadata.File.Buffer); return RC_ERROR_FILE; } + + char *Ptr = (char *)N->This; + uint64_t BytesIntoFile = Ptr - DB.Metadata.File.Buffer.Location; + + fwrite(DB.Metadata.File.Buffer.Location, BytesIntoFile, 1, DB.Metadata.File.Handle); + SetFileEditPosition(&DB.Metadata); + + BytesIntoFile += sizeof(*N->This); + AccumulateFileEditSize(&DB.Metadata, -sizeof(*N->This)); + + fwrite(DB.Metadata.File.Buffer.Location + BytesIntoFile, DB.Metadata.File.Buffer.Size - BytesIntoFile, 1, DB.Metadata.File.Handle); + // TODO(matt): At this point, the N->This and possibly N->Next pointers are rendered invalid, so we must reacquire + // the Neighbourhood + // AFD + N->This = 0; + DB.Metadata.Signposts.This.Ptr = 0; + + CycleSignpostedFile(&DB.Metadata); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + + if(N->Project->EntryCount > 0) + { + if(!(DB.File.Handle = fopen(DB.File.Path, "w"))) { FreeBuffer(&DB.File.Buffer); return RC_ERROR_FILE; } + fwrite(DB.File.Buffer.Location, DeleteFileFrom, 1, DB.File.Handle); + fwrite(DB.File.Buffer.Location + DeleteFileTo, DB.File.Buffer.Size - DeleteFileTo, 1, DB.File.Handle); + CycleFile(&DB.File); + } + } + + return Entry ? RC_SUCCESS : RC_NOOP; +} + +typedef struct +{ + char *String; + bool SelfClosing; +} html_element; + +html_element HTMLElements[] = +{ + { "a", FALSE }, + { "div", FALSE }, + { "img", TRUE }, + { "input", TRUE }, + { "label", FALSE }, + { "li", FALSE }, + { "nav", FALSE }, + { "script", FALSE }, + { "span", FALSE }, + { "ul", FALSE }, +}; + +typedef enum +{ + NODE_A, + NODE_DIV, + NODE_IMG, + NODE_INPUT, + NODE_LABEL, + NODE_LI, + NODE_NAV, + NODE_SCRIPT, + NODE_SPAN, + NODE_UL, +} html_element_id; + +void +OpenNode(buffer *B, uint32_t *IndentationLevel, html_element_id Element, char *ID) +{ + AppendStringToBuffer(B, Wrap0("<")); + AppendStringToBuffer(B, Wrap0(HTMLElements[Element].String)); + if(ID) + { + AppendStringToBuffer(B, Wrap0(" ")); + AppendStringToBuffer(B, Wrap0("id=\"")); + AppendStringToBuffer(B, Wrap0(ID)); + AppendStringToBuffer(B, Wrap0("\"")); + } + if(!HTMLElements[Element].SelfClosing) + { + ++*IndentationLevel; + } +} + +void +OpenNodeC(buffer *B, uint32_t *IndentationLevel, html_element_id Element, char *ID) +{ + AppendStringToBuffer(B, Wrap0("<")); + AppendStringToBuffer(B, Wrap0(HTMLElements[Element].String)); + if(ID) + { + AppendStringToBuffer(B, Wrap0(" ")); + AppendStringToBuffer(B, Wrap0("id=\"")); + AppendStringToBuffer(B, Wrap0(ID)); + AppendStringToBuffer(B, Wrap0("\"")); + } + AppendStringToBuffer(B, Wrap0(">")); + if(!HTMLElements[Element].SelfClosing) + { + ++*IndentationLevel; + } +} + +void +OpenNodeNewLine(buffer *B, uint32_t *IndentationLevel, html_element_id Element, char *ID) +{ + AppendStringToBuffer(B, Wrap0("\n")); + IndentBuffer(B, *IndentationLevel); + OpenNode(B, IndentationLevel, Element, ID); +} + +void +OpenNodeCNewLine(buffer *B, uint32_t *IndentationLevel, html_element_id Element, char *ID) +{ + AppendStringToBuffer(B, Wrap0("\n")); + IndentBuffer(B, *IndentationLevel); + OpenNodeC(B, IndentationLevel, Element, ID); +} + +void +CloseNode(buffer *B, uint32_t *IndentationLevel, html_element_id Element) +{ + --*IndentationLevel; + AppendStringToBuffer(B, Wrap0("")); +} + +void +CloseNodeNewLine(buffer *B, uint32_t *IndentationLevel, html_element_id Element) +{ + AppendStringToBuffer(B, Wrap0("\n")); + --*IndentationLevel; + IndentBuffer(B, *IndentationLevel); + AppendStringToBuffer(B, Wrap0("")); +} + +string +StripFromStringStart(string From, string Strip) +{ + string Result = From; + Result.Base += Strip.Length; + Result.Length -= Strip.Length; + return Result; +} + +void +GenerateFilterOfProjectAndChildren(buffer *Filter, db_header_project *StoredP, project *P, bool *SearchRequired, bool TopLevel, bool *RequiresCineraJS) +{ + if(!TopLevel || StoredP->EntryCount > 0) + { + OpenNodeNewLine(Filter, &Filter->IndentLevel, NODE_DIV, 0); + AppendStringToBuffer(Filter, Wrap0(" class=\"cineraFilterProject\" data-baseURL=\"")); + AppendStringToBuffer(Filter, P->BaseURL); + AppendStringToBuffer(Filter, Wrap0("\">")); + + OpenNodeNewLine(Filter, &Filter->IndentLevel, NODE_SPAN, 0); + AppendStringToBuffer(Filter, Wrap0(" class=\"cineraText\">")); + + PushIcon(Filter, TRUE, P->IconType, P->Icon, P->IconAsset, P->IconVariants, PAGE_SEARCH, RequiresCineraJS); + + AppendStringToBuffer(Filter, P->HTMLTitle.Length > 0 ? P->HTMLTitle : P->Title); + CloseNode(Filter, &Filter->IndentLevel, NODE_SPAN); + } + + db_header_project *StoredChild = LocateFirstChildProject(StoredP); + for(int ChildIndex = 0; ChildIndex < StoredP->ChildCount; ++ChildIndex) + { + project *Child = P->Child + ChildIndex; + GenerateFilterOfProjectAndChildren(Filter, StoredChild, Child, SearchRequired, FALSE, RequiresCineraJS); + StoredChild = SkipProjectAndChildren(StoredChild); + } + + if(!TopLevel || StoredP->EntryCount > 0) + { + CloseNodeNewLine(Filter, &Filter->IndentLevel, NODE_DIV); + } +} + +void +GenerateIndexOfProjectAndChildren(buffer *Index, db_header_project *StoredP, project *P, bool *SearchRequired, bool TopLevel, bool *RequiresCineraJS) +{ + if(!TopLevel || StoredP->EntryCount > 0) + { + OpenNodeNewLine(Index, &Index->IndentLevel, NODE_DIV, 0); + AppendStringToBuffer(Index, Wrap0(" class=\"cineraIndexProject ")); + AppendStringToBuffer(Index, P->Theme); + AppendStringToBuffer(Index, Wrap0("\" data-project=\"")); + AppendStringToBuffer(Index, P->ID); + AppendStringToBuffer(Index, Wrap0("\" data-baseURL=\"")); + AppendStringToBuffer(Index, P->BaseURL); + AppendStringToBuffer(Index, Wrap0("\" data-playerLocation=\"")); + AppendStringToBuffer(Index, P->PlayerLocation); + AppendStringToBuffer(Index, Wrap0("\">")); + + OpenNodeNewLine(Index, &Index->IndentLevel, NODE_DIV, 0); + AppendStringToBuffer(Index, Wrap0(" class=\"cineraProjectTitle\">")); + + PushIcon(Index, TRUE, P->IconType, P->Icon, P->IconAsset, P->IconVariants, PAGE_SEARCH, RequiresCineraJS); + + AppendStringToBuffer(Index, P->HTMLTitle.Length > 0 ? P->HTMLTitle : P->Title); + CloseNode(Index, &Index->IndentLevel, NODE_DIV); + } + + if(StoredP->EntryCount > 0) + { + OpenNodeNewLine(Index, &Index->IndentLevel, NODE_DIV, 0); + AppendStringToBuffer(Index, Wrap0(" class=\"cineraIndexEntries\">")); + db_entry *Entry = LocateFirstEntry(StoredP); + buffer PlayerURL; + ClaimBuffer(&PlayerURL, BID_URL_PLAYER, MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_BASE_FILENAME_LENGTH); + for(int i = 0; i < StoredP->EntryCount; ++i, ++Entry) + { + if(Entry->Size > 0) + { + *SearchRequired = TRUE; + OpenNodeCNewLine(Index, &Index->IndentLevel, NODE_DIV, 0); + + OpenNode(Index, &Index->IndentLevel, NODE_A, 0); + AppendStringToBuffer(Index, Wrap0(" href=\"")); + ConstructPlayerURL(&PlayerURL, StoredP, Wrap0i(Entry->OutputLocation, sizeof(Entry->OutputLocation))); + AppendBuffer(Index, &PlayerURL); + AppendStringToBuffer(Index, Wrap0("\">")); + + if(*StoredP->Unit) + { + // TODO(matt): That rigorous notion of numbering, goddammit?! + char Number[16] = {}; + string NumberL = StripFromStringStart(Wrap0i(Entry->HMMLBaseFilename, sizeof(Entry->HMMLBaseFilename)), + P->ID); + + CopyStringNoFormat(Number, sizeof(Number), NumberL); + if(CurrentProject->NumberingScheme == NS_LINEAR) + { + for(int i = 0; Number[i]; ++i) + { + if(Number[i] == '_') + { + Number[i] = '.'; + } + } + } + + AppendStringToBuffer(Index, P->Unit); + AppendStringToBuffer(Index, Wrap0(" ")); + AppendStringToBuffer(Index, Wrap0i(Number, sizeof(Number))); + AppendStringToBuffer(Index, Wrap0(": ")); + } + + // HERE + AppendStringToBufferHTMLSafe(Index, Wrap0i(Entry->Title, sizeof(Entry->Title))); + CloseNode(Index, &Index->IndentLevel, NODE_A); + CloseNode(Index, &Index->IndentLevel, NODE_DIV); + } + } + + DeclaimBuffer(&PlayerURL); + + CloseNodeNewLine(Index, &Index->IndentLevel, NODE_DIV); + } + + db_header_project *StoredChild = LocateFirstChildProject(StoredP); + for(int ChildIndex = 0; ChildIndex < StoredP->ChildCount; ++ChildIndex) + { + project *Child = &P->Child[ChildIndex]; + GenerateIndexOfProjectAndChildren(Index, StoredChild, Child, SearchRequired, FALSE, RequiresCineraJS); + StoredChild = SkipProjectAndChildren(StoredChild); + } + + if(!TopLevel || StoredP->EntryCount > 0) + { + CloseNodeNewLine(Index, &Index->IndentLevel, NODE_DIV); + } +} + +void +CountOfProjectOrDescendantsWithPublicEntries(db_header_project *P, uint8_t *Count) +{ + db_entry *Entry = LocateFirstEntry(P); + for(int EntryIndex = 0; EntryIndex < P->EntryCount; ++EntryIndex) + { + if(Entry->Size > 0) + { + ++*Count; + break; + } + ++Entry; + } + + db_header_project *Child = LocateFirstChildProject(P); + for(int ChildIndex = 0; ChildIndex < P->ChildCount; ++ChildIndex) + { + CountOfProjectOrDescendantsWithPublicEntries(Child, Count); + Child = SkipProjectAndChildren(Child); + } +} + +bool +ProjectsHavePublicEntries() +{ + db_block_projects *ProjectsBlock = DB.Metadata.Signposts.ProjectsBlock.Ptr ? DB.Metadata.Signposts.ProjectsBlock.Ptr : LocateBlock(B_PROJ); + db_header_project *Child = LocateFirstChildProjectOfBlock(ProjectsBlock); + uint8_t Count = 0; + for(int i = 0; i < ProjectsBlock->Count; ++i) + { + CountOfProjectOrDescendantsWithPublicEntries(Child, &Count); + if(Count) + { + return TRUE; + } + Child = SkipProjectAndChildren(Child); + } + return Count; +} + +bool +AtLeastNProjectsHavePublicEntries(uint8_t N) +{ + uint8_t FoundProjectsWithPublicEntries = 0; + db_block_projects *ProjectsBlock = DB.Metadata.Signposts.ProjectsBlock.Ptr ? DB.Metadata.Signposts.ProjectsBlock.Ptr : LocateBlock(B_PROJ); + db_header_project *Child = LocateFirstChildProjectOfBlock(ProjectsBlock); + for(int i = 0; i < ProjectsBlock->Count; ++i) + { + CountOfProjectOrDescendantsWithPublicEntries(Child, &FoundProjectsWithPublicEntries); + if(FoundProjectsWithPublicEntries >= N) + { + return TRUE; + } + Child = SkipProjectAndChildren(Child); + } + return FALSE; +} + +typedef struct +{ + string *S; + uint64_t Count; +} strings; + +void +PushUniqueStringsRecursively(strings *UniqueThemes, db_header_project *P) +{ + bool FoundMatch = FALSE; + string Theme = Wrap0i(P->Theme, sizeof(P->Theme)); + for(int i = 0; i < UniqueThemes->Count; ++i) + { + if(StringsMatch(UniqueThemes->S[i], Theme)) + { + FoundMatch = TRUE; + } + } + + if(!FoundMatch && Theme.Length > 0) + { + UniqueThemes->S = Fit(UniqueThemes->S, sizeof(*UniqueThemes->S), UniqueThemes->Count, 4, FALSE); + UniqueThemes->S[UniqueThemes->Count] = Theme; + ++UniqueThemes->Count; + } + + db_header_project *Child = LocateFirstChildProject(P); + for(int i = 0; i < P->ChildCount; ++i) + { + PushUniqueStringsRecursively(UniqueThemes, Child); + Child = SkipProject(Child); + } +} + +void +PushUniqueString(strings *UniqueThemes, string GlobalTheme) +{ + bool FoundMatch = FALSE; + for(int i = 0; i < UniqueThemes->Count; ++i) + { + if(StringsMatch(UniqueThemes->S[i], GlobalTheme)) + { + FoundMatch = TRUE; + } + } + + if(!FoundMatch && GlobalTheme.Length > 0) + { + UniqueThemes->S = Fit(UniqueThemes->S, sizeof(*UniqueThemes->S), UniqueThemes->Count, 4, FALSE); + UniqueThemes->S[UniqueThemes->Count] = GlobalTheme; + ++UniqueThemes->Count; + } +} + +void +GenerateThemeLinks(buffer *IncludesSearch, db_header_project *P) +{ + strings UniqueThemes = {}; + if(P) + { + PushUniqueStringsRecursively(&UniqueThemes, P); + } + else + { + PushUniqueString(&UniqueThemes, Config->GlobalTheme); + db_block_projects *ProjectsBlock = DB.Metadata.Signposts.ProjectsBlock.Ptr ? DB.Metadata.Signposts.ProjectsBlock.Ptr : LocateBlock(B_PROJ); + P = LocateFirstChildProjectOfBlock(ProjectsBlock); + for(int i = 0; i < ProjectsBlock->Count; ++i) + { + PushUniqueStringsRecursively(&UniqueThemes, P); + P = SkipProjectAndChildren(P); + } + } + + for(int i = 0; i < UniqueThemes.Count; ++i) + { + string ThemeID = UniqueThemes.S[i]; + string ThemeFilename = MakeString("sls", "cinera__", &ThemeID, ".css"); + asset *CSSTheme = GetAsset(ThemeFilename, ASSET_CSS); + FreeString(&ThemeFilename); + buffer URL; + ConstructResolvedAssetURL(&URL, CSSTheme, PAGE_SEARCH); + CopyStringToBuffer(IncludesSearch, + "\n "); + } + + FreeAndResetCount(UniqueThemes.S, UniqueThemes.Count); +} + +int +SearchToBuffer(buffers *CollationBuffers, db_header_project *StoredP, project *P, string Theme, bool RequiresCineraJS) // NOTE(matt): This guy malloc's CollationBuffers->Search +{ + bool DoWork = FALSE; + if(DB.Metadata.File.Buffer.Location) + { + if(StoredP) + { + uint8_t FoundProjectsWithPublicEntries = 0; + CountOfProjectOrDescendantsWithPublicEntries(StoredP, &FoundProjectsWithPublicEntries); + if(FoundProjectsWithPublicEntries > 0) + { + DoWork = TRUE; + } + } + else + { + if(ProjectsHavePublicEntries()) + { + DoWork = TRUE; + } + } + } + + if(DoWork) + { + DB.Header = *(db_header *)DB.Metadata.File.Buffer.Location; + + RewindBuffer(&CollationBuffers->IncludesSearch); + + buffer URL; + + asset *CSSCinera = GetAsset(Wrap0(BuiltinAssets[ASSET_CSS_CINERA].Filename), ASSET_CSS); + ConstructResolvedAssetURL(&URL, CSSCinera, PAGE_SEARCH); + CopyStringToBuffer(&CollationBuffers->IncludesSearch, + "\n" + " \n" + "\n" + " IncludesSearch, CSSCinera, PAGE_SEARCH, 0); + CopyStringToBuffer(&CollationBuffers->IncludesSearch, + "\">"); + + // TODO(matt): Switch the CollationBuffers->IncludesSearch to be allocated rather than claimed + GenerateThemeLinks(&CollationBuffers->IncludesSearch, StoredP); + + asset *CSSTopics = GetAsset(Wrap0(BuiltinAssets[ASSET_CSS_TOPICS].Filename), ASSET_CSS); + ConstructResolvedAssetURL(&URL, CSSTopics, PAGE_SEARCH); + CopyStringToBuffer(&CollationBuffers->IncludesSearch, + "\n IncludesSearch, CSSTopics, PAGE_SEARCH, 0); + CopyStringToBuffer(&CollationBuffers->IncludesSearch, + "\">"); + + asset *JSSearch = GetAsset(Wrap0(BuiltinAssets[ASSET_JS_SEARCH].Filename), ASSET_JS); + ConstructResolvedAssetURL(&URL, JSSearch, PAGE_SEARCH); + + uint32_t IndentationLevel = 1; + ++IndentationLevel; + + buffer Script = {}; + Script.ID = BID_SCRIPT; + OpenNodeNewLine(&Script, &IndentationLevel, NODE_SCRIPT, 0); + AppendStringToBuffer(&Script, Wrap0(" type=\"text/javascript\" src=\"")); + AppendStringToBuffer(&Script, Wrap0i(URL.Location, URL.Ptr - URL.Location)); + DeclaimBuffer(&URL); + PushAssetLandmark(&Script, JSSearch, PAGE_SEARCH, TRUE); + AppendStringToBuffer(&Script, Wrap0("\">")); + CloseNode(&Script, &IndentationLevel, NODE_SCRIPT); + + bool SearchRequired = FALSE; + + //IndentBuffer(&CollationBuffers->Search, ++IndentationLevel); + + buffer *B = &CollationBuffers->Search; + OpenNode(B, &IndentationLevel, NODE_DIV, "cineraIndexControl"); + AppendStringToBuffer(B, Wrap0(" class=\"")); + AppendStringToBuffer(B, Theme); + AppendStringToBuffer(B, Wrap0("\">")); + + OpenNodeCNewLine(B, &IndentationLevel, NODE_DIV, "cineraIndexSort"); + + AppendStringToBuffer(B, Wrap0("Sort: Old to New ⏶")); + + CloseNode(B, &IndentationLevel, NODE_DIV); + + bool FilterRequired = FALSE; + if(StoredP) + { + if(StoredP->ChildCount > 0) + { + uint8_t FoundProjectsWithPublicEntries = 0; + CountOfProjectOrDescendantsWithPublicEntries(StoredP, &FoundProjectsWithPublicEntries); + if(FoundProjectsWithPublicEntries > 0) + { + FilterRequired = TRUE; + } + } + } + else + { + if(AtLeastNProjectsHavePublicEntries(2)) + { + FilterRequired = TRUE; + } + } + + buffer Filter = { .ID = BID_FILTER, .IndentLevel = IndentationLevel }; + buffer Index = { .ID = BID_INDEX, .IndentLevel = IndentationLevel }; + + if(FilterRequired) + { + Index.IndentLevel -= 2; + + + OpenNodeNewLine(B, &IndentationLevel, NODE_DIV, 0); + AppendStringToBuffer(B, Wrap0(" class=\"cineraIndexFilter\">")); + + OpenNodeCNewLine(B, &IndentationLevel, NODE_SPAN, 0); + + OpenNode(B, &IndentationLevel, NODE_IMG, 0); + AppendStringToBuffer(B, Wrap0(" src=\"")); + asset *FilterImage = GetAsset(Wrap0(BuiltinAssets[ASSET_IMG_FILTER].Filename), ASSET_IMG); + ConstructResolvedAssetURL(&URL, FilterImage, PAGE_SEARCH); + AppendStringToBuffer(B, Wrap0i(URL.Location, URL.Ptr - URL.Location)); + DeclaimBuffer(&URL); + PushAssetLandmark(B, FilterImage, PAGE_SEARCH, TRUE); + AppendStringToBuffer(B, Wrap0("\">")); + + CloseNode(B, &IndentationLevel, NODE_SPAN); + + OpenNodeNewLine(B, &IndentationLevel, NODE_DIV, 0); + AppendStringToBuffer(B, Wrap0(" class=\"filter_container\">")); + } + + // NOTE(matt): What I think the rules should be are: + // 1: if(Multiple projects have entries) { GenerateFilter at all } + // 2: if(ProjectsBlock->Count > 1) { Include the top-level "holding projects" in the filter } + + if(StoredP) + { + if(FilterRequired) + { + GenerateFilterOfProjectAndChildren(&Filter, StoredP, P, &SearchRequired, TRUE, &RequiresCineraJS); + } + GenerateIndexOfProjectAndChildren(&Index, StoredP, P, &SearchRequired, TRUE, &RequiresCineraJS); + } + else + { + db_block_projects *ProjectsBlock = DB.Metadata.Signposts.ProjectsBlock.Ptr ? DB.Metadata.Signposts.ProjectsBlock.Ptr : LocateBlock(B_PROJ); + StoredP = LocateFirstChildProjectOfBlock(ProjectsBlock); + for(int i = 0; i < ProjectsBlock->Count; ++i) + { + project *Child = &Config->Project[i]; + if(FilterRequired) + { + GenerateFilterOfProjectAndChildren(&Filter, StoredP, Child, &SearchRequired, FALSE, &RequiresCineraJS); + } + GenerateIndexOfProjectAndChildren(&Index, StoredP, Child, &SearchRequired, FALSE, &RequiresCineraJS); + StoredP = SkipProjectAndChildren(StoredP); + } + } + if(FilterRequired) + { + AppendLandmarkedBuffer(B, &Filter, PAGE_SEARCH); + FreeBuffer(&Filter); + CloseNodeNewLine(B, &IndentationLevel, NODE_DIV); + CloseNodeNewLine(B, &IndentationLevel, NODE_DIV); + } + + asset *JSCineraPre = GetAsset(Wrap0(BuiltinAssets[ASSET_JS_CINERA_PRE].Filename), ASSET_JS); + ConstructResolvedAssetURL(&URL, JSCineraPre, PAGE_SEARCH); + CopyStringToBuffer(&CollationBuffers->IncludesSearch, + "\n "); + + if(RequiresCineraJS) + { + asset *JSCineraPost = GetAsset(Wrap0(BuiltinAssets[ASSET_JS_CINERA_POST].Filename), ASSET_JS); + ConstructResolvedAssetURL(&URL, JSCineraPost, PAGE_SEARCH); + CopyStringToBuffer(&CollationBuffers->IncludesSearch, + "\n "); + } + + OpenNodeNewLine(B, &IndentationLevel, NODE_DIV, 0); + AppendStringToBuffer(B, Wrap0(" class=\"cineraQueryContainer\">")); + + OpenNodeNewLine(B, &IndentationLevel, NODE_LABEL, 0); + AppendStringToBuffer(B, Wrap0(" for=\"query\">")); + AppendStringToBuffer(B, Wrap0("Query:")); + + CloseNode(B, &IndentationLevel, NODE_LABEL); + + OpenNodeNewLine(B, &IndentationLevel, NODE_DIV, 0); + AppendStringToBuffer(B, Wrap0(" class=\"inputContainer\">")); + + OpenNodeNewLine(B, &IndentationLevel, NODE_INPUT, 0); + AppendStringToBuffer(B, Wrap0(" type=\"text\" autocomplete=\"off\" id=\"query\">")); + + OpenNodeNewLine(B, &IndentationLevel, NODE_DIV, 0); + AppendStringToBuffer(B, Wrap0(" class=\"spinner\">")); + + AppendStringToBuffer(B, Wrap0("\n")); + IndentBuffer(B, IndentationLevel); + AppendStringToBuffer(B, Wrap0("Downloading data...")); + + CloseNodeNewLine(B, &IndentationLevel, NODE_DIV); + CloseNodeNewLine(B, &IndentationLevel, NODE_DIV); + CloseNodeNewLine(B, &IndentationLevel, NODE_DIV); + CloseNodeNewLine(B, &IndentationLevel, NODE_DIV); + + OpenNodeCNewLine(B, &IndentationLevel, NODE_DIV, "cineraResultsSummary"); + + AppendStringToBuffer(B, Wrap0("Found: 0 episodes, 0 markers, 0h 0m 0s total.")); + + CloseNode(B, &IndentationLevel, NODE_DIV); + + OpenNodeNewLine(B, &IndentationLevel, NODE_DIV, "cineraResults"); + AppendStringToBuffer(B, Wrap0(" data-single=\"")); + AppendStringToBuffer(B, Wrap0(CurrentProject->SingleBrowserTab ? "1" : "0")); + AppendStringToBuffer(B, Wrap0("\">")); + + CloseNode(B, &IndentationLevel, NODE_DIV); + + AppendStringToBuffer(B, Wrap0("\n")); + + OpenNodeCNewLine(B, &IndentationLevel, NODE_DIV, "cineraIndex"); + + + AppendLandmarkedBuffer(B, &Index, PAGE_SEARCH); + FreeBuffer(&Index); + + + CloseNodeNewLine(B, &IndentationLevel, NODE_DIV); + + AppendLandmarkedBuffer(B, &Script, PAGE_SEARCH); + FreeBuffer(&Script); + + if(!SearchRequired) { return RC_NOOP; } + else { return RC_SUCCESS; } + } + return RC_NOOP; +} + +int +GeneratePlayerPage(neighbourhood *N, buffers *CollationBuffers, template *PlayerTemplate, string OutputLocation, bool Reinserting) +{ + MEM_TEST_INITIAL(); + string PlayerLocationL = Wrap0i(N->Project->PlayerLocation, sizeof(N->Project->PlayerLocation)); + MEM_TEST_MID("GeneratePlayerPage1"); + char *PlayerPath = ConstructDirectoryPath(N->Project, &PlayerLocationL, &OutputLocation); + MEM_TEST_MID("GeneratePlayerPage2"); + + DIR *OutputDirectoryHandle; + if(!(OutputDirectoryHandle = opendir(PlayerPath))) // TODO(matt): open() + { + if(!MakeDir(Wrap0(PlayerPath))) + { + LogError(LOG_ERROR, "Unable to create directory %s: %s", PlayerPath, strerror(errno)); + fprintf(stderr, "Unable to create directory %s: %s\n", PlayerPath, strerror(errno)); + return RC_ERROR_DIRECTORY; + }; + } + closedir(OutputDirectoryHandle); + + ExtendString0(&PlayerPath, Wrap0("/index.html")); + MEM_TEST_MID("GeneratePlayerPage3"); + + bool SearchInTemplate = FALSE; + for(int TagIndex = 0; TagIndex < PlayerTemplate->Metadata.TagCount; ++TagIndex) + { + if(PlayerTemplate->Metadata.Tags[TagIndex].TagCode == TAG_SEARCH) + { + SearchInTemplate = TRUE; + // TODO(matt): Fully determine whether UpdateNeighbourhoodPointers() must happen here + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + SearchToBuffer(CollationBuffers, N->Project, CurrentProject, Wrap0i(N->Project->Theme, sizeof(N->Project->Theme)), PlayerTemplate->Metadata.RequiresCineraJS); + break; + } + } + MEM_TEST_MID("GeneratePlayerPage4"); + + BuffersToHTML(CollationBuffers, PlayerTemplate, PlayerPath, PAGE_PLAYER, &N->This->LinkOffsets.PrevStart); + MEM_TEST_MID("GeneratePlayerPage5"); + Free(PlayerPath); + // NOTE(matt): A previous InsertNeighbourLink() call will have done SnipeEntryIntoMetadataBuffer(), but we must do it here now + // that PrevStart has been adjusted by BuffersToHTML() + UpdateLandmarksForNeighbourhood(N, Reinserting ? EDIT_REINSERTION : EDIT_ADDITION); + MEM_TEST_MID("GeneratePlayerPage6"); + + MEM_TEST_MID("GeneratePlayerPage7"); + if(SearchInTemplate) + { + FreeBuffer(&CollationBuffers->Search); + } + MEM_TEST_MID("GeneratePlayerPage8"); + ResetAssetLandmarks(); + MEM_TEST_AFTER("GeneratePlayerPage()"); + return RC_SUCCESS; +} + +rc +GenerateSearchPage(neighbourhood *N, buffers *CollationBuffers, db_header_project *StoredP, project *P) +{ + string SearchLocationL = Wrap0i(StoredP->SearchLocation, sizeof(StoredP->SearchLocation)); + char *SearchPath = ConstructDirectoryPath(StoredP, &SearchLocationL, 0); + + DIR *OutputDirectoryHandle; + if(!(OutputDirectoryHandle = opendir(SearchPath))) // TODO(matt): open() + { + if(!MakeDir(Wrap0(SearchPath))) + { + LogError(LOG_ERROR, "Unable to create directory %s: %s", SearchPath, strerror(errno)); + fprintf(stderr, "Unable to create directory %s: %s\n", SearchPath, strerror(errno)); + return RC_ERROR_DIRECTORY; + }; + } + closedir(OutputDirectoryHandle); + + ExtendString0(&SearchPath, Wrap0("/index.html")); + // TODO(matt): We used to do UpdateNeighbourhoodPointers() here. If something goes awry near here, reinstate that call + switch(SearchToBuffer(CollationBuffers, StoredP, P, + Wrap0i(StoredP->Theme, sizeof(StoredP->Theme)), P->SearchTemplate.Metadata.RequiresCineraJS)) + { + case RC_SUCCESS: + { + BuffersToHTML(CollationBuffers, &P->SearchTemplate, SearchPath, PAGE_SEARCH, 0); + UpdateLandmarksForSearch(N, P->Index); + break; + } + case RC_NOOP: + { + DeleteSearchPageFromFilesystem(StoredP); + DeleteLandmarksForSearch(P->Index); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + break; + } + } + Free(SearchPath); + FreeBuffer(&CollationBuffers->Search); + ResetAssetLandmarks(); + return RC_SUCCESS; +} + +rc +GenerateGlobalSearchPage(neighbourhood *N, buffers *CollationBuffers) +{ + db_block_projects *ProjectsBlock = DB.Metadata.Signposts.ProjectsBlock.Ptr; + string SearchLocationL = Wrap0i(ProjectsBlock->GlobalSearchDir, sizeof(ProjectsBlock->GlobalSearchDir)); + char *SearchPath = MakeString0("l", &SearchLocationL); + + DIR *OutputDirectoryHandle; + if(!(OutputDirectoryHandle = opendir(SearchPath))) // TODO(matt): open() + { + if(!MakeDir(Wrap0(SearchPath))) + { + LogError(LOG_ERROR, "Unable to create directory %s: %s", SearchPath, strerror(errno)); + fprintf(stderr, "Unable to create directory %s: %s\n", SearchPath, strerror(errno)); + return RC_ERROR_DIRECTORY; + }; + } + closedir(OutputDirectoryHandle); + + ExtendString0(&SearchPath, Wrap0("/index.html")); + switch(SearchToBuffer(CollationBuffers, 0, 0, Config->GlobalTheme, Config->SearchTemplate.Metadata.RequiresCineraJS)) + { + case RC_SUCCESS: + { + BuffersToHTML(CollationBuffers, &Config->SearchTemplate, SearchPath, PAGE_SEARCH, 0); + UpdateLandmarksForSearch(N, GLOBAL_SEARCH_PAGE_INDEX); + break; + } + case RC_NOOP: + { + char *GlobalSearchPageLocation = MakeString0("l", &Config->GlobalSearchDir); + DeleteGlobalSearchPageFromFilesystem(GlobalSearchPageLocation); + Free(GlobalSearchPageLocation); + DeleteLandmarksForSearch(GLOBAL_SEARCH_PAGE_INDEX); + DeleteStaleAssets(); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + break; + } + } + Free(SearchPath); + FreeBuffer(&CollationBuffers->Search); + ResetAssetLandmarks(); + return RC_SUCCESS; +} + +int +GenerateSearchPages(neighbourhood *N, buffers *CollationBuffers) +{ + // TODO(matt): Ascend through all the ancestry, generating search pages + PrintFunctionName("GenerateSearchPage()"); + project *This = CurrentProject; + //PrintLineage(This->Lineage, TRUE); + ResetNeighbourhood(N); + N->Project = LocateProject(This->Index); + GenerateSearchPage(N, CollationBuffers, N->Project, This); + while(This->Parent) + { + //PrintLineage(This->Parent->Lineage, TRUE); + This = This->Parent; + ResetNeighbourhood(N); + N->Project = LocateProject(This->Index); + GenerateSearchPage(N, CollationBuffers, N->Project, This); + } + + if(Config->GlobalSearchDir.Length > 0 && Config->GlobalSearchURL.Length > 0) + { + // TODO(matt): Fully determine whether UpdateNeighbourhoodPointers() must happen here + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + GenerateGlobalSearchPage(N, CollationBuffers); + } + + //sleep(1); + return RC_SUCCESS; +} + +int +DeleteEntry(neighbourhood *N, string BaseFilename) +{ + // TODO(matt): DeleteFromDB() may reasonably use this BaseFilename to locate the Entry. But, we'll need to return the + // Entry->Output to pass to DeletePlayerPageFromFilesystem() + // db_entry5 + + // TODO(matt): Fix deletion of the final entry in a project + if(DeleteFromDB(N, BaseFilename) == RC_SUCCESS) + { + LinkNeighbours(N, LINK_EXCLUDE); + + DeletePlayerPageFromFilesystem(N->Project, BaseFilename, FALSE, TRUE); + UpdateLandmarksForNeighbourhood(N, EDIT_DELETION); + return RC_SUCCESS; + } + return RC_NOOP; +} + +int +InsertEntry(neighbourhood *N, buffers *CollationBuffers, template *BespokeTemplate, string BaseFilename, bool RecheckingPrivacy) +{ + MEM_TEST_INITIAL(); + bool Reinserting = FALSE; + MEM_TEST_MID("InsertEntry0"); + db_entry *Entry = InsertIntoDB(N, CollationBuffers, BespokeTemplate, BaseFilename, RecheckingPrivacy, &Reinserting); + MEM_TEST_MID("InsertEntry1"); + if(Entry) + { + MEM_TEST_MID("InsertEntry2"); + LinkNeighbours(N, LINK_INCLUDE); + MEM_TEST_MID("InsertEntry3"); + if(BespokeTemplate->File.Buffer.Location) + { + MEM_TEST_MID("InsertEntry4"); + GeneratePlayerPage(N, CollationBuffers, BespokeTemplate, Wrap0i(Entry->OutputLocation, sizeof(Entry->OutputLocation)), Reinserting); + MEM_TEST_MID("InsertEntry5"); + FreeTemplate(BespokeTemplate); + MEM_TEST_MID("InsertEntry6"); + } + else + { + MEM_TEST_MID("InsertEntry7"); + GeneratePlayerPage(N, CollationBuffers, &CurrentProject->PlayerTemplate, Wrap0i(Entry->OutputLocation, sizeof(Entry->OutputLocation)), Reinserting); + MEM_TEST_MID("InsertEntry8"); + } + MEM_TEST_AFTER("InsertEntry()"); + return RC_SUCCESS; + } + MEM_TEST_AFTER("InsertEntry()"); + return RC_NOOP; +} + +void +SetCurrentProject(project *P, neighbourhood *N) +{ + if(CurrentProject != P) + { + FreeFile(&DB.File); + + CurrentProject = P; + + InitNeighbourhood(N); + } +} + +void +RecheckPrivacyRecursively(project *P, neighbourhood *N, buffers *CollationBuffers, template *BespokeTemplate) +{ + SetCurrentProject(P, N); + if(DB.Metadata.File.Buffer.Size > 0) + { + ResetNeighbourhood(N); + char *Ptr = (char *)N->Project; + Ptr += sizeof(*N->Project); + db_entry *FirstEntry = (db_entry *)Ptr; + int PrivateEntryIndex = 0; + db_entry PrivateEntries[N->Project->EntryCount]; + bool Inserted = FALSE; + for(int IndexEntry = 0; IndexEntry < N->Project->EntryCount; ++IndexEntry) + { + db_entry *Entry = FirstEntry + IndexEntry; + if(Entry->Size == 0) + { + PrivateEntries[PrivateEntryIndex] = *Entry; + ++PrivateEntryIndex; + } + } + + for(int i = 0; i < PrivateEntryIndex; ++i) + { + Inserted = (InsertEntry(N, CollationBuffers, BespokeTemplate, Wrap0i(PrivateEntries[i].HMMLBaseFilename, sizeof(PrivateEntries[i].HMMLBaseFilename)), TRUE) == RC_SUCCESS); + } + + if(Inserted) + { + GenerateSearchPages(N, CollationBuffers); + DeleteStaleAssets(); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + } + + } + + for(int i = 0; i < P->ChildCount; ++i) + { + RecheckPrivacyRecursively(&P->Child[i], N, CollationBuffers, BespokeTemplate); + } +} + +void +RecheckPrivacy(neighbourhood *N, buffers *CollationBuffers, template *BespokeTemplate) +{ + for(int i = 0; i < Config->ProjectCount; ++i) + { + RecheckPrivacyRecursively(&Config->Project[i], N, CollationBuffers, BespokeTemplate); + } + LastPrivacyCheck = time(0); +} void UpdateDeferredAssetChecksums(void) { for(int i = 0; i < Assets.Count; ++i) { - if(Assets.Asset[i].DeferredUpdate) + asset *This = GetPlaceInBook(&Assets.Asset, i); + if(This->DeferredUpdate) { - UpdateAssetInDB(i); + UpdateAssetInDB(This); } } } +rc +RemoveDirectory(string D) +{ + // TODO(matt): Change this to operate on a string + // AFD + int Result; + char *Path = 0; + ExtendString0(&Path, D); + if((remove(Path) == -1)) + { + LogError(LOG_NOTICE, "Unable to remove directory %s: %s", Path, strerror(errno)); + fprintf(stderr, "%sUnable to remove directory%s %s: %s\n", ColourStrings[CS_WARNING], ColourStrings[CS_END], Path, strerror(errno)); + Result = RC_ERROR_DIRECTORY; + } + else + { + LogError(LOG_INFORMATIONAL, "Deleted %s", Path); + fprintf(stderr, "%sDeleted%s %s\n", ColourStrings[CS_DELETION], ColourStrings[CS_END], Path); + Result = RC_SUCCESS; + } + Free(Path); + return Result; +} + +void +RemoveChildDirectories(string FullPath, string ParentDirectory) +{ + RemoveDirectory(FullPath); + while(FullPath.Length > ParentDirectory.Length) + { + if(FullPath.Base[FullPath.Length] == '/') + { + RemoveDirectory(FullPath); + } + --FullPath.Length; + } +} + +void +RemoveDirectoryAndChildren(string FullPath, string ParentDirectory) +{ + RemoveChildDirectories(FullPath, ParentDirectory); + RemoveDirectory(ParentDirectory); +} + +int +UpgradeDB(int OriginalDBVersion) +{ + // NOTE(matt): For each new DB version, we must declare and initialise one instance of each preceding version, only cast the + // incoming DB to the type of the OriginalDBVersion, and move into the final case all operations on that incoming DB + + database4 DB4 = { }; + switch(OriginalDBVersion) + { + case 4: + { + DB4.Header = *(db_header4 *)DB.Metadata.File.Buffer.Location; + + DB.Header.HexSignature = DB4.Header.HexSignature; + + DB.Header.CurrentDBVersion = CINERA_DB_VERSION; + DB.Header.CurrentAppVersion = CINERA_APP_VERSION; + DB.Header.CurrentHMMLVersion.Major = hmml_version.Major; + DB.Header.CurrentHMMLVersion.Minor = hmml_version.Minor; + DB.Header.CurrentHMMLVersion.Patch = hmml_version.Patch; + + DB.Header.InitialDBVersion = DB4.Header.InitialDBVersion; + DB.Header.InitialAppVersion = DB4.Header.InitialAppVersion; + DB.Header.InitialHMMLVersion = DB4.Header.InitialHMMLVersion; + + DB.Header.BlockCount = 0; + + DB.ProjectsBlock.BlockID = FOURCC("PROJ"); + DB.ProjectsBlock.Count = 0; + ++DB.Header.BlockCount; + + DB.AssetsBlock.BlockID = FOURCC("ASET"); + DB.AssetsBlock.Count = 0; + ClearCopyStringNoFormat(DB.AssetsBlock.RootDir, sizeof(DB.AssetsBlock.RootDir), Config->AssetsRootDir); + ClearCopyStringNoFormat(DB.AssetsBlock.RootURL, sizeof(DB.AssetsBlock.RootURL), Config->AssetsRootURL); + ClearCopyStringNoFormat(DB.AssetsBlock.CSSDir, sizeof(DB.AssetsBlock.CSSDir), Config->CSSDir); + ClearCopyStringNoFormat(DB.AssetsBlock.ImagesDir, sizeof(DB.AssetsBlock.ImagesDir), Config->ImagesDir); + ClearCopyStringNoFormat(DB.AssetsBlock.JSDir, sizeof(DB.AssetsBlock.JSDir), Config->JSDir); + ++DB.Header.BlockCount; + + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + fwrite(&DB.Header, sizeof(DB.Header), 1, DB.Metadata.File.Handle); + + DB.Metadata.Signposts.ProjectsBlock.Byte = ftell(DB.Metadata.File.Handle); + fwrite(&DB.ProjectsBlock, sizeof(DB.ProjectsBlock), 1, DB.Metadata.File.Handle); + + DB.Metadata.Signposts.AssetsBlock.Byte = ftell(DB.Metadata.File.Handle); + fwrite(&DB.AssetsBlock, sizeof(DB.AssetsBlock), 1, DB.Metadata.File.Handle); + CycleSignpostedFile(&DB.Metadata); + + DB.Metadata.Signposts.ProjectsBlock.Ptr = DB.Metadata.File.Buffer.Location + DB.Metadata.Signposts.ProjectsBlock.Byte; + DB.Metadata.Signposts.AssetsBlock.Ptr = DB.Metadata.File.Buffer.Location + DB.Metadata.Signposts.AssetsBlock.Byte; + } // Falls through + // TODO(matt); DBVersion 4 is the first that uses HexSignatures + // We should try and deprecate earlier versions and just enforce a clean rebuild for invalid DB files + // Perhaps do this either the next time we have to bump the version, or when we scrap Single Edition + } + + fprintf(stderr, "\n%sUpgraded Cinera DB from %d to %d!%s\n\n", ColourStrings[CS_SUCCESS], OriginalDBVersion, DB.Header.CurrentDBVersion, ColourStrings[CS_END]); + return RC_SUCCESS; +} + +typedef struct +{ + bool Present; + char ID[MAX_BASE_FILENAME_LENGTH]; +} entry_presence_id; // Metadata, unless we actually want to bolster this? + +rc +DeleteDeadDBEntries(neighbourhood *N, bool *Modified) +{ + // TODO(matt): Additionally remove the BaseDir if it changed + bool NewPlayerLocation = FALSE; + bool NewSearchLocation = FALSE; + + if(StringsDiffer(CurrentProject->BaseDir, Wrap0i(N->Project->BaseDir, sizeof(N->Project->BaseDir))) || + StringsDiffer(CurrentProject->PlayerLocation, Wrap0i(N->Project->PlayerLocation, sizeof(N->Project->PlayerLocation)))) + { + string PlayerLocationL = Wrap0i(N->Project->PlayerLocation, sizeof(N->Project->PlayerLocation)); + char *OldPlayerDirectory = ConstructDirectoryPath(N->Project, &PlayerLocationL, 0); + + db_header_project NewProjectHeader = *N->Project; + ClearCopyStringNoFormat(NewProjectHeader.BaseDir, sizeof(NewProjectHeader.BaseDir), CurrentProject->BaseDir); + char *NewPlayerDirectory = ConstructDirectoryPath(&NewProjectHeader, &CurrentProject->PlayerLocation, 0); + + printf("%sRelocating Player Page%s from %s to %s%s\n", + ColourStrings[CS_REINSERTION], N->Project->EntryCount > 1 ? "s" : "", + OldPlayerDirectory, NewPlayerDirectory, ColourStrings[CS_END]); + Free(NewPlayerDirectory); + + db_entry *FirstEntry = LocateFirstEntry(N->Project); + for(int EntryIndex = 0; EntryIndex < N->Project->EntryCount; ++EntryIndex) + { + db_entry *This = FirstEntry + EntryIndex; + DeletePlayerPageFromFilesystem(N->Project, Wrap0i(This->OutputLocation, sizeof(This->OutputLocation)), TRUE, TRUE); + } + + if(PlayerLocationL.Length > 0) + { + RemoveChildDirectories(Wrap0(OldPlayerDirectory), Wrap0i(N->Project->BaseDir, sizeof(N->Project->BaseDir))); + } + + Free(OldPlayerDirectory); + ClearCopyStringNoFormat(N->Project->PlayerLocation, sizeof(N->Project->PlayerLocation), CurrentProject->PlayerLocation); + NewPlayerLocation = TRUE; + } + + if(StringsDiffer(CurrentProject->BaseDir, Wrap0i(N->Project->BaseDir, sizeof(N->Project->BaseDir))) || + StringsDiffer(CurrentProject->SearchLocation, Wrap0i(N->Project->SearchLocation, sizeof(N->Project->SearchLocation)))) + { + string SearchLocationL = Wrap0i(N->Project->SearchLocation, sizeof(N->Project->SearchLocation)); + char *OldSearchDirectory = ConstructDirectoryPath(N->Project, &SearchLocationL, 0); + + db_header_project NewProjectHeader = *N->Project; + ClearCopyStringNoFormat(NewProjectHeader.BaseDir, sizeof(NewProjectHeader.BaseDir), CurrentProject->BaseDir); + ClearCopyStringNoFormat(NewProjectHeader.SearchLocation, sizeof(NewProjectHeader.SearchLocation), CurrentProject->SearchLocation); + char *NewSearchDirectory = ConstructDirectoryPath(&NewProjectHeader, &CurrentProject->SearchLocation, 0); + MakeDir(Wrap0(NewSearchDirectory)); + + printf("%sRelocating Search Page from %s to %s%s\n", + ColourStrings[CS_REINSERTION], OldSearchDirectory, NewSearchDirectory, ColourStrings[CS_END]); + + char *OldSearchPagePath = MakeString0("ss", OldSearchDirectory, "/index.html"); + char *NewSearchPagePath = MakeString0("ss", NewSearchDirectory, "/index.html"); + rename(OldSearchPagePath, NewSearchPagePath); + Free(OldSearchPagePath); + Free(NewSearchPagePath); + + char *OldSearchIndexPath = ConstructIndexFilePath(N->Project); + char *NewSearchIndexPath = ConstructIndexFilePath(&NewProjectHeader); + rename(OldSearchIndexPath, NewSearchIndexPath); + Free(OldSearchIndexPath); + Free(NewSearchIndexPath); + + Free(NewSearchDirectory); + + FreeFile(&DB.File); + DB.File = InitFile(&CurrentProject->BaseDir, &CurrentProject->ID, EXT_INDEX); + ReadFileIntoBuffer(&DB.File); // NOTE(matt): Could we actually catch errors (permissions?) here and bail? + + if(SearchLocationL.Length > 0) + { + RemoveChildDirectories(Wrap0(OldSearchDirectory), Wrap0i(N->Project->BaseDir, sizeof(N->Project->BaseDir))); + } + + remove(OldSearchDirectory); + Free(OldSearchDirectory); + ClearCopyStringNoFormat(N->Project->SearchLocation, sizeof(N->Project->SearchLocation), CurrentProject->SearchLocation); + NewSearchLocation = TRUE; + } + + if(StringsDiffer(CurrentProject->BaseDir, Wrap0i(N->Project->BaseDir, sizeof(N->Project->BaseDir)))) + { + ClearCopyStringNoFormat(N->Project->BaseDir, sizeof(N->Project->BaseDir), CurrentProject->BaseDir); + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + WriteFromByteToEnd(&DB.Metadata.File, 0); + CycleSignpostedFile(&DB.Metadata); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + *Modified = TRUE; + } + + if(StringsDiffer(CurrentProject->BaseURL, Wrap0i(N->Project->BaseURL, sizeof(N->Project->BaseURL)))) + { + ClearCopyStringNoFormat(N->Project->BaseURL, sizeof(N->Project->BaseURL), CurrentProject->BaseURL); + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + WriteFromByteToEnd(&DB.Metadata.File, 0); + CycleSignpostedFile(&DB.Metadata); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + *Modified = TRUE; + } + + if(NewPlayerLocation || NewSearchLocation) + { + if(!(DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"))) { FreeBuffer(&DB.Metadata.File.Buffer); return RC_ERROR_FILE; } + ClearCopyStringNoFormat(N->Project->BaseDir, sizeof(N->Project->BaseDir), CurrentProject->BaseDir); + ClearCopyStringNoFormat(N->Project->BaseURL, sizeof(N->Project->BaseURL), CurrentProject->BaseURL); + fwrite(DB.Metadata.File.Buffer.Location, DB.Metadata.File.Buffer.Size, 1, DB.Metadata.File.Handle); + CycleSignpostedFile(&DB.Metadata); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + } + + entry_presence_id Entries[N->Project->EntryCount]; + + db_entry *FirstEntry = LocateFirstEntry(N->Project); + for(int EntryIndex = 0; EntryIndex < N->Project->EntryCount; ++EntryIndex) + { + db_entry *This = FirstEntry + EntryIndex; + CopyStringNoFormat(Entries[EntryIndex].ID, sizeof(Entries[EntryIndex].ID), Wrap0i(This->HMMLBaseFilename, sizeof(This->HMMLBaseFilename))); + Entries[EntryIndex].Present = FALSE; + } + + char *HMMLDir0 = MakeString0("l", &CurrentProject->HMMLDir); + DIR *HMMLDirHandle = opendir(HMMLDir0); + Free(HMMLDir0); + if(!HMMLDirHandle) + { + LogError(LOG_ERROR, "Unable to scan project directory %.*s: %s", (int)CurrentProject->HMMLDir.Length, CurrentProject->HMMLDir.Base, strerror(errno)); + fprintf(stderr, "Unable to scan project directory %.*s: %s\n", (int)CurrentProject->HMMLDir.Length, CurrentProject->HMMLDir.Base, strerror(errno)); + return RC_ERROR_DIRECTORY; + } + + struct dirent *ProjectFiles; + + while((ProjectFiles = readdir(HMMLDirHandle))) + { + string Filename = Wrap0(ProjectFiles->d_name); + if(ExtensionMatches(Filename, EXT_HMML)) + { + for(int i = 0; i < N->Project->EntryCount; ++i) + { + if(StringsMatch(Wrap0(Entries[i].ID), GetBaseFilename(Filename, EXT_HMML))) + { + Entries[i].Present = TRUE; + break; + } + } + } + } + closedir(HMMLDirHandle); + + bool Deleted = FALSE; + for(int i = 0; i < N->Project->EntryCount; ++i) + { + if(Entries[i].Present == FALSE) + { + Deleted = TRUE; + ResetNeighbourhood(N); + DeleteEntry(N, Wrap0(Entries[i].ID)); + } + } + + return Deleted ? RC_SUCCESS : RC_NOOP; +} + +int +SyncDBWithInput(neighbourhood *N, buffers *CollationBuffers, template *BespokeTemplate) +{ + MEM_TEST_INITIAL(); + if(DB.Metadata.File.Buffer.Size > 0 && Config->QueryString.Length == 0) + { + DeleteAllLandmarksAndAssets(); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + } + + bool Modified = FALSE; + if(StringsDiffer(CurrentProject->Title, Wrap0i(N->Project->Title, sizeof(N->Project->Title)))) + { + ClearCopyStringNoFormat(N->Project->Title, sizeof(N->Project->Title), CurrentProject->Title); + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + WriteFromByteToEnd(&DB.Metadata.File, 0); + CycleSignpostedFile(&DB.Metadata); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + Modified = TRUE; + } + + if(StringsDiffer(CurrentProject->Theme, Wrap0i(N->Project->Theme, sizeof(N->Project->Theme)))) + { + ClearCopyStringNoFormat(N->Project->Theme, sizeof(N->Project->Theme), CurrentProject->Theme); + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + WriteFromByteToEnd(&DB.Metadata.File, 0); + CycleSignpostedFile(&DB.Metadata); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + Modified = TRUE; + } + + if(StringsDiffer(CurrentProject->Unit, Wrap0i(N->Project->Unit, sizeof(N->Project->Unit)))) + { + ClearCopyStringNoFormat(N->Project->Unit, sizeof(N->Project->Unit), CurrentProject->Unit); + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + WriteFromByteToEnd(&DB.Metadata.File, 0); + CycleSignpostedFile(&DB.Metadata); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + Modified = TRUE; + } + + bool Deleted = FALSE; + Deleted = (DB.Metadata.File.Buffer.Size > 0 && DeleteDeadDBEntries(N, &Modified) == RC_SUCCESS); + + // NOTE(matt): Stack-string + char HMMLDir0[CurrentProject->HMMLDir.Length + 1]; + CopyStringNoFormat(HMMLDir0, sizeof(HMMLDir0), CurrentProject->HMMLDir); + DIR *HMMLDirHandle = opendir(HMMLDir0); + if(!HMMLDirHandle) + { + LogError(LOG_ERROR, "Unable to scan project directory %.*s: %s", (int)CurrentProject->HMMLDir.Length, CurrentProject->HMMLDir.Base, strerror(errno)); + fprintf(stderr, "Unable to scan project directory %.*s: %s\n", (int)CurrentProject->HMMLDir.Length, CurrentProject->HMMLDir.Base, strerror(errno)); + return RC_ERROR_DIRECTORY; + } + + MEM_TEST_MID("SyncDBWithInput1"); + struct dirent *ProjectFiles; + bool Inserted = FALSE; + while((ProjectFiles = readdir(HMMLDirHandle))) + { + string Filename = Wrap0(ProjectFiles->d_name); + if(ExtensionMatches(Filename, EXT_HMML)) + { + ResetNeighbourhood(N); + MEM_TEST_MID("SyncDBWithInput1a"); + + Inserted |= (InsertEntry(N, CollationBuffers, BespokeTemplate, GetBaseFilename(Filename, EXT_HMML), 0) == RC_SUCCESS); + MEM_TEST_MID("SyncDBWithInput1b"); + VerifyLandmarks(N); + } + } + closedir(HMMLDirHandle); + MEM_TEST_MID("SyncDBWithInput2"); + + + UpdateDeferredAssetChecksums(); + + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + + if(Deleted || Inserted || Modified) + { + // TODO(matt): The DB may not be set up correctly at this point. Figure out why! + // + // To reproduce: + // 1. Generate it with a blank global_search_dir and global_search_url + // 2. Generate it with a filled global_search_dir and global_search_url + // + // Of note: We seem to be producing the asset landmark for Generation -1 just fine. The problem is that + // we're not getting the index.html file + GenerateSearchPages(N, CollationBuffers); + DeleteStaleAssets(); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + VerifyLandmarks(N); + } + + MEM_TEST_AFTER("SyncDBWithInput()"); + return RC_SUCCESS; +} + +void +PrintVersions() +{ + curl_version_info_data *CurlVersion = curl_version_info(CURLVERSION_NOW); + printf("Cinera: %d.%d.%d\n" + "Cinera DB: %d\n" + "hmmlib: %d.%d.%d\n" + "libcurl: %s\n", + CINERA_APP_VERSION.Major, CINERA_APP_VERSION.Minor, CINERA_APP_VERSION.Patch, + CINERA_DB_VERSION, + hmml_version.Major, hmml_version.Minor, hmml_version.Patch, + CurlVersion->version); +} + +void +AccumulateProjectIndices(project_generations *A, db_header_project *P) +{ + AddEntryToGeneration(A, 0); + db_header_project *Child = LocateFirstChildProject(P); + IncrementCurrentGeneration(A); + for(int i = 0; i < P->ChildCount; ++i) + { + AccumulateProjectIndices(A, Child); + Child = SkipProjectAndChildren(Child); + } + DecrementCurrentGeneration(A); +} + +project_generations +CopyAccumulator(project_generations *G) +{ + project_generations Result = {}; + for(int i = 0; i < G->Count; ++i) + { + PushGeneration(&Result); + Result.EntriesInGeneration[i] = G->EntriesInGeneration[i]; + } + return Result; +} + +project_generations +InitAccumulator(project_generations *G) +{ + project_generations Result = {}; + for(int i = 0; i < G->CurrentGeneration; ++i) + { + PushGeneration(&Result); + ++Result.CurrentGeneration; + } + return Result; +} + +void +WriteEntireDatabase() +{ + // NOTE(matt): This may suffice when the only changes are to existing fixed-size variables, + // e.g. ProjectsBlock->GlobalSearchDir + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + fwrite(DB.Metadata.File.Buffer.Location, DB.Metadata.File.Buffer.Size, 1, DB.Metadata.File.Handle); + fclose(DB.Metadata.File.Handle); +} + +int +InitDB(void) +{ + // TODO(matt): InitDB() is called once on startup. This is correct for the .metadata file, because we only want one of + // those to house the info for all projects. However, we will want to create a .index file for each project, so + // need a separate InitIndex() function that we can call when looping over the projects after ParseConfig() + + DB.Metadata.File.Buffer.ID = BID_DATABASE; + DB.Metadata.File = InitFile(0, &Config->DatabaseLocation, EXT_NULL); + ReadFileIntoBuffer(&DB.Metadata.File); // NOTE(matt): Could we actually catch errors (permissions?) here and bail? + + if(DB.Metadata.File.Buffer.Location) + { + // TODO(matt): Handle this gracefully (it'll be an invalid file) + Assert(DB.Metadata.File.Buffer.Size >= sizeof(DB.Header)); + uint32_t OriginalDBVersion = 0; + uint32_t FirstInt = *(uint32_t *)DB.Metadata.File.Buffer.Location; + if(FirstInt != FOURCC("CNRA")) + { + // TODO(matt): More checking, somehow? Ideally this should be able to report "Invalid .metadata file" rather than + // trying to upgrade a file that isn't even valid + // TODO(matt): We should try and deprecate < CINERA_DB_VERSION 4 and enforce a clean rebuild for invalid DB files + // Perhaps do this either the next time we have to bump the version, or when we scrap Single Edition + OriginalDBVersion = FirstInt; + } + else + { + OriginalDBVersion = *(uint32_t *)(DB.Metadata.File.Buffer.Location + sizeof(DB.Header.HexSignature)); + } + + if(OriginalDBVersion < CINERA_DB_VERSION) + { + if(CINERA_DB_VERSION == 6) + { + fprintf(stderr, "\n%sHandle conversion from CINERA_DB_VERSION %d to %d!%s\n\n", ColourStrings[CS_ERROR], OriginalDBVersion, CINERA_DB_VERSION, ColourStrings[CS_END]); + FreeConfig(Config); + _exit(RC_ERROR_FATAL); + } + if(UpgradeDB(OriginalDBVersion) == RC_ERROR_FILE) { FreeConfig(Config); return RC_NOOP; } + } + else if(OriginalDBVersion > CINERA_DB_VERSION) + { + fprintf(stderr, "%sUnsupported DB Version (%d). Please upgrade Cinera%s\n", ColourStrings[CS_ERROR], OriginalDBVersion, ColourStrings[CS_END]); + FreeConfig(Config); + _exit(RC_ERROR_FATAL); + } + + db_header *Header = (db_header *)DB.Metadata.File.Buffer.Location; + Header->CurrentAppVersion = CINERA_APP_VERSION; + Header->CurrentHMMLVersion.Major = hmml_version.Major; + Header->CurrentHMMLVersion.Minor = hmml_version.Minor; + Header->CurrentHMMLVersion.Patch = hmml_version.Patch; + + buffer *B = &DB.Metadata.File.Buffer; + B->Ptr = B->Location + sizeof(*Header); + + for(int BlockIndex = 0; BlockIndex < Header->BlockCount; ++BlockIndex) + { + uint32_t BlockID = *(uint32_t *)B->Ptr; + if(BlockID == FOURCC("PROJ")) + { + DB.Metadata.Signposts.ProjectsBlock.Ptr = B->Ptr; + B->Ptr = SkipBlock(B->Ptr); + } + else if(BlockID == FOURCC("ASET")) + { + DB.Metadata.Signposts.AssetsBlock.Ptr = B->Ptr; + DB.Metadata.File.Buffer.Ptr = SkipBlock(B->Ptr); + } + else + { + fprintf(stderr, "%sMalformed metadata file%s: %s\n", + ColourStrings[CS_ERROR], ColourStrings[CS_END], DB.Metadata.File.Path); + } + } + + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + if(DB.Metadata.File.Handle) + { + fwrite(B->Location, B->Size, 1, DB.Metadata.File.Handle); + SetFileEditPosition(&DB.Metadata); + CycleSignpostedFile(&DB.Metadata); + } + else + { + // TODO(matt): Handle unopenable database files + PrintC(CS_RED, "Could not open database file: "); + PrintC(CS_MAGENTA, DB.Metadata.File.Path); + fprintf(stderr, "\n"); + _exit(0); + } + } + else + { + // NOTE(matt): Initialising new db_header + DB.Header.HexSignature = FOURCC("CNRA"); + DB.Header.InitialDBVersion = DB.Header.CurrentDBVersion = CINERA_DB_VERSION; + DB.Header.InitialAppVersion = DB.Header.CurrentAppVersion = CINERA_APP_VERSION; + DB.Header.InitialHMMLVersion.Major = DB.Header.CurrentHMMLVersion.Major = hmml_version.Major; + DB.Header.InitialHMMLVersion.Minor = DB.Header.CurrentHMMLVersion.Minor = hmml_version.Minor; + DB.Header.InitialHMMLVersion.Patch = DB.Header.CurrentHMMLVersion.Patch = hmml_version.Patch; + DB.Header.BlockCount = 0; + + DB.ProjectsBlock.BlockID = FOURCC("PROJ"); + ClearCopyStringNoFormat(DB.ProjectsBlock.GlobalSearchDir, sizeof(DB.ProjectsBlock.GlobalSearchDir), Config->GlobalSearchDir); + ClearCopyStringNoFormat(DB.ProjectsBlock.GlobalSearchURL, sizeof(DB.ProjectsBlock.GlobalSearchURL), Config->GlobalSearchURL); + DB.ProjectsBlock.Count = 0; + ++DB.Header.BlockCount; + + DB.AssetsBlock.BlockID = FOURCC("ASET"); + DB.AssetsBlock.Count = 0; + ClearCopyStringNoFormat(DB.AssetsBlock.RootDir, sizeof(DB.AssetsBlock.RootDir), Config->AssetsRootDir); + ClearCopyStringNoFormat(DB.AssetsBlock.RootURL, sizeof(DB.AssetsBlock.RootURL), Config->AssetsRootURL); + ClearCopyStringNoFormat(DB.AssetsBlock.CSSDir, sizeof(DB.AssetsBlock.CSSDir), Config->CSSDir); + ClearCopyStringNoFormat(DB.AssetsBlock.ImagesDir, sizeof(DB.AssetsBlock.ImagesDir), Config->ImagesDir); + ClearCopyStringNoFormat(DB.AssetsBlock.JSDir, sizeof(DB.AssetsBlock.JSDir), Config->JSDir); + ++DB.Header.BlockCount; + + char *DatabaseLocation0 = MakeString0("l", &Config->DatabaseLocation); + StripComponentFromPath0(DatabaseLocation0); + DIR *OutputDirectoryHandle = opendir(DatabaseLocation0); + if(!OutputDirectoryHandle) + { + if(!MakeDir(Wrap0(DatabaseLocation0))) + { + LogError(LOG_ERROR, "Unable to create directory %.*s: %s", (int)Config->DatabaseLocation.Length, Config->DatabaseLocation.Base, strerror(errno)); + fprintf(stderr, "Unable to create directory %.*s: %s\n", (int)Config->DatabaseLocation.Length, Config->DatabaseLocation.Base, strerror(errno)); + Free(DatabaseLocation0); + return RC_ERROR_DIRECTORY; + }; + } + else + { + closedir(OutputDirectoryHandle); + OutputDirectoryHandle = 0; + } + Free(DatabaseLocation0); + + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + if(DB.Metadata.File.Handle) + { + fwrite(&DB.Header, sizeof(DB.Header), 1, DB.Metadata.File.Handle); + + DB.Metadata.Signposts.ProjectsBlock.Byte = ftell(DB.Metadata.File.Handle); + fwrite(&DB.ProjectsBlock, sizeof(DB.ProjectsBlock), 1, DB.Metadata.File.Handle); + + DB.Metadata.Signposts.AssetsBlock.Byte = ftell(DB.Metadata.File.Handle); + fwrite(&DB.AssetsBlock, sizeof(DB.AssetsBlock), 1, DB.Metadata.File.Handle); + + fclose(DB.Metadata.File.Handle); + ReadFileIntoBuffer(&DB.Metadata.File); + DB.Metadata.Signposts.ProjectsBlock.Ptr = DB.Metadata.File.Buffer.Location + DB.Metadata.Signposts.ProjectsBlock.Byte; + DB.Metadata.Signposts.AssetsBlock.Ptr = DB.Metadata.File.Buffer.Location + DB.Metadata.Signposts.AssetsBlock.Byte; + } + else + { + // TODO(matt): Handle unopenable database files + PrintC(CS_RED, "Could not open database file: "); + PrintC(CS_MAGENTA_BOLD, DB.Metadata.File.Path); + fprintf(stderr, "\n"); + _exit(0); + } + +#if 0 + DB.File.Handle = fopen(DB.File.Path, "w"); + fprintf(DB.File.Handle, "---\n"); + fclose(DB.File.Handle); + ReadFileIntoBuffer(&DB.File); +#endif + } + return RC_SUCCESS; +} + +void +InitProjectInDBRecursively(project_generations *G, project *P) +{ + // TODO(matt): Remove this section once we know it has been run at least once! + char *BaseDir0 = MakeString0("l", &P->BaseDir); + DIR *BaseDirHandle = opendir(BaseDir0); + Free(BaseDir0); + if(BaseDirHandle) + { + struct dirent *BaseDirFiles; + + bool RemovedOldMetadataFile = FALSE; + while((BaseDirFiles = readdir(BaseDirHandle)) && !RemovedOldMetadataFile) + { + char *Ptr = BaseDirFiles->d_name; + Ptr += (StringLength(BaseDirFiles->d_name) - StringLength(".metadata")); + if(!(StringsDiffer0(Ptr, ".metadata"))) + { + char *MetadataPath = MakeString0("lss", &P->BaseDir, "/", BaseDirFiles->d_name); + remove(MetadataPath); + Free(MetadataPath); + rewinddir(BaseDirHandle); + while((BaseDirFiles = readdir(BaseDirHandle))) + { + string Filename = Wrap0(BaseDirFiles->d_name); + if(ExtensionMatches(Filename, EXT_INDEX)) + { + // NOTE(matt): Stack-string + int NullTerminationBytes = 1; + char IndexPath[P->BaseDir.Length + StringLength("/") + Filename.Length + NullTerminationBytes]; + char *Ptr = IndexPath; + Ptr += CopyStringToBarePtr(Ptr, P->BaseDir); + Ptr += CopyStringToBarePtr(Ptr, Wrap0("/")); + Ptr += CopyStringToBarePtr(Ptr, Filename); + *Ptr = '\0'; + remove(IndexPath); + break; + } + } + RemovedOldMetadataFile = TRUE; + } + } + closedir(BaseDirHandle); + } + // + //// + + + AddEntryToGeneration(G, P); + + db_header_project Project = {}; + CopyStringNoFormat(Project.ID, sizeof(Project.ID), P->ID); + CopyStringNoFormat(Project.Title, sizeof(Project.Title), P->Title); + // TODO(matt): Store the Theme as an index into the Assets block? + CopyStringNoFormat(Project.Theme, sizeof(Project.Theme), P->Theme); + CopyStringNoFormat(Project.BaseDir, sizeof(Project.BaseDir), P->BaseDir); + CopyStringNoFormat(Project.BaseURL, sizeof(Project.BaseURL), P->BaseURL); + CopyStringNoFormat(Project.SearchLocation, sizeof(Project.SearchLocation), P->SearchLocation); + CopyStringNoFormat(Project.PlayerLocation, sizeof(Project.PlayerLocation), P->PlayerLocation); + CopyStringNoFormat(Project.Unit, sizeof(Project.Unit), P->Unit); + Project.ArtIndex = SAI_UNSET; + Project.IconIndex = SAI_UNSET; + Project.EntryCount = 0; + Project.ChildCount = P->ChildCount; + + fwrite(&Project, sizeof(Project), 1, DB.Metadata.File.Handle); + AccumulateFileEditSize(&DB.Metadata, sizeof(Project)); + + IncrementCurrentGeneration(G); + for(int i = 0; i < P->ChildCount; ++i) + { + InitProjectInDBRecursively(G, &P->Child[i]); + } + DecrementCurrentGeneration(G); +} + +void +OffsetAssetLandmarks(db_asset *A, uint64_t *BytesThroughFile, project_generations *OldAcc, project_generations *NewAcc) +{ + db_landmark *Landmark = LocateFirstLandmark(A); + WriteFromByteToPointer(&DB.Metadata.File, BytesThroughFile, Landmark); + int i = 0; + while(i < A->LandmarkCount && Landmark->Project.Generation == -1) + { + fwrite(Landmark, sizeof(db_landmark), 1, DB.Metadata.File.Handle); + *BytesThroughFile += sizeof(db_landmark); + ++Landmark; + ++i; + } + + for(; i < A->LandmarkCount; ++i, ++Landmark) + { + db_landmark NewLandmark = *Landmark; + uint32_t OldEntriesInGeneration = OldAcc->EntriesInGeneration ? OldAcc->EntriesInGeneration[NewLandmark.Project.Generation] : 0; + if(NewLandmark.Project.Index >= OldEntriesInGeneration) + { + NewLandmark.Project.Index += + (NewLandmark.Project.Generation < NewAcc->Count ? NewAcc->EntriesInGeneration[NewLandmark.Project.Generation] : 0) + - + (NewLandmark.Project.Generation < OldAcc->Count ? OldEntriesInGeneration : 0); + } + fwrite(&NewLandmark, sizeof(db_landmark), 1, DB.Metadata.File.Handle); + *BytesThroughFile += sizeof(db_landmark); + } +} + +db_header_project * +InitProjectInDBPostamble(project_generations *OldAcc, project_generations *NewAcc) +{ + uint64_t ProjectHeaderPos = DB.Metadata.Signposts.Edit.BytePosition; + uint64_t Byte = ProjectHeaderPos; + + db_block_assets *AssetsBlock = LocateBlock(B_ASET); + db_asset *Asset = LocateFirstAsset(AssetsBlock); + WriteFromByteToPointer(&DB.Metadata.File, &Byte, Asset); + for(int i = 0; i < AssetsBlock->Count; ++i) + { + OffsetAssetLandmarks(Asset, &Byte, OldAcc, NewAcc); + Asset = SkipAsset(Asset); + } + + WriteFromByteToEnd(&DB.Metadata.File, Byte); + CycleSignpostedFile(&DB.Metadata); + DB.Metadata.Signposts.ProjectHeader.Ptr = DB.Metadata.File.Buffer.Location + ProjectHeaderPos; + return DB.Metadata.Signposts.ProjectHeader.Ptr; +} + +void +InsertProjectIntoDB_TopLevel(project_generations *G, db_block_projects **Block, db_header_project **Child, project *P) +{ + uint64_t Byte = 0; + + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + WriteFromByteToPointer(&DB.Metadata.File, &Byte, *Block); + + db_block_projects NewBlock = **Block; + ++NewBlock.Count; + fwrite(&NewBlock, sizeof(db_block_projects), 1, DB.Metadata.File.Handle); + Byte += sizeof(db_block_projects); + + WriteFromByteToPointer(&DB.Metadata.File, &Byte, *Child); + SetFileEditPosition(&DB.Metadata); + + project_generations OldG = CopyAccumulator(G); + InitProjectInDBRecursively(G, P); + uint64_t BlockPos = (char *)*Block - DB.Metadata.File.Buffer.Location; + *Child = InitProjectInDBPostamble(&OldG, G); + *Block = (db_block_projects *)(DB.Metadata.File.Buffer.Location + BlockPos); +} + +void +InsertProjectIntoDB(project_generations *G, db_header_project **Parent, db_header_project **Child, project *P) +{ + uint64_t Byte = 0; + + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + WriteFromByteToPointer(&DB.Metadata.File, &Byte, *Parent); + + db_header_project NewParent = **Parent; + ++NewParent.ChildCount; + fwrite(&NewParent, sizeof(db_header_project), 1, DB.Metadata.File.Handle); + Byte += sizeof(db_header_project); + + WriteFromByteToPointer(&DB.Metadata.File, &Byte, *Child); + SetFileEditPosition(&DB.Metadata); + + project_generations OldG = CopyAccumulator(G); + InitProjectInDBRecursively(G, P); + uint64_t PPos = (char *)*Parent - DB.Metadata.File.Buffer.Location; + *Child = InitProjectInDBPostamble(&OldG, G); + *Parent = (db_header_project *)(DB.Metadata.File.Buffer.Location + PPos); +} + +bool +ConfiguredAndStoredProjectIDsMatch(project *ConfiguredP, db_header_project *StoredP) +{ + return StringsMatch(ConfiguredP->ID, Wrap0i(StoredP->ID, sizeof(StoredP->ID))); +} + +void +DeleteLandmarksOfProjectAndChildren(db_asset *A, uint64_t *BytesThroughBuffer, project_generations *MainAcc, project_generations *LocalAcc) +{ + //db_project_index Index = GetCurrentProjectIndex(MainAcc); + //landmark_range LandmarkRange = DetermineProjectLandmarksRange(A, Index); + //LandmarkDeletionCount += LandmarkRange.Length; + + uint64_t LandmarkDeletionCount = 0; + for(int i = LocalAcc->CurrentGeneration; i < LocalAcc->Count; ++i) + { + for(int j = 0; j < LocalAcc->EntriesInGeneration[i]; ++j) + { + db_project_index Acc = {}; + Acc.Generation = i; + Acc.Index = j; + if(i < MainAcc->Count) + { + Acc.Index += MainAcc->EntriesInGeneration[i]; + } + landmark_range LandmarkRange = DetermineProjectLandmarksRange(A, Acc); + LandmarkDeletionCount += LandmarkRange.Length; + } + } + + AccumulateFileEditSize(&DB.Metadata, -(sizeof(db_landmark) * LandmarkDeletionCount)); + + db_asset NewAsset = *A; + NewAsset.LandmarkCount -= LandmarkDeletionCount; + + WriteFromByteToPointer(&DB.Metadata.File, BytesThroughBuffer, A); + fwrite(&NewAsset, sizeof(db_asset), 1, DB.Metadata.File.Handle); + *BytesThroughBuffer += sizeof(db_asset); + + // TODO(matt): For each generation in our LocalAcc + // Write out the landmarks to the first landmark in our range + // Omit the landmarks within our range + // Decrement the project index of the remaining landmarks in this generation + // + + db_landmark *FirstLandmark = LocateFirstLandmark(A); + uint64_t LandmarkIndex = 0; + for(int GenIndex = LocalAcc->CurrentGeneration; GenIndex < LocalAcc->Count; ++GenIndex) + { + uint64_t GenLandmarkDeletionCount = 0; + db_landmark *GenLandmark = 0; + //uint64_t LandmarkIndex = 0; + for(int j = 0; j < LocalAcc->EntriesInGeneration[GenIndex]; ++j) + { + db_project_index Acc = {}; + Acc.Generation = GenIndex; + Acc.Index = j; + if(GenIndex < MainAcc->Count) + { + Acc.Index += MainAcc->EntriesInGeneration[GenIndex]; + } + landmark_range LandmarkRange = DetermineProjectLandmarksRange(A, Acc); + GenLandmarkDeletionCount += LandmarkRange.Length; + if(!GenLandmark && LandmarkRange.Length > 0) + { + GenLandmark = FirstLandmark + LandmarkRange.First; + LandmarkIndex = LandmarkRange.First; + } + LandmarkIndex += LandmarkRange.Length; + } + + //db_landmark *Landmark = LocateFirstLandmark(A) + LandmarkRange.First; + if(GenLandmark) + { + WriteFromByteToPointer(&DB.Metadata.File, BytesThroughBuffer, GenLandmark); + *BytesThroughBuffer += sizeof(db_landmark) * GenLandmarkDeletionCount; + GenLandmark += GenLandmarkDeletionCount; + } + + db_landmark *TailLandmark = FirstLandmark + LandmarkIndex; + for(; + LandmarkIndex < A->LandmarkCount && TailLandmark->Project.Generation == GenIndex; + ++LandmarkIndex, ++TailLandmark) + { + db_landmark NewLandmark = *TailLandmark; + // NOTE(matt): These are all unsigned values. If the subtraction would yield a negative value, it actually ends up + // being positive because the high bit is set. Casting them to a larger type removes the possibility + // for under / overflow, and treating them as signed permits signed comparison + if((int64_t)NewLandmark.Project.Index - (int64_t)LocalAcc->EntriesInGeneration[GenIndex] >= (int64_t)MainAcc->EntriesInGeneration[GenIndex]) + { + NewLandmark.Project.Index -= LocalAcc->EntriesInGeneration[GenIndex]; + } + fwrite(&NewLandmark, sizeof(db_landmark), 1, DB.Metadata.File.Handle); + *BytesThroughBuffer += sizeof(db_landmark); + } + } + + db_landmark *Landmark = FirstLandmark + LandmarkIndex; + fwrite(Landmark, sizeof(db_landmark), A->LandmarkCount - LandmarkIndex, DB.Metadata.File.Handle); + *BytesThroughBuffer += sizeof(db_landmark) * (A->LandmarkCount - LandmarkIndex); +} + +void * +DeleteHTMLFilesOfProject(db_header_project *Project) +{ + char *Ptr = (char *)Project; + Ptr += sizeof(db_header_project) + sizeof(db_entry) * Project->EntryCount; + + db_entry *Entry = LocateFirstEntry(Project); + for(int i = 0; i < Project->EntryCount; ++i, ++Entry) + { + DeletePlayerPageFromFilesystem(Project, Wrap0i(Entry->OutputLocation, sizeof(Entry->OutputLocation)), FALSE, FALSE); + } + + DeleteSearchPageFromFilesystem(Project); + + return Ptr; +} + +void * +DeleteHTMLFilesOfProjectAndChildren(db_header_project *Project) +{ + db_header_project *Ptr = DeleteHTMLFilesOfProject(Project); + for(int i = 0; i < Project->ChildCount; ++i) + { + Ptr = DeleteHTMLFilesOfProjectAndChildren(Ptr); + } + DeleteSearchPageFromFilesystem(Project); + return Ptr; +} + +void +DeleteProjectInterior(db_header_project **Child, project_generations *G, uint64_t *BytesThroughBuffer) +{ + WriteFromByteToPointer(&DB.Metadata.File, BytesThroughBuffer, *Child); + + SetFileEditPosition(&DB.Metadata); + + char *AfterChild = DeleteHTMLFilesOfProjectAndChildren(*Child); + AccumulateFileEditSize(&DB.Metadata, -(AfterChild - (char *)*Child)); + *BytesThroughBuffer += AfterChild - (char *)*Child; + + DB.Metadata.Signposts.AssetsBlock.Ptr = LocateBlock(B_ASET); + db_block_assets *AssetsBlock = DB.Metadata.Signposts.AssetsBlock.Ptr; + db_asset *Asset = LocateFirstAsset(AssetsBlock); + + WriteFromByteToPointer(&DB.Metadata.File, BytesThroughBuffer, Asset); + + project_generations ChildAccumulator = InitAccumulator(G); + AccumulateProjectIndices(&ChildAccumulator, *Child); + + for(int AssetIndex = 0; AssetIndex < AssetsBlock->Count; ++AssetIndex) + { + DeleteLandmarksOfProjectAndChildren(Asset, BytesThroughBuffer, G, &ChildAccumulator); + Asset = SkipAsset(Asset); + } + + *BytesThroughBuffer += WriteFromByteToEnd(&DB.Metadata.File, *BytesThroughBuffer); + CycleSignpostedFile(&DB.Metadata); +} + +void +DeleteProject_TopLevel(db_block_projects **Parent, db_header_project **Child, project_generations *G) +{ + PrintFunctionName("DeleteProject_TopLevel()"); + // TODO(matt); Print out something sensible to inform real life users + // TODO(matt): + // + // 0:1 + // 1:4 + uint64_t PPos = (char *)*Parent - DB.Metadata.File.Buffer.Location; + uint64_t CPos = (char *)*Child - DB.Metadata.File.Buffer.Location; + + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + uint64_t Byte = 0; + + WriteFromByteToPointer(&DB.Metadata.File, &Byte, *Parent); + + db_block_projects NewParent = **Parent; + --NewParent.Count; + + fwrite(&NewParent, sizeof(db_block_projects), 1, DB.Metadata.File.Handle); + Byte += sizeof(db_block_projects); + + DeleteProjectInterior(Child, G, &Byte); + + *Parent = (db_block_projects *)(DB.Metadata.File.Buffer.Location + PPos); + *Child = (db_header_project *)(DB.Metadata.File.Buffer.Location + CPos); +} + +void +DeleteProject(db_header_project **Parent, db_header_project **Child, project_generations *G) +{ + PrintFunctionName("DeleteProject()"); + // TODO(matt); Print out something sensible to inform real life users + // TODO(matt): + // + // 0:1 + // 1:4 + uint64_t PPos = (char *)*Parent - DB.Metadata.File.Buffer.Location; + uint64_t CPos = (char *)*Child - DB.Metadata.File.Buffer.Location; + //db_project_index Index = GetCurrentProjectIndex(G); + + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + uint64_t Byte = 0; + + WriteFromByteToPointer(&DB.Metadata.File, &Byte, *Parent); + + db_header_project NewParent = **Parent; + --NewParent.ChildCount; + + fwrite(&NewParent, sizeof(db_header_project), 1, DB.Metadata.File.Handle); + Byte += sizeof(db_header_project); + + DeleteProjectInterior(Child, G, &Byte); + + *Parent = (db_header_project *)(DB.Metadata.File.Buffer.Location + PPos); + *Child = (db_header_project *)(DB.Metadata.File.Buffer.Location + CPos); +} + +void SyncProjects(project_generations *G, project *C, db_header_project **SParent, db_header_project **SChild); +void SyncProjects_TopLevel(project_generations *G, project *C, db_block_projects **SParent, db_header_project **SChild); + +db_landmark * +DetermineFirstLandmarkAndRangeOfProjectHierarchy(db_asset *A, + project_generations *MainAcc, project_generations *PrevAcc, project_generations *ThisAcc, + uint32_t GenIndex, landmark_range *Dest) +{ + db_landmark *Result = 0; + for(int j = 0; j < ThisAcc->EntriesInGeneration[GenIndex]; ++j) + { + db_project_index ThisIndex = {}; + ThisIndex.Generation = GenIndex; + ThisIndex.Index = j; + if(MainAcc && GenIndex < MainAcc->Count) { ThisIndex.Index += MainAcc->EntriesInGeneration[GenIndex]; } + if(PrevAcc && GenIndex < PrevAcc->Count) { ThisIndex.Index += PrevAcc->EntriesInGeneration[GenIndex]; } + landmark_range LandmarkRange = DetermineProjectLandmarksRange(A, ThisIndex); + if(!Result && LandmarkRange.Length > 0) + { + Dest->First = LandmarkRange.First; + Result = LocateFirstLandmark(A) + LandmarkRange.First; + } + Dest->Length += LandmarkRange.Length; + } + return Result; +} + +bool +ReorganiseProjectsInterior(project_generations *G, project *CChild, uint64_t ChildIndex, uint64_t ChildCount, db_header_project *SChild) +{ + bool Result = FALSE; + project_generations LocalAcc = InitAccumulator(G); + AccumulateProjectIndices(&LocalAcc, SChild); + + db_header_project *This = SkipProjectAndChildren(SChild); + + for(++ChildIndex; ChildIndex < ChildCount; ++ChildIndex) + { + if(ConfiguredAndStoredProjectIDsMatch(CChild, This)) + { + project_generations ThisAcc = InitAccumulator(G); + AccumulateProjectIndices(&ThisAcc, This); + + DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w"); + uint64_t Byte = 0; + + WriteFromByteToPointer(&DB.Metadata.File, &Byte, SChild); + + char *Next = SkipProjectAndChildren(This); + + db_block_assets *AssetsBlock = LocateBlock(B_ASET); + db_asset *Asset = LocateFirstAsset(AssetsBlock); + WriteFromPointerToPointer(&DB.Metadata.File, This, Next, &Byte); + WriteFromPointerToPointer(&DB.Metadata.File, SChild, This, &Byte); + WriteFromPointerToPointer(&DB.Metadata.File, Next, Asset, &Byte); + + // TODO(matt): Don't do this. Instead, do the actual assets loop, fixing up the landmarks + //WriteFromByteToEnd(&DB.Metadata.File, &Byte); + + for(int AssetIndex = 0; AssetIndex < AssetsBlock->Count; ++AssetIndex) + { + uint64_t RunningLandmarkIndex = 0; + db_landmark *FirstLandmark = LocateFirstLandmark(Asset); + WriteFromByteToPointer(&DB.Metadata.File, &Byte, FirstLandmark); + for(uint64_t GenIndex = 0; GenIndex < ThisAcc.Count; ++GenIndex) + { + landmark_range ThisRange = {}; + db_landmark *ThisLandmark = DetermineFirstLandmarkAndRangeOfProjectHierarchy(Asset, G, &LocalAcc, &ThisAcc, GenIndex, &ThisRange); + landmark_range LocalAccRange = {}; + db_landmark *LocalAccLandmark = DetermineFirstLandmarkAndRangeOfProjectHierarchy(Asset, G, 0, &LocalAcc, GenIndex, &LocalAccRange); + + if(LocalAccLandmark) + { + WriteFromByteToPointer(&DB.Metadata.File, &Byte, LocalAccLandmark); + RunningLandmarkIndex = LocalAccRange.First; + } + + for(int ThisLandmarkIndex = 0; ThisLandmarkIndex < ThisRange.Length; + ++ThisLandmarkIndex, ++RunningLandmarkIndex, ++ThisLandmark) + { + db_landmark NewLandmark = *ThisLandmark; + if(GenIndex < LocalAcc.Count) + { + NewLandmark.Project.Index -= LocalAcc.EntriesInGeneration[GenIndex]; + } + fwrite(&NewLandmark, sizeof(db_landmark), 1, DB.Metadata.File.Handle); + Byte += sizeof(db_landmark); + } + + for(int LocalAccLandmarkIndex = 0; LocalAccLandmarkIndex < LocalAccRange.Length; + ++LocalAccLandmarkIndex, ++RunningLandmarkIndex, ++LocalAccLandmark) + { + db_landmark NewLandmark = *LocalAccLandmark; + NewLandmark.Project.Index += ThisAcc.EntriesInGeneration[GenIndex]; + fwrite(&NewLandmark, sizeof(db_landmark), 1, DB.Metadata.File.Handle); + Byte += sizeof(db_landmark); + } + + db_landmark *TailLandmark = FirstLandmark + RunningLandmarkIndex; + for(; RunningLandmarkIndex < Asset->LandmarkCount && TailLandmark->Project.Generation == GenIndex; + ++RunningLandmarkIndex, ++TailLandmark) + { + fwrite(TailLandmark, sizeof(db_landmark), 1, DB.Metadata.File.Handle); + Byte += sizeof(db_landmark); + } + } + + db_landmark *Landmark = FirstLandmark + RunningLandmarkIndex; + fwrite(Landmark, sizeof(db_landmark), Asset->LandmarkCount - RunningLandmarkIndex, DB.Metadata.File.Handle); + Byte += sizeof(db_landmark) * (Asset->LandmarkCount - RunningLandmarkIndex); + + // For each generation + // Write up to the start of the LocalAcc block + // Write out the landmarks for This, --ing their Project.Index by the LocalAcc[Gen].EntriesInGeneration + // Write landmarks for projects in LocalAcc, ++ing their Project.Index by ThisAcc[Gen].EntriesInGeneration + // Write out the remaining landmarks for this generation + Asset = SkipAsset(Asset); + } + + CycleSignpostedFile(&DB.Metadata); + Result = TRUE; + break; + } + else + { + AccumulateProjectIndices(&LocalAcc, This); + } + This = SkipProjectAndChildren(This); + } + return Result; +} + +bool +ReorganiseProjects(project_generations *G, project *CChild, db_header_project **SParent, uint64_t ChildIndex, db_header_project **SChild) +{ + uint64_t SParentPos = (char *)*SParent - DB.Metadata.File.Buffer.Location; + uint64_t SChildPos = (char *)*SChild - DB.Metadata.File.Buffer.Location; + + bool Result = ReorganiseProjectsInterior(G, CChild, ChildIndex, (*SParent)->ChildCount, *SChild); + + if(Result == TRUE) + { + *SParent = (db_header_project *)(DB.Metadata.File.Buffer.Location + SParentPos); + *SChild = (db_header_project *)(DB.Metadata.File.Buffer.Location + SChildPos); + AddEntryToGeneration(G, CChild); + SyncProjects(G, CChild, SParent, SChild); + } + return Result; +} + +bool +ReorganiseProjects_TopLevel(project_generations *G, project *CChild, db_block_projects **SParent, uint64_t ChildIndex, db_header_project **SChild) +{ + uint64_t SParentPos = (char *)*SParent - DB.Metadata.File.Buffer.Location; + uint64_t SChildPos = (char *)*SChild - DB.Metadata.File.Buffer.Location; + + bool Result = ReorganiseProjectsInterior(G, CChild, ChildIndex, (*SParent)->Count, *SChild); + + if(Result == TRUE) + { + *SParent = (db_block_projects *)(DB.Metadata.File.Buffer.Location + SParentPos); + *SChild = (db_header_project *)(DB.Metadata.File.Buffer.Location + SChildPos); + AddEntryToGeneration(G, CChild); + SyncProjects_TopLevel(G, CChild, SParent, SChild); + } + return Result; +} + +void +SyncProjects_TopLevel(project_generations *G, project *C, db_block_projects **SParent, db_header_project **SChild) +{ + uint64_t SChildPos = (char *)*SChild - DB.Metadata.File.Buffer.Location; + uint64_t SParentPos = (char *)*SParent - DB.Metadata.File.Buffer.Location; + + IncrementCurrentGeneration(G); + + db_header_project *SGrandChild = LocateFirstChildProject(*SChild); + + for(int ci = 0; ci < C->ChildCount; ++ci) + { + bool Located = FALSE; + project *CChild = &C->Child[ci]; + if(ci < (*SChild)->ChildCount) + { + if(ConfiguredAndStoredProjectIDsMatch(CChild, SGrandChild)) + { + Located = TRUE; + AddEntryToGeneration(G, CChild); + SyncProjects(G, CChild, SChild, &SGrandChild); + } + else + { + Located = ReorganiseProjects(G, CChild, SChild, ci, &SGrandChild); + } + } + + if(!Located) + { + InsertProjectIntoDB(G, SChild, &SGrandChild, CChild); + } + + SGrandChild = SkipProjectAndChildren(SGrandChild); + } + + uint64_t DeletionCount = (*SChild)->ChildCount - C->ChildCount; + for(int DeletionIndex = 0; DeletionIndex < DeletionCount; ++DeletionIndex) + { + DeleteProject(SChild, &SGrandChild, G); + } + + if((*SChild)->ChildCount == 0 && (*SChild)->EntryCount == 0) + { + DeleteSearchPageFromFilesystem(*SChild); + DeleteLandmarksForSearch(C->Index); + DeleteStaleAssets(); + } + + DecrementCurrentGeneration(G); + *SParent = (db_block_projects *)(DB.Metadata.File.Buffer.Location + SParentPos); + *SChild = (db_header_project *)(DB.Metadata.File.Buffer.Location + SChildPos); +} + +void +SyncProjects(project_generations *G, project *C, db_header_project **SParent, db_header_project **SChild) +{ + uint64_t SChildPos = (char *)*SChild - DB.Metadata.File.Buffer.Location; + uint64_t SParentPos = (char *)*SParent - DB.Metadata.File.Buffer.Location; + + IncrementCurrentGeneration(G); + + db_header_project *SGrandChild = LocateFirstChildProject(*SChild); + + for(int ci = 0; ci < C->ChildCount; ++ci) + { + bool Located = FALSE; + project *CChild = &C->Child[ci]; + if(ci < (*SChild)->ChildCount) + { + if(ConfiguredAndStoredProjectIDsMatch(CChild, SGrandChild)) + { + Located = TRUE; + AddEntryToGeneration(G, CChild); + SyncProjects(G, CChild, SChild, &SGrandChild); + } + else + { + Located = ReorganiseProjects(G, CChild, SChild, ci, &SGrandChild); + } + } + + if(!Located) + { + InsertProjectIntoDB(G, SChild, &SGrandChild, CChild); + } + + SGrandChild = SkipProjectAndChildren(SGrandChild); + } + + uint64_t DeletionCount = (*SChild)->ChildCount - C->ChildCount; + for(int DeletionIndex = 0; DeletionIndex < DeletionCount; ++DeletionIndex) + { + DeleteProject(SChild, &SGrandChild, G); + } + + if((*SChild)->ChildCount == 0 && (*SChild)->EntryCount == 0) + { + DeleteSearchPageFromFilesystem(*SChild); + DeleteLandmarksForSearch(C->Index); + } + + DecrementCurrentGeneration(G); + *SParent = (db_header_project *)(DB.Metadata.File.Buffer.Location + SParentPos); + *SChild = (db_header_project *)(DB.Metadata.File.Buffer.Location + SChildPos); +} + +void +SyncDB(config *C) +{ + // TODO(matt): Ensure that the project hierarchy in the db and config are in alignment + // Update the Project.Index in db_landmark if projects have been reorganised + // Do the stuff that SyncDBWithInput() does + // + // After having done all this, I think we should just be monitoring the filesystem for changed files and in a + // position to figure out how to set the CurrentProject + // + DB.Metadata.Signposts.ProjectsBlock.Ptr = LocateBlock(B_PROJ); + project_generations Accumulator = {}; + + db_block_projects *SParent = DB.Metadata.Signposts.ProjectsBlock.Ptr; + + db_header_project *SChild = LocateFirstChildProjectOfBlock(SParent); + for(uint64_t ci = 0; ci < C->ProjectCount; ++ci) + { + bool Located = FALSE; + project *CChild = &C->Project[ci]; + if(ci < SParent->Count) + { + if(ConfiguredAndStoredProjectIDsMatch(CChild, SChild)) + { + Located = TRUE; + AddEntryToGeneration(&Accumulator, CChild); + SyncProjects_TopLevel(&Accumulator, CChild, &SParent, &SChild); + } + else + { + Located = ReorganiseProjects_TopLevel(&Accumulator, CChild, &SParent, ci, &SChild); + } + } + + if(!Located) + { + InsertProjectIntoDB_TopLevel(&Accumulator, &SParent, &SChild, CChild); + } + + SChild = SkipProjectAndChildren(SChild); + } + + uint64_t DeletionCount = SParent->Count - C->ProjectCount; + for(int DeletionIndex = 0; DeletionIndex < DeletionCount; ++DeletionIndex) + { + DeleteProject_TopLevel(&SParent, &SChild, &Accumulator); + } + + DeleteStaleAssets(); + //PrintAssetsBlock(); + + //PrintConfig(C); + + PrintGenerations(&Accumulator, FALSE); + FreeGenerations(&Accumulator); + //_exit(0); +} + +void +SyncGlobalPagesWithInput(neighbourhood *N, buffers *CollationBuffers) +{ + db_block_projects *ProjectsBlock = DB.Metadata.Signposts.ProjectsBlock.Ptr; + string StoredGlobalSearchDir = Wrap0i(ProjectsBlock->GlobalSearchDir, sizeof(ProjectsBlock->GlobalSearchDir)); + string StoredGlobalSearchURL = Wrap0i(ProjectsBlock->GlobalSearchURL, sizeof(ProjectsBlock->GlobalSearchURL)); + char *StoredGlobalSearchDir0 = MakeString0("l", &StoredGlobalSearchDir); + if(StringsDiffer(StoredGlobalSearchDir, Config->GlobalSearchDir)) + { + ClearCopyStringNoFormat(ProjectsBlock->GlobalSearchDir, sizeof(ProjectsBlock->GlobalSearchDir), Config->GlobalSearchDir); + WriteEntireDatabase(); + } + + if(StringsDiffer(StoredGlobalSearchURL, Config->GlobalSearchURL)) + { + ClearCopyStringNoFormat(ProjectsBlock->GlobalSearchURL, sizeof(ProjectsBlock->GlobalSearchURL), Config->GlobalSearchURL); + WriteEntireDatabase(); + } + + if(!ProjectsBlock->GlobalSearchDir[0]) + { + DeleteGlobalSearchPageFromFilesystem(StoredGlobalSearchDir0); + DeleteLandmarksForSearch(GLOBAL_SEARCH_PAGE_INDEX); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + } + else if(StringLength(StoredGlobalSearchDir0) > 0 && StringsDifferLv0(Config->GlobalSearchDir, StoredGlobalSearchDir0)) + { + // TODO(matt): Properly stress test this relocation code! + MakeDir(Config->GlobalSearchDir); + string StoredGlobalSearchDirL = Wrap0(StoredGlobalSearchDir0); + char *OldPath = ConstructHTMLIndexFilePath(0, &StoredGlobalSearchDirL, 0); + char *NewPath = ConstructHTMLIndexFilePath(0, &Config->GlobalSearchDir, 0); + rename(OldPath, NewPath); + Free(OldPath); + Free(NewPath); + } + + if(StringLength(StoredGlobalSearchDir0)) + { + GenerateGlobalSearchPage(N, CollationBuffers); + } + // TODO(matt): Come back to this! + Free(StoredGlobalSearchDir0); +} + +void +SetCacheDirectory(config *DefaultConfig) +{ + // TODO(matt): This might not have to exist because we should have a default cache directory already set from the config + int Flags = WRDE_NOCMD | WRDE_UNDEF | WRDE_APPEND; + wordexp_t Expansions = {}; + wordexp("$XDG_CACHE_HOME/cinera", &Expansions, Flags); + wordexp("$HOME/.cache/cinera", &Expansions, Flags); + if(Expansions.we_wordc > 0 ) + { +#if AFE + CopyString(DefaultConfig->CacheDir, sizeof(DefaultConfig->CacheDir), Expansions.we_wordv[0]); +#endif + } + wordfree(&Expansions); +} + +void +InitMemoryArena(arena *Arena, int Size) +{ + Arena->Size = Size; + if(!(Arena->Location = calloc(1, Arena->Size))) + { + _exit(RC_ERROR_MEMORY); + } + Arena->Ptr = Arena->Location; +} + +bool +ProjectIndexIs(db_project_index I, uint64_t Generation, uint64_t Index) +{ + return(I.Generation == Generation && I.Index == Index); +} + +void +GenerateGlobalNavigationRecursively(config *C, project *P, uint32_t *IndentationLevel) +{ + OpenNodeCNewLine(&C->NavGeneric, IndentationLevel, NODE_LI, 0); + OpenNode(&C->NavGeneric, IndentationLevel, NODE_A, 0); + AppendStringToBuffer(&C->NavGeneric, Wrap0(" href=\"")); + + buffer URL; + ClaimBuffer(&URL, BID_URL_SEARCH, MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1); + ConstructSearchURL(&URL, P); + AppendBuffer(&C->NavGeneric, &URL); + DeclaimBuffer(&URL); + + AppendStringToBuffer(&C->NavGeneric, Wrap0("\">")); + AppendStringToBuffer(&C->NavGeneric, P->HTMLTitle.Length ? P->HTMLTitle: P->Title); + CloseNode(&C->NavGeneric, IndentationLevel, NODE_A); + + if(P->ChildCount > 0) + { + OpenNodeCNewLine(&C->NavGeneric, IndentationLevel, NODE_UL, 0); + } + + for(int i = 0; i < P->ChildCount; ++i) + { + project *Child = &P->Child[i]; + GenerateGlobalNavigationRecursively(C, Child, IndentationLevel); + } + + if(P->ChildCount > 0) + { + CloseNodeNewLine(&C->NavGeneric, IndentationLevel, NODE_UL); + CloseNodeNewLine(&C->NavGeneric, IndentationLevel, NODE_LI); + } + else + { + CloseNode(&C->NavGeneric, IndentationLevel, NODE_LI); + } +} + +void +GenerateGlobalNavigationBars(config *C) +{ + // NavGeneric; + // NavDropdownPre; + // NavDropdownPost; + // NavHorizontalPre; + // NavPlainPre; + + uint32_t IndentationLevel = 0; + OpenNode(&C->NavDropdownPre, &IndentationLevel, NODE_NAV, 0); + AppendStringToBuffer(&C->NavDropdownPre, Wrap0(" class=\"cineraNavDropdown ")); + AppendStringToBuffer(&C->NavDropdownPre, C->GlobalTheme); + AppendStringToBuffer(&C->NavDropdownPre, Wrap0("\">")); + OpenNodeNewLine(&C->NavDropdownPre, &IndentationLevel, NODE_NAV, 0); + AppendStringToBuffer(&C->NavDropdownPre, Wrap0(" class=\"cineraNavTitle\">")); + AppendStringToBuffer(&C->NavDropdownPre, Wrap0("Indexed Episode Guides")); + CloseNode(&C->NavDropdownPre, &IndentationLevel, NODE_NAV); + OpenNodeNewLine(&C->NavDropdownPre, &IndentationLevel, NODE_DIV, 0); + AppendStringToBuffer(&C->NavDropdownPre, Wrap0(" class=\"cineraPositioner\">")); + + CloseNodeNewLine(&C->NavDropdownPost, &IndentationLevel, NODE_DIV); + CloseNodeNewLine(&C->NavDropdownPost, &IndentationLevel, NODE_NAV); + + IndentationLevel = 0; + OpenNode(&C->NavHorizontalPre, &IndentationLevel, NODE_UL, 0); + AppendStringToBuffer(&C->NavHorizontalPre, Wrap0(" class=\"cineraNavHorizontal ")); + AppendStringToBuffer(&C->NavHorizontalPre, C->GlobalTheme); + AppendStringToBuffer(&C->NavHorizontalPre, Wrap0("\">")); + + IndentationLevel = 0; + OpenNode(&C->NavPlainPre, &IndentationLevel, NODE_UL, 0); + AppendStringToBuffer(&C->NavPlainPre, Wrap0(" class=\"cineraNavPlain ")); + AppendStringToBuffer(&C->NavPlainPre, C->GlobalTheme); + AppendStringToBuffer(&C->NavPlainPre, Wrap0("\">")); + + IndentationLevel = 1; + + for(int i = 0; i < C->ProjectCount; ++i) + { + project *P = &C->Project[i]; + GenerateGlobalNavigationRecursively(C, P, &IndentationLevel); + } + + CloseNodeNewLine(&C->NavGeneric, &IndentationLevel, NODE_UL); +} + +void +GenerateNavigationRecursively(project *TargetP, project *P, uint32_t *IndentationLevel) +{ + if(TargetP == P) + { + OpenNodeNewLine(&TargetP->NavGeneric, IndentationLevel, NODE_LI, 0); + AppendStringToBuffer(&TargetP->NavGeneric, Wrap0(" class=\"current\">")); + } + else + { + OpenNodeCNewLine(&TargetP->NavGeneric, IndentationLevel, NODE_LI, 0); + } + OpenNode(&TargetP->NavGeneric, IndentationLevel, NODE_A, 0); + AppendStringToBuffer(&TargetP->NavGeneric, Wrap0(" href=\"")); + + buffer URL; + ClaimBuffer(&URL, BID_URL_SEARCH, MAX_BASE_URL_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1); + ConstructSearchURL(&URL, P); + AppendBuffer(&TargetP->NavGeneric, &URL); + DeclaimBuffer(&URL); + + AppendStringToBuffer(&TargetP->NavGeneric, Wrap0("\">")); + AppendStringToBuffer(&TargetP->NavGeneric, P->HTMLTitle.Length ? P->HTMLTitle : P->Title); + CloseNode(&TargetP->NavGeneric, IndentationLevel, NODE_A); + + if(P->ChildCount > 0) + { + OpenNodeCNewLine(&TargetP->NavGeneric, IndentationLevel, NODE_UL, 0); + } + + for(int i = 0; i < P->ChildCount; ++i) + { + project *Child = &P->Child[i]; + GenerateNavigationRecursively(TargetP, Child, IndentationLevel); + } + + if(P->ChildCount > 0) + { + CloseNodeNewLine(&TargetP->NavGeneric, IndentationLevel, NODE_UL); + CloseNodeNewLine(&TargetP->NavGeneric, IndentationLevel, NODE_LI); + } + else + { + CloseNode(&TargetP->NavGeneric, IndentationLevel, NODE_LI); + } +} + +void +GenerateNavigationBars(project *P) +{ + // NavGeneric; + // NavDropdownPre; + // NavDropdownPost; + // NavHorizontalPre; + // NavPlainPre; + + uint32_t IndentationLevel = 0; + OpenNode(&P->NavDropdownPre, &IndentationLevel, NODE_NAV, 0); + AppendStringToBuffer(&P->NavDropdownPre, Wrap0(" class=\"cineraNavDropdown ")); + AppendStringToBuffer(&P->NavDropdownPre, P->Theme); + AppendStringToBuffer(&P->NavDropdownPre, Wrap0("\">")); + OpenNodeNewLine(&P->NavDropdownPre, &IndentationLevel, NODE_NAV, 0); + AppendStringToBuffer(&P->NavDropdownPre, Wrap0(" class=\"cineraNavTitle\">")); + AppendStringToBuffer(&P->NavDropdownPre, P->HTMLTitle.Length ? P->HTMLTitle : P->Title); + CloseNode(&P->NavDropdownPre, &IndentationLevel, NODE_NAV); + OpenNodeNewLine(&P->NavDropdownPre, &IndentationLevel, NODE_DIV, 0); + AppendStringToBuffer(&P->NavDropdownPre, Wrap0(" class=\"cineraPositioner\">")); + + CloseNodeNewLine(&P->NavDropdownPost, &IndentationLevel, NODE_DIV); + CloseNodeNewLine(&P->NavDropdownPost, &IndentationLevel, NODE_NAV); + + IndentationLevel = 0; + OpenNode(&P->NavHorizontalPre, &IndentationLevel, NODE_UL, 0); + AppendStringToBuffer(&P->NavHorizontalPre, Wrap0(" class=\"cineraNavHorizontal ")); + AppendStringToBuffer(&P->NavHorizontalPre, P->Theme); + AppendStringToBuffer(&P->NavHorizontalPre, Wrap0("\">")); + + IndentationLevel = 0; + OpenNode(&P->NavPlainPre, &IndentationLevel, NODE_UL, 0); + AppendStringToBuffer(&P->NavPlainPre, Wrap0(" class=\"cineraNavPlain ")); + AppendStringToBuffer(&P->NavPlainPre, P->Theme); + AppendStringToBuffer(&P->NavPlainPre, Wrap0("\">")); + + IndentationLevel = 1; + + project *Parent = P; + while(Parent->Parent) + { + Parent = Parent->Parent; + } + + // TODO(matt): Should this be calling GenerateNavigationRecursively() on the Parent? + for(int i = 0; i < Parent->ChildCount; ++i) + { + project *Child = &Parent->Child[i]; + GenerateNavigationRecursively(P, Child, &IndentationLevel); + } + + CloseNodeNewLine(&P->NavGeneric, &IndentationLevel, NODE_UL); +} + +void +SyncProject(project *P, neighbourhood *N, buffers *CollationBuffers, template *BespokeTemplate) +{ + for(int i = 0; i < P->ChildCount; ++i) + { + SyncProject(&P->Child[i], N, CollationBuffers, BespokeTemplate); + } + + SetCurrentProject(P, N); + + fprintf(stderr, "\n" + "┌─ "); + PrintLineage(P->Lineage, FALSE); + fprintf(stderr, " ─╼"); + + bool Titled = FALSE; + bool TitledTemplates = FALSE; + // TODO(matt): We need to figure out a way to stay awake beyond the detection of an invalid template + if(CurrentProject->PlayerTemplatePath.Length > 0) + { + if(!TitledTemplates) + { + fprintf(stderr, "\n" + "%s─── Packing templates ───╼\n", Titled ? "╾" : "└"); + TitledTemplates = TRUE; + Titled = TRUE; + } + switch(PackTemplate(&CurrentProject->PlayerTemplate, CurrentProject->PlayerTemplatePath, TEMPLATE_PLAYER)) + { + case RC_ERROR_FILE: // Could not load template + case RC_ERROR_MEMORY: // Could not allocate memory for template + // TODO(matt): Urgently decide how we handle the case in which we've been given an invalid template + free(MemoryArena.Location); + _exit(0); + case RC_INVALID_TEMPLATE: // Invalid template + case RC_SUCCESS: + break; + } + } + + // TODO(matt): We need to figure out a way to stay awake beyond the detection of an invalid template + if(CurrentProject->SearchTemplatePath.Length > 0) + { + if(!TitledTemplates) + { + fprintf(stderr, "\n" + "%s─── Packing templates ───╼\n", Titled ? "╾" : "└"); + TitledTemplates = TRUE; + Titled = TRUE; + } + switch(PackTemplate(&CurrentProject->SearchTemplate, CurrentProject->SearchTemplatePath, TEMPLATE_SEARCH)) + { + case RC_ERROR_MEMORY: // Could not allocate memory for template + case RC_ERROR_FILE: // Could not load template + // TODO(matt): Urgently decide how we handle the case in which we've been given an invalid template + Free(MemoryArena.Location); + _exit(0); + case RC_INVALID_TEMPLATE: // Invalid template + case RC_SUCCESS: + break; + } + } + + GenerateNavigationBars(P); + + fprintf(stderr, "\n" + "%s─── Synchronising with Input Directories ───╼\n", Titled ? "╾" : "└"); + SyncDBWithInput(N, CollationBuffers, BespokeTemplate); + + PushWatchHandle(P->HMMLDir, EXT_HMML, WT_HMML, P, 0); +} + #define DEBUG_EVENTS 0 #if DEBUG_EVENTS void @@ -7937,11 +14694,163 @@ PrintEvent(struct inotify_event *Event, int EventIndex) } #endif -int -MonitorFilesystem(buffers *CollationBuffers, template *SearchTemplate, template *PlayerTemplate, template *BespokeTemplate) +void +InitAll(neighbourhood *Neighbourhood, buffers *CollationBuffers, template *BespokeTemplate) { - buffer Events; - if(ClaimBuffer(&Events, "inotify Events", Kilobytes(4)) == RC_ARENA_FULL) { return RC_ARENA_FULL; }; + RewindCollationBuffers(CollationBuffers); + InitDB(); + + // TODO(matt): Straight up remove these PrintAssetsBlock() calls + //PrintAssetsBlock(0); + + SyncDB(Config); + + //PrintAssetsBlock(0); + + + printf("\n╾─ Hashing assets ─╼\n"); + // NOTE(matt): This had to happen before PackTemplate() because those guys may need to do PushAsset() and we must + // ensure that the builtin assets get placed correctly + InitAssets(); + PushConfiguredAssets(); + // + //// + + if(Config->GlobalSearchTemplatePath.Length > 0) + { + fprintf(stderr, "\n"); + switch(PackTemplate(&Config->SearchTemplate, Config->GlobalSearchTemplatePath, TEMPLATE_GLOBAL_SEARCH)) + { + case RC_ERROR_MEMORY: // Could not allocate memory for template + case RC_ERROR_FILE: // Could not load template + // TODO(matt): Urgently decide how we handle the case in which we've been given an invalid template + free(MemoryArena.Location); + _exit(0); + case RC_INVALID_TEMPLATE: // Invalid template + case RC_SUCCESS: + break; + } + } + + GenerateGlobalNavigationBars(Config); + + for(int i = 0; i < Config->ProjectCount; ++i) + { + SyncProject(&Config->Project[i], Neighbourhood, CollationBuffers, BespokeTemplate); + } + + SyncGlobalPagesWithInput(Neighbourhood, CollationBuffers); + + for(int i = 0; i < Assets.Count; ++i) + { + asset *This = GetPlaceInBook(&Assets.Asset, i); + UpdateAssetInDB(This); + } + DeleteStaleAssets(); + //PrintAssetsBlock(0); + + printf("\n╾─ Monitoring file system for %snew%s, %sedited%s and %sdeleted%s .hmml and asset files ─╼\n", + ColourStrings[EditTypes[EDIT_ADDITION].Colour], ColourStrings[CS_END], + ColourStrings[EditTypes[EDIT_REINSERTION].Colour], ColourStrings[CS_END], + ColourStrings[EditTypes[EDIT_DELETION].Colour], ColourStrings[CS_END]); +} + +void +RemoveAndFreeWatchHandles(watch_handles *W) +{ + for(int i = 0; i < W->Count; ++i) + { + watch_handle *This = &W->Handles[i]; + inotify_rm_watch(inotifyInstance, This->Descriptor); + FreeAndResetCount(This->Files, This->FileCount); + } + FreeAndResetCountAndCapacity(W->Handles, W->Count, W->Capacity); + FreeAndReinitialiseBook(&W->Paths); +} + +void +DiscardAllAndFreeConfig(void) +{ + FreeSignpostedFile(&DB.Metadata); // NOTE(matt): This seems fine + FreeFile(&DB.File); // NOTE(matt): This seems fine + FreeAssets(&Assets); // NOTE(matt): This seems fine + RemoveAndFreeWatchHandles(&WatchHandles); + CurrentProject = 0; // NOTE(matt): This is fine + FreeConfig(Config); // NOTE(matt): This is fine + Config = 0; +} + +watch_file * +GetWatchFileForEvent(struct inotify_event *Event) +{ + watch_file *Result = 0; + bool Update = FALSE; + for(int HandleIndex = 0; HandleIndex < WatchHandles.Count; ++HandleIndex) + { + watch_handle *ThisWatch = WatchHandles.Handles + HandleIndex; + if(Event->wd == ThisWatch->Descriptor) + { + if(Event->mask & IN_DELETE_SELF || Event->mask == (IN_CREATE | IN_ISDIR)) + { + Update = TRUE; + } + + if(StringsMatch(ThisWatch->TargetPath, ThisWatch->WatchedPath)) + { + for(int FileIndex = 0; FileIndex < ThisWatch->FileCount; ++FileIndex) + { + watch_file *ThisFile = ThisWatch->Files + FileIndex; + if(ThisFile->Extension != EXT_NULL) + { + if(ExtensionMatches(Wrap0(Event->name), ThisFile->Extension)) + { + Result = ThisFile; + break; + } + } + else if(StringsMatch(Wrap0(Event->name), ThisFile->Path)) + { + Result = ThisFile; + break; + } + } + if(Result) + { + break; + } + } + } + } + + if(Update) + { + UpdateWatchHandles(Event->wd); + } + return Result; +} + +void +ParseAndEitherPrintConfigOrInitAll(string ConfigPath, tokens_list *TokensList, neighbourhood *N, buffers *CollationBuffers, template *BespokeTemplate) +{ + Config = ParseConfig(ConfigPath, TokensList); + if(Config) + { + if(Mode & MODE_DRYRUN) + { + PrintConfig(Config, TRUE); + } + else + { + InitAll(N, CollationBuffers, BespokeTemplate); + } + } +} + +int +MonitorFilesystem(neighbourhood *N, buffers *CollationBuffers, template *BespokeTemplate, string ConfigPath, tokens_list *TokensList) +{ + buffer Events = {}; + if(ClaimBuffer(&Events, BID_INOTIFY_EVENTS, Kilobytes(4)) == RC_ARENA_FULL) { return RC_ARENA_FULL; }; Clear(Events.Location, Events.Size); struct inotify_event *Event; @@ -7950,69 +14859,96 @@ MonitorFilesystem(buffers *CollationBuffers, template *SearchTemplate, template // TODO(matt): Test this with longer update intervals, and combinations of events... #if DEBUG_EVENTS - int i = 0; + if(BytesRead > 0) + { + PrintWatchHandles(); + } + int DebugEventIndex = 0; #endif bool Deleted = FALSE; bool Inserted = FALSE; +#if DEBUG_LANDMARKS bool UpdatedAsset = FALSE; +#endif for(Events.Ptr = Events.Location; - Events.Ptr - Events.Location < BytesRead && Events.Ptr - Events.Location < Events.Size; + Events.Ptr - Events.Location < BytesRead; Events.Ptr += sizeof(struct inotify_event) + Event->len #if DEBUG_EVENTS - , ++i + , ++DebugEventIndex #endif ) { Event = (struct inotify_event *)Events.Ptr; #if DEBUG_EVENTS - PrintEvent(Event, i); -#endif + PrintEvent(Event, DebugEventIndex); //PrintWatchHandles(); +#endif - for(int WatchHandleIndex = 0; WatchHandleIndex < WatchHandles.Count; ++WatchHandleIndex) + watch_file *WatchFile = GetWatchFileForEvent(Event); + if(WatchFile) { - if(Event->wd == WatchHandles.Handle[WatchHandleIndex].Descriptor) + char *PeekPtr = Events.Ptr + sizeof(struct inotify_event) + Event->len; + struct inotify_event *Peek = (struct inotify_event *)PeekPtr; + while(PeekPtr - Events.Location < BytesRead && Event->wd == Peek->wd && StringsMatch(Wrap0i(Event->name, Event->len), Wrap0i(Peek->name, Peek->len))) { - switch(WatchHandles.Handle[WatchHandleIndex].Type) - { - case WT_HMML: - { - char *Ptr = Event->name; - Ptr += (StringLength(Event->name) - StringLength(".hmml")); - if(!(StringsDiffer(Ptr, ".hmml"))) - { - *Ptr = '\0'; +#if DEBUG_EVENTS + fprintf(stderr, "Squashing events:\n" + " "); + PrintEvent(Event, DebugEventIndex++); + fprintf(stderr, "\n" + " "); + PrintEvent(Peek, DebugEventIndex); + fprintf(stderr, "\n"); +#endif - neighbourhood Neighbourhood = { }; - InitNeighbourhood(&Neighbourhood); - ResetAssetLandmarks(); + Event = Peek; + Events.Ptr = PeekPtr; - if(Event->mask & IN_DELETE) - { - Deleted |= (DeleteEntry(&Neighbourhood, Event->name) == RC_SUCCESS); - } - else if(Event->mask & IN_CLOSE_WRITE || Event->mask & IN_MOVED_TO) - { - Inserted |= (InsertEntry(&Neighbourhood, CollationBuffers, PlayerTemplate, BespokeTemplate, Event->name, 0) == RC_SUCCESS); - } - } - } break; - case WT_ASSET: + PeekPtr = Events.Ptr + sizeof(struct inotify_event) + Event->len; + Peek = (struct inotify_event *)PeekPtr; + } + + switch(WatchFile->Type) + { + // TODO(matt): We're probably watching for too many events when the target directory exists + case WT_HMML: + { + SetCurrentProject(WatchFile->Project, N); + //PrintLineage(CurrentProject->Lineage, TRUE); + + string BaseFilename = GetBaseFilename(Wrap0(Event->name), WatchFile->Extension); + if(Event->mask & (IN_DELETE | IN_MOVED_FROM)) { - for(int i = 0; i < Assets.Count; ++i) - { - if(!StringsDiffer(Event->name, Assets.Asset[i].Filename + Assets.Asset[i].FilenameAt)) - { - UpdateAsset(i, 0); - UpdatedAsset = TRUE; - break; - } - } - } break; - } - break; + // TODO(matt): Why are we getting here after editing a .hmml file? We should just reinsert + Deleted |= (DeleteEntry(N, BaseFilename) == RC_SUCCESS); + } + else if(Event->mask & (IN_CLOSE_WRITE | IN_MOVED_TO)) + { + Inserted |= (InsertEntry(N, CollationBuffers, BespokeTemplate, BaseFilename, 0) == RC_SUCCESS); + } + } break; + case WT_ASSET: + { + // TODO(matt): Why are we getting here after editing a .hmml file? + //PrintAssetsBlock(0); + UpdateAsset(WatchFile->Asset, FALSE); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); +#if DEBUG_LANDMARKS + UpdatedAsset = TRUE; +#endif + } break; + case WT_CONFIG: + { + if(Config) + { + DiscardAllAndFreeConfig(); + PushWatchHandle(ConfigPath, EXT_NULL, WT_CONFIG, 0, 0); + } + + ParseAndEitherPrintConfigOrInitAll(ConfigPath, TokensList, N, CollationBuffers, BespokeTemplate); + } break; } } } @@ -8020,779 +14956,74 @@ MonitorFilesystem(buffers *CollationBuffers, template *SearchTemplate, template if(Deleted || Inserted) { UpdateDeferredAssetChecksums(); - GenerateSearchPage(CollationBuffers, SearchTemplate); + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + GenerateSearchPages(N, CollationBuffers); DeleteStaleAssets(); -#if DEBUG_LANDMARKS - VerifyLandmarks(); -#endif + UpdateNeighbourhoodPointers(N, &DB.Metadata.Signposts); + VerifyLandmarks(N); } +#if DEBUG_LANDMARKS if(UpdatedAsset) { -#if DEBUG_LANDMARKS - VerifyLandmarks(); -#endif + VerifyLandmarks(N); + PrintAssetsBlock(0); } +#endif DeclaimBuffer(&Events); return RC_SUCCESS; } -int -RemoveDirectory(char *Path) -{ - if((remove(Path) == -1)) - { - LogError(LOG_NOTICE, "Unable to remove directory %s: %s", Path, strerror(errno)); - fprintf(stderr, "%sUnable to remove directory%s %s: %s\n", ColourStrings[CS_WARNING], ColourStrings[CS_END], Path, strerror(errno)); - return RC_ERROR_DIRECTORY; - } - else - { - LogError(LOG_INFORMATIONAL, "Deleted %s", Path); - //fprintf(stderr, "%sDeleted%s %s\n", ColourStrings[CS_DELETION], ColourStrings[CS_END], Path); - return RC_SUCCESS; - } -} - -int -RemoveDirectoryRecursively(char *Path) -{ - if(RemoveDirectory(Path) == RC_ERROR_DIRECTORY) { return RC_ERROR_DIRECTORY; } - char *Ptr = Path + StringLength(Path) - 1; - while(Ptr > Path) - { - if(*Ptr == '/') - { - *Ptr = '\0'; - if(RemoveDirectory(Path) == RC_ERROR_DIRECTORY) { return RC_ERROR_DIRECTORY; } - } - --Ptr; - } - return RC_SUCCESS; -} - -int -UpgradeDB(int OriginalDBVersion) -{ - // NOTE(matt): For each new DB version, we must declare and initialise one instance of each preceding version, only cast the - // incoming DB to the type of the OriginalDBVersion, and move into the final case all operations on that incoming DB - - bool OnlyHeaderChanged = TRUE; - int OriginalHeaderSize = 0; - database1 DB1 = { }; - database2 DB2 = { }; - database3 DB3 = { }; - - switch(OriginalDBVersion) - { - case 1: - { - OriginalHeaderSize = sizeof(db_header1); - DB1.Header = *(db_header1 *)DB.Metadata.Buffer.Location; - - DB2.Header.DBVersion = CINERA_DB_VERSION; - DB2.Header.AppVersion = CINERA_APP_VERSION; - DB2.Header.HMMLVersion.Major = hmml_version.Major; - DB2.Header.HMMLVersion.Minor = hmml_version.Minor; - DB2.Header.HMMLVersion.Patch = hmml_version.Patch; - DB2.Header.EntryCount = DB1.Header.EntryCount; - - Clear(DB2.Header.SearchLocation, sizeof(DB2.Header.SearchLocation)); - Clear(DB2.Header.PlayerLocation, sizeof(DB2.Header.PlayerLocation)); - } - case 2: - { - if(OriginalDBVersion == 2) - { - OriginalHeaderSize = sizeof(db_header2); - DB2.Header = *(db_header2 *)DB.Metadata.Buffer.Location; - - DB.Header.InitialDBVersion = DB2.Header.DBVersion; - DB.Header.InitialAppVersion = DB2.Header.AppVersion; - DB.Header.InitialHMMLVersion.Major = DB2.Header.HMMLVersion.Major; - DB.Header.InitialHMMLVersion.Minor = DB2.Header.HMMLVersion.Minor; - DB.Header.InitialHMMLVersion.Patch = DB2.Header.HMMLVersion.Patch; - - } - - DB.EntriesHeader.Count = DB2.Header.EntryCount; - - ClearCopyStringNoFormat(DB.EntriesHeader.ProjectID, sizeof(DB.EntriesHeader.ProjectID), Config.ProjectID); - - Clear(DB.EntriesHeader.ProjectName, sizeof(DB.EntriesHeader.ProjectName)); - for(int ProjectIndex = 0; ProjectIndex < ArrayCount(ProjectInfo); ++ProjectIndex) - { - if(!StringsDiffer(ProjectInfo[ProjectIndex].ProjectID, Config.ProjectID)) - { - CopyString(DB.EntriesHeader.ProjectName, sizeof(DB.EntriesHeader.ProjectName), "%s", ProjectInfo[ProjectIndex].FullName); - break; - } - } - - ClearCopyStringNoFormat(DB.EntriesHeader.BaseURL, sizeof(DB.EntriesHeader.BaseURL), Config.BaseURL); - ClearCopyStringNoFormat(DB.EntriesHeader.SearchLocation, sizeof(DB.EntriesHeader.SearchLocation), DB2.Header.SearchLocation); - ClearCopyStringNoFormat(DB.EntriesHeader.PlayerLocation, sizeof(DB.EntriesHeader.PlayerLocation), DB2.Header.PlayerLocation); - ClearCopyStringNoFormat(DB.EntriesHeader.PlayerURLPrefix, sizeof(DB.EntriesHeader.PlayerURLPrefix), Config.PlayerURLPrefix); - - OnlyHeaderChanged = FALSE; - - if(!(DB.Metadata.Handle = fopen(DB.Metadata.Path, "w"))) { FreeBuffer(&DB.Metadata.Buffer); return RC_ERROR_FILE; } - fwrite(&DB.Header, sizeof(DB.Header), 1, DB.Metadata.Handle); - - DB.Metadata.Buffer.Ptr = DB.Metadata.Buffer.Location + OriginalHeaderSize; - DB.File.Buffer.Ptr += StringLength("---\n"); - - for(int EntryIndex = 0; EntryIndex < DB.EntriesHeader.Count; ++EntryIndex) - { - // NOTE(matt): We can use either db_entry1 or 2 here because they are the same - db_entry2 This = *(db_entry2 *)DB.Metadata.Buffer.Ptr; - - DB.Entry.LinkOffsets.PrevStart = 0; - DB.Entry.LinkOffsets.NextStart = 0; - DB.Entry.LinkOffsets.PrevEnd = 0; - DB.Entry.LinkOffsets.NextEnd = 0; - - DB.Entry.Size = This.Size; - - ClearCopyStringNoFormat(DB.Entry.BaseFilename, sizeof(DB.Entry.BaseFilename), This.BaseFilename); - - char *EntryStart = DB.File.Buffer.Ptr; - SeekBufferForString(&DB.File.Buffer, "title: \"", C_SEEK_FORWARDS, C_SEEK_AFTER); - Clear(DB.Entry.Title, sizeof(DB.Entry.Title)); - CopyStringNoFormatT(DB.Entry.Title, sizeof(DB.Entry.Title), DB.File.Buffer.Ptr, '\n'); - DB.Entry.Title[StringLength(DB.Entry.Title) - 1] = '\0'; - - fwrite(&DB.Entry, sizeof(DB.Entry), 1, DB.Metadata.Handle); - - DB.Metadata.Buffer.Ptr += sizeof(This); - EntryStart += This.Size; - DB.File.Buffer.Ptr = EntryStart; - } - - CycleFile(&DB.Metadata); - } - case 3: - { - OriginalHeaderSize = sizeof(db_header3); - DB3.Header = *(db_header3 *)DB.Metadata.Buffer.Location; - DB.Header.HexSignature = FOURCC("CNRA"); - - DB.Header.CurrentDBVersion = CINERA_DB_VERSION; - DB.Header.CurrentAppVersion = CINERA_APP_VERSION; - DB.Header.CurrentHMMLVersion.Major = hmml_version.Major; - DB.Header.CurrentHMMLVersion.Minor = hmml_version.Minor; - DB.Header.CurrentHMMLVersion.Patch = hmml_version.Patch; - - DB.Header.InitialDBVersion = DB3.Header.InitialDBVersion; - DB.Header.InitialAppVersion = DB3.Header.InitialAppVersion; - DB.Header.InitialHMMLVersion.Major = DB3.Header.InitialHMMLVersion.Major; - DB.Header.InitialHMMLVersion.Minor = DB3.Header.InitialHMMLVersion.Minor; - DB.Header.InitialHMMLVersion.Patch = DB3.Header.InitialHMMLVersion.Patch; - DB.Header.BlockCount = 0; - - // TODO(matt): Consider allowing zero NTRY or ASET blocks in a future version - DB.EntriesHeader.BlockID = FOURCC("NTRY"); - DB.EntriesHeader.Count = DB3.Header.EntryCount; - ClearCopyStringNoFormat(DB.EntriesHeader.ProjectID, sizeof(DB.EntriesHeader.ProjectID), DB3.Header.ProjectID); - ClearCopyStringNoFormat(DB.EntriesHeader.ProjectName, sizeof(DB.EntriesHeader.ProjectName), DB3.Header.ProjectName); - ClearCopyStringNoFormat(DB.EntriesHeader.BaseURL, sizeof(DB.EntriesHeader.BaseURL), DB3.Header.BaseURL); - ClearCopyStringNoFormat(DB.EntriesHeader.SearchLocation, sizeof(DB.EntriesHeader.SearchLocation), DB3.Header.SearchLocation); - ClearCopyStringNoFormat(DB.EntriesHeader.PlayerLocation, sizeof(DB.EntriesHeader.PlayerLocation), DB3.Header.PlayerLocation); - ClearCopyStringNoFormat(DB.EntriesHeader.PlayerURLPrefix, sizeof(DB.EntriesHeader.PlayerURLPrefix), DB3.Header.PlayerURLPrefix); - ++DB.Header.BlockCount; - - DB.AssetsHeader.BlockID = FOURCC("ASET"); - DB.AssetsHeader.Count = 0; - ClearCopyStringNoFormat(DB.AssetsHeader.RootDir, sizeof(DB.AssetsHeader.RootDir), Config.RootDir); - ClearCopyStringNoFormat(DB.AssetsHeader.RootURL, sizeof(DB.AssetsHeader.RootURL), Config.RootURL); - ClearCopyStringNoFormat(DB.AssetsHeader.CSSDir, sizeof(DB.AssetsHeader.CSSDir), Config.CSSDir); - ClearCopyStringNoFormat(DB.AssetsHeader.ImagesDir, sizeof(DB.AssetsHeader.ImagesDir), Config.ImagesDir); - ClearCopyStringNoFormat(DB.AssetsHeader.JSDir, sizeof(DB.AssetsHeader.JSDir), Config.JSDir); - ++DB.Header.BlockCount; - - OnlyHeaderChanged = FALSE; - - if(!(DB.Metadata.Handle = fopen(DB.Metadata.Path, "w"))) { FreeBuffer(&DB.Metadata.Buffer); return RC_ERROR_FILE; } - - fwrite(&DB.Header, sizeof(DB.Header), 1, DB.Metadata.Handle); - fwrite(&DB.EntriesHeader, sizeof(DB.EntriesHeader), 1, DB.Metadata.Handle); - fwrite(DB.Metadata.Buffer.Location + OriginalHeaderSize, - sizeof(DB.Entry), - DB.EntriesHeader.Count, - DB.Metadata.Handle); - - fwrite(&DB.AssetsHeader, sizeof(DB.AssetsHeader), 1, DB.Metadata.Handle); - - CycleFile(&DB.Metadata); - } - // TODO(matt); DBVersion 4 is the first that uses HexSignatures - // We should try and deprecate earlier versions and just enforce a clean rebuild for invalid DB files - // Perhaps do this either the next time we have to bump the version, or when we scrap Single Edition - } - - if(OnlyHeaderChanged) - { - if(!(DB.Metadata.Handle = fopen(DB.Metadata.Path, "w"))) { FreeBuffer(&DB.Metadata.Buffer); return RC_ERROR_FILE; } - fwrite(&DB.Header, sizeof(DB.Header), 1, DB.Metadata.Handle); - fwrite(DB.Metadata.Buffer.Location + OriginalHeaderSize, DB.Metadata.FileSize - OriginalHeaderSize, 1, DB.Metadata.Handle); - CycleFile(&DB.Metadata); - } - - fprintf(stderr, "\n%sUpgraded Cinera DB from %d to %d!%s\n\n", ColourStrings[CS_SUCCESS], OriginalDBVersion, DB.Header.CurrentDBVersion, ColourStrings[CS_END]); - return RC_SUCCESS; -} - -typedef struct -{ - bool Present; - char ID[32]; -} entry_presence_id; // Metadata, unless we actually want to bolster this? - void -RemoveChildDirectories(buffer FullPath, char *ParentDirectory) +Exit(void) { - char *Ptr = FullPath.Location + StringLength(ParentDirectory); - RemoveDirectory(FullPath.Location); - while(FullPath.Ptr > Ptr) - { - if(*FullPath.Ptr == '/') - { - *FullPath.Ptr = '\0'; - RemoveDirectory(FullPath.Location); - } - --FullPath.Ptr; - } -} - -int -DeleteDeadDBEntries(void) -{ - bool NewPlayerLocation = FALSE; - bool NewSearchLocation = FALSE; - if(StringsDiffer(DB.EntriesHeader.PlayerLocation, Config.PlayerLocation)) - { - buffer OldPlayerDirectory; - ClaimBuffer(&OldPlayerDirectory, "OldPlayerDirectory", - MAX_BASE_DIR_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_PLAYER_URL_PREFIX_LENGTH + MAX_BASE_FILENAME_LENGTH + 1); - ConstructDirectoryPath(&OldPlayerDirectory, PAGE_PLAYER, DB.EntriesHeader.PlayerLocation, 0); - buffer NewPlayerDirectory; - ClaimBuffer(&NewPlayerDirectory, "NewPlayerDirectory", - MAX_BASE_DIR_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1 + MAX_PLAYER_URL_PREFIX_LENGTH + MAX_BASE_FILENAME_LENGTH + 1); - ConstructDirectoryPath(&NewPlayerDirectory, PAGE_PLAYER, Config.PlayerLocation, 0); - printf("%sRelocating Player Page%s from %s to %s%s\n", - ColourStrings[CS_REINSERTION], DB.EntriesHeader.Count > 1 ? "s" : "", - OldPlayerDirectory.Location, NewPlayerDirectory.Location, ColourStrings[CS_END]); - DeclaimBuffer(&NewPlayerDirectory); - - for(int EntryIndex = 0; EntryIndex < DB.EntriesHeader.Count; ++EntryIndex) - { - db_entry This = *(db_entry *)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * EntryIndex); - ConstructDirectoryPath(&OldPlayerDirectory, PAGE_PLAYER, DB.EntriesHeader.PlayerLocation, This.BaseFilename); - DeletePlayerPageFromFilesystem(This.BaseFilename, DB.EntriesHeader.PlayerLocation, TRUE); - } - - ConstructDirectoryPath(&OldPlayerDirectory, PAGE_PLAYER, DB.EntriesHeader.PlayerLocation, 0); - - if(StringLength(DB.EntriesHeader.PlayerLocation) > 0) - { - RemoveChildDirectories(OldPlayerDirectory, Config.BaseDir); - } - - DeclaimBuffer(&OldPlayerDirectory); - - ClearCopyStringNoFormat(DB.EntriesHeader.PlayerLocation, sizeof(DB.EntriesHeader.PlayerLocation), Config.PlayerLocation); - *(db_header *)DB.Metadata.Buffer.Location = DB.Header; - NewPlayerLocation = TRUE; - } - if(StringsDiffer(DB.EntriesHeader.SearchLocation, Config.SearchLocation)) - { - buffer OldSearchDirectory; - ClaimBuffer(&OldSearchDirectory, "OldSearchDirectory", MAX_BASE_DIR_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1); - ConstructDirectoryPath(&OldSearchDirectory, PAGE_SEARCH, DB.EntriesHeader.SearchLocation, 0); - buffer NewSearchDirectory; - ClaimBuffer(&NewSearchDirectory, "NewSearchDirectory", MAX_BASE_DIR_LENGTH + 1 + MAX_RELATIVE_PAGE_LOCATION_LENGTH + 1); - ConstructDirectoryPath(&NewSearchDirectory, PAGE_SEARCH, Config.SearchLocation, 0); - printf("%sRelocating Search Page from %s to %s%s\n", - ColourStrings[CS_REINSERTION], OldSearchDirectory.Location, NewSearchDirectory.Location, ColourStrings[CS_END]); - DeclaimBuffer(&NewSearchDirectory); - - char SearchPagePath[2048] = { 0 }; - CopyString(SearchPagePath, sizeof(SearchPagePath), "%s/index.html", OldSearchDirectory.Location); - remove(SearchPagePath); - if(StringLength(DB.EntriesHeader.SearchLocation) > 0) - { - RemoveChildDirectories(OldSearchDirectory, Config.BaseDir); - } - DeclaimBuffer(&OldSearchDirectory); - - ClearCopyStringNoFormat(DB.EntriesHeader.SearchLocation, sizeof(DB.EntriesHeader.SearchLocation), Config.SearchLocation); - *(db_header *)DB.Metadata.Buffer.Location = DB.Header; - NewSearchLocation = TRUE; - } - - if(NewPlayerLocation || NewSearchLocation) - { - if(!(DB.Metadata.Handle = fopen(DB.Metadata.Path, "w"))) { FreeBuffer(&DB.Metadata.Buffer); return RC_ERROR_FILE; } - fwrite(DB.Metadata.Buffer.Location, DB.Metadata.FileSize, 1, DB.Metadata.Handle); - CycleFile(&DB.Metadata); - } - - entry_presence_id Entries[DB.EntriesHeader.Count]; - - for(int EntryIndex = 0; EntryIndex < DB.EntriesHeader.Count; ++EntryIndex) - { - db_entry This = *(db_entry *)(DB.Metadata.Buffer.Location + sizeof(DB.Header) + sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * EntryIndex); - CopyStringNoFormat(Entries[EntryIndex].ID, sizeof(Entries[EntryIndex].ID), This.BaseFilename); - Entries[EntryIndex].Present = FALSE; - } - - DIR *ProjectDirHandle; - if(!(ProjectDirHandle = opendir(Config.ProjectDir))) - { - LogError(LOG_ERROR, "Unable to scan project directory %s: %s", Config.ProjectDir, strerror(errno)); - fprintf(stderr, "Unable to scan project directory %s: %s\n", Config.ProjectDir, strerror(errno)); - return RC_ERROR_DIRECTORY; - } - - struct dirent *ProjectFiles; - - while((ProjectFiles = readdir(ProjectDirHandle))) - { - char *Ptr; - Ptr = ProjectFiles->d_name; - Ptr += (StringLength(ProjectFiles->d_name) - StringLength(".hmml")); - if(!(StringsDiffer(Ptr, ".hmml"))) - { - *Ptr = '\0'; - for(int i = 0; i < DB.EntriesHeader.Count; ++i) - { - if(!StringsDiffer(Entries[i].ID, ProjectFiles->d_name)) - { - Entries[i].Present = TRUE; - break; - } - } - } - } - closedir(ProjectDirHandle); - - bool Deleted = FALSE; - for(int i = 0; i < DB.EntriesHeader.Count; ++i) - { - if(Entries[i].Present == FALSE) - { - Deleted = TRUE; - neighbourhood Neighbourhood = { }; - InitNeighbourhood(&Neighbourhood); - ResetAssetLandmarks(); - DeleteEntry(&Neighbourhood, Entries[i].ID); - } - } - - return Deleted ? RC_SUCCESS : RC_NOOP; -} - -int -SyncDBWithInput(buffers *CollationBuffers, template *SearchTemplate, template *PlayerTemplate, template *BespokeTemplate) -{ - if(DB.Metadata.FileSize > 0 && Config.Mode & MODE_NOREVVEDRESOURCE) - { - DeleteStaleLandmarks(); - } - - bool Deleted = FALSE; - Deleted = (DB.Metadata.FileSize > 0 && DB.File.FileSize > 0 && DeleteDeadDBEntries() == RC_SUCCESS); - - DIR *ProjectDirHandle; - if(!(ProjectDirHandle = opendir(Config.ProjectDir))) - { - LogError(LOG_ERROR, "Unable to scan project directory %s: %s", Config.ProjectDir, strerror(errno)); - fprintf(stderr, "Unable to scan project directory %s: %s\n", Config.ProjectDir, strerror(errno)); - return RC_ERROR_DIRECTORY; - } - - struct dirent *ProjectFiles; - bool Inserted = FALSE; - - while((ProjectFiles = readdir(ProjectDirHandle))) - { - char *Ptr = ProjectFiles->d_name; - Ptr += (StringLength(ProjectFiles->d_name) - StringLength(".hmml")); - if(!(StringsDiffer(Ptr, ".hmml"))) - { - *Ptr = '\0'; - neighbourhood Neighbourhood = { }; - InitNeighbourhood(&Neighbourhood); - ResetAssetLandmarks(); - Inserted |= (InsertEntry(&Neighbourhood, CollationBuffers, PlayerTemplate, BespokeTemplate, ProjectFiles->d_name, 0) == RC_SUCCESS); - } - } - closedir(ProjectDirHandle); - - UpdateDeferredAssetChecksums(); - if(Deleted || Inserted) - { - GenerateSearchPage(CollationBuffers, SearchTemplate); - DeleteStaleAssets(); -#if DEBUG_LANDMARKS - VerifyLandmarks(); -#endif - } - return RC_SUCCESS; + Free(MemoryArena.Location); + _exit(0); } void -PrintVersions() +InitWatchHandles(uint32_t DefaultEventsMask, uint64_t DesiredPageSize) { - curl_version_info_data *CurlVersion = curl_version_info(CURLVERSION_NOW); - printf("Cinera: %d.%d.%d\n" - "Cinera DB: %d\n" - "hmmlib: %d.%d.%d\n" - "libcurl: %s\n", - CINERA_APP_VERSION.Major, CINERA_APP_VERSION.Minor, CINERA_APP_VERSION.Patch, - CINERA_DB_VERSION, - hmml_version.Major, hmml_version.Minor, hmml_version.Patch, - CurlVersion->version); -} - -int -InitDB(void) -{ - DB.Metadata.Buffer.ID = "DBMetadata"; - CopyString(DB.Metadata.Path, sizeof(DB.Metadata.Path), "%s/%s.metadata", Config.BaseDir, Config.ProjectID); - ReadFileIntoBuffer(&DB.Metadata, 0); // NOTE(matt): Could we actually catch errors (permissions?) here and bail? - - DB.File.Buffer.ID = "DBFile"; - CopyString(DB.File.Path, sizeof(DB.File.Path), "%s/%s.index", Config.BaseDir, Config.ProjectID); - ReadFileIntoBuffer(&DB.File, 0); // NOTE(matt): Could we actually catch errors (permissions?) here and bail? - - if(DB.Metadata.Buffer.Location) - { - // TODO(matt): Handle this gracefully (it'll be an invalid file) - Assert(DB.Metadata.FileSize >= sizeof(DB.Header)); - uint32_t OriginalDBVersion = 0; - uint32_t FirstInt = *(uint32_t *)DB.Metadata.Buffer.Location; - if(FirstInt != FOURCC("CNRA")) - { - // TODO(matt): More checking, somehow? Ideally this should be able to report "Invalid .metadata file" rather than - // trying to upgrade a file that isn't even valid - // TODO(matt): We should try and deprecate < CINERA_DB_VERSION 4 and enforce a clean rebuild for invalid DB files - // Perhaps do this either the next time we have to bump the version, or when we scrap Single Edition - OriginalDBVersion = FirstInt; - } - else - { - OriginalDBVersion = *(uint32_t *)(DB.Metadata.Buffer.Location + sizeof(DB.Header.HexSignature)); - } - - if(OriginalDBVersion < CINERA_DB_VERSION) - { - if(CINERA_DB_VERSION == 5) - { - fprintf(stderr, "\n%sHandle conversion from CINERA_DB_VERSION %d to %d!%s\n\n", ColourStrings[CS_ERROR], OriginalDBVersion, CINERA_DB_VERSION, ColourStrings[CS_END]); - exit(RC_ERROR_FATAL); - } - if(UpgradeDB(OriginalDBVersion) == RC_ERROR_FILE) { return RC_NOOP; } - } - else if(OriginalDBVersion > CINERA_DB_VERSION) - { - fprintf(stderr, "%sUnsupported DB Version (%d). Please upgrade Cinera%s\n", ColourStrings[CS_ERROR], OriginalDBVersion, ColourStrings[CS_END]); - exit(RC_ERROR_FATAL); - } - - DB.Header = *(db_header *)DB.Metadata.Buffer.Location; - DB.Header.CurrentAppVersion = CINERA_APP_VERSION; - DB.Header.CurrentHMMLVersion.Major = hmml_version.Major; - DB.Header.CurrentHMMLVersion.Minor = hmml_version.Minor; - DB.Header.CurrentHMMLVersion.Patch = hmml_version.Patch; - - DB.Metadata.Handle = fopen(DB.Metadata.Path, "w"); - fwrite(&DB.Header, sizeof(DB.Header), 1, DB.Metadata.Handle); - - DB.Metadata.Buffer.Ptr = DB.Metadata.Buffer.Location + sizeof(DB.Header); - - // NOTE(matt): In the future we may want to support multiple occurrences of a block type, at which point this code - // should continue to work - for(int BlockIndex = 0; BlockIndex < DB.Header.BlockCount; ++BlockIndex) - { - uint32_t BlockID = *(uint32_t *)DB.Metadata.Buffer.Ptr; - if(BlockID == FOURCC("NTRY")) - { - DB.EntriesHeader = *(db_header_entries *)DB.Metadata.Buffer.Ptr; - fwrite(DB.Metadata.Buffer.Ptr, - sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * DB.EntriesHeader.Count, - 1, - DB.Metadata.Handle); - DB.Metadata.Buffer.Ptr += sizeof(DB.EntriesHeader) + sizeof(DB.Entry) * DB.EntriesHeader.Count; - } - else if(BlockID == FOURCC("ASET")) - { - DB.AssetsHeader = *(db_header_assets *)DB.Metadata.Buffer.Ptr; - ClearCopyStringNoFormat(DB.AssetsHeader.RootDir, sizeof(DB.AssetsHeader.RootDir), Config.RootDir); - ClearCopyStringNoFormat(DB.AssetsHeader.RootURL, sizeof(DB.AssetsHeader.RootURL), Config.RootURL); - ClearCopyStringNoFormat(DB.AssetsHeader.CSSDir, sizeof(DB.AssetsHeader.CSSDir), Config.CSSDir); - ClearCopyStringNoFormat(DB.AssetsHeader.ImagesDir, sizeof(DB.AssetsHeader.ImagesDir), Config.ImagesDir); - ClearCopyStringNoFormat(DB.AssetsHeader.JSDir, sizeof(DB.AssetsHeader.JSDir), Config.JSDir); - fwrite(&DB.AssetsHeader, sizeof(DB.AssetsHeader), 1, DB.Metadata.Handle); - DB.Metadata.Buffer.Ptr += sizeof(DB.AssetsHeader); - for(int AssetIndex = 0; AssetIndex < DB.AssetsHeader.Count; ++AssetIndex) - { - DB.Asset = *(db_asset *)DB.Metadata.Buffer.Ptr; - fwrite(DB.Metadata.Buffer.Ptr, - sizeof(DB.Asset) + sizeof(DB.Landmark) * DB.Asset.LandmarkCount, - 1, - DB.Metadata.Handle); - DB.Metadata.Buffer.Ptr += sizeof(DB.Asset) + sizeof(DB.Landmark) * DB.Asset.LandmarkCount; - } - } - else - { - printf("%sMalformed metadata file%s: %s\n", ColourStrings[CS_ERROR], ColourStrings[CS_END], DB.Metadata.Path); - } - } - - CycleFile(&DB.Metadata); - } - else - { - // NOTE(matt): Initialising new db_header - DB.Header.HexSignature = FOURCC("CNRA"); - DB.Header.InitialDBVersion = DB.Header.CurrentDBVersion = CINERA_DB_VERSION; - DB.Header.InitialAppVersion = DB.Header.CurrentAppVersion = CINERA_APP_VERSION; - DB.Header.InitialHMMLVersion.Major = DB.Header.CurrentHMMLVersion.Major = hmml_version.Major; - DB.Header.InitialHMMLVersion.Minor = DB.Header.CurrentHMMLVersion.Minor = hmml_version.Minor; - DB.Header.InitialHMMLVersion.Patch = DB.Header.CurrentHMMLVersion.Patch = hmml_version.Patch; - DB.Header.BlockCount = 0; - - CopyStringNoFormat(DB.EntriesHeader.ProjectID, sizeof(DB.EntriesHeader.ProjectID), Config.ProjectID); - - for(int ProjectIndex = 0; ProjectIndex < ArrayCount(ProjectInfo); ++ProjectIndex) - { - if(!StringsDiffer(ProjectInfo[ProjectIndex].ProjectID, Config.ProjectID)) - { - CopyStringNoFormat(DB.EntriesHeader.ProjectName, sizeof(DB.EntriesHeader.ProjectName), ProjectInfo[ProjectIndex].FullName); - break; - } - } - - // TODO(matt): Consider allowing zero NTRY or ASET blocks in a future version - DB.EntriesHeader.BlockID = FOURCC("NTRY"); - DB.EntriesHeader.Count = 0; - CopyStringNoFormat(DB.EntriesHeader.BaseURL, sizeof(DB.EntriesHeader.BaseURL), Config.BaseURL); - CopyStringNoFormat(DB.EntriesHeader.SearchLocation, sizeof(DB.EntriesHeader.SearchLocation), Config.SearchLocation); - CopyStringNoFormat(DB.EntriesHeader.PlayerLocation, sizeof(DB.EntriesHeader.PlayerLocation), Config.PlayerLocation); - CopyStringNoFormat(DB.EntriesHeader.PlayerURLPrefix, sizeof(DB.EntriesHeader.PlayerURLPrefix), Config.PlayerURLPrefix); - ++DB.Header.BlockCount; - - DB.AssetsHeader.BlockID = FOURCC("ASET"); - DB.AssetsHeader.Count = 0; - CopyStringNoFormat(DB.AssetsHeader.RootDir, sizeof(DB.AssetsHeader.RootDir), Config.RootDir); - CopyStringNoFormat(DB.AssetsHeader.RootURL, sizeof(DB.AssetsHeader.RootURL), Config.RootURL); - CopyStringNoFormat(DB.AssetsHeader.CSSDir, sizeof(DB.AssetsHeader.CSSDir), Config.CSSDir); - CopyStringNoFormat(DB.AssetsHeader.ImagesDir, sizeof(DB.AssetsHeader.ImagesDir), Config.ImagesDir); - CopyStringNoFormat(DB.AssetsHeader.JSDir, sizeof(DB.AssetsHeader.JSDir), Config.JSDir); - ++DB.Header.BlockCount; - - DIR *OutputDirectoryHandle; - if(!(OutputDirectoryHandle = opendir(Config.BaseDir))) - { - if(MakeDir(Config.BaseDir) == RC_ERROR_DIRECTORY) - { - LogError(LOG_ERROR, "Unable to create directory %s: %s", Config.BaseDir, strerror(errno)); - fprintf(stderr, "Unable to create directory %s: %s\n", Config.BaseDir, strerror(errno)); - return RC_ERROR_DIRECTORY; - }; - } - closedir(OutputDirectoryHandle); - - DB.Metadata.Handle = fopen(DB.Metadata.Path, "w"); - fwrite(&DB.Header, sizeof(DB.Header), 1, DB.Metadata.Handle); - fwrite(&DB.EntriesHeader, sizeof(DB.EntriesHeader), 1, DB.Metadata.Handle); - fwrite(&DB.AssetsHeader, sizeof(DB.AssetsHeader), 1, DB.Metadata.Handle); - fclose(DB.Metadata.Handle); - ReadFileIntoBuffer(&DB.Metadata, 0); - - DB.File.Handle = fopen(DB.File.Path, "w"); - fprintf(DB.File.Handle, "---\n"); - fclose(DB.File.Handle); - ReadFileIntoBuffer(&DB.File, 0); - } - return RC_SUCCESS; -} - -void -SetCacheDirectory(config *DefaultConfig) -{ - int Flags = WRDE_NOCMD | WRDE_UNDEF | WRDE_APPEND; - wordexp_t Expansions = {}; - wordexp("$XDG_CACHE_HOME/cinera", &Expansions, Flags); - wordexp("$HOME/.cache/cinera", &Expansions, Flags); - if(Expansions.we_wordc > 0 ) { CopyString(DefaultConfig->CacheDir, sizeof(DefaultConfig->CacheDir), Expansions.we_wordv[0]); } - wordfree(&Expansions); -} - -void -InitMemoryArena(arena *Arena, int Size) -{ - Arena->Size = Size; - if(!(Arena->Location = calloc(1, Arena->Size))) - { - exit(RC_ERROR_MEMORY); - } - Arena->Ptr = Arena->Location; + WatchHandles.DefaultEventsMask = DefaultEventsMask; + InitBook(&WatchHandles.Paths, 1, DesiredPageSize, MBT_STRING); } int main(int ArgC, char **Args) { - // TODO(matt): Read all defaults from the config - config DefaultConfig = { - .RootDir = ".", - .RootURL = "", - .CSSDir = "", - .ImagesDir = "", - .JSDir = "", - .QueryString = "r", - .TemplatesDir = ".", - .TemplateSearchLocation = "", - .TemplatePlayerLocation = "", - .BaseDir = ".", - .BaseURL = "", - .SearchLocation = "", - .PlayerLocation = "", // Should default to the ProjectID - .Edition = EDITION_SINGLE, - .LogLevel = LOG_EMERGENCY, - .DefaultMedium = "programming", - .Mode = 0, - .OutLocation = "out.html", - .OutIntegratedLocation = "out_integrated.html", - .ProjectDir = ".", - .ProjectID = "", - .Theme = "", - .UpdateInterval = 4, - .PlayerURLPrefix = "" - }; - - SetCacheDirectory(&DefaultConfig); - - Config = DefaultConfig; - - if(ArgC < 2) - { - PrintUsage(Args[0], &DefaultConfig); - return RC_RIP; - } - + Assert(ArrayCount(BufferIDStrings) == BID_COUNT); + Assert(ArrayCount(TemplateTags) == TEMPLATE_TAG_COUNT); char CommandLineArg; - while((CommandLineArg = getopt(ArgC, Args, "1a:b:B:c:d:efghi:j:l:m:n:o:p:qQ:r:R:s:t:u:vwx:y:")) != -1) + char *ConfigPath = "$XDG_CONFIG_HOME/cinera/cinera.conf"; + while((CommandLineArg = getopt(ArgC, Args, "0c:ehv")) != -1) { switch(CommandLineArg) { - case '1': - Config.Mode |= MODE_SINGLETAB; - break; - case 'a': - Config.PlayerLocation = StripSurroundingSlashes(optarg); - break; - case 'b': - Config.BaseDir = StripTrailingSlash(optarg); - break; - case 'B': - Config.BaseURL = StripTrailingSlash(optarg); - break; + case '0': + Mode |= MODE_DRYRUN; break; case 'c': - Config.CSSDir = StripSurroundingSlashes(optarg); - break; - case 'd': - Config.ProjectDir = StripTrailingSlash(optarg); - break; + ConfigPath = optarg; break; case 'e': - Config.Mode |= MODE_EXAMINE; - break; - case 'f': - Config.Mode |= MODE_FORCEINTEGRATION; - break; - case 'g': - Config.Mode |= MODE_NOPRIVACY; - break; - case 'i': - Config.ImagesDir = StripSurroundingSlashes(optarg); - break; - case 'j': - Config.JSDir = StripSurroundingSlashes(optarg); - break; - case 'l': - // TODO(matt): Make this actually take a string, rather than requiring the LogLevel number - Config.LogLevel = StringToInt(optarg); - break; - case 'm': - Config.DefaultMedium = optarg; - break; - case 'n': - Config.SearchLocation = StripSurroundingSlashes(optarg); - break; - case 'o': - Config.OutLocation = optarg; - Config.OutIntegratedLocation = optarg; - break; - case 'p': - Config.ProjectID = optarg; - break; - case 'q': - Config.Mode |= MODE_ONESHOT; - break; - case 'Q': - Config.QueryString = optarg; - if(!StringsDiffer(Config.QueryString, "")) - { - Config.Mode |= MODE_NOREVVEDRESOURCE; - } - break; - case 'r': - Config.RootDir = StripTrailingSlash(optarg); - break; - case 'R': - Config.RootURL = StripTrailingSlash(optarg); - break; - case 's': - Config.Theme = optarg; - break; - case 't': - Config.TemplatesDir = StripTrailingSlash(optarg); - break; - case 'u': - Config.UpdateInterval = StringToInt(optarg); - break; + Mode |= MODE_EXAMINE; break; case 'v': PrintVersions(); - return RC_SUCCESS; - case 'w': - Config.Mode |= MODE_NOCACHE; - break; - case 'x': - Config.TemplateSearchLocation = optarg; - break; - case 'y': - Config.TemplatePlayerLocation = optarg; + _exit(RC_SUCCESS); break; case 'h': default: - PrintUsage(Args[0], &DefaultConfig); + PrintHelp(Args[0]); return RC_SUCCESS; } } - if(Config.Mode & MODE_EXAMINE) - { - // TODO(matt): Allow optionally passing a .metadata file as an argument? - ExamineDB(); - exit(RC_SUCCESS); - } - // NOTE(matt): Init MemoryArenas (they are global) InitMemoryArena(&MemoryArena, Megabytes(4)); + ConfigPath = ExpandPath(Wrap0(ConfigPath), 0); + + PrintVersions(); + #if DEBUG_MEM FILE *MemLog = fopen("/home/matt/cinera_mem", "a+"); fprintf(MemLog, " Allocated MemoryArena (%d)\n", MemoryArena.Size); @@ -8814,375 +15045,82 @@ main(int ArgC, char **Args) // Search buffers CollationBuffers = {}; - if(ClaimBuffer(&CollationBuffers.IncludesPlayer, "IncludesPlayer", Kilobytes(1)) == RC_ARENA_FULL) { goto RIP; }; - if(ClaimBuffer(&CollationBuffers.Menus, "Menus", Kilobytes(32)) == RC_ARENA_FULL) { goto RIP; }; - if(ClaimBuffer(&CollationBuffers.Player, "Player", Kilobytes(256)) == RC_ARENA_FULL) { goto RIP; }; - if(ClaimBuffer(&CollationBuffers.ScriptPlayer, "ScriptPlayer", Kilobytes(8)) == RC_ARENA_FULL) { goto RIP; }; + if(ClaimBuffer(&CollationBuffers.IncludesPlayer, BID_COLLATION_BUFFERS_INCLUDES_PLAYER, Kilobytes(2)) == RC_ARENA_FULL) { Exit(); }; + if(ClaimBuffer(&CollationBuffers.Player, BID_COLLATION_BUFFERS_PLAYER, Kilobytes(552)) == RC_ARENA_FULL) { Exit(); }; - if(ClaimBuffer(&CollationBuffers.IncludesSearch, "IncludesSearch", Kilobytes(1)) == RC_ARENA_FULL) { goto RIP; }; - if(ClaimBuffer(&CollationBuffers.SearchEntry, "Search", Kilobytes(32)) == RC_ARENA_FULL) { goto RIP; }; + if(ClaimBuffer(&CollationBuffers.IncludesSearch, BID_COLLATION_BUFFERS_INCLUDES_SEARCH, Kilobytes(2)) == RC_ARENA_FULL) { Exit(); }; + if(ClaimBuffer(&CollationBuffers.SearchEntry, BID_COLLATION_BUFFERS_SEARCH_ENTRY, Kilobytes(32)) == RC_ARENA_FULL) { Exit(); }; - bool HaveConfigErrors = FALSE; - if(StringsDiffer(Config.ProjectID, "")) + CollationBuffers.Search.ID = BID_COLLATION_BUFFERS_SEARCH; // NOTE(matt): Allocated by SearchToBuffer() + + tokens_list TokensList = {}; + template BespokeTemplate = {}; + neighbourhood Neighbourhood = {}; + + InitWatchHandles((IN_CREATE | IN_MOVED_FROM | IN_MOVED_TO | IN_CLOSE_WRITE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF), Kilobytes(4)); + + inotifyInstance = inotify_init1(IN_NONBLOCK); + + string ConfigPathL = Wrap0(ConfigPath); + PushWatchHandle(ConfigPathL, EXT_NULL, WT_CONFIG, 0, 0); + + Config = ParseConfig(ConfigPathL, &TokensList); + if(Config) { - if(StringLength(Config.ProjectID) > MAX_PROJECT_ID_LENGTH) + if(Mode & MODE_EXAMINE) { - fprintf(stderr, "%sProjectID \"%s\" is too long (%d/%d characters)%s\n", ColourStrings[CS_ERROR], Config.ProjectID, StringLength(Config.ProjectID), MAX_PROJECT_ID_LENGTH, ColourStrings[CS_END]); - HaveConfigErrors = TRUE; + // TODO(matt): Allow optionally passing a .db file as an argument? + ExamineDB(); + _exit(RC_SUCCESS); } - Config.Edition = EDITION_PROJECT; - bool KnownProject = FALSE; - for(int ProjectInfoIndex = 0; ProjectInfoIndex < ArrayCount(ProjectInfo); ++ProjectInfoIndex) + if(Mode & MODE_DRYRUN) { - if(!StringsDiffer(Config.ProjectID, ProjectInfo[ProjectInfoIndex].ProjectID)) - { - KnownProject = TRUE; - CopyStringNoFormat(CollationBuffers.ProjectID, sizeof(CollationBuffers.ProjectID), Config.ProjectID); - if(!StringsDiffer(Config.Theme, "")) - { - Config.Theme = Config.ProjectID; - } - CopyStringNoFormat(CollationBuffers.Theme, sizeof(CollationBuffers.Theme), Config.Theme); - if(StringsDiffer(ProjectInfo[ProjectInfoIndex].FullName, "")) - { - CopyStringNoFormat(CollationBuffers.ProjectName, sizeof(CollationBuffers.ProjectName), ProjectInfo[ProjectInfoIndex].FullName); - } - if(StringsDiffer(ProjectInfo[ProjectInfoIndex].Medium, "")) - { - Config.DefaultMedium = ProjectInfo[ProjectInfoIndex].Medium; - } - - if(StringsDiffer(ProjectInfo[ProjectInfoIndex].AltURLPrefix, "")) - { - if(StringLength(ProjectInfo[ProjectInfoIndex].AltURLPrefix) > MAX_PLAYER_URL_PREFIX_LENGTH) - { - fprintf(stderr, "%sPlayer URL Prefix \"%s\" is too long (%d/%d characters)%s\n", ColourStrings[CS_ERROR], ProjectInfo[ProjectInfoIndex].AltURLPrefix, StringLength(ProjectInfo[ProjectInfoIndex].AltURLPrefix), MAX_PLAYER_URL_PREFIX_LENGTH, ColourStrings[CS_END]); - HaveConfigErrors = TRUE; - } - else - { - Config.PlayerURLPrefix = ProjectInfo[ProjectInfoIndex].AltURLPrefix; - } - } - - if(StringLength(ProjectInfo[ProjectInfoIndex].FullName) > MAX_PROJECT_NAME_LENGTH) - { - fprintf(stderr, "%sProject Name \"%s\" is too long (%d/%d characters)%s\n", ColourStrings[CS_ERROR], ProjectInfo[ProjectInfoIndex].FullName, StringLength(ProjectInfo[ProjectInfoIndex].FullName), MAX_PROJECT_NAME_LENGTH, ColourStrings[CS_END]); - HaveConfigErrors = TRUE; - } - - break; - } + PrintConfig(Config, FALSE); } - if(!KnownProject) + else { - fprintf(stderr, "%sMissing Project Info for %s%s %s(-p)%s\n", ColourStrings[CS_ERROR], Config.ProjectID, ColourStrings[CS_END], ColourStrings[CS_COMMENT], ColourStrings[CS_END]); - HaveConfigErrors = TRUE; - } - } - - if(StringsDiffer(Config.BaseURL, "") && StringLength(Config.BaseURL) > MAX_BASE_URL_LENGTH) - { - fprintf(stderr, "%sBase URL \"%s\" is too long (%d/%d characters)%s\n", ColourStrings[CS_ERROR], Config.BaseURL, StringLength(Config.BaseURL), MAX_BASE_URL_LENGTH, ColourStrings[CS_END]); - HaveConfigErrors = TRUE; - } - - if(StringsDiffer(Config.SearchLocation, "") && StringLength(Config.SearchLocation) > MAX_RELATIVE_PAGE_LOCATION_LENGTH) - { - fprintf(stderr, "%sRelative Search Page Location \"%s\" is too long (%d/%d characters)%s\n", ColourStrings[CS_ERROR], Config.SearchLocation, StringLength(Config.SearchLocation), MAX_RELATIVE_PAGE_LOCATION_LENGTH, ColourStrings[CS_END]); - HaveConfigErrors = TRUE; - } - - if(StringsDiffer(Config.PlayerLocation, "") && StringLength(Config.PlayerLocation) > MAX_RELATIVE_PAGE_LOCATION_LENGTH) - { - fprintf(stderr, "%sRelative Player Page Location \"%s\" is too long (%d/%d characters)%s\n", ColourStrings[CS_ERROR], Config.PlayerLocation, StringLength(Config.PlayerLocation), MAX_RELATIVE_PAGE_LOCATION_LENGTH, ColourStrings[CS_END]); - HaveConfigErrors = TRUE; - } - - if(!MediumExists(Config.DefaultMedium)) - { - // TODO(matt): We'll want to stick around when we have multiple projects configured - HaveConfigErrors = TRUE; - } - - if(HaveConfigErrors) { exit(RC_RIP); } - - // NOTE(matt): Templating - // - // Config will contain paths of multiple templates - // App is running all the time, and picking up changes to the config as we go - // If we find a new template, we first of all Init and Validate it - // In our case here, we just want to straight up Init our three possible templates - // and Validate the Search and Player templates if their locations are set - - if(Config.Edition == EDITION_PROJECT) - { -#if DEBUG_MEM - FILE *MemLog = fopen("/home/matt/cinera_mem", "w+"); - fprintf(MemLog, "Entered Project Edition\n"); - fclose(MemLog); -#endif - - // TODO(matt): Also log these startup messages? - PrintVersions(); - printf( "\n" - "Universal\n" - " Cache Directory: %s(XDG_CACHE_HOME)%s\t%s\n" - " Update Interval: %s(-u)%s\t\t%d second%s\n" - "\n" - " Assets Root\n" - " Directory: %s(-r)%s\t\t\t%s\n" - " URL: %s(-R)%s\t\t\t%s\n" - " Paths relative to root\n" - " CSS: %s(-c)%s\t\t\t%s\n" - " Images: %s(-i)%s\t\t\t%s\n" - " JS: %s(-j)%s\t\t\t%s\n" - " Revved resources query string: %s(-Q)%s\t%s\n" - "\n" - "Project\n" - " ID: %s(-p)%s\t\t\t\t%s\n" - " Default Medium: %s(-m)%s\t\t%s\n" - " Style / Theme: %s(-s)%s\t\t\t%s\n" - "\n" - "Input Paths\n" - " Annotations Directory: %s(-d)%s\t\t%s\n" - " Templates Directory: %s(-t)%s\t\t%s\n" - " Search Template: %s(-x)%s\t\t%s\n" - " Player Template: %s(-y)%s\t\t%s\n" - "\n" - "Output Paths\n" - " Base\n" - " Directory: %s(-b)%s\t\t\t%s\n" - " URL: %s(-B)%s\t\t\t%s\n" - " Paths relative to base\n" - " Search Page: %s(-n)%s\t\t%s\n" - /* NOTE(matt): Here, I think, is where we'll split into sub-projects (...really?...) */ - " Player Page(s): %s(-a)%s\t\t%s\n" - " Player Page Prefix: %s(hardcoded)%s\t%s\n" - "\n" - "Modes\n" - " Single browser tab: %s(-1)%s\t\t%s\n" - " Force template integration: %s(-f)%s\t%s\n" - " Ignore video privacy status: %s(-g)%s\t%s\n" - " Quit after sync: %s(-q)%s\t\t%s\n" - " Force quote cache rebuild: %s(-w)%s\t%s\n" - "\n", - - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.CacheDir, - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.UpdateInterval, - Config.UpdateInterval == 1 ? "" : "s", - - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.RootDir, - ColourStrings[CS_COMMENT], ColourStrings[CS_END], StringsDiffer(Config.RootURL, "") ? Config.RootURL : "[empty]", - - ColourStrings[CS_COMMENT], ColourStrings[CS_END], StringsDiffer(Config.CSSDir, "") ? Config.CSSDir : "(same as root)", - ColourStrings[CS_COMMENT], ColourStrings[CS_END], StringsDiffer(Config.ImagesDir, "") ? Config.ImagesDir : "(same as root)", - ColourStrings[CS_COMMENT], ColourStrings[CS_END], StringsDiffer(Config.JSDir, "") ? Config.JSDir : "(same as root)", - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.Mode & MODE_NOREVVEDRESOURCE ? "[disabled]" : Config.QueryString, - - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.ProjectID, - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.DefaultMedium, - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.Theme, - - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.ProjectDir, - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.TemplatesDir, - ColourStrings[CS_COMMENT], ColourStrings[CS_END], StringsDiffer(Config.TemplateSearchLocation, "") ? Config.TemplateSearchLocation : "[none set]", - ColourStrings[CS_COMMENT], ColourStrings[CS_END], StringsDiffer(Config.TemplatePlayerLocation, "") ? Config.TemplatePlayerLocation : "[none set]", - - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.BaseDir, - ColourStrings[CS_COMMENT], ColourStrings[CS_END], StringsDiffer(Config.BaseURL, "") ? Config.BaseURL : "[empty]", - ColourStrings[CS_COMMENT], ColourStrings[CS_END], StringsDiffer(Config.SearchLocation, "") ? Config.SearchLocation : "(same as base)", - ColourStrings[CS_COMMENT], ColourStrings[CS_END], StringsDiffer(Config.PlayerLocation, "") ? Config.PlayerLocation : "(directly descended from base)", - ColourStrings[CS_COMMENT], ColourStrings[CS_END], StringsDiffer(Config.PlayerURLPrefix, "") ? Config.PlayerURLPrefix : Config.ProjectID, - - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.Mode & MODE_SINGLETAB ? "on" : "off", - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.Mode & MODE_FORCEINTEGRATION ? "on" : "off", - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.Mode & MODE_NOPRIVACY ? "on" : "off", - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.Mode & MODE_ONESHOT ? "on" : "off", - ColourStrings[CS_COMMENT], ColourStrings[CS_END], Config.Mode & MODE_NOCACHE ? "on" : "off"); - - if((StringsDiffer(Config.SearchLocation, "") || StringsDiffer(Config.PlayerLocation, "")) - && StringLength(Config.BaseURL) == 0) - { - printf("%sPlease set a Project Base URL (-B) so we can output the Search / Player pages to\n" - "locations other than the defaults%s\n", - ColourStrings[CS_ERROR], ColourStrings[CS_END]); - return(RC_SUCCESS); - } -#if 0 - for(int i = 0; i < Assets.Count; ++i) - { - printf("%08x - %s\n", Assets.Asset[i].Hash, Assets.Asset[i].Filename); - } -#endif - - InitDB(); - inotifyInstance = inotify_init1(IN_NONBLOCK); - - printf("┌╼ Hashing assets ╾┐\n"); - // NOTE(matt): This had to happen before PackTemplate() because those guys may need to do PushAsset() and we must - // ensure that the builtin assets get placed correctly - InitAssets(); - } - - printf("┌╼ Packing templates ╾┐\n"); - template SearchTemplate; - template PlayerTemplate; - template BespokeTemplate; - - if(StringsDiffer(Config.TemplatePlayerLocation, "")) - { - switch(PackTemplate(&PlayerTemplate, Config.TemplatePlayerLocation, TEMPLATE_PLAYER)) - { - case RC_INVALID_TEMPLATE: // Invalid template - case RC_ERROR_FILE: // Could not load template - case RC_ERROR_MEMORY: // Could not allocate memory for template - goto RIP; - case RC_SUCCESS: - break; - } - } - - if(Config.Edition == EDITION_PROJECT && StringsDiffer(Config.TemplateSearchLocation, "")) - { - switch(PackTemplate(&SearchTemplate, Config.TemplateSearchLocation, TEMPLATE_SEARCH)) - { - case RC_INVALID_TEMPLATE: // Invalid template - case RC_ERROR_MEMORY: // Could not allocate memory for template - case RC_ERROR_FILE: // Could not load template - goto RIP; - case RC_SUCCESS: - break; - } - } - - if(Config.Edition == EDITION_PROJECT) - { - printf("\n┌╼ Synchronising with Annotations Directory ╾┐\n"); - SyncDBWithInput(&CollationBuffers, &SearchTemplate, &PlayerTemplate, &BespokeTemplate); - if(Config.Mode & MODE_ONESHOT) - { - goto RIP; - } - - printf("\n┌╼ Monitoring file system for %snew%s, %sedited%s and %sdeleted%s .hmml and asset files ╾┐\n", - ColourStrings[EditTypes[EDIT_ADDITION].Colour], ColourStrings[CS_END], - ColourStrings[EditTypes[EDIT_REINSERTION].Colour], ColourStrings[CS_END], - ColourStrings[EditTypes[EDIT_DELETION].Colour], ColourStrings[CS_END]); - // NOTE(matt): Do we want to also watch IN_DELETE_SELF events? - PushHMMLWatchHandle(); - - while(MonitorFilesystem(&CollationBuffers, &SearchTemplate, &PlayerTemplate, &BespokeTemplate) != RC_ARENA_FULL) - { - // TODO(matt): Refetch the quotes and rebuild player pages if needed - // - // Every sixty mins, redownload the quotes and, I suppose, SyncDBWithInput(). But here we still don't even know - // who the speaker is. To know, we'll probably have to store all quoted speakers in the project's .metadata. Maybe - // postpone this for now, but we will certainly need this to happen - // - // The most ideal solution is possibly that we store quote numbers in the Metadata->Entry, listen for and handle a - // REST PUT request from insobot when a quote changes (unless we're supposed to poll insobot for them?), and rebuild - // the player page(s) accordingly. - // - if(!(Config.Mode & MODE_NOPRIVACY) && time(0) - LastPrivacyCheck > 60 * 60 * 4) - { - RecheckPrivacy(&CollationBuffers, &SearchTemplate, &PlayerTemplate, &BespokeTemplate); - } - sleep(Config.UpdateInterval); + InitAll(&Neighbourhood, &CollationBuffers, &BespokeTemplate); } } else { - // TODO(matt): Get rid of Single Edition once and for all, probably for v1.0.0 - if(optind == ArgC) + fprintf(stderr, "\n" + "Print config help? (Y/n)\n"); + if(getchar() != 'n') { - fprintf(stderr, "%s: requires at least one input .hmml file\n", Args[0]); - PrintUsage(Args[0], &DefaultConfig); - goto RIP; - } - - inotifyInstance = inotify_init1(IN_NONBLOCK); - - printf("┌╼ Hashing assets ╾┐\n"); - InitBuiltinAssets(); - -NextFile: - // TODO(matt): Just change the default output location so all these guys won't overwrite each other - for(int FileIndex = optind; FileIndex < ArgC; ++FileIndex) - { - bool HasBespokeTemplate = FALSE; - char *Ptr = Args[FileIndex]; - Ptr += (StringLength(Args[FileIndex]) - StringLength(".hmml")); - if(!(StringsDiffer(Ptr, ".hmml"))) + if(ArgC < 2) { - CopyString(Config.SingleHMMLFilePath, sizeof(Config.SingleHMMLFilePath), "%s", Args[FileIndex]); - switch(HMMLToBuffers(&CollationBuffers, &BespokeTemplate, Args[FileIndex], 0)) - { - // TODO(matt): Actually sort out the fatality of these cases - case RC_ERROR_FILE: - case RC_ERROR_FATAL: - goto RIP; - case RC_ERROR_HMML: - case RC_ERROR_MAX_REFS: - case RC_ERROR_QUOTE: - case RC_INVALID_REFERENCE: - if(FileIndex < (ArgC - 1)) { goto NextFile; } - else { goto RIP; } - case RC_SUCCESS: - break; - }; - - HasBespokeTemplate = BespokeTemplate.File.Buffer.Location != NULL; - - switch(BuffersToHTML(&CollationBuffers, - HasBespokeTemplate ? &BespokeTemplate : &PlayerTemplate, - 0, - PAGE_PLAYER, 0)) - { - // TODO(matt): Actually sort out the fatality of these cases - case RC_INVALID_TEMPLATE: - if(HasBespokeTemplate) { FreeTemplate(&BespokeTemplate); } - if(FileIndex < (ArgC - 1)) { goto NextFile; } - case RC_ERROR_MEMORY: - case RC_ERROR_FILE: - case RC_ARENA_FULL: - goto RIP; - case RC_SUCCESS: -#if 0 - fprintf(stdout, "%sWritten%s %s\n", HasBespokeTemplate ? Config.OutIntegratedLocation : Config.OutLocation); -#endif - if(HasBespokeTemplate) { FreeTemplate(&BespokeTemplate); } - break; - }; + PrintHelp(Args[0]); + } + else + { + PrintHelpConfig(); } } } - if(PlayerTemplate.File.Buffer.Location) + //PrintWatchHandles(); + while(MonitorFilesystem(&Neighbourhood, &CollationBuffers, &BespokeTemplate, ConfigPathL, &TokensList) != RC_ARENA_FULL) { - FreeTemplate(&PlayerTemplate); - } - if(Config.Edition == EDITION_PROJECT && SearchTemplate.File.Buffer.Location) - { - FreeTemplate(&SearchTemplate); + // TODO(matt): Refetch the quotes and rebuild player pages if needed + // + // Every sixty mins, redownload the quotes and, I suppose, SyncDBWithInput(). But here we still don't even know + // who the speaker is. To know, we'll probably have to store all quoted speakers in the project's .metadata. Maybe + // postpone this for now, but we will certainly need this to happen + // + // The most ideal solution is possibly that we store quote numbers in the Metadata->Entry, listen for and handle a + // REST PUT request from insobot when a quote changes (unless we're supposed to poll insobot for them?), and rebuild + // the player page(s) accordingly. + // + if(!(Mode & MODE_DRYRUN) && Config && Config->RespectingPrivacy && time(0) - LastPrivacyCheck > Config->PrivacyCheckInterval) + { + RecheckPrivacy(&Neighbourhood, &CollationBuffers, &BespokeTemplate); + } + sleep(GLOBAL_UPDATE_INTERVAL); } - DeclaimBuffer(&CollationBuffers.SearchEntry); - DeclaimBuffer(&CollationBuffers.IncludesSearch); - - DeclaimBuffer(&CollationBuffers.ScriptPlayer); - DeclaimBuffer(&CollationBuffers.Player); - DeclaimBuffer(&CollationBuffers.Menus); - DeclaimBuffer(&CollationBuffers.IncludesPlayer); -RIP: - free(MemoryArena.Location); - -#if DEBUG_MEM - MemLog = fopen("/home/matt/cinera_mem", "a+"); - fprintf(MemLog, " Freed MemoryArena\n"); - fclose(MemLog); - printf(" Freed MemoryArena\n"); -#endif - + DiscardAllAndFreeConfig(); + RemoveAndFreeWatchHandles(&WatchHandles); + Exit(); } diff --git a/cinera/cinera.css b/cinera/cinera.css index 0ff86b8..03804a8 100644 --- a/cinera/cinera.css +++ b/cinera/cinera.css @@ -1,12 +1,131 @@ +/* Generic Navigation */ + +/* Generic Navigation end */ + +/* Structure */ +/* Horizontal */ +ul.cineraNavHorizontal, +ul.cineraNavHorizontal ul { + display: flex; + flex-grow: 1; + flex-flow: row wrap; + border: 0; + margin: 0; + padding: 0; +} + +ul.cineraNavHorizontal li { + display: flex; + flex-direction: column; + flex-grow: 1; +} + +ul.cineraNavHorizontal { + border-right: 1px solid; +} + +ul.cineraNavHorizontal a { + flex-grow: 1; + padding: 8px; + + display: flex; + justify-content: center; + align-items: center; + + border-left: 1px solid; + border-bottom: 1px solid; +} +/* Horizontal end */ + +/* Dropdown */ +nav.cineraNavDropdown { + display: flex; + flex-direction: column; +} + +nav.cineraNavDropdown .cineraNavTitle { + text-align: center; + padding: 8px 0; + user-select: none; + -moz-user-select: none; + -webkit-user-select: none; +} + +.cineraPositioner { + position: relative; +} + +nav.cineraNavDropdown ul.cineraNavHorizontal { + display: none; + width: 100%; + z-index: 8; + position: absolute; +} + +nav.cineraNavDropdown ul.cineraNavHorizontal.visible { + display: flex; +} +/* Dropdown end */ + +/* Structure end */ + +/* Typography */ +ul.cineraNavHorizontal a, +ul.cineraNavPlain a { + text-decoration: none; +} + +/* Plain */ +ul.cineraNavPlain li.current > a { + font-weight: bold; +} +/* Plain end */ +/* Typography end */ + + /* Index */ #cineraIndexControl { display: flex; margin: 16px auto; max-width: 1024px; + align-items: center; padding: 8px; } +.cineraFilterProject .cineraText, +.cineraProjectTitle +{ + display: flex; +} + +.cineraIndexFilter { + padding: 10px; +} + +.cineraIndexFilter .filter_container +{ + display: none; + position: absolute; + z-index: 64; +} + +.cineraFilterProject, +.cineraIndexProject > .cineraIndexProject { + padding-left: 1em; +} + +.cineraIndexProject { + margin-top: 4px; +} + +.cineraIndexProject .cineraProjectTitle, +.cineraFilterProject +{ + font-weight: bold; + font-size: 12px; +} + .cineraQueryContainer { flex-grow: 1; padding-left: 16px; @@ -43,22 +162,30 @@ display: block; } - #cineraResults, #cineraIndex { margin: 0 auto; max-width: 800px; } +#cineraResults .projectContainer { + display: flex; + flex-flow: column; +} + #cineraIndexControl #cineraIndexSort { padding: 4px; +} + +#cineraIndexControl #cineraIndexSort, +#cineraIndexControl .cineraIndexFilter .filter_container { cursor: pointer; user-select: none; -moz-user-select: none; -webkit-user-select: none; } -#cineraIndex #cineraIndexEntries { +#cineraIndex .cineraIndexEntries { display: flex; flex-flow: column; width: 100%; @@ -79,13 +206,13 @@ width: 200px; } -#cineraIndexEntries div a { +.cineraIndexEntries div a { display: block; padding: 5px; text-decoration: none; } -#cineraIndexEntries div a::before { +.cineraIndexEntries div a::before { content: "\2713"; margin: 0 4px; } @@ -141,14 +268,14 @@ max-width: 100%; } - #cineraIndexEntries div a { + .cineraIndexEntries div a { font-size: 80%; } } /* Player */ -/* Structure */ +/* Player / Structure */ .cineraMenus, .cineraMenus > .menu > .refs .ref, @@ -282,6 +409,12 @@ display: block; } +#cineraIndexControl .cineraIndexFilter .filter_container +{ + border: 1px solid; + border-top: none; +} + .cineraMenus > .menu .quotes_container, .cineraMenus > .menu .references_container, .cineraMenus > .menu .filter_container, @@ -343,11 +476,6 @@ padding: 16px; } -.cineraMenus > .menu > .credits_container .credit .support .support_icon { - height: 16px; - width: 16px; -} - .cineraMenus > .menu > .refs .ref:last-child, .cineraMenus > .menu > .credits_container .credit:last-child { border: none; diff --git a/cinera/cinera_config.c b/cinera/cinera_config.c new file mode 100644 index 0000000..ddfffd4 --- /dev/null +++ b/cinera/cinera_config.c @@ -0,0 +1,4682 @@ +#if 0 +ctime -begin ${0%.*}.ctm +gcc -g -Wall -fsanitize=address -std=c99 $0 -o ${0%.*} +#clang -g -fsanitize=address -std=c99 $0 -o ${0%.*} +ctime -end ${0%.*}.ctm +exit +#endif + +// config +// +#include +#include +#include +#include +#include + +#define ArgCountPointer(...) (sizeof((void *[]){__VA_ARGS__})/sizeof(void *)) + +#define DigitsInInt(I) DigitsInInt_(I, sizeof(*I)) +uint8_t +DigitsInInt_(void *Int, uint8_t WidthInBytes) +{ + uint8_t Result = 0; + switch(WidthInBytes) + { + case 1: + { + int8_t This = *(int8_t *)Int; + if(This <= 0) { This = ~This; ++Result; if(This == 0) { ++Result; return Result; } } + for(; This > 0; This /= 10) { ++Result; } + } break; + case 2: + { + int16_t This = *(int16_t *)Int; + if(This <= 0) { This = ~This; ++Result; if(This == 0) { ++Result; return Result; } } + for(; This > 0; This /= 10) { ++Result; } + } break; + case 4: + { + int32_t This = *(int32_t *)Int; + if(This <= 0) { This = ~This; ++Result; if(This == 0) { ++Result; return Result; } } + for(; This > 0; This /= 10) { ++Result; } + } break; + case 8: + { + int64_t This = *(int64_t *)Int; + if(This <= 0) { This = ~This; ++Result; if(This == 0) { ++Result; return Result; } } + for(; This > 0; This /= 10) { ++Result; } + } break; + default: + { + Assert(0); + } + } + return Result; +} + +#define DigitsInUint(I) DigitsInUint_(I, sizeof(*I)) +uint8_t +DigitsInUint_(void *Uint, uint8_t WidthInBytes) +{ + uint32_t Result = 0; + switch(WidthInBytes) + { + case 1: + { + uint8_t This = *(uint8_t *)Uint; + if(This == 0) { ++Result; return Result; } + for(; This > 0; This /= 10) { ++Result; } + } break; + case 2: + { + uint16_t This = *(uint16_t *)Uint; + if(This == 0) { ++Result; return Result; } + for(; This > 0; This /= 10) { ++Result; } + } break; + case 4: + { + uint32_t This = *(uint32_t *)Uint; + if(This == 0) { ++Result; return Result; } + for(; This > 0; This /= 10) { ++Result; } + } break; + case 8: + { + uint64_t This = *(uint64_t *)Uint; + if(This == 0) { ++Result; return Result; } + for(; This > 0; This /= 10) { ++Result; } + } break; + default: + { + Assert(0); + } + } + return Result; +} + + +#define MakeString(Format, ...) MakeString_(__FILE__, __LINE__, Format, ArgCountPointer(__VA_ARGS__), __VA_ARGS__) +string +MakeString_(char *Filename, int LineNumber, char *Format, int ArgCount, ...) +{ + // NOTE(matt): Be sure to free built strings when you're finished with them + // + // Usage: + // + // Similar to printf and friends, MakeString() takes a format string as its first argument. + // While printf has many format specifiers - e.g. %s, %d, %c - MakeString() has two: s, l + // s: Null-terminated string, i.e. char * + // l: Length-string, i.e. string * + // We allocate memory, null-terminate and return the char * + // + // Examples: + // 1. char *BuiltString = MakeString("ss", "First", "Second"); + // 2. char *Input = "First"; + // char *BuiltString = MakeString("ss", Input, "Second"); + // 3. string Input = { .Base = "Second", .Length = StringLength("Second") }; + // char *BuiltString = MakeString("sl", "First", &Input); + + int FormatLength = StringLength(Format); + if(FormatLength != ArgCount) + { + Colourise(CS_FAILURE); fprintf(stderr, "%s:%d MakeString()", Filename, LineNumber); Colourise(CS_END); + fprintf(stderr, + "\n" + " MakeString() has been passed a format string containing "); + + Colourise(CS_BLUE_BOLD); fprintf(stderr, "%d specifier%s", FormatLength, FormatLength == 1 ? "" : "s"); Colourise(CS_END); + + fprintf(stderr, + ",\n" + " and "); + + Colourise(CS_BLUE_BOLD); fprintf(stderr, "%d argument%s", ArgCount, ArgCount == 1 ? "" : "s"); Colourise(CS_END); + + fprintf(stderr, ", but these numbers should be equal.\n"); + __asm__("int3"); + } + + // TODO(matt): Type-checking, if possible? + va_list Args; + va_start(Args, ArgCount); + string Result = {}; + for(int i = 0; i < FormatLength; ++i) + { + switch(Format[i]) + { + case 's': + { + char *Next = va_arg(Args, char *); + int NextLength = StringLength(Next); + Result.Base = realloc(Result.Base, Result.Length + NextLength); + char *Ptr = Result.Base + Result.Length; + while(*Next) { *Ptr++ = *Next++; } + Result.Length += NextLength; + } break; + case 'l': + { + string *Next = va_arg(Args, string *); + Result.Base = realloc(Result.Base, Result.Length + Next->Length); + char *Ptr = Result.Base + Result.Length; + int j; + for(j = 0; j < Next->Length && Next->Base[j]; ++j) + { + *Ptr++ = Next->Base[j]; + } + Result.Length += j; + } break; + default: + { + Colourise(CS_FAILURE); fprintf(stderr, "%s:%d", Filename, LineNumber); Colourise(CS_END); + fprintf(stderr, "\n" + " MakeString() has been passed an unsupported format specifier: %c\n", Format[i]); + __asm__("int3"); + } break; + } + } + + va_end(Args); + return Result; +} + +#define MakeString0(Format, ...) MakeString0_(__FILE__, __LINE__, Format, ArgCountPointer(__VA_ARGS__), __VA_ARGS__) +char * +MakeString0_(char *Filename, int LineNumber, char *Format, int ArgCount, ...) +{ + // NOTE(matt): Be sure to free built strings when you're finished with them + // + // Usage: + // + // Similar to printf and friends, MakeString() takes a format string as its first argument. + // While printf has many format specifiers - e.g. %s, %d, %c - MakeString() has two: s, l + // s: Null-terminated string, i.e. char * + // l: Length-string, i.e. string * + // We allocate memory, null-terminate and return the char * + // + // Examples: + // 1. char *BuiltString = MakeString("ss", "First", "Second"); + // 2. char *Input = "First"; + // char *BuiltString = MakeString("ss", Input, "Second"); + // 3. string Input = { .Base = "Second", .Length = StringLength("Second") }; + // char *BuiltString = MakeString("sl", "First", &Input); + + int FormatLength = StringLength(Format); + if(FormatLength != ArgCount) + { + Colourise(CS_FAILURE); fprintf(stderr, "%s:%d MakeString0()", Filename, LineNumber); Colourise(CS_END); + fprintf(stderr, + "\n" + " MakeString0() has been passed a format string containing "); + + Colourise(CS_BLUE_BOLD); fprintf(stderr, "%d specifier%s", FormatLength, FormatLength == 1 ? "" : "s"); Colourise(CS_END); + + fprintf(stderr, + ",\n" + " and "); + + Colourise(CS_BLUE_BOLD); fprintf(stderr, "%d argument%s", ArgCount, ArgCount == 1 ? "" : "s"); Colourise(CS_END); + + fprintf(stderr, ", but these numbers should be equal.\n"); + __asm__("int3"); + } + + // TODO(matt): Type-checking, if possible? + va_list Args; + va_start(Args, ArgCount); + char *Result = 0; + int RequiredSpace = 0; + for(int i = 0; i < FormatLength; ++i) + { + switch(Format[i]) + { + case 's': + { + char *Next = va_arg(Args, char *); + int NextLength = StringLength(Next); + Result = realloc(Result, RequiredSpace + NextLength); + char *Ptr = Result + RequiredSpace; + while(*Next) { *Ptr++ = *Next++; } + RequiredSpace += NextLength; + } break; + case 'l': + { + string *Next = va_arg(Args, string *); + Result = realloc(Result, RequiredSpace + Next->Length); + char *Ptr = Result + RequiredSpace; + int j; + for(j = 0; j < Next->Length && Next->Base[j]; ++j) + { + *Ptr++ = Next->Base[j]; + } + RequiredSpace += j; + } break; + case 'd': + { + int32_t Next = *va_arg(Args, int32_t *); + int NextLength = DigitsInInt(&Next); + Result = realloc(Result, RequiredSpace + NextLength); + char *Ptr = Result + RequiredSpace; + sprintf(Ptr, "%d", Next); + RequiredSpace += NextLength; + } break; + case 'u': + { + uint32_t Next = *va_arg(Args, uint32_t *); + int NextLength = DigitsInUint(&Next); + Result = realloc(Result, RequiredSpace + NextLength); + char *Ptr = Result + RequiredSpace; + sprintf(Ptr, "%u", Next); + RequiredSpace += NextLength; + } break; + default: + { + Colourise(CS_FAILURE); fprintf(stderr, "%s:%d", Filename, LineNumber); Colourise(CS_END); + fprintf(stderr, "\n" + " MakeString0() has been passed an unsupported format specifier: %c\n", Format[i]); + __asm__("int3"); + } break; + } + } + + va_end(Args); + ++RequiredSpace; + Result = realloc(Result, RequiredSpace); + Result[RequiredSpace - 1] = '\0'; + return Result; +} + +file +InitFile(string *Directory, string *Filename, extension_id Extension) +{ + file Result = {}; + if(Directory) + { + ExtendString0(&Result.Path, *Directory); + ExtendString0(&Result.Path, Wrap0("/")); + } + + ExtendString0(&Result.Path, *Filename); + + if(Extension != EXT_NULL) + { + ExtendString0(&Result.Path, ExtensionStrings[Extension]); + } + return Result; +} + +void +ReadFileIntoBuffer(file *F) +{ + if(F->Path) + { + if((F->Handle = fopen(F->Path, "r"))) + { + fseek(F->Handle, 0, SEEK_END); + F->Buffer.Size = ftell(F->Handle); + F->Buffer.Location = malloc(F->Buffer.Size); + F->Buffer.Ptr = F->Buffer.Location; + fseek(F->Handle, 0, SEEK_SET); + fread(F->Buffer.Location, F->Buffer.Size, 1, F->Handle); + fclose(F->Handle); + F->Handle = 0; + } + else + { + perror(F->Path); + } + } +} + +void +PushToken(tokens *Set, token *Token) +{ + // NOTE(matt): We don't bother to push comments or newlines on to the token list + //PrintFunctionName("PushToken()"); + if(!(Token->Type == TOKEN_NULL || Token->Type == TOKEN_COMMENT || Token->Type == TOKEN_NEWLINE)) + { + Set->Token = Fit(Set->Token, sizeof(*Set->Token), Set->Count, 16, TRUE); + Set->Token[Set->Count].Type = Token->Type; + Set->Token[Set->Count].LineNumber = Set->CurrentLine; + + if(Token->Type == TOKEN_NUMBER) + { + Set->Token[Set->Count].int64_t = StringToInt(Token->Content); + } + else + { + Set->Token[Set->Count].Content = Token->Content; + } + ++Set->Count; + } +} + +void +PrintToken(token *T, uint64_t Index) +{ + fprintf(stderr, "[%lu, %s] ", Index, TokenTypeStrings[T->Type]); + if(T->Type == TOKEN_NUMBER) + { + Colourise(CS_BLUE_BOLD); + fprintf(stderr, "%li", T->int64_t); + Colourise(CS_END); + + } + else if(T->Type == TOKEN_COMMENT) + { + PrintStringC(CS_BLACK_BOLD, T->Content); + } + else + { + PrintString(T->Content); + } + fprintf(stderr, "\n"); +} + +void +PrintTokens(tokens *T) +{ + //PrintFunctionName("PrintTokens()"); + for(int i = 0; i < T->Count; ++i) + { + PrintToken(&T->Token[i], i); + } +} + +tokens * +PushTokens(tokens_list *S) +{ + S->Tokens = Fit(S->Tokens, sizeof(*S->Tokens), S->Count, 4, TRUE); + S->Tokens[S->Count].CurrentLine = 1; + ++S->Count; + return &S->Tokens[S->Count - 1]; +} + +void +FreeTokensList(tokens_list *T) +{ + for(int i = 0; i < T->Count; ++i) + { + T->Tokens[i].CurrentIndex = 0; + T->Tokens[i].CurrentLine = 0; + FreeAndResetCount(T->Tokens[i].Token, T->Tokens[i].Count); + FreeFile(&T->Tokens[i].File); + } + T->Count = 0; +} + +void +SkipWhitespace(tokens *T, buffer *B) +{ + while(B->Ptr - B->Location < B->Size && + (*B->Ptr == ' ' || + //*B->Ptr == '\n' || + *B->Ptr == '\t')) + { + ++B->Ptr; + } +} + +void +Advance(buffer *B, uint64_t Distance) +{ + if(B->Ptr + Distance - B->Location <= B->Size) + { + B->Ptr += Distance; + } +} + +string +GetUTF8Character(char *Ptr, uint64_t BytesRemaining) +{ + string Result = {}; + if((uint8_t)(Ptr[0] & 0b11111000) == 0b11110000) + { + ++Result.Length; + if(BytesRemaining >= 3) + { + if((uint8_t)(Ptr[1] & 0b11000000) == 0b10000000 && + (uint8_t)(Ptr[2] & 0b11000000) == 0b10000000 && + (uint8_t)(Ptr[3] & 0b11000000) == 0b10000000) + { + Result.Length = 4; + Result.Base = Ptr; + return Result; + } + } + } + else if((uint8_t)(Ptr[0] & 0b11110000) == 0b11100000) + { + ++Result.Length; + if(BytesRemaining >= 2) + { + if((uint8_t)(Ptr[1] & 0b11000000) == 0b10000000 && + (uint8_t)(Ptr[2] & 0b11000000) == 0b10000000) + { + Result.Length = 3; + Result.Base = Ptr; + return Result; + } + } + } + else if((uint8_t)(Ptr[0] & 0b11100000) == 0b11000000) + { + ++Result.Length; + if(BytesRemaining >= 1) + { + if((uint8_t)(Ptr[1] & 0b11000000) == 0b10000000) + { + Result.Length = 2; + Result.Base = Ptr; + return Result; + } + } + } + else if((uint8_t)(Ptr[0] & 0b10000000) == 0b00000000) + { + ++Result.Length; + Result.Base = Ptr; + return Result; + } + + for(; BytesRemaining >= Result.Length; ++Result.Length) + { + if(((uint8_t)(Ptr[Result.Length] & 0b11111000) == 0b11110000) || + ((uint8_t)(Ptr[Result.Length] & 0b11110000) == 0b11100000) || + ((uint8_t)(Ptr[Result.Length] & 0b11100000) == 0b11000000) || + ((uint8_t)(Ptr[Result.Length] & 0b10000000) == 0b00000000)) + { + break; + } + } + return Result; +} + +typedef struct +{ + string ID; + string Icon; + string IconNormal; + string IconFocused; + icon_type IconType; + uint64_t IconVariants; + asset *IconAsset; + string URL; +} support; + +typedef struct +{ + string ID; + string Icon; + string IconNormal; + string IconDisabled; + string IconFocused; + icon_type IconType; + uint64_t IconVariants; + asset *IconAsset; + + string Name; + bool Hidden; +} medium; + +typedef struct +{ + string ID; + string Name; + string Homepage; + support *Support; + uint8_t SupportCount; +} person; + +typedef struct project +{ + string ID; + string Lineage; + + string Title; + string HTMLTitle; + string Unit; + string HMMLDir; + string TemplatesDir; + string SearchTemplatePath; + string PlayerTemplatePath; + + string BaseDir; + string BaseURL; + string SearchLocation; + string PlayerLocation; + string Theme; + + string Art; + uint64_t ArtVariants; + asset *ArtAsset; + + string Icon; + string IconNormal; + string IconDisabled; + string IconFocused; + icon_type IconType; + uint64_t IconVariants; + asset *IconAsset; + + string StreamUsername; + string VODPlatform; + + bool SingleBrowserTab; + bool IgnorePrivacy; + + person *Owner; + person **Indexer; + person **Guest; + person **CoHost; + + medium *Medium; // Multiple + medium *DefaultMedium; // Pointer to Medium + + struct project *Child; // Multiple + struct project *Parent; // Pointer to single parent + + genre Genre; + numbering_scheme NumberingScheme; + + uint8_t IndexerCount; + uint8_t GuestCount; + uint8_t CoHostCount; + + uint8_t MediumCount; + + uint8_t ChildCount; + + db_project_index Index; + template PlayerTemplate; + template SearchTemplate; + + buffer NavDropdownPre; + buffer NavHorizontalPre; + buffer NavPlainPre; + buffer NavGeneric; + buffer NavDropdownPost; +} project; + +typedef struct +{ + string DatabaseLocation; + string CacheDir; + + string GlobalTemplatesDir; + string GlobalSearchTemplatePath; + string GlobalSearchDir; + string GlobalSearchURL; + string GlobalTheme; + template SearchTemplate; + + buffer NavDropdownPre; + buffer NavHorizontalPre; + buffer NavPlainPre; + buffer NavGeneric; + buffer NavDropdownPost; + + string AssetsRootDir; + string AssetsRootURL; + string CSSDir; + string ImagesDir; + string JSDir; + string QueryString; + + bool RespectingPrivacy; + time_t PrivacyCheckInterval; + uint8_t LogLevel; + + person *Person; + project *Project; + uint8_t ProjectCount; + uint8_t PersonCount; + memory_book ResolvedVariables; +} config; + +char *ExpandPath(string Path, string *RelativeToFile); // NOTE(matt): Forward declared. Consider reorganising the code? +void PushWatchHandle(string Path, extension_id Extension, watch_type Type, project *Project, asset *Asset); // NOTE(matt): Forward declared. Consider reorganising the code? + +tokens * +Tokenise(tokens_list *TokensList, string Path) +{ + tokens *Result = 0; + char *Path0 = MakeString0("l", &Path); + FILE *Handle = 0; + PushWatchHandle(Path, EXT_NULL, WT_CONFIG, 0, 0); + if((Handle = fopen(Path0, "r"))) + { + fclose(Handle); + Result = PushTokens(TokensList); + Result->File = InitFile(0, &Path, EXT_NULL); + ReadFileIntoBuffer(&Result->File); + buffer *B = &Result->File.Buffer; + SkipWhitespace(Result, B); + while(B->Ptr - B->Location < B->Size) + { + token T = {}; + uint64_t Advancement = 0; + + if(!StringsDifferS(TokenStrings[TOKEN_COMMENT_SINGLE], B)) + { + T.Type = TOKEN_COMMENT; + B->Ptr += StringLength(TokenStrings[TOKEN_COMMENT_SINGLE]); + SkipWhitespace(Result, B); + T.Content.Base = B->Ptr; + while(B->Ptr && *B->Ptr != '\n') + { + ++T.Content.Length; + ++B->Ptr; + } + if(*B->Ptr == '\n') + { + ++Result->CurrentLine; + } + Advancement = 1; + } + else if(!StringsDifferS(TokenStrings[TOKEN_COMMENT_MULTI_OPEN], B)) + { + uint64_t CommentDepth = 1; + T.Type = TOKEN_COMMENT; + B->Ptr += StringLength(TokenStrings[TOKEN_COMMENT_MULTI_OPEN]); + SkipWhitespace(Result, B); + T.Content.Base = B->Ptr; + while(B->Ptr - B->Location < B->Size && CommentDepth) + { + if(!StringsDifferS(TokenStrings[TOKEN_COMMENT_MULTI_CLOSE], B)) + { + --CommentDepth; + B->Ptr += StringLength(TokenStrings[TOKEN_COMMENT_MULTI_CLOSE]); + } + else if(B->Ptr - B->Location < B->Size && *B->Ptr == '\n') + { + ++Result->CurrentLine; + ++B->Ptr; + } + else if(!StringsDifferS(TokenStrings[TOKEN_COMMENT_MULTI_OPEN], B)) + { + ++CommentDepth; + B->Ptr += StringLength(TokenStrings[TOKEN_COMMENT_MULTI_OPEN]); + } + else + { + ++B->Ptr; + } + } + T.Content.Length = B->Ptr - T.Content.Base; + Advancement = 0;//StringLength(TokenStrings[TOKEN_COMMENT_MULTI_CLOSE]); + } + else if(!StringsDifferS(TokenStrings[TOKEN_COMMENT_MULTI_CLOSE], B)) + { + Advancement = 2; + T.Type = TOKEN_NULL; + string Char = { .Base = B->Ptr, .Length = 2 }; + ConfigError(Result->File.Path, Result->CurrentLine, S_WARNING, "Mismatched closing multiline comment marker: ", &Char); + } + else if(!StringsDifferS(TokenStrings[TOKEN_DOUBLEQUOTE], B)) + { + T.Type = TOKEN_STRING; + ++B->Ptr; + T.Content.Base = B->Ptr; + while(B->Ptr - B->Location < B->Size && *B->Ptr != '"') + { + if(*B->Ptr == '\\') + { + ++T.Content.Length; + ++B->Ptr; + } + ++T.Content.Length; + ++B->Ptr; + } + Advancement = 1; + } + else if(!StringsDifferS(TokenStrings[TOKEN_ASSIGN], B)) + { + T.Type = TOKEN_ASSIGN; + T.Content.Base = B->Ptr; + ++T.Content.Length; + Advancement = 1; + } + else if(!StringsDifferS(TokenStrings[TOKEN_OPEN_BRACE], B)) + { + T.Type = TOKEN_OPEN_BRACE; + T.Content.Base = B->Ptr; + ++T.Content.Length; + Advancement = 1; + } + else if(!StringsDifferS(TokenStrings[TOKEN_CLOSE_BRACE], B)) + { + T.Type = TOKEN_CLOSE_BRACE; + T.Content.Base = B->Ptr; + ++T.Content.Length; + Advancement = 1; + } + else if(!StringsDifferS(TokenStrings[TOKEN_SEMICOLON], B)) + { + T.Type = TOKEN_SEMICOLON; + T.Content.Base = B->Ptr; + ++T.Content.Length; + Advancement = 1; + } + else if(IsValidIdentifierCharacter(*B->Ptr)) + { + T.Type = TOKEN_IDENTIFIER; + T.Content.Base = B->Ptr; + while(IsValidIdentifierCharacter(*B->Ptr)) + { + ++T.Content.Length; + ++B->Ptr; + } + Advancement = 0; + } + else if(IsNumber(*B->Ptr)) + { + T.Type = TOKEN_NUMBER; + T.Content.Base = B->Ptr; + while(IsNumber(*B->Ptr)) + { + ++T.Content.Length; + ++B->Ptr; + } + Advancement = 0; + } + else if(*B->Ptr == '\n') + { + T.Type = TOKEN_NEWLINE; + T.Content.Base = B->Ptr; + T.Content.Length = 1; + ++Result->CurrentLine; + Advancement = 1; + } + else + { + T.Type = TOKEN_NULL; + string Char = GetUTF8Character(B->Ptr, B->Size - (B->Ptr - B->Location)); + Advancement = Char.Length; + if(Char.Base) + { + ConfigError(Result->File.Path, Result->CurrentLine, S_WARNING, "Unhandled character (ignored): ", &Char); + } + else + { + ConfigErrorInt(Result->File.Path, Result->CurrentLine, S_WARNING, "Malformed UTF-8 bytes encountered (skipped): ", Char.Length); + } + } + + PushToken(Result, &T); + Advance(B, Advancement); + SkipWhitespace(Result, B); + } + // TODO(matt): PushConfigWatchHandle() + } + else + { + ConfigError(0, 0, S_WARNING, "Unable to open config file: ", &Path); + } + Free(Path0); + return Result; +} + +bool +TokenEquals(tokens *T, char *String) +{ + int i = 0; + for(; i < T->Token[T->CurrentIndex].Content.Length && *String && T->Token[T->CurrentIndex].Content.Base[i] == String[i]; ++i) + { + } + if(i == T->Token[T->CurrentIndex].Content.Length && !String[i]) + { + return TRUE; + } + return FALSE; +} + +bool +TokenIs(tokens *T, token_type Type) +{ + return(T->Token[T->CurrentIndex].Type == Type); +} + +bool +ExpectToken(tokens *T, token_type Type, token *Dest) +{ + bool Result = FALSE; + + if(T->Token[T->CurrentIndex].Type == Type) + { + Result = TRUE; + if(Dest) + { + *Dest = T->Token[T->CurrentIndex]; + } + } + else + { + Dest = 0; + ConfigErrorExpectation(T, Type, 0); + } + ++T->CurrentIndex; + return Result; +} + +char *FieldTypeNames[] = +{ + "bare", + "boolean", + "number", + "scope", + "string", +}; + +typedef enum +{ + FT_BARE, + FT_BOOLEAN, + FT_NUMBER, + FT_SCOPE, + FT_STRING, +} config_field_type; + +typedef struct +{ + config_identifier_id ID; + config_field_type Type; + bool Singleton; + bool IsSetLocally; +} config_type_field; + +typedef struct +{ + config_identifier_id ID; + bool Permeable; + uint64_t FieldCount; + config_type_field *Field; +} config_type_spec; + +typedef struct +{ + uint64_t Count; + config_type_spec *Spec; +} config_type_specs; + +config_type_spec * +PushTypeSpec(config_type_specs *S, config_identifier_id ID, bool IsPermeable) +{ + //PrintFunctionName("PushTypeSpec()"); + S->Spec = Fit(S->Spec, sizeof(*S->Spec), S->Count, 16, FALSE); + config_type_spec *Result = S->Spec + S->Count; + S->Spec[S->Count].ID = ID; + S->Spec[S->Count].FieldCount = 0; + S->Spec[S->Count].Field = 0; + S->Spec[S->Count].Permeable = IsPermeable; + ++S->Count; + return Result; +} + +void +PushTypeSpecField(config_type_spec *Spec, config_field_type Type, config_identifier_id ID, bool Singleton) +{ + //PrintFunctionName("PushTypeSpecField()"); + Spec->Field = Fit(Spec->Field, sizeof(*Spec->Field), Spec->FieldCount, 4, FALSE); + Spec->Field[Spec->FieldCount].Type = Type; + Spec->Field[Spec->FieldCount].ID = ID; + Spec->Field[Spec->FieldCount].Singleton = Singleton; + Spec->Field[Spec->FieldCount].IsSetLocally = FALSE; + ++Spec->FieldCount; +} + +config_type_specs +InitTypeSpecs(void) +{ + //PrintFunctionName("InitTypeSpecs()"); + config_type_specs Result = {}; + config_type_spec *Root = PushTypeSpec(&Result, IDENT_NULL, FALSE); + PushTypeSpecField(Root, FT_STRING, IDENT_ASSETS_ROOT_DIR, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_ASSETS_ROOT_URL, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_BASE_DIR, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_BASE_URL, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_CACHE_DIR, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_COHOST, FALSE); + PushTypeSpecField(Root, FT_STRING, IDENT_CSS_PATH, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_DB_LOCATION, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_DEFAULT_MEDIUM, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_GENRE, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_GLOBAL_SEARCH_DIR, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_GLOBAL_SEARCH_TEMPLATE, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_GLOBAL_SEARCH_URL, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_GLOBAL_TEMPLATES_DIR, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_GLOBAL_THEME, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_GUEST, FALSE); + PushTypeSpecField(Root, FT_STRING, IDENT_HMML_DIR, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_IMAGES_PATH, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_INDEXER, FALSE); + PushTypeSpecField(Root, FT_STRING, IDENT_JS_PATH, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_LOG_LEVEL, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_NUMBERING_SCHEME, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_OWNER, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_PLAYER_LOCATION, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_PLAYER_TEMPLATE, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_QUERY_STRING, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_SEARCH_LOCATION, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_SEARCH_TEMPLATE, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_TEMPLATES_DIR, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_STREAM_USERNAME, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_VOD_PLATFORM, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_THEME, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_TITLE, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_HTML_TITLE, TRUE); + // TODO(matt): Sort out this title list stuff + // + PushTypeSpecField(Root, FT_STRING, IDENT_TITLE_LIST_DELIMITER, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_TITLE_LIST_PREFIX, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_TITLE_LIST_SUFFIX, TRUE); + PushTypeSpecField(Root, FT_STRING, IDENT_TITLE_SUFFIX, TRUE); + // + //// + PushTypeSpecField(Root, FT_STRING, IDENT_UNIT, TRUE); + PushTypeSpecField(Root, FT_BARE, IDENT_TITLE_LIST_END, TRUE); + PushTypeSpecField(Root, FT_BOOLEAN, IDENT_IGNORE_PRIVACY, TRUE); + PushTypeSpecField(Root, FT_BOOLEAN, IDENT_SINGLE_BROWSER_TAB, TRUE); + PushTypeSpecField(Root, FT_NUMBER, IDENT_PRIVACY_CHECK_INTERVAL, TRUE); + PushTypeSpecField(Root, FT_SCOPE, IDENT_INCLUDE, FALSE); + PushTypeSpecField(Root, FT_SCOPE, IDENT_MEDIUM, FALSE); + PushTypeSpecField(Root, FT_SCOPE, IDENT_PERSON, FALSE); + PushTypeSpecField(Root, FT_SCOPE, IDENT_PROJECT, FALSE); + PushTypeSpecField(Root, FT_SCOPE, IDENT_SUPPORT, FALSE); + + config_type_spec *Support = PushTypeSpec(&Result, IDENT_SUPPORT, FALSE); + PushTypeSpecField(Support, FT_STRING, IDENT_ICON, TRUE); + PushTypeSpecField(Support, FT_STRING, IDENT_ICON_NORMAL, TRUE); + PushTypeSpecField(Support, FT_STRING, IDENT_ICON_FOCUSED, TRUE); + PushTypeSpecField(Support, FT_STRING, IDENT_ICON_TYPE, TRUE); + PushTypeSpecField(Support, FT_STRING, IDENT_ICON_VARIANTS, TRUE); + PushTypeSpecField(Support, FT_STRING, IDENT_URL, TRUE); + + config_type_spec *Include = PushTypeSpec(&Result, IDENT_INCLUDE, FALSE); + PushTypeSpecField(Include, FT_STRING, IDENT_ALLOW, FALSE); + PushTypeSpecField(Include, FT_STRING, IDENT_DENY, FALSE); + + config_type_spec *Person = PushTypeSpec(&Result, IDENT_PERSON, FALSE); + PushTypeSpecField(Person, FT_STRING, IDENT_NAME, TRUE); + PushTypeSpecField(Person, FT_STRING, IDENT_HOMEPAGE, TRUE); + PushTypeSpecField(Person, FT_SCOPE, IDENT_SUPPORT, FALSE); + + config_type_spec *Medium = PushTypeSpec(&Result, IDENT_MEDIUM, FALSE); + PushTypeSpecField(Medium, FT_STRING, IDENT_ICON, TRUE); + PushTypeSpecField(Medium, FT_STRING, IDENT_ICON_NORMAL, TRUE); + PushTypeSpecField(Medium, FT_STRING, IDENT_ICON_FOCUSED, TRUE); + PushTypeSpecField(Medium, FT_STRING, IDENT_ICON_DISABLED, TRUE); + PushTypeSpecField(Medium, FT_STRING, IDENT_ICON_TYPE, TRUE); + PushTypeSpecField(Medium, FT_STRING, IDENT_ICON_VARIANTS, TRUE); + PushTypeSpecField(Medium, FT_STRING, IDENT_NAME, TRUE); + PushTypeSpecField(Medium, FT_BOOLEAN, IDENT_HIDDEN, TRUE); + + config_type_spec *Project = PushTypeSpec(&Result, IDENT_PROJECT, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_BASE_DIR, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_BASE_URL, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_COHOST, FALSE); + PushTypeSpecField(Project, FT_STRING, IDENT_DEFAULT_MEDIUM, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_GENRE, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_GUEST, FALSE); + PushTypeSpecField(Project, FT_STRING, IDENT_HMML_DIR, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_INDEXER, FALSE); + PushTypeSpecField(Project, FT_STRING, IDENT_NUMBERING_SCHEME, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_OWNER, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_PLAYER_LOCATION, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_PLAYER_TEMPLATE, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_QUERY_STRING, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_SEARCH_LOCATION, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_SEARCH_TEMPLATE, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_TEMPLATES_DIR, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_STREAM_USERNAME, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_VOD_PLATFORM, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_THEME, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_TITLE, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_HTML_TITLE, TRUE); + + PushTypeSpecField(Project, FT_STRING, IDENT_ART, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_ART_VARIANTS, TRUE); + + PushTypeSpecField(Project, FT_STRING, IDENT_ICON, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_ICON_NORMAL, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_ICON_FOCUSED, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_ICON_DISABLED, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_ICON_TYPE, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_ICON_VARIANTS, TRUE); + // TODO(matt): Sort out this title list stuff + // + PushTypeSpecField(Project, FT_STRING, IDENT_TITLE_LIST_DELIMITER, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_TITLE_LIST_PREFIX, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_TITLE_LIST_SUFFIX, TRUE); + PushTypeSpecField(Project, FT_STRING, IDENT_TITLE_SUFFIX, TRUE); + // + //// + PushTypeSpecField(Project, FT_STRING, IDENT_UNIT, TRUE); + PushTypeSpecField(Project, FT_BARE, IDENT_TITLE_LIST_END, TRUE); + + // NOTE(matt): Modes + PushTypeSpecField(Project, FT_BOOLEAN, IDENT_IGNORE_PRIVACY, TRUE); + PushTypeSpecField(Project, FT_BOOLEAN, IDENT_SINGLE_BROWSER_TAB, TRUE); + // + + PushTypeSpecField(Project, FT_SCOPE, IDENT_INCLUDE, FALSE); + PushTypeSpecField(Project, FT_SCOPE, IDENT_MEDIUM, FALSE); + PushTypeSpecField(Project, FT_SCOPE, IDENT_PROJECT, FALSE); + return Result; +} + +void +PrintTypeField(config_type_field *F, config_identifier_id ParentScopeID, int IndentationLevel) +{ + IndentedCarriageReturn(IndentationLevel); + PrintC(CS_YELLOW_BOLD, ConfigIdentifiers[F->ID].String); + PrintC(CS_BLACK_BOLD, " ["); + if(F->Type == FT_NUMBER) + { + PrintC(CS_BLUE_BOLD, FieldTypeNames[F->Type]); + } + else if(F->Type == FT_BARE) + { + PrintC(CS_BLUE, FieldTypeNames[F->Type]); + } + else{ + Colourise(CS_GREEN_BOLD); + fprintf(stderr, "\"%s\"", FieldTypeNames[F->Type]); + Colourise(CS_END); + } + PrintC(CS_BLACK_BOLD, " • "); + if(F->Singleton) + { + PrintC(CS_CYAN, "single"); + } + else + { + PrintC(CS_MAGENTA, "multi"); + } + PrintC(CS_BLACK_BOLD, "]"); + // TODO(matt): Consider preventing the description from being written if it had already been + // written for a higher-level scope + if(ConfigIdentifiers[F->ID].IdentifierDescription) + { + if((F->ID == IDENT_ICON || F->ID == IDENT_NAME) && ParentScopeID == IDENT_MEDIUM) + { + if(!ConfigIdentifiers[F->ID].IdentifierDescription_MediumDisplayed) + { + ++IndentationLevel; + IndentedCarriageReturn(IndentationLevel); + TypesetString(INDENT_WIDTH * IndentationLevel, Wrap0(ConfigIdentifiers[F->ID].IdentifierDescription_Medium)); + ConfigIdentifiers[F->ID].IdentifierDescription_MediumDisplayed = TRUE; + --IndentationLevel; + } + } + else + { + if(!ConfigIdentifiers[F->ID].IdentifierDescriptionDisplayed) + { + ++IndentationLevel; + IndentedCarriageReturn(IndentationLevel); + TypesetString(INDENT_WIDTH * IndentationLevel, Wrap0(ConfigIdentifiers[F->ID].IdentifierDescription)); + ConfigIdentifiers[F->ID].IdentifierDescriptionDisplayed = TRUE; + --IndentationLevel; + } + } + } +} + +void +PrintTypeSpec(config_type_spec *S, int IndentationLevel) +{ + if(S->ID != IDENT_NULL) + { + IndentedCarriageReturn(IndentationLevel); + ++IndentationLevel; + PrintStringC(CS_CYAN, Wrap0(ConfigIdentifiers[S->ID].String)); + } + + if(ConfigIdentifiers[S->ID].IdentifierDescription && !ConfigIdentifiers[S->ID].IdentifierDescriptionDisplayed) + { + IndentedCarriageReturn(IndentationLevel); + TypesetString(INDENT_WIDTH * IndentationLevel, Wrap0(ConfigIdentifiers[S->ID].IdentifierDescription)); + ConfigIdentifiers[S->ID].IdentifierDescriptionDisplayed = TRUE; + } + + for(int i = 0; i < S->FieldCount; ++i) + { + if(!(S->ID == IDENT_NULL && S->Field[i].Type == FT_SCOPE)) + { + PrintTypeField(&S->Field[i], S->ID, IndentationLevel); + } + } + + if(S->ID != IDENT_NULL) { --IndentationLevel; } +} + +void +PrintTypeSpecs(config_type_specs *S, int IndentationLevel) +{ + fprintf(stderr, "\n"); + // TODO(matt): Document the fact that scopes within scopes are fully configurable, even though + // we only print out the "id" of scopes, omitting their containing fields + for(int i = 0; i < S->Count; ++i) + { + PrintTypeSpec(&S->Spec[i], IndentationLevel); + } +} + +void +FreeTypeSpec(config_type_spec *S) +{ + FreeAndResetCount(S->Field, S->FieldCount); +} + +void +FreeTypeSpecs(config_type_specs *S) +{ + for(int i = 0; i < S->Count; ++i) + { + FreeTypeSpec(&S->Spec[i]); + } + FreeAndResetCount(S->Spec, S->Count); +} + +typedef struct +{ + char *Filename; + uint64_t LineNumber; +} token_position; + +typedef struct +{ + config_identifier_id Key; + string Value; + token_position Position; +} config_pair; + +typedef struct +{ + config_identifier_id Key; + uint64_t Value; + token_position Position; +} config_int_pair; + +typedef struct +{ + config_identifier_id Key; + bool Value; + token_position Position; +} config_bool_pair; + +typedef struct scope_tree +{ + config_pair ID; + uint64_t TreeCount; + struct scope_tree *Trees; + uint64_t PairCount; + config_pair *Pairs; + uint64_t IntPairCount; + config_int_pair *IntPairs; + uint64_t BoolPairCount; + config_bool_pair *BoolPairs; + struct scope_tree *Parent; + config_type_spec TypeSpec; + //token_position Position; // NOTE(matt): We chose not to make ScopeTokens() set this value +} scope_tree; + +config_type_spec * +GetTypeSpec(config_type_specs *S, config_identifier_id ID) +{ + for(int i = 0; i < S->Count; ++i) + { + if(S->Spec[i].ID == ID) + { + return &S->Spec[i]; + } + } + return 0; +} + +void +SetTypeSpec(scope_tree *Type, config_type_specs *TypeSpecs) +{ + config_type_spec *Spec = GetTypeSpec(TypeSpecs, Type->ID.Key); + //PrintTypeSpec(Spec); + Type->TypeSpec.ID = Spec->ID; + Type->TypeSpec.Permeable = Spec->Permeable; + Type->TypeSpec.FieldCount = Spec->FieldCount; + Type->TypeSpec.Field = malloc(Type->TypeSpec.FieldCount * sizeof(config_type_field)); + for(int i = 0; i < Type->TypeSpec.FieldCount; ++i) + { + Type->TypeSpec.Field[i].Type = Spec->Field[i].Type; + Type->TypeSpec.Field[i].ID = Spec->Field[i].ID; + Type->TypeSpec.Field[i].Singleton = Spec->Field[i].Singleton; + Type->TypeSpec.Field[i].IsSetLocally = Spec->Field[i].IsSetLocally; + } +} + +config_type_field * +CurrentFieldIsInSpec(config_type_spec *Spec, tokens *T) +{ + + if(Spec) + { + for(int i = 0; i < Spec->FieldCount; ++i) + { + if(!StringsDifferLv0(T->Token[T->CurrentIndex].Content, ConfigIdentifiers[Spec->Field[i].ID].String)) + { + return &Spec->Field[i]; + } + } + } + return 0; +} + +typedef struct +{ + uint64_t Count; + string *String; +} string_list; + +typedef struct +{ + string Filename; + string_list Allow; + string_list Deny; +} config_include; + +void +PushString(string_list *Dest, string *Src) +{ + Dest->String = Fit(Dest->String, sizeof(*Dest->String), Dest->Count, 4, FALSE); + Dest->String[Dest->Count] = *Src; + ++Dest->Count; +} + +bool +ParseMultiStringAssignment(tokens *T, string_list *Dest, config_type_field *Field) +{ + ++T->CurrentIndex; + token Token = {}; + if(!ExpectToken(T, TOKEN_ASSIGN, &Token)) + { + return FALSE; + } + if(!ExpectToken(T, TOKEN_STRING, &Token)) + { + return FALSE; + } + + PushString(Dest, &Token.Content); + + Field->IsSetLocally = TRUE; + + if(!ExpectToken(T, TOKEN_SEMICOLON, &Token)) + { + return FALSE; + } + return TRUE; +} + +void +PrintCurrentToken(colour_code C, tokens *T) +{ + Colourise(C); + PrintToken(&T->Token[T->CurrentIndex], T->CurrentIndex); + Colourise(CS_END); +} + +bool +DepartAssignment(tokens *T) +{ + uint64_t ScopeLevel = 0; + do { + if(TokenIs(T, TOKEN_IDENTIFIER)) + { + ++T->CurrentIndex; + if(!ExpectToken(T, TOKEN_ASSIGN, 0)) + { + return FALSE; + } + if(TokenIs(T, TOKEN_STRING) || TokenIs(T, TOKEN_NUMBER)) + { + ++T->CurrentIndex; + } + else + { + ConfigErrorExpectation(T, TOKEN_STRING, TOKEN_NUMBER); + return FALSE; + } + + if(TokenIs(T, TOKEN_SEMICOLON)) + { + ++T->CurrentIndex; + } + else if(TokenIs(T, TOKEN_OPEN_BRACE)) + { + ++ScopeLevel; + ++T->CurrentIndex; + } + else + { + ConfigErrorExpectation(T, TOKEN_SEMICOLON, TOKEN_OPEN_BRACE); + return FALSE; + } + } + else if(TokenIs(T, TOKEN_CLOSE_BRACE)) + { + --ScopeLevel; + ++T->CurrentIndex; + } + else + { + ConfigErrorExpectation(T, TOKEN_IDENTIFIER, TOKEN_CLOSE_BRACE); + return FALSE; + } + } while(ScopeLevel > 0); + return TRUE; +} + +bool +DepartIncludeAssignment(tokens *T, uint64_t IncludeIdentifierTokenIndex) +{ + T->CurrentIndex = IncludeIdentifierTokenIndex; + return(DepartAssignment(T)); +} + +void +FreeString(string *S) +{ + Free(S->Base); + S->Length = 0; +} + +void +FreeStringList(string_list *S) +{ + FreeAndResetCount(S->String, S->Count); +} + +void +FreeInclude(config_include *I) +{ + FreeStringList(&I->Allow); + FreeStringList(&I->Deny); + free(I); + I = 0; +} + +bool +IsStringEmpty(string *S) +{ + return S ? S->Length == 0 : TRUE; +} + +// TODO(matt): This never gets called. Probably remove +void +PrintInclude(config_include *I) +{ + PrintStringC(CS_YELLOW_BOLD, I->Filename); + fprintf(stderr, "\n AllowCount: %lu", I->Allow.Count); + fprintf(stderr, "\n DenyCount: %lu", I->Deny.Count); +#if 1 + for(int i = 0; i < I->Allow.Count; ++i) + { + fprintf(stderr, "\n Allow: "); + PrintStringC(CS_GREEN, I->Allow.String[i]); + } + for(int i = 0; i < I->Deny.Count; ++i) + { + fprintf(stderr, "\n Deny: "); + PrintStringC(CS_GREEN, I->Deny.String[i]); + } +#endif + fprintf(stderr, "\n"); +} + +void +FreeScopeTreeRecursively(scope_tree *T) +{ + FreeAndResetCount(T->Pairs, T->PairCount); + FreeAndResetCount(T->IntPairs, T->IntPairCount); + FreeAndResetCount(T->BoolPairs, T->BoolPairCount); + Free(T->TypeSpec.Field); + //Free(T->Position.Filename); + for(int i = 0; i < T->TreeCount; ++i) + { + FreeScopeTreeRecursively(&T->Trees[i]); + } + FreeAndResetCount(T->Trees, T->TreeCount); +} + +void +FreeScopeTree(scope_tree *T) +{ + FreeAndResetCount(T->Pairs, T->PairCount); + FreeAndResetCount(T->IntPairs, T->IntPairCount); + FreeAndResetCount(T->BoolPairs, T->BoolPairCount); + Free(T->TypeSpec.Field); + //Free(T->Position.Filename); + for(int i = 0; i < T->TreeCount; ++i) + { + FreeScopeTreeRecursively(&T->Trees[i]); + } + FreeAndResetCount(T->Trees, T->TreeCount); + Free(T); +} + +void +PrintPair(config_pair *P, char *Delimiter, bool FillSyntax, uint64_t Indentation, bool PrependNewline, bool AppendNewline) +{ + if(PrependNewline) { fprintf(stderr, "\n"); } + Indent(Indentation); + PrintC(CS_YELLOW_BOLD, ConfigIdentifiers[P->Key].String); + fprintf(stderr, "%s", Delimiter); + if(P->Value.Length) + { + Colourise(CS_GREEN_BOLD); + if(FillSyntax) { fprintf(stderr, "\""); } + PrintString(P->Value); + if(FillSyntax) { fprintf(stderr, "\""); } + Colourise(CS_END); + if(FillSyntax) { fprintf(stderr, ";"); } + } + else + { + PrintC(CS_YELLOW, "[unset]"); + } + if(AppendNewline) { fprintf(stderr, "\n"); } +} + +void +PrintScopeID(config_pair *P, char *Delimiter, uint64_t Indentation) +{ + fprintf(stderr, "\n"); + Indent(Indentation); + PrintC(CS_YELLOW_BOLD, ConfigIdentifiers[P->Key].String); + fprintf(stderr, "%s", Delimiter); + if(P->Value.Length) + { + Colourise(CS_GREEN_BOLD); + fprintf(stderr, "\""); + PrintString(P->Value); + fprintf(stderr, "\""); + Colourise(CS_END); + IndentedCarriageReturn(Indentation); + fprintf(stderr, "{"); + } + else + { + PrintC(CS_YELLOW, "[unset]"); + } + //fprintf(stderr, "\n"); +} + +void +PrintIntPair(config_int_pair *P, char *Delimiter, bool FillSyntax, uint64_t Indentation, bool PrependNewline, bool AppendNewline) +{ + if(PrependNewline) { fprintf(stderr, "\n"); } + Indent(Indentation); + PrintC(CS_YELLOW_BOLD, ConfigIdentifiers[P->Key].String); + fprintf(stderr, "%s", Delimiter); + switch(P->Key) + { + case IDENT_GENRE: + { + Colourise(CS_GREEN_BOLD); + if(FillSyntax) { fprintf(stderr, "\""); } + fprintf(stderr, "%s", GenreStrings[P->Value]); + if(FillSyntax) { fprintf(stderr, "\""); } + } break; + case IDENT_LOG_LEVEL: + { + Colourise(CS_GREEN_BOLD); + if(FillSyntax) { fprintf(stderr, "\""); } + fprintf(stderr, "%s", LogLevelStrings[P->Value]); + if(FillSyntax) { fprintf(stderr, "\""); } + } break; + case IDENT_NUMBERING_SCHEME: + { + Colourise(CS_GREEN_BOLD); + if(FillSyntax) { fprintf(stderr, "\""); } + fprintf(stderr, "%s", NumberingSchemeStrings[P->Value]); + if(FillSyntax) { fprintf(stderr, "\""); } + } break; + default: + { + Colourise(CS_BLUE_BOLD); + fprintf(stderr, "%lu", P->Value); + } break; + } + Colourise(CS_END); + if(FillSyntax) { fprintf(stderr, ";"); } + if(AppendNewline) { fprintf(stderr, "\n"); } +} + +void +PrintBool(char *Title, bool Bool, char *Delimiter, bool FillSyntax, uint64_t Indentation, bool PrependNewline, bool AppendNewline) +{ + if(PrependNewline) { fprintf(stderr, "\n"); } + Indent(Indentation); + PrintC(CS_YELLOW_BOLD, Title); + fprintf(stderr, "%s", Delimiter); + if(Bool == TRUE) + { + Colourise(CS_GREEN); + if(FillSyntax) { fprintf(stderr, "\""); } + fprintf(stderr, "true"); + if(FillSyntax) { fprintf(stderr, "\""); } + } + else + { + Colourise(CS_RED); + if(FillSyntax) { fprintf(stderr, "\""); } + fprintf(stderr, "false"); + if(FillSyntax) { fprintf(stderr, "\""); } + } + Colourise(CS_END); + if(FillSyntax) { fprintf(stderr, ";"); } + if(AppendNewline) { fprintf(stderr, "\n"); } +} + +void +PrintBoolPair(config_bool_pair *P, char *Delimiter, bool FillSyntax, uint64_t Indentation, bool PrependNewline, bool AppendNewline) +{ + PrintBool(ConfigIdentifiers[P->Key].String, P->Value, Delimiter, FillSyntax, Indentation, PrependNewline, AppendNewline); +} + +void PrintScopeTree(scope_tree *T, int IndentationLevel); +void +PrintScopeTree(scope_tree *T, int IndentationLevel) +{ + if(T) + { + if(T->ID.Key != IDENT_NULL) + { + PrintScopeID(&T->ID, " = ", IndentationLevel); + ++IndentationLevel; + } + bool ShouldPrependNewline = TRUE; + bool ShouldFillSyntax = TRUE; + for(int i = 0; i < T->PairCount; ++i) + { + PrintPair(&T->Pairs[i], " = ", ShouldFillSyntax, IndentationLevel, ShouldPrependNewline, FALSE); + } + for(int i = 0; i < T->IntPairCount; ++i) + { + PrintIntPair(&T->IntPairs[i], " = ", ShouldFillSyntax, IndentationLevel, ShouldPrependNewline, FALSE); + } + for(int i = 0; i < T->BoolPairCount; ++i) + { + PrintBoolPair(&T->BoolPairs[i], " = ", ShouldFillSyntax, IndentationLevel, ShouldPrependNewline, FALSE); + } + for(int i = 0; i < T->TreeCount; ++i) + { + PrintScopeTree(&T->Trees[i], IndentationLevel); + } + if(T->ID.Key != IDENT_NULL) + { + --IndentationLevel; + IndentedCarriageReturn(IndentationLevel); + fprintf(stderr, "}"); + } + } +} + +void +PushPair(scope_tree *Parent, config_pair *P) +{ + Parent->Pairs = Fit(Parent->Pairs, sizeof(*Parent->Pairs), Parent->PairCount, 4, FALSE); + Parent->Pairs[Parent->PairCount] = *P; + ++Parent->PairCount; +} + +void +PushIntPair(scope_tree *Parent, config_int_pair *I) +{ + Parent->IntPairs = Fit(Parent->IntPairs, sizeof(*Parent->IntPairs), Parent->IntPairCount, 4, FALSE); + Parent->IntPairs[Parent->IntPairCount] = *I; + ++Parent->IntPairCount; +} + +void +PushBoolPair(scope_tree *Parent, config_bool_pair *B) +{ + Parent->BoolPairs = Fit(Parent->BoolPairs, sizeof(*Parent->BoolPairs), Parent->BoolPairCount, 4, FALSE); + Parent->BoolPairs[Parent->BoolPairCount] = *B; + ++Parent->BoolPairCount; +} + +scope_tree * +PushScope(config_type_specs *TypeSpecs, scope_tree *Parent, config_pair *ID) +{ + // TODO(matt): I reckon what's going on here is that a scope_tree's Parent pointer gets invalidated when that scope_tree's + // Grandparent needs to reallocate its Trees pointer. + // + // So to fix it we have two options: + // 1. When realloc'ing, loop over the Trees, offsetting the Trees->Parent pointer of each Tree's child + // 2. Store this whole stuff in a memory_book-type of thing, so that pointers never get invalidated + // + // We did Option 1, but think we probably ought to use Option 2 instead. Doing so will save us having to + // remember if anything points into our memory, and to offset those pointers if so + scope_tree *Current = Parent->Trees; + Parent->Trees = Fit(Parent->Trees, sizeof(*Parent->Trees), Parent->TreeCount, 4, TRUE); + int64_t Diff = Parent->Trees - Current; + if(Diff) + { + for(int i = 0; i < Parent->TreeCount; ++i) + { + scope_tree *Child = Parent->Trees + i; + for(int j = 0; j < Child->TreeCount; ++j) + { + Child->Trees[j].Parent += Diff; + } + } + } + + Parent->Trees[Parent->TreeCount].ID = *ID; + SetTypeSpec(&Parent->Trees[Parent->TreeCount], TypeSpecs); + Parent->Trees[Parent->TreeCount].Parent = Parent; + ++Parent->TreeCount; + return(&Parent->Trees[Parent->TreeCount - 1]); +} + +config_identifier_id +GetConfigIdentifierFromToken(token *T) +{ + for(config_identifier_id i = 0; i < IDENT_COUNT; ++i) + { + if(!StringsDifferLv0(T->Content, ConfigIdentifiers[i].String)) + { + return i; + } + } + return IDENT_NULL; +} + +config_identifier_id +GetConfigIdentifierFromString(string S) +{ + for(config_identifier_id i = 0; i < IDENT_COUNT; ++i) + { + if(!StringsDifferLv0(S, ConfigIdentifiers[i].String)) + { + return i; + } + } + return IDENT_NULL; +} + +config_type_field * +FieldIsInSpec(config_type_spec *Spec, config_identifier_id ID) +{ + for(int i = 0; i < Spec->FieldCount; ++i) + { + if(Spec->Field[i].ID == ID) + { + return &Spec->Field[i]; + } + } + return 0; +} + +bool +PairsMatch(config_pair *A, config_pair *B) +{ + return A->Key == B->Key && StringsMatch(A->Value, B->Value); +} + +scope_tree * +GetScope(scope_tree *T, config_pair *ID, bool Singleton) +{ + for(int i = 0; i < T->TreeCount; ++i) + { + if(Singleton) + { + if(T->Trees[i].ID.Key == ID->Key) + { + return &T->Trees[i]; + } + } + else + { + if(PairsMatch(&T->Trees[i].ID, ID)) + { + return &T->Trees[i]; + } + } + } + return 0; +} + +config_pair * +GetAssignment(scope_tree *T, config_pair *Assignment, bool Singleton) +{ + for(int i = 0; i < T->PairCount; ++i) + { + if(Singleton) + { + if(T->Pairs[i].Key == Assignment->Key) + { + return &T->Pairs[i]; + } + } + else + { + if(PairsMatch(&T->Pairs[i], Assignment)) + { + return &T->Pairs[i]; + } + } + } + return 0; +} + +config_int_pair * +GetIntAssignment(scope_tree *T, config_int_pair *Assignment, bool Singleton) +{ + for(int i = 0; i < T->IntPairCount; ++i) + { + if(T->IntPairs[i].Key == Assignment->Key) + { + if(Singleton || T->IntPairs[i].Value == Assignment->Value) + { + return &T->IntPairs[i]; + } + } + } + return 0; +} + +config_bool_pair * +GetBoolAssignment(scope_tree *T, config_bool_pair *Assignment, bool Singleton) +{ + for(int i = 0; i < T->BoolPairCount; ++i) + { + if(T->BoolPairs[i].Key == Assignment->Key) + { + if(Singleton || T->BoolPairs[i].Value == Assignment->Value) + { + return &T->BoolPairs[i]; + } + } + } + return 0; +} + +scope_tree *CopyScope(config_type_specs *TypeSpecs, scope_tree *Dest, scope_tree *Src); +void +CopyScopeContents(config_type_specs *TypeSpecs, scope_tree *Dest, scope_tree *Src) +{ + for(int i = 0; i < Src->PairCount; ++i) + { + PushPair(Dest, &Src->Pairs[i]); + } + for(int i = 0; i < Src->IntPairCount; ++i) + { + PushIntPair(Dest, &Src->IntPairs[i]); + } + for(int i = 0; i < Src->BoolPairCount; ++i) + { + PushBoolPair(Dest, &Src->BoolPairs[i]); + } + for(int i = 0; i < Src->TreeCount; ++i) + { + CopyScope(TypeSpecs, Dest, &Src->Trees[i]); + } +} + +scope_tree * +CopyScope(config_type_specs *TypeSpecs, scope_tree *Dest, scope_tree *Src) +{ + Dest = PushScope(TypeSpecs, Dest, &Src->ID); + CopyScopeContents(TypeSpecs, Dest, Src); + return Dest; +} + +void +MergeScopes(config_type_specs *TypeSpecs, scope_tree *Dest, scope_tree *Src, bool Intergenerational) +{ + if(!Intergenerational || Dest->TypeSpec.Permeable) + { + for(int i = 0; i < Src->PairCount; ++i) + { + config_type_field *Field = FieldIsInSpec(&Dest->TypeSpec, Src->Pairs[i].Key); + if(Field) + { + config_pair *ThisAssignment = GetAssignment(Dest, &Src->Pairs[i], Field->Singleton); + if(!ThisAssignment) { PushPair(Dest, &Src->Pairs[i]); } + } + } + for(int i = 0; i < Src->IntPairCount; ++i) + { + config_type_field *Field = FieldIsInSpec(&Dest->TypeSpec, Src->IntPairs[i].Key); + if(Field) + { + config_int_pair *ThisAssignment = GetIntAssignment(Dest, &Src->IntPairs[i], Field->Singleton); + if(!ThisAssignment) { PushIntPair(Dest, &Src->IntPairs[i]); } + } + } + for(int i = 0; i < Src->BoolPairCount; ++i) + { + config_type_field *Field = FieldIsInSpec(&Dest->TypeSpec, Src->BoolPairs[i].Key); + if(Field) + { + config_bool_pair *ThisAssignment = GetBoolAssignment(Dest, &Src->BoolPairs[i], Field->Singleton); + if(!ThisAssignment) { PushBoolPair(Dest, &Src->BoolPairs[i]); } + } + } + for(int i = 0; i < Src->TreeCount; ++i) + { + config_type_field *Field = FieldIsInSpec(&Dest->TypeSpec, Src->Trees[i].ID.Key); + if(Field && !(Intergenerational && Src->Trees[i].ID.Key == Dest->ID.Key)) + { + scope_tree *ThisAssignment = GetScope(Dest, &Src->Trees[i].ID, Field->Singleton); + if(!ThisAssignment) { CopyScope(TypeSpecs, Dest, &Src->Trees[i]); } + } + } + } +} + +typedef enum +{ + CRT_NULL, + CRT_ALLOW, + CRT_DENY, +} config_rule_type; + +typedef struct +{ + config_rule_type Scheme; + uint64_t Count; + config_identifier_id *ID; +} config_include_rules; + +void +PushRule(config_include_rules *R, config_identifier_id ID) +{ + for(int i = 0; i < R->Count; ++i) + { + if(ID == R->ID[i]) + { + return; + } + } + R->ID = Fit(R->ID, sizeof(*R->ID), R->Count, 8, FALSE); + R->ID[R->Count] = ID; + ++R->Count; +} + +void +FreeRules(config_include_rules *R) +{ + FreeAndResetCount(R->ID, R->Count); +} + +void +ParseRuleString(char *Filename, config_type_specs *TypeSpecs, config_type_spec *ParentTypeSpec, config_include_rules *Rules, token *RuleString) +{ + int i = SkipWhitespaceS(&RuleString->Content, 0); + config_type_field *Field = 0; + for(; i < RuleString->Content.Length; i = SkipWhitespaceS(&RuleString->Content, i)) + { + if(IsValidIdentifierCharacter(RuleString->Content.Base[i])) + { + string S = {}; + S.Base = RuleString->Content.Base + i; + for(; i < RuleString->Content.Length && IsValidIdentifierCharacter(RuleString->Content.Base[i]); ++i) + { + ++S.Length; + } + config_identifier_id ID; + if((ID = GetConfigIdentifierFromString(S)) != IDENT_NULL) + { + if((Field = FieldIsInSpec(ParentTypeSpec, ID))) + { + PushRule(Rules, ID); + if(i == RuleString->Content.Length && (ParentTypeSpec = GetTypeSpec(TypeSpecs, Field->ID))) + { + for(int j = 0; j < ParentTypeSpec->FieldCount; ++j) + { + PushRule(Rules, ParentTypeSpec->Field[j].ID); + config_type_spec *ThisTypeSpec = GetTypeSpec(TypeSpecs, ParentTypeSpec->Field[j].ID); + if(ThisTypeSpec) + { + for(int k = 0; k < ThisTypeSpec->FieldCount; ++k) + { + PushRule(Rules, ThisTypeSpec->Field[k].ID); + } + } + } + } + } + else + { + ConfigError(Filename, RuleString->LineNumber, S_WARNING, "Field not in spec: ", &S); + PrintTypeSpec(ParentTypeSpec, 0); + return; + } + } + else + { + ConfigError(Filename, RuleString->LineNumber, S_WARNING, "Unknown identifier: ", &S); + return; + } + } + else if(RuleString->Content.Base[i] == '.') + { + if(Field) + { + ParentTypeSpec = GetTypeSpec(TypeSpecs, Field->ID); + ++i; + if(!ParentTypeSpec) + { + string This = Wrap0(ConfigIdentifiers[Field->ID].String); + ConfigError(Filename, RuleString->LineNumber, S_WARNING, "Invalid permission, type has no members: ", &This); + } + } + else + { + ConfigError(Filename, RuleString->LineNumber, S_WARNING, "Malformed rule string. \".\" must be preceded by a valid type", 0); + return; + } + } + else + { + string Char = GetUTF8Character(RuleString->Content.Base + i, RuleString->Content.Length - i); + ConfigError(Filename, RuleString->LineNumber, S_WARNING, "Invalid character in include rule: ", &Char); + return; + } + } +} + +rc +ParseIncludeRules(config_type_specs *TypeSpecs, config_type_spec *ParentTypeSpec, tokens *T, config_include_rules *Rules) +{ + uint64_t IncludeIdentifierTokenIndex = T->CurrentIndex - 3; + if(TokenIs(T, TOKEN_SEMICOLON)) + { + ++T->CurrentIndex; + return RC_SUCCESS; + } + else if(TokenIs(T, TOKEN_OPEN_BRACE)) + { + ++T->CurrentIndex; + for(; T->CurrentIndex < T->Count;) + { + if(TokenIs(T, TOKEN_IDENTIFIER)) + { + if(TokenEquals(T, "allow")) + { + if(Rules->Scheme == CRT_DENY) + { + FreeRules(Rules); + ConfigError(T->File.Path, T->Token[T->CurrentIndex].LineNumber, S_WARNING, "Mixture of allow and deny identifiers in \"include\" scope", 0); + return DepartIncludeAssignment(T, IncludeIdentifierTokenIndex) ? RC_SCHEME_MIXTURE : RC_SYNTAX_ERROR; + } + Rules->Scheme = CRT_ALLOW; + } + else if(TokenEquals(T, "deny")) + { + if(Rules->Scheme == CRT_ALLOW) + { + FreeRules(Rules); + ConfigError(T->File.Path, T->Token[T->CurrentIndex].LineNumber, S_WARNING, "Mixture of allow and deny identifiers in \"include\" scope", 0); + return DepartIncludeAssignment(T, IncludeIdentifierTokenIndex) ? RC_SCHEME_MIXTURE : RC_SYNTAX_ERROR; + } + Rules->Scheme = CRT_DENY; + } + else + { + ConfigError(T->File.Path, T->Token[T->CurrentIndex].LineNumber, S_WARNING, "Invalid identifier in \"include\" scope: ", &T->Token[T->CurrentIndex].Content); + fprintf(stderr, + " Valid identifiers:\n" + " allow\n" + " deny\n"); + FreeRules(Rules); + return DepartIncludeAssignment(T, IncludeIdentifierTokenIndex) ? RC_INVALID_IDENTIFIER : RC_SYNTAX_ERROR; + } + ++T->CurrentIndex; + if(!ExpectToken(T, TOKEN_ASSIGN, 0)) { FreeRules(Rules); return RC_SYNTAX_ERROR; } + token RuleString = {}; + if(!ExpectToken(T, TOKEN_STRING, &RuleString)) { FreeRules(Rules); return RC_SYNTAX_ERROR; } + ParseRuleString(T->File.Path, TypeSpecs, ParentTypeSpec, Rules, &RuleString); + if(!ExpectToken(T, TOKEN_SEMICOLON, 0)) { FreeRules(Rules); return RC_SYNTAX_ERROR; } + } + else if(TokenIs(T, TOKEN_CLOSE_BRACE)) + { + ++T->CurrentIndex; + return RC_SUCCESS; + } + else + { + FreeRules(Rules); + return RC_SYNTAX_ERROR; + } + } + } + else + { + FreeRules(Rules); + ConfigErrorExpectation(T, TOKEN_SEMICOLON, TOKEN_OPEN_BRACE); + return RC_SYNTAX_ERROR; + } + return RC_SUCCESS; +} + +bool +FieldIsPermitted(config_include_rules *Rules, config_type_field *Field) +{ + for(int i = 0; i < Rules->Count; ++i) + { + if(Field->ID == Rules->ID[i]) + { + return Rules->Scheme == CRT_ALLOW ? TRUE : FALSE; + } + } + return Rules->Scheme == CRT_ALLOW ? FALSE : TRUE; +} + +void +ResetFields(config_type_spec *S) +{ + for(int i = 0; i < S->FieldCount; ++i) + { + S->Field[i].IsSetLocally = FALSE; + } +} + +scope_tree * +PushDefaultScope(config_type_specs *TypeSpecs, scope_tree *Parent, config_identifier_id Key, string Value) +{ + config_pair ID = { .Key = Key, .Value = Value }; + return PushScope(TypeSpecs, Parent, &ID); +} + +void +PushDefaultPair(scope_tree *Parent, config_identifier_id Key, string Value) +{ + config_pair Pair = { .Key = Key, .Value = Value }; + return PushPair(Parent, &Pair); +} + +void +PushDefaultIntPair(scope_tree *Parent, config_identifier_id Key, uint64_t Value) +{ + config_int_pair IntPair = { .Key = Key, .Value = Value }; + return PushIntPair(Parent, &IntPair); +} + +#define DEFAULT_PRIVACY_CHECK_INTERVAL 60 * 4 +void +SetDefaults(scope_tree *Root, config_type_specs *TypeSpecs) +{ + scope_tree *SupportPatreon = PushDefaultScope(TypeSpecs, Root, IDENT_SUPPORT, Wrap0("patreon")); + PushDefaultPair(SupportPatreon, IDENT_ICON, Wrap0("cinera_sprite_patreon.png")); + PushDefaultIntPair(SupportPatreon, IDENT_ICON_TYPE, IT_GRAPHICAL); + PushDefaultIntPair(SupportPatreon, IDENT_ICON_VARIANTS, GetArtVariantFromString(Wrap0("normal"))); + PushDefaultPair(SupportPatreon, IDENT_URL, Wrap0("https://patreon.com/$person")); + + scope_tree *SupportSendOwl = PushDefaultScope(TypeSpecs, Root, IDENT_SUPPORT, Wrap0("sendowl")); + PushDefaultPair(SupportSendOwl, IDENT_ICON, Wrap0("cinera_sprite_sendowl.png")); + + scope_tree *MediumAuthored = PushDefaultScope(TypeSpecs, Root, IDENT_MEDIUM, Wrap0("authored")); + PushDefaultPair(MediumAuthored, IDENT_ICON, Wrap0("🗪")); + PushDefaultPair(MediumAuthored, IDENT_NAME, Wrap0("Chat Comment")); + + PushDefaultPair(Root, IDENT_QUERY_STRING, Wrap0("r")); + PushDefaultPair(Root, IDENT_THEME, Wrap0("$origin")); + PushDefaultPair(Root, IDENT_CACHE_DIR, Wrap0("$XDG_CACHE_HOME/cinera")); + PushDefaultPair(Root, IDENT_DB_LOCATION, Wrap0("$XDG_CONFIG_HOME/cinera/cinera.db")); + + // TODO(matt): Consider where the genre setting should apply, project vs entry level + PushDefaultIntPair(Root, IDENT_GENRE, GENRE_VIDEO); + PushDefaultIntPair(Root, IDENT_NUMBERING_SCHEME, NS_LINEAR); + PushDefaultIntPair(Root, IDENT_PRIVACY_CHECK_INTERVAL, DEFAULT_PRIVACY_CHECK_INTERVAL); + PushDefaultIntPair(Root, IDENT_LOG_LEVEL, LOG_ERROR); +} + +scope_tree * +ScopeTokens(scope_tree *Tree, tokens_list *TokensList, tokens *T, config_type_specs *TypeSpecs, config_include_rules *Rules) +{ + scope_tree *Parent = Tree; + for(T->CurrentIndex = 0; T->CurrentIndex < T->Count;) + { + if(TokenIs(T, TOKEN_IDENTIFIER)) + { + config_type_field *Field = CurrentFieldIsInSpec(&Parent->TypeSpec, T); + if(Field) + { + if(!Rules || FieldIsPermitted(Rules, Field)) + { + if(Field->IsSetLocally && Field->Singleton) + { + ConfigError(T->File.Path, T->Token[T->CurrentIndex].LineNumber, S_WARNING, "Field already set: ", &T->Token[T->CurrentIndex].Content); + } + // NOTE(matt): If we get rid of FT_BARE, then the ++T->CurrentIndex that all cases perform could happen + // right here + config_pair Pair = {}; + config_int_pair IntPair = {}; + config_bool_pair BoolPair = {}; + + Pair.Key = IntPair.Key = BoolPair.Key = Field->ID; + Pair.Position.Filename = IntPair.Position.Filename = BoolPair.Position.Filename = T->File.Path; + + switch(Field->Type) + { + case FT_BARE: + { + token Value = T->Token[T->CurrentIndex]; + Pair.Position.LineNumber = Value.LineNumber; + + ++T->CurrentIndex; + if(!ExpectToken(T, TOKEN_SEMICOLON, 0)) { FreeScopeTree(Tree); return 0; } + config_pair *Assignment = 0; + if(!(Assignment = GetAssignment(Parent, &Pair, Field->Singleton))) + { + PushPair(Parent, &Pair); + } + } break; + case FT_BOOLEAN: + { + ++T->CurrentIndex; + if(!ExpectToken(T, TOKEN_ASSIGN, 0)) { FreeScopeTree(Tree); return 0; } + + token Value = {}; + if(!ExpectToken(T, TOKEN_STRING, &Value)) { FreeScopeTree(Tree); return 0; } + BoolPair.Position.LineNumber = Value.LineNumber; + + if(!ExpectToken(T, TOKEN_SEMICOLON, 0)) { FreeScopeTree(Tree); return 0; } + bool Bool = GetBoolFromString(T->File.Path, &Value); + if(Bool != -1) + { + BoolPair.Value = Bool; + config_bool_pair *Assignment = 0; + if((Assignment = GetBoolAssignment(Parent, &BoolPair, Field->Singleton))) + { + Assignment->Value = BoolPair.Value; + } + else + { + PushBoolPair(Parent, &BoolPair); + } + } + else { FreeScopeTree(Tree); return 0; } + } break; + case FT_STRING: + { + ++T->CurrentIndex; + if(!ExpectToken(T, TOKEN_ASSIGN, 0)) { FreeScopeTree(Tree); return 0; } + + token Value = {}; + if(!ExpectToken(T, TOKEN_STRING, &Value)) { FreeScopeTree(Tree); return 0; } + Pair.Position.LineNumber = Value.LineNumber; + IntPair.Position.LineNumber = Value.LineNumber; + + if(!ExpectToken(T, TOKEN_SEMICOLON, 0)) { FreeScopeTree(Tree); return 0; } + + if(Field->ID == IDENT_NUMBERING_SCHEME) + { + numbering_scheme NumberingScheme = GetNumberingSchemeFromString(T->File.Path, &Value); + if(NumberingScheme != NS_COUNT) + { + IntPair.Value = NumberingScheme; + config_int_pair *Assignment = 0; + if((Assignment = GetIntAssignment(Parent, &IntPair, Field->Singleton))) + { + Assignment->Value = IntPair.Value; + } + else + { + PushIntPair(Parent, &IntPair); + } + } + else { FreeScopeTree(Tree); return 0; } + } + else if(Field->ID == IDENT_LOG_LEVEL) + { + log_level LogLevel = GetLogLevelFromString(T->File.Path, &Value); + if(LogLevel != LOG_COUNT) + { + IntPair.Value = LogLevel; + config_int_pair *Assignment = 0; + if((Assignment = GetIntAssignment(Parent, &IntPair, Field->Singleton))) + { + Assignment->Value = IntPair.Value; + } + else + { + PushIntPair(Parent, &IntPair); + } + } + else { FreeScopeTree(Tree); return 0; } + } + else if(Field->ID == IDENT_GENRE) + { + genre Genre = GetGenreFromString(T->File.Path, &Value); + if(Genre != GENRE_COUNT) + { + IntPair.Value = Genre; + config_int_pair *Assignment = 0; + if((Assignment = GetIntAssignment(Parent, &IntPair, Field->Singleton))) + { + Assignment->Value = IntPair.Value; + } + else + { + PushIntPair(Parent, &IntPair); + } + } + else { FreeScopeTree(Tree); return 0; } + } + else if(Field->ID == IDENT_ICON_TYPE) + { + icon_type IconType = GetIconTypeFromString(T->File.Path, &Value); + if(IconType != IT_COUNT) + { + IntPair.Value = IconType; + config_int_pair *Assignment = 0; + if((Assignment = GetIntAssignment(Parent, &IntPair, Field->Singleton))) + { + Assignment->Value = IntPair.Value; + } + else + { + PushIntPair(Parent, &IntPair); + } + } + else { FreeScopeTree(Tree); return 0; } + } + else if(Field->ID == IDENT_ART_VARIANTS || Field->ID == IDENT_ICON_VARIANTS) + { + int64_t ArtVariants = ParseArtVariantsString(T->File.Path, &Value); + if(ArtVariants != -1) + { + IntPair.Value = ArtVariants; + config_int_pair *Assignment = 0; + if((Assignment = GetIntAssignment(Parent, &IntPair, Field->Singleton))) + { + Assignment->Value = IntPair.Value; + } + else + { + PushIntPair(Parent, &IntPair); + } + } + else { FreeScopeTree(Tree); return 0; } + } + else + { + Pair.Value = Value.Content; + config_pair *Assignment = 0; + + if((Assignment = GetAssignment(Parent, &Pair, Field->Singleton))) + { + Assignment->Value = Pair.Value; + Assignment->Position.LineNumber = Pair.Position.LineNumber; + Assignment->Position.Filename = Pair.Position.Filename; + } + else + { + PushPair(Parent, &Pair); + } + } + } break; + case FT_NUMBER: + { + ++T->CurrentIndex; + if(!ExpectToken(T, TOKEN_ASSIGN, 0)) { FreeScopeTree(Tree); return 0; } + + token Value = {}; + if(!ExpectToken(T, TOKEN_NUMBER, &Value)) { FreeScopeTree(Tree); return 0; } + IntPair.Value = Value.int64_t; + + if(!ExpectToken(T, TOKEN_SEMICOLON, 0)) { FreeScopeTree(Tree); return 0; } + + config_int_pair *Assignment = 0; + if((Assignment = GetIntAssignment(Parent, &IntPair, Field->Singleton))) + { + Assignment->Value = IntPair.Value; + } + else + { + PushIntPair(Parent, &IntPair); + } + } break; + case FT_SCOPE: + { + ++T->CurrentIndex; + if(!ExpectToken(T, TOKEN_ASSIGN, 0)) { FreeScopeTree(Tree); return 0; } + + token Value = {}; + if(!ExpectToken(T, TOKEN_STRING, &Value)) { FreeScopeTree(Tree); return 0; } + Pair.Value = Value.Content; + Pair.Position.LineNumber = Value.LineNumber; + + if(Field->ID == IDENT_INCLUDE) + { + int IncludePathTokenIndex = T->CurrentIndex - 1; + config_include_rules Rules = {}; + switch(ParseIncludeRules(TypeSpecs, &Parent->TypeSpec, T, &Rules)) + { + case RC_SUCCESS: + { + string IncluderFilePath = Wrap0(T->File.Path); + char *IncludePath = ExpandPath(Pair.Value, &IncluderFilePath); + string IncludePathL = Wrap0(IncludePath); + + tokens *I = 0; + for(int i = 0; i < TokensList->Count; ++i) + { + if(!StringsDifferLv0(Value.Content, TokensList->Tokens[i].File.Path)) + { + I = TokensList->Tokens + i; + I->CurrentIndex = 0; + I->CurrentLine = 0; + } + } + if(!I) + { + I = Tokenise(TokensList, IncludePathL); + } + if(I) + { + Parent = ScopeTokens(Parent, TokensList, I, TypeSpecs, &Rules); + FreeRules(&Rules); + if(!Parent) + { + return 0; + } + } + else + { + ConfigFileIncludeError(T->File.Path, T->Token[IncludePathTokenIndex].LineNumber, Wrap0(IncludePath)); + } + Free(IncludePath); + } break; + case RC_SCHEME_MIXTURE: + { + } break; + case RC_SYNTAX_ERROR: + { + FreeScopeTree(Tree); return 0; + } break; + case RC_INVALID_IDENTIFIER: + { + } break; + default: break; + } + } + else + { + scope_tree *Search = 0; + if(Field->ID == IDENT_SUPPORT) + { + scope_tree *ParentP = Parent; + Search = GetScope(ParentP, &Pair, Field->Singleton); + while(!Search && ParentP->Parent) + { + ParentP = ParentP->Parent; + Search = GetScope(ParentP, &Pair, Field->Singleton); + } + } + else + { + Search = GetScope(Parent, &Pair, Field->Singleton); + } + + + if(Search) + { + if(Field->ID == IDENT_SUPPORT) + { + Parent = CopyScope(TypeSpecs, Parent, Search); + } + else + { + Parent = Search; + MergeScopes(TypeSpecs, Parent, Parent->Parent, TRUE); + } + } + else + { + Parent = PushScope(TypeSpecs, Parent, &Pair); + if(Parent->ID.Key == IDENT_MEDIUM) + { + config_bool_pair Hidden = {}; + Hidden.Key = IDENT_HIDDEN; + Hidden.Value = FALSE; + PushBoolPair(Parent, &Hidden); + } + MergeScopes(TypeSpecs, Parent, Parent->Parent, TRUE); + } + + ResetFields(&Parent->TypeSpec); + + if(TokenIs(T, TOKEN_SEMICOLON)) + { + Parent = Parent->Parent; + } + else if(TokenIs(T, TOKEN_OPEN_BRACE)) + { + } + else + { + ConfigErrorExpectation(T, TOKEN_SEMICOLON, TOKEN_OPEN_BRACE); + FreeScopeTree(Tree); return 0; + } + ++T->CurrentIndex; + } + } break; + } + // NOTE(matt): This may not be appropriate for the IDENT_INCLUDE... + Field->IsSetLocally = TRUE; + } + else + { + ConfigError(T->File.Path, T->Token[T->CurrentIndex].LineNumber, S_WARNING, "Field not allowed: ", &T->Token[T->CurrentIndex].Content); + DepartAssignment(T); + } + } + else + { + ConfigError(T->File.Path, T->Token[T->CurrentIndex].LineNumber, S_WARNING, "Invalid field: ", &T->Token[T->CurrentIndex].Content); + if(!DepartAssignment(T)) + { + FreeScopeTree(Tree); + return 0; + } + } + } + else if(TokenIs(T, TOKEN_CLOSE_BRACE)) + { + if(!Parent->Parent) + { + ConfigError(T->File.Path, T->Token[T->CurrentIndex].LineNumber, S_ERROR, "Syntax error: Unpaired closing brace", 0); + FreeScopeTree(Tree); + return 0; + } + Parent = Parent->Parent; + ++T->CurrentIndex; + } + else + { + ConfigErrorExpectation(T, TOKEN_IDENTIFIER, TOKEN_CLOSE_BRACE); + FreeScopeTree(Tree); + return 0; + } + } + return Tree; +} + +// TODO(matt): Fully nail down the title list stuff. Perhaps, make the title_list_end; identifier somehow indicate to child +// scopes that they should not absorb any title_list stuff... + +typedef struct +{ + token_position Position; + config_identifier_id Variable; +} resolution_error; + +typedef struct +{ + uint64_t ErrorCount; + resolution_error *Errors; + uint64_t WarningCount; + resolution_error *Warnings; +} resolution_errors; + +void +PushError(resolution_errors *E, severity Severity, token_position *Position, config_identifier_id Variable) +{ + if(Severity == S_WARNING) + { + E->Warnings = Fit(E->Warnings, sizeof(*E->Warnings), E->WarningCount, 16, TRUE); + if(Position) + { + E->Warnings[E->WarningCount].Position = *Position; + } + E->Warnings[E->WarningCount].Variable = Variable; + ++E->WarningCount; + } + else + { + E->Errors = Fit(E->Errors, sizeof(*E->Errors), E->ErrorCount, 16, TRUE); + if(Position) + { + E->Errors[E->ErrorCount].Position = *Position; + } + E->Errors[E->ErrorCount].Variable = Variable; + ++E->ErrorCount; + } +} + +resolution_error * +GetError(resolution_errors *E, severity Severity, token_position *Position, config_identifier_id Variable) +{ + if(Severity == S_WARNING) + { + for(int i = 0; i < E->WarningCount; ++i) + { + if(E->Warnings[i].Position.Filename == Position->Filename && + E->Warnings[i].Position.LineNumber == Position->LineNumber && + E->Warnings[i].Variable == Variable) + { + return &E->Warnings[i]; + } + } + } + else + { + for(int i = 0; i < E->ErrorCount; ++i) + { + if(E->Errors[i].Position.Filename == Position->Filename && + E->Errors[i].Position.LineNumber == Position->LineNumber && + E->Errors[i].Variable == Variable) + { + return &E->Errors[i]; + } + } + } + return 0; +} + +void +FreeErrors(resolution_errors *E) +{ + FreeAndResetCount(E->Errors, E->ErrorCount); + FreeAndResetCount(E->Warnings, E->WarningCount); +} + +string +ResolveEnvironmentVariable(memory_book *M, string Variable) +{ + string Result = {}; + int DollarCharacterBytes = 1; + int NullTerminationBytes = 1; + // NOTE(matt): Stack-string + char Variable0[DollarCharacterBytes + Variable.Length + NullTerminationBytes]; + char *Ptr = Variable0; + if(!StringsMatch(Variable, Wrap0("~"))) + { + *Ptr++ = '$'; + } + for(int i = 0; i < Variable.Length; ++i) + { + *Ptr++ = Variable.Base[i]; + } + *Ptr = '\0'; + int Flags = WRDE_NOCMD | WRDE_UNDEF | WRDE_APPEND; + wordexp_t Expansions = {}; + wordexp(Variable0, &Expansions, Flags); + if(Expansions.we_wordc > 0) + { + Result = ExtendStringInBook(M, Wrap0(Expansions.we_wordv[0])); + } + wordfree(&Expansions); +#endif + return Result; +} + +person * +GetPerson(config *C, resolution_errors *E, config_pair *PersonID) +{ + for(int i = 0; i < C->PersonCount; ++i) + { + if(!StringsDifferCaseInsensitive(C->Person[i].ID, PersonID->Value)) + { + return &C->Person[i]; + } + } + + if(!GetError(E, S_WARNING, &PersonID->Position, IDENT_PERSON)) + { + ConfigError(PersonID->Position.Filename, PersonID->Position.LineNumber, S_WARNING, "Could not find person: ", &PersonID->Value); + PushError(E, S_WARNING, &PersonID->Position, IDENT_PERSON); + } + return 0; +} + +string +DeriveLineageOfProject(config *C, scope_tree *Project) +{ + string Result = {}; + string_list StringList = {}; + if(Project) + { + PushString(&StringList, &Project->ID.Value); + Project = Project->Parent; + } + while(Project && Project->ID.Key == IDENT_PROJECT) + { + string Slash = Wrap0("/"); + PushString(&StringList, &Slash); + PushString(&StringList, &Project->ID.Value); + Project = Project->Parent; + } + + for(int i = StringList.Count - 1; i >= 0; --i) + { + Result = ExtendStringInBook(&C->ResolvedVariables, StringList.String[i]); + } + FreeStringList(&StringList); + return Result; +} + +string +EmptyString(void) +{ + string Result = {}; + return Result; +} + +string +DeriveLineageWithoutOriginOfProject(config *C, scope_tree *Project) +{ + string Result = {}; + string_list StringList = {}; + if(Project->Parent && Project->Parent->ID.Key == IDENT_PROJECT) + { + PushString(&StringList, &Project->ID.Value); + Project = Project->Parent; + } + while(Project->Parent && Project->Parent->ID.Key == IDENT_PROJECT) + { + string Slash = Wrap0("/"); + PushString(&StringList, &Slash); + PushString(&StringList, &Project->ID.Value); + Project = Project->Parent; + } + + if(StringList.Count > 0) + { + for(int i = StringList.Count - 1; i >= 0; --i) + { + Result = ExtendStringInBook(&C->ResolvedVariables, StringList.String[i]); + } + FreeStringList(&StringList); + } + else + { + Result = ExtendStringInBook(&C->ResolvedVariables, EmptyString()); + } + return Result; +} + +string +ResolveLocalVariable(config *C, resolution_errors *E, scope_tree *Scope, config_identifier_id Variable, token_position *Position) +{ + string Result = {}; + switch(Variable) + { + case IDENT_LINEAGE: + { + if(Scope->ID.Key == IDENT_PROJECT) + { + Result = DeriveLineageOfProject(C, Scope); + } + else + { + string This = Wrap0(ConfigIdentifiers[Variable].String); + if(!GetError(E, S_WARNING, Position, Variable)) + { + ConfigError(Position->Filename, Position->LineNumber, S_WARNING, "Variable could not be resolved: ", &This); + PushError(E, S_WARNING, Position, Variable); + } + } + } break; + case IDENT_LINEAGE_WITHOUT_ORIGIN: + { + if(Scope->ID.Key == IDENT_PROJECT) + { + Result = DeriveLineageWithoutOriginOfProject(C, Scope); + } + else + { + string This = Wrap0(ConfigIdentifiers[Variable].String); + if(!GetError(E, S_WARNING, Position, Variable)) + { + ConfigError(Position->Filename, Position->LineNumber, S_WARNING, "Variable could not be resolved: ", &This); + PushError(E, S_WARNING, Position, Variable); + } + } + } break; + case IDENT_ORIGIN: + { + if(Scope->ID.Key == IDENT_PROJECT) + { + while(Scope->Parent && Scope->Parent->ID.Key == IDENT_PROJECT) + { + Scope = Scope->Parent; + } + Result = ExtendStringInBook(&C->ResolvedVariables, Scope->ID.Value); + } + else + { + string This = Wrap0(ConfigIdentifiers[Variable].String); + if(!GetError(E, S_WARNING, Position, Variable)) + { + ConfigError(Position->Filename, Position->LineNumber, S_WARNING, "Variable could not be resolved: ", &This); + PushError(E, S_WARNING, Position, Variable); + } + } + } break; + case IDENT_OWNER: + { + bool Processed = FALSE; + for(int i = 0; i < Scope->PairCount; ++i) + { + if(Scope->Pairs[i].Key == Variable) + { + if(GetPerson(C, E, &Scope->Pairs[i])) + { + Result = ExtendStringInBook(&C->ResolvedVariables, Scope->Pairs[i].Value); + Processed = TRUE; + break; + } + else + { + if(!GetError(E, S_WARNING, Position, Variable)) + { + ConfigError(Position->Filename, Position->LineNumber, S_WARNING, "Owner set, but the person does not exist: ", &Scope->Pairs[i].Value); + PushError(E, S_WARNING, Position, Variable); + } + Processed = TRUE; + } + } + } + if(!Processed) + { + if(!GetError(E, S_WARNING, Position, Variable)) + { + ConfigError(Position->Filename, Position->LineNumber, S_WARNING, "No owner set", 0); + PushError(E, S_WARNING, Position, Variable); + } + } + } break; + case IDENT_PERSON: + { + if(Scope->ID.Key != Variable) + { + Scope = Scope->Parent; + } + } // NOTE(matt): Intentional fall-through + case IDENT_PROJECT: + case IDENT_SUPPORT: + { + if(Scope && Scope->ID.Key == Variable) + { + Result = ExtendStringInBook(&C->ResolvedVariables, Scope->ID.Value); + } + else + { + string This = Wrap0(ConfigIdentifiers[Variable].String); + if(!GetError(E, S_WARNING, Position, Variable)) + { + ConfigError(Position->Filename, Position->LineNumber, S_WARNING, "Variable could not be resolved: ", &This); + PushError(E, S_WARNING, Position, Variable); + } + } + } break; + default: + { + if(!GetError(E, S_WARNING, Position, Variable)) + { + string This = Wrap0(ConfigIdentifiers[Variable].String); + ConfigError(Position->Filename, Position->LineNumber, S_WARNING, "Unhandled local variable: ", &This); + PushError(E, S_WARNING, Position, Variable); + } + } break; + }; + + return Result; +} + +string +ResolveString(config *C, resolution_errors *E, scope_tree *Parent, config_pair *S, bool AbsoluteFilePath) +{ + //PrintFunctionName("ResolveString()"); + string Result = {}; + ResetPen(&C->ResolvedVariables); + for(int i = 0; i < S->Value.Length;) + { + switch(S->Value.Base[i]) + { + case '$': + { + string Test = {}; + ++i; + if(i < S->Value.Length && S->Value.Base[i] == '{') + { + ++i; + Test.Base = S->Value.Base + i; + while(i < S->Value.Length && S->Value.Base[i] != '}') + { + ++Test.Length; + ++i; + } + } + else + { + Test.Base = S->Value.Base + i; + while(i < S->Value.Length && IsValidIdentifierCharacter(S->Value.Base[i])) + { + ++Test.Length; + ++i; + } + if(i > 0) { --i; } + } + + config_identifier_id ID = GetConfigIdentifierFromString(Test); + if(ID != IDENT_NULL) + { + Result = ResolveLocalVariable(C, E, Parent, ID, &S->Position); + } + else + { + Result = ResolveEnvironmentVariable(&C->ResolvedVariables, Test); + } + } break; + case '~': + { + string Char = { .Base = S->Value.Base + i, .Length = 1 }; + if(AbsoluteFilePath && i == 0) + { + Result = ResolveEnvironmentVariable(&C->ResolvedVariables, Char); + } + else + { + Result = ExtendStringInBook(&C->ResolvedVariables, Char); + } + } break; + case '\\': { ++i; }; // NOTE(matt): Intentional fall-through + default: + { + string Char = { .Base = S->Value.Base + i, .Length = 1 }; + Result = ExtendStringInBook(&C->ResolvedVariables, Char); + } break; + + } + ++i; + } + return Result; +} + +typedef enum +{ + P_ABS, + P_REL, +} path_relativity; + +string +StripSlashes(string Path, path_relativity Relativity) +{ + string Result = Path; + if(Relativity == P_REL) + { + while(*Result.Base == '/') + { + ++Result.Base; + --Result.Length; + } + } + + while(Result.Base[Result.Length - 1] == '/') + { + --Result.Length; + } + return Result; +} + +bool +HasImageFileExtension(string Path) +{ + return ExtensionMatches(Path, EXT_GIF) || + ExtensionMatches(Path, EXT_JPEG) || + ExtensionMatches(Path, EXT_JPG) || + ExtensionMatches(Path, EXT_PNG); +} + +typedef struct +{ + string Path; + uint64_t *Variants; + uint64_t VariantsCount; +} variant_string; + +typedef struct +{ + variant_string *Variants; + uint64_t Count; +} variant_strings; + +void +PushVariantUniquely(resolution_errors *E, variant_strings *VS, string Path, uint64_t Variants, config_identifier_id Identifier) +{ + if(Path.Length > 0) + { + bool FoundPath = FALSE; + for(int i = 0; i < VS->Count; ++i) + { + variant_string *This = VS->Variants + i; + if(StringsMatch(This->Path, Path)) + { + FoundPath = TRUE; +/* +Incoming Variants: 4 +List: + 16 + 2 + +Incoming Variants: 4 +List: + 16 + 2 + 4 + +Incoming Variants: 2 +List: + 16 + 2 + 4 +*/ + bool FoundVariantsMatch = FALSE; + for(int VariantsIndex = 0; VariantsIndex < This->VariantsCount; ++VariantsIndex) + { + uint64_t *Test = This->Variants + VariantsIndex; + if(*Test == Variants) + { + FoundVariantsMatch = TRUE; + } + } + + if(!FoundVariantsMatch) + { + This->Variants = Fit(This->Variants, sizeof(*This->Variants), This->VariantsCount, 4, TRUE); + This->Variants[This->VariantsCount] = Variants; + ++This->VariantsCount; + PushError(E, S_ERROR, 0, Identifier); + } + } + } + + if(!FoundPath) + { + VS->Variants = Fit(VS->Variants, sizeof(*VS->Variants), VS->Count, 8, TRUE); + variant_string *New = VS->Variants + VS->Count; + New->Path = Path; + New->Variants = Fit(New->Variants, sizeof(*New->Variants), New->VariantsCount, 4, TRUE); + New->Variants[New->VariantsCount] = Variants; + ++New->VariantsCount; + + ++VS->Count; + } + } +} + +typedef struct +{ + project *Alloc; + project *Project; +} project_and_alloc; + +typedef struct +{ + string String; + project_and_alloc *Projects; + uint64_t ProjectCount; +} config_string_association; + +typedef struct +{ + config_string_association *Associations; + config_identifier_id ID; + uint64_t Count; +} config_string_associations; + +typedef struct +{ + variant_strings VariantStrings; + config_string_associations HMMLDirs; +} config_verifiers; + +void +PushMedium(config *C, resolution_errors *E, config_verifiers *V, project *P, scope_tree *MediumTree) +{ + // TODO(matt): Do we need to warn about incomplete medium information? + P->Medium = Fit(P->Medium, sizeof(*P->Medium), P->MediumCount, 8, TRUE); + + medium *Medium = P->Medium + P->MediumCount; + Medium->ID = ResolveString(C, E, MediumTree, &MediumTree->ID, FALSE); + for(int i = 0; i < MediumTree->PairCount; ++i) + { + config_pair *This = MediumTree->Pairs + i; + switch(This->Key) + { + case IDENT_ICON: { Medium->Icon = ResolveString(C, E, MediumTree, This, FALSE); } break; + case IDENT_ICON_NORMAL: { Medium->IconNormal = ResolveString(C, E, MediumTree, This, FALSE); } break; + case IDENT_ICON_FOCUSED: { Medium->IconFocused = ResolveString(C, E, MediumTree, This, FALSE); } break; + case IDENT_ICON_DISABLED: { Medium->IconDisabled = ResolveString(C, E, MediumTree, This, FALSE); } break; + case IDENT_NAME: { Medium->Name = ResolveString(C, E, MediumTree, This, FALSE); } break; + default: break; + } + } + + for(int i = 0; i < MediumTree->IntPairCount; ++i) + { + config_int_pair *This = MediumTree->IntPairs + i; + switch(This->Key) + { + case IDENT_ICON_TYPE: { Medium->IconType = This->Value; } break; + case IDENT_ICON_VARIANTS: { Medium->IconVariants = This->Value; } break; + default: break; + } + } + + for(int i = 0; i < MediumTree->BoolPairCount; ++i) + { + config_bool_pair *This = MediumTree->BoolPairs + i; + switch(This->Key) + { + case IDENT_HIDDEN: { Medium->Hidden = This->Value; } break; + default: break; + } + } + + if(Medium->Icon.Length > 0) + { + if(Medium->IconType == IT_DEFAULT_UNSET) + { + if(HasImageFileExtension(Medium->Icon)) + { + Medium->IconType = IT_GRAPHICAL; + } + else + { + Medium->IconType = IT_TEXTUAL; + } + } + + switch(Medium->IconType) + { + case IT_GRAPHICAL: + { + PushVariantUniquely(E, &V->VariantStrings, Medium->Icon, Medium->IconVariants, IDENT_ICON); + } break; + case IT_TEXTUAL: + { + if(Medium->IconNormal.Length == 0) { Medium->IconNormal = Medium->Icon; } + if(Medium->IconFocused.Length == 0) { Medium->IconFocused = Medium->Icon; } + if(Medium->IconDisabled.Length == 0) { Medium->IconDisabled = Medium->Icon; } + } break; + default: break; + } + } + + ++P->MediumCount; +} + +medium * +GetMediumFromProject(project *P, string ID) +{ + medium *Result = 0; + for(int i = 0; i < P->MediumCount; ++i) + { + medium *This = P->Medium + i; + if(StringsMatch(ID, This->ID)) + { + Result = This; + break; + } + } + return Result; +} + +void +PushSupport(config *C, resolution_errors *E, config_verifiers *V, person *P, scope_tree *SupportTree) +{ + P->Support = Fit(P->Support, sizeof(*P->Support), P->SupportCount, 2, TRUE); + support *Support = P->Support + P->SupportCount; + Support->ID = ResolveString(C, E, SupportTree, &SupportTree->ID, FALSE); + for(int i = 0; i < SupportTree->PairCount; ++i) + { + config_pair *This = SupportTree->Pairs + i; + switch(This->Key) + { + case IDENT_ICON: { Support->Icon = StripSlashes(ResolveString(C, E, SupportTree, This, FALSE), P_REL); } break; + case IDENT_ICON_NORMAL: { Support->IconNormal = ResolveString(C, E, SupportTree, This, FALSE); } break; + case IDENT_ICON_FOCUSED: { Support->IconFocused = ResolveString(C, E, SupportTree, This, FALSE); } break; + case IDENT_URL: { Support->URL = ResolveString(C, E, SupportTree, This, FALSE); } break; + default: break; + } + } + + for(int i = 0; i < SupportTree->IntPairCount; ++i) + { + config_int_pair *This = SupportTree->IntPairs + i; + switch(This->Key) + { + case IDENT_ICON_TYPE: { Support->IconType = This->Value; } break; + case IDENT_ICON_VARIANTS: { Support->IconVariants = This->Value; } break; + default: break; + } + } + + if(Support->Icon.Length > 0) + { + if(Support->IconType == IT_DEFAULT_UNSET) + { + if(HasImageFileExtension(Support->Icon)) + { + Support->IconType = IT_GRAPHICAL; + } + else + { + Support->IconType = IT_TEXTUAL; + } + } + + switch(Support->IconType) + { + case IT_GRAPHICAL: + { + PushVariantUniquely(E, &V->VariantStrings, Support->Icon, Support->IconVariants, IDENT_ICON); + } break; + case IT_TEXTUAL: + { + if(Support->IconNormal.Length == 0) { Support->IconNormal = Support->Icon; } + if(Support->IconFocused.Length == 0) { Support->IconFocused = Support->Icon; } + } break; + default: break; + } + } + + ++P->SupportCount; +} + +void +PushPersonOntoConfig(config *C, resolution_errors *E, config_verifiers *V, scope_tree *PersonTree) +{ + //PrintFunctionName("PushPersonOntoConfig()"); + //PrintScopeTree(PersonTree); + C->Person = Fit(C->Person, sizeof(*C->Person), C->PersonCount, 8, TRUE); + C->Person[C->PersonCount].ID = ResolveString(C, E, PersonTree, &PersonTree->ID, FALSE); + for(int i = 0; i < PersonTree->PairCount; ++i) + { + if(PersonTree->Pairs[i].Key == IDENT_NAME) + { + C->Person[C->PersonCount].Name = ResolveString(C, E, PersonTree, &PersonTree->Pairs[i], FALSE); + } + else if(PersonTree->Pairs[i].Key == IDENT_HOMEPAGE) + { + C->Person[C->PersonCount].Homepage = ResolveString(C, E, PersonTree, &PersonTree->Pairs[i], FALSE); + } + } + + for(int i = 0; i < PersonTree->TreeCount; ++i) + { + PushSupport(C, E, V, &C->Person[C->PersonCount], &PersonTree->Trees[i]); + } + + ++C->PersonCount; +} + +void +PushPersonOntoProject(config *C, resolution_errors *E, project *P, config_pair *Actor) +{ + person *Person = GetPerson(C, E, Actor); + if(Person) + { + switch(Actor->Key) + { + case IDENT_INDEXER: + { + P->Indexer = Fit(P->Indexer, sizeof(*P->Indexer), P->IndexerCount, 4, FALSE); + P->Indexer[P->IndexerCount] = Person; + ++P->IndexerCount; + } break; + case IDENT_COHOST: + { + P->CoHost = Fit(P->CoHost, sizeof(*P->CoHost), P->CoHostCount, 4, FALSE); + P->CoHost[P->CoHostCount] = Person; + ++P->CoHostCount; + } break; + case IDENT_GUEST: + { + P->Guest = Fit(P->Guest, sizeof(*P->Guest), P->GuestCount, 4, FALSE); + P->Guest[P->GuestCount] = Person; + ++P->GuestCount; + } break; + default:; + } + } +} + +void PushProjectOntoProject(config *C, resolution_errors *E, config_verifiers *Verifiers, project *Parent, scope_tree *ProjectTree); + +void +AddProjectToAssociation(config_string_association *A, project *Alloc, project *P) +{ + A->Projects = Fit(A->Projects, sizeof(*A->Projects), A->ProjectCount, 4, TRUE); + A->Projects[A->ProjectCount].Alloc = Alloc; + A->Projects[A->ProjectCount].Project = P; + ++A->ProjectCount; +} + +void +PushHMMLDirAssociation(config_string_associations *HMMLDirs, string *S, project *Alloc, project *P) +{ + HMMLDirs->Associations = Fit(HMMLDirs->Associations, sizeof(*HMMLDirs->Associations), HMMLDirs->Count, 8, TRUE); + HMMLDirs->Associations[HMMLDirs->Count].String = *S; + AddProjectToAssociation(&HMMLDirs->Associations[HMMLDirs->Count], Alloc, P); + ++HMMLDirs->Count; +} + +void +SetUniqueHMMLDir(config *C, resolution_errors *E, config_string_associations *HMMLDirs, project *Alloc, project *P, scope_tree *ProjectTree, config_pair *HMMLDir) +{ + string Result = StripSlashes(ResolveString(C, E, ProjectTree, HMMLDir, TRUE), P_ABS); + bool Clashed = FALSE; + for(int i = 0; i < HMMLDirs->Count; ++i) + { + if(StringsMatch(HMMLDirs->Associations[i].String, Result)) + { + AddProjectToAssociation(&HMMLDirs->Associations[i], Alloc, P); + PushError(E, S_ERROR, &HMMLDir->Position, IDENT_HMML_DIR); + Clashed = TRUE; + break; + } + } + + if(!Clashed) + { + PushHMMLDirAssociation(HMMLDirs, &Result, Alloc, P); + } + P->HMMLDir = Result; +} + +void +PushProject(config *C, resolution_errors *E, config_verifiers *V, project *Alloc, project *P, scope_tree *ProjectTree) +{ + config_string_associations *HMMLDirs = &V->HMMLDirs; + P->ID = ResolveString(C, E, ProjectTree, &ProjectTree->ID, FALSE); + if(P->ID.Length > MAX_PROJECT_ID_LENGTH) + { + ConfigErrorSizing(ProjectTree->ID.Position.Filename, ProjectTree->ID.Position.LineNumber, IDENT_PROJECT, &P->ID, MAX_PROJECT_ID_LENGTH); + PushError(E, S_ERROR, 0, IDENT_PROJECT); + } + + ResetPen(&C->ResolvedVariables); + P->Lineage = DeriveLineageOfProject(C, ProjectTree); + + for(int i = 0; i < ProjectTree->TreeCount; ++i) + { + if(ProjectTree->Trees[i].ID.Key == IDENT_MEDIUM) + { + PushMedium(C, E, V, P, &ProjectTree->Trees[i]); + } + } + + for(int i = 0; i < ProjectTree->PairCount; ++i) + { + config_pair *This = ProjectTree->Pairs + i; + switch(This->Key) + { + case IDENT_DEFAULT_MEDIUM: + { P->DefaultMedium = GetMediumFromProject(P, This->Value); } break; + case IDENT_HMML_DIR: + { SetUniqueHMMLDir(C, E, HMMLDirs, Alloc, P, ProjectTree, This); } break; + case IDENT_INDEXER: + case IDENT_COHOST: + case IDENT_GUEST: + { PushPersonOntoProject(C, E, P, This); } break; + case IDENT_OWNER: + { + // TODO(matt): Do we need to fail completely if owner cannot be found? + P->Owner = GetPerson(C, E, This); + } break; + case IDENT_PLAYER_TEMPLATE: + { P->PlayerTemplatePath = StripSlashes(ResolveString(C, E, ProjectTree, This, FALSE), P_REL); } break; + case IDENT_THEME: + { P->Theme = ResolveString(C, E, ProjectTree, This, FALSE); + if(P->Theme.Length > MAX_THEME_LENGTH) + { + ConfigErrorSizing(This->Position.Filename, This->Position.LineNumber, This->Key, + &P->Theme, MAX_THEME_LENGTH); + PushError(E, S_ERROR, 0, This->Key); + } + } break; + case IDENT_TITLE: + { + P->Title = ResolveString(C, E, ProjectTree, This, FALSE); + if(P->Title.Length > MAX_PROJECT_NAME_LENGTH) + { + ConfigErrorSizing(This->Position.Filename, This->Position.LineNumber, This->Key, + &P->Title, MAX_PROJECT_NAME_LENGTH); + PushError(E, S_ERROR, 0, This->Key); + } + } break; + case IDENT_HTML_TITLE: { P->HTMLTitle = ResolveString(C, E, ProjectTree, This, FALSE); } break; + // TODO(matt): Sort out this title list stuff + //case IDENT_TITLE_LIST_DELIMITER: { C->Project[C->ProjectCount].HMMLDir = ResolveString(This); } break; + //case IDENT_TITLE_LIST_PREFIX: { C->Project[C->ProjectCount].HMMLDir = ResolveString(This); } break; + //case IDENT_TITLE_LIST_SUFFIX: { C->Project[C->ProjectCount].HMMLDir = ResolveString(This); } break; + //case IDENT_TITLE_SUFFIX: { C->Project[C->ProjectCount].HMMLDir = ResolveString(This); } break; + case IDENT_UNIT: { P->Unit = ResolveString(C, E, ProjectTree, This, FALSE); } break; + case IDENT_BASE_DIR: + { + P->BaseDir = StripSlashes(ResolveString(C, E, ProjectTree, This, TRUE), P_ABS); + if(P->BaseDir.Length > MAX_BASE_DIR_LENGTH) + { + ConfigErrorSizing(This->Position.Filename, This->Position.LineNumber, This->Key, + &P->BaseDir, MAX_BASE_DIR_LENGTH); + PushError(E, S_ERROR, 0, This->Key); + } + } break; + case IDENT_BASE_URL: + { + P->BaseURL = ResolveString(C, E, ProjectTree, This, FALSE); + if(P->BaseURL.Length > MAX_BASE_URL_LENGTH) + { + ConfigErrorSizing(This->Position.Filename, This->Position.LineNumber, This->Key, + &P->BaseURL, MAX_BASE_URL_LENGTH); + PushError(E, S_ERROR, 0, This->Key); + } + } break; + case IDENT_PLAYER_LOCATION: + { + P->PlayerLocation = StripSlashes(ResolveString(C, E, ProjectTree, This, FALSE), P_REL); + if(P->PlayerLocation.Length > MAX_RELATIVE_PAGE_LOCATION_LENGTH) + { + ConfigErrorSizing(This->Position.Filename, This->Position.LineNumber, This->Key, + &P->PlayerLocation, MAX_RELATIVE_PAGE_LOCATION_LENGTH); + PushError(E, S_ERROR, 0, This->Key); + } + } break; + case IDENT_SEARCH_LOCATION: + { + P->SearchLocation = StripSlashes(ResolveString(C, E, ProjectTree, This, FALSE), P_REL); + if(P->SearchLocation.Length > MAX_RELATIVE_PAGE_LOCATION_LENGTH) + { + ConfigErrorSizing(This->Position.Filename, This->Position.LineNumber, This->Key, + &P->SearchLocation, MAX_RELATIVE_PAGE_LOCATION_LENGTH); + PushError(E, S_ERROR, 0, This->Key); + } + } break; + case IDENT_SEARCH_TEMPLATE: + { P->SearchTemplatePath = StripSlashes(ResolveString(C, E, ProjectTree, This, FALSE), P_REL); } break; + case IDENT_TEMPLATES_DIR: + { P->TemplatesDir = StripSlashes(ResolveString(C, E, ProjectTree, This, TRUE), P_ABS); } break; + case IDENT_STREAM_USERNAME: + { P->StreamUsername = ResolveString(C, E, ProjectTree, This, FALSE); } break; + case IDENT_VOD_PLATFORM: + { P->VODPlatform = ResolveString(C, E, ProjectTree, This, FALSE); } break; + + case IDENT_ART: { P->Art = StripSlashes(ResolveString(C, E, ProjectTree, This, FALSE), P_REL); } break; + case IDENT_ICON: { P->Icon = StripSlashes(ResolveString(C, E, ProjectTree, This, FALSE), P_REL); } break; + case IDENT_ICON_NORMAL: { P->IconNormal = ResolveString(C, E, ProjectTree, This, FALSE); } break; + case IDENT_ICON_FOCUSED: { P->IconFocused = ResolveString(C, E, ProjectTree, This, FALSE); } break; + case IDENT_ICON_DISABLED: { P->IconDisabled = ResolveString(C, E, ProjectTree, This, FALSE); } break; + + default: break; + } + } + + for(int i = 0; i < ProjectTree->IntPairCount; ++i) + { + config_int_pair *This = ProjectTree->IntPairs + i; + switch(This->Key) + { + case IDENT_GENRE: { P->Genre = This->Value; } break; + case IDENT_ART_VARIANTS: { P->ArtVariants = This->Value; } break; + case IDENT_ICON_TYPE: { P->IconType = This->Value; } break; + case IDENT_ICON_VARIANTS: { P->IconVariants = This->Value; } break; + case IDENT_NUMBERING_SCHEME: { P->NumberingScheme = This->Value; } break; + default: break; + } + } + + for(int i = 0; i < ProjectTree->BoolPairCount; ++i) + { + config_bool_pair *This = ProjectTree->BoolPairs + i; + switch(This->Key) + { + case IDENT_IGNORE_PRIVACY: + { P->IgnorePrivacy = This->Value; } break; + case IDENT_SINGLE_BROWSER_TAB: + { P->SingleBrowserTab = This->Value; } break; + default: break; + } + } + + for(int i = 0; i < ProjectTree->TreeCount; ++i) + { + scope_tree *This = ProjectTree->Trees + i; + if(This->ID.Key == IDENT_PROJECT) + { + PushProjectOntoProject(C, E, V, P, This); + } + } + + if(!P->DefaultMedium) + { + ResetPen(&C->ResolvedVariables); + ConfigError(0, 0, S_WARNING, "Unset default_medium for project: ", &P->Lineage); + PushError(E, S_WARNING, 0, IDENT_DEFAULT_MEDIUM); + } + + if(P->Art.Length > 0) + { + PushVariantUniquely(E, &V->VariantStrings, P->Art, P->ArtVariants, IDENT_ART); + } + + if(P->Icon.Length > 0) + { + if(P->IconType == IT_DEFAULT_UNSET) + { + if(HasImageFileExtension(P->Icon)) + { + P->IconType = IT_GRAPHICAL; + } + else + { + P->IconType = IT_TEXTUAL; + } + } + + switch(P->IconType) + { + case IT_GRAPHICAL: + { + PushVariantUniquely(E, &V->VariantStrings, P->Icon, P->IconVariants, IDENT_ICON); + } break; + case IT_TEXTUAL: + { + if(P->IconNormal.Length == 0) { P->IconNormal = P->Icon; } + if(P->IconFocused.Length == 0) { P->IconFocused = P->Icon; } + if(P->IconDisabled.Length == 0) { P->IconDisabled = P->Icon; } + } break; + default: break; + } + } + + C->RespectingPrivacy |= !P->IgnorePrivacy; +} + +void +OffsetAssociationProjectPointers(config_string_associations *HMMLDirs, project *Alloc, int64_t Diff) +{ + for(int i = 0; i < HMMLDirs->Count; ++i) + { + config_string_association *A = HMMLDirs->Associations + i; + for(int j = 0; j < A->ProjectCount; ++j) + { + if(A->Projects[j].Alloc == Alloc) + { + A->Projects[j].Alloc += Diff; + A->Projects[j].Project += Diff; + } + } + } +} + +void +OffsetProjectPointers_TopLevel(config_string_associations *HMMLDirs, config *Parent, project *Alloc, int64_t Diff) +{ + OffsetAssociationProjectPointers(HMMLDirs, Alloc, Diff); + for(int i = 0; i < Parent->ProjectCount; ++i) + { + project *Child = Parent->Project + i; + for(int j = 0; j < Child->ChildCount; ++j) + { + project *Grandchild = Child->Child + j; + Grandchild->Parent += Diff; + } + } +} + +void +OffsetProjectPointers(config_string_associations *HMMLDirs, project *Parent, project *Alloc, int64_t Diff) +{ + OffsetAssociationProjectPointers(HMMLDirs, Alloc, Diff); + for(int i = 0; i < Parent->ChildCount; ++i) + { + project *Child = Parent->Child + i; + for(int j = 0; j < Child->ChildCount; ++j) + { + project *Grandchild = Child->Child + j; + Grandchild->Parent += Diff; + } + } +} + +void +PushProjectOntoProject(config *C, resolution_errors *E, config_verifiers *Verifiers, project *Parent, scope_tree *ProjectTree) +{ + config_string_associations *HMMLDirs = &Verifiers->HMMLDirs; + project *Current = Parent->Child; + Parent->Child = Fit(Parent->Child, sizeof(*Parent->Child), Parent->ChildCount, 8, TRUE); + int64_t Diff = Parent->Child - Current; + if(Diff) + { + OffsetProjectPointers(HMMLDirs, Parent, Current, Diff); + } + + project *P = Parent->Child + Parent->ChildCount; + + PushProject(C, E, Verifiers, Parent->Child, P, ProjectTree); + P->Parent = Parent; + + ++Parent->ChildCount; +} + +project * +GetProjectFromProject(project *Parent, string ID) +{ + if(Parent) + { + for(int i = 0; i < Parent->ChildCount; ++i) + { + if(StringsMatch(Parent->Child[i].ID, ID)) + { + return &Parent->Child[i]; + } + } + } + return 0; +} + +void +PushProjectOntoConfig(config *C, resolution_errors *E, config_verifiers *Verifiers, scope_tree *ProjectTree) +{ + config_string_associations *HMMLDirs = &Verifiers->HMMLDirs; + project *Current = C->Project; + C->Project = Fit(C->Project, sizeof(*C->Project), C->ProjectCount, 8, TRUE); + int64_t Diff = C->Project - Current; + if(Diff) + { + OffsetProjectPointers_TopLevel(HMMLDirs, C, Current, Diff); + } + project *P = C->Project + C->ProjectCount; + + PushProject(C, E, Verifiers, C->Project, P, ProjectTree); + + ++C->ProjectCount; +} + +project * +GetProjectFromConfig(config *C, string ID) +{ + if(C) + { + for(int i = 0; i < C->ProjectCount; ++i) + { + if(StringsMatch(C->Project[i].ID, ID)) + { + return &C->Project[i]; + } + } + } + return 0; +} + +void +PrintAssociationClashes(config_string_associations *A) +{ + for(int i = 0; i < A->Count; ++i) + { + config_string_association *Association = A->Associations + i; + if(Association->ProjectCount > 1) + { + string ID = Wrap0(ConfigIdentifiers[A->ID].String); + ConfigError(0, 0, S_ERROR, "Multiple projects share the same ", &ID); + fprintf(stderr, " "); + PrintStringC(CS_MAGENTA_BOLD, Association->String); + fprintf(stderr, "\n"); + for(int j = 0; j < Association->ProjectCount; ++j) + { + project *P = Association->Projects[j].Project; + fprintf(stderr, " "); + PrintStringC(CS_CYAN, P->Lineage); + fprintf(stderr, "\n"); + } + } + } +} + +void +PrintVariants(uint64_t V, colour_code ColourCode, string Margin) +{ + int TermCols = GetTerminalColumns(); + int AvailableColumnsForValue = TermCols - Margin.Length; + int RunningAvailableColumns = TermCols - Margin.Length; + PrintString(Margin); + bool NeedsSpacing = FALSE; + if(V) + { + for(int VariantShifterIndex = 0; VariantShifterIndex < AVS_COUNT; ++VariantShifterIndex) + { + if(V & (1 << VariantShifterIndex)) + { + char *String = ArtVariantStrings[VariantShifterIndex]; + int Length = StringLength(String); + if(RunningAvailableColumns - Length >= 0) + { + fprintf(stderr, "%s", NeedsSpacing ? " " : ""); + } + else + { + fprintf(stderr, "\n"); + PrintString(Margin); + NeedsSpacing = FALSE; + RunningAvailableColumns = AvailableColumnsForValue; + } + PrintC(ColourCode, String); + RunningAvailableColumns -= Length + NeedsSpacing; + NeedsSpacing = TRUE; + } + } + } + else + { + PrintC(CS_YELLOW, "[unset]"); + } +} + +void +PrintVariantInconsistencies(variant_strings *V) +{ + for(int i = 0; i < V->Count; ++i) + { + variant_string *This = V->Variants + i; + if(This->VariantsCount > 1) + { + ConfigError(0, 0, S_ERROR, "Sprite has inconsistently set variants ", &This->Path); + for(int j = 0; j < This->VariantsCount; ++j) + { + string Margin = Wrap0(" "); + uint64_t V = This->Variants[j]; + PrintVariants(V, j % 2 ? CS_CYAN_BOLD : CS_YELLOW_BOLD, Margin); + fprintf(stderr, "\n"); + } + } + } +} + +void +FreeAssociations(config_string_associations *A) +{ + for(int i = 0; i < A->Count; ++i) + { + FreeAndResetCount(A->Associations[i].Projects, A->Associations[i].ProjectCount); + } + FreeAndResetCount(A->Associations, A->Count); +} + +void +FreeVariants(variant_strings *V) +{ + for(int i = 0; i < V->Count; ++i) + { + FreeAndResetCount(V->Variants[i].Variants, V->Variants[i].VariantsCount); + } + FreeAndResetCount(V->Variants, V->Count); +} + +void +FreeVerifiers(config_verifiers *Verifiers) +{ + FreeAssociations(&Verifiers->HMMLDirs); + FreeVariants(&Verifiers->VariantStrings); +} + +config * +ResolveVariables(scope_tree *S) +{ + Assert(LOG_COUNT == ArrayCount(LogLevelStrings)); + config *Result = calloc(1, sizeof(config)); + InitBook(&Result->ResolvedVariables, 1, Kilobytes(1), MBT_STRING); + resolution_errors Errors = {}; + config_verifiers Verifiers = {}; + Verifiers.HMMLDirs.ID = IDENT_HMML_DIR; + + for(int i = 0; i < S->PairCount; ++i) + { + switch(S->Pairs[i].Key) + { + case IDENT_DB_LOCATION: + { Result->DatabaseLocation = StripSlashes(ResolveString(Result, &Errors, S, &S->Pairs[i], TRUE), P_ABS); } break; + case IDENT_GLOBAL_SEARCH_DIR: + { + Result->GlobalSearchDir = StripSlashes(ResolveString(Result, &Errors, S, &S->Pairs[i], TRUE), P_ABS); + if(Result->GlobalSearchDir.Length > MAX_BASE_DIR_LENGTH) + { + ConfigErrorSizing(S->Pairs[i].Position.Filename, S->Pairs[i].Position.LineNumber, S->Pairs[i].Key, + &Result->GlobalSearchDir, MAX_BASE_DIR_LENGTH); + PushError(&Errors, S_ERROR, 0, S->Pairs[i].Key); + } + } break; + case IDENT_GLOBAL_SEARCH_URL: + { + Result->GlobalSearchURL = StripSlashes(ResolveString(Result, &Errors, S, &S->Pairs[i], TRUE), P_ABS); + if(Result->GlobalSearchURL.Length > MAX_BASE_URL_LENGTH) + { + ConfigErrorSizing(S->Pairs[i].Position.Filename, S->Pairs[i].Position.LineNumber, S->Pairs[i].Key, + &Result->GlobalSearchURL, MAX_BASE_URL_LENGTH); + PushError(&Errors, S_ERROR, 0, S->Pairs[i].Key); + } + } break; + case IDENT_GLOBAL_TEMPLATES_DIR: + { Result->GlobalTemplatesDir = StripSlashes(ResolveString(Result, &Errors, S, &S->Pairs[i], TRUE), P_ABS); } break; + case IDENT_GLOBAL_SEARCH_TEMPLATE: + { Result->GlobalSearchTemplatePath = StripSlashes(ResolveString(Result, &Errors, S, &S->Pairs[i], FALSE), P_REL); } break; + case IDENT_GLOBAL_THEME: + { + Result->GlobalTheme = ResolveString(Result, &Errors, S, &S->Pairs[i], FALSE); + if(Result->GlobalTheme.Length > MAX_THEME_LENGTH) + { + ConfigErrorSizing(S->Pairs[i].Position.Filename, S->Pairs[i].Position.LineNumber, S->Pairs[i].Key, + &Result->GlobalTheme, MAX_THEME_LENGTH); + PushError(&Errors, S_ERROR, 0, S->Pairs[i].Key); + } + } break; + case IDENT_ASSETS_ROOT_DIR: + { + Result->AssetsRootDir = StripSlashes(ResolveString(Result, &Errors, S, &S->Pairs[i], TRUE), P_ABS); + if(Result->AssetsRootDir.Length > MAX_ROOT_DIR_LENGTH) + { + ConfigErrorSizing(S->Pairs[i].Position.Filename, S->Pairs[i].Position.LineNumber, S->Pairs[i].Key, + &Result->AssetsRootDir, MAX_ROOT_DIR_LENGTH); + PushError(&Errors, S_ERROR, 0, S->Pairs[i].Key); + } + } break; + case IDENT_ASSETS_ROOT_URL: + { + Result->AssetsRootURL = StripSlashes(ResolveString(Result, &Errors, S, &S->Pairs[i], TRUE), P_ABS); + if(Result->AssetsRootURL.Length > MAX_ROOT_URL_LENGTH) + { + ConfigErrorSizing(S->Pairs[i].Position.Filename, S->Pairs[i].Position.LineNumber, S->Pairs[i].Key, + &Result->AssetsRootURL, MAX_ROOT_URL_LENGTH); + PushError(&Errors, S_ERROR, 0, S->Pairs[i].Key); + } + } break; + case IDENT_CSS_PATH: + { + Result->CSSDir = StripSlashes(ResolveString(Result, &Errors, S, &S->Pairs[i], FALSE), P_REL); + if(Result->CSSDir.Length > MAX_RELATIVE_ASSET_LOCATION_LENGTH) + { + ConfigErrorSizing(S->Pairs[i].Position.Filename, S->Pairs[i].Position.LineNumber, S->Pairs[i].Key, + &Result->CSSDir, MAX_RELATIVE_ASSET_LOCATION_LENGTH); + PushError(&Errors, S_ERROR, 0, S->Pairs[i].Key); + } + } break; + case IDENT_IMAGES_PATH: + { + Result->ImagesDir = StripSlashes(ResolveString(Result, &Errors, S, &S->Pairs[i], FALSE), P_REL); + if(Result->ImagesDir.Length > MAX_RELATIVE_ASSET_LOCATION_LENGTH) + { + ConfigErrorSizing(S->Pairs[i].Position.Filename, S->Pairs[i].Position.LineNumber, S->Pairs[i].Key, + &Result->ImagesDir, MAX_RELATIVE_ASSET_LOCATION_LENGTH); + PushError(&Errors, S_ERROR, 0, S->Pairs[i].Key); + } + } break; + case IDENT_JS_PATH: + { + Result->JSDir = StripSlashes(ResolveString(Result, &Errors, S, &S->Pairs[i], FALSE), P_REL); + if(Result->JSDir.Length > MAX_RELATIVE_ASSET_LOCATION_LENGTH) + { + ConfigErrorSizing(S->Pairs[i].Position.Filename, S->Pairs[i].Position.LineNumber, S->Pairs[i].Key, + &Result->JSDir, MAX_RELATIVE_ASSET_LOCATION_LENGTH); + PushError(&Errors, S_ERROR, 0, S->Pairs[i].Key); + } + } break; + case IDENT_QUERY_STRING: + { Result->QueryString = ResolveString(Result, &Errors, S, &S->Pairs[i], FALSE); } break; + case IDENT_CACHE_DIR: + { Result->CacheDir = StripSlashes(ResolveString(Result, &Errors, S, &S->Pairs[i], TRUE), P_ABS); } break; + default: break; + } + } + + int SecondsPerMinute = 60; + for(int i = 0; i < S->IntPairCount; ++i) + { + switch(S->IntPairs[i].Key) + { + case IDENT_PRIVACY_CHECK_INTERVAL: + { Result->PrivacyCheckInterval = SecondsPerMinute * S->IntPairs[i].Value; } break; + case IDENT_LOG_LEVEL: + { Result->LogLevel = S->IntPairs[i].Value; } break; + default: break; + } + } + + for(int i = 0; i < S->TreeCount; ++i) + { + if(S->Trees[i].ID.Key == IDENT_PERSON) + { + PushPersonOntoConfig(Result, &Errors, &Verifiers, &S->Trees[i]); + } + } + + for(int i = 0; i < S->TreeCount; ++i) + { + if(S->Trees[i].ID.Key == IDENT_PROJECT) + { + PushProjectOntoConfig(Result, &Errors, &Verifiers, &S->Trees[i]); + } + } + + // TODO(matt): Mandate that, if a GlobalSearchDir or GlobalSearchURL is set, then both must be set + if(!Result->GlobalTheme.Length) + { + ConfigErrorUnset(IDENT_GLOBAL_THEME); + PushError(&Errors, S_ERROR, 0, IDENT_GLOBAL_THEME); + } + + if(Errors.ErrorCount > 0) + { + PrintAssociationClashes(&Verifiers.HMMLDirs); + PrintVariantInconsistencies(&Verifiers.VariantStrings); + Free(Result); + } + + FreeVerifiers(&Verifiers); + FreeErrors(&Errors); + + return Result; +} + +void +PrintLogLevel(char *Title, log_level Level, char *Delimiter, uint64_t Indentation, bool AppendNewline) +{ + Indent(Indentation); + PrintC(CS_YELLOW_BOLD, ConfigIdentifiers[IDENT_LOG_LEVEL].String); + fprintf(stderr, "%s", Delimiter); + PrintC(CS_GREEN_BOLD, LogLevelStrings[Level]); + if(AppendNewline) { fprintf(stderr, "\n"); } +} + +typedef struct +{ + char *UpperLeftCorner; + char *UpperLeft; + char *Horizontal; + char *UpperRight; + char *Vertical; + char *LowerLeftCorner; + char *LowerLeft; + char *Margin; + char *Delimiter; + char *Separator; +} typography; + +void +PrintVerticals(typography *T, int Count) +{ + for(int i = 0; i < Count; ++i) + { + fprintf(stderr, "%s", T->Vertical); + } +} + +void +CarriageReturn(typography *T, int VerticalsRequired) +{ + fprintf(stderr, "\n"); + PrintVerticals(T, VerticalsRequired); +} + +uint64_t +TypesetIconType(typography *T, uint8_t Generation, icon_type Type, bool PrependNewline, bool AppendNewline) +{ + uint64_t Result = 0; + + // TODO(matt): Implement me, please! + + //CarriageReturn(T, Generation); + //fprintf(stderr, "%s", T.Margin); +#if 0 + PrintC(CS_YELLOW_BOLD, ConfigIdentifiers[IDENT_ICON_TYPE].String); + fprintf(stderr, "%s", T.Delimiter); + PrintC(CS_GREEN_BOLD, IconTypeStrings[Type]); + fprintf(stderr, "\n"); +#endif + + + + + + + if(PrependNewline) + { + CarriageReturn(T, Generation); + ++Result; + } + + fprintf(stderr, "%s", T->Margin); + config_identifier_id Key = IDENT_ICON_TYPE; + PrintC(CS_YELLOW_BOLD, ConfigIdentifiers[Key].String); + fprintf(stderr, "%s", T->Delimiter); + if(Type == IT_DEFAULT_UNSET) + { + PrintC(CS_YELLOW, "[unset]"); + } + else + { + PrintStringC(CS_GREEN_BOLD, Wrap0(IconTypeStrings[Type])); + } + + if(AppendNewline) + { + CarriageReturn(T, Generation); + ++Result; + } + + return Result; +} + +void +TypesetVariants(typography *T, uint8_t Generation, config_identifier_id Key, uint64_t Value, int AvailableColumns, uint8_t *RowsRequired, bool PrependNewline, bool AppendNewline) +{ + // NOTE(matt): RowsRequired is a value passed only by PrintSupport(). + if(PrependNewline) + { + CarriageReturn(T, Generation); + if(RowsRequired) { --*RowsRequired; } + } + + if(RowsRequired) + { + fprintf(stderr, "%s%s ", *RowsRequired == 1 ? T->LowerLeft : T->Vertical, T->Margin); + } + + fprintf(stderr, "%s", T->Margin); + + int CharactersInKeyPlusDelimiter = StringLength(ConfigIdentifiers[Key].String) + StringLength(T->Delimiter); + int AvailableColumnsForValue = AvailableColumns - CharactersInKeyPlusDelimiter; + int RunningAvailableColumns = AvailableColumnsForValue; + + PrintC(CS_YELLOW_BOLD, ConfigIdentifiers[Key].String); + fprintf(stderr, "%s", T->Delimiter); + + bool NeedsSpacing = FALSE; + if(Value) + { + for(int VariantShifterIndex = 0; VariantShifterIndex < AVS_COUNT; ++VariantShifterIndex) + { + if(Value & (1 << VariantShifterIndex)) + { + char *String = ArtVariantStrings[VariantShifterIndex]; + int Length = StringLength(String); + if(RunningAvailableColumns - Length >= 0) + { + fprintf(stderr, "%s", NeedsSpacing ? " " : ""); + } + else + { + CarriageReturn(T, Generation); + if(RowsRequired) + { + --*RowsRequired; + fprintf(stderr, "%s%s ", *RowsRequired == 1 ? T->LowerLeft : T->Vertical, T->Margin); + } + fprintf(stderr, "%s", T->Margin); + for(int i = 0; i < CharactersInKeyPlusDelimiter; ++i) + { + fprintf(stderr, "%s", T->Margin); + } + NeedsSpacing = FALSE; + RunningAvailableColumns = AvailableColumnsForValue; + } + PrintC(CS_GREEN_BOLD, String); + RunningAvailableColumns -= Length + NeedsSpacing; + NeedsSpacing = TRUE; + } + } + } + else + { + PrintC(CS_YELLOW, "[unset]"); + } + + if(AppendNewline) + { + CarriageReturn(T, Generation); + } +} + +void +PrintMedium(typography *T, medium *M, char *Delimiter, uint64_t Indentation, bool AppendNewline) +{ + // TODO(matt): Print Me! + PrintStringC(CS_BLUE_BOLD, M->ID); + if(M->Hidden) + { + PrintC(CS_BLACK_BOLD, " [hidden]"); + } + fprintf(stderr, "%s", Delimiter); + PrintStringC(CS_MAGENTA, M->Name); + fprintf(stderr, " ("); + if(M->Icon.Length > 0) + { + PrintStringI(M->Icon, Indentation); + } + if(M->IconNormal.Length > 0) + { + PrintStringI(M->IconNormal, Indentation); + } + if(M->IconFocused.Length > 0) + { + PrintStringI(M->IconFocused, Indentation); + } + TypesetIconType(T, Indentation, M->IconType, FALSE, AppendNewline); + TypesetVariants(T, Indentation, IDENT_ICON_VARIANTS, M->IconVariants, 0, 0, FALSE, AppendNewline); + fprintf(stderr, ")"); + if(AppendNewline) + { + fprintf(stderr, "\n"); + } +} + +void +PrintSupport(support *S, typography *Typography, int IndentationLevel, uint8_t *RowsRequired) +{ + bool ShouldFillSyntax = FALSE; + config_pair ID = { .Key = IDENT_SUPPORT, .Value = S->ID }; + fprintf(stderr, "%s%s", *RowsRequired == 1 ? Typography->LowerLeft : Typography->Vertical, Typography->Margin); + PrintPair(&ID, Typography->Delimiter, ShouldFillSyntax, IndentationLevel, FALSE, TRUE); + --*RowsRequired; + if(S->URL.Length > 0) + { + config_pair URL = { .Key = IDENT_URL, .Value = S->URL }; + fprintf(stderr, "%s%s ", *RowsRequired == 1 ? Typography->LowerLeft : Typography->Vertical, Typography->Margin); + PrintPair(&URL, Typography->Delimiter, ShouldFillSyntax, IndentationLevel, FALSE, TRUE); + --*RowsRequired; + } + + if(S->Icon.Length > 0) + { + config_pair Icon = { .Key = IDENT_ICON, .Value = S->Icon }; + fprintf(stderr, "%s%s ", *RowsRequired == 1 ? Typography->LowerLeft : Typography->Vertical, Typography->Margin); + PrintPair(&Icon, Typography->Delimiter, ShouldFillSyntax, IndentationLevel, FALSE, TRUE); + --*RowsRequired; + } + if(S->IconNormal.Length > 0) + { + config_pair IconNormal = { .Key = IDENT_ICON_NORMAL, .Value = S->IconNormal }; + fprintf(stderr, "%s%s ", *RowsRequired == 1 ? Typography->LowerLeft : Typography->Vertical, Typography->Margin); + PrintPair(&IconNormal, Typography->Delimiter, ShouldFillSyntax, IndentationLevel, FALSE, TRUE); + --*RowsRequired; + } + if(S->IconFocused.Length > 0) + { + config_pair IconFocused = { .Key = IDENT_ICON_FOCUSED, .Value = S->IconFocused }; + fprintf(stderr, "%s%s ", *RowsRequired == 1 ? Typography->LowerLeft : Typography->Vertical, Typography->Margin); + PrintPair(&IconFocused, Typography->Delimiter, ShouldFillSyntax, IndentationLevel, FALSE, TRUE); + --*RowsRequired; + } + +#if 1 + // TODO(matt): FIX ME + fprintf(stderr, "%s%s ", *RowsRequired == 1 ? Typography->LowerLeft : Typography->Vertical, Typography->Margin); + *RowsRequired -= TypesetIconType(Typography, IndentationLevel, S->IconType, FALSE, TRUE); + uint8_t StartingColumn = 4; + uint64_t AvailableColumns = GetTerminalColumns() - StartingColumn; + TypesetVariants(Typography, IndentationLevel, IDENT_ICON_VARIANTS, S->IconVariants, AvailableColumns, RowsRequired, FALSE, TRUE); +#endif +} + +uint8_t +GetRowsRequiredForVariants(typography *T, config_identifier_id Key, uint64_t Value, int AvailableColumns) +{ + int CharactersInKeyPlusDelimiter = StringLength(ConfigIdentifiers[Key].String) + StringLength(T->Delimiter); + int AvailableColumnsForValue = AvailableColumns - CharactersInKeyPlusDelimiter; + int RunningAvailableColumns = AvailableColumnsForValue; + + uint8_t Result = 1; + bool NeedsSpacing = FALSE; + for(int VariantShifterIndex = 0; VariantShifterIndex < AVS_COUNT; ++VariantShifterIndex) + { + if(Value & (1 << VariantShifterIndex)) + { + char *String = ArtVariantStrings[VariantShifterIndex]; + int Length = StringLength(String); + if(!(RunningAvailableColumns - Length >= 0)) + { + ++Result; + NeedsSpacing = FALSE; + RunningAvailableColumns = AvailableColumnsForValue; + } + RunningAvailableColumns -= Length + NeedsSpacing; + NeedsSpacing = TRUE; + } + } + return Result; +} + +uint8_t +GetRowsRequiredForPersonInfo(typography *T, person *P) +{ + uint8_t RowsRequired = 0; + + if(P->Name.Length > 0) { ++RowsRequired; } + if(P->Homepage.Length > 0) { ++RowsRequired; } + for(int i = 0; i < P->SupportCount; ++i) + { + ++RowsRequired; + support *This = P->Support + i; + if(This->URL.Length > 0) { ++RowsRequired; } + if(This->Icon.Length > 0) { ++RowsRequired; } + if(This->IconNormal.Length > 0) { ++RowsRequired; } + if(This->IconFocused.Length > 0) { ++RowsRequired; } + ++RowsRequired; // NOTE(matt): For TypesetIconType() + int StartingColumn = 4; + RowsRequired += GetRowsRequiredForVariants(T, IDENT_ICON_VARIANTS, This->IconVariants, GetTerminalColumns() - StartingColumn); + } + return RowsRequired; +} + +void +PrintPerson(person *P, config_identifier_id Role, typography *Typography) +{ + bool HaveInfo = P->Name.Length > 0 || P->Homepage.Length > 0 || P->SupportCount > 0; + fprintf(stderr, "\n" + "%s%s%s%s%s ", HaveInfo ? Typography->UpperLeftCorner : Typography->UpperLeft, + Typography->Horizontal, Typography->Horizontal, Typography->Horizontal, Typography->UpperRight); + PrintStringC(CS_GREEN_BOLD, P->ID); + fprintf(stderr, "\n"); + + uint8_t RowsRequired = GetRowsRequiredForPersonInfo(Typography, P); + + int IndentationLevel = 0; + bool ShouldFillSyntax = FALSE; + if(P->Name.Length > 0) + { + fprintf(stderr, "%s ", RowsRequired == 1 ? Typography->LowerLeft : Typography->Vertical); + config_pair Name = { .Key = IDENT_NAME, .Value = P->Name }; PrintPair(&Name, Typography->Delimiter, ShouldFillSyntax, IndentationLevel, FALSE, TRUE); + --RowsRequired; + } + + if(P->Homepage.Length > 0) + { + fprintf(stderr, "%s ", RowsRequired == 1 ? Typography->LowerLeft : Typography->Vertical); + config_pair Homepage = { .Key = IDENT_HOMEPAGE, .Value = P->Homepage }; PrintPair(&Homepage, Typography->Delimiter, ShouldFillSyntax, IndentationLevel, FALSE, TRUE); + --RowsRequired; + } + + for(int i = 0; i < P->SupportCount; ++i) + { + PrintSupport(&P->Support[i], Typography, IndentationLevel, &RowsRequired); + } +} + +void +PrintLineage(string Lineage, bool AppendNewline) +{ + string Parent = StripComponentFromPath(Lineage); + PrintStringC(CS_BLACK_BOLD, Parent); + if(Parent.Length > 0) + { + PrintC(CS_BLACK_BOLD, "/"); + } + string Us = GetFinalComponent(Lineage); + PrintStringC(CS_BLUE_BOLD, Us); + if(AppendNewline) { fprintf(stderr, "\n"); } +} + +uint8_t +GetColumnsRequiredForMedium(medium *M, typography *T) +{ + uint8_t Result = M->ID.Length + StringLength(T->Delimiter) + M->Name.Length + StringLength(" (") + M->Icon.Length + StringLength(")"); + if(M->Hidden) { Result += StringLength(" [hidden]"); } + return Result; +} + +void +PrintTitle(char *Text, int AvailableColumns) +{ + char *LeftmostChar = "╾"; + char *InnerChar = "─"; + char *RightmostChar = "╼"; + + int LeftmostCharLength = 1; + int SpacesRequired = 2; + int RightmostCharLength = 1; + int InnerCharsRequired = AvailableColumns - LeftmostCharLength - StringLength(Text) - RightmostCharLength - SpacesRequired; + int LeftCharsRequired = 4; + int RightCharsRequired = InnerCharsRequired - LeftCharsRequired; + fprintf(stderr, "%s", LeftmostChar); + for(int i = 0; i < LeftCharsRequired; ++i) + { + fprintf(stderr, "%s", InnerChar); + } + fprintf(stderr, " %s ", Text); + for(int i = 0; i < RightCharsRequired; ++i) + { + fprintf(stderr, "%s", InnerChar); + } + fprintf(stderr, "%s", RightmostChar); +} + +void +PrintMedia(project *P, typography *T, uint8_t Generation, int AvailableColumns) +{ + int IndentationLevel = 0; + CarriageReturn(T, Generation); + fprintf(stderr, "%s", T->Margin); + PrintTitle("Media", AvailableColumns); + CarriageReturn(T, Generation); + fprintf(stderr, "%s", T->Margin); + //AvailableColumns -= Generation + 1; + int RunningAvailableColumns = AvailableColumns; + int ColumnsRequired = GetColumnsRequiredForMedium(&P->Medium[0], T); + if(ColumnsRequired <= RunningAvailableColumns) + { + PrintMedium(T, &P->Medium[0], T->Delimiter, IndentationLevel, FALSE); + } + RunningAvailableColumns -= ColumnsRequired; + int StringLengthOfSpacePlusSeparator = 2; + for(int i = 1; i < P->MediumCount; ++i) + { + medium *This = P->Medium + i; + ColumnsRequired = GetColumnsRequiredForMedium(This, T); + if(RunningAvailableColumns >= StringLengthOfSpacePlusSeparator) + { + fprintf(stderr, " %s", T->Separator); + RunningAvailableColumns -= StringLengthOfSpacePlusSeparator; + + if(ColumnsRequired + 1 <= RunningAvailableColumns) + { + fprintf(stderr, " "); + --RunningAvailableColumns; + PrintMedium(T, This, T->Delimiter, IndentationLevel, FALSE); + } + else + { + CarriageReturn(T, Generation); + fprintf(stderr, "%s", T->Margin); + RunningAvailableColumns = AvailableColumns; + PrintMedium(T, This, T->Delimiter, IndentationLevel, FALSE); + } + } + else + { + CarriageReturn(T, Generation); + fprintf(stderr, "%s", T->Margin); + RunningAvailableColumns = AvailableColumns; + fprintf(stderr, "%s ", T->Separator); + RunningAvailableColumns -= StringLengthOfSpacePlusSeparator; + PrintMedium(T, This, T->Delimiter, IndentationLevel, FALSE); + } + RunningAvailableColumns -= ColumnsRequired; + } +} + +void +TypesetPair(typography *T, uint8_t Generation, config_identifier_id Key, string Value, int AvailableColumns) +{ + CarriageReturn(T, Generation); + fprintf(stderr, "%s", T->Margin); + + int CharactersInKeyPlusDelimiter = StringLength(ConfigIdentifiers[Key].String) + StringLength(T->Delimiter); + int AvailableColumnsForValue = AvailableColumns - CharactersInKeyPlusDelimiter; + + PrintC(CS_YELLOW_BOLD, ConfigIdentifiers[Key].String); + fprintf(stderr, "%s", T->Delimiter); + + int CharactersToWrite = Value.Length; + if(Value.Length > 0) + { + string Pen = { .Base = Value.Base + Value.Length - CharactersToWrite, + .Length = MIN(Value.Length, AvailableColumnsForValue) }; + PrintStringC(CS_GREEN_BOLD, Pen); + CharactersToWrite -= Pen.Length; + while(CharactersToWrite > 0) + { + CarriageReturn(T, Generation); + fprintf(stderr, "%s", T->Margin); + for(int i = 0; i < CharactersInKeyPlusDelimiter; ++i) + { + fprintf(stderr, "%s", T->Margin); + } + + string Pen = { .Base = Value.Base + Value.Length - CharactersToWrite, + .Length = MIN(CharactersToWrite, AvailableColumnsForValue) }; + PrintStringC(CS_GREEN_BOLD, Pen); + CharactersToWrite -= Pen.Length; + } + } + else + { + PrintC(CS_YELLOW, "[unset]"); + } +} + +void +TypesetBool(typography *T, uint8_t Generation, config_identifier_id Key, bool Value) +{ + CarriageReturn(T, Generation); + fprintf(stderr, "%s", T->Margin); + config_bool_pair Pair = { .Key = Key, .Value = Value }; + bool ShouldFillSyntax = FALSE; + PrintBoolPair(&Pair, T->Delimiter, ShouldFillSyntax, 0, FALSE, FALSE); +} + +void +TypesetGenre(typography *T, uint8_t Generation, genre G) +{ + CarriageReturn(T, Generation); + fprintf(stderr, "%s", T->Margin); + PrintC(CS_YELLOW_BOLD, ConfigIdentifiers[IDENT_GENRE].String); + fprintf(stderr, "%s", T->Delimiter); + PrintC(CS_GREEN_BOLD, GenreStrings[G]); +} + +void +TypesetNumberingScheme(typography *T, uint8_t Generation, numbering_scheme N) +{ + CarriageReturn(T, Generation); + fprintf(stderr, "%s", T->Margin); + PrintC(CS_YELLOW_BOLD, ConfigIdentifiers[IDENT_NUMBERING_SCHEME].String); + fprintf(stderr, "%s", T-> Delimiter); + PrintC(CS_GREEN_BOLD, NumberingSchemeStrings[N]); +} + +void +PrintProject(project *P, typography *T, int Ancestors, int IndentationLevel, int TerminalColumns) +{ + int Generation = Ancestors + 1; + CarriageReturn(T, Ancestors); + + fprintf(stderr, "%s%s%s%s%s ", T->UpperLeftCorner, + T->Horizontal, T->Horizontal, T->Horizontal, T->UpperRight); + PrintLineage(P->Lineage, FALSE); + + int CharactersInMargin = 1; + int AvailableColumns = TerminalColumns - (Generation + CharactersInMargin); + if(P->MediumCount > 0) + { + PrintMedia(P, T, Generation, AvailableColumns); + } + + CarriageReturn(T, Generation); + CarriageReturn(T, Generation); + fprintf(stderr, "%s", T->Margin); + PrintTitle("Settings", AvailableColumns); + + TypesetPair(T, Generation, IDENT_TITLE, P->Title, AvailableColumns); + TypesetPair(T, Generation, IDENT_HTML_TITLE, P->HTMLTitle, AvailableColumns); + TypesetPair(T, Generation, IDENT_DEFAULT_MEDIUM, P->DefaultMedium ? P->DefaultMedium->ID : EmptyString(), AvailableColumns); + + TypesetPair(T, Generation, IDENT_UNIT, P->Unit, AvailableColumns); + TypesetPair(T, Generation, IDENT_HMML_DIR, P->HMMLDir, AvailableColumns); + TypesetPair(T, Generation, IDENT_TEMPLATES_DIR, P->TemplatesDir, AvailableColumns); + TypesetPair(T, Generation, IDENT_SEARCH_TEMPLATE, P->SearchTemplatePath, AvailableColumns); + TypesetPair(T, Generation, IDENT_PLAYER_TEMPLATE, P->PlayerTemplatePath, AvailableColumns); + + CarriageReturn(T, Generation); + TypesetPair(T, Generation, IDENT_BASE_DIR, P->BaseDir, AvailableColumns); + TypesetPair(T, Generation, IDENT_BASE_URL, P->BaseURL, AvailableColumns); + TypesetPair(T, Generation, IDENT_SEARCH_LOCATION, P->SearchLocation, AvailableColumns); + TypesetPair(T, Generation, IDENT_PLAYER_LOCATION, P->PlayerLocation, AvailableColumns); + TypesetPair(T, Generation, IDENT_STREAM_USERNAME, P->StreamUsername, AvailableColumns); + TypesetPair(T, Generation, IDENT_VOD_PLATFORM, P->VODPlatform, AvailableColumns); + TypesetPair(T, Generation, IDENT_THEME, P->Theme, AvailableColumns); + + CarriageReturn(T, Generation); + TypesetPair(T, Generation, IDENT_ART, P->Art, AvailableColumns); + TypesetVariants(T, Generation, IDENT_ART_VARIANTS, P->ArtVariants, AvailableColumns, 0, TRUE, FALSE); + TypesetPair(T, Generation, IDENT_ICON, P->Icon, AvailableColumns); + TypesetPair(T, Generation, IDENT_ICON_NORMAL, P->IconNormal, AvailableColumns); + TypesetPair(T, Generation, IDENT_ICON_FOCUSED, P->IconFocused, AvailableColumns); + TypesetPair(T, Generation, IDENT_ICON_DISABLED, P->IconDisabled, AvailableColumns); + TypesetIconType(T, Generation, P->IconType, TRUE, FALSE); + TypesetVariants(T, Generation, IDENT_ICON_VARIANTS, P->IconVariants, AvailableColumns, 0, TRUE, FALSE); + + CarriageReturn(T, Generation); + TypesetGenre(T, Generation, P->Genre); + TypesetNumberingScheme(T, Generation, P->NumberingScheme); + TypesetBool(T, Generation, IDENT_IGNORE_PRIVACY, P->IgnorePrivacy); + TypesetBool(T, Generation, IDENT_SINGLE_BROWSER_TAB, P->SingleBrowserTab); + + if(P->Owner) + { + TypesetPair(T, Generation, IDENT_OWNER, P->Owner->ID, AvailableColumns); + } + + for(int i = 0; i < P->IndexerCount; ++i) + { TypesetPair(T, Generation, IDENT_INDEXER, P->Indexer[i]->ID, AvailableColumns); } + for(int i = 0; i < P->CoHostCount; ++i) + { TypesetPair(T, Generation, IDENT_COHOST, P->CoHost[i]->ID, AvailableColumns); } + for(int i = 0; i < P->GuestCount; ++i) + { TypesetPair(T, Generation, IDENT_GUEST, P->Guest[i]->ID, AvailableColumns); } + + if(P->ChildCount) + { + CarriageReturn(T, Generation); + CarriageReturn(T, Generation); + fprintf(stderr, "%s", T->Margin); + PrintTitle("Children", AvailableColumns); + ++Ancestors; + for(int i = 0; i < P->ChildCount; ++i) + { + PrintProject(&P->Child[i], T, Ancestors, IndentationLevel, TerminalColumns); + } + --Ancestors; + } + CarriageReturn(T, Ancestors); + fprintf(stderr, "%s%s%s%s%s ", T->LowerLeftCorner, + T->Horizontal, T->Horizontal, T->Horizontal, T->UpperRight); + PrintLineage(P->Lineage, FALSE); +} + +void +PrintConfig(config *C, bool ShouldClearTerminal) +{ + if(C) + { + int TermCols = GetTerminalColumns(); + if(ShouldClearTerminal) { ClearTerminal(); } + typography Typography = + { + .UpperLeftCorner = "┌", + .UpperLeft = "╾", + .Horizontal = "─", + .UpperRight = "╼", + .Vertical = "│", + .LowerLeftCorner = "└", + .LowerLeft = "╽", + .Margin = " ", + .Delimiter = ": ", + .Separator = "•", + }; + + // separate + fprintf(stderr, "\n"); + PrintTitle("Global Settings", TermCols); + int IndentationLevel = 0; + + int AvailableColumns = TermCols - StringLength(Typography.Margin); + TypesetPair(&Typography, 0, IDENT_DB_LOCATION, C->DatabaseLocation, AvailableColumns); + TypesetPair(&Typography, 0, IDENT_CACHE_DIR, C->CacheDir, AvailableColumns); + + fprintf(stderr, "\n"); + TypesetPair(&Typography, 0, IDENT_GLOBAL_TEMPLATES_DIR, C->GlobalTemplatesDir, AvailableColumns); + TypesetPair(&Typography, 0, IDENT_GLOBAL_SEARCH_TEMPLATE, C->GlobalSearchTemplatePath, AvailableColumns); + TypesetPair(&Typography, 0, IDENT_GLOBAL_SEARCH_DIR, C->GlobalSearchDir, AvailableColumns); + TypesetPair(&Typography, 0, IDENT_GLOBAL_SEARCH_URL, C->GlobalSearchURL, AvailableColumns); + TypesetPair(&Typography, 0, IDENT_GLOBAL_THEME, C->GlobalTheme, AvailableColumns); + + fprintf(stderr, "\n"); + TypesetPair(&Typography, 0, IDENT_ASSETS_ROOT_DIR, C->AssetsRootDir, AvailableColumns); + TypesetPair(&Typography, 0, IDENT_ASSETS_ROOT_URL, C->AssetsRootURL, AvailableColumns); + TypesetPair(&Typography, 0, IDENT_CSS_PATH, C->CSSDir, AvailableColumns); + TypesetPair(&Typography, 0, IDENT_IMAGES_PATH, C->ImagesDir, AvailableColumns); + TypesetPair(&Typography, 0, IDENT_JS_PATH, C->JSDir, AvailableColumns); + TypesetPair(&Typography, 0, IDENT_QUERY_STRING, C->QueryString, AvailableColumns); + + fprintf(stderr, "\n" + "\n" + "%s", Typography.Margin); + bool ShouldFillSyntax = FALSE; + PrintBool("Respecting Privacy (derived from projects)", C->RespectingPrivacy, Typography.Delimiter, ShouldFillSyntax, IndentationLevel, FALSE, TRUE); + fprintf(stderr, "%s", Typography.Margin); + int SecondsPerMinute = 60; + config_int_pair PrivacyCheckInterval = { .Key = IDENT_PRIVACY_CHECK_INTERVAL, .Value = C->PrivacyCheckInterval / SecondsPerMinute }; PrintIntPair(&PrivacyCheckInterval, Typography.Delimiter, ShouldFillSyntax, IndentationLevel, FALSE, TRUE); + fprintf(stderr, "%s", Typography.Margin); + PrintLogLevel(ConfigIdentifiers[IDENT_LOG_LEVEL].String, C->LogLevel, Typography.Delimiter, IndentationLevel, TRUE); + + fprintf(stderr, "\n"); + PrintTitle("People", TermCols); + for(int i = 0; i < C->PersonCount; ++i) + { + PrintPerson(&C->Person[i], IDENT_NULL, &Typography); + } + + fprintf(stderr, "\n"); + PrintTitle("Projects", TermCols); + for(int i = 0; i < C->ProjectCount; ++i) + { + PrintProject(&C->Project[i], &Typography, 0, IndentationLevel, TermCols); + } + } +} + +void +FreePerson(person *P) +{ + FreeAndResetCount(P->Support, P->SupportCount); +} + +void +FreeProject(project *P) +{ + FreeAndResetCount(P->Medium, P->MediumCount); + + FreeAndResetCount(P->Indexer, P->IndexerCount); + FreeAndResetCount(P->Guest, P->GuestCount); + FreeAndResetCount(P->CoHost, P->CoHostCount); + for(int i = 0; i < P->ChildCount; ++i) + { + FreeProject(&P->Child[i]); + } + FreeAndResetCount(P->Child, P->ChildCount); + FreeTemplate(&P->SearchTemplate); + FreeTemplate(&P->PlayerTemplate); + + FreeBuffer(&P->NavGeneric); + FreeBuffer(&P->NavDropdownPre); + FreeBuffer(&P->NavDropdownPost); + FreeBuffer(&P->NavHorizontalPre); + FreeBuffer(&P->NavPlainPre); +} + +void +FreeConfig(config *C) +{ + if(C) + { + for(int i = 0; i < C->PersonCount; ++i) + { + FreePerson(&C->Person[i]); + } + FreeAndResetCount(C->Person, C->PersonCount); + for(int i = 0; i < C->ProjectCount; ++i) + { + FreeProject(&C->Project[i]); + } + FreeAndResetCount(C->Project, C->ProjectCount); + FreeBook(&C->ResolvedVariables); + FreeTemplate(&C->SearchTemplate); + + FreeBuffer(&C->NavGeneric); + FreeBuffer(&C->NavDropdownPre); + FreeBuffer(&C->NavDropdownPost); + FreeBuffer(&C->NavHorizontalPre); + FreeBuffer(&C->NavPlainPre); + + Free(C); + } +} + +config * +ParseConfig(string Path, tokens_list *TokensList) +{ + //PrintFunctionName("ParseConfig()"); + config_type_specs TypeSpecs = InitTypeSpecs(); + config *Result = 0; + tokens *T = Tokenise(TokensList, Path); + +#if 0 + MEM_LOOP_PRE_FREE("Tokenise") + FreeTokensList(&TokensList); + //FreeAndResetCount(WatchHandles.Handle, WatchHandles.Count); + MEM_LOOP_PRE_WORK() + T = Tokenise(&TokensList, Path); + MEM_LOOP_POST("Tokenise") +#endif + + if(T) + { + scope_tree *ScopeTree = calloc(1, sizeof(scope_tree)); + SetTypeSpec(ScopeTree, &TypeSpecs); + SetDefaults(ScopeTree, &TypeSpecs); + ScopeTree = ScopeTokens(ScopeTree, TokensList, T, &TypeSpecs, 0); + + // TODO(matt): Mem testing + // +#if 0 + MEM_LOOP_PRE_FREE("ScopeTree") + FreeScopeTree(ScopeTree); + FreeTokensList(&TokensList); + MEM_LOOP_PRE_WORK() + T = Tokenise(&TokensList, Path); + ScopeTree = calloc(1, sizeof(scope_tree)); + SetTypeSpec(ScopeTree, &TypeSpecs); + SetDefaults(ScopeTree, &TypeSpecs); + ScopeTree = ScopeTokens(ScopeTree, &TokensList, T, &TypeSpecs, 0); + MEM_LOOP_POST("ScopeTree") +#endif + // + //// + + //PrintScopeTree(ScopeTree); + if(ScopeTree) + { + //PrintC(CS_GREEN, "Resolving scope tree\n"); + Result = ResolveVariables(ScopeTree); + +#if 0 + MEM_LOOP_PRE_FREE("ResolveVariables") + FreeConfig(Result); + MEM_LOOP_PRE_WORK() + Result = ResolveVariables(ScopeTree); + MEM_LOOP_POST("ResolveVariables") +#endif + + FreeScopeTree(ScopeTree); + } + FreeTokensList(TokensList); + } + FreeTypeSpecs(&TypeSpecs); + return Result; +} +// +// config + +#if 0 +int +main(int ArgC, char **Args) +{ + //fprintf(stderr, "%s\n", ConfigFile.Buffer.Location); + config_type_specs TypeSpecs = InitTypeSpecs(); + + config *Config = ParseConfig(&TypeSpecs, Wrap0("cinera.conf")); + if(Config) + { + //PrintConfig(Config); + FreeConfig(Config); + } + + FreeTypeSpecs(&TypeSpecs); + _exit(0); +} +#endif diff --git a/cinera/cinera_player_post.js b/cinera/cinera_player_post.js index 63b33f2..7e3a200 100644 --- a/cinera/cinera_player_post.js +++ b/cinera/cinera_player_post.js @@ -211,31 +211,24 @@ if(creditsMenu) if(this != lastFocusedCreditItem) { lastFocusedCreditItem.classList.remove("focused"); + unfocusSprite(lastFocusedCreditItem); if(lastFocusedCreditItem.classList.contains("support")) { - setIconLightness(lastFocusedCreditItem.firstChild); + setSpriteLightness(lastFocusedCreditItem.firstChild); } lastFocusedCreditItem = this; focusedElement = lastFocusedCreditItem; focusedElement.classList.add("focused"); + focusSprite(focusedElement); if(focusedElement.classList.contains("support")) { - setIconLightness(focusedElement.firstChild); + setSpriteLightness(focusedElement.firstChild); } } }) } - - var supportIcons = creditsMenu.querySelectorAll(".support_icon"); - { - for(var i = 0; i < supportIcons.length; ++i) - { - supportIcons[i].style.backgroundImage = "url(\"" + supportIcons[i].getAttribute("data-sprite") + "\")"; - setIconLightness(supportIcons[i]); - } - } - } + var sourceMenus = titleBar.querySelectorAll(".menu"); var helpButton = titleBar.querySelector(".help"); @@ -319,3 +312,495 @@ else if(lastAnnotation = localStorage.getItem(lastAnnotationStorageItem)) { player.setTime(lastAnnotation); } + +function handleKey(key) { + var gotKey = true; + switch (key) { + case "q": { + if(quotesMenu) + { + toggleMenuVisibility(quotesMenu) + } + } break; + case "r": { + if(referencesMenu) + { + toggleMenuVisibility(referencesMenu) + } + } break; + case "f": { + if(filterMenu) + { + toggleMenuVisibility(filterMenu) + } + } break; + case "y": { + if(linkMenu) + { + toggleMenuVisibility(linkMenu) + } + break; + } + case "c": { + if(creditsMenu) + { + toggleMenuVisibility(creditsMenu) + } + } break; + case "t": { + if(cinera) + { + toggleTheatreMode(); + } + } break; + case "T": { + if(cinera) + { + toggleSuperTheatreMode(); + } + } break; + + case "Enter": { + if(focusedElement) + { + if(focusedElement.parentNode.classList.contains("quotes_container")) + { + var time = focusedElement.querySelector(".timecode").getAttribute("data-timestamp"); + player.setTime(parseInt(time, 10)); + player.play(); + } + else if(focusedElement.parentNode.classList.contains("references_container")) + { + var time = focusedIdentifier.getAttribute("data-timestamp"); + player.setTime(parseInt(time, 10)); + player.play(); + } + else if(focusedElement.parentNode.classList.contains("credit")) + { + if(focusedElement.hasAttribute) + { + var url = focusedElement.getAttribute("href"); + window.open(url, "_blank"); + } + } + } + else + { + console.log("TODO(matt): Implement me, perhaps?\n"); + } + } break; + + case "o": { + if(focusedElement) + { + if(focusedElement.parentNode.classList.contains("references_container") || + focusedElement.parentNode.classList.contains("quotes_container")) + { + var url = focusedElement.getAttribute("href"); + window.open(url, "_blank"); + } + else if(focusedElement.parentNode.classList.contains("credit")) + { + if(focusedElement.hasAttribute("href")) + { + var url = focusedElement.getAttribute("href"); + window.open(url, "_blank"); + } + } + } + } break; + + case "w": case "k": case "ArrowUp": { + if(focusedElement) + { + if(focusedElement.parentNode.classList.contains("quotes_container")) + { + if(focusedElement.previousElementSibling) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + + lastFocusedQuote = focusedElement.previousElementSibling; + focusedElement = lastFocusedQuote; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + } + else if(focusedElement.parentNode.classList.contains("references_container")) + { + if(focusedElement.previousElementSibling) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + focusedIdentifier.classList.remove("focused"); + unfocusSprite(focusedIdentifier); + + lastFocusedReference = focusedElement.previousElementSibling; + focusedElement = lastFocusedReference; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + + lastFocusedIdentifier = focusedElement.querySelector(".ref_indices").firstElementChild; + focusedIdentifier = lastFocusedIdentifier; + focusedIdentifier.classList.add("focused"); + focusSprite(focusedIdentifier); + } + } + else if(focusedElement.parentNode.parentNode.classList.contains("filters")) + { + if(focusedElement.previousElementSibling && + focusedElement.previousElementSibling.classList.contains("filter_content")) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + + lastFocusedCategory = focusedElement.previousElementSibling; + focusedElement = lastFocusedCategory; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + } + else if(focusedElement.parentNode.classList.contains("credit")) + { + if(focusedElement.parentNode.previousElementSibling) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + if(focusedElement.parentNode.previousElementSibling.querySelector(".support") && + focusedElement.classList.contains("support")) + { + setSpriteLightness(focusedElement.firstChild); + lastFocusedCreditItem = focusedElement.parentNode.previousElementSibling.querySelector(".support"); + focusedElement = lastFocusedCreditItem; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + else + { + lastFocusedCreditItem = focusedElement.parentNode.previousElementSibling.querySelector(".person"); + focusedElement = lastFocusedCreditItem; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + } + } + } + } break; + + case "s": case "j": case "ArrowDown": { + if(focusedElement) + { + if(focusedElement.parentNode.classList.contains("quotes_container")) + { + if(focusedElement.nextElementSibling) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + + lastFocusedQuote = focusedElement.nextElementSibling; + focusedElement = lastFocusedQuote; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + } + else if(focusedElement.parentNode.classList.contains("references_container")) + { + if(focusedElement.nextElementSibling) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + focusedIdentifier.classList.remove("focused"); + unfocusSprite(focusedIdentifier); + + lastFocusedReference = focusedElement.nextElementSibling; + focusedElement = lastFocusedReference; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + + lastFocusedIdentifier = focusedElement.querySelector(".ref_indices").firstElementChild; + focusedIdentifier = lastFocusedIdentifier; + focusedIdentifier.classList.add("focused"); + focusSprite(focusedIdentifier); + } + } + else if(focusedElement.parentNode.parentNode.classList.contains("filters")) + { + if(focusedElement.nextElementSibling && + focusedElement.nextElementSibling.classList.contains("filter_content")) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + + lastFocusedCategory = focusedElement.nextElementSibling; + focusedElement = lastFocusedCategory; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + } + else if(focusedElement.parentNode.classList.contains("credit")) + { + if(focusedElement.parentNode.nextElementSibling) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + if(focusedElement.parentNode.nextElementSibling.querySelector(".support") && + focusedElement.classList.contains("support")) + { + setSpriteLightness(focusedElement.firstChild); + lastFocusedCreditItem = focusedElement.parentNode.nextElementSibling.querySelector(".support"); + focusedElement = lastFocusedCreditItem; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + else + { + lastFocusedCreditItem = focusedElement.parentNode.nextElementSibling.querySelector(".person"); + focusedElement = lastFocusedCreditItem; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + } + } + } + } break; + + case "a": case "h": case "ArrowLeft": { + if(focusedElement) + { + if(focusedElement.parentNode.classList.contains("references_container")) + { + if(focusedIdentifier.previousElementSibling) + { + focusedIdentifier.classList.remove("focused"); + unfocusSprite(focusedIdentifier); + lastFocusedIdentifier = focusedIdentifier.previousElementSibling; + focusedIdentifier = lastFocusedIdentifier; + focusedIdentifier.classList.add("focused"); + focusSprite(focusedIdentifier); + } + else if(focusedIdentifier.parentNode.previousElementSibling.classList.contains("ref_indices")) + { + focusedIdentifier.classList.remove("focused"); + unfocusSprite(focusedIdentifier); + lastFocusedIdentifier = focusedIdentifier.parentNode.previousElementSibling.lastElementChild; + focusedIdentifier = lastFocusedIdentifier; + focusedIdentifier.classList.add("focused"); + focusSprite(focusedIdentifier); + } + } + else if(focusedElement.classList.contains("filter_content")) + { + if(focusedElement.parentNode.classList.contains("filter_media") && + focusedElement.parentNode.previousElementSibling) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + lastFocusedMedium = focusedElement; + + if(!lastFocusedTopic) + { + lastFocusedTopic = focusedElement.parentNode.previousElementSibling.children[1]; + } + lastFocusedCategory = lastFocusedTopic; + focusedElement = lastFocusedCategory; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + } + else if(focusedElement.parentNode.classList.contains("credit")) + { + if(focusedElement.classList.contains("support")) + { + focusedElement.classList.remove("focused"); + console.log(focusedElement); + unfocusSprite(focusedElement); + + lastFocusedCreditItem = focusedElement.previousElementSibling; + setSpriteLightness(focusedElement.firstChild); + focusedElement = lastFocusedCreditItem; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + } + } + } break; + + case "d": case "l": case "ArrowRight": { + if(focusedElement) + { + if(focusedElement.parentNode.classList.contains("references_container")) + { + if(focusedIdentifier.nextElementSibling) + { + focusedIdentifier.classList.remove("focused"); + unfocusSprite(focusedIdentifier); + + lastFocusedIdentifier = focusedIdentifier.nextElementSibling; + focusedIdentifier = lastFocusedIdentifier; + focusedIdentifier.classList.add("focused"); + focusSprite(focusedIdentifier); + } + else if(focusedIdentifier.parentNode.nextElementSibling) + { + focusedIdentifier.classList.remove("focused"); + unfocusSprite(focusedIdentifier); + lastFocusedIdentifier = focusedIdentifier.parentNode.nextElementSibling.firstElementChild; + focusedIdentifier = lastFocusedIdentifier; + focusedIdentifier.classList.add("focused"); + focusSprite(focusedIdentifier); + } + } + else if(focusedElement.classList.contains("filter_content")) + { + if(focusedElement.parentNode.classList.contains("filter_topics") && + focusedElement.parentNode.nextElementSibling) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + lastFocusedTopic = focusedElement; + + if(!lastFocusedMedium) + { + lastFocusedMedium = focusedElement.parentNode.nextElementSibling.children[1]; + } + lastFocusedCategory = lastFocusedMedium; + focusedElement = lastFocusedCategory; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + } + else if(focusedElement.parentNode.classList.contains("credit")) + { + if(focusedElement.classList.contains("person") && + focusedElement.nextElementSibling) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + + lastFocusedCreditItem = focusedElement.nextElementSibling; + focusedElement = lastFocusedCreditItem; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + } + } + } break; + + case "x": case " ": { + if(focusedElement && focusedElement.classList.contains("filter_content")) + { + filterItemToggle(focusedElement); + if(focusedElement.nextElementSibling && + focusedElement.nextElementSibling.classList.contains("filter_content")) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + if(focusedElement.parentNode.classList.contains("filter_topics")) + { + lastFocusedTopic = focusedElement.nextElementSibling; + lastFocusedCategory = lastFocusedTopic; + } + else + { + lastFocusedMedium = focusedElement.nextElementSibling; + lastFocusedCategory = lastFocusedMedium; + } + lastFocusedElement = lastFocusedCategory; + focusedElement = lastFocusedElement; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + } + } break; + + case "X": case "capitalSpace": { + if(focusedElement && focusedElement.classList.contains("filter_content")) + { + filterItemToggle(focusedElement); + if(focusedElement.previousElementSibling && + focusedElement.previousElementSibling.classList.contains("filter_content")) + { + focusedElement.classList.remove("focused"); + unfocusSprite(focusedElement); + if(focusedElement.parentNode.classList.contains("filter_topics")) + { + lastFocusedTopic = focusedElement.previousElementSibling; + lastFocusedCategory = lastFocusedTopic; + } + else + { + lastFocusedMedium = focusedElement.previousElementSibling; + lastFocusedCategory = lastFocusedMedium; + } + lastFocusedElement = lastFocusedCategory; + focusedElement = lastFocusedElement; + focusedElement.classList.add("focused"); + focusSprite(focusedElement); + } + } + } break; + + case "z": { + toggleFilterOrLinkMode(); + } break; + + case "v": { + if(focusedElement && focusedElement.classList.contains("filter_content")) + { + invertFilter(focusedElement) + } + } break; + + case "V": { + resetFilter(); + } break; + + case "?": { + helpDocumentation.classList.toggle("visible"); + } break; + + case 'N': + case 'J': + case 'S': { + player.jumpToNextMarker(); + } break; + + case 'P': + case 'K': + case 'W': { + player.jumpToPrevMarker(); + } break; + case '[': + case '<': { + if(prevEpisode) + { + location = prevEpisode.href; + } + } break; + case ']': + case '>': { + if(nextEpisode) + { + location = nextEpisode.href; + } + } break; + case 'Y': { + if(cineraLink) + { + if(linkAnnotation == false && player.playing) + { + player.pause(); + } + if(linkMenu && !linkMenu.classList.contains("visible")) + { + toggleMenuVisibility(linkMenu); + } + SelectText(cineraLink); + } + } + default: { + gotKey = false; + } break; + } + return gotKey; +} diff --git a/cinera/cinera_player_pre.js b/cinera/cinera_player_pre.js index f6eb523..48a6d42 100644 --- a/cinera/cinera_player_pre.js +++ b/cinera/cinera_player_pre.js @@ -501,11 +501,11 @@ function toggleLinkMode(linkMode, link) linkAnnotation = !linkAnnotation; if(linkAnnotation == true) { - linkMode.textContent = "Link to current annotation"; + linkMode.textContent = "Link to: current timestamp"; } else { - linkMode.textContent = "Link to nearest second"; + linkMode.textContent = "Link to: nearest second"; } updateLink(); } @@ -598,7 +598,7 @@ function toggleMenuVisibility(element) { lastFocusedCreditItem = element.querySelectorAll(".credit .support")[0]; focusedElement = lastFocusedCreditItem; focusedElement.classList.add("focused"); - setIconLightness(focusedElement.firstChild); + setSpriteLightness(focusedElement.firstChild); } else { @@ -901,11 +901,11 @@ function handleKey(key) { if(focusedElement.parentNode.previousElementSibling.querySelector(".support") && focusedElement.classList.contains("support")) { - setIconLightness(focusedElement.firstChild); + setSpriteLightness(focusedElement.firstChild); lastFocusedCreditItem = focusedElement.parentNode.previousElementSibling.querySelector(".support"); focusedElement = lastFocusedCreditItem; focusedElement.classList.add("focused"); - setIconLightness(focusedElement.firstChild); + setSpriteLightness(focusedElement.firstChild); } else { @@ -968,11 +968,11 @@ function handleKey(key) { if(focusedElement.parentNode.nextElementSibling.querySelector(".support") && focusedElement.classList.contains("support")) { - setIconLightness(focusedElement.firstChild); + setSpriteLightness(focusedElement.firstChild); lastFocusedCreditItem = focusedElement.parentNode.nextElementSibling.querySelector(".support"); focusedElement = lastFocusedCreditItem; focusedElement.classList.add("focused"); - setIconLightness(focusedElement.firstChild); + setSpriteLightness(focusedElement.firstChild); } else { @@ -1029,7 +1029,10 @@ function handleKey(key) { focusedElement.classList.remove("focused"); lastFocusedCreditItem = focusedElement.previousElementSibling; - setIconLightness(focusedElement.firstChild); + if(focusedElement.firstChild.classList.contains("cineraSprite")) + { + setSpriteLightness(focusedElement.firstChild); + } focusedElement = lastFocusedCreditItem; focusedElement.classList.add("focused"); } @@ -1085,7 +1088,10 @@ function handleKey(key) { lastFocusedCreditItem = focusedElement.nextElementSibling; focusedElement = lastFocusedCreditItem; focusedElement.classList.add("focused"); - setIconLightness(focusedElement.firstChild); + if(focusedElement.firstChild.classList.contains("cineraSprite")) + { + setSpriteLightness(focusedElement.firstChild); + } } } } @@ -1244,6 +1250,7 @@ function filterItemToggle(filterItem) { if(filterState[selectedCategory].off) { filterItem.classList.add("off"); + disableSprite(filterItem); if(!filterItem.parentNode.classList.contains("filter_media")) { filterItem.querySelector(".icon").style.backgroundColor = "transparent"; @@ -1273,6 +1280,7 @@ function filterItemToggle(filterItem) { if(markerCategories[k].classList.contains(selectedCategory)) { markerCategories[k].classList.add("off"); + disableSprite(markerCategories[k]); } } testMarkers[j].classList.remove(selectedCategory); @@ -1305,6 +1313,7 @@ function filterItemToggle(filterItem) { else { filterItem.classList.remove("off"); + enableSprite(filterItem); if(!filterItem.parentNode.classList.contains("filter_media")) { filterItem.querySelector(".icon").style.backgroundColor = getComputedStyle(filterItem.querySelector(".icon")).getPropertyValue("border-color"); @@ -1338,6 +1347,7 @@ function filterItemToggle(filterItem) { if(markerCategories[k].classList.contains(selectedCategory)) { markerCategories[k].classList.remove("off"); + enableSprite(markerCategories[k]); } } } @@ -1489,6 +1499,7 @@ function navigateFilter(filterItem) { if(filterItem != lastFocusedCategory) { lastFocusedCategory.classList.remove("focused"); + unfocusSprite(lastFocusedCategory); if(filterItem.parentNode.classList.contains("filter_topics")) { lastFocusedTopic = filterItem; @@ -1501,6 +1512,7 @@ function navigateFilter(filterItem) { } focusedElement = lastFocusedCategory; focusedElement.classList.add("focused"); + focusSprite(focusedElement); } } @@ -1636,6 +1648,7 @@ function getBackgroundBrightness(element) { var result = Math.sqrt(rgb[0] * rgb[0] * .241 + rgb[1] * rgb[1] * .691 + rgb[2] * rgb[2] * .068); + console.log(result); return result; } @@ -1668,15 +1681,3 @@ function setDotLightness(topicDot) topicDot.style.borderColor = ("hsl(" + Hue + ", " + Saturation + "%, 47%)"); } } - -function setIconLightness(iconElement) -{ - if(getBackgroundBrightness(iconElement) < 127) - { - iconElement.style.backgroundPosition = ("0px 0px"); - } - else - { - iconElement.style.backgroundPosition = ("16px 0px"); - } -} diff --git a/cinera/cinera_post.js b/cinera/cinera_post.js new file mode 100644 index 0000000..b4d654c --- /dev/null +++ b/cinera/cinera_post.js @@ -0,0 +1,86 @@ +var cineraDropdownNavigation = document.getElementsByClassName("cineraNavDropdown"); +for(var i = 0; i < cineraDropdownNavigation.length; ++i) +{ + var cineraFamily = cineraDropdownNavigation[i].getElementsByClassName("cineraNavHorizontal")[0]; + cineraDropdownNavigation[i].addEventListener("click", function() { + if(cineraFamily.classList.contains("visible")) + { + cineraFamily.classList.remove("visible"); + } + else + { + cineraFamily.classList.add("visible"); + } + }); + + cineraDropdownNavigation[i].addEventListener("mouseenter", function() { + cineraFamily.classList.add("visible"); + }); + + cineraDropdownNavigation[i].addEventListener("mouseleave", function() { + cineraFamily.classList.remove("visible"); + }); +} + +var Sprites = document.getElementsByClassName("cineraSprite"); + +for(var i = 0; i < Sprites.length; ++i) +{ + var This = Sprites[i]; + + var TileX = This.getAttribute("data-tile-width"); + var TileY = This.getAttribute("data-tile-height"); + var AspectRatio = TileX / TileY; + + // TODO(matt): Nail down the desiredness situation. Perhaps respond to: + // width / min-width / height / min-height set in a CSS file + // flexbox layout, if possible + + // NOTE(matt): These values are "decoupled" here, to facilitate handling of sizes other than the original + // We'll probably need some way of checking the desired and original of both the X and Y, and pick which one on + // which to base the computation of the other + var DesiredX = TileX; + var DesiredY = DesiredX / AspectRatio; + var Proportion = DesiredX / TileX; + // + //// + + // NOTE(matt): Size the container and its background image + // + This.style.width = DesiredX + "px"; + This.style.height = DesiredY + "px"; + + var SpriteWidth = This.getAttribute("data-sprite-width"); + var SpriteHeight = This.getAttribute("data-sprite-height"); + This.style.backgroundSize = SpriteWidth * Proportion + "px " + SpriteHeight * Proportion + "px"; + // + //// + + // NOTE(matt): Pick the tile + // + setSpriteLightness(This); + if(This.classList.contains("dark")) + { + This.style.backgroundPositionX = This.getAttribute("data-x-dark") + "px"; + } + + if(elementIsFocused(This)) + { + This.style.backgroundPositionY = This.getAttribute("data-y-focused") + "px"; + } + + if(This.classList.contains("off")) + { + This.style.backgroundPositionY = This.getAttribute("data-y-disabled") + "px"; + } + else + { + This.style.backgroundPositionY = This.getAttribute("data-y-normal") + "px"; + } + // + //// + + // NOTE(matt): Finally apply the background image + var URL = This.getAttribute("data-src"); + This.style.backgroundImage = "url('" + URL + "')"; +} diff --git a/cinera/cinera_pre.js b/cinera/cinera_pre.js new file mode 100644 index 0000000..fecf7b5 --- /dev/null +++ b/cinera/cinera_pre.js @@ -0,0 +1,112 @@ +function getBackgroundBrightness(element) { + var colour = getComputedStyle(element).getPropertyValue("background-color"); + var depth = 0; + while((colour == "transparent" || colour == "rgba(0, 0, 0, 0)") && depth <= 4) + { + element = element.parentNode; + colour = getComputedStyle(element).getPropertyValue("background-color"); + ++depth; + } + var rgb = colour.slice(4, -1).split(", "); + var result = Math.sqrt(rgb[0] * rgb[0] * .241 + + rgb[1] * rgb[1] * .691 + + rgb[2] * rgb[2] * .068); + return result; +} + +function setSpriteLightness(spriteElement) +{ + if(getBackgroundBrightness(spriteElement) < 127) + { + spriteElement.classList.add("dark"); + } + else + { + spriteElement.classList.remove("dark"); + } +} + +function elementIsFocused(Element) +{ + var Result = false; + if(Element.classList.contains("focused")) + { + Result = true; + } + while(Element.parent) + { + Element = Element.parent; + if(Element.classList.contains("focused")) + { + Result = true; + break; + } + } + return Result; +} + +function focusSprite(Element) +{ + if(Element.classList.contains("cineraSprite")) + { + setSpriteLightness(Element); + Element.style.backgroundPositionY = Element.getAttribute("data-y-focused") + "px"; + } + for(var i = 0; i < Element.childElementCount; ++i) + { + focusSprite(Element.children[i]); + } +} + +function enableSprite(Element) +{ + if(Element.classList.contains("focused")) + { + focusSprite(Element); + } + else + { + if(Element.classList.contains("cineraSprite")) + { + setSpriteLightness(Element); + Element.style.backgroundPositionY = Element.getAttribute("data-y-normal") + "px"; + } + for(var i = 0; i < Element.childElementCount; ++i) + { + enableSprite(Element.children[i]); + } + } +} + +function disableSprite(Element) +{ + if(Element.classList.contains("cineraSprite")) + { + setSpriteLightness(Element); + Element.style.backgroundPositionY = Element.getAttribute("data-y-disabled") + "px"; + } + for(var i = 0; i < Element.childElementCount; ++i) + { + disableSprite(Element.children[i]); + } +} + +function unfocusSprite(Element) +{ + if(Element.classList.contains("off")) + { + disableSprite(Element); + } + else + { + if(Element.classList.contains("cineraSprite")) + { + setSpriteLightness(Element); + Element.style.backgroundPositionY = Element.getAttribute("data-y-normal") + "px"; + } + for(var i = 0; i < Element.childElementCount; ++i) + { + unfocusSprite(Element.children[i]); + } + } +} diff --git a/cinera/cinera_search.js b/cinera/cinera_search.js index 6c470c5..df5b460 100644 --- a/cinera/cinera_search.js +++ b/cinera/cinera_search.js @@ -9,41 +9,202 @@ if (location.hash && location.hash.length > 0) { } var indexControl = document.getElementById("cineraIndexControl"); -var indexContainer = document.getElementById("cineraIndex"); -var projectID = indexContainer.attributes.getNamedItem("data-project").value; -var theme = indexContainer.classList.item(0); -var baseURL = indexContainer.attributes.getNamedItem("data-baseURL").value; -var playerLocation = indexContainer.attributes.getNamedItem("data-playerLocation").value; -var resultsSummary = document.getElementById("cineraResultsSummary"); - var indexSort = indexControl.querySelector("#cineraIndexSort"); -var indexEntries = indexContainer.querySelector("#cineraIndexEntries"); var indexSortChronological = true; -indexSort.addEventListener("click", function(ev) { - if(indexSortChronological) +var filterMenu = indexControl.querySelector(".cineraIndexFilter"); +if(filterMenu) +{ + var filterContainer = filterMenu.querySelector(".filter_container"); + //menuState.push(linkMenu); + + filterMenu.addEventListener("mouseenter", function(ev) { + filterContainer.style.display = "block"; + }); + + filterMenu.addEventListener("mouseleave", function(ev) { + filterContainer.style.display = "none"; + }); +} + +function hideEntriesOfProject(ProjectElement) +{ + if(!ProjectElement.classList.contains("off")) { - this.firstChild.nodeValue = "Sort: New to Old ⏷" - indexEntries.style.flexFlow = "column-reverse"; + ProjectElement.classList.add("off"); + } + var baseURL = ProjectElement.attributes.getNamedItem("data-baseURL").value; + for(var i = 0; i < projects.length; ++i) + { + var ThisProject = projects[i]; + if(ThisProject.baseURL === baseURL) + { + ThisProject.filteredOut = true; + if(ThisProject.entriesContainer != null) + { + ThisProject.entriesContainer.style.display = "none"; + disableSprite(ThisProject.entriesContainer.parentElement); + } + } + } +} + +function showEntriesOfProject(ProjectElement) +{ + if(ProjectElement.classList.contains("off")) + { + ProjectElement.classList.remove("off"); + } + var baseURL = ProjectElement.attributes.getNamedItem("data-baseURL").value; + for(var i = 0; i < projects.length; ++i) + { + var ThisProject = projects[i]; + if(ThisProject.baseURL === baseURL) + { + ThisProject.filteredOut = false; + if(ThisProject.entriesContainer != null) + { + ThisProject.entriesContainer.style.display = "flex"; + enableSprite(ThisProject.entriesContainer.parentElement); + } + } + } +} + +function hideProjectSearchResults(baseURL) +{ + var cineraResults = document.getElementById("cineraResults"); + if(cineraResults) + { + var cineraResultsProjects = cineraResults.querySelectorAll(".projectContainer"); + for(var i = 0; i < cineraResultsProjects.length; ++i) + { + var resultBaseURL = cineraResultsProjects[i].attributes.getNamedItem("data-baseURL").value; + if(baseURL === resultBaseURL) + { + cineraResultsProjects[i].style.display = "none"; + return; + } + } + } +} + +function showProjectSearchResults(baseURL) +{ + var cineraResults = document.getElementById("cineraResults"); + if(cineraResults) + { + var cineraResultsProjects = cineraResults.querySelectorAll(".projectContainer"); + for(var i = 0; i < cineraResultsProjects.length; ++i) + { + var resultBaseURL = cineraResultsProjects[i].attributes.getNamedItem("data-baseURL").value; + if(baseURL === resultBaseURL) + { + cineraResultsProjects[i].style.display = "flex"; + return; + } + } + } +} + +function toggleEntriesOfProjectAndChildren(ProjectFilterElement) +{ + var baseURL = ProjectFilterElement.attributes.getNamedItem("data-baseURL").value; + var shouldShow = ProjectFilterElement.classList.contains("off"); + if(shouldShow) + { + ProjectFilterElement.classList.remove("off"); + enableSprite(ProjectFilterElement); } else { - this.firstChild.nodeValue = "Sort: Old to New ⏶" - indexEntries.style.flexFlow = "column"; + ProjectFilterElement.classList.add("off"); + disableSprite(ProjectFilterElement); } - indexSortChronological = !indexSortChronological; -}); -var lastQuery = null; -var resultsToRender = []; -var resultsIndex = 0; -var resultsMarkerIndex = -1; + for(var i = 0; i < projects.length; ++i) + { + var ThisProject = projects[i]; + if(ThisProject.baseURL === baseURL) + { + if(shouldShow) + { + ThisProject.filteredOut = false; + enableSprite(ThisProject.projectTitleElement.parentElement); + if(ThisProject.entriesContainer != null) + { + ThisProject.entriesContainer.style.display = "flex"; + } + showProjectSearchResults(projects[i].baseURL); + } + else + { + ThisProject.filteredOut = true; + disableSprite(ThisProject.projectTitleElement.parentElement); + if(ThisProject.entriesContainer != null) + { + ThisProject.entriesContainer.style.display = "none"; + } + hideProjectSearchResults(ThisProject.baseURL); + } + } + } + + var indexChildFilterProjects = ProjectFilterElement.querySelectorAll(".cineraFilterProject"); + + for(var j = 0; j < indexChildFilterProjects.length; ++j) + { + var ThisElement = indexChildFilterProjects[j]; + if(shouldShow) + { + var baseURL = ThisElement.attributes.getNamedItem("data-baseURL").value; + showEntriesOfProject(ThisElement); + showProjectSearchResults(baseURL); + } + else + { + var baseURL = ThisElement.attributes.getNamedItem("data-baseURL").value; + hideEntriesOfProject(ThisElement); + hideProjectSearchResults(baseURL); + } + } +} + +var indexFilter = indexControl.querySelector(".cineraIndexFilter"); +if(indexFilter) +{ + var indexFilterProjects = indexFilter.querySelectorAll(".cineraFilterProject"); + for(var i = 0; i < indexFilterProjects.length; ++i) + { + indexFilterProjects[i].addEventListener("mouseover", function(ev) { + ev.stopPropagation(); + this.classList.add("focused"); + focusSprite(this); + }); + indexFilterProjects[i].addEventListener("mouseout", function(ev) { + ev.stopPropagation(); + this.classList.remove("focused"); + unfocusSprite(this); + }); + indexFilterProjects[i].addEventListener("click", function(ev) { + ev.stopPropagation(); + toggleEntriesOfProjectAndChildren(this); + }); + } +} + +var resultsSummary = document.getElementById("cineraResultsSummary"); var resultsContainer = document.getElementById("cineraResults"); -var rendering = false; + +var indexContainer = document.getElementById("cineraIndex"); + +var projectsContainer = indexContainer.querySelectorAll(".cineraIndexProject"); + +var projectContainerPrototype = document.createElement("DIV"); +projectContainerPrototype.classList.add("projectContainer"); var dayContainerPrototype = document.createElement("DIV"); dayContainerPrototype.classList.add("dayContainer"); -dayContainerPrototype.classList.add(theme); var dayNamePrototype = document.createElement("SPAN"); dayNamePrototype.classList.add("dayName"); @@ -51,7 +212,6 @@ dayContainerPrototype.appendChild(dayNamePrototype); var markerListPrototype = document.createElement("DIV"); markerListPrototype.classList.add("markerList"); -markerListPrototype.classList.add(theme); dayContainerPrototype.appendChild(markerListPrototype); var markerPrototype = document.createElement("A"); @@ -61,9 +221,132 @@ if(resultsContainer.getAttribute("data-single") == 0) markerPrototype.setAttribute("target", "_blank"); } -var highlightPrototype = document.createElement("B"); +function prepareToParseIndexFile(project) +{ + project.xhr.addEventListener("load", function() { + var contents = project.xhr.response; + var lines = contents.split("\n"); + var mode = "none"; + var episode = null; + for (var i = 0; i < lines.length; ++i) { + var line = lines[i]; + if (line.trim().length == 0) { continue; } + if (line == "---") { + if (episode != null && episode.name != null && episode.title != null) { + episode.filename = episode.name; + episode.day = getEpisodeName(episode.filename + ".html.md"); + episode.dayContainerPrototype = project.dayContainerPrototype; + episode.markerPrototype = markerPrototype; + episode.playerURLPrefix = project.playerURLPrefix; + project.episodes.push(episode); + } + episode = {}; + mode = "none"; + } else if (line.startsWith("name:")) { + episode.name = line.slice(6); + } else if (line.startsWith("title:")) { + episode.title = line.slice(7).trim().slice(1, -1); + } else if (line.startsWith("markers")) { + mode = "markers"; + episode.markers = []; + } else if (mode == "markers") { + var match = line.match(/"(\d+)": "(.+)"/); + if (match == null) { + console.log(name, line); + } else { + var totalTime = parseInt(line.slice(1)); + var marker = { + totalTime: totalTime, + prettyTime: markerTime(totalTime), + text: match[2].replace(/\\"/g, "\"") + } + episode.markers.push(marker); + } + } + } + document.querySelector(".spinner").classList.remove("show"); + project.parsed = true; + runSearch(true); + }); + project.xhr.addEventListener("error", function() { + console.error("Failed to load content"); + }); +} -var episodes = []; +var projects = []; +function prepareProjects() +{ + for(var i = 0; i < projectsContainer.length; ++i) + { + var ID = projectsContainer[i].attributes.getNamedItem("data-project").value; + var baseURL = projectsContainer[i].attributes.getNamedItem("data-baseURL").value; + var playerLocation = projectsContainer[i].attributes.getNamedItem("data-playerLocation").value; + var theme = projectsContainer[i].classList.item(1); + + projects[i] = + { + baseURL: baseURL, + playerURLPrefix: (baseURL ? baseURL + "/" : "") + (playerLocation ? playerLocation + "/" : ""), + indexLocation: (baseURL ? baseURL + "/" : "") + ID + ".index", + projectTitleElement: projectsContainer[i].querySelector(":scope > .cineraProjectTitle"), + entriesContainer: projectsContainer[i].querySelector(":scope > .cineraIndexEntries"), + dayContainerPrototype: dayContainerPrototype.cloneNode(true), + filteredOut: false, + parsed: false, + searched: false, + resultsToRender: [], + resultsIndex: 0, + theme: theme, + episodes: [], + xhr: new XMLHttpRequest(), + } + + projects[i].dayContainerPrototype.classList.add(theme); + projects[i].dayContainerPrototype.children[1].classList.add(theme); + + document.querySelector(".spinner").classList.add("show"); + projects[i].xhr.open("GET", projects[i].indexLocation); + projects[i].xhr.setRequestHeader("Content-Type", "text/plain"); + projects[i].xhr.send(); + prepareToParseIndexFile(projects[i]); + } +} +prepareProjects(); + +indexSort.addEventListener("click", function(ev) { + if(indexSortChronological) + { + this.firstChild.nodeValue = "Sort: New to Old ⏷" + for(var i = 0; i < projects.length; ++i) + { + if(projects[i].entriesContainer) + { + projects[i].entriesContainer.style.flexFlow = "column-reverse"; + } + } + } + else + { + this.firstChild.nodeValue = "Sort: Old to New ⏶" + for(var i = 0; i < projects.length; ++i) + { + if(projects[i].entriesContainer) + { + projects[i].entriesContainer.style.flexFlow = "column"; + } + } + } + indexSortChronological = !indexSortChronological; + runSearch(true); +}); + +var lastQuery = null; +var markerList = null; +var projectContainer = null; +var resultsMarkerIndex = -1; +var rendering = false; + +var highlightPrototype = document.createElement("B"); function getEpisodeName(filename) { var day = filename; @@ -90,57 +373,128 @@ function padTimeComponent(component) { return (component < 10 ? "0" + component : component); } +function resetProjectsForSearch() +{ + for(var i = 0; i < projects.length; ++i) + { + var project = projects[i]; + project.searched = false; + project.resultsToRender = []; + } +} + var renderHandle; -function runSearch() { + +function runSearch(refresh) { var queryStr = document.getElementById("query").value; - if (lastQuery != queryStr) { + if (refresh || lastQuery != queryStr) { var oldResultsContainer = resultsContainer; resultsContainer = oldResultsContainer.cloneNode(false); oldResultsContainer.parentNode.insertBefore(resultsContainer, oldResultsContainer); oldResultsContainer.remove(); - resultsIndex = 0; + for(var i = 0; i < projects.length; ++i) + { + projects[i].resultsIndex = 0; + } resultsMarkerIndex = -1; } lastQuery = queryStr; - resultsToRender = []; + + resetProjectsForSearch(); + var numEpisodes = 0; var numMarkers = 0; var totalSeconds = 0; + + // NOTE(matt): Function defined within runSearch() so that we can modify numEpisodes, numMarkers and totalSeconds + function runSearchInterior(resultsToRender, query, episode) + { + var matches = []; + for (var k = 0; k < episode.markers.length; ++k) { + query.lastIndex = 0; + var result = query.exec(episode.markers[k].text); + if (result && result[0].length > 0) { + numMarkers++; + matches.push(episode.markers[k]); + if (k < episode.markers.length-1) { + totalSeconds += episode.markers[k+1].totalTime - episode.markers[k].totalTime; + } + } + } + if (matches.length > 0) { + numEpisodes++; + resultsToRender.push({ + query: query, + episode: episode, + matches: matches + }); + } + } + if (queryStr && queryStr.length > 0) { indexContainer.style.display = "none"; resultsSummary.style.display = "block"; - if (episodes.length > 0) { - var query = new RegExp(queryStr.replace("(", "\\(").replace(")", "\\)").replace(/\|+/, "\|").replace(/\|$/, "").replace(/(^|[^\\])\\$/, "$1"), "gi"); - for (var i = 0; i < episodes.length; ++i) { - var episode = episodes[i]; - var matches = []; - for (var j = 0; j < episode.markers.length; ++j) { - query.lastIndex = 0; - var result = query.exec(episode.markers[j].text); - if (result && result[0].length > 0) { - numMarkers++; - matches.push(episode.markers[j]); - if (j < episode.markers.length-1) { - totalSeconds += episode.markers[j+1].totalTime - episode.markers[j].totalTime; - } + var shouldRender = false; + var query = new RegExp(queryStr.replace("(", "\\(").replace(")", "\\)").replace(/\|+/, "\|").replace(/\|$/, "").replace(/(^|[^\\])\\$/, "$1"), "gi"); + + // Visible + for(var i = 0; i < projects.length; ++i) + { + var project = projects[i]; + if(project.parsed && !project.filteredOut && project.episodes.length > 0) { + if(indexSortChronological) + { + for(var j = 0; j < project.episodes.length; ++j) { + var episode = project.episodes[j]; + runSearchInterior(project.resultsToRender, query, episode); } } - if (matches.length > 0) { - numEpisodes++; - resultsToRender.push({ - query: query, - episode: episode, - matches: matches - }); + else + { + for(var j = project.episodes.length; j > 0; --j) { + var episode = project.episodes[j - 1]; + runSearchInterior(project.resultsToRender, query, episode); + } } - } + shouldRender = true; + project.searched = true; + + } + } + + // Invisible + for(var i = 0; i < projects.length; ++i) + { + var project = projects[i]; + if(project.parsed && project.filteredOut && !project.searched && project.episodes.length > 0) { + if(indexSortChronological) + { + for(var j = 0; j < project.episodes.length; ++j) { + var episode = project.episodes[j]; + runSearchInterior(project.resultsToRender, query, episode); + } + } + else + { + for(var j = project.episodes.length; j > 0; --j) { + var episode = project.episodes[j - 1]; + runSearchInterior(project.resultsToRender, query, episode); + } + } + + shouldRender = true; + project.searched = true; + + } + } + + if(shouldRender) + { if (rendering) { clearTimeout(renderHandle); } renderResults(); - } else { - document.querySelector(".spinner").classList.add("show"); } } else @@ -155,63 +509,85 @@ function runSearch() { } function renderResults() { - if (resultsIndex < resultsToRender.length) { - rendering = true; - var maxItems = 42; - var numItems = 0; - while (numItems < maxItems && resultsIndex < resultsToRender.length) { - var query = resultsToRender[resultsIndex].query; - var episode = resultsToRender[resultsIndex].episode; - var matches = resultsToRender[resultsIndex].matches; - var markerList = null; - if (resultsMarkerIndex == -1) { - var dayContainer = dayContainerPrototype.cloneNode(true); - var dayName = dayContainer.children[0]; - markerList = dayContainer.children[1]; - dayName.textContent = episode.day + ": " + episode.title; - resultsContainer.appendChild(dayContainer); - resultsMarkerIndex = 0; - numItems++; - } else { - markerList = document.querySelector("#cineraResults > .dayContainer:nth-child(" + (resultsIndex+1) + ") .markerList"); - } - - while (numItems < maxItems && resultsMarkerIndex < matches.length) { - var match = matches[resultsMarkerIndex]; - var marker = markerPrototype.cloneNode(); - var playerURLPrefix = (baseURL ? baseURL + "/" : "") + (playerLocation ? playerLocation + "/" : ""); - marker.setAttribute("href", playerURLPrefix + episode.filename.replace(/"/g, "") + "/#" + match.totalTime); - query.lastIndex = 0; - var cursor = 0; - var text = match.text; - var result = null; - marker.appendChild(document.createTextNode(match.prettyTime + " ")); - while (result = query.exec(text)) { - if (result.index > cursor) { - marker.appendChild(document.createTextNode(text.slice(cursor, result.index))); + var maxItems = 42; + var numItems = 0; + for(var i = 0; i < projects.length; ++i) + { + var project = projects[i]; + if (project.resultsIndex < project.resultsToRender.length) { + rendering = true; + while (numItems < maxItems && project.resultsIndex < project.resultsToRender.length) { + var query = project.resultsToRender[project.resultsIndex].query; + var episode = project.resultsToRender[project.resultsIndex].episode; + var matches = project.resultsToRender[project.resultsIndex].matches; + if (resultsMarkerIndex == -1) { + if(project.resultsIndex == 0 || project.resultsToRender[project.resultsIndex - 1].episode.playerURLPrefix != episode.playerURLPrefix) + { + projectContainer = projectContainerPrototype.cloneNode(true); + for(var i = 0; i < projects.length; ++i) + { + if(projects[i].playerURLPrefix === episode.playerURLPrefix) + { + projectContainer.setAttribute("data-baseURL", projects[i].baseURL); + if(projects[i].filteredOut) + { + projectContainer.style.display = "none"; + } + } + } + resultsContainer.appendChild(projectContainer); } - var highlightEl = highlightPrototype.cloneNode(); - highlightEl.textContent = result[0]; - marker.appendChild(highlightEl); - cursor = result.index + result[0].length; + else + { + projectContainer = resultsContainer.lastElementChild; + } + + + var dayContainer = episode.dayContainerPrototype.cloneNode(true); + var dayName = dayContainer.children[0]; + markerList = dayContainer.children[1]; + dayName.textContent = episode.day + ": " + episode.title; + projectContainer.appendChild(dayContainer); + resultsMarkerIndex = 0; + numItems++; + } + + while (numItems < maxItems && resultsMarkerIndex < matches.length) { + var match = matches[resultsMarkerIndex]; + var marker = episode.markerPrototype.cloneNode(true); + marker.setAttribute("href", episode.playerURLPrefix + episode.filename.replace(/"/g, "") + "/#" + match.totalTime); + query.lastIndex = 0; + var cursor = 0; + var text = match.text; + var result = null; + marker.appendChild(document.createTextNode(match.prettyTime + " ")); + while (result = query.exec(text)) { + if (result.index > cursor) { + marker.appendChild(document.createTextNode(text.slice(cursor, result.index))); + } + var highlightEl = highlightPrototype.cloneNode(); + highlightEl.textContent = result[0]; + marker.appendChild(highlightEl); + cursor = result.index + result[0].length; + } + + if (cursor < text.length) { + marker.appendChild(document.createTextNode(text.slice(cursor, text.length))); + } + markerList.appendChild(marker); + numItems++; + resultsMarkerIndex++; } - - if (cursor < text.length) { - marker.appendChild(document.createTextNode(text.slice(cursor, text.length))); + + if (resultsMarkerIndex == matches.length) { + resultsMarkerIndex = -1; + project.resultsIndex++; } - markerList.appendChild(marker); - numItems++; - resultsMarkerIndex++; - } - - if (resultsMarkerIndex == matches.length) { - resultsMarkerIndex = -1; - resultsIndex++; } + renderHandle = setTimeout(renderResults, 0); + } else { + rendering = false; } - renderHandle = setTimeout(renderResults, 0); - } else { - rendering = false; } } @@ -244,55 +620,6 @@ queryEl.addEventListener("input", function(ev) { runSearch(); }); -var xhr = new XMLHttpRequest(); -xhr.addEventListener("load", function() { - var contents = xhr.response; - var lines = contents.split("\n"); - var mode = "none"; - var episode = null; - for (var i = 0; i < lines.length; ++i) { - var line = lines[i]; - if (line.trim().length == 0) { continue; } - if (line == "---") { - if (episode != null) { - episode.filename = episode.name; - episode.day = getEpisodeName(episode.filename + ".html.md"); - episodes.push(episode); - } - episode = {}; - mode = "none"; - } else if (line.startsWith("name:")) { - episode.name = line.slice(6); - } else if (line.startsWith("title:")) { - episode.title = line.slice(7).trim().slice(1, -1); - } else if (line.startsWith("markers")) { - mode = "markers"; - episode.markers = []; - } else if (mode == "markers") { - var match = line.match(/"(\d+)": "(.+)"/); - if (match == null) { - console.log(name, line); - } else { - var totalTime = parseInt(line.slice(1)); - var marker = { - totalTime: totalTime, - prettyTime: markerTime(totalTime), - text: match[2].replace(/\\"/g, "\"") - } - episode.markers.push(marker); - } - } - } - document.querySelector(".spinner").classList.remove("show"); - runSearch(); -}); -xhr.addEventListener("error", function() { - console.error("Failed to load content"); -}); - -var indexLocation = (baseURL ? baseURL + "/" : "") + projectID + ".index"; -xhr.open("GET", indexLocation); -xhr.setRequestHeader("Content-Type", "text/plain"); -xhr.send(); - runSearch(); + +// Testing diff --git a/cinera/cinera_sprite_patreon.png b/cinera/cinera_sprite_patreon.png index 33b19eb..343e967 100644 Binary files a/cinera/cinera_sprite_patreon.png and b/cinera/cinera_sprite_patreon.png differ diff --git a/cinera/cinera_sprite_sendowl.png b/cinera/cinera_sprite_sendowl.png index feee616..511876b 100644 Binary files a/cinera/cinera_sprite_sendowl.png and b/cinera/cinera_sprite_sendowl.png differ diff --git a/cinera/stb_image.h b/cinera/stb_image.h new file mode 100644 index 0000000..2857f05 --- /dev/null +++ b/cinera/stb_image.h @@ -0,0 +1,7656 @@ +/* stb_image - v2.25 - public domain image loader - http://nothings.org/stb + no warranty implied; use at your own risk + + Do this: + #define STB_IMAGE_IMPLEMENTATION + before you include this file in *one* C or C++ file to create the implementation. + + // i.e. it should look like this: + #include ... + #include ... + #include ... + #define STB_IMAGE_IMPLEMENTATION + #include "stb_image.h" + + You can #define STBI_ASSERT(x) before the #include to avoid using assert.h. + And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free + + + QUICK NOTES: + Primarily of interest to game developers and other people who can + avoid problematic images and only need the trivial interface + + JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib) + PNG 1/2/4/8/16-bit-per-channel + + TGA (not sure what subset, if a subset) + BMP non-1bpp, non-RLE + PSD (composited view only, no extra channels, 8/16 bit-per-channel) + + GIF (*comp always reports as 4-channel) + HDR (radiance rgbE format) + PIC (Softimage PIC) + PNM (PPM and PGM binary only) + + Animated GIF still needs a proper API, but here's one way to do it: + http://gist.github.com/urraka/685d9a6340b26b830d49 + + - decode from memory or through FILE (define STBI_NO_STDIO to remove code) + - decode from arbitrary I/O callbacks + - SIMD acceleration on x86/x64 (SSE2) and ARM (NEON) + + Full documentation under "DOCUMENTATION" below. + + +LICENSE + + See end of file for license information. + +RECENT REVISION HISTORY: + + 2.25 (2020-02-02) fix warnings + 2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically + 2.23 (2019-08-11) fix clang static analysis warning + 2.22 (2019-03-04) gif fixes, fix warnings + 2.21 (2019-02-25) fix typo in comment + 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs + 2.19 (2018-02-11) fix warning + 2.18 (2018-01-30) fix warnings + 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings + 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes + 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC + 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs + 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes + 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes + 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64 + RGB-format JPEG; remove white matting in PSD; + allocate large structures on the stack; + correct channel count for PNG & BMP + 2.10 (2016-01-22) avoid warning introduced in 2.09 + 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED + + See end of file for full revision history. + + + ============================ Contributors ========================= + + Image formats Extensions, features + Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info) + Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info) + Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG) + Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks) + Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG) + Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip) + Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD) + github:urraka (animated gif) Junggon Kim (PNM comments) + Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA) + socks-the-fox (16-bit PNG) + Jeremy Sawicki (handle all ImageNet JPGs) + Optimizations & bugfixes Mikhail Morozov (1-bit BMP) + Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query) + Arseny Kapoulkine + John-Mark Allen + Carmelo J Fdez-Aguera + + Bug & warning fixes + Marc LeBlanc David Woo Guillaume George Martins Mozeiko + Christpher Lloyd Jerry Jansson Joseph Thomson Phil Jordan + Dave Moore Roy Eltham Hayaki Saito Nathan Reed + Won Chun Luke Graham Johan Duparc Nick Verigakis + the Horde3D community Thomas Ruf Ronny Chevalier github:rlyeh + Janez Zemva John Bartholomew Michal Cichon github:romigrou + Jonathan Blow Ken Hamada Tero Hanninen github:svdijk + Laurent Gomila Cort Stratton Sergio Gonzalez github:snagar + Aruelien Pocheville Thibault Reuille Cass Everitt github:Zelex + Ryamond Barbiero Paul Du Bois Engin Manap github:grim210 + Aldo Culquicondor Philipp Wiesemann Dale Weiler github:sammyhw + Oriol Ferrer Mesia Josh Tobin Matthew Gregan github:phprus + Julian Raschke Gregory Mullen Baldur Karlsson github:poppolopoppo + Christian Floisand Kevin Schmidt JR Smith github:darealshinji + Brad Weinberger Matvey Cherevko github:Michaelangel007 + Blazej Dariusz Roszkowski Alexander Veselov +*/ + +#ifndef STBI_INCLUDE_STB_IMAGE_H +#define STBI_INCLUDE_STB_IMAGE_H + +// DOCUMENTATION +// +// Limitations: +// - no 12-bit-per-channel JPEG +// - no JPEGs with arithmetic coding +// - GIF always returns *comp=4 +// +// Basic usage (see HDR discussion below for HDR usage): +// int x,y,n; +// unsigned char *data = stbi_load(filename, &x, &y, &n, 0); +// // ... process data if not NULL ... +// // ... x = width, y = height, n = # 8-bit components per pixel ... +// // ... replace '0' with '1'..'4' to force that many components per pixel +// // ... but 'n' will always be the number that it would have been if you said 0 +// stbi_image_free(data) +// +// Standard parameters: +// int *x -- outputs image width in pixels +// int *y -- outputs image height in pixels +// int *channels_in_file -- outputs # of image components in image file +// int desired_channels -- if non-zero, # of image components requested in result +// +// The return value from an image loader is an 'unsigned char *' which points +// to the pixel data, or NULL on an allocation failure or if the image is +// corrupt or invalid. The pixel data consists of *y scanlines of *x pixels, +// with each pixel consisting of N interleaved 8-bit components; the first +// pixel pointed to is top-left-most in the image. There is no padding between +// image scanlines or between pixels, regardless of format. The number of +// components N is 'desired_channels' if desired_channels is non-zero, or +// *channels_in_file otherwise. If desired_channels is non-zero, +// *channels_in_file has the number of components that _would_ have been +// output otherwise. E.g. if you set desired_channels to 4, you will always +// get RGBA output, but you can check *channels_in_file to see if it's trivially +// opaque because e.g. there were only 3 channels in the source image. +// +// An output image with N components has the following components interleaved +// in this order in each pixel: +// +// N=#comp components +// 1 grey +// 2 grey, alpha +// 3 red, green, blue +// 4 red, green, blue, alpha +// +// If image loading fails for any reason, the return value will be NULL, +// and *x, *y, *channels_in_file will be unchanged. The function +// stbi_failure_reason() can be queried for an extremely brief, end-user +// unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS +// to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly +// more user-friendly ones. +// +// Paletted PNG, BMP, GIF, and PIC images are automatically depalettized. +// +// =========================================================================== +// +// UNICODE: +// +// If compiling for Windows and you wish to use Unicode filenames, compile +// with +// #define STBI_WINDOWS_UTF8 +// and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert +// Windows wchar_t filenames to utf8. +// +// =========================================================================== +// +// Philosophy +// +// stb libraries are designed with the following priorities: +// +// 1. easy to use +// 2. easy to maintain +// 3. good performance +// +// Sometimes I let "good performance" creep up in priority over "easy to maintain", +// and for best performance I may provide less-easy-to-use APIs that give higher +// performance, in addition to the easy-to-use ones. Nevertheless, it's important +// to keep in mind that from the standpoint of you, a client of this library, +// all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all. +// +// Some secondary priorities arise directly from the first two, some of which +// provide more explicit reasons why performance can't be emphasized. +// +// - Portable ("ease of use") +// - Small source code footprint ("easy to maintain") +// - No dependencies ("ease of use") +// +// =========================================================================== +// +// I/O callbacks +// +// I/O callbacks allow you to read from arbitrary sources, like packaged +// files or some other source. Data read from callbacks are processed +// through a small internal buffer (currently 128 bytes) to try to reduce +// overhead. +// +// The three functions you must define are "read" (reads some bytes of data), +// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end). +// +// =========================================================================== +// +// SIMD support +// +// The JPEG decoder will try to automatically use SIMD kernels on x86 when +// supported by the compiler. For ARM Neon support, you must explicitly +// request it. +// +// (The old do-it-yourself SIMD API is no longer supported in the current +// code.) +// +// On x86, SSE2 will automatically be used when available based on a run-time +// test; if not, the generic C versions are used as a fall-back. On ARM targets, +// the typical path is to have separate builds for NEON and non-NEON devices +// (at least this is true for iOS and Android). Therefore, the NEON support is +// toggled by a build flag: define STBI_NEON to get NEON loops. +// +// If for some reason you do not want to use any of SIMD code, or if +// you have issues compiling it, you can disable it entirely by +// defining STBI_NO_SIMD. +// +// =========================================================================== +// +// HDR image support (disable by defining STBI_NO_HDR) +// +// stb_image supports loading HDR images in general, and currently the Radiance +// .HDR file format specifically. You can still load any file through the existing +// interface; if you attempt to load an HDR file, it will be automatically remapped +// to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1; +// both of these constants can be reconfigured through this interface: +// +// stbi_hdr_to_ldr_gamma(2.2f); +// stbi_hdr_to_ldr_scale(1.0f); +// +// (note, do not use _inverse_ constants; stbi_image will invert them +// appropriately). +// +// Additionally, there is a new, parallel interface for loading files as +// (linear) floats to preserve the full dynamic range: +// +// float *data = stbi_loadf(filename, &x, &y, &n, 0); +// +// If you load LDR images through this interface, those images will +// be promoted to floating point values, run through the inverse of +// constants corresponding to the above: +// +// stbi_ldr_to_hdr_scale(1.0f); +// stbi_ldr_to_hdr_gamma(2.2f); +// +// Finally, given a filename (or an open file or memory block--see header +// file for details) containing image data, you can query for the "most +// appropriate" interface to use (that is, whether the image is HDR or +// not), using: +// +// stbi_is_hdr(char *filename); +// +// =========================================================================== +// +// iPhone PNG support: +// +// By default we convert iphone-formatted PNGs back to RGB, even though +// they are internally encoded differently. You can disable this conversion +// by calling stbi_convert_iphone_png_to_rgb(0), in which case +// you will always just get the native iphone "format" through (which +// is BGR stored in RGB). +// +// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per +// pixel to remove any premultiplied alpha *only* if the image file explicitly +// says there's premultiplied data (currently only happens in iPhone images, +// and only if iPhone convert-to-rgb processing is on). +// +// =========================================================================== +// +// ADDITIONAL CONFIGURATION +// +// - You can suppress implementation of any of the decoders to reduce +// your code footprint by #defining one or more of the following +// symbols before creating the implementation. +// +// STBI_NO_JPEG +// STBI_NO_PNG +// STBI_NO_BMP +// STBI_NO_PSD +// STBI_NO_TGA +// STBI_NO_GIF +// STBI_NO_HDR +// STBI_NO_PIC +// STBI_NO_PNM (.ppm and .pgm) +// +// - You can request *only* certain decoders and suppress all other ones +// (this will be more forward-compatible, as addition of new decoders +// doesn't require you to disable them explicitly): +// +// STBI_ONLY_JPEG +// STBI_ONLY_PNG +// STBI_ONLY_BMP +// STBI_ONLY_PSD +// STBI_ONLY_TGA +// STBI_ONLY_GIF +// STBI_ONLY_HDR +// STBI_ONLY_PIC +// STBI_ONLY_PNM (.ppm and .pgm) +// +// - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still +// want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB +// + + +#ifndef STBI_NO_STDIO +#include +#endif // STBI_NO_STDIO + +#define STBI_VERSION 1 + +enum +{ + STBI_default = 0, // only used for desired_channels + + STBI_grey = 1, + STBI_grey_alpha = 2, + STBI_rgb = 3, + STBI_rgb_alpha = 4 +}; + +#include +typedef unsigned char stbi_uc; +typedef unsigned short stbi_us; + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef STBIDEF +#ifdef STB_IMAGE_STATIC +#define STBIDEF static +#else +#define STBIDEF extern +#endif +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// PRIMARY API - works on images of any type +// + +// +// load image by filename, open file, or memory buffer +// + +typedef struct +{ + int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read + void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative + int (*eof) (void *user); // returns nonzero if we are at end of file/data +} stbi_io_callbacks; + +//////////////////////////////////// +// +// 8-bits-per-channel interface +// + +STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels); + +#ifndef STBI_NO_STDIO +STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); +// for stbi_load_from_file, file pointer is left pointing immediately after image +#endif + +#ifndef STBI_NO_GIF +STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp); +#endif + +#ifdef STBI_WINDOWS_UTF8 +STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input); +#endif + +//////////////////////////////////// +// +// 16-bits-per-channel interface +// + +STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels); + +#ifndef STBI_NO_STDIO +STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); +#endif + +//////////////////////////////////// +// +// float-per-channel interface +// +#ifndef STBI_NO_LINEAR + STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels); + STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels); + + #ifndef STBI_NO_STDIO + STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); + STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); + #endif +#endif + +#ifndef STBI_NO_HDR + STBIDEF void stbi_hdr_to_ldr_gamma(float gamma); + STBIDEF void stbi_hdr_to_ldr_scale(float scale); +#endif // STBI_NO_HDR + +#ifndef STBI_NO_LINEAR + STBIDEF void stbi_ldr_to_hdr_gamma(float gamma); + STBIDEF void stbi_ldr_to_hdr_scale(float scale); +#endif // STBI_NO_LINEAR + +// stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR +STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user); +STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len); +#ifndef STBI_NO_STDIO +STBIDEF int stbi_is_hdr (char const *filename); +STBIDEF int stbi_is_hdr_from_file(FILE *f); +#endif // STBI_NO_STDIO + + +// get a VERY brief reason for failure +// on most compilers (and ALL modern mainstream compilers) this is threadsafe +STBIDEF const char *stbi_failure_reason (void); + +// free the loaded image -- this is just free() +STBIDEF void stbi_image_free (void *retval_from_stbi_load); + +// get image dimensions & components without fully decoding +STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp); +STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp); +STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len); +STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user); + +#ifndef STBI_NO_STDIO +STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp); +STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp); +STBIDEF int stbi_is_16_bit (char const *filename); +STBIDEF int stbi_is_16_bit_from_file(FILE *f); +#endif + + + +// for image formats that explicitly notate that they have premultiplied alpha, +// we just return the colors as stored in the file. set this flag to force +// unpremultiplication. results are undefined if the unpremultiply overflow. +STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply); + +// indicate whether we should process iphone images back to canonical format, +// or just pass them through "as-is" +STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert); + +// flip the image vertically, so the first pixel in the output array is the bottom left +STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip); + +// as above, but only applies to images loaded on the thread that calls the function +// this function is only available if your compiler supports thread-local variables; +// calling it will fail to link if your compiler doesn't +STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip); + +// ZLIB client - used by PNG, available for other purposes + +STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen); +STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header); +STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen); +STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); + +STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen); +STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); + + +#ifdef __cplusplus +} +#endif + +// +// +//// end header file ///////////////////////////////////////////////////// +#endif // STBI_INCLUDE_STB_IMAGE_H + +#ifdef STB_IMAGE_IMPLEMENTATION + +#if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \ + || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \ + || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \ + || defined(STBI_ONLY_ZLIB) + #ifndef STBI_ONLY_JPEG + #define STBI_NO_JPEG + #endif + #ifndef STBI_ONLY_PNG + #define STBI_NO_PNG + #endif + #ifndef STBI_ONLY_BMP + #define STBI_NO_BMP + #endif + #ifndef STBI_ONLY_PSD + #define STBI_NO_PSD + #endif + #ifndef STBI_ONLY_TGA + #define STBI_NO_TGA + #endif + #ifndef STBI_ONLY_GIF + #define STBI_NO_GIF + #endif + #ifndef STBI_ONLY_HDR + #define STBI_NO_HDR + #endif + #ifndef STBI_ONLY_PIC + #define STBI_NO_PIC + #endif + #ifndef STBI_ONLY_PNM + #define STBI_NO_PNM + #endif +#endif + +#if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && !defined(STBI_NO_ZLIB) +#define STBI_NO_ZLIB +#endif + + +#include +#include // ptrdiff_t on osx +#include +#include +#include + +#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) +#include // ldexp, pow +#endif + +#ifndef STBI_NO_STDIO +#include +#endif + +#ifndef STBI_ASSERT +#include +#define STBI_ASSERT(x) assert(x) +#endif + +#ifdef __cplusplus +#define STBI_EXTERN extern "C" +#else +#define STBI_EXTERN extern +#endif + + +#ifndef _MSC_VER + #ifdef __cplusplus + #define stbi_inline inline + #else + #define stbi_inline + #endif +#else + #define stbi_inline __forceinline +#endif + +#ifndef STBI_NO_THREAD_LOCALS + #if defined(__cplusplus) && __cplusplus >= 201103L + #define STBI_THREAD_LOCAL thread_local + #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L + #define STBI_THREAD_LOCAL _Thread_local + #elif defined(__GNUC__) + #define STBI_THREAD_LOCAL __thread + #elif defined(_MSC_VER) + #define STBI_THREAD_LOCAL __declspec(thread) +#endif +#endif + +#ifdef _MSC_VER +typedef unsigned short stbi__uint16; +typedef signed short stbi__int16; +typedef unsigned int stbi__uint32; +typedef signed int stbi__int32; +#else +#include +typedef uint16_t stbi__uint16; +typedef int16_t stbi__int16; +typedef uint32_t stbi__uint32; +typedef int32_t stbi__int32; +#endif + +// should produce compiler error if size is wrong +typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1]; + +#ifdef _MSC_VER +#define STBI_NOTUSED(v) (void)(v) +#else +#define STBI_NOTUSED(v) (void)sizeof(v) +#endif + +#ifdef _MSC_VER +#define STBI_HAS_LROTL +#endif + +#ifdef STBI_HAS_LROTL + #define stbi_lrot(x,y) _lrotl(x,y) +#else + #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y)))) +#endif + +#if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED)) +// ok +#elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED) +// ok +#else +#error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)." +#endif + +#ifndef STBI_MALLOC +#define STBI_MALLOC(sz) malloc(sz) +#define STBI_REALLOC(p,newsz) realloc(p,newsz) +#define STBI_FREE(p) free(p) +#endif + +#ifndef STBI_REALLOC_SIZED +#define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz) +#endif + +// x86/x64 detection +#if defined(__x86_64__) || defined(_M_X64) +#define STBI__X64_TARGET +#elif defined(__i386) || defined(_M_IX86) +#define STBI__X86_TARGET +#endif + +#if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && !defined(STBI_NO_SIMD) +// gcc doesn't support sse2 intrinsics unless you compile with -msse2, +// which in turn means it gets to use SSE2 everywhere. This is unfortunate, +// but previous attempts to provide the SSE2 functions with runtime +// detection caused numerous issues. The way architecture extensions are +// exposed in GCC/Clang is, sadly, not really suited for one-file libs. +// New behavior: if compiled with -msse2, we use SSE2 without any +// detection; if not, we don't use it at all. +#define STBI_NO_SIMD +#endif + +#if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD) +// Note that __MINGW32__ doesn't actually mean 32-bit, so we have to avoid STBI__X64_TARGET +// +// 32-bit MinGW wants ESP to be 16-byte aligned, but this is not in the +// Windows ABI and VC++ as well as Windows DLLs don't maintain that invariant. +// As a result, enabling SSE2 on 32-bit MinGW is dangerous when not +// simultaneously enabling "-mstackrealign". +// +// See https://github.com/nothings/stb/issues/81 for more information. +// +// So default to no SSE2 on 32-bit MinGW. If you've read this far and added +// -mstackrealign to your build settings, feel free to #define STBI_MINGW_ENABLE_SSE2. +#define STBI_NO_SIMD +#endif + +#if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET)) +#define STBI_SSE2 +#include + +#ifdef _MSC_VER + +#if _MSC_VER >= 1400 // not VC6 +#include // __cpuid +static int stbi__cpuid3(void) +{ + int info[4]; + __cpuid(info,1); + return info[3]; +} +#else +static int stbi__cpuid3(void) +{ + int res; + __asm { + mov eax,1 + cpuid + mov res,edx + } + return res; +} +#endif + +#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name + +#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2) +static int stbi__sse2_available(void) +{ + int info3 = stbi__cpuid3(); + return ((info3 >> 26) & 1) != 0; +} +#endif + +#else // assume GCC-style if not VC++ +#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16))) + +#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2) +static int stbi__sse2_available(void) +{ + // If we're even attempting to compile this on GCC/Clang, that means + // -msse2 is on, which means the compiler is allowed to use SSE2 + // instructions at will, and so are we. + return 1; +} +#endif + +#endif +#endif + +// ARM NEON +#if defined(STBI_NO_SIMD) && defined(STBI_NEON) +#undef STBI_NEON +#endif + +#ifdef STBI_NEON +#include +// assume GCC or Clang on ARM targets +#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16))) +#endif + +#ifndef STBI_SIMD_ALIGN +#define STBI_SIMD_ALIGN(type, name) type name +#endif + +/////////////////////////////////////////////// +// +// stbi__context struct and start_xxx functions + +// stbi__context structure is our basic context used by all images, so it +// contains all the IO context, plus some basic image information +typedef struct +{ + stbi__uint32 img_x, img_y; + int img_n, img_out_n; + + stbi_io_callbacks io; + void *io_user_data; + + int read_from_callbacks; + int buflen; + stbi_uc buffer_start[128]; + + stbi_uc *img_buffer, *img_buffer_end; + stbi_uc *img_buffer_original, *img_buffer_original_end; +} stbi__context; + + +static void stbi__refill_buffer(stbi__context *s); + +// initialize a memory-decode context +static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len) +{ + s->io.read = NULL; + s->read_from_callbacks = 0; + s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer; + s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len; +} + +// initialize a callback-based context +static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user) +{ + s->io = *c; + s->io_user_data = user; + s->buflen = sizeof(s->buffer_start); + s->read_from_callbacks = 1; + s->img_buffer_original = s->buffer_start; + stbi__refill_buffer(s); + s->img_buffer_original_end = s->img_buffer_end; +} + +#ifndef STBI_NO_STDIO + +static int stbi__stdio_read(void *user, char *data, int size) +{ + return (int) fread(data,1,size,(FILE*) user); +} + +static void stbi__stdio_skip(void *user, int n) +{ + fseek((FILE*) user, n, SEEK_CUR); +} + +static int stbi__stdio_eof(void *user) +{ + return feof((FILE*) user); +} + +static stbi_io_callbacks stbi__stdio_callbacks = +{ + stbi__stdio_read, + stbi__stdio_skip, + stbi__stdio_eof, +}; + +static void stbi__start_file(stbi__context *s, FILE *f) +{ + stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f); +} + +//static void stop_file(stbi__context *s) { } + +#endif // !STBI_NO_STDIO + +static void stbi__rewind(stbi__context *s) +{ + // conceptually rewind SHOULD rewind to the beginning of the stream, + // but we just rewind to the beginning of the initial buffer, because + // we only use it after doing 'test', which only ever looks at at most 92 bytes + s->img_buffer = s->img_buffer_original; + s->img_buffer_end = s->img_buffer_original_end; +} + +enum +{ + STBI_ORDER_RGB, + STBI_ORDER_BGR +}; + +typedef struct +{ + int bits_per_channel; + int num_channels; + int channel_order; +} stbi__result_info; + +#ifndef STBI_NO_JPEG +static int stbi__jpeg_test(stbi__context *s); +static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_PNG +static int stbi__png_test(stbi__context *s); +static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp); +static int stbi__png_is16(stbi__context *s); +#endif + +#ifndef STBI_NO_BMP +static int stbi__bmp_test(stbi__context *s); +static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_TGA +static int stbi__tga_test(stbi__context *s); +static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_PSD +static int stbi__psd_test(stbi__context *s); +static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc); +static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp); +static int stbi__psd_is16(stbi__context *s); +#endif + +#ifndef STBI_NO_HDR +static int stbi__hdr_test(stbi__context *s); +static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_PIC +static int stbi__pic_test(stbi__context *s); +static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_GIF +static int stbi__gif_test(stbi__context *s); +static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp); +static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_PNM +static int stbi__pnm_test(stbi__context *s); +static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +static +#ifdef STBI_THREAD_LOCAL +STBI_THREAD_LOCAL +#endif +const char *stbi__g_failure_reason; + +STBIDEF const char *stbi_failure_reason(void) +{ + return stbi__g_failure_reason; +} + +#ifndef STBI_NO_FAILURE_STRINGS +static int stbi__err(const char *str) +{ + stbi__g_failure_reason = str; + return 0; +} +#endif + +static void *stbi__malloc(size_t size) +{ + return STBI_MALLOC(size); +} + +// stb_image uses ints pervasively, including for offset calculations. +// therefore the largest decoded image size we can support with the +// current code, even on 64-bit targets, is INT_MAX. this is not a +// significant limitation for the intended use case. +// +// we do, however, need to make sure our size calculations don't +// overflow. hence a few helper functions for size calculations that +// multiply integers together, making sure that they're non-negative +// and no overflow occurs. + +// return 1 if the sum is valid, 0 on overflow. +// negative terms are considered invalid. +static int stbi__addsizes_valid(int a, int b) +{ + if (b < 0) return 0; + // now 0 <= b <= INT_MAX, hence also + // 0 <= INT_MAX - b <= INTMAX. + // And "a + b <= INT_MAX" (which might overflow) is the + // same as a <= INT_MAX - b (no overflow) + return a <= INT_MAX - b; +} + +// returns 1 if the product is valid, 0 on overflow. +// negative factors are considered invalid. +static int stbi__mul2sizes_valid(int a, int b) +{ + if (a < 0 || b < 0) return 0; + if (b == 0) return 1; // mul-by-0 is always safe + // portable way to check for no overflows in a*b + return a <= INT_MAX/b; +} + +#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) +// returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow +static int stbi__mad2sizes_valid(int a, int b, int add) +{ + return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add); +} +#endif + +// returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow +static int stbi__mad3sizes_valid(int a, int b, int c, int add) +{ + return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && + stbi__addsizes_valid(a*b*c, add); +} + +// returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow +#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) +static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add) +{ + return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && + stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add); +} +#endif + +#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) +// mallocs with size overflow checking +static void *stbi__malloc_mad2(int a, int b, int add) +{ + if (!stbi__mad2sizes_valid(a, b, add)) return NULL; + return stbi__malloc(a*b + add); +} +#endif + +static void *stbi__malloc_mad3(int a, int b, int c, int add) +{ + if (!stbi__mad3sizes_valid(a, b, c, add)) return NULL; + return stbi__malloc(a*b*c + add); +} + +#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) +static void *stbi__malloc_mad4(int a, int b, int c, int d, int add) +{ + if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL; + return stbi__malloc(a*b*c*d + add); +} +#endif + +// stbi__err - error +// stbi__errpf - error returning pointer to float +// stbi__errpuc - error returning pointer to unsigned char + +#ifdef STBI_NO_FAILURE_STRINGS + #define stbi__err(x,y) 0 +#elif defined(STBI_FAILURE_USERMSG) + #define stbi__err(x,y) stbi__err(y) +#else + #define stbi__err(x,y) stbi__err(x) +#endif + +#define stbi__errpf(x,y) ((float *)(size_t) (stbi__err(x,y)?NULL:NULL)) +#define stbi__errpuc(x,y) ((unsigned char *)(size_t) (stbi__err(x,y)?NULL:NULL)) + +STBIDEF void stbi_image_free(void *retval_from_stbi_load) +{ + STBI_FREE(retval_from_stbi_load); +} + +#ifndef STBI_NO_LINEAR +static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp); +#endif + +#ifndef STBI_NO_HDR +static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp); +#endif + +static int stbi__vertically_flip_on_load_global = 0; + +STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip) +{ + stbi__vertically_flip_on_load_global = flag_true_if_should_flip; +} + +#ifndef STBI_THREAD_LOCAL +#define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global +#else +static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set; + +STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip) +{ + stbi__vertically_flip_on_load_local = flag_true_if_should_flip; + stbi__vertically_flip_on_load_set = 1; +} + +#define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \ + ? stbi__vertically_flip_on_load_local \ + : stbi__vertically_flip_on_load_global) +#endif // STBI_THREAD_LOCAL + +static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc) +{ + memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields + ri->bits_per_channel = 8; // default is 8 so most paths don't have to be changed + ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order + ri->num_channels = 0; + + #ifndef STBI_NO_JPEG + if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_PNG + if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_BMP + if (stbi__bmp_test(s)) return stbi__bmp_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_GIF + if (stbi__gif_test(s)) return stbi__gif_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_PSD + if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp, ri, bpc); + #else + STBI_NOTUSED(bpc); + #endif + #ifndef STBI_NO_PIC + if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_PNM + if (stbi__pnm_test(s)) return stbi__pnm_load(s,x,y,comp,req_comp, ri); + #endif + + #ifndef STBI_NO_HDR + if (stbi__hdr_test(s)) { + float *hdr = stbi__hdr_load(s, x,y,comp,req_comp, ri); + return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp); + } + #endif + + #ifndef STBI_NO_TGA + // test tga last because it's a crappy test! + if (stbi__tga_test(s)) + return stbi__tga_load(s,x,y,comp,req_comp, ri); + #endif + + return stbi__errpuc("unknown image type", "Image not of any known type, or corrupt"); +} + +static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels) +{ + int i; + int img_len = w * h * channels; + stbi_uc *reduced; + + reduced = (stbi_uc *) stbi__malloc(img_len); + if (reduced == NULL) return stbi__errpuc("outofmem", "Out of memory"); + + for (i = 0; i < img_len; ++i) + reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16->8 bit scaling + + STBI_FREE(orig); + return reduced; +} + +static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels) +{ + int i; + int img_len = w * h * channels; + stbi__uint16 *enlarged; + + enlarged = (stbi__uint16 *) stbi__malloc(img_len*2); + if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory"); + + for (i = 0; i < img_len; ++i) + enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff + + STBI_FREE(orig); + return enlarged; +} + +static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel) +{ + int row; + size_t bytes_per_row = (size_t)w * bytes_per_pixel; + stbi_uc temp[2048]; + stbi_uc *bytes = (stbi_uc *)image; + + for (row = 0; row < (h>>1); row++) { + stbi_uc *row0 = bytes + row*bytes_per_row; + stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row; + // swap row0 with row1 + size_t bytes_left = bytes_per_row; + while (bytes_left) { + size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp); + memcpy(temp, row0, bytes_copy); + memcpy(row0, row1, bytes_copy); + memcpy(row1, temp, bytes_copy); + row0 += bytes_copy; + row1 += bytes_copy; + bytes_left -= bytes_copy; + } + } +} + +#ifndef STBI_NO_GIF +static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel) +{ + int slice; + int slice_size = w * h * bytes_per_pixel; + + stbi_uc *bytes = (stbi_uc *)image; + for (slice = 0; slice < z; ++slice) { + stbi__vertical_flip(bytes, w, h, bytes_per_pixel); + bytes += slice_size; + } +} +#endif + +static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) +{ + stbi__result_info ri; + void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8); + + if (result == NULL) + return NULL; + + if (ri.bits_per_channel != 8) { + STBI_ASSERT(ri.bits_per_channel == 16); + result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp); + ri.bits_per_channel = 8; + } + + // @TODO: move stbi__convert_format to here + + if (stbi__vertically_flip_on_load) { + int channels = req_comp ? req_comp : *comp; + stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc)); + } + + return (unsigned char *) result; +} + +static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) +{ + stbi__result_info ri; + void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16); + + if (result == NULL) + return NULL; + + if (ri.bits_per_channel != 16) { + STBI_ASSERT(ri.bits_per_channel == 8); + result = stbi__convert_8_to_16((stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp); + ri.bits_per_channel = 16; + } + + // @TODO: move stbi__convert_format16 to here + // @TODO: special case RGB-to-Y (and RGBA-to-YA) for 8-bit-to-16-bit case to keep more precision + + if (stbi__vertically_flip_on_load) { + int channels = req_comp ? req_comp : *comp; + stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi__uint16)); + } + + return (stbi__uint16 *) result; +} + +#if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR) +static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp) +{ + if (stbi__vertically_flip_on_load && result != NULL) { + int channels = req_comp ? req_comp : *comp; + stbi__vertical_flip(result, *x, *y, channels * sizeof(float)); + } +} +#endif + +#ifndef STBI_NO_STDIO + +#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) +STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide); +STBI_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default); +#endif + +#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) +STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input) +{ + return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL); +} +#endif + +static FILE *stbi__fopen(char const *filename, char const *mode) +{ + FILE *f; +#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) + wchar_t wMode[64]; + wchar_t wFilename[1024]; + if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename))) + return 0; + + if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode))) + return 0; + +#if _MSC_VER >= 1400 + if (0 != _wfopen_s(&f, wFilename, wMode)) + f = 0; +#else + f = _wfopen(wFilename, wMode); +#endif + +#elif defined(_MSC_VER) && _MSC_VER >= 1400 + if (0 != fopen_s(&f, filename, mode)) + f=0; +#else + f = fopen(filename, mode); +#endif + return f; +} + + +STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp) +{ + FILE *f = stbi__fopen(filename, "rb"); + unsigned char *result; + if (!f) return stbi__errpuc("can't fopen", "Unable to open file"); + result = stbi_load_from_file(f,x,y,comp,req_comp); + fclose(f); + return result; +} + +STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) +{ + unsigned char *result; + stbi__context s; + stbi__start_file(&s,f); + result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); + if (result) { + // need to 'unget' all the characters in the IO buffer + fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR); + } + return result; +} + +STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f, int *x, int *y, int *comp, int req_comp) +{ + stbi__uint16 *result; + stbi__context s; + stbi__start_file(&s,f); + result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp); + if (result) { + // need to 'unget' all the characters in the IO buffer + fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR); + } + return result; +} + +STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *comp, int req_comp) +{ + FILE *f = stbi__fopen(filename, "rb"); + stbi__uint16 *result; + if (!f) return (stbi_us *) stbi__errpuc("can't fopen", "Unable to open file"); + result = stbi_load_from_file_16(f,x,y,comp,req_comp); + fclose(f); + return result; +} + + +#endif //!STBI_NO_STDIO + +STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); +} + +STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user); + return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); +} + +STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); +} + +STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); + return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); +} + +#ifndef STBI_NO_GIF +STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp) +{ + unsigned char *result; + stbi__context s; + stbi__start_mem(&s,buffer,len); + + result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp); + if (stbi__vertically_flip_on_load) { + stbi__vertical_flip_slices( result, *x, *y, *z, *comp ); + } + + return result; +} +#endif + +#ifndef STBI_NO_LINEAR +static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp) +{ + unsigned char *data; + #ifndef STBI_NO_HDR + if (stbi__hdr_test(s)) { + stbi__result_info ri; + float *hdr_data = stbi__hdr_load(s,x,y,comp,req_comp, &ri); + if (hdr_data) + stbi__float_postprocess(hdr_data,x,y,comp,req_comp); + return hdr_data; + } + #endif + data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp); + if (data) + return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp); + return stbi__errpf("unknown image type", "Image not of any known type, or corrupt"); +} + +STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__loadf_main(&s,x,y,comp,req_comp); +} + +STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); + return stbi__loadf_main(&s,x,y,comp,req_comp); +} + +#ifndef STBI_NO_STDIO +STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp) +{ + float *result; + FILE *f = stbi__fopen(filename, "rb"); + if (!f) return stbi__errpf("can't fopen", "Unable to open file"); + result = stbi_loadf_from_file(f,x,y,comp,req_comp); + fclose(f); + return result; +} + +STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_file(&s,f); + return stbi__loadf_main(&s,x,y,comp,req_comp); +} +#endif // !STBI_NO_STDIO + +#endif // !STBI_NO_LINEAR + +// these is-hdr-or-not is defined independent of whether STBI_NO_LINEAR is +// defined, for API simplicity; if STBI_NO_LINEAR is defined, it always +// reports false! + +STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len) +{ + #ifndef STBI_NO_HDR + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__hdr_test(&s); + #else + STBI_NOTUSED(buffer); + STBI_NOTUSED(len); + return 0; + #endif +} + +#ifndef STBI_NO_STDIO +STBIDEF int stbi_is_hdr (char const *filename) +{ + FILE *f = stbi__fopen(filename, "rb"); + int result=0; + if (f) { + result = stbi_is_hdr_from_file(f); + fclose(f); + } + return result; +} + +STBIDEF int stbi_is_hdr_from_file(FILE *f) +{ + #ifndef STBI_NO_HDR + long pos = ftell(f); + int res; + stbi__context s; + stbi__start_file(&s,f); + res = stbi__hdr_test(&s); + fseek(f, pos, SEEK_SET); + return res; + #else + STBI_NOTUSED(f); + return 0; + #endif +} +#endif // !STBI_NO_STDIO + +STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user) +{ + #ifndef STBI_NO_HDR + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); + return stbi__hdr_test(&s); + #else + STBI_NOTUSED(clbk); + STBI_NOTUSED(user); + return 0; + #endif +} + +#ifndef STBI_NO_LINEAR +static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f; + +STBIDEF void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; } +STBIDEF void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; } +#endif + +static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f; + +STBIDEF void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1/gamma; } +STBIDEF void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1/scale; } + + +////////////////////////////////////////////////////////////////////////////// +// +// Common code used by all image loaders +// + +enum +{ + STBI__SCAN_load=0, + STBI__SCAN_type, + STBI__SCAN_header +}; + +static void stbi__refill_buffer(stbi__context *s) +{ + int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen); + if (n == 0) { + // at end of file, treat same as if from memory, but need to handle case + // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file + s->read_from_callbacks = 0; + s->img_buffer = s->buffer_start; + s->img_buffer_end = s->buffer_start+1; + *s->img_buffer = 0; + } else { + s->img_buffer = s->buffer_start; + s->img_buffer_end = s->buffer_start + n; + } +} + +stbi_inline static stbi_uc stbi__get8(stbi__context *s) +{ + if (s->img_buffer < s->img_buffer_end) + return *s->img_buffer++; + if (s->read_from_callbacks) { + stbi__refill_buffer(s); + return *s->img_buffer++; + } + return 0; +} + +#if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) +// nothing +#else +stbi_inline static int stbi__at_eof(stbi__context *s) +{ + if (s->io.read) { + if (!(s->io.eof)(s->io_user_data)) return 0; + // if feof() is true, check if buffer = end + // special case: we've only got the special 0 character at the end + if (s->read_from_callbacks == 0) return 1; + } + + return s->img_buffer >= s->img_buffer_end; +} +#endif + +#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) +// nothing +#else +static void stbi__skip(stbi__context *s, int n) +{ + if (n < 0) { + s->img_buffer = s->img_buffer_end; + return; + } + if (s->io.read) { + int blen = (int) (s->img_buffer_end - s->img_buffer); + if (blen < n) { + s->img_buffer = s->img_buffer_end; + (s->io.skip)(s->io_user_data, n - blen); + return; + } + } + s->img_buffer += n; +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM) +// nothing +#else +static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n) +{ + if (s->io.read) { + int blen = (int) (s->img_buffer_end - s->img_buffer); + if (blen < n) { + int res, count; + + memcpy(buffer, s->img_buffer, blen); + + count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen); + res = (count == (n-blen)); + s->img_buffer = s->img_buffer_end; + return res; + } + } + + if (s->img_buffer+n <= s->img_buffer_end) { + memcpy(buffer, s->img_buffer, n); + s->img_buffer += n; + return 1; + } else + return 0; +} +#endif + +#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC) +// nothing +#else +static int stbi__get16be(stbi__context *s) +{ + int z = stbi__get8(s); + return (z << 8) + stbi__get8(s); +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC) +// nothing +#else +static stbi__uint32 stbi__get32be(stbi__context *s) +{ + stbi__uint32 z = stbi__get16be(s); + return (z << 16) + stbi__get16be(s); +} +#endif + +#if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) +// nothing +#else +static int stbi__get16le(stbi__context *s) +{ + int z = stbi__get8(s); + return z + (stbi__get8(s) << 8); +} +#endif + +#ifndef STBI_NO_BMP +static stbi__uint32 stbi__get32le(stbi__context *s) +{ + stbi__uint32 z = stbi__get16le(s); + return z + (stbi__get16le(s) << 16); +} +#endif + +#define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings + +#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) +// nothing +#else +////////////////////////////////////////////////////////////////////////////// +// +// generic converter from built-in img_n to req_comp +// individual types do this automatically as much as possible (e.g. jpeg +// does all cases internally since it needs to colorspace convert anyway, +// and it never has alpha, so very few cases ). png can automatically +// interleave an alpha=255 channel, but falls back to this for other cases +// +// assume data buffer is malloced, so malloc a new one and free that one +// only failure mode is malloc failing + +static stbi_uc stbi__compute_y(int r, int g, int b) +{ + return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8); +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) +// nothing +#else +static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y) +{ + int i,j; + unsigned char *good; + + if (req_comp == img_n) return data; + STBI_ASSERT(req_comp >= 1 && req_comp <= 4); + + good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0); + if (good == NULL) { + STBI_FREE(data); + return stbi__errpuc("outofmem", "Out of memory"); + } + + for (j=0; j < (int) y; ++j) { + unsigned char *src = data + j * x * img_n ; + unsigned char *dest = good + j * x * req_comp; + + #define STBI__COMBO(a,b) ((a)*8+(b)) + #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b) + // convert source image with img_n components to one with req_comp components; + // avoid switch per pixel, so use switch per scanline and massive macros + switch (STBI__COMBO(img_n, req_comp)) { + STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; } break; + STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; + STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; } break; + STBI__CASE(2,1) { dest[0]=src[0]; } break; + STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; + STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break; + STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; } break; + STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break; + STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; } break; + STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break; + STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; } break; + STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break; + default: STBI_ASSERT(0); + } + #undef STBI__CASE + } + + STBI_FREE(data); + return good; +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) +// nothing +#else +static stbi__uint16 stbi__compute_y_16(int r, int g, int b) +{ + return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8); +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) +// nothing +#else +static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y) +{ + int i,j; + stbi__uint16 *good; + + if (req_comp == img_n) return data; + STBI_ASSERT(req_comp >= 1 && req_comp <= 4); + + good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2); + if (good == NULL) { + STBI_FREE(data); + return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory"); + } + + for (j=0; j < (int) y; ++j) { + stbi__uint16 *src = data + j * x * img_n ; + stbi__uint16 *dest = good + j * x * req_comp; + + #define STBI__COMBO(a,b) ((a)*8+(b)) + #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b) + // convert source image with img_n components to one with req_comp components; + // avoid switch per pixel, so use switch per scanline and massive macros + switch (STBI__COMBO(img_n, req_comp)) { + STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; } break; + STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; + STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; } break; + STBI__CASE(2,1) { dest[0]=src[0]; } break; + STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; + STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break; + STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; } break; + STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break; + STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; } break; + STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break; + STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; } break; + STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break; + default: STBI_ASSERT(0); + } + #undef STBI__CASE + } + + STBI_FREE(data); + return good; +} +#endif + +#ifndef STBI_NO_LINEAR +static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp) +{ + int i,k,n; + float *output; + if (!data) return NULL; + output = (float *) stbi__malloc_mad4(x, y, comp, sizeof(float), 0); + if (output == NULL) { STBI_FREE(data); return stbi__errpf("outofmem", "Out of memory"); } + // compute number of non-alpha components + if (comp & 1) n = comp; else n = comp-1; + for (i=0; i < x*y; ++i) { + for (k=0; k < n; ++k) { + output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale); + } + } + if (n < comp) { + for (i=0; i < x*y; ++i) { + output[i*comp + n] = data[i*comp + n]/255.0f; + } + } + STBI_FREE(data); + return output; +} +#endif + +#ifndef STBI_NO_HDR +#define stbi__float2int(x) ((int) (x)) +static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp) +{ + int i,k,n; + stbi_uc *output; + if (!data) return NULL; + output = (stbi_uc *) stbi__malloc_mad3(x, y, comp, 0); + if (output == NULL) { STBI_FREE(data); return stbi__errpuc("outofmem", "Out of memory"); } + // compute number of non-alpha components + if (comp & 1) n = comp; else n = comp-1; + for (i=0; i < x*y; ++i) { + for (k=0; k < n; ++k) { + float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f; + if (z < 0) z = 0; + if (z > 255) z = 255; + output[i*comp + k] = (stbi_uc) stbi__float2int(z); + } + if (k < comp) { + float z = data[i*comp+k] * 255 + 0.5f; + if (z < 0) z = 0; + if (z > 255) z = 255; + output[i*comp + k] = (stbi_uc) stbi__float2int(z); + } + } + STBI_FREE(data); + return output; +} +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// "baseline" JPEG/JFIF decoder +// +// simple implementation +// - doesn't support delayed output of y-dimension +// - simple interface (only one output format: 8-bit interleaved RGB) +// - doesn't try to recover corrupt jpegs +// - doesn't allow partial loading, loading multiple at once +// - still fast on x86 (copying globals into locals doesn't help x86) +// - allocates lots of intermediate memory (full size of all components) +// - non-interleaved case requires this anyway +// - allows good upsampling (see next) +// high-quality +// - upsampled channels are bilinearly interpolated, even across blocks +// - quality integer IDCT derived from IJG's 'slow' +// performance +// - fast huffman; reasonable integer IDCT +// - some SIMD kernels for common paths on targets with SSE2/NEON +// - uses a lot of intermediate memory, could cache poorly + +#ifndef STBI_NO_JPEG + +// huffman decoding acceleration +#define FAST_BITS 9 // larger handles more cases; smaller stomps less cache + +typedef struct +{ + stbi_uc fast[1 << FAST_BITS]; + // weirdly, repacking this into AoS is a 10% speed loss, instead of a win + stbi__uint16 code[256]; + stbi_uc values[256]; + stbi_uc size[257]; + unsigned int maxcode[18]; + int delta[17]; // old 'firstsymbol' - old 'firstcode' +} stbi__huffman; + +typedef struct +{ + stbi__context *s; + stbi__huffman huff_dc[4]; + stbi__huffman huff_ac[4]; + stbi__uint16 dequant[4][64]; + stbi__int16 fast_ac[4][1 << FAST_BITS]; + +// sizes for components, interleaved MCUs + int img_h_max, img_v_max; + int img_mcu_x, img_mcu_y; + int img_mcu_w, img_mcu_h; + +// definition of jpeg image component + struct + { + int id; + int h,v; + int tq; + int hd,ha; + int dc_pred; + + int x,y,w2,h2; + stbi_uc *data; + void *raw_data, *raw_coeff; + stbi_uc *linebuf; + short *coeff; // progressive only + int coeff_w, coeff_h; // number of 8x8 coefficient blocks + } img_comp[4]; + + stbi__uint32 code_buffer; // jpeg entropy-coded buffer + int code_bits; // number of valid bits + unsigned char marker; // marker seen while filling entropy buffer + int nomore; // flag if we saw a marker so must stop + + int progressive; + int spec_start; + int spec_end; + int succ_high; + int succ_low; + int eob_run; + int jfif; + int app14_color_transform; // Adobe APP14 tag + int rgb; + + int scan_n, order[4]; + int restart_interval, todo; + +// kernels + void (*idct_block_kernel)(stbi_uc *out, int out_stride, short data[64]); + void (*YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step); + stbi_uc *(*resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs); +} stbi__jpeg; + +static int stbi__build_huffman(stbi__huffman *h, int *count) +{ + int i,j,k=0; + unsigned int code; + // build size list for each symbol (from JPEG spec) + for (i=0; i < 16; ++i) + for (j=0; j < count[i]; ++j) + h->size[k++] = (stbi_uc) (i+1); + h->size[k] = 0; + + // compute actual symbols (from jpeg spec) + code = 0; + k = 0; + for(j=1; j <= 16; ++j) { + // compute delta to add to code to compute symbol id + h->delta[j] = k - code; + if (h->size[k] == j) { + while (h->size[k] == j) + h->code[k++] = (stbi__uint16) (code++); + if (code-1 >= (1u << j)) return stbi__err("bad code lengths","Corrupt JPEG"); + } + // compute largest code + 1 for this size, preshifted as needed later + h->maxcode[j] = code << (16-j); + code <<= 1; + } + h->maxcode[j] = 0xffffffff; + + // build non-spec acceleration table; 255 is flag for not-accelerated + memset(h->fast, 255, 1 << FAST_BITS); + for (i=0; i < k; ++i) { + int s = h->size[i]; + if (s <= FAST_BITS) { + int c = h->code[i] << (FAST_BITS-s); + int m = 1 << (FAST_BITS-s); + for (j=0; j < m; ++j) { + h->fast[c+j] = (stbi_uc) i; + } + } + } + return 1; +} + +// build a table that decodes both magnitude and value of small ACs in +// one go. +static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h) +{ + int i; + for (i=0; i < (1 << FAST_BITS); ++i) { + stbi_uc fast = h->fast[i]; + fast_ac[i] = 0; + if (fast < 255) { + int rs = h->values[fast]; + int run = (rs >> 4) & 15; + int magbits = rs & 15; + int len = h->size[fast]; + + if (magbits && len + magbits <= FAST_BITS) { + // magnitude code followed by receive_extend code + int k = ((i << len) & ((1 << FAST_BITS) - 1)) >> (FAST_BITS - magbits); + int m = 1 << (magbits - 1); + if (k < m) k += (~0U << magbits) + 1; + // if the result is small enough, we can fit it in fast_ac table + if (k >= -128 && k <= 127) + fast_ac[i] = (stbi__int16) ((k * 256) + (run * 16) + (len + magbits)); + } + } + } +} + +static void stbi__grow_buffer_unsafe(stbi__jpeg *j) +{ + do { + unsigned int b = j->nomore ? 0 : stbi__get8(j->s); + if (b == 0xff) { + int c = stbi__get8(j->s); + while (c == 0xff) c = stbi__get8(j->s); // consume fill bytes + if (c != 0) { + j->marker = (unsigned char) c; + j->nomore = 1; + return; + } + } + j->code_buffer |= b << (24 - j->code_bits); + j->code_bits += 8; + } while (j->code_bits <= 24); +} + +// (1 << n) - 1 +static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535}; + +// decode a jpeg huffman value from the bitstream +stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h) +{ + unsigned int temp; + int c,k; + + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + + // look at the top FAST_BITS and determine what symbol ID it is, + // if the code is <= FAST_BITS + c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); + k = h->fast[c]; + if (k < 255) { + int s = h->size[k]; + if (s > j->code_bits) + return -1; + j->code_buffer <<= s; + j->code_bits -= s; + return h->values[k]; + } + + // naive test is to shift the code_buffer down so k bits are + // valid, then test against maxcode. To speed this up, we've + // preshifted maxcode left so that it has (16-k) 0s at the + // end; in other words, regardless of the number of bits, it + // wants to be compared against something shifted to have 16; + // that way we don't need to shift inside the loop. + temp = j->code_buffer >> 16; + for (k=FAST_BITS+1 ; ; ++k) + if (temp < h->maxcode[k]) + break; + if (k == 17) { + // error! code not found + j->code_bits -= 16; + return -1; + } + + if (k > j->code_bits) + return -1; + + // convert the huffman code to the symbol id + c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k]; + STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]); + + // convert the id to a symbol + j->code_bits -= k; + j->code_buffer <<= k; + return h->values[c]; +} + +// bias[n] = (-1<code_bits < n) stbi__grow_buffer_unsafe(j); + + sgn = (stbi__int32)j->code_buffer >> 31; // sign bit is always in MSB + k = stbi_lrot(j->code_buffer, n); + STBI_ASSERT(n >= 0 && n < (int) (sizeof(stbi__bmask)/sizeof(*stbi__bmask))); + j->code_buffer = k & ~stbi__bmask[n]; + k &= stbi__bmask[n]; + j->code_bits -= n; + return k + (stbi__jbias[n] & ~sgn); +} + +// get some unsigned bits +stbi_inline static int stbi__jpeg_get_bits(stbi__jpeg *j, int n) +{ + unsigned int k; + if (j->code_bits < n) stbi__grow_buffer_unsafe(j); + k = stbi_lrot(j->code_buffer, n); + j->code_buffer = k & ~stbi__bmask[n]; + k &= stbi__bmask[n]; + j->code_bits -= n; + return k; +} + +stbi_inline static int stbi__jpeg_get_bit(stbi__jpeg *j) +{ + unsigned int k; + if (j->code_bits < 1) stbi__grow_buffer_unsafe(j); + k = j->code_buffer; + j->code_buffer <<= 1; + --j->code_bits; + return k & 0x80000000; +} + +// given a value that's at position X in the zigzag stream, +// where does it appear in the 8x8 matrix coded as row-major? +static const stbi_uc stbi__jpeg_dezigzag[64+15] = +{ + 0, 1, 8, 16, 9, 2, 3, 10, + 17, 24, 32, 25, 18, 11, 4, 5, + 12, 19, 26, 33, 40, 48, 41, 34, + 27, 20, 13, 6, 7, 14, 21, 28, + 35, 42, 49, 56, 57, 50, 43, 36, + 29, 22, 15, 23, 30, 37, 44, 51, + 58, 59, 52, 45, 38, 31, 39, 46, + 53, 60, 61, 54, 47, 55, 62, 63, + // let corrupt input sample past end + 63, 63, 63, 63, 63, 63, 63, 63, + 63, 63, 63, 63, 63, 63, 63 +}; + +// decode one 64-entry block-- +static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman *hdc, stbi__huffman *hac, stbi__int16 *fac, int b, stbi__uint16 *dequant) +{ + int diff,dc,k; + int t; + + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + t = stbi__jpeg_huff_decode(j, hdc); + if (t < 0) return stbi__err("bad huffman code","Corrupt JPEG"); + + // 0 all the ac values now so we can do it 32-bits at a time + memset(data,0,64*sizeof(data[0])); + + diff = t ? stbi__extend_receive(j, t) : 0; + dc = j->img_comp[b].dc_pred + diff; + j->img_comp[b].dc_pred = dc; + data[0] = (short) (dc * dequant[0]); + + // decode AC components, see JPEG spec + k = 1; + do { + unsigned int zig; + int c,r,s; + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); + r = fac[c]; + if (r) { // fast-AC path + k += (r >> 4) & 15; // run + s = r & 15; // combined length + j->code_buffer <<= s; + j->code_bits -= s; + // decode into unzigzag'd location + zig = stbi__jpeg_dezigzag[k++]; + data[zig] = (short) ((r >> 8) * dequant[zig]); + } else { + int rs = stbi__jpeg_huff_decode(j, hac); + if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG"); + s = rs & 15; + r = rs >> 4; + if (s == 0) { + if (rs != 0xf0) break; // end block + k += 16; + } else { + k += r; + // decode into unzigzag'd location + zig = stbi__jpeg_dezigzag[k++]; + data[zig] = (short) (stbi__extend_receive(j,s) * dequant[zig]); + } + } + } while (k < 64); + return 1; +} + +static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__huffman *hdc, int b) +{ + int diff,dc; + int t; + if (j->spec_end != 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); + + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + + if (j->succ_high == 0) { + // first scan for DC coefficient, must be first + memset(data,0,64*sizeof(data[0])); // 0 all the ac values now + t = stbi__jpeg_huff_decode(j, hdc); + diff = t ? stbi__extend_receive(j, t) : 0; + + dc = j->img_comp[b].dc_pred + diff; + j->img_comp[b].dc_pred = dc; + data[0] = (short) (dc << j->succ_low); + } else { + // refinement scan for DC coefficient + if (stbi__jpeg_get_bit(j)) + data[0] += (short) (1 << j->succ_low); + } + return 1; +} + +// @OPTIMIZE: store non-zigzagged during the decode passes, +// and only de-zigzag when dequantizing +static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__huffman *hac, stbi__int16 *fac) +{ + int k; + if (j->spec_start == 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); + + if (j->succ_high == 0) { + int shift = j->succ_low; + + if (j->eob_run) { + --j->eob_run; + return 1; + } + + k = j->spec_start; + do { + unsigned int zig; + int c,r,s; + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); + r = fac[c]; + if (r) { // fast-AC path + k += (r >> 4) & 15; // run + s = r & 15; // combined length + j->code_buffer <<= s; + j->code_bits -= s; + zig = stbi__jpeg_dezigzag[k++]; + data[zig] = (short) ((r >> 8) << shift); + } else { + int rs = stbi__jpeg_huff_decode(j, hac); + if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG"); + s = rs & 15; + r = rs >> 4; + if (s == 0) { + if (r < 15) { + j->eob_run = (1 << r); + if (r) + j->eob_run += stbi__jpeg_get_bits(j, r); + --j->eob_run; + break; + } + k += 16; + } else { + k += r; + zig = stbi__jpeg_dezigzag[k++]; + data[zig] = (short) (stbi__extend_receive(j,s) << shift); + } + } + } while (k <= j->spec_end); + } else { + // refinement scan for these AC coefficients + + short bit = (short) (1 << j->succ_low); + + if (j->eob_run) { + --j->eob_run; + for (k = j->spec_start; k <= j->spec_end; ++k) { + short *p = &data[stbi__jpeg_dezigzag[k]]; + if (*p != 0) + if (stbi__jpeg_get_bit(j)) + if ((*p & bit)==0) { + if (*p > 0) + *p += bit; + else + *p -= bit; + } + } + } else { + k = j->spec_start; + do { + int r,s; + int rs = stbi__jpeg_huff_decode(j, hac); // @OPTIMIZE see if we can use the fast path here, advance-by-r is so slow, eh + if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG"); + s = rs & 15; + r = rs >> 4; + if (s == 0) { + if (r < 15) { + j->eob_run = (1 << r) - 1; + if (r) + j->eob_run += stbi__jpeg_get_bits(j, r); + r = 64; // force end of block + } else { + // r=15 s=0 should write 16 0s, so we just do + // a run of 15 0s and then write s (which is 0), + // so we don't have to do anything special here + } + } else { + if (s != 1) return stbi__err("bad huffman code", "Corrupt JPEG"); + // sign bit + if (stbi__jpeg_get_bit(j)) + s = bit; + else + s = -bit; + } + + // advance by r + while (k <= j->spec_end) { + short *p = &data[stbi__jpeg_dezigzag[k++]]; + if (*p != 0) { + if (stbi__jpeg_get_bit(j)) + if ((*p & bit)==0) { + if (*p > 0) + *p += bit; + else + *p -= bit; + } + } else { + if (r == 0) { + *p = (short) s; + break; + } + --r; + } + } + } while (k <= j->spec_end); + } + } + return 1; +} + +// take a -128..127 value and stbi__clamp it and convert to 0..255 +stbi_inline static stbi_uc stbi__clamp(int x) +{ + // trick to use a single test to catch both cases + if ((unsigned int) x > 255) { + if (x < 0) return 0; + if (x > 255) return 255; + } + return (stbi_uc) x; +} + +#define stbi__f2f(x) ((int) (((x) * 4096 + 0.5))) +#define stbi__fsh(x) ((x) * 4096) + +// derived from jidctint -- DCT_ISLOW +#define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \ + int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \ + p2 = s2; \ + p3 = s6; \ + p1 = (p2+p3) * stbi__f2f(0.5411961f); \ + t2 = p1 + p3*stbi__f2f(-1.847759065f); \ + t3 = p1 + p2*stbi__f2f( 0.765366865f); \ + p2 = s0; \ + p3 = s4; \ + t0 = stbi__fsh(p2+p3); \ + t1 = stbi__fsh(p2-p3); \ + x0 = t0+t3; \ + x3 = t0-t3; \ + x1 = t1+t2; \ + x2 = t1-t2; \ + t0 = s7; \ + t1 = s5; \ + t2 = s3; \ + t3 = s1; \ + p3 = t0+t2; \ + p4 = t1+t3; \ + p1 = t0+t3; \ + p2 = t1+t2; \ + p5 = (p3+p4)*stbi__f2f( 1.175875602f); \ + t0 = t0*stbi__f2f( 0.298631336f); \ + t1 = t1*stbi__f2f( 2.053119869f); \ + t2 = t2*stbi__f2f( 3.072711026f); \ + t3 = t3*stbi__f2f( 1.501321110f); \ + p1 = p5 + p1*stbi__f2f(-0.899976223f); \ + p2 = p5 + p2*stbi__f2f(-2.562915447f); \ + p3 = p3*stbi__f2f(-1.961570560f); \ + p4 = p4*stbi__f2f(-0.390180644f); \ + t3 += p1+p4; \ + t2 += p2+p3; \ + t1 += p2+p4; \ + t0 += p1+p3; + +static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64]) +{ + int i,val[64],*v=val; + stbi_uc *o; + short *d = data; + + // columns + for (i=0; i < 8; ++i,++d, ++v) { + // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing + if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0 + && d[40]==0 && d[48]==0 && d[56]==0) { + // no shortcut 0 seconds + // (1|2|3|4|5|6|7)==0 0 seconds + // all separate -0.047 seconds + // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds + int dcterm = d[0]*4; + v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm; + } else { + STBI__IDCT_1D(d[ 0],d[ 8],d[16],d[24],d[32],d[40],d[48],d[56]) + // constants scaled things up by 1<<12; let's bring them back + // down, but keep 2 extra bits of precision + x0 += 512; x1 += 512; x2 += 512; x3 += 512; + v[ 0] = (x0+t3) >> 10; + v[56] = (x0-t3) >> 10; + v[ 8] = (x1+t2) >> 10; + v[48] = (x1-t2) >> 10; + v[16] = (x2+t1) >> 10; + v[40] = (x2-t1) >> 10; + v[24] = (x3+t0) >> 10; + v[32] = (x3-t0) >> 10; + } + } + + for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) { + // no fast case since the first 1D IDCT spread components out + STBI__IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7]) + // constants scaled things up by 1<<12, plus we had 1<<2 from first + // loop, plus horizontal and vertical each scale by sqrt(8) so together + // we've got an extra 1<<3, so 1<<17 total we need to remove. + // so we want to round that, which means adding 0.5 * 1<<17, + // aka 65536. Also, we'll end up with -128 to 127 that we want + // to encode as 0..255 by adding 128, so we'll add that before the shift + x0 += 65536 + (128<<17); + x1 += 65536 + (128<<17); + x2 += 65536 + (128<<17); + x3 += 65536 + (128<<17); + // tried computing the shifts into temps, or'ing the temps to see + // if any were out of range, but that was slower + o[0] = stbi__clamp((x0+t3) >> 17); + o[7] = stbi__clamp((x0-t3) >> 17); + o[1] = stbi__clamp((x1+t2) >> 17); + o[6] = stbi__clamp((x1-t2) >> 17); + o[2] = stbi__clamp((x2+t1) >> 17); + o[5] = stbi__clamp((x2-t1) >> 17); + o[3] = stbi__clamp((x3+t0) >> 17); + o[4] = stbi__clamp((x3-t0) >> 17); + } +} + +#ifdef STBI_SSE2 +// sse2 integer IDCT. not the fastest possible implementation but it +// produces bit-identical results to the generic C version so it's +// fully "transparent". +static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64]) +{ + // This is constructed to match our regular (generic) integer IDCT exactly. + __m128i row0, row1, row2, row3, row4, row5, row6, row7; + __m128i tmp; + + // dot product constant: even elems=x, odd elems=y + #define dct_const(x,y) _mm_setr_epi16((x),(y),(x),(y),(x),(y),(x),(y)) + + // out(0) = c0[even]*x + c0[odd]*y (c0, x, y 16-bit, out 32-bit) + // out(1) = c1[even]*x + c1[odd]*y + #define dct_rot(out0,out1, x,y,c0,c1) \ + __m128i c0##lo = _mm_unpacklo_epi16((x),(y)); \ + __m128i c0##hi = _mm_unpackhi_epi16((x),(y)); \ + __m128i out0##_l = _mm_madd_epi16(c0##lo, c0); \ + __m128i out0##_h = _mm_madd_epi16(c0##hi, c0); \ + __m128i out1##_l = _mm_madd_epi16(c0##lo, c1); \ + __m128i out1##_h = _mm_madd_epi16(c0##hi, c1) + + // out = in << 12 (in 16-bit, out 32-bit) + #define dct_widen(out, in) \ + __m128i out##_l = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), (in)), 4); \ + __m128i out##_h = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), (in)), 4) + + // wide add + #define dct_wadd(out, a, b) \ + __m128i out##_l = _mm_add_epi32(a##_l, b##_l); \ + __m128i out##_h = _mm_add_epi32(a##_h, b##_h) + + // wide sub + #define dct_wsub(out, a, b) \ + __m128i out##_l = _mm_sub_epi32(a##_l, b##_l); \ + __m128i out##_h = _mm_sub_epi32(a##_h, b##_h) + + // butterfly a/b, add bias, then shift by "s" and pack + #define dct_bfly32o(out0, out1, a,b,bias,s) \ + { \ + __m128i abiased_l = _mm_add_epi32(a##_l, bias); \ + __m128i abiased_h = _mm_add_epi32(a##_h, bias); \ + dct_wadd(sum, abiased, b); \ + dct_wsub(dif, abiased, b); \ + out0 = _mm_packs_epi32(_mm_srai_epi32(sum_l, s), _mm_srai_epi32(sum_h, s)); \ + out1 = _mm_packs_epi32(_mm_srai_epi32(dif_l, s), _mm_srai_epi32(dif_h, s)); \ + } + + // 8-bit interleave step (for transposes) + #define dct_interleave8(a, b) \ + tmp = a; \ + a = _mm_unpacklo_epi8(a, b); \ + b = _mm_unpackhi_epi8(tmp, b) + + // 16-bit interleave step (for transposes) + #define dct_interleave16(a, b) \ + tmp = a; \ + a = _mm_unpacklo_epi16(a, b); \ + b = _mm_unpackhi_epi16(tmp, b) + + #define dct_pass(bias,shift) \ + { \ + /* even part */ \ + dct_rot(t2e,t3e, row2,row6, rot0_0,rot0_1); \ + __m128i sum04 = _mm_add_epi16(row0, row4); \ + __m128i dif04 = _mm_sub_epi16(row0, row4); \ + dct_widen(t0e, sum04); \ + dct_widen(t1e, dif04); \ + dct_wadd(x0, t0e, t3e); \ + dct_wsub(x3, t0e, t3e); \ + dct_wadd(x1, t1e, t2e); \ + dct_wsub(x2, t1e, t2e); \ + /* odd part */ \ + dct_rot(y0o,y2o, row7,row3, rot2_0,rot2_1); \ + dct_rot(y1o,y3o, row5,row1, rot3_0,rot3_1); \ + __m128i sum17 = _mm_add_epi16(row1, row7); \ + __m128i sum35 = _mm_add_epi16(row3, row5); \ + dct_rot(y4o,y5o, sum17,sum35, rot1_0,rot1_1); \ + dct_wadd(x4, y0o, y4o); \ + dct_wadd(x5, y1o, y5o); \ + dct_wadd(x6, y2o, y5o); \ + dct_wadd(x7, y3o, y4o); \ + dct_bfly32o(row0,row7, x0,x7,bias,shift); \ + dct_bfly32o(row1,row6, x1,x6,bias,shift); \ + dct_bfly32o(row2,row5, x2,x5,bias,shift); \ + dct_bfly32o(row3,row4, x3,x4,bias,shift); \ + } + + __m128i rot0_0 = dct_const(stbi__f2f(0.5411961f), stbi__f2f(0.5411961f) + stbi__f2f(-1.847759065f)); + __m128i rot0_1 = dct_const(stbi__f2f(0.5411961f) + stbi__f2f( 0.765366865f), stbi__f2f(0.5411961f)); + __m128i rot1_0 = dct_const(stbi__f2f(1.175875602f) + stbi__f2f(-0.899976223f), stbi__f2f(1.175875602f)); + __m128i rot1_1 = dct_const(stbi__f2f(1.175875602f), stbi__f2f(1.175875602f) + stbi__f2f(-2.562915447f)); + __m128i rot2_0 = dct_const(stbi__f2f(-1.961570560f) + stbi__f2f( 0.298631336f), stbi__f2f(-1.961570560f)); + __m128i rot2_1 = dct_const(stbi__f2f(-1.961570560f), stbi__f2f(-1.961570560f) + stbi__f2f( 3.072711026f)); + __m128i rot3_0 = dct_const(stbi__f2f(-0.390180644f) + stbi__f2f( 2.053119869f), stbi__f2f(-0.390180644f)); + __m128i rot3_1 = dct_const(stbi__f2f(-0.390180644f), stbi__f2f(-0.390180644f) + stbi__f2f( 1.501321110f)); + + // rounding biases in column/row passes, see stbi__idct_block for explanation. + __m128i bias_0 = _mm_set1_epi32(512); + __m128i bias_1 = _mm_set1_epi32(65536 + (128<<17)); + + // load + row0 = _mm_load_si128((const __m128i *) (data + 0*8)); + row1 = _mm_load_si128((const __m128i *) (data + 1*8)); + row2 = _mm_load_si128((const __m128i *) (data + 2*8)); + row3 = _mm_load_si128((const __m128i *) (data + 3*8)); + row4 = _mm_load_si128((const __m128i *) (data + 4*8)); + row5 = _mm_load_si128((const __m128i *) (data + 5*8)); + row6 = _mm_load_si128((const __m128i *) (data + 6*8)); + row7 = _mm_load_si128((const __m128i *) (data + 7*8)); + + // column pass + dct_pass(bias_0, 10); + + { + // 16bit 8x8 transpose pass 1 + dct_interleave16(row0, row4); + dct_interleave16(row1, row5); + dct_interleave16(row2, row6); + dct_interleave16(row3, row7); + + // transpose pass 2 + dct_interleave16(row0, row2); + dct_interleave16(row1, row3); + dct_interleave16(row4, row6); + dct_interleave16(row5, row7); + + // transpose pass 3 + dct_interleave16(row0, row1); + dct_interleave16(row2, row3); + dct_interleave16(row4, row5); + dct_interleave16(row6, row7); + } + + // row pass + dct_pass(bias_1, 17); + + { + // pack + __m128i p0 = _mm_packus_epi16(row0, row1); // a0a1a2a3...a7b0b1b2b3...b7 + __m128i p1 = _mm_packus_epi16(row2, row3); + __m128i p2 = _mm_packus_epi16(row4, row5); + __m128i p3 = _mm_packus_epi16(row6, row7); + + // 8bit 8x8 transpose pass 1 + dct_interleave8(p0, p2); // a0e0a1e1... + dct_interleave8(p1, p3); // c0g0c1g1... + + // transpose pass 2 + dct_interleave8(p0, p1); // a0c0e0g0... + dct_interleave8(p2, p3); // b0d0f0h0... + + // transpose pass 3 + dct_interleave8(p0, p2); // a0b0c0d0... + dct_interleave8(p1, p3); // a4b4c4d4... + + // store + _mm_storel_epi64((__m128i *) out, p0); out += out_stride; + _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p0, 0x4e)); out += out_stride; + _mm_storel_epi64((__m128i *) out, p2); out += out_stride; + _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p2, 0x4e)); out += out_stride; + _mm_storel_epi64((__m128i *) out, p1); out += out_stride; + _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p1, 0x4e)); out += out_stride; + _mm_storel_epi64((__m128i *) out, p3); out += out_stride; + _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p3, 0x4e)); + } + +#undef dct_const +#undef dct_rot +#undef dct_widen +#undef dct_wadd +#undef dct_wsub +#undef dct_bfly32o +#undef dct_interleave8 +#undef dct_interleave16 +#undef dct_pass +} + +#endif // STBI_SSE2 + +#ifdef STBI_NEON + +// NEON integer IDCT. should produce bit-identical +// results to the generic C version. +static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64]) +{ + int16x8_t row0, row1, row2, row3, row4, row5, row6, row7; + + int16x4_t rot0_0 = vdup_n_s16(stbi__f2f(0.5411961f)); + int16x4_t rot0_1 = vdup_n_s16(stbi__f2f(-1.847759065f)); + int16x4_t rot0_2 = vdup_n_s16(stbi__f2f( 0.765366865f)); + int16x4_t rot1_0 = vdup_n_s16(stbi__f2f( 1.175875602f)); + int16x4_t rot1_1 = vdup_n_s16(stbi__f2f(-0.899976223f)); + int16x4_t rot1_2 = vdup_n_s16(stbi__f2f(-2.562915447f)); + int16x4_t rot2_0 = vdup_n_s16(stbi__f2f(-1.961570560f)); + int16x4_t rot2_1 = vdup_n_s16(stbi__f2f(-0.390180644f)); + int16x4_t rot3_0 = vdup_n_s16(stbi__f2f( 0.298631336f)); + int16x4_t rot3_1 = vdup_n_s16(stbi__f2f( 2.053119869f)); + int16x4_t rot3_2 = vdup_n_s16(stbi__f2f( 3.072711026f)); + int16x4_t rot3_3 = vdup_n_s16(stbi__f2f( 1.501321110f)); + +#define dct_long_mul(out, inq, coeff) \ + int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \ + int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff) + +#define dct_long_mac(out, acc, inq, coeff) \ + int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \ + int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff) + +#define dct_widen(out, inq) \ + int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \ + int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12) + +// wide add +#define dct_wadd(out, a, b) \ + int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \ + int32x4_t out##_h = vaddq_s32(a##_h, b##_h) + +// wide sub +#define dct_wsub(out, a, b) \ + int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \ + int32x4_t out##_h = vsubq_s32(a##_h, b##_h) + +// butterfly a/b, then shift using "shiftop" by "s" and pack +#define dct_bfly32o(out0,out1, a,b,shiftop,s) \ + { \ + dct_wadd(sum, a, b); \ + dct_wsub(dif, a, b); \ + out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \ + out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \ + } + +#define dct_pass(shiftop, shift) \ + { \ + /* even part */ \ + int16x8_t sum26 = vaddq_s16(row2, row6); \ + dct_long_mul(p1e, sum26, rot0_0); \ + dct_long_mac(t2e, p1e, row6, rot0_1); \ + dct_long_mac(t3e, p1e, row2, rot0_2); \ + int16x8_t sum04 = vaddq_s16(row0, row4); \ + int16x8_t dif04 = vsubq_s16(row0, row4); \ + dct_widen(t0e, sum04); \ + dct_widen(t1e, dif04); \ + dct_wadd(x0, t0e, t3e); \ + dct_wsub(x3, t0e, t3e); \ + dct_wadd(x1, t1e, t2e); \ + dct_wsub(x2, t1e, t2e); \ + /* odd part */ \ + int16x8_t sum15 = vaddq_s16(row1, row5); \ + int16x8_t sum17 = vaddq_s16(row1, row7); \ + int16x8_t sum35 = vaddq_s16(row3, row5); \ + int16x8_t sum37 = vaddq_s16(row3, row7); \ + int16x8_t sumodd = vaddq_s16(sum17, sum35); \ + dct_long_mul(p5o, sumodd, rot1_0); \ + dct_long_mac(p1o, p5o, sum17, rot1_1); \ + dct_long_mac(p2o, p5o, sum35, rot1_2); \ + dct_long_mul(p3o, sum37, rot2_0); \ + dct_long_mul(p4o, sum15, rot2_1); \ + dct_wadd(sump13o, p1o, p3o); \ + dct_wadd(sump24o, p2o, p4o); \ + dct_wadd(sump23o, p2o, p3o); \ + dct_wadd(sump14o, p1o, p4o); \ + dct_long_mac(x4, sump13o, row7, rot3_0); \ + dct_long_mac(x5, sump24o, row5, rot3_1); \ + dct_long_mac(x6, sump23o, row3, rot3_2); \ + dct_long_mac(x7, sump14o, row1, rot3_3); \ + dct_bfly32o(row0,row7, x0,x7,shiftop,shift); \ + dct_bfly32o(row1,row6, x1,x6,shiftop,shift); \ + dct_bfly32o(row2,row5, x2,x5,shiftop,shift); \ + dct_bfly32o(row3,row4, x3,x4,shiftop,shift); \ + } + + // load + row0 = vld1q_s16(data + 0*8); + row1 = vld1q_s16(data + 1*8); + row2 = vld1q_s16(data + 2*8); + row3 = vld1q_s16(data + 3*8); + row4 = vld1q_s16(data + 4*8); + row5 = vld1q_s16(data + 5*8); + row6 = vld1q_s16(data + 6*8); + row7 = vld1q_s16(data + 7*8); + + // add DC bias + row0 = vaddq_s16(row0, vsetq_lane_s16(1024, vdupq_n_s16(0), 0)); + + // column pass + dct_pass(vrshrn_n_s32, 10); + + // 16bit 8x8 transpose + { +// these three map to a single VTRN.16, VTRN.32, and VSWP, respectively. +// whether compilers actually get this is another story, sadly. +#define dct_trn16(x, y) { int16x8x2_t t = vtrnq_s16(x, y); x = t.val[0]; y = t.val[1]; } +#define dct_trn32(x, y) { int32x4x2_t t = vtrnq_s32(vreinterpretq_s32_s16(x), vreinterpretq_s32_s16(y)); x = vreinterpretq_s16_s32(t.val[0]); y = vreinterpretq_s16_s32(t.val[1]); } +#define dct_trn64(x, y) { int16x8_t x0 = x; int16x8_t y0 = y; x = vcombine_s16(vget_low_s16(x0), vget_low_s16(y0)); y = vcombine_s16(vget_high_s16(x0), vget_high_s16(y0)); } + + // pass 1 + dct_trn16(row0, row1); // a0b0a2b2a4b4a6b6 + dct_trn16(row2, row3); + dct_trn16(row4, row5); + dct_trn16(row6, row7); + + // pass 2 + dct_trn32(row0, row2); // a0b0c0d0a4b4c4d4 + dct_trn32(row1, row3); + dct_trn32(row4, row6); + dct_trn32(row5, row7); + + // pass 3 + dct_trn64(row0, row4); // a0b0c0d0e0f0g0h0 + dct_trn64(row1, row5); + dct_trn64(row2, row6); + dct_trn64(row3, row7); + +#undef dct_trn16 +#undef dct_trn32 +#undef dct_trn64 + } + + // row pass + // vrshrn_n_s32 only supports shifts up to 16, we need + // 17. so do a non-rounding shift of 16 first then follow + // up with a rounding shift by 1. + dct_pass(vshrn_n_s32, 16); + + { + // pack and round + uint8x8_t p0 = vqrshrun_n_s16(row0, 1); + uint8x8_t p1 = vqrshrun_n_s16(row1, 1); + uint8x8_t p2 = vqrshrun_n_s16(row2, 1); + uint8x8_t p3 = vqrshrun_n_s16(row3, 1); + uint8x8_t p4 = vqrshrun_n_s16(row4, 1); + uint8x8_t p5 = vqrshrun_n_s16(row5, 1); + uint8x8_t p6 = vqrshrun_n_s16(row6, 1); + uint8x8_t p7 = vqrshrun_n_s16(row7, 1); + + // again, these can translate into one instruction, but often don't. +#define dct_trn8_8(x, y) { uint8x8x2_t t = vtrn_u8(x, y); x = t.val[0]; y = t.val[1]; } +#define dct_trn8_16(x, y) { uint16x4x2_t t = vtrn_u16(vreinterpret_u16_u8(x), vreinterpret_u16_u8(y)); x = vreinterpret_u8_u16(t.val[0]); y = vreinterpret_u8_u16(t.val[1]); } +#define dct_trn8_32(x, y) { uint32x2x2_t t = vtrn_u32(vreinterpret_u32_u8(x), vreinterpret_u32_u8(y)); x = vreinterpret_u8_u32(t.val[0]); y = vreinterpret_u8_u32(t.val[1]); } + + // sadly can't use interleaved stores here since we only write + // 8 bytes to each scan line! + + // 8x8 8-bit transpose pass 1 + dct_trn8_8(p0, p1); + dct_trn8_8(p2, p3); + dct_trn8_8(p4, p5); + dct_trn8_8(p6, p7); + + // pass 2 + dct_trn8_16(p0, p2); + dct_trn8_16(p1, p3); + dct_trn8_16(p4, p6); + dct_trn8_16(p5, p7); + + // pass 3 + dct_trn8_32(p0, p4); + dct_trn8_32(p1, p5); + dct_trn8_32(p2, p6); + dct_trn8_32(p3, p7); + + // store + vst1_u8(out, p0); out += out_stride; + vst1_u8(out, p1); out += out_stride; + vst1_u8(out, p2); out += out_stride; + vst1_u8(out, p3); out += out_stride; + vst1_u8(out, p4); out += out_stride; + vst1_u8(out, p5); out += out_stride; + vst1_u8(out, p6); out += out_stride; + vst1_u8(out, p7); + +#undef dct_trn8_8 +#undef dct_trn8_16 +#undef dct_trn8_32 + } + +#undef dct_long_mul +#undef dct_long_mac +#undef dct_widen +#undef dct_wadd +#undef dct_wsub +#undef dct_bfly32o +#undef dct_pass +} + +#endif // STBI_NEON + +#define STBI__MARKER_none 0xff +// if there's a pending marker from the entropy stream, return that +// otherwise, fetch from the stream and get a marker. if there's no +// marker, return 0xff, which is never a valid marker value +static stbi_uc stbi__get_marker(stbi__jpeg *j) +{ + stbi_uc x; + if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none; return x; } + x = stbi__get8(j->s); + if (x != 0xff) return STBI__MARKER_none; + while (x == 0xff) + x = stbi__get8(j->s); // consume repeated 0xff fill bytes + return x; +} + +// in each scan, we'll have scan_n components, and the order +// of the components is specified by order[] +#define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7) + +// after a restart interval, stbi__jpeg_reset the entropy decoder and +// the dc prediction +static void stbi__jpeg_reset(stbi__jpeg *j) +{ + j->code_bits = 0; + j->code_buffer = 0; + j->nomore = 0; + j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = j->img_comp[3].dc_pred = 0; + j->marker = STBI__MARKER_none; + j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff; + j->eob_run = 0; + // no more than 1<<31 MCUs if no restart_interal? that's plenty safe, + // since we don't even allow 1<<30 pixels +} + +static int stbi__parse_entropy_coded_data(stbi__jpeg *z) +{ + stbi__jpeg_reset(z); + if (!z->progressive) { + if (z->scan_n == 1) { + int i,j; + STBI_SIMD_ALIGN(short, data[64]); + int n = z->order[0]; + // non-interleaved data, we just need to process one block at a time, + // in trivial scanline order + // number of blocks to do just depends on how many actual "pixels" this + // component has, independent of interleaved MCU blocking and such + int w = (z->img_comp[n].x+7) >> 3; + int h = (z->img_comp[n].y+7) >> 3; + for (j=0; j < h; ++j) { + for (i=0; i < w; ++i) { + int ha = z->img_comp[n].ha; + if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0; + z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data); + // every data block is an MCU, so countdown the restart interval + if (--z->todo <= 0) { + if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); + // if it's NOT a restart, then just bail, so we get corrupt data + // rather than no data + if (!STBI__RESTART(z->marker)) return 1; + stbi__jpeg_reset(z); + } + } + } + return 1; + } else { // interleaved + int i,j,k,x,y; + STBI_SIMD_ALIGN(short, data[64]); + for (j=0; j < z->img_mcu_y; ++j) { + for (i=0; i < z->img_mcu_x; ++i) { + // scan an interleaved mcu... process scan_n components in order + for (k=0; k < z->scan_n; ++k) { + int n = z->order[k]; + // scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (y=0; y < z->img_comp[n].v; ++y) { + for (x=0; x < z->img_comp[n].h; ++x) { + int x2 = (i*z->img_comp[n].h + x)*8; + int y2 = (j*z->img_comp[n].v + y)*8; + int ha = z->img_comp[n].ha; + if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0; + z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data); + } + } + } + // after all interleaved components, that's an interleaved MCU, + // so now count down the restart interval + if (--z->todo <= 0) { + if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); + if (!STBI__RESTART(z->marker)) return 1; + stbi__jpeg_reset(z); + } + } + } + return 1; + } + } else { + if (z->scan_n == 1) { + int i,j; + int n = z->order[0]; + // non-interleaved data, we just need to process one block at a time, + // in trivial scanline order + // number of blocks to do just depends on how many actual "pixels" this + // component has, independent of interleaved MCU blocking and such + int w = (z->img_comp[n].x+7) >> 3; + int h = (z->img_comp[n].y+7) >> 3; + for (j=0; j < h; ++j) { + for (i=0; i < w; ++i) { + short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w); + if (z->spec_start == 0) { + if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n)) + return 0; + } else { + int ha = z->img_comp[n].ha; + if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha])) + return 0; + } + // every data block is an MCU, so countdown the restart interval + if (--z->todo <= 0) { + if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); + if (!STBI__RESTART(z->marker)) return 1; + stbi__jpeg_reset(z); + } + } + } + return 1; + } else { // interleaved + int i,j,k,x,y; + for (j=0; j < z->img_mcu_y; ++j) { + for (i=0; i < z->img_mcu_x; ++i) { + // scan an interleaved mcu... process scan_n components in order + for (k=0; k < z->scan_n; ++k) { + int n = z->order[k]; + // scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (y=0; y < z->img_comp[n].v; ++y) { + for (x=0; x < z->img_comp[n].h; ++x) { + int x2 = (i*z->img_comp[n].h + x); + int y2 = (j*z->img_comp[n].v + y); + short *data = z->img_comp[n].coeff + 64 * (x2 + y2 * z->img_comp[n].coeff_w); + if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n)) + return 0; + } + } + } + // after all interleaved components, that's an interleaved MCU, + // so now count down the restart interval + if (--z->todo <= 0) { + if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); + if (!STBI__RESTART(z->marker)) return 1; + stbi__jpeg_reset(z); + } + } + } + return 1; + } + } +} + +static void stbi__jpeg_dequantize(short *data, stbi__uint16 *dequant) +{ + int i; + for (i=0; i < 64; ++i) + data[i] *= dequant[i]; +} + +static void stbi__jpeg_finish(stbi__jpeg *z) +{ + if (z->progressive) { + // dequantize and idct the data + int i,j,n; + for (n=0; n < z->s->img_n; ++n) { + int w = (z->img_comp[n].x+7) >> 3; + int h = (z->img_comp[n].y+7) >> 3; + for (j=0; j < h; ++j) { + for (i=0; i < w; ++i) { + short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w); + stbi__jpeg_dequantize(data, z->dequant[z->img_comp[n].tq]); + z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data); + } + } + } + } +} + +static int stbi__process_marker(stbi__jpeg *z, int m) +{ + int L; + switch (m) { + case STBI__MARKER_none: // no marker found + return stbi__err("expected marker","Corrupt JPEG"); + + case 0xDD: // DRI - specify restart interval + if (stbi__get16be(z->s) != 4) return stbi__err("bad DRI len","Corrupt JPEG"); + z->restart_interval = stbi__get16be(z->s); + return 1; + + case 0xDB: // DQT - define quantization table + L = stbi__get16be(z->s)-2; + while (L > 0) { + int q = stbi__get8(z->s); + int p = q >> 4, sixteen = (p != 0); + int t = q & 15,i; + if (p != 0 && p != 1) return stbi__err("bad DQT type","Corrupt JPEG"); + if (t > 3) return stbi__err("bad DQT table","Corrupt JPEG"); + + for (i=0; i < 64; ++i) + z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(sixteen ? stbi__get16be(z->s) : stbi__get8(z->s)); + L -= (sixteen ? 129 : 65); + } + return L==0; + + case 0xC4: // DHT - define huffman table + L = stbi__get16be(z->s)-2; + while (L > 0) { + stbi_uc *v; + int sizes[16],i,n=0; + int q = stbi__get8(z->s); + int tc = q >> 4; + int th = q & 15; + if (tc > 1 || th > 3) return stbi__err("bad DHT header","Corrupt JPEG"); + for (i=0; i < 16; ++i) { + sizes[i] = stbi__get8(z->s); + n += sizes[i]; + } + L -= 17; + if (tc == 0) { + if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0; + v = z->huff_dc[th].values; + } else { + if (!stbi__build_huffman(z->huff_ac+th, sizes)) return 0; + v = z->huff_ac[th].values; + } + for (i=0; i < n; ++i) + v[i] = stbi__get8(z->s); + if (tc != 0) + stbi__build_fast_ac(z->fast_ac[th], z->huff_ac + th); + L -= n; + } + return L==0; + } + + // check for comment block or APP blocks + if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) { + L = stbi__get16be(z->s); + if (L < 2) { + if (m == 0xFE) + return stbi__err("bad COM len","Corrupt JPEG"); + else + return stbi__err("bad APP len","Corrupt JPEG"); + } + L -= 2; + + if (m == 0xE0 && L >= 5) { // JFIF APP0 segment + static const unsigned char tag[5] = {'J','F','I','F','\0'}; + int ok = 1; + int i; + for (i=0; i < 5; ++i) + if (stbi__get8(z->s) != tag[i]) + ok = 0; + L -= 5; + if (ok) + z->jfif = 1; + } else if (m == 0xEE && L >= 12) { // Adobe APP14 segment + static const unsigned char tag[6] = {'A','d','o','b','e','\0'}; + int ok = 1; + int i; + for (i=0; i < 6; ++i) + if (stbi__get8(z->s) != tag[i]) + ok = 0; + L -= 6; + if (ok) { + stbi__get8(z->s); // version + stbi__get16be(z->s); // flags0 + stbi__get16be(z->s); // flags1 + z->app14_color_transform = stbi__get8(z->s); // color transform + L -= 6; + } + } + + stbi__skip(z->s, L); + return 1; + } + + return stbi__err("unknown marker","Corrupt JPEG"); +} + +// after we see SOS +static int stbi__process_scan_header(stbi__jpeg *z) +{ + int i; + int Ls = stbi__get16be(z->s); + z->scan_n = stbi__get8(z->s); + if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s->img_n) return stbi__err("bad SOS component count","Corrupt JPEG"); + if (Ls != 6+2*z->scan_n) return stbi__err("bad SOS len","Corrupt JPEG"); + for (i=0; i < z->scan_n; ++i) { + int id = stbi__get8(z->s), which; + int q = stbi__get8(z->s); + for (which = 0; which < z->s->img_n; ++which) + if (z->img_comp[which].id == id) + break; + if (which == z->s->img_n) return 0; // no match + z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return stbi__err("bad DC huff","Corrupt JPEG"); + z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return stbi__err("bad AC huff","Corrupt JPEG"); + z->order[i] = which; + } + + { + int aa; + z->spec_start = stbi__get8(z->s); + z->spec_end = stbi__get8(z->s); // should be 63, but might be 0 + aa = stbi__get8(z->s); + z->succ_high = (aa >> 4); + z->succ_low = (aa & 15); + if (z->progressive) { + if (z->spec_start > 63 || z->spec_end > 63 || z->spec_start > z->spec_end || z->succ_high > 13 || z->succ_low > 13) + return stbi__err("bad SOS", "Corrupt JPEG"); + } else { + if (z->spec_start != 0) return stbi__err("bad SOS","Corrupt JPEG"); + if (z->succ_high != 0 || z->succ_low != 0) return stbi__err("bad SOS","Corrupt JPEG"); + z->spec_end = 63; + } + } + + return 1; +} + +static int stbi__free_jpeg_components(stbi__jpeg *z, int ncomp, int why) +{ + int i; + for (i=0; i < ncomp; ++i) { + if (z->img_comp[i].raw_data) { + STBI_FREE(z->img_comp[i].raw_data); + z->img_comp[i].raw_data = NULL; + z->img_comp[i].data = NULL; + } + if (z->img_comp[i].raw_coeff) { + STBI_FREE(z->img_comp[i].raw_coeff); + z->img_comp[i].raw_coeff = 0; + z->img_comp[i].coeff = 0; + } + if (z->img_comp[i].linebuf) { + STBI_FREE(z->img_comp[i].linebuf); + z->img_comp[i].linebuf = NULL; + } + } + return why; +} + +static int stbi__process_frame_header(stbi__jpeg *z, int scan) +{ + stbi__context *s = z->s; + int Lf,p,i,q, h_max=1,v_max=1,c; + Lf = stbi__get16be(s); if (Lf < 11) return stbi__err("bad SOF len","Corrupt JPEG"); // JPEG + p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline + s->img_y = stbi__get16be(s); if (s->img_y == 0) return stbi__err("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG + s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG"); // JPEG requires + c = stbi__get8(s); + if (c != 3 && c != 1 && c != 4) return stbi__err("bad component count","Corrupt JPEG"); + s->img_n = c; + for (i=0; i < c; ++i) { + z->img_comp[i].data = NULL; + z->img_comp[i].linebuf = NULL; + } + + if (Lf != 8+3*s->img_n) return stbi__err("bad SOF len","Corrupt JPEG"); + + z->rgb = 0; + for (i=0; i < s->img_n; ++i) { + static const unsigned char rgb[3] = { 'R', 'G', 'B' }; + z->img_comp[i].id = stbi__get8(s); + if (s->img_n == 3 && z->img_comp[i].id == rgb[i]) + ++z->rgb; + q = stbi__get8(s); + z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return stbi__err("bad H","Corrupt JPEG"); + z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return stbi__err("bad V","Corrupt JPEG"); + z->img_comp[i].tq = stbi__get8(s); if (z->img_comp[i].tq > 3) return stbi__err("bad TQ","Corrupt JPEG"); + } + + if (scan != STBI__SCAN_load) return 1; + + if (!stbi__mad3sizes_valid(s->img_x, s->img_y, s->img_n, 0)) return stbi__err("too large", "Image too large to decode"); + + for (i=0; i < s->img_n; ++i) { + if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h; + if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v; + } + + // compute interleaved mcu info + z->img_h_max = h_max; + z->img_v_max = v_max; + z->img_mcu_w = h_max * 8; + z->img_mcu_h = v_max * 8; + // these sizes can't be more than 17 bits + z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w; + z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h; + + for (i=0; i < s->img_n; ++i) { + // number of effective pixels (e.g. for non-interleaved MCU) + z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max; + z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max; + // to simplify generation, we'll allocate enough memory to decode + // the bogus oversized data from using interleaved MCUs and their + // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't + // discard the extra data until colorspace conversion + // + // img_mcu_x, img_mcu_y: <=17 bits; comp[i].h and .v are <=4 (checked earlier) + // so these muls can't overflow with 32-bit ints (which we require) + z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8; + z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8; + z->img_comp[i].coeff = 0; + z->img_comp[i].raw_coeff = 0; + z->img_comp[i].linebuf = NULL; + z->img_comp[i].raw_data = stbi__malloc_mad2(z->img_comp[i].w2, z->img_comp[i].h2, 15); + if (z->img_comp[i].raw_data == NULL) + return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory")); + // align blocks for idct using mmx/sse + z->img_comp[i].data = (stbi_uc*) (((size_t) z->img_comp[i].raw_data + 15) & ~15); + if (z->progressive) { + // w2, h2 are multiples of 8 (see above) + z->img_comp[i].coeff_w = z->img_comp[i].w2 / 8; + z->img_comp[i].coeff_h = z->img_comp[i].h2 / 8; + z->img_comp[i].raw_coeff = stbi__malloc_mad3(z->img_comp[i].w2, z->img_comp[i].h2, sizeof(short), 15); + if (z->img_comp[i].raw_coeff == NULL) + return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory")); + z->img_comp[i].coeff = (short*) (((size_t) z->img_comp[i].raw_coeff + 15) & ~15); + } + } + + return 1; +} + +// use comparisons since in some cases we handle more than one case (e.g. SOF) +#define stbi__DNL(x) ((x) == 0xdc) +#define stbi__SOI(x) ((x) == 0xd8) +#define stbi__EOI(x) ((x) == 0xd9) +#define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1 || (x) == 0xc2) +#define stbi__SOS(x) ((x) == 0xda) + +#define stbi__SOF_progressive(x) ((x) == 0xc2) + +static int stbi__decode_jpeg_header(stbi__jpeg *z, int scan) +{ + int m; + z->jfif = 0; + z->app14_color_transform = -1; // valid values are 0,1,2 + z->marker = STBI__MARKER_none; // initialize cached marker to empty + m = stbi__get_marker(z); + if (!stbi__SOI(m)) return stbi__err("no SOI","Corrupt JPEG"); + if (scan == STBI__SCAN_type) return 1; + m = stbi__get_marker(z); + while (!stbi__SOF(m)) { + if (!stbi__process_marker(z,m)) return 0; + m = stbi__get_marker(z); + while (m == STBI__MARKER_none) { + // some files have extra padding after their blocks, so ok, we'll scan + if (stbi__at_eof(z->s)) return stbi__err("no SOF", "Corrupt JPEG"); + m = stbi__get_marker(z); + } + } + z->progressive = stbi__SOF_progressive(m); + if (!stbi__process_frame_header(z, scan)) return 0; + return 1; +} + +// decode image to YCbCr format +static int stbi__decode_jpeg_image(stbi__jpeg *j) +{ + int m; + for (m = 0; m < 4; m++) { + j->img_comp[m].raw_data = NULL; + j->img_comp[m].raw_coeff = NULL; + } + j->restart_interval = 0; + if (!stbi__decode_jpeg_header(j, STBI__SCAN_load)) return 0; + m = stbi__get_marker(j); + while (!stbi__EOI(m)) { + if (stbi__SOS(m)) { + if (!stbi__process_scan_header(j)) return 0; + if (!stbi__parse_entropy_coded_data(j)) return 0; + if (j->marker == STBI__MARKER_none ) { + // handle 0s at the end of image data from IP Kamera 9060 + while (!stbi__at_eof(j->s)) { + int x = stbi__get8(j->s); + if (x == 255) { + j->marker = stbi__get8(j->s); + break; + } + } + // if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0 + } + } else if (stbi__DNL(m)) { + int Ld = stbi__get16be(j->s); + stbi__uint32 NL = stbi__get16be(j->s); + if (Ld != 4) return stbi__err("bad DNL len", "Corrupt JPEG"); + if (NL != j->s->img_y) return stbi__err("bad DNL height", "Corrupt JPEG"); + } else { + if (!stbi__process_marker(j, m)) return 0; + } + m = stbi__get_marker(j); + } + if (j->progressive) + stbi__jpeg_finish(j); + return 1; +} + +// static jfif-centered resampling (across block boundaries) + +typedef stbi_uc *(*resample_row_func)(stbi_uc *out, stbi_uc *in0, stbi_uc *in1, + int w, int hs); + +#define stbi__div4(x) ((stbi_uc) ((x) >> 2)) + +static stbi_uc *resample_row_1(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + STBI_NOTUSED(out); + STBI_NOTUSED(in_far); + STBI_NOTUSED(w); + STBI_NOTUSED(hs); + return in_near; +} + +static stbi_uc* stbi__resample_row_v_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // need to generate two samples vertically for every one in input + int i; + STBI_NOTUSED(hs); + for (i=0; i < w; ++i) + out[i] = stbi__div4(3*in_near[i] + in_far[i] + 2); + return out; +} + +static stbi_uc* stbi__resample_row_h_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // need to generate two samples horizontally for every one in input + int i; + stbi_uc *input = in_near; + + if (w == 1) { + // if only one sample, can't do any interpolation + out[0] = out[1] = input[0]; + return out; + } + + out[0] = input[0]; + out[1] = stbi__div4(input[0]*3 + input[1] + 2); + for (i=1; i < w-1; ++i) { + int n = 3*input[i]+2; + out[i*2+0] = stbi__div4(n+input[i-1]); + out[i*2+1] = stbi__div4(n+input[i+1]); + } + out[i*2+0] = stbi__div4(input[w-2]*3 + input[w-1] + 2); + out[i*2+1] = input[w-1]; + + STBI_NOTUSED(in_far); + STBI_NOTUSED(hs); + + return out; +} + +#define stbi__div16(x) ((stbi_uc) ((x) >> 4)) + +static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // need to generate 2x2 samples for every one in input + int i,t0,t1; + if (w == 1) { + out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2); + return out; + } + + t1 = 3*in_near[0] + in_far[0]; + out[0] = stbi__div4(t1+2); + for (i=1; i < w; ++i) { + t0 = t1; + t1 = 3*in_near[i]+in_far[i]; + out[i*2-1] = stbi__div16(3*t0 + t1 + 8); + out[i*2 ] = stbi__div16(3*t1 + t0 + 8); + } + out[w*2-1] = stbi__div4(t1+2); + + STBI_NOTUSED(hs); + + return out; +} + +#if defined(STBI_SSE2) || defined(STBI_NEON) +static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // need to generate 2x2 samples for every one in input + int i=0,t0,t1; + + if (w == 1) { + out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2); + return out; + } + + t1 = 3*in_near[0] + in_far[0]; + // process groups of 8 pixels for as long as we can. + // note we can't handle the last pixel in a row in this loop + // because we need to handle the filter boundary conditions. + for (; i < ((w-1) & ~7); i += 8) { +#if defined(STBI_SSE2) + // load and perform the vertical filtering pass + // this uses 3*x + y = 4*x + (y - x) + __m128i zero = _mm_setzero_si128(); + __m128i farb = _mm_loadl_epi64((__m128i *) (in_far + i)); + __m128i nearb = _mm_loadl_epi64((__m128i *) (in_near + i)); + __m128i farw = _mm_unpacklo_epi8(farb, zero); + __m128i nearw = _mm_unpacklo_epi8(nearb, zero); + __m128i diff = _mm_sub_epi16(farw, nearw); + __m128i nears = _mm_slli_epi16(nearw, 2); + __m128i curr = _mm_add_epi16(nears, diff); // current row + + // horizontal filter works the same based on shifted vers of current + // row. "prev" is current row shifted right by 1 pixel; we need to + // insert the previous pixel value (from t1). + // "next" is current row shifted left by 1 pixel, with first pixel + // of next block of 8 pixels added in. + __m128i prv0 = _mm_slli_si128(curr, 2); + __m128i nxt0 = _mm_srli_si128(curr, 2); + __m128i prev = _mm_insert_epi16(prv0, t1, 0); + __m128i next = _mm_insert_epi16(nxt0, 3*in_near[i+8] + in_far[i+8], 7); + + // horizontal filter, polyphase implementation since it's convenient: + // even pixels = 3*cur + prev = cur*4 + (prev - cur) + // odd pixels = 3*cur + next = cur*4 + (next - cur) + // note the shared term. + __m128i bias = _mm_set1_epi16(8); + __m128i curs = _mm_slli_epi16(curr, 2); + __m128i prvd = _mm_sub_epi16(prev, curr); + __m128i nxtd = _mm_sub_epi16(next, curr); + __m128i curb = _mm_add_epi16(curs, bias); + __m128i even = _mm_add_epi16(prvd, curb); + __m128i odd = _mm_add_epi16(nxtd, curb); + + // interleave even and odd pixels, then undo scaling. + __m128i int0 = _mm_unpacklo_epi16(even, odd); + __m128i int1 = _mm_unpackhi_epi16(even, odd); + __m128i de0 = _mm_srli_epi16(int0, 4); + __m128i de1 = _mm_srli_epi16(int1, 4); + + // pack and write output + __m128i outv = _mm_packus_epi16(de0, de1); + _mm_storeu_si128((__m128i *) (out + i*2), outv); +#elif defined(STBI_NEON) + // load and perform the vertical filtering pass + // this uses 3*x + y = 4*x + (y - x) + uint8x8_t farb = vld1_u8(in_far + i); + uint8x8_t nearb = vld1_u8(in_near + i); + int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(farb, nearb)); + int16x8_t nears = vreinterpretq_s16_u16(vshll_n_u8(nearb, 2)); + int16x8_t curr = vaddq_s16(nears, diff); // current row + + // horizontal filter works the same based on shifted vers of current + // row. "prev" is current row shifted right by 1 pixel; we need to + // insert the previous pixel value (from t1). + // "next" is current row shifted left by 1 pixel, with first pixel + // of next block of 8 pixels added in. + int16x8_t prv0 = vextq_s16(curr, curr, 7); + int16x8_t nxt0 = vextq_s16(curr, curr, 1); + int16x8_t prev = vsetq_lane_s16(t1, prv0, 0); + int16x8_t next = vsetq_lane_s16(3*in_near[i+8] + in_far[i+8], nxt0, 7); + + // horizontal filter, polyphase implementation since it's convenient: + // even pixels = 3*cur + prev = cur*4 + (prev - cur) + // odd pixels = 3*cur + next = cur*4 + (next - cur) + // note the shared term. + int16x8_t curs = vshlq_n_s16(curr, 2); + int16x8_t prvd = vsubq_s16(prev, curr); + int16x8_t nxtd = vsubq_s16(next, curr); + int16x8_t even = vaddq_s16(curs, prvd); + int16x8_t odd = vaddq_s16(curs, nxtd); + + // undo scaling and round, then store with even/odd phases interleaved + uint8x8x2_t o; + o.val[0] = vqrshrun_n_s16(even, 4); + o.val[1] = vqrshrun_n_s16(odd, 4); + vst2_u8(out + i*2, o); +#endif + + // "previous" value for next iter + t1 = 3*in_near[i+7] + in_far[i+7]; + } + + t0 = t1; + t1 = 3*in_near[i] + in_far[i]; + out[i*2] = stbi__div16(3*t1 + t0 + 8); + + for (++i; i < w; ++i) { + t0 = t1; + t1 = 3*in_near[i]+in_far[i]; + out[i*2-1] = stbi__div16(3*t0 + t1 + 8); + out[i*2 ] = stbi__div16(3*t1 + t0 + 8); + } + out[w*2-1] = stbi__div4(t1+2); + + STBI_NOTUSED(hs); + + return out; +} +#endif + +static stbi_uc *stbi__resample_row_generic(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // resample with nearest-neighbor + int i,j; + STBI_NOTUSED(in_far); + for (i=0; i < w; ++i) + for (j=0; j < hs; ++j) + out[i*hs+j] = in_near[i]; + return out; +} + +// this is a reduced-precision calculation of YCbCr-to-RGB introduced +// to make sure the code produces the same results in both SIMD and scalar +#define stbi__float2fixed(x) (((int) ((x) * 4096.0f + 0.5f)) << 8) +static void stbi__YCbCr_to_RGB_row(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step) +{ + int i; + for (i=0; i < count; ++i) { + int y_fixed = (y[i] << 20) + (1<<19); // rounding + int r,g,b; + int cr = pcr[i] - 128; + int cb = pcb[i] - 128; + r = y_fixed + cr* stbi__float2fixed(1.40200f); + g = y_fixed + (cr*-stbi__float2fixed(0.71414f)) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000); + b = y_fixed + cb* stbi__float2fixed(1.77200f); + r >>= 20; + g >>= 20; + b >>= 20; + if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; } + if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; } + if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; } + out[0] = (stbi_uc)r; + out[1] = (stbi_uc)g; + out[2] = (stbi_uc)b; + out[3] = 255; + out += step; + } +} + +#if defined(STBI_SSE2) || defined(STBI_NEON) +static void stbi__YCbCr_to_RGB_simd(stbi_uc *out, stbi_uc const *y, stbi_uc const *pcb, stbi_uc const *pcr, int count, int step) +{ + int i = 0; + +#ifdef STBI_SSE2 + // step == 3 is pretty ugly on the final interleave, and i'm not convinced + // it's useful in practice (you wouldn't use it for textures, for example). + // so just accelerate step == 4 case. + if (step == 4) { + // this is a fairly straightforward implementation and not super-optimized. + __m128i signflip = _mm_set1_epi8(-0x80); + __m128i cr_const0 = _mm_set1_epi16( (short) ( 1.40200f*4096.0f+0.5f)); + __m128i cr_const1 = _mm_set1_epi16( - (short) ( 0.71414f*4096.0f+0.5f)); + __m128i cb_const0 = _mm_set1_epi16( - (short) ( 0.34414f*4096.0f+0.5f)); + __m128i cb_const1 = _mm_set1_epi16( (short) ( 1.77200f*4096.0f+0.5f)); + __m128i y_bias = _mm_set1_epi8((char) (unsigned char) 128); + __m128i xw = _mm_set1_epi16(255); // alpha channel + + for (; i+7 < count; i += 8) { + // load + __m128i y_bytes = _mm_loadl_epi64((__m128i *) (y+i)); + __m128i cr_bytes = _mm_loadl_epi64((__m128i *) (pcr+i)); + __m128i cb_bytes = _mm_loadl_epi64((__m128i *) (pcb+i)); + __m128i cr_biased = _mm_xor_si128(cr_bytes, signflip); // -128 + __m128i cb_biased = _mm_xor_si128(cb_bytes, signflip); // -128 + + // unpack to short (and left-shift cr, cb by 8) + __m128i yw = _mm_unpacklo_epi8(y_bias, y_bytes); + __m128i crw = _mm_unpacklo_epi8(_mm_setzero_si128(), cr_biased); + __m128i cbw = _mm_unpacklo_epi8(_mm_setzero_si128(), cb_biased); + + // color transform + __m128i yws = _mm_srli_epi16(yw, 4); + __m128i cr0 = _mm_mulhi_epi16(cr_const0, crw); + __m128i cb0 = _mm_mulhi_epi16(cb_const0, cbw); + __m128i cb1 = _mm_mulhi_epi16(cbw, cb_const1); + __m128i cr1 = _mm_mulhi_epi16(crw, cr_const1); + __m128i rws = _mm_add_epi16(cr0, yws); + __m128i gwt = _mm_add_epi16(cb0, yws); + __m128i bws = _mm_add_epi16(yws, cb1); + __m128i gws = _mm_add_epi16(gwt, cr1); + + // descale + __m128i rw = _mm_srai_epi16(rws, 4); + __m128i bw = _mm_srai_epi16(bws, 4); + __m128i gw = _mm_srai_epi16(gws, 4); + + // back to byte, set up for transpose + __m128i brb = _mm_packus_epi16(rw, bw); + __m128i gxb = _mm_packus_epi16(gw, xw); + + // transpose to interleave channels + __m128i t0 = _mm_unpacklo_epi8(brb, gxb); + __m128i t1 = _mm_unpackhi_epi8(brb, gxb); + __m128i o0 = _mm_unpacklo_epi16(t0, t1); + __m128i o1 = _mm_unpackhi_epi16(t0, t1); + + // store + _mm_storeu_si128((__m128i *) (out + 0), o0); + _mm_storeu_si128((__m128i *) (out + 16), o1); + out += 32; + } + } +#endif + +#ifdef STBI_NEON + // in this version, step=3 support would be easy to add. but is there demand? + if (step == 4) { + // this is a fairly straightforward implementation and not super-optimized. + uint8x8_t signflip = vdup_n_u8(0x80); + int16x8_t cr_const0 = vdupq_n_s16( (short) ( 1.40200f*4096.0f+0.5f)); + int16x8_t cr_const1 = vdupq_n_s16( - (short) ( 0.71414f*4096.0f+0.5f)); + int16x8_t cb_const0 = vdupq_n_s16( - (short) ( 0.34414f*4096.0f+0.5f)); + int16x8_t cb_const1 = vdupq_n_s16( (short) ( 1.77200f*4096.0f+0.5f)); + + for (; i+7 < count; i += 8) { + // load + uint8x8_t y_bytes = vld1_u8(y + i); + uint8x8_t cr_bytes = vld1_u8(pcr + i); + uint8x8_t cb_bytes = vld1_u8(pcb + i); + int8x8_t cr_biased = vreinterpret_s8_u8(vsub_u8(cr_bytes, signflip)); + int8x8_t cb_biased = vreinterpret_s8_u8(vsub_u8(cb_bytes, signflip)); + + // expand to s16 + int16x8_t yws = vreinterpretq_s16_u16(vshll_n_u8(y_bytes, 4)); + int16x8_t crw = vshll_n_s8(cr_biased, 7); + int16x8_t cbw = vshll_n_s8(cb_biased, 7); + + // color transform + int16x8_t cr0 = vqdmulhq_s16(crw, cr_const0); + int16x8_t cb0 = vqdmulhq_s16(cbw, cb_const0); + int16x8_t cr1 = vqdmulhq_s16(crw, cr_const1); + int16x8_t cb1 = vqdmulhq_s16(cbw, cb_const1); + int16x8_t rws = vaddq_s16(yws, cr0); + int16x8_t gws = vaddq_s16(vaddq_s16(yws, cb0), cr1); + int16x8_t bws = vaddq_s16(yws, cb1); + + // undo scaling, round, convert to byte + uint8x8x4_t o; + o.val[0] = vqrshrun_n_s16(rws, 4); + o.val[1] = vqrshrun_n_s16(gws, 4); + o.val[2] = vqrshrun_n_s16(bws, 4); + o.val[3] = vdup_n_u8(255); + + // store, interleaving r/g/b/a + vst4_u8(out, o); + out += 8*4; + } + } +#endif + + for (; i < count; ++i) { + int y_fixed = (y[i] << 20) + (1<<19); // rounding + int r,g,b; + int cr = pcr[i] - 128; + int cb = pcb[i] - 128; + r = y_fixed + cr* stbi__float2fixed(1.40200f); + g = y_fixed + cr*-stbi__float2fixed(0.71414f) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000); + b = y_fixed + cb* stbi__float2fixed(1.77200f); + r >>= 20; + g >>= 20; + b >>= 20; + if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; } + if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; } + if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; } + out[0] = (stbi_uc)r; + out[1] = (stbi_uc)g; + out[2] = (stbi_uc)b; + out[3] = 255; + out += step; + } +} +#endif + +// set up the kernels +static void stbi__setup_jpeg(stbi__jpeg *j) +{ + j->idct_block_kernel = stbi__idct_block; + j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row; + j->resample_row_hv_2_kernel = stbi__resample_row_hv_2; + +#ifdef STBI_SSE2 + if (stbi__sse2_available()) { + j->idct_block_kernel = stbi__idct_simd; + j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd; + j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd; + } +#endif + +#ifdef STBI_NEON + j->idct_block_kernel = stbi__idct_simd; + j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd; + j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd; +#endif +} + +// clean up the temporary component buffers +static void stbi__cleanup_jpeg(stbi__jpeg *j) +{ + stbi__free_jpeg_components(j, j->s->img_n, 0); +} + +typedef struct +{ + resample_row_func resample; + stbi_uc *line0,*line1; + int hs,vs; // expansion factor in each axis + int w_lores; // horizontal pixels pre-expansion + int ystep; // how far through vertical expansion we are + int ypos; // which pre-expansion row we're on +} stbi__resample; + +// fast 0..255 * 0..255 => 0..255 rounded multiplication +static stbi_uc stbi__blinn_8x8(stbi_uc x, stbi_uc y) +{ + unsigned int t = x*y + 128; + return (stbi_uc) ((t + (t >>8)) >> 8); +} + +static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp) +{ + int n, decode_n, is_rgb; + z->s->img_n = 0; // make stbi__cleanup_jpeg safe + + // validate req_comp + if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error"); + + // load a jpeg image from whichever source, but leave in YCbCr format + if (!stbi__decode_jpeg_image(z)) { stbi__cleanup_jpeg(z); return NULL; } + + // determine actual number of components to generate + n = req_comp ? req_comp : z->s->img_n >= 3 ? 3 : 1; + + is_rgb = z->s->img_n == 3 && (z->rgb == 3 || (z->app14_color_transform == 0 && !z->jfif)); + + if (z->s->img_n == 3 && n < 3 && !is_rgb) + decode_n = 1; + else + decode_n = z->s->img_n; + + // resample and color-convert + { + int k; + unsigned int i,j; + stbi_uc *output; + stbi_uc *coutput[4] = { NULL, NULL, NULL, NULL }; + + stbi__resample res_comp[4]; + + for (k=0; k < decode_n; ++k) { + stbi__resample *r = &res_comp[k]; + + // allocate line buffer big enough for upsampling off the edges + // with upsample factor of 4 + z->img_comp[k].linebuf = (stbi_uc *) stbi__malloc(z->s->img_x + 3); + if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); } + + r->hs = z->img_h_max / z->img_comp[k].h; + r->vs = z->img_v_max / z->img_comp[k].v; + r->ystep = r->vs >> 1; + r->w_lores = (z->s->img_x + r->hs-1) / r->hs; + r->ypos = 0; + r->line0 = r->line1 = z->img_comp[k].data; + + if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1; + else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2; + else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2; + else if (r->hs == 2 && r->vs == 2) r->resample = z->resample_row_hv_2_kernel; + else r->resample = stbi__resample_row_generic; + } + + // can't error after this so, this is safe + output = (stbi_uc *) stbi__malloc_mad3(n, z->s->img_x, z->s->img_y, 1); + if (!output) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); } + + // now go ahead and resample + for (j=0; j < z->s->img_y; ++j) { + stbi_uc *out = output + n * z->s->img_x * j; + for (k=0; k < decode_n; ++k) { + stbi__resample *r = &res_comp[k]; + int y_bot = r->ystep >= (r->vs >> 1); + coutput[k] = r->resample(z->img_comp[k].linebuf, + y_bot ? r->line1 : r->line0, + y_bot ? r->line0 : r->line1, + r->w_lores, r->hs); + if (++r->ystep >= r->vs) { + r->ystep = 0; + r->line0 = r->line1; + if (++r->ypos < z->img_comp[k].y) + r->line1 += z->img_comp[k].w2; + } + } + if (n >= 3) { + stbi_uc *y = coutput[0]; + if (z->s->img_n == 3) { + if (is_rgb) { + for (i=0; i < z->s->img_x; ++i) { + out[0] = y[i]; + out[1] = coutput[1][i]; + out[2] = coutput[2][i]; + out[3] = 255; + out += n; + } + } else { + z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); + } + } else if (z->s->img_n == 4) { + if (z->app14_color_transform == 0) { // CMYK + for (i=0; i < z->s->img_x; ++i) { + stbi_uc m = coutput[3][i]; + out[0] = stbi__blinn_8x8(coutput[0][i], m); + out[1] = stbi__blinn_8x8(coutput[1][i], m); + out[2] = stbi__blinn_8x8(coutput[2][i], m); + out[3] = 255; + out += n; + } + } else if (z->app14_color_transform == 2) { // YCCK + z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); + for (i=0; i < z->s->img_x; ++i) { + stbi_uc m = coutput[3][i]; + out[0] = stbi__blinn_8x8(255 - out[0], m); + out[1] = stbi__blinn_8x8(255 - out[1], m); + out[2] = stbi__blinn_8x8(255 - out[2], m); + out += n; + } + } else { // YCbCr + alpha? Ignore the fourth channel for now + z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); + } + } else + for (i=0; i < z->s->img_x; ++i) { + out[0] = out[1] = out[2] = y[i]; + out[3] = 255; // not used if n==3 + out += n; + } + } else { + if (is_rgb) { + if (n == 1) + for (i=0; i < z->s->img_x; ++i) + *out++ = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]); + else { + for (i=0; i < z->s->img_x; ++i, out += 2) { + out[0] = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]); + out[1] = 255; + } + } + } else if (z->s->img_n == 4 && z->app14_color_transform == 0) { + for (i=0; i < z->s->img_x; ++i) { + stbi_uc m = coutput[3][i]; + stbi_uc r = stbi__blinn_8x8(coutput[0][i], m); + stbi_uc g = stbi__blinn_8x8(coutput[1][i], m); + stbi_uc b = stbi__blinn_8x8(coutput[2][i], m); + out[0] = stbi__compute_y(r, g, b); + out[1] = 255; + out += n; + } + } else if (z->s->img_n == 4 && z->app14_color_transform == 2) { + for (i=0; i < z->s->img_x; ++i) { + out[0] = stbi__blinn_8x8(255 - coutput[0][i], coutput[3][i]); + out[1] = 255; + out += n; + } + } else { + stbi_uc *y = coutput[0]; + if (n == 1) + for (i=0; i < z->s->img_x; ++i) out[i] = y[i]; + else + for (i=0; i < z->s->img_x; ++i) { *out++ = y[i]; *out++ = 255; } + } + } + } + stbi__cleanup_jpeg(z); + *out_x = z->s->img_x; + *out_y = z->s->img_y; + if (comp) *comp = z->s->img_n >= 3 ? 3 : 1; // report original components, not output + return output; + } +} + +static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + unsigned char* result; + stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg)); + STBI_NOTUSED(ri); + j->s = s; + stbi__setup_jpeg(j); + result = load_jpeg_image(j, x,y,comp,req_comp); + STBI_FREE(j); + return result; +} + +static int stbi__jpeg_test(stbi__context *s) +{ + int r; + stbi__jpeg* j = (stbi__jpeg*)stbi__malloc(sizeof(stbi__jpeg)); + j->s = s; + stbi__setup_jpeg(j); + r = stbi__decode_jpeg_header(j, STBI__SCAN_type); + stbi__rewind(s); + STBI_FREE(j); + return r; +} + +static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp) +{ + if (!stbi__decode_jpeg_header(j, STBI__SCAN_header)) { + stbi__rewind( j->s ); + return 0; + } + if (x) *x = j->s->img_x; + if (y) *y = j->s->img_y; + if (comp) *comp = j->s->img_n >= 3 ? 3 : 1; + return 1; +} + +static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp) +{ + int result; + stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg))); + j->s = s; + result = stbi__jpeg_info_raw(j, x, y, comp); + STBI_FREE(j); + return result; +} +#endif + +// public domain zlib decode v0.2 Sean Barrett 2006-11-18 +// simple implementation +// - all input must be provided in an upfront buffer +// - all output is written to a single output buffer (can malloc/realloc) +// performance +// - fast huffman + +#ifndef STBI_NO_ZLIB + +// fast-way is faster to check than jpeg huffman, but slow way is slower +#define STBI__ZFAST_BITS 9 // accelerate all cases in default tables +#define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1) + +// zlib-style huffman encoding +// (jpegs packs from left, zlib from right, so can't share code) +typedef struct +{ + stbi__uint16 fast[1 << STBI__ZFAST_BITS]; + stbi__uint16 firstcode[16]; + int maxcode[17]; + stbi__uint16 firstsymbol[16]; + stbi_uc size[288]; + stbi__uint16 value[288]; +} stbi__zhuffman; + +stbi_inline static int stbi__bitreverse16(int n) +{ + n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1); + n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2); + n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4); + n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8); + return n; +} + +stbi_inline static int stbi__bit_reverse(int v, int bits) +{ + STBI_ASSERT(bits <= 16); + // to bit reverse n bits, reverse 16 and shift + // e.g. 11 bits, bit reverse and shift away 5 + return stbi__bitreverse16(v) >> (16-bits); +} + +static int stbi__zbuild_huffman(stbi__zhuffman *z, const stbi_uc *sizelist, int num) +{ + int i,k=0; + int code, next_code[16], sizes[17]; + + // DEFLATE spec for generating codes + memset(sizes, 0, sizeof(sizes)); + memset(z->fast, 0, sizeof(z->fast)); + for (i=0; i < num; ++i) + ++sizes[sizelist[i]]; + sizes[0] = 0; + for (i=1; i < 16; ++i) + if (sizes[i] > (1 << i)) + return stbi__err("bad sizes", "Corrupt PNG"); + code = 0; + for (i=1; i < 16; ++i) { + next_code[i] = code; + z->firstcode[i] = (stbi__uint16) code; + z->firstsymbol[i] = (stbi__uint16) k; + code = (code + sizes[i]); + if (sizes[i]) + if (code-1 >= (1 << i)) return stbi__err("bad codelengths","Corrupt PNG"); + z->maxcode[i] = code << (16-i); // preshift for inner loop + code <<= 1; + k += sizes[i]; + } + z->maxcode[16] = 0x10000; // sentinel + for (i=0; i < num; ++i) { + int s = sizelist[i]; + if (s) { + int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s]; + stbi__uint16 fastv = (stbi__uint16) ((s << 9) | i); + z->size [c] = (stbi_uc ) s; + z->value[c] = (stbi__uint16) i; + if (s <= STBI__ZFAST_BITS) { + int j = stbi__bit_reverse(next_code[s],s); + while (j < (1 << STBI__ZFAST_BITS)) { + z->fast[j] = fastv; + j += (1 << s); + } + } + ++next_code[s]; + } + } + return 1; +} + +// zlib-from-memory implementation for PNG reading +// because PNG allows splitting the zlib stream arbitrarily, +// and it's annoying structurally to have PNG call ZLIB call PNG, +// we require PNG read all the IDATs and combine them into a single +// memory buffer + +typedef struct +{ + stbi_uc *zbuffer, *zbuffer_end; + int num_bits; + stbi__uint32 code_buffer; + + char *zout; + char *zout_start; + char *zout_end; + int z_expandable; + + stbi__zhuffman z_length, z_distance; +} stbi__zbuf; + +stbi_inline static stbi_uc stbi__zget8(stbi__zbuf *z) +{ + if (z->zbuffer >= z->zbuffer_end) return 0; + return *z->zbuffer++; +} + +static void stbi__fill_bits(stbi__zbuf *z) +{ + do { + STBI_ASSERT(z->code_buffer < (1U << z->num_bits)); + z->code_buffer |= (unsigned int) stbi__zget8(z) << z->num_bits; + z->num_bits += 8; + } while (z->num_bits <= 24); +} + +stbi_inline static unsigned int stbi__zreceive(stbi__zbuf *z, int n) +{ + unsigned int k; + if (z->num_bits < n) stbi__fill_bits(z); + k = z->code_buffer & ((1 << n) - 1); + z->code_buffer >>= n; + z->num_bits -= n; + return k; +} + +static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z) +{ + int b,s,k; + // not resolved by fast table, so compute it the slow way + // use jpeg approach, which requires MSbits at top + k = stbi__bit_reverse(a->code_buffer, 16); + for (s=STBI__ZFAST_BITS+1; ; ++s) + if (k < z->maxcode[s]) + break; + if (s == 16) return -1; // invalid code! + // code size is s, so: + b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s]; + STBI_ASSERT(z->size[b] == s); + a->code_buffer >>= s; + a->num_bits -= s; + return z->value[b]; +} + +stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z) +{ + int b,s; + if (a->num_bits < 16) stbi__fill_bits(a); + b = z->fast[a->code_buffer & STBI__ZFAST_MASK]; + if (b) { + s = b >> 9; + a->code_buffer >>= s; + a->num_bits -= s; + return b & 511; + } + return stbi__zhuffman_decode_slowpath(a, z); +} + +static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes +{ + char *q; + int cur, limit, old_limit; + z->zout = zout; + if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG"); + cur = (int) (z->zout - z->zout_start); + limit = old_limit = (int) (z->zout_end - z->zout_start); + while (cur + n > limit) + limit *= 2; + q = (char *) STBI_REALLOC_SIZED(z->zout_start, old_limit, limit); + STBI_NOTUSED(old_limit); + if (q == NULL) return stbi__err("outofmem", "Out of memory"); + z->zout_start = q; + z->zout = q + cur; + z->zout_end = q + limit; + return 1; +} + +static const int stbi__zlength_base[31] = { + 3,4,5,6,7,8,9,10,11,13, + 15,17,19,23,27,31,35,43,51,59, + 67,83,99,115,131,163,195,227,258,0,0 }; + +static const int stbi__zlength_extra[31]= +{ 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 }; + +static const int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, +257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0}; + +static const int stbi__zdist_extra[32] = +{ 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; + +static int stbi__parse_huffman_block(stbi__zbuf *a) +{ + char *zout = a->zout; + for(;;) { + int z = stbi__zhuffman_decode(a, &a->z_length); + if (z < 256) { + if (z < 0) return stbi__err("bad huffman code","Corrupt PNG"); // error in huffman codes + if (zout >= a->zout_end) { + if (!stbi__zexpand(a, zout, 1)) return 0; + zout = a->zout; + } + *zout++ = (char) z; + } else { + stbi_uc *p; + int len,dist; + if (z == 256) { + a->zout = zout; + return 1; + } + z -= 257; + len = stbi__zlength_base[z]; + if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]); + z = stbi__zhuffman_decode(a, &a->z_distance); + if (z < 0) return stbi__err("bad huffman code","Corrupt PNG"); + dist = stbi__zdist_base[z]; + if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]); + if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG"); + if (zout + len > a->zout_end) { + if (!stbi__zexpand(a, zout, len)) return 0; + zout = a->zout; + } + p = (stbi_uc *) (zout - dist); + if (dist == 1) { // run of one byte; common in images. + stbi_uc v = *p; + if (len) { do *zout++ = v; while (--len); } + } else { + if (len) { do *zout++ = *p++; while (--len); } + } + } + } +} + +static int stbi__compute_huffman_codes(stbi__zbuf *a) +{ + static const stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 }; + stbi__zhuffman z_codelength; + stbi_uc lencodes[286+32+137];//padding for maximum single op + stbi_uc codelength_sizes[19]; + int i,n; + + int hlit = stbi__zreceive(a,5) + 257; + int hdist = stbi__zreceive(a,5) + 1; + int hclen = stbi__zreceive(a,4) + 4; + int ntot = hlit + hdist; + + memset(codelength_sizes, 0, sizeof(codelength_sizes)); + for (i=0; i < hclen; ++i) { + int s = stbi__zreceive(a,3); + codelength_sizes[length_dezigzag[i]] = (stbi_uc) s; + } + if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0; + + n = 0; + while (n < ntot) { + int c = stbi__zhuffman_decode(a, &z_codelength); + if (c < 0 || c >= 19) return stbi__err("bad codelengths", "Corrupt PNG"); + if (c < 16) + lencodes[n++] = (stbi_uc) c; + else { + stbi_uc fill = 0; + if (c == 16) { + c = stbi__zreceive(a,2)+3; + if (n == 0) return stbi__err("bad codelengths", "Corrupt PNG"); + fill = lencodes[n-1]; + } else if (c == 17) + c = stbi__zreceive(a,3)+3; + else { + STBI_ASSERT(c == 18); + c = stbi__zreceive(a,7)+11; + } + if (ntot - n < c) return stbi__err("bad codelengths", "Corrupt PNG"); + memset(lencodes+n, fill, c); + n += c; + } + } + if (n != ntot) return stbi__err("bad codelengths","Corrupt PNG"); + if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0; + if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0; + return 1; +} + +static int stbi__parse_uncompressed_block(stbi__zbuf *a) +{ + stbi_uc header[4]; + int len,nlen,k; + if (a->num_bits & 7) + stbi__zreceive(a, a->num_bits & 7); // discard + // drain the bit-packed data into header + k = 0; + while (a->num_bits > 0) { + header[k++] = (stbi_uc) (a->code_buffer & 255); // suppress MSVC run-time check + a->code_buffer >>= 8; + a->num_bits -= 8; + } + STBI_ASSERT(a->num_bits == 0); + // now fill header the normal way + while (k < 4) + header[k++] = stbi__zget8(a); + len = header[1] * 256 + header[0]; + nlen = header[3] * 256 + header[2]; + if (nlen != (len ^ 0xffff)) return stbi__err("zlib corrupt","Corrupt PNG"); + if (a->zbuffer + len > a->zbuffer_end) return stbi__err("read past buffer","Corrupt PNG"); + if (a->zout + len > a->zout_end) + if (!stbi__zexpand(a, a->zout, len)) return 0; + memcpy(a->zout, a->zbuffer, len); + a->zbuffer += len; + a->zout += len; + return 1; +} + +static int stbi__parse_zlib_header(stbi__zbuf *a) +{ + int cmf = stbi__zget8(a); + int cm = cmf & 15; + /* int cinfo = cmf >> 4; */ + int flg = stbi__zget8(a); + if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec + if (flg & 32) return stbi__err("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png + if (cm != 8) return stbi__err("bad compression","Corrupt PNG"); // DEFLATE required for png + // window = 1 << (8 + cinfo)... but who cares, we fully buffer output + return 1; +} + +static const stbi_uc stbi__zdefault_length[288] = +{ + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8 +}; +static const stbi_uc stbi__zdefault_distance[32] = +{ + 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5 +}; +/* +Init algorithm: +{ + int i; // use <= to match clearly with spec + for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8; + for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9; + for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7; + for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8; + + for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5; +} +*/ + +static int stbi__parse_zlib(stbi__zbuf *a, int parse_header) +{ + int final, type; + if (parse_header) + if (!stbi__parse_zlib_header(a)) return 0; + a->num_bits = 0; + a->code_buffer = 0; + do { + final = stbi__zreceive(a,1); + type = stbi__zreceive(a,2); + if (type == 0) { + if (!stbi__parse_uncompressed_block(a)) return 0; + } else if (type == 3) { + return 0; + } else { + if (type == 1) { + // use fixed code lengths + if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , 288)) return 0; + if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0; + } else { + if (!stbi__compute_huffman_codes(a)) return 0; + } + if (!stbi__parse_huffman_block(a)) return 0; + } + } while (!final); + return 1; +} + +static int stbi__do_zlib(stbi__zbuf *a, char *obuf, int olen, int exp, int parse_header) +{ + a->zout_start = obuf; + a->zout = obuf; + a->zout_end = obuf + olen; + a->z_expandable = exp; + + return stbi__parse_zlib(a, parse_header); +} + +STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen) +{ + stbi__zbuf a; + char *p = (char *) stbi__malloc(initial_size); + if (p == NULL) return NULL; + a.zbuffer = (stbi_uc *) buffer; + a.zbuffer_end = (stbi_uc *) buffer + len; + if (stbi__do_zlib(&a, p, initial_size, 1, 1)) { + if (outlen) *outlen = (int) (a.zout - a.zout_start); + return a.zout_start; + } else { + STBI_FREE(a.zout_start); + return NULL; + } +} + +STBIDEF char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen) +{ + return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen); +} + +STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header) +{ + stbi__zbuf a; + char *p = (char *) stbi__malloc(initial_size); + if (p == NULL) return NULL; + a.zbuffer = (stbi_uc *) buffer; + a.zbuffer_end = (stbi_uc *) buffer + len; + if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) { + if (outlen) *outlen = (int) (a.zout - a.zout_start); + return a.zout_start; + } else { + STBI_FREE(a.zout_start); + return NULL; + } +} + +STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen) +{ + stbi__zbuf a; + a.zbuffer = (stbi_uc *) ibuffer; + a.zbuffer_end = (stbi_uc *) ibuffer + ilen; + if (stbi__do_zlib(&a, obuffer, olen, 0, 1)) + return (int) (a.zout - a.zout_start); + else + return -1; +} + +STBIDEF char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen) +{ + stbi__zbuf a; + char *p = (char *) stbi__malloc(16384); + if (p == NULL) return NULL; + a.zbuffer = (stbi_uc *) buffer; + a.zbuffer_end = (stbi_uc *) buffer+len; + if (stbi__do_zlib(&a, p, 16384, 1, 0)) { + if (outlen) *outlen = (int) (a.zout - a.zout_start); + return a.zout_start; + } else { + STBI_FREE(a.zout_start); + return NULL; + } +} + +STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen) +{ + stbi__zbuf a; + a.zbuffer = (stbi_uc *) ibuffer; + a.zbuffer_end = (stbi_uc *) ibuffer + ilen; + if (stbi__do_zlib(&a, obuffer, olen, 0, 0)) + return (int) (a.zout - a.zout_start); + else + return -1; +} +#endif + +// public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18 +// simple implementation +// - only 8-bit samples +// - no CRC checking +// - allocates lots of intermediate memory +// - avoids problem of streaming data between subsystems +// - avoids explicit window management +// performance +// - uses stb_zlib, a PD zlib implementation with fast huffman decoding + +#ifndef STBI_NO_PNG +typedef struct +{ + stbi__uint32 length; + stbi__uint32 type; +} stbi__pngchunk; + +static stbi__pngchunk stbi__get_chunk_header(stbi__context *s) +{ + stbi__pngchunk c; + c.length = stbi__get32be(s); + c.type = stbi__get32be(s); + return c; +} + +static int stbi__check_png_header(stbi__context *s) +{ + static const stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 }; + int i; + for (i=0; i < 8; ++i) + if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig","Not a PNG"); + return 1; +} + +typedef struct +{ + stbi__context *s; + stbi_uc *idata, *expanded, *out; + int depth; +} stbi__png; + + +enum { + STBI__F_none=0, + STBI__F_sub=1, + STBI__F_up=2, + STBI__F_avg=3, + STBI__F_paeth=4, + // synthetic filters used for first scanline to avoid needing a dummy row of 0s + STBI__F_avg_first, + STBI__F_paeth_first +}; + +static stbi_uc first_row_filter[5] = +{ + STBI__F_none, + STBI__F_sub, + STBI__F_none, + STBI__F_avg_first, + STBI__F_paeth_first +}; + +static int stbi__paeth(int a, int b, int c) +{ + int p = a + b - c; + int pa = abs(p-a); + int pb = abs(p-b); + int pc = abs(p-c); + if (pa <= pb && pa <= pc) return a; + if (pb <= pc) return b; + return c; +} + +static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 }; + +// create the png data from post-deflated data +static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color) +{ + int bytes = (depth == 16? 2 : 1); + stbi__context *s = a->s; + stbi__uint32 i,j,stride = x*out_n*bytes; + stbi__uint32 img_len, img_width_bytes; + int k; + int img_n = s->img_n; // copy it into a local for later + + int output_bytes = out_n*bytes; + int filter_bytes = img_n*bytes; + int width = x; + + STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1); + a->out = (stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0); // extra bytes to write off the end into + if (!a->out) return stbi__err("outofmem", "Out of memory"); + + if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return stbi__err("too large", "Corrupt PNG"); + img_width_bytes = (((img_n * x * depth) + 7) >> 3); + img_len = (img_width_bytes + 1) * y; + + // we used to check for exact match between raw_len and img_len on non-interlaced PNGs, + // but issue #276 reported a PNG in the wild that had extra data at the end (all zeros), + // so just check for raw_len < img_len always. + if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG"); + + for (j=0; j < y; ++j) { + stbi_uc *cur = a->out + stride*j; + stbi_uc *prior; + int filter = *raw++; + + if (filter > 4) + return stbi__err("invalid filter","Corrupt PNG"); + + if (depth < 8) { + STBI_ASSERT(img_width_bytes <= x); + cur += x*out_n - img_width_bytes; // store output to the rightmost img_len bytes, so we can decode in place + filter_bytes = 1; + width = img_width_bytes; + } + prior = cur - stride; // bugfix: need to compute this after 'cur +=' computation above + + // if first row, use special filter that doesn't sample previous row + if (j == 0) filter = first_row_filter[filter]; + + // handle first byte explicitly + for (k=0; k < filter_bytes; ++k) { + switch (filter) { + case STBI__F_none : cur[k] = raw[k]; break; + case STBI__F_sub : cur[k] = raw[k]; break; + case STBI__F_up : cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break; + case STBI__F_avg : cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); break; + case STBI__F_paeth : cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0,prior[k],0)); break; + case STBI__F_avg_first : cur[k] = raw[k]; break; + case STBI__F_paeth_first: cur[k] = raw[k]; break; + } + } + + if (depth == 8) { + if (img_n != out_n) + cur[img_n] = 255; // first pixel + raw += img_n; + cur += out_n; + prior += out_n; + } else if (depth == 16) { + if (img_n != out_n) { + cur[filter_bytes] = 255; // first pixel top byte + cur[filter_bytes+1] = 255; // first pixel bottom byte + } + raw += filter_bytes; + cur += output_bytes; + prior += output_bytes; + } else { + raw += 1; + cur += 1; + prior += 1; + } + + // this is a little gross, so that we don't switch per-pixel or per-component + if (depth < 8 || img_n == out_n) { + int nk = (width - 1)*filter_bytes; + #define STBI__CASE(f) \ + case f: \ + for (k=0; k < nk; ++k) + switch (filter) { + // "none" filter turns into a memcpy here; make that explicit. + case STBI__F_none: memcpy(cur, raw, nk); break; + STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); } break; + STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break; + STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); } break; + STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],prior[k],prior[k-filter_bytes])); } break; + STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); } break; + STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],0,0)); } break; + } + #undef STBI__CASE + raw += nk; + } else { + STBI_ASSERT(img_n+1 == out_n); + #define STBI__CASE(f) \ + case f: \ + for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) \ + for (k=0; k < filter_bytes; ++k) + switch (filter) { + STBI__CASE(STBI__F_none) { cur[k] = raw[k]; } break; + STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k- output_bytes]); } break; + STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break; + STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k- output_bytes])>>1)); } break; + STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],prior[k],prior[k- output_bytes])); } break; + STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k- output_bytes] >> 1)); } break; + STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],0,0)); } break; + } + #undef STBI__CASE + + // the loop above sets the high byte of the pixels' alpha, but for + // 16 bit png files we also need the low byte set. we'll do that here. + if (depth == 16) { + cur = a->out + stride*j; // start at the beginning of the row again + for (i=0; i < x; ++i,cur+=output_bytes) { + cur[filter_bytes+1] = 255; + } + } + } + } + + // we make a separate pass to expand bits to pixels; for performance, + // this could run two scanlines behind the above code, so it won't + // intefere with filtering but will still be in the cache. + if (depth < 8) { + for (j=0; j < y; ++j) { + stbi_uc *cur = a->out + stride*j; + stbi_uc *in = a->out + stride*j + x*out_n - img_width_bytes; + // unpack 1/2/4-bit into a 8-bit buffer. allows us to keep the common 8-bit path optimal at minimal cost for 1/2/4-bit + // png guarante byte alignment, if width is not multiple of 8/4/2 we'll decode dummy trailing data that will be skipped in the later loop + stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1; // scale grayscale values to 0..255 range + + // note that the final byte might overshoot and write more data than desired. + // we can allocate enough data that this never writes out of memory, but it + // could also overwrite the next scanline. can it overwrite non-empty data + // on the next scanline? yes, consider 1-pixel-wide scanlines with 1-bit-per-pixel. + // so we need to explicitly clamp the final ones + + if (depth == 4) { + for (k=x*img_n; k >= 2; k-=2, ++in) { + *cur++ = scale * ((*in >> 4) ); + *cur++ = scale * ((*in ) & 0x0f); + } + if (k > 0) *cur++ = scale * ((*in >> 4) ); + } else if (depth == 2) { + for (k=x*img_n; k >= 4; k-=4, ++in) { + *cur++ = scale * ((*in >> 6) ); + *cur++ = scale * ((*in >> 4) & 0x03); + *cur++ = scale * ((*in >> 2) & 0x03); + *cur++ = scale * ((*in ) & 0x03); + } + if (k > 0) *cur++ = scale * ((*in >> 6) ); + if (k > 1) *cur++ = scale * ((*in >> 4) & 0x03); + if (k > 2) *cur++ = scale * ((*in >> 2) & 0x03); + } else if (depth == 1) { + for (k=x*img_n; k >= 8; k-=8, ++in) { + *cur++ = scale * ((*in >> 7) ); + *cur++ = scale * ((*in >> 6) & 0x01); + *cur++ = scale * ((*in >> 5) & 0x01); + *cur++ = scale * ((*in >> 4) & 0x01); + *cur++ = scale * ((*in >> 3) & 0x01); + *cur++ = scale * ((*in >> 2) & 0x01); + *cur++ = scale * ((*in >> 1) & 0x01); + *cur++ = scale * ((*in ) & 0x01); + } + if (k > 0) *cur++ = scale * ((*in >> 7) ); + if (k > 1) *cur++ = scale * ((*in >> 6) & 0x01); + if (k > 2) *cur++ = scale * ((*in >> 5) & 0x01); + if (k > 3) *cur++ = scale * ((*in >> 4) & 0x01); + if (k > 4) *cur++ = scale * ((*in >> 3) & 0x01); + if (k > 5) *cur++ = scale * ((*in >> 2) & 0x01); + if (k > 6) *cur++ = scale * ((*in >> 1) & 0x01); + } + if (img_n != out_n) { + int q; + // insert alpha = 255 + cur = a->out + stride*j; + if (img_n == 1) { + for (q=x-1; q >= 0; --q) { + cur[q*2+1] = 255; + cur[q*2+0] = cur[q]; + } + } else { + STBI_ASSERT(img_n == 3); + for (q=x-1; q >= 0; --q) { + cur[q*4+3] = 255; + cur[q*4+2] = cur[q*3+2]; + cur[q*4+1] = cur[q*3+1]; + cur[q*4+0] = cur[q*3+0]; + } + } + } + } + } else if (depth == 16) { + // force the image data from big-endian to platform-native. + // this is done in a separate pass due to the decoding relying + // on the data being untouched, but could probably be done + // per-line during decode if care is taken. + stbi_uc *cur = a->out; + stbi__uint16 *cur16 = (stbi__uint16*)cur; + + for(i=0; i < x*y*out_n; ++i,cur16++,cur+=2) { + *cur16 = (cur[0] << 8) | cur[1]; + } + } + + return 1; +} + +static int stbi__create_png_image(stbi__png *a, stbi_uc *image_data, stbi__uint32 image_data_len, int out_n, int depth, int color, int interlaced) +{ + int bytes = (depth == 16 ? 2 : 1); + int out_bytes = out_n * bytes; + stbi_uc *final; + int p; + if (!interlaced) + return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color); + + // de-interlacing + final = (stbi_uc *) stbi__malloc_mad3(a->s->img_x, a->s->img_y, out_bytes, 0); + for (p=0; p < 7; ++p) { + int xorig[] = { 0,4,0,2,0,1,0 }; + int yorig[] = { 0,0,4,0,2,0,1 }; + int xspc[] = { 8,8,4,4,2,2,1 }; + int yspc[] = { 8,8,8,4,4,2,2 }; + int i,j,x,y; + // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1 + x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p]; + y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p]; + if (x && y) { + stbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y; + if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) { + STBI_FREE(final); + return 0; + } + for (j=0; j < y; ++j) { + for (i=0; i < x; ++i) { + int out_y = j*yspc[p]+yorig[p]; + int out_x = i*xspc[p]+xorig[p]; + memcpy(final + out_y*a->s->img_x*out_bytes + out_x*out_bytes, + a->out + (j*x+i)*out_bytes, out_bytes); + } + } + STBI_FREE(a->out); + image_data += img_len; + image_data_len -= img_len; + } + } + a->out = final; + + return 1; +} + +static int stbi__compute_transparency(stbi__png *z, stbi_uc tc[3], int out_n) +{ + stbi__context *s = z->s; + stbi__uint32 i, pixel_count = s->img_x * s->img_y; + stbi_uc *p = z->out; + + // compute color-based transparency, assuming we've + // already got 255 as the alpha value in the output + STBI_ASSERT(out_n == 2 || out_n == 4); + + if (out_n == 2) { + for (i=0; i < pixel_count; ++i) { + p[1] = (p[0] == tc[0] ? 0 : 255); + p += 2; + } + } else { + for (i=0; i < pixel_count; ++i) { + if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) + p[3] = 0; + p += 4; + } + } + return 1; +} + +static int stbi__compute_transparency16(stbi__png *z, stbi__uint16 tc[3], int out_n) +{ + stbi__context *s = z->s; + stbi__uint32 i, pixel_count = s->img_x * s->img_y; + stbi__uint16 *p = (stbi__uint16*) z->out; + + // compute color-based transparency, assuming we've + // already got 65535 as the alpha value in the output + STBI_ASSERT(out_n == 2 || out_n == 4); + + if (out_n == 2) { + for (i = 0; i < pixel_count; ++i) { + p[1] = (p[0] == tc[0] ? 0 : 65535); + p += 2; + } + } else { + for (i = 0; i < pixel_count; ++i) { + if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) + p[3] = 0; + p += 4; + } + } + return 1; +} + +static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int pal_img_n) +{ + stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y; + stbi_uc *p, *temp_out, *orig = a->out; + + p = (stbi_uc *) stbi__malloc_mad2(pixel_count, pal_img_n, 0); + if (p == NULL) return stbi__err("outofmem", "Out of memory"); + + // between here and free(out) below, exitting would leak + temp_out = p; + + if (pal_img_n == 3) { + for (i=0; i < pixel_count; ++i) { + int n = orig[i]*4; + p[0] = palette[n ]; + p[1] = palette[n+1]; + p[2] = palette[n+2]; + p += 3; + } + } else { + for (i=0; i < pixel_count; ++i) { + int n = orig[i]*4; + p[0] = palette[n ]; + p[1] = palette[n+1]; + p[2] = palette[n+2]; + p[3] = palette[n+3]; + p += 4; + } + } + STBI_FREE(a->out); + a->out = temp_out; + + STBI_NOTUSED(len); + + return 1; +} + +static int stbi__unpremultiply_on_load = 0; +static int stbi__de_iphone_flag = 0; + +STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply) +{ + stbi__unpremultiply_on_load = flag_true_if_should_unpremultiply; +} + +STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert) +{ + stbi__de_iphone_flag = flag_true_if_should_convert; +} + +static void stbi__de_iphone(stbi__png *z) +{ + stbi__context *s = z->s; + stbi__uint32 i, pixel_count = s->img_x * s->img_y; + stbi_uc *p = z->out; + + if (s->img_out_n == 3) { // convert bgr to rgb + for (i=0; i < pixel_count; ++i) { + stbi_uc t = p[0]; + p[0] = p[2]; + p[2] = t; + p += 3; + } + } else { + STBI_ASSERT(s->img_out_n == 4); + if (stbi__unpremultiply_on_load) { + // convert bgr to rgb and unpremultiply + for (i=0; i < pixel_count; ++i) { + stbi_uc a = p[3]; + stbi_uc t = p[0]; + if (a) { + stbi_uc half = a / 2; + p[0] = (p[2] * 255 + half) / a; + p[1] = (p[1] * 255 + half) / a; + p[2] = ( t * 255 + half) / a; + } else { + p[0] = p[2]; + p[2] = t; + } + p += 4; + } + } else { + // convert bgr to rgb + for (i=0; i < pixel_count; ++i) { + stbi_uc t = p[0]; + p[0] = p[2]; + p[2] = t; + p += 4; + } + } + } +} + +#define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d)) + +static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) +{ + stbi_uc palette[1024], pal_img_n=0; + stbi_uc has_trans=0, tc[3]={0}; + stbi__uint16 tc16[3]; + stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0; + int first=1,k,interlace=0, color=0, is_iphone=0; + stbi__context *s = z->s; + + z->expanded = NULL; + z->idata = NULL; + z->out = NULL; + + if (!stbi__check_png_header(s)) return 0; + + if (scan == STBI__SCAN_type) return 1; + + for (;;) { + stbi__pngchunk c = stbi__get_chunk_header(s); + switch (c.type) { + case STBI__PNG_TYPE('C','g','B','I'): + is_iphone = 1; + stbi__skip(s, c.length); + break; + case STBI__PNG_TYPE('I','H','D','R'): { + int comp,filter; + if (!first) return stbi__err("multiple IHDR","Corrupt PNG"); + first = 0; + if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG"); + s->img_x = stbi__get32be(s); if (s->img_x > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)"); + s->img_y = stbi__get32be(s); if (s->img_y > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)"); + z->depth = stbi__get8(s); if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only"); + color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG"); + if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG"); + if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG"); + comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG"); + filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG"); + interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG"); + if (!s->img_x || !s->img_y) return stbi__err("0-pixel image","Corrupt PNG"); + if (!pal_img_n) { + s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0); + if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode"); + if (scan == STBI__SCAN_header) return 1; + } else { + // if paletted, then pal_n is our final components, and + // img_n is # components to decompress/filter. + s->img_n = 1; + if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG"); + // if SCAN_header, have to scan to see if we have a tRNS + } + break; + } + + case STBI__PNG_TYPE('P','L','T','E'): { + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if (c.length > 256*3) return stbi__err("invalid PLTE","Corrupt PNG"); + pal_len = c.length / 3; + if (pal_len * 3 != c.length) return stbi__err("invalid PLTE","Corrupt PNG"); + for (i=0; i < pal_len; ++i) { + palette[i*4+0] = stbi__get8(s); + palette[i*4+1] = stbi__get8(s); + palette[i*4+2] = stbi__get8(s); + palette[i*4+3] = 255; + } + break; + } + + case STBI__PNG_TYPE('t','R','N','S'): { + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if (z->idata) return stbi__err("tRNS after IDAT","Corrupt PNG"); + if (pal_img_n) { + if (scan == STBI__SCAN_header) { s->img_n = 4; return 1; } + if (pal_len == 0) return stbi__err("tRNS before PLTE","Corrupt PNG"); + if (c.length > pal_len) return stbi__err("bad tRNS len","Corrupt PNG"); + pal_img_n = 4; + for (i=0; i < c.length; ++i) + palette[i*4+3] = stbi__get8(s); + } else { + if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG"); + if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG"); + has_trans = 1; + if (z->depth == 16) { + for (k = 0; k < s->img_n; ++k) tc16[k] = (stbi__uint16)stbi__get16be(s); // copy the values as-is + } else { + for (k = 0; k < s->img_n; ++k) tc[k] = (stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth]; // non 8-bit images will be larger + } + } + break; + } + + case STBI__PNG_TYPE('I','D','A','T'): { + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if (pal_img_n && !pal_len) return stbi__err("no PLTE","Corrupt PNG"); + if (scan == STBI__SCAN_header) { s->img_n = pal_img_n; return 1; } + if ((int)(ioff + c.length) < (int)ioff) return 0; + if (ioff + c.length > idata_limit) { + stbi__uint32 idata_limit_old = idata_limit; + stbi_uc *p; + if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096; + while (ioff + c.length > idata_limit) + idata_limit *= 2; + STBI_NOTUSED(idata_limit_old); + p = (stbi_uc *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory"); + z->idata = p; + } + if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG"); + ioff += c.length; + break; + } + + case STBI__PNG_TYPE('I','E','N','D'): { + stbi__uint32 raw_len, bpl; + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if (scan != STBI__SCAN_load) return 1; + if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG"); + // initial guess for decoded data size to avoid unnecessary reallocs + bpl = (s->img_x * z->depth + 7) / 8; // bytes per line, per component + raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */; + z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone); + if (z->expanded == NULL) return 0; // zlib should set error + STBI_FREE(z->idata); z->idata = NULL; + if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans) + s->img_out_n = s->img_n+1; + else + s->img_out_n = s->img_n; + if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0; + if (has_trans) { + if (z->depth == 16) { + if (!stbi__compute_transparency16(z, tc16, s->img_out_n)) return 0; + } else { + if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0; + } + } + if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2) + stbi__de_iphone(z); + if (pal_img_n) { + // pal_img_n == 3 or 4 + s->img_n = pal_img_n; // record the actual colors we had + s->img_out_n = pal_img_n; + if (req_comp >= 3) s->img_out_n = req_comp; + if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n)) + return 0; + } else if (has_trans) { + // non-paletted image with tRNS -> source image has (constant) alpha + ++s->img_n; + } + STBI_FREE(z->expanded); z->expanded = NULL; + // end of PNG chunk, read and skip CRC + stbi__get32be(s); + return 1; + } + + default: + // if critical, fail + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if ((c.type & (1 << 29)) == 0) { + #ifndef STBI_NO_FAILURE_STRINGS + // not threadsafe + static char invalid_chunk[] = "XXXX PNG chunk not known"; + invalid_chunk[0] = STBI__BYTECAST(c.type >> 24); + invalid_chunk[1] = STBI__BYTECAST(c.type >> 16); + invalid_chunk[2] = STBI__BYTECAST(c.type >> 8); + invalid_chunk[3] = STBI__BYTECAST(c.type >> 0); + #endif + return stbi__err(invalid_chunk, "PNG not supported: unknown PNG chunk type"); + } + stbi__skip(s, c.length); + break; + } + // end of PNG chunk, read and skip CRC + stbi__get32be(s); + } +} + +static void *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp, stbi__result_info *ri) +{ + void *result=NULL; + if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error"); + if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) { + if (p->depth < 8) + ri->bits_per_channel = 8; + else + ri->bits_per_channel = p->depth; + result = p->out; + p->out = NULL; + if (req_comp && req_comp != p->s->img_out_n) { + if (ri->bits_per_channel == 8) + result = stbi__convert_format((unsigned char *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y); + else + result = stbi__convert_format16((stbi__uint16 *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y); + p->s->img_out_n = req_comp; + if (result == NULL) return result; + } + *x = p->s->img_x; + *y = p->s->img_y; + if (n) *n = p->s->img_n; + } + STBI_FREE(p->out); p->out = NULL; + STBI_FREE(p->expanded); p->expanded = NULL; + STBI_FREE(p->idata); p->idata = NULL; + + return result; +} + +static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + stbi__png p; + p.s = s; + return stbi__do_png(&p, x,y,comp,req_comp, ri); +} + +static int stbi__png_test(stbi__context *s) +{ + int r; + r = stbi__check_png_header(s); + stbi__rewind(s); + return r; +} + +static int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp) +{ + if (!stbi__parse_png_file(p, STBI__SCAN_header, 0)) { + stbi__rewind( p->s ); + return 0; + } + if (x) *x = p->s->img_x; + if (y) *y = p->s->img_y; + if (comp) *comp = p->s->img_n; + return 1; +} + +static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp) +{ + stbi__png p; + p.s = s; + return stbi__png_info_raw(&p, x, y, comp); +} + +static int stbi__png_is16(stbi__context *s) +{ + stbi__png p; + p.s = s; + if (!stbi__png_info_raw(&p, NULL, NULL, NULL)) + return 0; + if (p.depth != 16) { + stbi__rewind(p.s); + return 0; + } + return 1; +} +#endif + +// Microsoft/Windows BMP image + +#ifndef STBI_NO_BMP +static int stbi__bmp_test_raw(stbi__context *s) +{ + int r; + int sz; + if (stbi__get8(s) != 'B') return 0; + if (stbi__get8(s) != 'M') return 0; + stbi__get32le(s); // discard filesize + stbi__get16le(s); // discard reserved + stbi__get16le(s); // discard reserved + stbi__get32le(s); // discard data offset + sz = stbi__get32le(s); + r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124); + return r; +} + +static int stbi__bmp_test(stbi__context *s) +{ + int r = stbi__bmp_test_raw(s); + stbi__rewind(s); + return r; +} + + +// returns 0..31 for the highest set bit +static int stbi__high_bit(unsigned int z) +{ + int n=0; + if (z == 0) return -1; + if (z >= 0x10000) { n += 16; z >>= 16; } + if (z >= 0x00100) { n += 8; z >>= 8; } + if (z >= 0x00010) { n += 4; z >>= 4; } + if (z >= 0x00004) { n += 2; z >>= 2; } + if (z >= 0x00002) { n += 1;/* >>= 1;*/ } + return n; +} + +static int stbi__bitcount(unsigned int a) +{ + a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2 + a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4 + a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits + a = (a + (a >> 8)); // max 16 per 8 bits + a = (a + (a >> 16)); // max 32 per 8 bits + return a & 0xff; +} + +// extract an arbitrarily-aligned N-bit value (N=bits) +// from v, and then make it 8-bits long and fractionally +// extend it to full full range. +static int stbi__shiftsigned(unsigned int v, int shift, int bits) +{ + static unsigned int mul_table[9] = { + 0, + 0xff/*0b11111111*/, 0x55/*0b01010101*/, 0x49/*0b01001001*/, 0x11/*0b00010001*/, + 0x21/*0b00100001*/, 0x41/*0b01000001*/, 0x81/*0b10000001*/, 0x01/*0b00000001*/, + }; + static unsigned int shift_table[9] = { + 0, 0,0,1,0,2,4,6,0, + }; + if (shift < 0) + v <<= -shift; + else + v >>= shift; + STBI_ASSERT(v < 256); + v >>= (8-bits); + STBI_ASSERT(bits >= 0 && bits <= 8); + return (int) ((unsigned) v * mul_table[bits]) >> shift_table[bits]; +} + +typedef struct +{ + int bpp, offset, hsz; + unsigned int mr,mg,mb,ma, all_a; + int extra_read; +} stbi__bmp_data; + +static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info) +{ + int hsz; + if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP", "Corrupt BMP"); + stbi__get32le(s); // discard filesize + stbi__get16le(s); // discard reserved + stbi__get16le(s); // discard reserved + info->offset = stbi__get32le(s); + info->hsz = hsz = stbi__get32le(s); + info->mr = info->mg = info->mb = info->ma = 0; + info->extra_read = 14; + + if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown"); + if (hsz == 12) { + s->img_x = stbi__get16le(s); + s->img_y = stbi__get16le(s); + } else { + s->img_x = stbi__get32le(s); + s->img_y = stbi__get32le(s); + } + if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP", "bad BMP"); + info->bpp = stbi__get16le(s); + if (hsz != 12) { + int compress = stbi__get32le(s); + if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE"); + stbi__get32le(s); // discard sizeof + stbi__get32le(s); // discard hres + stbi__get32le(s); // discard vres + stbi__get32le(s); // discard colorsused + stbi__get32le(s); // discard max important + if (hsz == 40 || hsz == 56) { + if (hsz == 56) { + stbi__get32le(s); + stbi__get32le(s); + stbi__get32le(s); + stbi__get32le(s); + } + if (info->bpp == 16 || info->bpp == 32) { + if (compress == 0) { + if (info->bpp == 32) { + info->mr = 0xffu << 16; + info->mg = 0xffu << 8; + info->mb = 0xffu << 0; + info->ma = 0xffu << 24; + info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 0 + } else { + info->mr = 31u << 10; + info->mg = 31u << 5; + info->mb = 31u << 0; + } + } else if (compress == 3) { + info->mr = stbi__get32le(s); + info->mg = stbi__get32le(s); + info->mb = stbi__get32le(s); + info->extra_read += 12; + // not documented, but generated by photoshop and handled by mspaint + if (info->mr == info->mg && info->mg == info->mb) { + // ?!?!? + return stbi__errpuc("bad BMP", "bad BMP"); + } + } else + return stbi__errpuc("bad BMP", "bad BMP"); + } + } else { + int i; + if (hsz != 108 && hsz != 124) + return stbi__errpuc("bad BMP", "bad BMP"); + info->mr = stbi__get32le(s); + info->mg = stbi__get32le(s); + info->mb = stbi__get32le(s); + info->ma = stbi__get32le(s); + stbi__get32le(s); // discard color space + for (i=0; i < 12; ++i) + stbi__get32le(s); // discard color space parameters + if (hsz == 124) { + stbi__get32le(s); // discard rendering intent + stbi__get32le(s); // discard offset of profile data + stbi__get32le(s); // discard size of profile data + stbi__get32le(s); // discard reserved + } + } + } + return (void *) 1; +} + + +static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + stbi_uc *out; + unsigned int mr=0,mg=0,mb=0,ma=0, all_a; + stbi_uc pal[256][4]; + int psize=0,i,j,width; + int flip_vertically, pad, target; + stbi__bmp_data info; + STBI_NOTUSED(ri); + + info.all_a = 255; + if (stbi__bmp_parse_header(s, &info) == NULL) + return NULL; // error code already set + + flip_vertically = ((int) s->img_y) > 0; + s->img_y = abs((int) s->img_y); + + mr = info.mr; + mg = info.mg; + mb = info.mb; + ma = info.ma; + all_a = info.all_a; + + if (info.hsz == 12) { + if (info.bpp < 24) + psize = (info.offset - info.extra_read - 24) / 3; + } else { + if (info.bpp < 16) + psize = (info.offset - info.extra_read - info.hsz) >> 2; + } + if (psize == 0) { + STBI_ASSERT(info.offset == (s->img_buffer - s->buffer_start)); + } + + if (info.bpp == 24 && ma == 0xff000000) + s->img_n = 3; + else + s->img_n = ma ? 4 : 3; + if (req_comp && req_comp >= 3) // we can directly decode 3 or 4 + target = req_comp; + else + target = s->img_n; // if they want monochrome, we'll post-convert + + // sanity-check size + if (!stbi__mad3sizes_valid(target, s->img_x, s->img_y, 0)) + return stbi__errpuc("too large", "Corrupt BMP"); + + out = (stbi_uc *) stbi__malloc_mad3(target, s->img_x, s->img_y, 0); + if (!out) return stbi__errpuc("outofmem", "Out of memory"); + if (info.bpp < 16) { + int z=0; + if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); } + for (i=0; i < psize; ++i) { + pal[i][2] = stbi__get8(s); + pal[i][1] = stbi__get8(s); + pal[i][0] = stbi__get8(s); + if (info.hsz != 12) stbi__get8(s); + pal[i][3] = 255; + } + stbi__skip(s, info.offset - info.extra_read - info.hsz - psize * (info.hsz == 12 ? 3 : 4)); + if (info.bpp == 1) width = (s->img_x + 7) >> 3; + else if (info.bpp == 4) width = (s->img_x + 1) >> 1; + else if (info.bpp == 8) width = s->img_x; + else { STBI_FREE(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); } + pad = (-width)&3; + if (info.bpp == 1) { + for (j=0; j < (int) s->img_y; ++j) { + int bit_offset = 7, v = stbi__get8(s); + for (i=0; i < (int) s->img_x; ++i) { + int color = (v>>bit_offset)&0x1; + out[z++] = pal[color][0]; + out[z++] = pal[color][1]; + out[z++] = pal[color][2]; + if (target == 4) out[z++] = 255; + if (i+1 == (int) s->img_x) break; + if((--bit_offset) < 0) { + bit_offset = 7; + v = stbi__get8(s); + } + } + stbi__skip(s, pad); + } + } else { + for (j=0; j < (int) s->img_y; ++j) { + for (i=0; i < (int) s->img_x; i += 2) { + int v=stbi__get8(s),v2=0; + if (info.bpp == 4) { + v2 = v & 15; + v >>= 4; + } + out[z++] = pal[v][0]; + out[z++] = pal[v][1]; + out[z++] = pal[v][2]; + if (target == 4) out[z++] = 255; + if (i+1 == (int) s->img_x) break; + v = (info.bpp == 8) ? stbi__get8(s) : v2; + out[z++] = pal[v][0]; + out[z++] = pal[v][1]; + out[z++] = pal[v][2]; + if (target == 4) out[z++] = 255; + } + stbi__skip(s, pad); + } + } + } else { + int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0; + int z = 0; + int easy=0; + stbi__skip(s, info.offset - info.extra_read - info.hsz); + if (info.bpp == 24) width = 3 * s->img_x; + else if (info.bpp == 16) width = 2*s->img_x; + else /* bpp = 32 and pad = 0 */ width=0; + pad = (-width) & 3; + if (info.bpp == 24) { + easy = 1; + } else if (info.bpp == 32) { + if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000) + easy = 2; + } + if (!easy) { + if (!mr || !mg || !mb) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); } + // right shift amt to put high bit in position #7 + rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr); + gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg); + bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb); + ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma); + } + for (j=0; j < (int) s->img_y; ++j) { + if (easy) { + for (i=0; i < (int) s->img_x; ++i) { + unsigned char a; + out[z+2] = stbi__get8(s); + out[z+1] = stbi__get8(s); + out[z+0] = stbi__get8(s); + z += 3; + a = (easy == 2 ? stbi__get8(s) : 255); + all_a |= a; + if (target == 4) out[z++] = a; + } + } else { + int bpp = info.bpp; + for (i=0; i < (int) s->img_x; ++i) { + stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s)); + unsigned int a; + out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount)); + out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount)); + out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount)); + a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255); + all_a |= a; + if (target == 4) out[z++] = STBI__BYTECAST(a); + } + } + stbi__skip(s, pad); + } + } + + // if alpha channel is all 0s, replace with all 255s + if (target == 4 && all_a == 0) + for (i=4*s->img_x*s->img_y-1; i >= 0; i -= 4) + out[i] = 255; + + if (flip_vertically) { + stbi_uc t; + for (j=0; j < (int) s->img_y>>1; ++j) { + stbi_uc *p1 = out + j *s->img_x*target; + stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target; + for (i=0; i < (int) s->img_x*target; ++i) { + t = p1[i]; p1[i] = p2[i]; p2[i] = t; + } + } + } + + if (req_comp && req_comp != target) { + out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y); + if (out == NULL) return out; // stbi__convert_format frees input on failure + } + + *x = s->img_x; + *y = s->img_y; + if (comp) *comp = s->img_n; + return out; +} +#endif + +// Targa Truevision - TGA +// by Jonathan Dummer +#ifndef STBI_NO_TGA +// returns STBI_rgb or whatever, 0 on error +static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16) +{ + // only RGB or RGBA (incl. 16bit) or grey allowed + if (is_rgb16) *is_rgb16 = 0; + switch(bits_per_pixel) { + case 8: return STBI_grey; + case 16: if(is_grey) return STBI_grey_alpha; + // fallthrough + case 15: if(is_rgb16) *is_rgb16 = 1; + return STBI_rgb; + case 24: // fallthrough + case 32: return bits_per_pixel/8; + default: return 0; + } +} + +static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp) +{ + int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel, tga_colormap_bpp; + int sz, tga_colormap_type; + stbi__get8(s); // discard Offset + tga_colormap_type = stbi__get8(s); // colormap type + if( tga_colormap_type > 1 ) { + stbi__rewind(s); + return 0; // only RGB or indexed allowed + } + tga_image_type = stbi__get8(s); // image type + if ( tga_colormap_type == 1 ) { // colormapped (paletted) image + if (tga_image_type != 1 && tga_image_type != 9) { + stbi__rewind(s); + return 0; + } + stbi__skip(s,4); // skip index of first colormap entry and number of entries + sz = stbi__get8(s); // check bits per palette color entry + if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) { + stbi__rewind(s); + return 0; + } + stbi__skip(s,4); // skip image x and y origin + tga_colormap_bpp = sz; + } else { // "normal" image w/o colormap - only RGB or grey allowed, +/- RLE + if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) { + stbi__rewind(s); + return 0; // only RGB or grey allowed, +/- RLE + } + stbi__skip(s,9); // skip colormap specification and image x/y origin + tga_colormap_bpp = 0; + } + tga_w = stbi__get16le(s); + if( tga_w < 1 ) { + stbi__rewind(s); + return 0; // test width + } + tga_h = stbi__get16le(s); + if( tga_h < 1 ) { + stbi__rewind(s); + return 0; // test height + } + tga_bits_per_pixel = stbi__get8(s); // bits per pixel + stbi__get8(s); // ignore alpha bits + if (tga_colormap_bpp != 0) { + if((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) { + // when using a colormap, tga_bits_per_pixel is the size of the indexes + // I don't think anything but 8 or 16bit indexes makes sense + stbi__rewind(s); + return 0; + } + tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL); + } else { + tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL); + } + if(!tga_comp) { + stbi__rewind(s); + return 0; + } + if (x) *x = tga_w; + if (y) *y = tga_h; + if (comp) *comp = tga_comp; + return 1; // seems to have passed everything +} + +static int stbi__tga_test(stbi__context *s) +{ + int res = 0; + int sz, tga_color_type; + stbi__get8(s); // discard Offset + tga_color_type = stbi__get8(s); // color type + if ( tga_color_type > 1 ) goto errorEnd; // only RGB or indexed allowed + sz = stbi__get8(s); // image type + if ( tga_color_type == 1 ) { // colormapped (paletted) image + if (sz != 1 && sz != 9) goto errorEnd; // colortype 1 demands image type 1 or 9 + stbi__skip(s,4); // skip index of first colormap entry and number of entries + sz = stbi__get8(s); // check bits per palette color entry + if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd; + stbi__skip(s,4); // skip image x and y origin + } else { // "normal" image w/o colormap + if ( (sz != 2) && (sz != 3) && (sz != 10) && (sz != 11) ) goto errorEnd; // only RGB or grey allowed, +/- RLE + stbi__skip(s,9); // skip colormap specification and image x/y origin + } + if ( stbi__get16le(s) < 1 ) goto errorEnd; // test width + if ( stbi__get16le(s) < 1 ) goto errorEnd; // test height + sz = stbi__get8(s); // bits per pixel + if ( (tga_color_type == 1) && (sz != 8) && (sz != 16) ) goto errorEnd; // for colormapped images, bpp is size of an index + if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd; + + res = 1; // if we got this far, everything's good and we can return 1 instead of 0 + +errorEnd: + stbi__rewind(s); + return res; +} + +// read 16bit value and convert to 24bit RGB +static void stbi__tga_read_rgb16(stbi__context *s, stbi_uc* out) +{ + stbi__uint16 px = (stbi__uint16)stbi__get16le(s); + stbi__uint16 fiveBitMask = 31; + // we have 3 channels with 5bits each + int r = (px >> 10) & fiveBitMask; + int g = (px >> 5) & fiveBitMask; + int b = px & fiveBitMask; + // Note that this saves the data in RGB(A) order, so it doesn't need to be swapped later + out[0] = (stbi_uc)((r * 255)/31); + out[1] = (stbi_uc)((g * 255)/31); + out[2] = (stbi_uc)((b * 255)/31); + + // some people claim that the most significant bit might be used for alpha + // (possibly if an alpha-bit is set in the "image descriptor byte") + // but that only made 16bit test images completely translucent.. + // so let's treat all 15 and 16bit TGAs as RGB with no alpha. +} + +static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + // read in the TGA header stuff + int tga_offset = stbi__get8(s); + int tga_indexed = stbi__get8(s); + int tga_image_type = stbi__get8(s); + int tga_is_RLE = 0; + int tga_palette_start = stbi__get16le(s); + int tga_palette_len = stbi__get16le(s); + int tga_palette_bits = stbi__get8(s); + int tga_x_origin = stbi__get16le(s); + int tga_y_origin = stbi__get16le(s); + int tga_width = stbi__get16le(s); + int tga_height = stbi__get16le(s); + int tga_bits_per_pixel = stbi__get8(s); + int tga_comp, tga_rgb16=0; + int tga_inverted = stbi__get8(s); + // int tga_alpha_bits = tga_inverted & 15; // the 4 lowest bits - unused (useless?) + // image data + unsigned char *tga_data; + unsigned char *tga_palette = NULL; + int i, j; + unsigned char raw_data[4] = {0}; + int RLE_count = 0; + int RLE_repeating = 0; + int read_next_pixel = 1; + STBI_NOTUSED(ri); + STBI_NOTUSED(tga_x_origin); // @TODO + STBI_NOTUSED(tga_y_origin); // @TODO + + // do a tiny bit of precessing + if ( tga_image_type >= 8 ) + { + tga_image_type -= 8; + tga_is_RLE = 1; + } + tga_inverted = 1 - ((tga_inverted >> 5) & 1); + + // If I'm paletted, then I'll use the number of bits from the palette + if ( tga_indexed ) tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16); + else tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3), &tga_rgb16); + + if(!tga_comp) // shouldn't really happen, stbi__tga_test() should have ensured basic consistency + return stbi__errpuc("bad format", "Can't find out TGA pixelformat"); + + // tga info + *x = tga_width; + *y = tga_height; + if (comp) *comp = tga_comp; + + if (!stbi__mad3sizes_valid(tga_width, tga_height, tga_comp, 0)) + return stbi__errpuc("too large", "Corrupt TGA"); + + tga_data = (unsigned char*)stbi__malloc_mad3(tga_width, tga_height, tga_comp, 0); + if (!tga_data) return stbi__errpuc("outofmem", "Out of memory"); + + // skip to the data's starting position (offset usually = 0) + stbi__skip(s, tga_offset ); + + if ( !tga_indexed && !tga_is_RLE && !tga_rgb16 ) { + for (i=0; i < tga_height; ++i) { + int row = tga_inverted ? tga_height -i - 1 : i; + stbi_uc *tga_row = tga_data + row*tga_width*tga_comp; + stbi__getn(s, tga_row, tga_width * tga_comp); + } + } else { + // do I need to load a palette? + if ( tga_indexed) + { + // any data to skip? (offset usually = 0) + stbi__skip(s, tga_palette_start ); + // load the palette + tga_palette = (unsigned char*)stbi__malloc_mad2(tga_palette_len, tga_comp, 0); + if (!tga_palette) { + STBI_FREE(tga_data); + return stbi__errpuc("outofmem", "Out of memory"); + } + if (tga_rgb16) { + stbi_uc *pal_entry = tga_palette; + STBI_ASSERT(tga_comp == STBI_rgb); + for (i=0; i < tga_palette_len; ++i) { + stbi__tga_read_rgb16(s, pal_entry); + pal_entry += tga_comp; + } + } else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) { + STBI_FREE(tga_data); + STBI_FREE(tga_palette); + return stbi__errpuc("bad palette", "Corrupt TGA"); + } + } + // load the data + for (i=0; i < tga_width * tga_height; ++i) + { + // if I'm in RLE mode, do I need to get a RLE stbi__pngchunk? + if ( tga_is_RLE ) + { + if ( RLE_count == 0 ) + { + // yep, get the next byte as a RLE command + int RLE_cmd = stbi__get8(s); + RLE_count = 1 + (RLE_cmd & 127); + RLE_repeating = RLE_cmd >> 7; + read_next_pixel = 1; + } else if ( !RLE_repeating ) + { + read_next_pixel = 1; + } + } else + { + read_next_pixel = 1; + } + // OK, if I need to read a pixel, do it now + if ( read_next_pixel ) + { + // load however much data we did have + if ( tga_indexed ) + { + // read in index, then perform the lookup + int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s); + if ( pal_idx >= tga_palette_len ) { + // invalid index + pal_idx = 0; + } + pal_idx *= tga_comp; + for (j = 0; j < tga_comp; ++j) { + raw_data[j] = tga_palette[pal_idx+j]; + } + } else if(tga_rgb16) { + STBI_ASSERT(tga_comp == STBI_rgb); + stbi__tga_read_rgb16(s, raw_data); + } else { + // read in the data raw + for (j = 0; j < tga_comp; ++j) { + raw_data[j] = stbi__get8(s); + } + } + // clear the reading flag for the next pixel + read_next_pixel = 0; + } // end of reading a pixel + + // copy data + for (j = 0; j < tga_comp; ++j) + tga_data[i*tga_comp+j] = raw_data[j]; + + // in case we're in RLE mode, keep counting down + --RLE_count; + } + // do I need to invert the image? + if ( tga_inverted ) + { + for (j = 0; j*2 < tga_height; ++j) + { + int index1 = j * tga_width * tga_comp; + int index2 = (tga_height - 1 - j) * tga_width * tga_comp; + for (i = tga_width * tga_comp; i > 0; --i) + { + unsigned char temp = tga_data[index1]; + tga_data[index1] = tga_data[index2]; + tga_data[index2] = temp; + ++index1; + ++index2; + } + } + } + // clear my palette, if I had one + if ( tga_palette != NULL ) + { + STBI_FREE( tga_palette ); + } + } + + // swap RGB - if the source data was RGB16, it already is in the right order + if (tga_comp >= 3 && !tga_rgb16) + { + unsigned char* tga_pixel = tga_data; + for (i=0; i < tga_width * tga_height; ++i) + { + unsigned char temp = tga_pixel[0]; + tga_pixel[0] = tga_pixel[2]; + tga_pixel[2] = temp; + tga_pixel += tga_comp; + } + } + + // convert to target component count + if (req_comp && req_comp != tga_comp) + tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height); + + // the things I do to get rid of an error message, and yet keep + // Microsoft's C compilers happy... [8^( + tga_palette_start = tga_palette_len = tga_palette_bits = + tga_x_origin = tga_y_origin = 0; + STBI_NOTUSED(tga_palette_start); + // OK, done + return tga_data; +} +#endif + +// ************************************************************************************************* +// Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB + +#ifndef STBI_NO_PSD +static int stbi__psd_test(stbi__context *s) +{ + int r = (stbi__get32be(s) == 0x38425053); + stbi__rewind(s); + return r; +} + +static int stbi__psd_decode_rle(stbi__context *s, stbi_uc *p, int pixelCount) +{ + int count, nleft, len; + + count = 0; + while ((nleft = pixelCount - count) > 0) { + len = stbi__get8(s); + if (len == 128) { + // No-op. + } else if (len < 128) { + // Copy next len+1 bytes literally. + len++; + if (len > nleft) return 0; // corrupt data + count += len; + while (len) { + *p = stbi__get8(s); + p += 4; + len--; + } + } else if (len > 128) { + stbi_uc val; + // Next -len+1 bytes in the dest are replicated from next source byte. + // (Interpret len as a negative 8-bit int.) + len = 257 - len; + if (len > nleft) return 0; // corrupt data + val = stbi__get8(s); + count += len; + while (len) { + *p = val; + p += 4; + len--; + } + } + } + + return 1; +} + +static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc) +{ + int pixelCount; + int channelCount, compression; + int channel, i; + int bitdepth; + int w,h; + stbi_uc *out; + STBI_NOTUSED(ri); + + // Check identifier + if (stbi__get32be(s) != 0x38425053) // "8BPS" + return stbi__errpuc("not PSD", "Corrupt PSD image"); + + // Check file type version. + if (stbi__get16be(s) != 1) + return stbi__errpuc("wrong version", "Unsupported version of PSD image"); + + // Skip 6 reserved bytes. + stbi__skip(s, 6 ); + + // Read the number of channels (R, G, B, A, etc). + channelCount = stbi__get16be(s); + if (channelCount < 0 || channelCount > 16) + return stbi__errpuc("wrong channel count", "Unsupported number of channels in PSD image"); + + // Read the rows and columns of the image. + h = stbi__get32be(s); + w = stbi__get32be(s); + + // Make sure the depth is 8 bits. + bitdepth = stbi__get16be(s); + if (bitdepth != 8 && bitdepth != 16) + return stbi__errpuc("unsupported bit depth", "PSD bit depth is not 8 or 16 bit"); + + // Make sure the color mode is RGB. + // Valid options are: + // 0: Bitmap + // 1: Grayscale + // 2: Indexed color + // 3: RGB color + // 4: CMYK color + // 7: Multichannel + // 8: Duotone + // 9: Lab color + if (stbi__get16be(s) != 3) + return stbi__errpuc("wrong color format", "PSD is not in RGB color format"); + + // Skip the Mode Data. (It's the palette for indexed color; other info for other modes.) + stbi__skip(s,stbi__get32be(s) ); + + // Skip the image resources. (resolution, pen tool paths, etc) + stbi__skip(s, stbi__get32be(s) ); + + // Skip the reserved data. + stbi__skip(s, stbi__get32be(s) ); + + // Find out if the data is compressed. + // Known values: + // 0: no compression + // 1: RLE compressed + compression = stbi__get16be(s); + if (compression > 1) + return stbi__errpuc("bad compression", "PSD has an unknown compression format"); + + // Check size + if (!stbi__mad3sizes_valid(4, w, h, 0)) + return stbi__errpuc("too large", "Corrupt PSD"); + + // Create the destination image. + + if (!compression && bitdepth == 16 && bpc == 16) { + out = (stbi_uc *) stbi__malloc_mad3(8, w, h, 0); + ri->bits_per_channel = 16; + } else + out = (stbi_uc *) stbi__malloc(4 * w*h); + + if (!out) return stbi__errpuc("outofmem", "Out of memory"); + pixelCount = w*h; + + // Initialize the data to zero. + //memset( out, 0, pixelCount * 4 ); + + // Finally, the image data. + if (compression) { + // RLE as used by .PSD and .TIFF + // Loop until you get the number of unpacked bytes you are expecting: + // Read the next source byte into n. + // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally. + // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times. + // Else if n is 128, noop. + // Endloop + + // The RLE-compressed data is preceded by a 2-byte data count for each row in the data, + // which we're going to just skip. + stbi__skip(s, h * channelCount * 2 ); + + // Read the RLE data by channel. + for (channel = 0; channel < 4; channel++) { + stbi_uc *p; + + p = out+channel; + if (channel >= channelCount) { + // Fill this channel with default data. + for (i = 0; i < pixelCount; i++, p += 4) + *p = (channel == 3 ? 255 : 0); + } else { + // Read the RLE data. + if (!stbi__psd_decode_rle(s, p, pixelCount)) { + STBI_FREE(out); + return stbi__errpuc("corrupt", "bad RLE data"); + } + } + } + + } else { + // We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...) + // where each channel consists of an 8-bit (or 16-bit) value for each pixel in the image. + + // Read the data by channel. + for (channel = 0; channel < 4; channel++) { + if (channel >= channelCount) { + // Fill this channel with default data. + if (bitdepth == 16 && bpc == 16) { + stbi__uint16 *q = ((stbi__uint16 *) out) + channel; + stbi__uint16 val = channel == 3 ? 65535 : 0; + for (i = 0; i < pixelCount; i++, q += 4) + *q = val; + } else { + stbi_uc *p = out+channel; + stbi_uc val = channel == 3 ? 255 : 0; + for (i = 0; i < pixelCount; i++, p += 4) + *p = val; + } + } else { + if (ri->bits_per_channel == 16) { // output bpc + stbi__uint16 *q = ((stbi__uint16 *) out) + channel; + for (i = 0; i < pixelCount; i++, q += 4) + *q = (stbi__uint16) stbi__get16be(s); + } else { + stbi_uc *p = out+channel; + if (bitdepth == 16) { // input bpc + for (i = 0; i < pixelCount; i++, p += 4) + *p = (stbi_uc) (stbi__get16be(s) >> 8); + } else { + for (i = 0; i < pixelCount; i++, p += 4) + *p = stbi__get8(s); + } + } + } + } + } + + // remove weird white matte from PSD + if (channelCount >= 4) { + if (ri->bits_per_channel == 16) { + for (i=0; i < w*h; ++i) { + stbi__uint16 *pixel = (stbi__uint16 *) out + 4*i; + if (pixel[3] != 0 && pixel[3] != 65535) { + float a = pixel[3] / 65535.0f; + float ra = 1.0f / a; + float inv_a = 65535.0f * (1 - ra); + pixel[0] = (stbi__uint16) (pixel[0]*ra + inv_a); + pixel[1] = (stbi__uint16) (pixel[1]*ra + inv_a); + pixel[2] = (stbi__uint16) (pixel[2]*ra + inv_a); + } + } + } else { + for (i=0; i < w*h; ++i) { + unsigned char *pixel = out + 4*i; + if (pixel[3] != 0 && pixel[3] != 255) { + float a = pixel[3] / 255.0f; + float ra = 1.0f / a; + float inv_a = 255.0f * (1 - ra); + pixel[0] = (unsigned char) (pixel[0]*ra + inv_a); + pixel[1] = (unsigned char) (pixel[1]*ra + inv_a); + pixel[2] = (unsigned char) (pixel[2]*ra + inv_a); + } + } + } + } + + // convert to desired output format + if (req_comp && req_comp != 4) { + if (ri->bits_per_channel == 16) + out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, 4, req_comp, w, h); + else + out = stbi__convert_format(out, 4, req_comp, w, h); + if (out == NULL) return out; // stbi__convert_format frees input on failure + } + + if (comp) *comp = 4; + *y = h; + *x = w; + + return out; +} +#endif + +// ************************************************************************************************* +// Softimage PIC loader +// by Tom Seddon +// +// See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format +// See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/ + +#ifndef STBI_NO_PIC +static int stbi__pic_is4(stbi__context *s,const char *str) +{ + int i; + for (i=0; i<4; ++i) + if (stbi__get8(s) != (stbi_uc)str[i]) + return 0; + + return 1; +} + +static int stbi__pic_test_core(stbi__context *s) +{ + int i; + + if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) + return 0; + + for(i=0;i<84;++i) + stbi__get8(s); + + if (!stbi__pic_is4(s,"PICT")) + return 0; + + return 1; +} + +typedef struct +{ + stbi_uc size,type,channel; +} stbi__pic_packet; + +static stbi_uc *stbi__readval(stbi__context *s, int channel, stbi_uc *dest) +{ + int mask=0x80, i; + + for (i=0; i<4; ++i, mask>>=1) { + if (channel & mask) { + if (stbi__at_eof(s)) return stbi__errpuc("bad file","PIC file too short"); + dest[i]=stbi__get8(s); + } + } + + return dest; +} + +static void stbi__copyval(int channel,stbi_uc *dest,const stbi_uc *src) +{ + int mask=0x80,i; + + for (i=0;i<4; ++i, mask>>=1) + if (channel&mask) + dest[i]=src[i]; +} + +static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result) +{ + int act_comp=0,num_packets=0,y,chained; + stbi__pic_packet packets[10]; + + // this will (should...) cater for even some bizarre stuff like having data + // for the same channel in multiple packets. + do { + stbi__pic_packet *packet; + + if (num_packets==sizeof(packets)/sizeof(packets[0])) + return stbi__errpuc("bad format","too many packets"); + + packet = &packets[num_packets++]; + + chained = stbi__get8(s); + packet->size = stbi__get8(s); + packet->type = stbi__get8(s); + packet->channel = stbi__get8(s); + + act_comp |= packet->channel; + + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (reading packets)"); + if (packet->size != 8) return stbi__errpuc("bad format","packet isn't 8bpp"); + } while (chained); + + *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel? + + for(y=0; ytype) { + default: + return stbi__errpuc("bad format","packet has bad compression type"); + + case 0: {//uncompressed + int x; + + for(x=0;xchannel,dest)) + return 0; + break; + } + + case 1://Pure RLE + { + int left=width, i; + + while (left>0) { + stbi_uc count,value[4]; + + count=stbi__get8(s); + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pure read count)"); + + if (count > left) + count = (stbi_uc) left; + + if (!stbi__readval(s,packet->channel,value)) return 0; + + for(i=0; ichannel,dest,value); + left -= count; + } + } + break; + + case 2: {//Mixed RLE + int left=width; + while (left>0) { + int count = stbi__get8(s), i; + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (mixed read count)"); + + if (count >= 128) { // Repeated + stbi_uc value[4]; + + if (count==128) + count = stbi__get16be(s); + else + count -= 127; + if (count > left) + return stbi__errpuc("bad file","scanline overrun"); + + if (!stbi__readval(s,packet->channel,value)) + return 0; + + for(i=0;ichannel,dest,value); + } else { // Raw + ++count; + if (count>left) return stbi__errpuc("bad file","scanline overrun"); + + for(i=0;ichannel,dest)) + return 0; + } + left-=count; + } + break; + } + } + } + } + + return result; +} + +static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp, stbi__result_info *ri) +{ + stbi_uc *result; + int i, x,y, internal_comp; + STBI_NOTUSED(ri); + + if (!comp) comp = &internal_comp; + + for (i=0; i<92; ++i) + stbi__get8(s); + + x = stbi__get16be(s); + y = stbi__get16be(s); + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)"); + if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large", "PIC image too large to decode"); + + stbi__get32be(s); //skip `ratio' + stbi__get16be(s); //skip `fields' + stbi__get16be(s); //skip `pad' + + // intermediate buffer is RGBA + result = (stbi_uc *) stbi__malloc_mad3(x, y, 4, 0); + memset(result, 0xff, x*y*4); + + if (!stbi__pic_load_core(s,x,y,comp, result)) { + STBI_FREE(result); + result=0; + } + *px = x; + *py = y; + if (req_comp == 0) req_comp = *comp; + result=stbi__convert_format(result,4,req_comp,x,y); + + return result; +} + +static int stbi__pic_test(stbi__context *s) +{ + int r = stbi__pic_test_core(s); + stbi__rewind(s); + return r; +} +#endif + +// ************************************************************************************************* +// GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb + +#ifndef STBI_NO_GIF +typedef struct +{ + stbi__int16 prefix; + stbi_uc first; + stbi_uc suffix; +} stbi__gif_lzw; + +typedef struct +{ + int w,h; + stbi_uc *out; // output buffer (always 4 components) + stbi_uc *background; // The current "background" as far as a gif is concerned + stbi_uc *history; + int flags, bgindex, ratio, transparent, eflags; + stbi_uc pal[256][4]; + stbi_uc lpal[256][4]; + stbi__gif_lzw codes[8192]; + stbi_uc *color_table; + int parse, step; + int lflags; + int start_x, start_y; + int max_x, max_y; + int cur_x, cur_y; + int line_size; + int delay; +} stbi__gif; + +static int stbi__gif_test_raw(stbi__context *s) +{ + int sz; + if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0; + sz = stbi__get8(s); + if (sz != '9' && sz != '7') return 0; + if (stbi__get8(s) != 'a') return 0; + return 1; +} + +static int stbi__gif_test(stbi__context *s) +{ + int r = stbi__gif_test_raw(s); + stbi__rewind(s); + return r; +} + +static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][4], int num_entries, int transp) +{ + int i; + for (i=0; i < num_entries; ++i) { + pal[i][2] = stbi__get8(s); + pal[i][1] = stbi__get8(s); + pal[i][0] = stbi__get8(s); + pal[i][3] = transp == i ? 0 : 255; + } +} + +static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_info) +{ + stbi_uc version; + if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') + return stbi__err("not GIF", "Corrupt GIF"); + + version = stbi__get8(s); + if (version != '7' && version != '9') return stbi__err("not GIF", "Corrupt GIF"); + if (stbi__get8(s) != 'a') return stbi__err("not GIF", "Corrupt GIF"); + + stbi__g_failure_reason = ""; + g->w = stbi__get16le(s); + g->h = stbi__get16le(s); + g->flags = stbi__get8(s); + g->bgindex = stbi__get8(s); + g->ratio = stbi__get8(s); + g->transparent = -1; + + if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments + + if (is_info) return 1; + + if (g->flags & 0x80) + stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1); + + return 1; +} + +static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp) +{ + stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif)); + if (!stbi__gif_header(s, g, comp, 1)) { + STBI_FREE(g); + stbi__rewind( s ); + return 0; + } + if (x) *x = g->w; + if (y) *y = g->h; + STBI_FREE(g); + return 1; +} + +static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code) +{ + stbi_uc *p, *c; + int idx; + + // recurse to decode the prefixes, since the linked-list is backwards, + // and working backwards through an interleaved image would be nasty + if (g->codes[code].prefix >= 0) + stbi__out_gif_code(g, g->codes[code].prefix); + + if (g->cur_y >= g->max_y) return; + + idx = g->cur_x + g->cur_y; + p = &g->out[idx]; + g->history[idx / 4] = 1; + + c = &g->color_table[g->codes[code].suffix * 4]; + if (c[3] > 128) { // don't render transparent pixels; + p[0] = c[2]; + p[1] = c[1]; + p[2] = c[0]; + p[3] = c[3]; + } + g->cur_x += 4; + + if (g->cur_x >= g->max_x) { + g->cur_x = g->start_x; + g->cur_y += g->step; + + while (g->cur_y >= g->max_y && g->parse > 0) { + g->step = (1 << g->parse) * g->line_size; + g->cur_y = g->start_y + (g->step >> 1); + --g->parse; + } + } +} + +static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g) +{ + stbi_uc lzw_cs; + stbi__int32 len, init_code; + stbi__uint32 first; + stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear; + stbi__gif_lzw *p; + + lzw_cs = stbi__get8(s); + if (lzw_cs > 12) return NULL; + clear = 1 << lzw_cs; + first = 1; + codesize = lzw_cs + 1; + codemask = (1 << codesize) - 1; + bits = 0; + valid_bits = 0; + for (init_code = 0; init_code < clear; init_code++) { + g->codes[init_code].prefix = -1; + g->codes[init_code].first = (stbi_uc) init_code; + g->codes[init_code].suffix = (stbi_uc) init_code; + } + + // support no starting clear code + avail = clear+2; + oldcode = -1; + + len = 0; + for(;;) { + if (valid_bits < codesize) { + if (len == 0) { + len = stbi__get8(s); // start new block + if (len == 0) + return g->out; + } + --len; + bits |= (stbi__int32) stbi__get8(s) << valid_bits; + valid_bits += 8; + } else { + stbi__int32 code = bits & codemask; + bits >>= codesize; + valid_bits -= codesize; + // @OPTIMIZE: is there some way we can accelerate the non-clear path? + if (code == clear) { // clear code + codesize = lzw_cs + 1; + codemask = (1 << codesize) - 1; + avail = clear + 2; + oldcode = -1; + first = 0; + } else if (code == clear + 1) { // end of stream code + stbi__skip(s, len); + while ((len = stbi__get8(s)) > 0) + stbi__skip(s,len); + return g->out; + } else if (code <= avail) { + if (first) { + return stbi__errpuc("no clear code", "Corrupt GIF"); + } + + if (oldcode >= 0) { + p = &g->codes[avail++]; + if (avail > 8192) { + return stbi__errpuc("too many codes", "Corrupt GIF"); + } + + p->prefix = (stbi__int16) oldcode; + p->first = g->codes[oldcode].first; + p->suffix = (code == avail) ? p->first : g->codes[code].first; + } else if (code == avail) + return stbi__errpuc("illegal code in raster", "Corrupt GIF"); + + stbi__out_gif_code(g, (stbi__uint16) code); + + if ((avail & codemask) == 0 && avail <= 0x0FFF) { + codesize++; + codemask = (1 << codesize) - 1; + } + + oldcode = code; + } else { + return stbi__errpuc("illegal code in raster", "Corrupt GIF"); + } + } + } +} + +// this function is designed to support animated gifs, although stb_image doesn't support it +// two back is the image from two frames ago, used for a very specific disposal format +static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back) +{ + int dispose; + int first_frame; + int pi; + int pcount; + STBI_NOTUSED(req_comp); + + // on first frame, any non-written pixels get the background colour (non-transparent) + first_frame = 0; + if (g->out == 0) { + if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header + if (!stbi__mad3sizes_valid(4, g->w, g->h, 0)) + return stbi__errpuc("too large", "GIF image is too large"); + pcount = g->w * g->h; + g->out = (stbi_uc *) stbi__malloc(4 * pcount); + g->background = (stbi_uc *) stbi__malloc(4 * pcount); + g->history = (stbi_uc *) stbi__malloc(pcount); + if (!g->out || !g->background || !g->history) + return stbi__errpuc("outofmem", "Out of memory"); + + // image is treated as "transparent" at the start - ie, nothing overwrites the current background; + // background colour is only used for pixels that are not rendered first frame, after that "background" + // color refers to the color that was there the previous frame. + memset(g->out, 0x00, 4 * pcount); + memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent) + memset(g->history, 0x00, pcount); // pixels that were affected previous frame + first_frame = 1; + } else { + // second frame - how do we dispoase of the previous one? + dispose = (g->eflags & 0x1C) >> 2; + pcount = g->w * g->h; + + if ((dispose == 3) && (two_back == 0)) { + dispose = 2; // if I don't have an image to revert back to, default to the old background + } + + if (dispose == 3) { // use previous graphic + for (pi = 0; pi < pcount; ++pi) { + if (g->history[pi]) { + memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 ); + } + } + } else if (dispose == 2) { + // restore what was changed last frame to background before that frame; + for (pi = 0; pi < pcount; ++pi) { + if (g->history[pi]) { + memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 ); + } + } + } else { + // This is a non-disposal case eithe way, so just + // leave the pixels as is, and they will become the new background + // 1: do not dispose + // 0: not specified. + } + + // background is what out is after the undoing of the previou frame; + memcpy( g->background, g->out, 4 * g->w * g->h ); + } + + // clear my history; + memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame + + for (;;) { + int tag = stbi__get8(s); + switch (tag) { + case 0x2C: /* Image Descriptor */ + { + stbi__int32 x, y, w, h; + stbi_uc *o; + + x = stbi__get16le(s); + y = stbi__get16le(s); + w = stbi__get16le(s); + h = stbi__get16le(s); + if (((x + w) > (g->w)) || ((y + h) > (g->h))) + return stbi__errpuc("bad Image Descriptor", "Corrupt GIF"); + + g->line_size = g->w * 4; + g->start_x = x * 4; + g->start_y = y * g->line_size; + g->max_x = g->start_x + w * 4; + g->max_y = g->start_y + h * g->line_size; + g->cur_x = g->start_x; + g->cur_y = g->start_y; + + // if the width of the specified rectangle is 0, that means + // we may not see *any* pixels or the image is malformed; + // to make sure this is caught, move the current y down to + // max_y (which is what out_gif_code checks). + if (w == 0) + g->cur_y = g->max_y; + + g->lflags = stbi__get8(s); + + if (g->lflags & 0x40) { + g->step = 8 * g->line_size; // first interlaced spacing + g->parse = 3; + } else { + g->step = g->line_size; + g->parse = 0; + } + + if (g->lflags & 0x80) { + stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1); + g->color_table = (stbi_uc *) g->lpal; + } else if (g->flags & 0x80) { + g->color_table = (stbi_uc *) g->pal; + } else + return stbi__errpuc("missing color table", "Corrupt GIF"); + + o = stbi__process_gif_raster(s, g); + if (!o) return NULL; + + // if this was the first frame, + pcount = g->w * g->h; + if (first_frame && (g->bgindex > 0)) { + // if first frame, any pixel not drawn to gets the background color + for (pi = 0; pi < pcount; ++pi) { + if (g->history[pi] == 0) { + g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be; + memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 ); + } + } + } + + return o; + } + + case 0x21: // Comment Extension. + { + int len; + int ext = stbi__get8(s); + if (ext == 0xF9) { // Graphic Control Extension. + len = stbi__get8(s); + if (len == 4) { + g->eflags = stbi__get8(s); + g->delay = 10 * stbi__get16le(s); // delay - 1/100th of a second, saving as 1/1000ths. + + // unset old transparent + if (g->transparent >= 0) { + g->pal[g->transparent][3] = 255; + } + if (g->eflags & 0x01) { + g->transparent = stbi__get8(s); + if (g->transparent >= 0) { + g->pal[g->transparent][3] = 0; + } + } else { + // don't need transparent + stbi__skip(s, 1); + g->transparent = -1; + } + } else { + stbi__skip(s, len); + break; + } + } + while ((len = stbi__get8(s)) != 0) { + stbi__skip(s, len); + } + break; + } + + case 0x3B: // gif stream termination code + return (stbi_uc *) s; // using '1' causes warning on some compilers + + default: + return stbi__errpuc("unknown code", "Corrupt GIF"); + } + } +} + +static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp) +{ + if (stbi__gif_test(s)) { + int layers = 0; + stbi_uc *u = 0; + stbi_uc *out = 0; + stbi_uc *two_back = 0; + stbi__gif g; + int stride; + memset(&g, 0, sizeof(g)); + if (delays) { + *delays = 0; + } + + do { + u = stbi__gif_load_next(s, &g, comp, req_comp, two_back); + if (u == (stbi_uc *) s) u = 0; // end of animated gif marker + + if (u) { + *x = g.w; + *y = g.h; + ++layers; + stride = g.w * g.h * 4; + + if (out) { + void *tmp = (stbi_uc*) STBI_REALLOC( out, layers * stride ); + if (NULL == tmp) { + STBI_FREE(g.out); + STBI_FREE(g.history); + STBI_FREE(g.background); + return stbi__errpuc("outofmem", "Out of memory"); + } + else + out = (stbi_uc*) tmp; + if (delays) { + *delays = (int*) STBI_REALLOC( *delays, sizeof(int) * layers ); + } + } else { + out = (stbi_uc*)stbi__malloc( layers * stride ); + if (delays) { + *delays = (int*) stbi__malloc( layers * sizeof(int) ); + } + } + memcpy( out + ((layers - 1) * stride), u, stride ); + if (layers >= 2) { + two_back = out - 2 * stride; + } + + if (delays) { + (*delays)[layers - 1U] = g.delay; + } + } + } while (u != 0); + + // free temp buffer; + STBI_FREE(g.out); + STBI_FREE(g.history); + STBI_FREE(g.background); + + // do the final conversion after loading everything; + if (req_comp && req_comp != 4) + out = stbi__convert_format(out, 4, req_comp, layers * g.w, g.h); + + *z = layers; + return out; + } else { + return stbi__errpuc("not GIF", "Image was not as a gif type."); + } +} + +static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + stbi_uc *u = 0; + stbi__gif g; + memset(&g, 0, sizeof(g)); + STBI_NOTUSED(ri); + + u = stbi__gif_load_next(s, &g, comp, req_comp, 0); + if (u == (stbi_uc *) s) u = 0; // end of animated gif marker + if (u) { + *x = g.w; + *y = g.h; + + // moved conversion to after successful load so that the same + // can be done for multiple frames. + if (req_comp && req_comp != 4) + u = stbi__convert_format(u, 4, req_comp, g.w, g.h); + } else if (g.out) { + // if there was an error and we allocated an image buffer, free it! + STBI_FREE(g.out); + } + + // free buffers needed for multiple frame loading; + STBI_FREE(g.history); + STBI_FREE(g.background); + + return u; +} + +static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp) +{ + return stbi__gif_info_raw(s,x,y,comp); +} +#endif + +// ************************************************************************************************* +// Radiance RGBE HDR loader +// originally by Nicolas Schulz +#ifndef STBI_NO_HDR +static int stbi__hdr_test_core(stbi__context *s, const char *signature) +{ + int i; + for (i=0; signature[i]; ++i) + if (stbi__get8(s) != signature[i]) + return 0; + stbi__rewind(s); + return 1; +} + +static int stbi__hdr_test(stbi__context* s) +{ + int r = stbi__hdr_test_core(s, "#?RADIANCE\n"); + stbi__rewind(s); + if(!r) { + r = stbi__hdr_test_core(s, "#?RGBE\n"); + stbi__rewind(s); + } + return r; +} + +#define STBI__HDR_BUFLEN 1024 +static char *stbi__hdr_gettoken(stbi__context *z, char *buffer) +{ + int len=0; + char c = '\0'; + + c = (char) stbi__get8(z); + + while (!stbi__at_eof(z) && c != '\n') { + buffer[len++] = c; + if (len == STBI__HDR_BUFLEN-1) { + // flush to end of line + while (!stbi__at_eof(z) && stbi__get8(z) != '\n') + ; + break; + } + c = (char) stbi__get8(z); + } + + buffer[len] = 0; + return buffer; +} + +static void stbi__hdr_convert(float *output, stbi_uc *input, int req_comp) +{ + if ( input[3] != 0 ) { + float f1; + // Exponent + f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8)); + if (req_comp <= 2) + output[0] = (input[0] + input[1] + input[2]) * f1 / 3; + else { + output[0] = input[0] * f1; + output[1] = input[1] * f1; + output[2] = input[2] * f1; + } + if (req_comp == 2) output[1] = 1; + if (req_comp == 4) output[3] = 1; + } else { + switch (req_comp) { + case 4: output[3] = 1; /* fallthrough */ + case 3: output[0] = output[1] = output[2] = 0; + break; + case 2: output[1] = 1; /* fallthrough */ + case 1: output[0] = 0; + break; + } + } +} + +static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + char buffer[STBI__HDR_BUFLEN]; + char *token; + int valid = 0; + int width, height; + stbi_uc *scanline; + float *hdr_data; + int len; + unsigned char count, value; + int i, j, k, c1,c2, z; + const char *headerToken; + STBI_NOTUSED(ri); + + // Check identifier + headerToken = stbi__hdr_gettoken(s,buffer); + if (strcmp(headerToken, "#?RADIANCE") != 0 && strcmp(headerToken, "#?RGBE") != 0) + return stbi__errpf("not HDR", "Corrupt HDR image"); + + // Parse header + for(;;) { + token = stbi__hdr_gettoken(s,buffer); + if (token[0] == 0) break; + if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1; + } + + if (!valid) return stbi__errpf("unsupported format", "Unsupported HDR format"); + + // Parse width and height + // can't use sscanf() if we're not using stdio! + token = stbi__hdr_gettoken(s,buffer); + if (strncmp(token, "-Y ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format"); + token += 3; + height = (int) strtol(token, &token, 10); + while (*token == ' ') ++token; + if (strncmp(token, "+X ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format"); + token += 3; + width = (int) strtol(token, NULL, 10); + + *x = width; + *y = height; + + if (comp) *comp = 3; + if (req_comp == 0) req_comp = 3; + + if (!stbi__mad4sizes_valid(width, height, req_comp, sizeof(float), 0)) + return stbi__errpf("too large", "HDR image is too large"); + + // Read data + hdr_data = (float *) stbi__malloc_mad4(width, height, req_comp, sizeof(float), 0); + if (!hdr_data) + return stbi__errpf("outofmem", "Out of memory"); + + // Load image data + // image data is stored as some number of sca + if ( width < 8 || width >= 32768) { + // Read flat data + for (j=0; j < height; ++j) { + for (i=0; i < width; ++i) { + stbi_uc rgbe[4]; + main_decode_loop: + stbi__getn(s, rgbe, 4); + stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp); + } + } + } else { + // Read RLE-encoded data + scanline = NULL; + + for (j = 0; j < height; ++j) { + c1 = stbi__get8(s); + c2 = stbi__get8(s); + len = stbi__get8(s); + if (c1 != 2 || c2 != 2 || (len & 0x80)) { + // not run-length encoded, so we have to actually use THIS data as a decoded + // pixel (note this can't be a valid pixel--one of RGB must be >= 128) + stbi_uc rgbe[4]; + rgbe[0] = (stbi_uc) c1; + rgbe[1] = (stbi_uc) c2; + rgbe[2] = (stbi_uc) len; + rgbe[3] = (stbi_uc) stbi__get8(s); + stbi__hdr_convert(hdr_data, rgbe, req_comp); + i = 1; + j = 0; + STBI_FREE(scanline); + goto main_decode_loop; // yes, this makes no sense + } + len <<= 8; + len |= stbi__get8(s); + if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); } + if (scanline == NULL) { + scanline = (stbi_uc *) stbi__malloc_mad2(width, 4, 0); + if (!scanline) { + STBI_FREE(hdr_data); + return stbi__errpf("outofmem", "Out of memory"); + } + } + + for (k = 0; k < 4; ++k) { + int nleft; + i = 0; + while ((nleft = width - i) > 0) { + count = stbi__get8(s); + if (count > 128) { + // Run + value = stbi__get8(s); + count -= 128; + if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); } + for (z = 0; z < count; ++z) + scanline[i++ * 4 + k] = value; + } else { + // Dump + if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); } + for (z = 0; z < count; ++z) + scanline[i++ * 4 + k] = stbi__get8(s); + } + } + } + for (i=0; i < width; ++i) + stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp); + } + if (scanline) + STBI_FREE(scanline); + } + + return hdr_data; +} + +static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp) +{ + char buffer[STBI__HDR_BUFLEN]; + char *token; + int valid = 0; + int dummy; + + if (!x) x = &dummy; + if (!y) y = &dummy; + if (!comp) comp = &dummy; + + if (stbi__hdr_test(s) == 0) { + stbi__rewind( s ); + return 0; + } + + for(;;) { + token = stbi__hdr_gettoken(s,buffer); + if (token[0] == 0) break; + if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1; + } + + if (!valid) { + stbi__rewind( s ); + return 0; + } + token = stbi__hdr_gettoken(s,buffer); + if (strncmp(token, "-Y ", 3)) { + stbi__rewind( s ); + return 0; + } + token += 3; + *y = (int) strtol(token, &token, 10); + while (*token == ' ') ++token; + if (strncmp(token, "+X ", 3)) { + stbi__rewind( s ); + return 0; + } + token += 3; + *x = (int) strtol(token, NULL, 10); + *comp = 3; + return 1; +} +#endif // STBI_NO_HDR + +#ifndef STBI_NO_BMP +static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp) +{ + void *p; + stbi__bmp_data info; + + info.all_a = 255; + p = stbi__bmp_parse_header(s, &info); + stbi__rewind( s ); + if (p == NULL) + return 0; + if (x) *x = s->img_x; + if (y) *y = s->img_y; + if (comp) { + if (info.bpp == 24 && info.ma == 0xff000000) + *comp = 3; + else + *comp = info.ma ? 4 : 3; + } + return 1; +} +#endif + +#ifndef STBI_NO_PSD +static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp) +{ + int channelCount, dummy, depth; + if (!x) x = &dummy; + if (!y) y = &dummy; + if (!comp) comp = &dummy; + if (stbi__get32be(s) != 0x38425053) { + stbi__rewind( s ); + return 0; + } + if (stbi__get16be(s) != 1) { + stbi__rewind( s ); + return 0; + } + stbi__skip(s, 6); + channelCount = stbi__get16be(s); + if (channelCount < 0 || channelCount > 16) { + stbi__rewind( s ); + return 0; + } + *y = stbi__get32be(s); + *x = stbi__get32be(s); + depth = stbi__get16be(s); + if (depth != 8 && depth != 16) { + stbi__rewind( s ); + return 0; + } + if (stbi__get16be(s) != 3) { + stbi__rewind( s ); + return 0; + } + *comp = 4; + return 1; +} + +static int stbi__psd_is16(stbi__context *s) +{ + int channelCount, depth; + if (stbi__get32be(s) != 0x38425053) { + stbi__rewind( s ); + return 0; + } + if (stbi__get16be(s) != 1) { + stbi__rewind( s ); + return 0; + } + stbi__skip(s, 6); + channelCount = stbi__get16be(s); + if (channelCount < 0 || channelCount > 16) { + stbi__rewind( s ); + return 0; + } + (void) stbi__get32be(s); + (void) stbi__get32be(s); + depth = stbi__get16be(s); + if (depth != 16) { + stbi__rewind( s ); + return 0; + } + return 1; +} +#endif + +#ifndef STBI_NO_PIC +static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp) +{ + int act_comp=0,num_packets=0,chained,dummy; + stbi__pic_packet packets[10]; + + if (!x) x = &dummy; + if (!y) y = &dummy; + if (!comp) comp = &dummy; + + if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) { + stbi__rewind(s); + return 0; + } + + stbi__skip(s, 88); + + *x = stbi__get16be(s); + *y = stbi__get16be(s); + if (stbi__at_eof(s)) { + stbi__rewind( s); + return 0; + } + if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) { + stbi__rewind( s ); + return 0; + } + + stbi__skip(s, 8); + + do { + stbi__pic_packet *packet; + + if (num_packets==sizeof(packets)/sizeof(packets[0])) + return 0; + + packet = &packets[num_packets++]; + chained = stbi__get8(s); + packet->size = stbi__get8(s); + packet->type = stbi__get8(s); + packet->channel = stbi__get8(s); + act_comp |= packet->channel; + + if (stbi__at_eof(s)) { + stbi__rewind( s ); + return 0; + } + if (packet->size != 8) { + stbi__rewind( s ); + return 0; + } + } while (chained); + + *comp = (act_comp & 0x10 ? 4 : 3); + + return 1; +} +#endif + +// ************************************************************************************************* +// Portable Gray Map and Portable Pixel Map loader +// by Ken Miller +// +// PGM: http://netpbm.sourceforge.net/doc/pgm.html +// PPM: http://netpbm.sourceforge.net/doc/ppm.html +// +// Known limitations: +// Does not support comments in the header section +// Does not support ASCII image data (formats P2 and P3) +// Does not support 16-bit-per-channel + +#ifndef STBI_NO_PNM + +static int stbi__pnm_test(stbi__context *s) +{ + char p, t; + p = (char) stbi__get8(s); + t = (char) stbi__get8(s); + if (p != 'P' || (t != '5' && t != '6')) { + stbi__rewind( s ); + return 0; + } + return 1; +} + +static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + stbi_uc *out; + STBI_NOTUSED(ri); + + if (!stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n)) + return 0; + + *x = s->img_x; + *y = s->img_y; + if (comp) *comp = s->img_n; + + if (!stbi__mad3sizes_valid(s->img_n, s->img_x, s->img_y, 0)) + return stbi__errpuc("too large", "PNM too large"); + + out = (stbi_uc *) stbi__malloc_mad3(s->img_n, s->img_x, s->img_y, 0); + if (!out) return stbi__errpuc("outofmem", "Out of memory"); + stbi__getn(s, out, s->img_n * s->img_x * s->img_y); + + if (req_comp && req_comp != s->img_n) { + out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y); + if (out == NULL) return out; // stbi__convert_format frees input on failure + } + return out; +} + +static int stbi__pnm_isspace(char c) +{ + return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r'; +} + +static void stbi__pnm_skip_whitespace(stbi__context *s, char *c) +{ + for (;;) { + while (!stbi__at_eof(s) && stbi__pnm_isspace(*c)) + *c = (char) stbi__get8(s); + + if (stbi__at_eof(s) || *c != '#') + break; + + while (!stbi__at_eof(s) && *c != '\n' && *c != '\r' ) + *c = (char) stbi__get8(s); + } +} + +static int stbi__pnm_isdigit(char c) +{ + return c >= '0' && c <= '9'; +} + +static int stbi__pnm_getinteger(stbi__context *s, char *c) +{ + int value = 0; + + while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) { + value = value*10 + (*c - '0'); + *c = (char) stbi__get8(s); + } + + return value; +} + +static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp) +{ + int maxv, dummy; + char c, p, t; + + if (!x) x = &dummy; + if (!y) y = &dummy; + if (!comp) comp = &dummy; + + stbi__rewind(s); + + // Get identifier + p = (char) stbi__get8(s); + t = (char) stbi__get8(s); + if (p != 'P' || (t != '5' && t != '6')) { + stbi__rewind(s); + return 0; + } + + *comp = (t == '6') ? 3 : 1; // '5' is 1-component .pgm; '6' is 3-component .ppm + + c = (char) stbi__get8(s); + stbi__pnm_skip_whitespace(s, &c); + + *x = stbi__pnm_getinteger(s, &c); // read width + stbi__pnm_skip_whitespace(s, &c); + + *y = stbi__pnm_getinteger(s, &c); // read height + stbi__pnm_skip_whitespace(s, &c); + + maxv = stbi__pnm_getinteger(s, &c); // read max value + + if (maxv > 255) + return stbi__err("max value > 255", "PPM image not 8-bit"); + else + return 1; +} +#endif + +static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp) +{ + #ifndef STBI_NO_JPEG + if (stbi__jpeg_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_PNG + if (stbi__png_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_GIF + if (stbi__gif_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_BMP + if (stbi__bmp_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_PSD + if (stbi__psd_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_PIC + if (stbi__pic_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_PNM + if (stbi__pnm_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_HDR + if (stbi__hdr_info(s, x, y, comp)) return 1; + #endif + + // test tga last because it's a crappy test! + #ifndef STBI_NO_TGA + if (stbi__tga_info(s, x, y, comp)) + return 1; + #endif + return stbi__err("unknown image type", "Image not of any known type, or corrupt"); +} + +static int stbi__is_16_main(stbi__context *s) +{ + #ifndef STBI_NO_PNG + if (stbi__png_is16(s)) return 1; + #endif + + #ifndef STBI_NO_PSD + if (stbi__psd_is16(s)) return 1; + #endif + + return 0; +} + +#ifndef STBI_NO_STDIO +STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp) +{ + FILE *f = stbi__fopen(filename, "rb"); + int result; + if (!f) return stbi__err("can't fopen", "Unable to open file"); + result = stbi_info_from_file(f, x, y, comp); + fclose(f); + return result; +} + +STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp) +{ + int r; + stbi__context s; + long pos = ftell(f); + stbi__start_file(&s, f); + r = stbi__info_main(&s,x,y,comp); + fseek(f,pos,SEEK_SET); + return r; +} + +STBIDEF int stbi_is_16_bit(char const *filename) +{ + FILE *f = stbi__fopen(filename, "rb"); + int result; + if (!f) return stbi__err("can't fopen", "Unable to open file"); + result = stbi_is_16_bit_from_file(f); + fclose(f); + return result; +} + +STBIDEF int stbi_is_16_bit_from_file(FILE *f) +{ + int r; + stbi__context s; + long pos = ftell(f); + stbi__start_file(&s, f); + r = stbi__is_16_main(&s); + fseek(f,pos,SEEK_SET); + return r; +} +#endif // !STBI_NO_STDIO + +STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__info_main(&s,x,y,comp); +} + +STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user); + return stbi__info_main(&s,x,y,comp); +} + +STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__is_16_main(&s); +} + +STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user); + return stbi__is_16_main(&s); +} + +#endif // STB_IMAGE_IMPLEMENTATION + +/* + revision history: + 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs + 2.19 (2018-02-11) fix warning + 2.18 (2018-01-30) fix warnings + 2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug + 1-bit BMP + *_is_16_bit api + avoid warnings + 2.16 (2017-07-23) all functions have 16-bit variants; + STBI_NO_STDIO works again; + compilation fixes; + fix rounding in unpremultiply; + optimize vertical flip; + disable raw_len validation; + documentation fixes + 2.15 (2017-03-18) fix png-1,2,4 bug; now all Imagenet JPGs decode; + warning fixes; disable run-time SSE detection on gcc; + uniform handling of optional "return" values; + thread-safe initialization of zlib tables + 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs + 2.13 (2016-11-29) add 16-bit API, only supported for PNG right now + 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes + 2.11 (2016-04-02) allocate large structures on the stack + remove white matting for transparent PSD + fix reported channel count for PNG & BMP + re-enable SSE2 in non-gcc 64-bit + support RGB-formatted JPEG + read 16-bit PNGs (only as 8-bit) + 2.10 (2016-01-22) avoid warning introduced in 2.09 by STBI_REALLOC_SIZED + 2.09 (2016-01-16) allow comments in PNM files + 16-bit-per-pixel TGA (not bit-per-component) + info() for TGA could break due to .hdr handling + info() for BMP to shares code instead of sloppy parse + can use STBI_REALLOC_SIZED if allocator doesn't support realloc + code cleanup + 2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA + 2.07 (2015-09-13) fix compiler warnings + partial animated GIF support + limited 16-bpc PSD support + #ifdef unused functions + bug with < 92 byte PIC,PNM,HDR,TGA + 2.06 (2015-04-19) fix bug where PSD returns wrong '*comp' value + 2.05 (2015-04-19) fix bug in progressive JPEG handling, fix warning + 2.04 (2015-04-15) try to re-enable SIMD on MinGW 64-bit + 2.03 (2015-04-12) extra corruption checking (mmozeiko) + stbi_set_flip_vertically_on_load (nguillemot) + fix NEON support; fix mingw support + 2.02 (2015-01-19) fix incorrect assert, fix warning + 2.01 (2015-01-17) fix various warnings; suppress SIMD on gcc 32-bit without -msse2 + 2.00b (2014-12-25) fix STBI_MALLOC in progressive JPEG + 2.00 (2014-12-25) optimize JPG, including x86 SSE2 & NEON SIMD (ryg) + progressive JPEG (stb) + PGM/PPM support (Ken Miller) + STBI_MALLOC,STBI_REALLOC,STBI_FREE + GIF bugfix -- seemingly never worked + STBI_NO_*, STBI_ONLY_* + 1.48 (2014-12-14) fix incorrectly-named assert() + 1.47 (2014-12-14) 1/2/4-bit PNG support, both direct and paletted (Omar Cornut & stb) + optimize PNG (ryg) + fix bug in interlaced PNG with user-specified channel count (stb) + 1.46 (2014-08-26) + fix broken tRNS chunk (colorkey-style transparency) in non-paletted PNG + 1.45 (2014-08-16) + fix MSVC-ARM internal compiler error by wrapping malloc + 1.44 (2014-08-07) + various warning fixes from Ronny Chevalier + 1.43 (2014-07-15) + fix MSVC-only compiler problem in code changed in 1.42 + 1.42 (2014-07-09) + don't define _CRT_SECURE_NO_WARNINGS (affects user code) + fixes to stbi__cleanup_jpeg path + added STBI_ASSERT to avoid requiring assert.h + 1.41 (2014-06-25) + fix search&replace from 1.36 that messed up comments/error messages + 1.40 (2014-06-22) + fix gcc struct-initialization warning + 1.39 (2014-06-15) + fix to TGA optimization when req_comp != number of components in TGA; + fix to GIF loading because BMP wasn't rewinding (whoops, no GIFs in my test suite) + add support for BMP version 5 (more ignored fields) + 1.38 (2014-06-06) + suppress MSVC warnings on integer casts truncating values + fix accidental rename of 'skip' field of I/O + 1.37 (2014-06-04) + remove duplicate typedef + 1.36 (2014-06-03) + convert to header file single-file library + if de-iphone isn't set, load iphone images color-swapped instead of returning NULL + 1.35 (2014-05-27) + various warnings + fix broken STBI_SIMD path + fix bug where stbi_load_from_file no longer left file pointer in correct place + fix broken non-easy path for 32-bit BMP (possibly never used) + TGA optimization by Arseny Kapoulkine + 1.34 (unknown) + use STBI_NOTUSED in stbi__resample_row_generic(), fix one more leak in tga failure case + 1.33 (2011-07-14) + make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements + 1.32 (2011-07-13) + support for "info" function for all supported filetypes (SpartanJ) + 1.31 (2011-06-20) + a few more leak fixes, bug in PNG handling (SpartanJ) + 1.30 (2011-06-11) + added ability to load files via callbacks to accomidate custom input streams (Ben Wenger) + removed deprecated format-specific test/load functions + removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway + error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha) + fix inefficiency in decoding 32-bit BMP (David Woo) + 1.29 (2010-08-16) + various warning fixes from Aurelien Pocheville + 1.28 (2010-08-01) + fix bug in GIF palette transparency (SpartanJ) + 1.27 (2010-08-01) + cast-to-stbi_uc to fix warnings + 1.26 (2010-07-24) + fix bug in file buffering for PNG reported by SpartanJ + 1.25 (2010-07-17) + refix trans_data warning (Won Chun) + 1.24 (2010-07-12) + perf improvements reading from files on platforms with lock-heavy fgetc() + minor perf improvements for jpeg + deprecated type-specific functions so we'll get feedback if they're needed + attempt to fix trans_data warning (Won Chun) + 1.23 fixed bug in iPhone support + 1.22 (2010-07-10) + removed image *writing* support + stbi_info support from Jetro Lauha + GIF support from Jean-Marc Lienher + iPhone PNG-extensions from James Brown + warning-fixes from Nicolas Schulz and Janez Zemva (i.stbi__err. Janez (U+017D)emva) + 1.21 fix use of 'stbi_uc' in header (reported by jon blow) + 1.20 added support for Softimage PIC, by Tom Seddon + 1.19 bug in interlaced PNG corruption check (found by ryg) + 1.18 (2008-08-02) + fix a threading bug (local mutable static) + 1.17 support interlaced PNG + 1.16 major bugfix - stbi__convert_format converted one too many pixels + 1.15 initialize some fields for thread safety + 1.14 fix threadsafe conversion bug + header-file-only version (#define STBI_HEADER_FILE_ONLY before including) + 1.13 threadsafe + 1.12 const qualifiers in the API + 1.11 Support installable IDCT, colorspace conversion routines + 1.10 Fixes for 64-bit (don't use "unsigned long") + optimized upsampling by Fabian "ryg" Giesen + 1.09 Fix format-conversion for PSD code (bad global variables!) + 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz + 1.07 attempt to fix C++ warning/errors again + 1.06 attempt to fix C++ warning/errors again + 1.05 fix TGA loading to return correct *comp and use good luminance calc + 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free + 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR + 1.02 support for (subset of) HDR files, float interface for preferred access to them + 1.01 fix bug: possible bug in handling right-side up bmps... not sure + fix bug: the stbi__bmp_load() and stbi__tga_load() functions didn't work at all + 1.00 interface to zlib that skips zlib header + 0.99 correct handling of alpha in palette + 0.98 TGA loader by lonesock; dynamically add loaders (untested) + 0.97 jpeg errors on too large a file; also catch another malloc failure + 0.96 fix detection of invalid v value - particleman@mollyrocket forum + 0.95 during header scan, seek to markers in case of padding + 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same + 0.93 handle jpegtran output; verbose errors + 0.92 read 4,8,16,24,32-bit BMP files of several formats + 0.91 output 24-bit Windows 3.0 BMP files + 0.90 fix a few more warnings; bump version number to approach 1.0 + 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd + 0.60 fix compiling as c++ + 0.59 fix warnings: merge Dave Moore's -Wall fixes + 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian + 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available + 0.56 fix bug: zlib uncompressed mode len vs. nlen + 0.55 fix bug: restart_interval not initialized to 0 + 0.54 allow NULL for 'int *comp' + 0.53 fix bug in png 3->4; speedup png decoding + 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments + 0.51 obey req_comp requests, 1-component jpegs return as 1-component, + on 'test' only check type, not whether we support this variant + 0.50 (2006-11-19) + first released version +*/ + + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/