GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
percent.c
Go to the documentation of this file.
1 
15 #include <stdio.h>
16 #include <grass/gis.h>
17 
18 
19 static int prev = -1;
20 static int first = 1;
21 
22 static int (*ext_percent) (int);
23 
63 int G_percent(long n, long d, int s)
64 {
65  return (G_percent2(n, d, s, stderr));
66 }
67 
68 
83 int G_percent2(long n, long d, int s, FILE *out)
84 {
85  int x, format;
86 
87  format = G_info_format();
88 
89  x = (d <= 0 || s <= 0)
90  ? 100 : (int)(100 * n / d);
91 
92  /* be verbose only 1> */
93  if (format == G_INFO_FORMAT_SILENT || G_verbose() < 1)
94  return 0;
95 
96  if (n <= 0 || n >= d || x > prev + s) {
97  prev = x;
98 
99  if (ext_percent) {
100  ext_percent(x);
101  }
102  else {
103  if (format == G_INFO_FORMAT_STANDARD) {
104  if (out != NULL) {
105  fprintf(out, "%4d%%\b\b\b\b\b", x);
106  }
107  }
108  else {
109  if (format == G_INFO_FORMAT_PLAIN) {
110  if (out != NULL) {
111  if (x == 100)
112  fprintf(out, "%d\n", x);
113  else
114  fprintf(out, "%d..", x);
115  }
116  }
117  else { /* GUI */
118  if (out != NULL) {
119  if (first) {
120  fprintf(out, "\n");
121  }
122  fprintf(out, "GRASS_INFO_PERCENT: %d\n", x);
123  fflush(out);
124  }
125  first = 0;
126  }
127  }
128  }
129  }
130  if (x >= 100) {
131  if (ext_percent) {
132  ext_percent(100);
133  }
134  else if (format == G_INFO_FORMAT_STANDARD) {
135  if (out != NULL) {
136  fprintf(out, "\n");
137  }
138  }
139  prev = -1;
140  first = 1;
141  }
142 
143  return 0;
144 }
145 
153 {
154  prev = -1;
155  first = 1;
156 
157  return 0;
158 }
159 
166 void G_set_percent_routine(int (*percent_routine) (int))
167 {
168  ext_percent = percent_routine;
169 }
170 
178 {
179  ext_percent = NULL;
180 }
void G_set_percent_routine(int(*percent_routine)(int))
Establishes percent_routine as the routine that will handle the printing of percentage progress messa...
Definition: percent.c:166
int G_percent2(long n, long d, int s, FILE *out)
Print percent complete messages.
Definition: percent.c:83
int G_percent(long n, long d, int s)
Print percent complete messages.
Definition: percent.c:63
int G_percent_reset(void)
Reset G_percent() to 0%; do not add newline.
Definition: percent.c:152
int G_info_format(void)
Get current message format.
void G_unset_percent_routine(void)
After this call subsequent percentage progress messages will be handled in the default method...
Definition: percent.c:177
int
Definition: g3dcolor.c:48
int first
Definition: form/open.c:25
int G_verbose(void)
Get current verbosity level.
Definition: verbose.c:45
return NULL
Definition: dbfopen.c:1394
int n
Definition: dataquad.c:291