X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=implode.cpp;fp=implode.cpp;h=f845c1e09c6d3746ae33a0373fc89f27835d39dd;hb=868cf77b9cf3134a70f90802440ec713b3703e1c;hp=0000000000000000000000000000000000000000;hpb=a19e1741311ea264469e094d6bdb3a9543939f27;p=ardour-manual-diverged diff --git a/implode.cpp b/implode.cpp new file mode 100644 index 0000000..f845c1e --- /dev/null +++ b/implode.cpp @@ -0,0 +1,339 @@ +// +// Small program to 'implode' the master document automagically from separate +// files in the include/ directory. +// +// by James Hammons +// (C) 2017 Underground Software +// + +#include +#include +#include +#include // For mkdir() +#include + + +// Global variable (OMG YOU EVIL BASTARD!!) +char buffer[1024000], keyword[1024], token[1024]; +char title[1024], shortTitle[1024], inclFile[1024], style[1024]; +int level = 0, lastLevel = -1; +int lineCount = 0, startLine, sectionLineCount, fileCount = 0; +int part = 0, chapter = 0, subchapter = 0; +bool first = true; +bool nomove = false; +FILE * newFile = NULL; +char level1File[1024], level2File[1024], level3File[1024], temp[1024]; +char partFN[1024], chapterFN[1024], scLink[4096]; + + +void MakeFilename(char * fn) +{ + int l = strlen(fn); + + for(int i=0; i= 'A' && fn[i] <= 'Z') + fn[i] |= 0x20; + else if (fn[i] >= 'a' && fn[i] <= 'z') + ; + else if (fn[i] >= '0' && fn[i] <= '9') + ; + else + fn[i] = '_'; + } +} + + +long SkipHeader(FILE * file) +{ + long length = 0; + + while (true) + { + // If we hit the EOF before finishing, something went horribly wrong + if (feof(file)) + break; + + fgets(buffer, 1023999, file); + length += strlen(buffer) + 1; + + // If we're seeing the end of header sentinel, return; we're done. + if (strncmp(buffer, "---", 3) == 0) + break; + } + + while (true) + { + // If we hit the EOF before finishing, something went horribly wrong + if (feof(file)) + break; + + fgets(buffer, 1023999, file); + length += strlen(buffer) + 1; + + // If we're seeing the end of header sentinel, return; we're done. + if (strncmp(buffer, "---", 3) == 0) + break; + } + + return length; +} + + +void CopyFileToStream(char * from, FILE * toFile) +{ + FILE * fromFile = fopen(from, "r"); + + if (fromFile == NULL) + { + printf("\n\nCould not open file '%s' for reading! Aborting!!\n", from); + exit(1); + } + + fseek(fromFile, 0, SEEK_END); + long length = ftell(fromFile); + rewind(fromFile); + long skipped = SkipHeader(fromFile); + skipped = ftell(fromFile); + + for(long i=skipped; i", 17) == 0) + { + printf("Master file has already been imploded.\n"); + return 0; + } + + // First, rename the master document + rename("master-doc.txt", "master-doc.bak"); + + // Then open the .bak file + FILE * master = fopen("master-doc.bak", "r"); + + if (master == NULL) + { + printf("Could not open master doc (master-doc.txt) file!\n"); + return -1; + } + + // Create new master document + FILE * exp = fopen("master-doc.txt", "w"); + + if (exp == NULL) + { + printf("Could not open 'master-doc.txt' file!\n"); + fclose(master); + return -1; + } + + fprintf(exp, "\n"); + + while (!feof(master)) + { + fgets(buffer, 1023999, master); + lineCount++; + + // Look for start of file marker + if (strncmp(buffer, "---", 3) == 0) + { + // Reset the "short" title, include file & style + shortTitle[0] = 0; + inclFile[0] = 0; + style[0] = 0; + nomove = false; + + if (!ParseHeader(master)) + { + printf("Something went horribly wrong with the header parsing! Aborting!\n"); + break; + } + + // Close any previously opened files... + if (newFile != NULL) + { + fclose(newFile); + newFile = NULL; + } + + // We finished parsing our keyword block, now do something about + // it... :-P + if (level == 0) + { + // Parts don't have any content... + + // Set up the "part" level of TOC link + sprintf(partFN, "%s", temp); + + // Set up content for the imploded part level + fprintf(exp, "\n---\n"); + fprintf(exp, "title: %s\n", title); + + if (strlen(shortTitle) > 0) + fprintf(exp, "menu_title: %s\n", shortTitle); + + fprintf(exp, "part: part\n"); + fprintf(exp, "---\n\n\n"); + } + else if (level == 1) + { + // Make a filename from the title (not short title!) + strcpy(chapterFN, title); + MakeFilename(chapterFN); + + // Set up content for the imploded chapter level + fprintf(exp, "---\n"); + fprintf(exp, "title: %s\n", title); + + if (strlen(shortTitle) > 0) + fprintf(exp, "menu_title: %s\n", shortTitle); + + // If it was already an include file, mark it to stay in include/ + if (nomove) + { + fprintf(exp, "include: %s\n", inclFile); + fprintf(exp, "exclude: yes\n"); + } + + fprintf(exp, "part: chapter\n"); + fprintf(exp, "---\n"); + + // Make the file expected at this level (but only if not + // included)... + if (nomove == false) + { + fileCount++; + sprintf(temp, "include/%s.html", chapterFN); + CopyFileToStream(temp, exp); + remove(temp); + } + else + fprintf(exp, "\n"); + } + else if (level == 2) + { + strcpy(scLink, title); + MakeFilename(scLink); + + // Set up content for the exploded subchapter level + fprintf(exp, "---\n"); + fprintf(exp, "title: %s\n", title); + + if (strlen(shortTitle) > 0) + fprintf(exp, "menu_title: %s\n", shortTitle); + + // If it was already an include file, mark it to stay in include/ + if (nomove) + { + fprintf(exp, "include: %s\n", inclFile); + fprintf(exp, "exclude: yes\n"); + } + + fprintf(exp, "part: subchapter\n"); + fprintf(exp, "---\n"); + + if (nomove == false) + { + fileCount++; + sprintf(temp, "include/%s.html", scLink); + CopyFileToStream(temp, exp); + remove(temp); + } + else + fprintf(exp, "\n"); + } + } + + // Kill the buffer to prevent false positives... + buffer[0] = 0; + } + + printf("\nProcessed %i lines.\n", lineCount); + printf("Imploded master document from %i files.\n", fileCount); + + fclose(master); + fclose(exp); + + if (newFile) + fclose(newFile); + + // Finally, remove the .bak file: + remove("master-doc.bak"); + + return 0; +} +