gwenhywfar  4.99.15beta
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)
33 {
35 
38  cp->gui=gui;
39  cp->id=id;
40  cp->startTime=time(0);
41  cp->flags=progressFlags;
42  if (title)
43  cp->title=strdup(title);
44  if (text)
45  cp->text=strdup(text);
46  cp->total=total;
47  cp->logBuf=GWEN_Buffer_new(0, 256, 0, 1);
48 
49  if (!(cp->flags & GWEN_GUI_PROGRESS_DELAY)) {
50  GWEN_Gui_StdPrintf(gui, stderr, "===== %s =====\n", title);
51  cp->shown=1;
52  }
53 
54  return cp;
55 }
56 
57 
58 
60 {
61  if (cp) {
63  GWEN_Buffer_free(cp->logBuf);
64  free(cp->text);
65  free(cp->title);
66  GWEN_FREE_OBJECT(cp);
67  }
68 }
69 
70 
71 
73 {
74  assert(cp);
75  return cp->gui;
76 }
77 
78 
79 
81 {
82  assert(cp);
83  return cp->id;
84 }
85 
86 
87 
89 {
90  assert(cp);
91  return cp->title;
92 }
93 
94 
95 
97 {
98  assert(cp);
99  return cp->text;
100 }
101 
102 
103 
105 {
106  assert(cp);
107  return cp->total;
108 }
109 
110 
111 
113 {
114  assert(cp);
115  cp->total=i;
116 }
117 
118 
119 
121 {
122  assert(cp);
123  return cp->current;
124 }
125 
126 
127 
129 {
130  assert(cp);
131  assert(cp->logBuf);
132  return GWEN_Buffer_GetStart(cp->logBuf);
133 }
134 
135 
136 
138 {
139  assert(cp);
140  return cp->aborted;
141 }
142 
143 
144 
145 
146 
147 
149 {
150 #ifndef OS_WIN32
151  int fl;
152 #endif
153 
154  assert(cp);
155  if (!cp->shown) {
156  time_t t1;
157 
158  t1=time(0);
159  if (difftime(t1, cp->startTime)>GWEN_GUI_DELAY_SECS) {
161  GWEN_Gui_StdPrintf(cp->gui, stderr, "%s: Started.\n", cp->title);
162  cp->shown=1;
163  }
164  }
165 
166  if (progress==GWEN_GUI_PROGRESS_ONE)
167  progress=cp->current+1;
168  if (progress!=GWEN_GUI_PROGRESS_NONE) {
169  if (progress!=cp->current) {
170  if (cp->shown) {
172  if (cp->total==GWEN_GUI_PROGRESS_NONE)
173  GWEN_Gui_StdPrintf(cp->gui, stderr, "%s: %llu\n", cp->title,
174  (long long unsigned)progress);
175  else
176  GWEN_Gui_StdPrintf(cp->gui, stderr, "%s: %llu of %llu\n",
177  cp->title,
178  (long long unsigned)progress,
179  (long long unsigned)cp->total);
180  }
181  }
182  cp->current=progress;
183  }
184  }
185  if (cp->aborted)
187 
188 #ifndef OS_WIN32
190  /* check for abort */
191  fl=fcntl(fileno(stdin), F_GETFL);
192  if (fl!=-1) {
193  int chr;
194 
195  /* set stdin to nonblocking */
196  if (fcntl(fileno(stdin), F_SETFL, fl | O_NONBLOCK)) {
197  DBG_INFO(GWEN_LOGDOMAIN, "fcntl(stdin): %s", strerror(errno));
198  return 0;
199  }
200  /* check whether there is a character */
201  chr=getchar();
202  /* set blocking mode to what we found before modification */
203  fcntl(fileno(stdin), F_SETFL, fl);
204  if (chr==GWEN_GUI_CPROGRESS_CHAR_ABORT) {
205  GWEN_Gui_StdPrintf(cp->gui, stderr, "------> ABORTED BY USER\n");
206  cp->aborted=1;
208  }
209  }
210  }
211 #endif
212 
213  return 0;
214 }
215 
216 
217 
220  const char *text)
221 {
222  assert(cp);
223  assert(text);
224 
226  GWEN_BUFFER *tbuf;
227  const char *t;
228 
229  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
230  GWEN_Gui_GetRawText(cp->gui, text, tbuf);
231  t=GWEN_Buffer_GetStart(tbuf);
232  if (t[GWEN_Buffer_GetUsedBytes(tbuf)-1]!='\n') {
233  GWEN_Buffer_AppendByte(tbuf, '\n');
234  /* Just in case the buffer has been reallocated */
235  t=GWEN_Buffer_GetStart(tbuf);
236  }
237  GWEN_Gui_StdPrintf(cp->gui, stderr, "%s", t);
238 
239  GWEN_Buffer_AppendString(cp->logBuf, t);
240  GWEN_Buffer_free(tbuf);
241  tbuf=0;
242  if (cp->aborted)
244  }
245  return 0;
246 }
247 
248 
249 
251 {
252  assert(cp);
253 
254  if (cp->shown) {
256  GWEN_Gui_StdPrintf(cp->gui, stderr, "%s: Finished.\n", cp->title);
257  }
258  if (cp->aborted)
260 
261  return 0;
262 }
263 
264 
265 
266 
const char * GWEN_Gui_CProgress_GetText(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:96
int GWEN_Gui_CProgress_GetAborted(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:137
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
uint32_t GWEN_Gui_CProgress_GetId(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:80
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
Definition: buffer.c:282
#define GWEN_GUI_FLAGS_NONINTERACTIVE
Definition: gui.h:986
GWEN_LOGGER_LEVEL
Definition: logger.h:64
uint64_t GWEN_Gui_CProgress_GetCurrent(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:120
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:92
const char * GWEN_Gui_CProgress_GetLogBuf(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:128
#define GWEN_GUI_PROGRESS_DELAY
Definition: gui.h:192
#define GWEN_LOGDOMAIN
Definition: logger.h:35
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:38
int GWEN_Gui_CProgress_End(GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:250
GWEN_GUI * GWEN_Gui_CProgress_GetGui(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:72
#define GWEN_GUI_PROGRESS_ONE
Definition: gui.h:376
#define GWEN_NEW_OBJECT(typ, varname)
Definition: memory.h:86
void GWEN_Gui_GetRawText(const GWEN_GUI *gui, const char *text, GWEN_BUFFER *tbuf)
Definition: gui.c:339
#define GWEN_GUI_PROGRESS_NONE
Definition: gui.h:368
GWEN_GUI_CPROGRESS * GWEN_Gui_CProgress_new(GWEN_GUI *gui, uint32_t id, uint32_t progressFlags, const char *title, const char *text, uint64_t total)
Definition: cprogress.c:27
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
Definition: buffer.c:399
uint64_t GWEN_Gui_CProgress_GetTotal(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:104
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:85
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:41
int GWEN_Gui_CProgress_Advance(GWEN_GUI_CPROGRESS *cp, uint64_t progress)
Definition: cprogress.c:148
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:177
#define GWEN_LIST_INIT(t, element)
Definition: list1.h:465
struct GWEN_GUI GWEN_GUI
Definition: gui.h:176
const char * GWEN_Gui_CProgress_GetTitle(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:88
void GWEN_Gui_CProgress_SetTotal(GWEN_GUI_CPROGRESS *cp, uint64_t i)
Definition: cprogress.c:112
struct GWEN_GUI_CPROGRESS GWEN_GUI_CPROGRESS
Definition: cprogress_l.h:14
#define GWEN_LIST_FUNCTIONS(t, pr)
Definition: list1.h:366
void GWEN_Gui_CProgress_free(GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:59
#define GWEN_ERROR_USER_ABORTED
Definition: error.h:65
int GWEN_Gui_StdPrintf(const GWEN_GUI *gui, FILE *stream, const char *fmt,...)
Definition: gui.c:259
uint32_t GWEN_Gui_GetFlags(const GWEN_GUI *gui)
Definition: gui.c:686
#define GWEN_LIST_FINI(t, element)
Definition: list1.h:474
#define GWEN_UNUSED
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:1062
#define GWEN_GUI_DELAY_SECS
Definition: gui.h:183
int GWEN_Gui_CProgress_Log(GWEN_GUI_CPROGRESS *cp, GWEN_UNUSED GWEN_LOGGER_LEVEL level, const char *text)
Definition: cprogress.c:218