1 /* ---------------------------- progress output ---------------------- */
6 FILE *fp=NULL; /* output stream for progress info */
7 time_t printinterval = 10; /* approx. seconds between printouts, 1.. */
9 /* initialization of progress output, fname="<fileID>","<filename>","-" */
10 int ini_progress(char *fname){
12 if (fp) { fclose(fp); fp=NULL; }
13 if (fname) if (fname[0]) {
15 if(fd>255 || fname[((fd>99)?3:((fd>9)?2:1))]) fd=-1; /* be sure */
16 if (fname[0]=='-' && fname[1]==0) { fp=stdout; }
18 else if (fd>0) { fp=fdopen(fd,"w"); } /* not sure that "w" is ok ???? */
20 else { fp=fopen(fname,"w");if(!fp)fp=fopen(fname,"a"); }
22 fprintf(stderr,"could not open %s for progress output\n",fname);
23 return -1; /* no success */
26 /* fprintf(stderr,"# progress: fd=%d\n",fileno(fp)); */
27 return 0; /* no error */
30 progress_counter_t *open_progress(int maxcount, const char *name){
31 progress_counter_t *pc;
32 pc = (progress_counter_t*) malloc( sizeof(progress_counter_t) );
33 if (!pc) return 0; /* nonfatal */
34 pc->starttime = time(NULL);
35 pc->maxcount = maxcount;
37 pc->lastprintcount = -1;
39 pc->lastprinttime = pc->starttime;
43 int close_progress(progress_counter_t *counter){
44 if (counter) free(counter);
47 /* progress meter output
48 * only 1output/10s, + estimated endtime (test on pixelfields)
49 * ToDo: to stderr by default? remove subprogress, ini_progress? rm_progress?
52 int progress(int counter, progress_counter_t *pc){
53 /* we try to save computing time, so we skip early */
54 if ((!fp) || counter - pc->lastprintcount <= pc->numskip) return 0;
57 time_t now = time(NULL);
60 fprintf(fp," progress %s %3d / %d time %d skip %d\n",
61 pc->name,counter,pc->maxcount,(int)(now - pc->starttime),
62 pc->numskip); fflush(fp);
64 if (5*(now - pc->lastprinttime) < 2*printinterval
65 && counter - pc->lastprintcount >= pc->numskip) { /* save for tests */
66 if (pc->numskip < 1024) pc->numskip += pc->numskip+1;
68 if (3*(now - pc->lastprinttime) < 2*printinterval ) {
69 return 0; /* to early for printing */
71 if (2*(now - pc->lastprinttime) > 3*printinterval ) {
72 pc->numskip >>= 1; /* to late for printing */
74 if (fileno(fp)<3) cr='\r'; /* may be choosen in ini? */
76 fprintf(fp," progress %s %5d / %d time[s] %5d / %5d (skip=%d)%c",
77 pc->name,counter,pc->maxcount,
78 (int)(now - pc->starttime), /* time gone since start */
79 (int)(now - pc->starttime)*pc->maxcount/(counter), /* estimated */
82 pc->lastprintcount=counter;
83 pc->lastprinttime=now;
85 return 0; /* no error */
87 /* --------------------- end of progress output ---------------------- */