automatic commit
[folded-ctf.git] / progress_bar.cc
1 /*
2  *  folded-ctf is an implementation of the folded hierarchy of
3  *  classifiers for object detection, developed by Francois Fleuret
4  *  and Donald Geman.
5  *
6  *  Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of folded-ctf.
10  *
11  *  folded-ctf is free software: you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published
13  *  by the Free Software Foundation, either version 3 of the License,
14  *  or (at your option) any later version.
15  *
16  *  folded-ctf is distributed in the hope that it will be useful, but
17  *  WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with folded-ctf.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25
26 #include <time.h>
27 #include "progress_bar.h"
28
29 ProgressBar::ProgressBar()  : _visible(false), _value_max(-1) { }
30
31 void ProgressBar::set_visible(bool visible) {
32   _visible = visible;
33 }
34
35 void ProgressBar::init(ostream *out, scalar_t value_max) {
36   _value_max = value_max;
37   _last_step = -1;
38   time(&_initial_time);
39   refresh(out, 0);
40 }
41
42 void ProgressBar::refresh(ostream *out, scalar_t value) {
43   if(_visible && _value_max > 0) {
44     int step = int((value * 40) / _value_max);
45
46     if(1 || step > _last_step) {
47       char buffer[width + 1], date_buffer[buffer_size];
48       int i, j;
49       j = sprintf(buffer, "Timer: ");
50
51       for(i = 0; i < step; i++) buffer[j + i] = 'X';
52       for(; i < 40; i++) buffer[j + i] = (i%4 == 0) ? '+' : '-';
53       j += i;
54
55       time_t current_time; time(&current_time);
56       int rt = int(((current_time - _initial_time)/scalar_t(value)) * scalar_t(_value_max - value));
57
58       if(rt > 0) {
59         if(rt > 3600 * 24) {
60           time_t current;
61           time(&current);
62           current += rt;
63           strftime(date_buffer, buffer_size, "%a %b %e %H:%M", localtime(&current));
64           j += snprintf(buffer + j, width - j - 1, " (end ~ %s)", date_buffer);
65         } else {
66           int hours = rt/3600, min = (rt%3600)/60, sec = rt%60;
67           if(hours > 0)
68             j += snprintf(buffer + j, width - j - 1, " (~%dh%dmin left)", hours, min);
69           else if(min > 0)
70             j += snprintf(buffer + j, width - j - 1, " (~%dmin%ds left)", min, sec);
71           else
72             j += snprintf(buffer + j, width - j - 1, " (~%ds left)", sec);
73         }
74       }
75
76       for(; j < width; j++) buffer[j] = ' ';
77       buffer[j] = '\0';
78       (*out) << buffer << "\r";
79       out->flush();
80       _last_step = step;
81     }
82   }
83 }
84
85 void ProgressBar::finish(ostream *out) {
86   if(_visible) {
87     char buffer[width + 1];
88     int j;
89     time_t current_time; time(&current_time);
90     int rt = int(current_time - _initial_time);
91     int min = rt/60, sec = rt%60;
92     j = sprintf(buffer, "Timer: Total %dmin%ds", min, sec);
93     for(; j < width; j++) buffer[j] = ' ';
94     buffer[j] = '\0';
95     (*out) << buffer << endl;
96     out->flush();
97   }
98 }