PLearn 0.1
ShellProgressBar.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //
00004 // Copyright (C) 2004 Universite de Montreal
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //
00009 //  1. Redistributions of source code must retain the above copyright
00010 //     notice, this list of conditions and the following disclaimer.
00011 //
00012 //  2. Redistributions in binary form must reproduce the above copyright
00013 //     notice, this list of conditions and the following disclaimer in the
00014 //     documentation and/or other materials provided with the distribution.
00015 //
00016 //  3. The name of the authors may not be used to endorse or promote
00017 //     products derived from this software without specific prior written
00018 //     permission.
00019 //
00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 //
00031 // This file is part of the PLearn library. For more information on the PLearn
00032 // library, go to the PLearn Web site at www.plearn.org
00033 
00034 /* *******************************************************
00035  * $Id: ShellProgressBar.cc 3994 2005-08-25 13:35:03Z chapados $
00036  ******************************************************* */
00037 
00038 
00039 #include "ShellProgressBar.h"
00040 #include <plearn/base/stringutils.h> 
00041 #include <fstream>
00042 #include <plearn/sys/Popen.h> 
00043 
00044 namespace PLearn {
00045 using namespace std;
00046 
00047 ShellProgressBar::ShellProgressBar(int min, int max, string caption, int width) 
00048     : min(min), max(max), caption(caption), width(width)
00049 {
00050     init();
00051 }
00052 
00053 void ShellProgressBar::init()
00054 {
00055     blockwidth = (real)(max - min) / width;
00056     pos = 0;
00057     max_reached = false;
00058 }
00059 
00060 void ShellProgressBar::draw(bool simple_mode)
00061 {
00062     if (simple_mode)
00063     {
00064         cout << caption << " " << getTime() << " ";
00065         cout.flush();
00066     } else
00067     {
00068         cout << caption << " " << getTime() << " |";
00069         for (int i = 0;i < width; i++) cout << " ";
00070         cout <<"|";
00071         string move_cursor_left = "\033[" + tostring(width + 1) + "D";
00072         cout << move_cursor_left;
00073         cout.flush();
00074     }
00075 }
00076 
00077 bool ShellProgressBar::update(int value)
00078 {
00079     if (value < min || max_reached)
00080     {
00081         return false;
00082     } else if (value >= max)
00083     {
00084         for (int i = pos; i < width; i++)
00085         {
00086             cout << "=";
00087             cout.flush();
00088         }
00089         max_reached = true;
00090         return true;
00091     }
00092 
00093     int inc = (int)((value - min) / blockwidth);
00094 
00095     int i;
00096     for (i = pos; i < inc; i++)
00097     {
00098         cout << "=";
00099         cout.flush();
00100     }
00101     pos = i;
00102 
00103     return true;
00104 }
00105 
00106 void ShellProgressBar::reset()
00107 {
00108     max_reached = false;
00109     pos = 0;
00110 }
00111 
00112 void ShellProgressBar::setCaption(string the_caption)
00113 {
00114     caption = the_caption;
00115 }
00116 
00117 void ShellProgressBar::setMin(int the_min)
00118 {
00119     min = the_min;
00120 }
00121 
00122 void ShellProgressBar::setMax(int the_max)
00123 {
00124     max = the_max;
00125 }
00126 
00127 void ShellProgressBar::done()
00128 {
00129     if (!max_reached)
00130         update(max);
00131     cout << "\033[2C" << getTime() << endl;
00132 }
00133 
00134 int ShellProgressBar::getAsciiFileLineCount(string file)
00135 {
00136     // not terribly efficient, I fear, but I don't 
00137     // have the time for a better solution (the one
00138     // with Popen crashes with a MPI program...)
00139     ifstream in(file.c_str());
00140     int n_lines = 0;
00141     while (in)
00142     {
00143         pgetline(in);
00144         n_lines++;
00145     }
00146     in.close();
00147     return n_lines;
00148 }
00149 
00150 int ShellProgressBar::getWcAsciiFileLineCount(string file)
00151 {
00152     string wc = "wc -l " + file;
00153     vector<string> answer = execute(wc);
00154     return toint(answer[0]);
00155 }
00156 
00157 string ShellProgressBar::getTime()
00158 {
00159     time_t tt;
00160     time(&tt);
00161     string time_str(ctime(&tt));
00162     vector<string> tokens = split(time_str);
00163     return "[" + tokens[3] + "]";
00164 }
00165 
00166 } // end of namespace PLearn
00167 
00168 
00169 /*
00170   Local Variables:
00171   mode:c++
00172   c-basic-offset:4
00173   c-file-style:"stroustrup"
00174   c-file-offsets:((innamespace . 0)(inline-open . 0))
00175   indent-tabs-mode:nil
00176   fill-column:79
00177   End:
00178 */
00179 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines