gwenhywfar  4.3.3
cprogress.c
Go to the documentation of this file.
1 
2 
3 #ifdef HAVE_CONFIG_H
4 # include <config.h>
5 #endif
6 
7 #include "cprogress_p.h"
8 #include "cgui_l.h"
9 
10 #include <gwenhywfar/inherit.h>
11 #include <gwenhywfar/debug.h>
12 #include <gwenhywfar/misc.h>
13 
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <string.h>
20 
21 
22 
23 GWEN_LIST_FUNCTIONS(GWEN_GUI_CPROGRESS, GWEN_Gui_CProgress)
24 
25 
26 
28  uint32_t id,
29  uint32_t progressFlags,
30  const char *title,
31  const char *text,
32  uint64_t total) {
34 
37  cp->gui=gui;
38  cp->id=id;
39  cp->startTime=time(0);
40  cp->flags=progressFlags;
41  if (title)
42  cp->title=strdup(title);
43  if (text)
44  cp->text=strdup(text);
45  cp->total=total;
46  cp->logBuf=GWEN_Buffer_new(0, 256, 0, 1);
47 
48  if (!(cp->flags & GWEN_GUI_PROGRESS_DELAY)) {
49  fprintf(stderr, "%s: Started.\n", cp->title);
50  cp->shown=1;
51  }
52 
53  return cp;
54 }
55 
56 
57 
59  if (cp) {
61  GWEN_Buffer_free(cp->logBuf);
62  free(cp->text);
63  free(cp->title);
64  GWEN_FREE_OBJECT(cp);
65  }
66 }
67 
68 
69 
71  assert(cp);
72  return cp->gui;
73 }
74 
75 
76 
78  assert(cp);
79  return cp->id;
80 }
81 
82 
83 
85  assert(cp);
86  return cp->title;
87 }
88 
89 
90 
92  assert(cp);
93  return cp->text;
94 }
95 
96 
97 
99  assert(cp);
100  return cp->total;
101 }
102 
103 
104 
106  assert(cp);
107  return cp->current;
108 }
109 
110 
111 
113  assert(cp);
114  assert(cp->logBuf);
115  return GWEN_Buffer_GetStart(cp->logBuf);
116 }
117 
118 
119 
121  assert(cp);
122  return cp->aborted;
123 }
124 
125 
126 
127 
128 
129 
130 int GWEN_Gui_CProgress_Advance(GWEN_GUI_CPROGRESS *cp, uint64_t progress) {
131 #ifndef OS_WIN32
132  int fl;
133 #endif
134 
135  assert(cp);
136  if (!cp->shown) {
137  time_t t1;
138 
139  t1=time(0);
140  if (difftime(t1, cp->startTime)>GWEN_GUI_DELAY_SECS) {
142  fprintf(stderr, "%s: Started.\n", cp->title);
143  cp->shown=1;
144  }
145  }
146 
147  if (progress==GWEN_GUI_PROGRESS_ONE)
148  progress=cp->current+1;
149  if (progress!=GWEN_GUI_PROGRESS_NONE) {
150  if (progress!=cp->current) {
151  if (cp->shown) {
153  if (cp->total==GWEN_GUI_PROGRESS_NONE)
154  fprintf(stderr, "%s: %llu\n", cp->title,
155  (long long unsigned)progress);
156  else
157  fprintf(stderr, "%s: %llu of %llu\n",
158  cp->title,
159  (long long unsigned)progress,
160  (long long unsigned)cp->total);
161  }
162  }
163  cp->current=progress;
164  }
165  }
166  if (cp->aborted)
168 
169 #ifndef OS_WIN32
171  /* check for abort */
172  fl=fcntl(fileno(stdin), F_GETFL);
173  if (fl!=-1) {
174  int chr;
175 
176  /* set stdin to nonblocking */
177  if (fcntl(fileno(stdin), F_SETFL, fl | O_NONBLOCK)) {
178  DBG_INFO(GWEN_LOGDOMAIN, "fcntl(stdin): %s", strerror(errno));
179  return 0;
180  }
181  /* check whether there is a character */
182  chr=getchar();
183  /* set blocking mode to what we found before modification */
184  fcntl(fileno(stdin), F_SETFL, fl);
185  if (chr==GWEN_GUI_CPROGRESS_CHAR_ABORT) {
186  fprintf(stderr, "------> ABORTED BY USER\n");
187  cp->aborted=1;
189  }
190  }
191  }
192 #endif
193 
194  return 0;
195 }
196 
197 
198 
201  const char *text) {
202  assert(cp);
203  assert(text);
204 
206  GWEN_BUFFER *tbuf;
207  const char *t;
208 
209  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
210  GWEN_Gui_GetRawText(cp->gui, text, tbuf);
211  t=GWEN_Buffer_GetStart(tbuf);
212  if (t[strlen(t)-1]!='\n')
213  GWEN_Buffer_AppendByte(tbuf, '\n');
214  fprintf(stderr, "%s", t);
215 
217  GWEN_Buffer_free(tbuf);
218  tbuf=0;
219  if (cp->aborted)
221  }
222  return 0;
223 }
224 
225 
226 
228  assert(cp);
229 
230  if (cp->shown) {
232  fprintf(stderr, "%s: Finished.\n", cp->title);
233  }
234  if (cp->aborted)
236 
237  return 0;
238 }
239 
240 
241 
242