gwenhywfar  4.99.8beta
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  GWEN_Gui_StdPrintf(gui, stderr, "===== %s =====\n", 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  GWEN_Gui_StdPrintf(cp->gui, 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  GWEN_Gui_StdPrintf(cp->gui, stderr, "%s: %llu\n", cp->title,
162  (long long unsigned)progress);
163  else
164  GWEN_Gui_StdPrintf(cp->gui, 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  GWEN_Gui_StdPrintf(cp->gui, 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[GWEN_Buffer_GetUsedBytes(tbuf)-1]!='\n') {
220  GWEN_Buffer_AppendByte(tbuf, '\n');
221  /* Just in case the buffer has been reallocated */
222  t=GWEN_Buffer_GetStart(tbuf);
223  }
224  GWEN_Gui_StdPrintf(cp->gui, stderr, "%s", t);
225 
226  GWEN_Buffer_AppendString(cp->logBuf, t);
227  GWEN_Buffer_free(tbuf);
228  tbuf=0;
229  if (cp->aborted)
231  }
232  return 0;
233 }
234 
235 
236 
238  assert(cp);
239 
240  if (cp->shown) {
242  GWEN_Gui_StdPrintf(cp->gui, stderr, "%s: Finished.\n", cp->title);
243  }
244  if (cp->aborted)
246 
247  return 0;
248 }
249 
250 
251 
252 
const char * GWEN_Gui_CProgress_GetText(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:91
int GWEN_Gui_CProgress_GetAborted(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:127
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:223
uint32_t GWEN_Gui_CProgress_GetId(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:77
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
Definition: buffer.c:266
#define GWEN_GUI_FLAGS_NONINTERACTIVE
Definition: gui.h:979
GWEN_LOGGER_LEVEL
Definition: logger.h:64
uint64_t GWEN_Gui_CProgress_GetCurrent(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:112
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:92
const char * GWEN_Gui_CProgress_GetLogBuf(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:119
#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:237
GWEN_GUI * GWEN_Gui_CProgress_GetGui(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:70
#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:329
#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:380
uint64_t GWEN_Gui_CProgress_GetTotal(const GWEN_GUI_CPROGRESS *cp)
Definition: cprogress.c:98
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:83
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:137
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:164
#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:84
void GWEN_Gui_CProgress_SetTotal(GWEN_GUI_CPROGRESS *cp, uint64_t i)
Definition: cprogress.c:105
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:58
#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:251
uint32_t GWEN_Gui_GetFlags(const GWEN_GUI *gui)
Definition: gui.c:660
#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:1014
#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:206