replaced libart with new polygon code
[swftools.git] / lib / gocr / progress.c
1 /* ---------------------------- progress output ---------------------- */
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "progress.h"
5
6 FILE *fp=NULL; /* output stream for progress info */ 
7 time_t printinterval = 10; /* approx. seconds between printouts, 1.. */
8
9 /* initialization of progress output, fname="<fileID>","<filename>","-"  */
10 int ini_progress(char *fname){
11   int fd;
12   if (fp) { fclose(fp); fp=NULL; }
13   if (fname) if (fname[0]) {
14     fd=atoi(fname);
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; }
17 #ifdef __USE_POSIX
18     else if (fd>0) { fp=fdopen(fd,"w"); } /* not sure that "w" is ok ???? */
19 #endif
20     else { fp=fopen(fname,"w");if(!fp)fp=fopen(fname,"a"); }
21     if (!fp) {
22       fprintf(stderr,"could not open %s for progress output\n",fname);
23       return -1; /* no success */
24     }
25   }
26   /* fprintf(stderr,"# progress: fd=%d\n",fileno(fp)); */
27   return 0; /* no error */
28 }
29
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;
36   pc->numskip = 0;
37   pc->lastprintcount = -1;
38   pc->name = name;
39   pc->lastprinttime = pc->starttime;
40   return pc;
41 }
42 /* free counter */
43 int close_progress(progress_counter_t *counter){
44   if (counter) free(counter);
45   return 0;
46 }
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?
50  *   test on tcl
51  */
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;
55   {
56     char cr='\n';
57     time_t now = time(NULL);
58 #if 0 /* debugging */
59     if (counter)
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);
63 #endif
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;
67     }
68     if (3*(now - pc->lastprinttime) < 2*printinterval ) {
69       return 0; /* to early for printing */
70     }
71     if (2*(now - pc->lastprinttime) > 3*printinterval ) {
72       pc->numskip >>= 1;  /* to late for printing */
73     }
74     if (fileno(fp)<3) cr='\r'; /* may be choosen in ini? */
75     if (counter)
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 */
80             pc->numskip, cr);
81     fflush(fp);
82     pc->lastprintcount=counter;
83     pc->lastprinttime=now;
84   }
85   return 0; /* no error */
86 }
87 /* --------------------- end of progress output ---------------------- */