SOSpin is hosted by Hepforge, IPPP Durham
SOSpin  1.0.0
progressStatus.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // SOSpin Library
3 // Copyright (C) 2015 SOSpin Project
4 //
5 // Authors:
6 //
7 // Nuno Cardoso (nuno.cardoso@tecnico.ulisboa.pt)
8 // David Emmanuel-Costa (david.costa@tecnico.ulisboa.pt)
9 // Nuno Gonçalves (nunogon@deec.uc.pt)
10 // Catarina Simoes (csimoes@ulg.ac.be)
11 //
12 // ----------------------------------------------------------------------------
13 // This file is part of SOSpin Library.
14 //
15 // SOSpin Library is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or any
18 // later version.
19 //
20 // SOSpin Library is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 // GNU General Public License for more details.
24 //
25 // You should have received a copy of the GNU General Public License
26 // along with SOSpin Library. If not, see <http://www.gnu.org/licenses/>.
27 // ----------------------------------------------------------------------------
28 
29 // progressStatus.cpp created on 27/02/2015
30 //
31 // This file contains the functions necessary to do things
32 // in the SOSpin Library.
33 //
34 // Revision 1.1 28/02/2015 23:19:29 david
35 // License updated
36 //
37 
44 #include <stdio.h>
45 
46 #include <stdlib.h>
47 
48 #include "progressStatus.h"
49 
50 #include "son.h"
51 
52 
53 
54 
55 
56 using namespace std;
57 
58 
59 
60 
61 
62 namespace sospin {
63 
64 
65 
66 #define PROGRESS_STATUS_BAR_LENGTH 46
67 
68 
69 
70 
71 
72 
73 
74 void DoProgress( string label, unsigned int step, unsigned int total, unsigned int interval ){
75 
76  if(getVerbosity() == SILENT || total < 1) return;
77 
78  if ( step % interval != 0 && step < total) return;
79 
80  // Calculuate the ratio of complete-to-incomplete.
81 
82  float ratio = step/(float)total;
83 
84  int c = ratio * PROGRESS_STATUS_BAR_LENGTH;
85 
86  // Show the percentage completed.
87 
88  printf("%s %3d%% [", label.c_str(), (int)(ratio*100) );
89 
90  // Show the loading bar.
91 
92  for (int x=0; x<c; x++) printf("=");
93 
94  for (int x=c; x<PROGRESS_STATUS_BAR_LENGTH; x++) printf(" ");
95 
96  printf("]\n");
97 
98 
99 
100  // ANSI Control codes to go back to the previous line and clear it.
101 
102  if(getVerbosity() >= VERBOSE){
103 
104  if(step != total)
105 
106  printf("\033[F\033[J");
107 
108  }
109 
110  else{
111 
112  printf("\033[F\033[J");
113 
114  }
115 
116 }
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 void DoProgress( string label, unsigned int step, unsigned int total ){
127 
128  DoProgress(label, step, total, 10);
129 
130 }
131 
132 
133 
134 #ifdef PROGRESS_STATUS_BAR_LENGTH
135 
136 #undef PROGRESS_STATUS_BAR_LENGTH
137 
138 #endif
139 
140 
141 
142 }
143 
Verbosity getVerbosity()
Return current verbosity level.
Definition: son.cpp:84
Definition: enum.h:64
Definition: enum.h:66
Main Sospin header file. Includes C++ macros, to simplify expression writing, B operator, Verbosity level and memory usage.
void DoProgress(string label, unsigned int step, unsigned int total)
#define PROGRESS_STATUS_BAR_LENGTH
Specific functions progress status bar.