]> Shamusworld >> Repos - ardour-manual-diverged/blobdiff - munge.cpp
Added explode/implode convenience commands.
[ardour-manual-diverged] / munge.cpp
index 61e1a6b7f5e1569c79a2e62d0f01b57dd94cebf0..7cca1336c0fb9ec53eaca32e8393829c71190cec 100644 (file)
--- a/munge.cpp
+++ b/munge.cpp
 #include <sys/types.h>
 
 
+// Global variables (ZOMG YOU HIDEOUS 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;
+int part = 0, chapter = 0, subchapter = 0;
+bool first = true, nomove = false;
+FILE * newFile = NULL;
+char level1File[1024], level2File[1024], level3File[1024], temp[1024], temp2[1024];
+char partFN[1024], chapterFN[1024], scLink[4096];
+
+
 void MakeFilename(char * fn)
 {
        int l = strlen(fn);
@@ -33,6 +45,101 @@ void MakeFilename(char * fn)
 }
 
 
+//
+// There's probably posix function to do this, if you find it, feel free to
+// nuke this crap
+//
+void CopyFile(char * from, char * to)
+{
+       FILE * fromFile = fopen(from, "r");
+
+       if (fromFile == NULL)
+       {
+               printf("\n\nCould not open file '%s' for reading! Aborting!!\n", from);
+               exit(1);
+       }
+
+       FILE * toFile = fopen(to, "w");
+
+       if (toFile == NULL)
+       {
+               printf("\n\nCould not open file '%s' for writing! Aborting!!\n", to);
+               exit(1);
+       }
+
+       fseek(fromFile, 0, SEEK_END);
+       long length = ftell(fromFile);
+       rewind(fromFile);
+
+       for(long i=0; i<length; i++)
+               fputc(fgetc(fromFile), toFile);
+
+       fclose(toFile);
+       fclose(fromFile);
+}
+
+
+bool ParseHeader(FILE * file)
+{
+       while (true)
+       {
+               // If we hit the EOF before finishing, something went horribly wrong
+               if (feof(file))
+                       break;
+
+               fgets(buffer, 1023999, file);
+               lineCount++;
+
+               // If we're seeing the end of header sentinel, return; we're done.
+               if (strncmp(buffer, "---", 3) == 0)
+                       return true;
+
+               sscanf(buffer, "%[^:]: %[^\n]", keyword, token);
+
+               if (strcmp(keyword, "title") == 0)
+               {
+                       strcpy(title, token);
+               }
+               else if (strcmp(keyword, "part") == 0)
+               {
+                       lastLevel = level;
+                       int len = strlen(token);
+
+                       if (len == 4)
+                               level = 0, part++;
+                       else if (len == 7)
+                               level = 1, chapter++, subchapter = 0;
+                       else if (len == 10)
+                               level = 2, subchapter++;
+                       else
+                               level = -1;  // Something went wrong
+               }
+               else if (strcmp(keyword, "include") == 0)
+               {
+                       strcpy(inclFile, token);
+               }
+               else if (strcmp(keyword, "menu_title") == 0)
+               {
+                       strcpy(shortTitle, token);
+               }
+               else if (strcmp(keyword, "style") == 0)
+               {
+                       strcpy(style, token);
+               }
+               else if (strcmp(keyword, "exclude") == 0)
+               {
+                       // Don't care about the token, just the keyword
+                       nomove = true;
+               }
+               else
+                       printf("Unknown keyword '%s' (token: %s)\n", keyword, token);
+       }
+
+       // If we end up here, something went horribly wrong...
+       return false;
+}
+
+
 int main(int argc, char * argv[])
 {
        char roman[21][10] = { "0", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX" };
@@ -58,16 +165,6 @@ int main(int argc, char * argv[])
        fprintf(toc, "title: Ardour Table of Contents\n");
        fprintf(toc, "---\n\n");
 
-       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;
-       int part = 0, chapter = 0, subchapter = 0;
-       bool first = true;
-       FILE * newFile = NULL;
-       char level1File[1024], level2File[1024], level3File[1024], temp[1024];
-       char partFN[1024], chapterFN[1024], scLink[4096];
-
        while (!feof(master))
        {
                fgets(buffer, 1023999, master);
@@ -88,185 +185,155 @@ int main(int argc, char * argv[])
                        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;
+                       }
+
+                       // We hit the end of our keyword block, now do something
+                       // about it... :-P
+                       if (level == 0)
+                               printf("\nPart %s: ", roman[part]);
+                       else if (level == 1)
+                               printf("\n\tCh. %d: ", chapter);
+                       else if (level == 2)
+                               printf("\t\t");
+
+                       printf("%s", title);
+
+                       if (strlen(inclFile) > 0)
+                               printf(" [%s]", inclFile);
+
+                       startLine = lineCount;
+
+                       // Add appropriate suffix chapter content if needed
+                       if ((lastLevel == 1) && (level == 2))
+                       {
+                               if (newFile != NULL)
+                               {
+                                       fprintf(newFile, "\n{%% children %%}\n\n");
+                               }
+                               else
+                               {
+                                       // If it didn't stream, it was a included file, so append to
+                                       // it directly...
+                                       sprintf(temp2, "%s.html", level2File);
+                                       FILE * tfp = fopen(temp2, "a");
+                                       fprintf(tfp, "\n{%% children %%}\n\n");
+                                       fclose(tfp);
+                               }
+                       }
+
+                       // Close any previously opened files...
+                       if (newFile != NULL)
+                       {
+                               fclose(newFile);
+                               newFile = NULL;
+                       }
 
-                       while (true)
+                       if (level == 0)
+                       {
+                               // Parts don't have any content...
+                               strcpy(temp, title);
+                               MakeFilename(temp);
+                               sprintf(level1File, "_manual/%02d_%s", part, temp);
+                               mkdir(level1File, 0777);
+
+                               // Set up the "part" level of TOC link
+                               sprintf(partFN, "%s", temp);
+                               fprintf(toc, "<h2>Part %s: %s</h2>\n", roman[part], title);
+
+                               // Make the file expected at this level...
+                               sprintf(temp, "%s.html", level1File);
+                               FILE * tfp = fopen(temp, "w");
+                               fprintf(tfp, "---\n");
+                               fprintf(tfp, "title: %s\n", title);
+
+                               if (strlen(shortTitle) > 0)
+                                       fprintf(tfp, "menu_title: %s\n", shortTitle);
+
+                               fprintf(tfp, "---\n");
+                               fprintf(tfp, "\n{%% children %%}\n\n");
+                               fclose(tfp);
+                       }
+                       else if (level == 1)
                        {
-                               fgets(buffer, 1023999, master);
-                               lineCount++;
-       
-                               if (strncmp(buffer, "---", 3) != 0)
+                               strcpy(temp, title);
+                               MakeFilename(temp);
+                               sprintf(level2File, "%s/%02d_%s", level1File, chapter, temp);
+                               mkdir(level2File, 0777);
+
+                               // Set up the "chapter" part of the TOC link
+                               sprintf(chapterFN, "%s", temp);
+                               fprintf(toc, "  <p id=chapter>Ch. %d:&nbsp;&nbsp;<a href=\"/%s/%s/\">%s</a></p>\n", chapter, partFN, chapterFN, title);
+
+                               // If there's an include file, copy it...
+                               if (strlen(inclFile) > 0)
                                {
-                                       sscanf(buffer, "%[^:]: %[^\n]", keyword, token);
-
-                                       if (strcmp(keyword, "title") == 0)
-                                       {
-                                               strcpy(title, token);
-                                       }
-                                       else if (strcmp(keyword, "part") == 0)
-                                       {
-                                               lastLevel = level;
-                                               int len = strlen(token);
-
-                                               if (len == 4)
-                                                       level = 0, part++;
-                                               else if (len == 7)
-                                                       level = 1, chapter++, subchapter = 0;
-                                               else if (len == 10)
-                                                       level = 2, subchapter++;
-                                               else
-                                                       level = -1;  // Something went wrong
-                                       }
-                                       else if (strcmp(keyword, "include") == 0)
-                                       {
-                                               strcpy(inclFile, token);
-                                       }
-                                       else if (strcmp(keyword, "menu_title") == 0)
-                                       {
-                                               strcpy(shortTitle, token);
-                                       }
-                                       else if (strcmp(keyword, "style") == 0)
-                                       {
-                                               strcpy(style, token);
-                                       }
-                                       else
-                                               printf("Unknown keyword '%s' (token: %s)\n", keyword, token);
+                                       sprintf(temp, "include/%s", inclFile);
+                                       sprintf(temp2, "%s.html", level2File);
+                                       CopyFile(temp, temp2);
                                }
                                else
                                {
-                                       // We hit the end of our keyword block, now do something
-                                       // about it... :-P
-                                       if (level == 0)
-                                               printf("\nPart %s: ", roman[part]);
-                                       else if (level == 1)
-                                               printf("\n\tCh. %d: ", chapter);
-                                       else if (level == 2)
-                                               printf("\t\t");
-
-                                       printf("%s", title);
-
-                                       if (strlen(inclFile) > 0)
-                                               printf(" [%s]", inclFile);
-
-                                       startLine = lineCount;
-
-                                       // Cleanup our chapter content
-                                       if (lastLevel == 1)
-                                       {
-                                               if (newFile != NULL)
-                                               {
-                                                       if (level == 2)
-                                                               fprintf(newFile, "\n{%% children %%}\n\n");
-
-                                                       fclose(newFile);
-                                                       newFile = NULL;
-                                               }
-                                       }
-
-                                       if (level == 0)
-                                       {
-                                               // Parts & chapters don't have any content...
-                                               strcpy(temp, title);
-                                               MakeFilename(temp);
-                                               sprintf(level1File, "_manual/%02d_%s", part, temp);
-                                               mkdir(level1File, 0777);
-
-                                               // Set up the "part" level of TOC link
-                                               sprintf(partFN, "%s", temp);
-                                               fprintf(toc, "<h2>Part %s: %s</h2>\n", roman[part], title);
-
-                                               // Make the file expected at this level...
-                                               sprintf(temp, "%s.html", level1File);
-                                               FILE * tfp = fopen(temp, "w");
-                                               fprintf(tfp, "---\n");
-                                               fprintf(tfp, "title: %s\n", title);
-
-                                               if (strlen(shortTitle) > 0)
-                                                       fprintf(tfp, "menu_title: %s\n", shortTitle);
-
-                                               fprintf(tfp, "---\n");
-                                               fprintf(tfp, "\n{%% children %%}\n\n");
-                                               fclose(tfp);
-                                       }
-                                       else if (level == 1)
-                                       {
-                                               strcpy(temp, title);
-                                               MakeFilename(temp);
-                                               sprintf(level2File, "%s/%02d_%s", level1File, chapter, temp);
-                                               mkdir(level2File, 0777);
-
-                                               // Set up the "chapter" part of the TOC link
-                                               sprintf(chapterFN, "%s", temp);
-                                               fprintf(toc, "  <p id=chapter>Ch. %d:&nbsp;&nbsp;<a href=\"/%s/%s/\">%s</a></p>\n", chapter, partFN, chapterFN, title);
-
-                                               // Make the file expected at this level...
-                                               sprintf(temp, "%s.html", level2File);
-                                               newFile = fopen(temp, "w");
-                                               fprintf(newFile, "---\n");
-                                               fprintf(newFile, "title: %s\n", title);
-
-                                               if (strlen(shortTitle) > 0)
-                                                       fprintf(newFile, "menu_title: %s\n", shortTitle);
-
-                                               fprintf(newFile, "---\n\n");
-//                                             fprintf(tfp, "\n{%% children %%}\n\n");
-//                                             fclose(tfp);
-                                       }
-                                       else if (level == 2)
-                                       {
-                                               strcpy(temp, title);
-                                               MakeFilename(temp);
-                                               sprintf(level3File, "%s/%02d_%s.html", level2File, subchapter, temp);
-
-                                               // Make the file expected at this level...
-                                               if (newFile != NULL)
-                                                       fclose(newFile);
-
-                                               // Make the link
-                                               sprintf(scLink, "/%s/%s/%s/", partFN, chapterFN, temp);
-                                               fprintf(toc, "    <a id=subchapter href=\"%s\">%s</a><br />\n", scLink, title);
-
-                                               if (strlen(inclFile) > 0)
-                                               {
-                                                       // Copy the include file to the appropriate spot...
-                                                       sprintf(temp, "include/%s", inclFile);
-                                                       FILE * fileToCopy = fopen(temp, "r");
-                                                       newFile = fopen(level3File, "w");
-                                                       fseek(fileToCopy, 0, SEEK_END);
-                                                       long length = ftell(fileToCopy);
-                                                       rewind(fileToCopy);
-
-                                                       for(long i=0; i<length; i++)
-                                                               fputc(fgetc(fileToCopy), newFile);
-
-                                                       fclose(newFile);
-                                                       fclose(fileToCopy);
-                                                       newFile = NULL;
-                                               }
-                                               else
-                                               {
-                                                       newFile = fopen(level3File, "w");
-                                                       fprintf(newFile, "---\n");
-                                                       fprintf(newFile, "title: %s\n", title);
-
-                                                       if (strlen(shortTitle) > 0)
-                                                               fprintf(newFile, "menu_title: %s\n", shortTitle);
-
-                                                       if (strlen(style) > 0)
-                                                               fprintf(newFile, "style: %s\n", style);
-
-                                                       fprintf(newFile, "---\n\n");
-                                               }
-                                       }
-
-                                       break;
+                                       // Make the file expected at this level...
+                                       sprintf(temp, "%s.html", level2File);
+                                       newFile = fopen(temp, "w");
+                                       fprintf(newFile, "---\n");
+                                       fprintf(newFile, "title: %s\n", title);
+
+                                       if (strlen(shortTitle) > 0)
+                                               fprintf(newFile, "menu_title: %s\n", shortTitle);
+
+                                       fprintf(newFile, "---\n\n");
+                               }
+                       }
+                       else if (level == 2)
+                       {
+                               strcpy(temp, title);
+                               MakeFilename(temp);
+                               sprintf(level3File, "%s/%02d_%s.html", level2File, subchapter, temp);
+
+                               // Make the file expected at this level...
+
+                               // Make the link
+                               sprintf(scLink, "/%s/%s/%s/", partFN, chapterFN, temp);
+                               fprintf(toc, "    <a id=subchapter href=\"%s\">%s</a><br />\n", scLink, title);
+
+                               // If there's an include file, copy it...
+                               if (strlen(inclFile) > 0)
+                               {
+                                       sprintf(temp, "include/%s", inclFile);
+                                       CopyFile(temp, level3File);
+                               }
+                               else
+                               {
+                                       // Otherwise, stream it out of the master document
+                                       newFile = fopen(level3File, "w");
+                                       fprintf(newFile, "---\n");
+                                       fprintf(newFile, "title: %s\n", title);
+
+                                       if (strlen(shortTitle) > 0)
+                                               fprintf(newFile, "menu_title: %s\n", shortTitle);
+
+                                       if (strlen(style) > 0)
+                                               fprintf(newFile, "style: %s\n", style);
+
+                                       fprintf(newFile, "---\n\n");
                                }
                        }
                }
                else
                {
                        if (((level == 1) || (level == 2)) && (newFile != NULL))
-                               fprintf(newFile, "%s\n", buffer);
+                               fprintf(newFile, "%s", buffer);
                }
+
+               // Make sure no spurious garbage signals a false positive
+               buffer[0] = 0;
        }
 
        printf("\n\nProcessed %i lines.\n", lineCount);