Procházet zdrojové kódy

PS Driver: Write data to a tempfile before closing (rel78) (#1506)

* PS Driver: Use a temporary file and create an output on closing

* Use tempfp to make it clear that we are writing a tempfile
Huidae Cho před 4 roky
rodič
revize
3a96670f5a
3 změnil soubory, kde provedl 22 přidání a 15 odebrání
  1. 4 1
      lib/psdriver/graph_close.c
  2. 16 13
      lib/psdriver/graph_set.c
  3. 2 1
      lib/psdriver/psdriver.h

+ 4 - 1
lib/psdriver/graph_close.c

@@ -3,6 +3,7 @@
  * termination time.
  */
 
+#include <grass/gis.h>
 #include "psdriver.h"
 
 void PS_Graph_close(void)
@@ -13,5 +14,7 @@ void PS_Graph_close(void)
 	output("%%%%EndTrailer\n");
     }
 
-    fclose(ps.outfp);
+    fclose(ps.tempfp);
+
+    G_rename_file(ps.tempfile, ps.outfile);
 }

+ 16 - 13
lib/psdriver/graph_set.c

@@ -15,6 +15,7 @@
 #include <stdarg.h>
 #include <time.h>
 #include <math.h>
+#include <unistd.h>
 
 #include <grass/gis.h>
 #include <grass/glocale.h>
@@ -24,8 +25,6 @@
 
 struct ps_state ps;
 
-static const char *file_name;
-
 static double width, height;
 static int landscape;
 
@@ -72,7 +71,7 @@ static void write_prolog(void)
 
     output("%%%%LanguageLevel: %d\n", 3);
     output("%%%%Creator: GRASS PS Driver\n");
-    output("%%%%Title: %s\n", file_name);
+    output("%%%%Title: %s\n", ps.outfile);
     output("%%%%For: %s\n", G_whoami());
     output("%%%%Orientation: %s\n", landscape ? "Landscape" : "Portrait");
     output("%%%%BoundingBox: %d %d %d %d\n",
@@ -88,7 +87,7 @@ static void write_prolog(void)
 	if (!fgets(buf, sizeof(buf), prolog_fp))
 	    break;
 
-	fputs(buf, ps.outfp);
+	fputs(buf, ps.tempfp);
     }
     output("%%%%EndProlog\n");
 
@@ -180,8 +179,8 @@ int PS_Graph_set(void)
     if (!p || strlen(p) == 0)
 	p = FILE_NAME;
 
-    file_name = p;
-    p = file_name + strlen(file_name) - 4;
+    ps.outfile = p;
+    p = ps.outfile + strlen(ps.outfile) - 4;
     ps.encapsulated = (G_strcasecmp(p, ".eps") == 0);
 
     p = getenv("GRASS_RENDER_TRUECOLOR");
@@ -201,21 +200,25 @@ int PS_Graph_set(void)
 
     get_paper();
 
-    ps.outfp = fopen(file_name, ps.no_header ? "a" : "w");
+    ps.tempfile = G_tempfile();
+    if (ps.no_header && access(ps.outfile, F_OK) == 0)
+	G_rename_file(ps.outfile, ps.tempfile);
+
+    ps.tempfp = fopen(ps.tempfile, ps.no_header ? "a" : "w");
 
-    if (!ps.outfp)
-	G_fatal_error("Unable to open output file: %s", file_name);
+    if (!ps.tempfp)
+	G_fatal_error("Unable to open output file: %s", ps.outfile);
 
     if (!ps.no_header) {
 	write_prolog();
 	write_setup();
     }
 
-    G_verbose_message(_("ps: collecting to file '%s'"), file_name);
+    G_verbose_message(_("ps: collecting to file '%s'"), ps.outfile);
     G_verbose_message(_("ps: image size %dx%d"),
 		      screen_width, screen_height);
 
-    fflush(ps.outfp);
+    fflush(ps.tempfp);
 
     return 0;
 }
@@ -227,7 +230,7 @@ int PS_Graph_set(void)
 */
 const char *PS_Graph_get_file(void)
 {
-    return file_name;
+    return ps.outfile;
 }
 
 void output(const char *fmt, ...)
@@ -235,6 +238,6 @@ void output(const char *fmt, ...)
     va_list va;
 
     va_start(va, fmt);
-    vfprintf(ps.outfp, fmt, va);
+    vfprintf(ps.tempfp, fmt, va);
     va_end(va);
 }

+ 2 - 1
lib/psdriver/psdriver.h

@@ -10,7 +10,8 @@
 
 struct ps_state
 {
-    FILE *outfp;
+    const char *tempfile, *outfile;
+    FILE *tempfp;
     int true_color;
     int encapsulated;
     int no_header, no_trailer;