|
bpp-core
2.1.0
|
00001 // 00002 // File: ApplicationTools.cpp 00003 // Created by: Julien Dutheil 00004 // Created on: Fri Oct 21 16:19 2005 00005 // From old file created on: Sun Dec 14 09:36:26 2003 00006 // 00007 00008 /* 00009 Copyright or © or Copr. Bio++ Development Team, (November 16, 2004) 00010 00011 This software is a computer program whose purpose is to provide basal and 00012 utilitary classes. This file belongs to the Bio++ Project. 00013 00014 This software is governed by the CeCILL license under French law and 00015 abiding by the rules of distribution of free software. You can use, 00016 modify and/ or redistribute the software under the terms of the CeCILL 00017 license as circulated by CEA, CNRS and INRIA at the following URL 00018 "http://www.cecill.info". 00019 00020 As a counterpart to the access to the source code and rights to copy, 00021 modify and redistribute granted by the license, users are provided only 00022 with a limited warranty and the software's author, the holder of the 00023 economic rights, and the successive licensors have only limited 00024 liability. 00025 00026 In this respect, the user's attention is drawn to the risks associated 00027 with loading, using, modifying and/or developing or reproducing the 00028 software by the user in light of its specific status of free software, 00029 that may mean that it is complicated to manipulate, and that also 00030 therefore means that it is reserved for developers and experienced 00031 professionals having in-depth computer knowledge. Users are therefore 00032 encouraged to load and test the software's suitability as regards their 00033 requirements in conditions enabling the security of their systems and/or 00034 data to be ensured and, more generally, to use and operate it in the 00035 same conditions as regards security. 00036 00037 The fact that you are presently reading this means that you have had 00038 knowledge of the CeCILL license and that you accept its terms. 00039 */ 00040 00041 #include "ApplicationTools.h" 00042 00043 using namespace bpp; 00044 00045 // From the STL: 00046 #include <cmath> 00047 00048 using namespace std; 00049 00050 /******************************************************************************/ 00051 00052 OutputStream* ApplicationTools::error = new StdErr(); 00053 OutputStream* ApplicationTools::message = new StdOut(); 00054 OutputStream* ApplicationTools::warning = new StdOut(); 00055 time_t ApplicationTools::startTime; 00056 size_t ApplicationTools::terminalWidth = 80; 00057 float ApplicationTools::terminalSplit = 0.5; 00058 bool ApplicationTools::interactive = true; 00059 00060 /******************************************************************************/ 00061 00062 bool ApplicationTools::parameterExists( 00063 const std::string & parameterName, 00064 std::map<std::string, std::string>& params) 00065 { 00066 return (params.find(parameterName) != params.end() && !TextTools::isEmpty(params[parameterName])); 00067 } 00068 00069 /******************************************************************************/ 00070 00071 std::string ApplicationTools::getAFilePath( 00072 const std::string& parameter, 00073 std::map<std::string, std::string>& params, 00074 bool isRequired, 00075 bool mustExist, 00076 const std::string& suffix, 00077 bool suffixIsOptional) throw (Exception) 00078 { 00079 string filePath = getStringParameter(parameter, params, "none", suffix, suffixIsOptional, false); 00080 if (filePath == "") filePath = "none"; 00081 if (filePath == "none" && isRequired) 00082 { 00083 throw Exception("You must specify a file for this parameter: " + parameter + (suffixIsOptional ? "" : suffix)); 00084 } 00085 if(filePath == "none") return filePath; 00086 if(mustExist && !FileTools::fileExists(filePath)) 00087 { 00088 throw Exception("File does not exists: " + filePath); 00089 } 00090 return filePath; 00091 } 00092 00093 /******************************************************************************/ 00094 00095 double ApplicationTools::getDoubleParameter( 00096 const std::string & parameterName, 00097 std::map<std::string, std::string> & params, 00098 double defaultValue, 00099 const std::string & suffix, 00100 bool suffixIsOptional, 00101 bool warn) 00102 { 00103 double dParam = defaultValue; 00104 if (parameterExists(parameterName + suffix, params)) 00105 { 00106 dParam = TextTools::toDouble(params[parameterName + suffix]); 00107 } 00108 else if (suffixIsOptional && parameterExists(parameterName, params)) 00109 { 00110 dParam = TextTools::toDouble(params[parameterName]); 00111 } 00112 else if(warn) 00113 { 00114 displayWarning("Parameter " + parameterName + suffix + " not specified. Default used instead: " + TextTools::toString(defaultValue)); 00115 } 00116 return dParam; 00117 } 00118 00119 /******************************************************************************/ 00120 00121 int ApplicationTools::getIntParameter( 00122 const std::string & parameterName, 00123 std::map<std::string, std::string> & params, 00124 int defaultValue, 00125 const std::string & suffix, 00126 bool suffixIsOptional, 00127 bool warn) 00128 { 00129 int iParam = defaultValue; 00130 if (parameterExists(parameterName + suffix, params)) { 00131 iParam = TextTools::toInt(params[parameterName + suffix]); 00132 } else if(suffixIsOptional && parameterExists(parameterName, params)) { 00133 iParam = TextTools::toInt(params[parameterName]); 00134 } else if (warn) { 00135 displayWarning("Parameter " + parameterName + suffix + " not specified. Default used instead: " + TextTools::toString(defaultValue)); 00136 } 00137 return iParam; 00138 } 00139 00140 /******************************************************************************/ 00141 00142 std::string ApplicationTools::getStringParameter( 00143 const std::string& parameterName, 00144 std::map<std::string, std::string>& params, 00145 const std::string& defaultValue, 00146 const std::string& suffix, 00147 bool suffixIsOptional, 00148 bool warn) 00149 { 00150 string sParam = defaultValue; 00151 if (parameterExists(parameterName + suffix, params)) { 00152 sParam = params[parameterName + suffix]; 00153 } else if (suffixIsOptional && parameterExists(parameterName, params)) { 00154 sParam = params[parameterName]; 00155 } else if (warn) { 00156 displayWarning("Parameter " + parameterName + " not specified. Default used instead: " + defaultValue); 00157 } 00158 return sParam; 00159 } 00160 00161 /******************************************************************************/ 00162 00163 bool ApplicationTools::getBooleanParameter( 00164 const std::string& parameterName, 00165 std::map<std::string, std::string>& params, 00166 bool defaultValue, 00167 const std::string& suffix, 00168 bool suffixIsOptional, 00169 bool warn) 00170 { 00171 string sParam; 00172 bool bParam = defaultValue; 00173 if (parameterExists(parameterName + suffix, params)) 00174 { 00175 sParam = params[parameterName + suffix]; 00176 } 00177 else if (suffixIsOptional && parameterExists(parameterName, params)) 00178 { 00179 sParam = params[parameterName]; 00180 } 00181 else { 00182 if (warn) 00183 { 00184 displayWarning("Parameter " + parameterName + " not specified. Default used instead: " + TextTools::toString(defaultValue)); 00185 } 00186 return bParam; 00187 } 00188 if ((sParam == "true") 00189 || (sParam == "TRUE") 00190 || (sParam == "t") 00191 || (sParam == "T") 00192 || (sParam == "yes") 00193 || (sParam == "YES") 00194 || (sParam == "y") 00195 || (sParam == "Y") 00196 || (sParam == "1")) 00197 bParam = true; 00198 else if ((sParam == "false") 00199 || (sParam == "FALSE") 00200 || (sParam == "f") 00201 || (sParam == "F") 00202 || (sParam == "no") 00203 || (sParam == "NO") 00204 || (sParam == "n") 00205 || (sParam == "N") 00206 || (sParam == "0")) 00207 bParam = false; 00208 else throw Exception("ApplicationTools::getBooleanParameter. Wrong description:" + sParam); 00209 00210 return bParam; 00211 } 00212 00213 /******************************************************************************/ 00214 00215 void ApplicationTools::displayMessage(const std::string& text) { if(message) (*message << text).endLine(); } 00216 00217 void ApplicationTools::displayError(const std::string& text) { if(error) (*error << "ERROR!!! " << text).endLine(); } 00218 00219 void ApplicationTools::displayWarning(const std::string& text) { if(warning) (*warning << "WARNING!!! " << text).endLine(); } 00220 00221 void ApplicationTools::displayTask(const std::string& text, bool eof) 00222 { 00223 if (message) 00224 { 00225 *message << TextTools::resizeRight(text, static_cast<size_t>(static_cast<float>(terminalWidth) * terminalSplit - 1), '.') << ": "; 00226 if (eof) message->endLine(); 00227 else message->flush(); 00228 } 00229 } 00230 00231 void ApplicationTools::displayTaskDone() { if(message) (*message << "Done.").endLine(); } 00232 00233 /******************************************************************************/ 00234 00235 void ApplicationTools::displayGauge(size_t iter, size_t total, char symbol, const std::string& mes) 00236 { 00237 if (!message) return; 00238 if (total == 0) return;//We do not display anything in that case. 00239 size_t width = static_cast<size_t>(static_cast<float>(terminalWidth) * terminalSplit - 2); 00240 string gauge = string(static_cast<size_t>((1. * static_cast<double>(iter) / static_cast<double>(total)) * static_cast<double>(width)), symbol); 00241 string info = mes; 00242 if (interactive) 00243 { 00244 string fill = string(width - gauge.length(), ' '); 00245 gauge = "[" + gauge + fill + "] " + TextTools::resizeLeft(TextTools::toString(100 * iter / total), 3) + "%"; 00246 if (mes.size() > terminalWidth - gauge.size()) 00247 info = TextTools::resizeRight(mes, terminalWidth - gauge.size()); 00248 *message << '\r' + info + gauge; 00249 message->flush(); 00250 } 00251 else 00252 { 00253 if (iter == 0) 00254 { 00255 *message << "["; 00256 message->flush(); 00257 return; 00258 } 00259 size_t step = static_cast<size_t>(ceil(1. * static_cast<double>(total) / static_cast<double>(width))); 00260 if (iter >= total) 00261 { 00262 size_t fill = static_cast<size_t>(static_cast<float>(terminalWidth) * terminalSplit) - (total - 1) / step - 1; 00263 *message << TextTools::resizeLeft("]", fill, symbol); 00264 message->flush(); 00265 return; 00266 } 00267 size_t x = iter % step; 00268 if (x == 0) { *message << symbol; message->flush(); } 00269 } 00270 } 00271 00272 /******************************************************************************/ 00273 00274 void ApplicationTools::displayUnlimitedGauge(size_t iter, const std::string& mes) 00275 { 00276 if (!message) return; 00277 string chars = "-/-\\"; 00278 string info = mes; 00279 if (interactive) 00280 { 00281 unsigned int i = iter % 4; 00282 *message << '\r' << info << chars[i] << " " << TextTools::toString(iter); 00283 message->flush(); 00284 } 00285 else 00286 { 00287 if (iter == 0) 00288 *message << info; 00289 *message << "*"; 00290 message->flush(); 00291 return; 00292 } 00293 } 00294 00295 /******************************************************************************/ 00296 00297 void ApplicationTools::displayTime(const std::string& msg) 00298 { 00299 time_t endTime; 00300 time(&endTime); 00301 if (message) 00302 { 00303 double nsec = difftime(endTime, startTime); 00304 double nmin = floor(nsec/60.); 00305 double nhou = floor(nmin/60.); 00306 double nday = floor(nhou/24.); 00307 nhou = nhou - nday * 24; 00308 nmin = nmin - (nday * 24 + nhou) * 60; 00309 nsec = nsec - ((nday * 24 + nhou) * 60 + nmin) * 60; 00310 *message << msg << " "; 00311 *message << nday << "d, "; 00312 *message << nhou << "h, "; 00313 *message << nmin << "m, "; 00314 *message << nsec << "s."; 00315 message->endLine(); 00316 } 00317 } 00318 00319 /******************************************************************************/ 00320 00321 double ApplicationTools::getTime() 00322 { 00323 time_t endTime; 00324 time(&endTime); 00325 return difftime(endTime, startTime); 00326 } 00327 00328 /******************************************************************************/ 00329