percent.c

Go to the documentation of this file.
00001 #include "gis.h"
00002 /******************************************************************
00003 * G_percent (n, d, s)
00004 *
00005 *  print a counter to stderr
00006 *  prints percentage of n in d, in increments of s
00007 *
00008 *  example:
00009 *
00010 *       for (row = 0; row < nrows; row++)
00011 *       {
00012 *           G_percent (row, nrows, 10);
00013 *                ...
00014 *       }
00015 *       G_percent (row, nrows, 10);
00016 *
00017 *  will print percent complete for row/nrows in multiples of 10  
00018 *****************************************************************/ 
00019 #include <stdio.h>
00020 
00021 static int prev = -1;
00022 static int first = 1;
00023 
00024 
00051 int G_percent (int n,int d,int s)
00052 {
00053     return ( G_percent2 ( n, d, s, stderr ) );
00054 }
00055 
00056 
00084 int G_percent2 (int n,int d,int s, FILE *out)
00085 {
00086     int x, format;
00087 
00088     format = G_info_format ();
00089 
00090     x = (d <= 0 || s <= 0)
00091         ? 100
00092         : 100 * n / d;
00093         
00094 
00095     if (n <= 0 || n >= d || x > prev + s)
00096     {
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         } else { /* GUI */
00104             if ( out != NULL ) {
00105                 if ( first ) {
00106                     fprintf (out,"\n");
00107                 }
00108                 fprintf (out,"GRASS_INFO_PERCENT: %d\n", x);
00109                 fflush ( out );
00110             }
00111             first = 0;
00112         }
00113     }
00114 
00115     if (x >= 100)
00116     {
00117         if ( format == G_INFO_FORMAT_STANDARD ) {
00118             if ( out != NULL ) {
00119                 fprintf (out,"\n");
00120             }
00121         }
00122         prev = -1;
00123         first = 1;
00124     }
00125 
00126     return 0;
00127 }

Generated on Mon Jan 1 19:49:25 2007 for GRASS by  doxygen 1.5.1