gwenhywfar  4.6.0beta
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  cp->total=i;
108 }
109 
110 
111 
113  assert(cp);
114  return cp->current;
115 }
116 
117 
118 
120  assert(cp);
121  assert(cp->logBuf);
122  return GWEN_Buffer_GetStart(cp->logBuf);
123 }
124 
125 
126 
128  assert(cp);
129  return cp->aborted;
130 }
131 
132 
133 
134 
135 
136 
137 int GWEN_Gui_CProgress_Advance(GWEN_GUI_CPROGRESS *cp, uint64_t progress) {
138 #ifndef OS_WIN32
139  int fl;
140 #endif
141 
142  assert(cp);
143  if (!cp->shown) {
144  time_t t1;
145 
146  t1=time(0);
147  if (difftime(t1, cp->startTime)>GWEN_GUI_DELAY_SECS) {
149  fprintf(stderr, "%s: Started.\n", cp->title);
150  cp->shown=1;
151  }
152  }
153 
154  if (progress==GWEN_GUI_PROGRESS_ONE)
155  progress=cp->current+1;
156  if (progress!=GWEN_GUI_PROGRESS_NONE) {
157  if (progress!=cp->current) {
158  if (cp->shown) {
160  if (cp->total==GWEN_GUI_PROGRESS_NONE)
161  fprintf(stderr, "%s: %llu\n", cp->title,
162  (long long unsigned)progress);
163  else
164  fprintf(stderr, "%s: %llu of %llu\n",
165  cp->title,
166  (long long unsigned)progress,
167  (long long unsigned)cp->total);
168  }
169  }
170  cp->current=progress;
171  }
172  }
173  if (cp->aborted)
175 
176 #ifndef OS_WIN32
178  /* check for abort */
179  fl=fcntl(fileno(stdin), F_GETFL);
180  if (fl!=-1) {
181  int chr;
182 
183  /* set stdin to nonblocking */
184  if (fcntl(fileno(stdin), F_SETFL, fl | O_NONBLOCK)) {
185  DBG_INFO(GWEN_LOGDOMAIN, "fcntl(stdin): %s", strerror(errno));
186  return 0;
187  }
188  /* check whether there is a character */
189  chr=getchar();
190  /* set blocking mode to what we found before modification */
191  fcntl(fileno(stdin), F_SETFL, fl);
192  if (chr==GWEN_GUI_CPROGRESS_CHAR_ABORT) {
193  fprintf(stderr, "------> ABORTED BY USER\n");
194  cp->aborted=1;
196  }
197  }
198  }
199 #endif
200 
201  return 0;
202 }
203 
204 
205 
208  const char *text) {
209  assert(cp);
210  assert(text);
211 
213  GWEN_BUFFER *tbuf;
214  const char *t;
215 
216  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
217  GWEN_Gui_GetRawText(cp->gui, text, tbuf);
218  t=GWEN_Buffer_GetStart(tbuf);
219  if (t[strlen(t)-1]!='\n')
220  GWEN_Buffer_AppendByte(tbuf, '\n');
221  fprintf(stderr, "%s", t);
222 
224  GWEN_Buffer_free(tbuf);
225  tbuf=0;
226  if (cp->aborted)
228  }
229  return 0;
230 }
231 
232 
233 
235  assert(cp);
236 
237  if (cp->shown) {
239  fprintf(stderr, "%s: Finished.\n", cp->title);
240  }
241  if (cp->aborted)
243 
244  return 0;
245 }
246 
247 
248 
249