GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
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 (ext_percent) {
00100             ext_percent(x);
00101         }
00102         else {
00103             if (format == G_INFO_FORMAT_STANDARD) {
00104                 if (out != NULL) {
00105                     fprintf(out, "%4d%%\b\b\b\b\b", x);
00106                 }
00107             }
00108             else {
00109                 if (format == G_INFO_FORMAT_PLAIN) {
00110                     if (out != NULL) {
00111                         if (x == 100)
00112                             fprintf(out, "%d\n", x);
00113                         else
00114                             fprintf(out, "%d..", x);
00115                     }
00116                 }
00117                 else {          /* GUI */
00118                     if (out != NULL) {
00119                         if (first) {
00120                             fprintf(out, "\n");
00121                         }
00122                         fprintf(out, "GRASS_INFO_PERCENT: %d\n", x);
00123                         fflush(out);
00124                     }
00125                     first = 0;
00126                 }
00127             }
00128         }
00129     }
00130     if (x >= 100) {
00131         if (ext_percent) {
00132             ext_percent(100);
00133         }
00134         else if (format == G_INFO_FORMAT_STANDARD) {
00135             if (out != NULL) {
00136                 fprintf(out, "\n");
00137             }
00138         }
00139         prev = -1;
00140         first = 1;
00141     }
00142 
00143     return 0;
00144 }
00145 
00152 int G_percent_reset(void)
00153 {
00154     prev = -1;
00155     first = 1;
00156 
00157     return 0;
00158 }
00159 
00166 void G_set_percent_routine(int (*percent_routine) (int))
00167 {
00168     ext_percent = percent_routine;
00169 }
00170 
00177 void G_unset_percent_routine(void)
00178 {
00179     ext_percent = NULL;
00180 }