GRASS GIS 7 Programmer's Manual  7.0.5(2016)-r00000
percent.c
Go to the documentation of this file.
1 
15 #include <stdio.h>
16 #include <grass/gis.h>
17 
18 static struct state {
19  int prev;
20  int first;
21 } state = {-1, 1};
22 
23 static struct state *st = &state;
24 static int (*ext_percent) (int);
25 
62 void G_percent(long n, long d, int s)
63 {
64  int x, format;
65 
66  format = G_info_format();
67 
68  x = (d <= 0 || s <= 0)
69  ? 100 : (int)(100 * n / d);
70 
71  /* be verbose only 1> */
72  if (format == G_INFO_FORMAT_SILENT || G_verbose() < 1)
73  return;
74 
75  if (n <= 0 || n >= d || x > st->prev + s) {
76  st->prev = x;
77 
78  if (ext_percent) {
79  ext_percent(x);
80  }
81  else {
82  if (format == G_INFO_FORMAT_STANDARD) {
83  fprintf(stderr, "%4d%%\b\b\b\b\b", x);
84  }
85  else {
86  if (format == G_INFO_FORMAT_PLAIN) {
87  if (x == 100)
88  fprintf(stderr, "%d\n", x);
89  else
90  fprintf(stderr, "%d..", x);
91  }
92  else { /* GUI */
93  if (st->first) {
94  fprintf(stderr, "\n");
95  }
96  fprintf(stderr, "GRASS_INFO_PERCENT: %d\n", x);
97  fflush(stderr);
98  st->first = 0;
99  }
100  }
101  }
102  }
103 
104  if (x >= 100) {
105  if (ext_percent) {
106  ext_percent(100);
107  }
108  else if (format == G_INFO_FORMAT_STANDARD) {
109  fprintf(stderr, "\n");
110  }
111  st->prev = -1;
112  st->first = 1;
113  }
114 }
115 
119 void G_percent_reset(void)
120 {
121  st->prev = -1;
122  st->first = 1;
123 }
124 
160 void G_progress(long n, int s)
161 {
162  int format;
163 
164  format = G_info_format();
165 
166  /* be verbose only 1> */
167  if (format == G_INFO_FORMAT_SILENT || G_verbose() < 1)
168  return;
169 
170  if (n == s && n == 1) {
171  if (format != G_INFO_FORMAT_PLAIN)
172  fprintf(stderr, "\r");
173  else
174  fprintf(stderr, "\n");
175  return;
176  }
177 
178  if (n % s == 0) {
179  if (format == G_INFO_FORMAT_PLAIN)
180  fprintf(stderr, "%ld..", n);
181  else
182  fprintf(stderr, "%10ld\b\b\b\b\b\b\b\b\b\b", n);
183  }
184 }
185 
192 void G_set_percent_routine(int (*percent_routine) (int))
193 {
194  ext_percent = percent_routine;
195 }
196 
204 {
205  ext_percent = NULL;
206 }
int G_info_format(void)
Get current message format.
Definition: gis/error.c:531
#define NULL
Definition: ccmath.h:32
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:192
struct state * st
Definition: parser.c:101
void G_unset_percent_routine(void)
After this call subsequent percentage progress messages will be handled in the default method...
Definition: percent.c:203
void G_percent_reset(void)
Reset G_percent() to 0%; do not add newline.
Definition: percent.c:119
void G_percent(long n, long d, int s)
Print percent complete messages.
Definition: percent.c:62
int G_verbose(void)
Get current verbosity level.
Definition: verbose.c:55
void G_progress(long n, int s)
Print progress info messages.
Definition: percent.c:160
struct state state
Definition: parser.c:100