]> Shamusworld >> Repos - ardour-manual-diverged/blobdiff - explode.cpp
Sync with master branch.
[ardour-manual-diverged] / explode.cpp
index 7d5cc3246853f4e0e3b3513127f0205a46e665e3..3124819e0ccb0995d4dddd795e0c2a89872bf24a 100644 (file)
@@ -6,6 +6,8 @@
 // (C) 2017 Underground Software
 //
 
+// still need a fix for titles with the same name...
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -21,7 +23,7 @@ int lineCount = 0, startLine, sectionLineCount, fileCount = 0;
 int part = 0, chapter = 0, subchapter = 0;
 bool first = true;
 bool nomove = false;
-FILE * newFile = NULL;
+FILE * newFile = NULL, * master = NULL, * exp = NULL;
 char level1File[1024], level2File[1024], level3File[1024], temp[1024];
 char partFN[1024], chapterFN[1024], scLink[4096];
 
@@ -107,6 +109,24 @@ bool ParseHeader(FILE * file)
 }
 
 
+//
+// Restore the master document back to its pre-run state
+//
+void HandleFailure(void)
+{
+       // Close open files...
+       fclose(master);
+       fclose(exp);
+
+       if (newFile)
+               fclose(newFile);
+
+       // Remove our work, and restore the backup of the master document
+       remove("master-doc.txt");
+       rename("master-doc.bak", "master-doc.txt");
+}
+
+
 int main(int argc, char * argv[])
 {
        // First, check to see if this has been run here already.
@@ -124,7 +144,7 @@ int main(int argc, char * argv[])
        rename("master-doc.txt", "master-doc.bak");
 
        // Then open the .bak file
-       FILE * master = fopen("master-doc.bak", "r");
+       master = fopen("master-doc.bak", "r");
 
        if (master == NULL)
        {
@@ -133,7 +153,7 @@ int main(int argc, char * argv[])
        }
 
        // Create new master document
-       FILE * exp = fopen("master-doc.txt", "w");
+       exp = fopen("master-doc.txt", "w");
 
        if (exp == NULL)
        {
@@ -153,6 +173,7 @@ int main(int argc, char * argv[])
                if (strncmp(buffer, "---", 3) == 0)
                {
                        // Reset the "short" title, include file & sytle
+                       title[0] = 0;
                        shortTitle[0] = 0;
                        inclFile[0] = 0;
                        style[0] = 0;
@@ -161,7 +182,15 @@ int main(int argc, char * argv[])
                        if (!ParseHeader(master))
                        {
                                printf("Something went horribly wrong with the header parsing! Aborting!\n");
-                               break;
+                               HandleFailure();
+                               return -1;
+                       }
+
+                       if (strlen(title) == 0)
+                       {
+                               printf("Header is missing a 'title:' section, aborting!\n");
+                               HandleFailure();
+                               return -1;
                        }
 
                        // Close any previously opened files...