浏览代码

libgis: G_set_percent_routine() and G_unset_percent_routine() added

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@35951 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 16 年之前
父节点
当前提交
56b732bcc5
共有 2 个文件被更改,包括 49 次插入18 次删除
  1. 2 0
      include/gisdefs.h
  2. 47 18
      lib/gis/percent.c

+ 2 - 0
include/gisdefs.h

@@ -870,6 +870,8 @@ int G_stat(const char *, struct stat *);
 /* percent.c */
 void G_percent(long, long, int);
 void G_percent_reset(void);
+void G_set_percent_routine(int (*) (int));
+void G_unset_percent_routine(void);
 
 /* plot.c */
 void G_setup_plot(double, double, double, double, int (*)(int, int),

+ 47 - 18
lib/gis/percent.c

@@ -1,17 +1,15 @@
 
 /**
- * \file percent.c
+ * \file gis/percent.c
  *
  * \brief GIS Library - percentage progress functions.
  *
- * (C) 2001-2008 by the GRASS Development Team
+ * (C) 2001-2009 by the GRASS Development Team
  *
  * This program is free software under the GNU General Public License
  * (>=v2). Read the file COPYING that comes with GRASS for details.
  *
  * \author GRASS GIS Development Team
- *
- * \date 1999-2008
  */
 
 #include <stdio.h>
@@ -23,6 +21,7 @@ static struct state {
 } state = {-1, 1};
 
 static struct state *st = &state;
+static int (*ext_percent) (int);
 
 /**
  * \brief Print percent complete messages.
@@ -79,29 +78,37 @@ void G_percent(long n, long d, int s)
     if (n <= 0 || n >= d || x > st->prev + s) {
 	st->prev = x;
 
-	if (format == G_INFO_FORMAT_STANDARD) {
-	    fprintf(stderr, "%4d%%\b\b\b\b\b", x);
+	if (ext_percent) {
+	    ext_percent(x);
 	}
 	else {
-	    if (format == G_INFO_FORMAT_PLAIN) {
-		if (x == 100)
-		    fprintf(stderr, "%d\n", x);
-		else
-		    fprintf(stderr, "%d..", x);
+	    if (format == G_INFO_FORMAT_STANDARD) {
+		fprintf(stderr, "%4d%%\b\b\b\b\b", x);
 	    }
-	    else {		/* GUI */
-		if (st->first) {
-		    fprintf(stderr, "\n");
+	    else {
+		if (format == G_INFO_FORMAT_PLAIN) {
+		    if (x == 100)
+			fprintf(stderr, "%d\n", x);
+		    else
+			fprintf(stderr, "%d..", x);
+		}
+		else {		/* GUI */
+		    if (st->first) {
+			fprintf(stderr, "\n");
+		    }
+		    fprintf(stderr, "GRASS_INFO_PERCENT: %d\n", x);
+		    fflush(stderr);
+		    st->first = 0;
 		}
-		fprintf(stderr, "GRASS_INFO_PERCENT: %d\n", x);
-		fflush(stderr);
-		st->first = 0;
 	    }
 	}
     }
 
     if (x >= 100) {
-	if (format == G_INFO_FORMAT_STANDARD) {
+	if (ext_percent) {
+	    ext_percent(100);
+	}
+	else if (format == G_INFO_FORMAT_STANDARD) {
 	    fprintf(stderr, "\n");
 	}
 	st->prev = -1;
@@ -121,3 +128,25 @@ void G_percent_reset(void)
     st->prev = -1;
     st->first = 1;
 }
+
+/**
+ * \brief Establishes percent_routine as the routine that will handle
+ * the printing of percentage progress messages.
+ * 
+ * \param percent_routine routine will be called like this: percent_routine(x)
+ */
+void G_set_percent_routine(int (*percent_routine) (int))
+{
+    ext_percent = percent_routine;
+}
+
+/**
+ * \brief After this call subsequent percentage progress messages will
+ * be handled in the default method.
+ * 
+ * Percentage progress messages are printed directly to stderr.
+ */
+void G_unset_percent_routine(void)
+{
+    ext_percent = NULL;
+}