GRASS Programmer's Manual  6.4.2(2012)
percent.c
Go to the documentation of this file.
00001 
00015 #include <stdio.h>
00016 #include <grass/gis.h>
00017 
00018 
00019 static int prev = -1;
00020 static int first = 1;
00021 
00022 static int (*ext_percent) (int);
00023 
00063 int G_percent(long n, long d, int s)
00064 {
00065     return (G_percent2(n, d, s, stderr));
00066 }
00067 
00068 
00083 int G_percent2(long n, long d, int s, FILE *out)
00084 {
00085     int x, format;
00086 
00087     format = G_info_format();
00088 
00089     x = (d <= 0 || s <= 0)
00090         ? 100 : (int)(100 * n / d);
00091 
00092     /* be verbose only 1> */
00093     if (format == G_INFO_FORMAT_SILENT || G_verbose() < 1)
00094         return 0;
00095 
00096     if (n <= 0 || n >= d || x > prev + s) {
00097         prev = x;
00098 
00099         if (format == G_INFO_FORMAT_STANDARD) {
00100             if (out != NULL) {
00101                 fprintf(out, "%4d%%\b\b\b\b\b", x);
00102             }
00103         }
00104         else {
00105             if (format == G_INFO_FORMAT_PLAIN) {
00106                 if (out != NULL) {
00107                     if (x == 100)
00108                         fprintf(out, "%d\n", x);
00109                     else
00110                         fprintf(out, "%d..", x);
00111                 }
00112             }
00113             else {              /* GUI */
00114                 if (out != NULL) {
00115                     if (first) {
00116                         fprintf(out, "\n");
00117                     }
00118                     fprintf(out, "GRASS_INFO_PERCENT: %d\n", x);
00119                     fflush(out);
00120                 }
00121                 first = 0;
00122             }
00123         }
00124     }
00125 
00126     if (x >= 100) {
00127         if (format == G_INFO_FORMAT_STANDARD) {
00128             if (out != NULL) {
00129                 fprintf(out, "\n");
00130             }
00131         }
00132         prev = -1;
00133         first = 1;
00134     }
00135 
00136     return 0;
00137 }
00138 
00139 
00146 int G_percent_reset(void)
00147 {
00148     prev = -1;
00149     first = 1;
00150 
00151     return 0;
00152 }
00153 
00160 void G_set_percent_routine(int (*percent_routine) (int))
00161 {
00162     ext_percent = percent_routine;
00163 }
00164 
00171 void G_unset_percent_routine(void)
00172 {
00173     ext_percent = NULL;
00174 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines