GRASS Programmer's Manual  6.4.4(2014)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 (format == G_INFO_FORMAT_STANDARD) {
100  if (out != NULL) {
101  fprintf(out, "%4d%%\b\b\b\b\b", x);
102  }
103  }
104  else {
105  if (format == G_INFO_FORMAT_PLAIN) {
106  if (out != NULL) {
107  if (x == 100)
108  fprintf(out, "%d\n", x);
109  else
110  fprintf(out, "%d..", x);
111  }
112  }
113  else { /* GUI */
114  if (out != NULL) {
115  if (first) {
116  fprintf(out, "\n");
117  }
118  fprintf(out, "GRASS_INFO_PERCENT: %d\n", x);
119  fflush(out);
120  }
121  first = 0;
122  }
123  }
124  }
125 
126  if (x >= 100) {
127  if (format == G_INFO_FORMAT_STANDARD) {
128  if (out != NULL) {
129  fprintf(out, "\n");
130  }
131  }
132  prev = -1;
133  first = 1;
134  }
135 
136  return 0;
137 }
138 
139 
147 {
148  prev = -1;
149  first = 1;
150 
151  return 0;
152 }
153 
160 void G_set_percent_routine(int (*percent_routine) (int))
161 {
162  ext_percent = percent_routine;
163 }
164 
172 {
173  ext_percent = NULL;
174 }
#define NULL
Definition: strings.c:26
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:160
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:146
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:171
int first
Definition: form/open.c:25
int G_verbose(void)
Get current verbosity level.
Definition: verbose.c:45