[GRASS-SVN] r51602 - grass/trunk/vector/v.net.salesman

svn_grass at osgeo.org svn_grass at osgeo.org
Tue May 8 09:35:09 EDT 2012


Author: mmetz
Date: 2012-05-08 06:35:08 -0700 (Tue, 08 May 2012)
New Revision: 51602

Modified:
   grass/trunk/vector/v.net.salesman/main.c
Log:
v.net.salesman: fix write sequence to stdout

Modified: grass/trunk/vector/v.net.salesman/main.c
===================================================================
--- grass/trunk/vector/v.net.salesman/main.c	2012-05-08 11:33:40 UTC (rev 51601)
+++ grass/trunk/vector/v.net.salesman/main.c	2012-05-08 13:35:08 UTC (rev 51602)
@@ -115,7 +115,8 @@
     struct line_cats *Cats;
     struct line_pnts *Points;
     const char *dstr;
-/*    char buf[2000], buf2[2000]; */
+    const char *seqname;
+    int seq2stdout;
     FILE *fp;
 
     /* Initialize the GIS calls */
@@ -476,16 +477,22 @@
 	G_debug(2, "%d. arc: cat %d", i + 1, cat);
     }
     
+    seq2stdout = 0;
+    seqname = NULL;
     if (seq->answer) {
 	if (strcmp(seq->answer, "-")) {
-	    fp = fopen(seq->answer, "w");
-	    if (!fp)
-		G_fatal_error(_("Unable to open file '%s' for writing"),
-		              seq->answer);
+	    seqname = seq->answer;
 	}
-	else
-	    fp = stdout;
+	else {
+	    seqname = G_tempfile();
+	    seq2stdout = 1;
+	}
 
+	fp = fopen(seqname, "w");
+	if (!fp)
+	    G_fatal_error(_("Unable to open file '%s' for writing"),
+			  seqname);
+
 	fprintf(fp, "sequence;category;cost_to_next\n");
     }
     else
@@ -518,9 +525,6 @@
 
 	G_debug(2, "%d. node: cat %d", k, cat);
     }
-    
-    if (fp && fp != stdout)
-	fclose(fp);
 
     Vect_build(&Out);
 
@@ -530,6 +534,23 @@
     Vect_close(&Map);
     Vect_close(&Out);
 
+    if (fp) {
+	fclose(fp);
+	if (seq2stdout) {
+	    char buf[2000];
+
+	    /* spacer to previous output to stderr */
+	    G_message(" ");
+
+	    fp = fopen(seqname, "r");
+	    while (G_getl2(buf, 2000, fp) != 0)
+		fprintf(stdout, "%s\n", buf);
+
+	    fclose(fp);
+	    remove(seqname);
+	}
+    }
+
     exit(EXIT_SUCCESS);
 }
 



More information about the grass-commit mailing list