UtilityBase.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2013, Institute for Artificial Intelligence,
00005  *  Universität Bremen.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Institute for Artificial Intelligence,
00019  *     Universität Bremen, nor the names of its contributors may be
00020  *     used to endorse or promote products derived from this software
00021  *     without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *********************************************************************/
00036 
00040 #include <UtilityBase.h>
00041 
00042 
00043 namespace beliefstate {
00044   bool UtilityBase::m_bRedirectOutput;
00045   
00046   
00047   UtilityBase::UtilityBase() {
00048     m_strMessagePrefixLabel = "";
00049     m_bRedirectOutput = false;
00050     m_bOnlyDisplayImportant = false;
00051   }
00052   
00053   UtilityBase::~UtilityBase() {
00054   }
00055   
00056   void UtilityBase::setMessagePrefixLabel(std::string strMessagePrefixLabel) {
00057     m_strMessagePrefixLabel = strMessagePrefixLabel;
00058   }
00059   
00060   std::string UtilityBase::messagePrefixLabel() {
00061     return m_strMessagePrefixLabel;
00062   }
00063   
00064   void UtilityBase::coloredText(std::string strText, std::string strColorValue, bool bBold, bool bImportant) {
00065     if(!m_bOnlyDisplayImportant || (m_bOnlyDisplayImportant && bImportant)) {
00066       StatusMessage msgStatus = queueMessage(strColorValue, bBold, this->messagePrefixLabel(), strText);
00067       
00068       if(!m_bRedirectOutput) {
00069         this->outputColoredStatusMessage(msgStatus);
00070       }
00071     }
00072   }
00073   
00074   void UtilityBase::info(std::string strMessage, bool bImportant) {
00075     this->coloredText(strMessage, "37", false, bImportant);
00076   }
00077   
00078   void UtilityBase::success(std::string strMessage, bool bImportant) {
00079     this->coloredText(strMessage, "32", false, bImportant);
00080   }
00081   
00082   void UtilityBase::warn(std::string strMessage, bool bImportant) {
00083     this->coloredText(strMessage, "33", true, bImportant);
00084   }
00085   
00086   void UtilityBase::fail(std::string strMessage, bool bImportant) {
00087     this->coloredText(strMessage, "31", true, bImportant);
00088   }
00089   
00090   bool UtilityBase::fileExists(std::string strFileName) {
00091     bool bGood = false;
00092     std::ifstream ifile(strFileName.c_str(), std::ifstream::in);
00093     
00094     if(ifile) {
00095       bGood = ifile.good();
00096       
00097       ifile.close();
00098     }
00099     
00100     return bGood;
00101   }
00102   
00103   std::string UtilityBase::stripPostfix(std::string strString, std::string strPostfix) {
00104     if(strString.length() >= strPostfix.length()) {
00105       if(strString.substr(strString.length() - strPostfix.length()) == strPostfix) {
00106         return strString.substr(0, strString.length() - strPostfix.length());
00107       }
00108     }
00109     
00110     return strString;
00111   }
00112   
00113   void UtilityBase::outputColoredStatusMessage(StatusMessage msgStatus) {
00114     std::cout << "\033[" << (msgStatus.bBold ? "1" : "0") << ";" << msgStatus.strColorCode << "m"
00115               << "[ " << msgStatus.strPrefix << " ] " << msgStatus.strMessage << "\033[0m" << std::endl;
00116   }
00117   
00118   void UtilityBase::setRedirectOutput(bool bRedirect) {
00119     m_bRedirectOutput = bRedirect;
00120   }
00121   
00122   void UtilityBase::replaceStringInPlace(std::string& subject, const std::string& search, const std::string& replace) {
00123     size_t pos = 0;
00124     
00125     while((pos = subject.find(search, pos)) != string::npos) {
00126       subject.replace(pos, search.length(), replace);
00127       pos += replace.length();
00128     }
00129   }
00130   
00131   int UtilityBase::getTimeStamp() {
00132     return std::time(0);
00133   }
00134   
00135   std::string UtilityBase::getTimeStampStr() {
00136     return this->str(this->getTimeStamp());
00137   }
00138   
00139   std::string UtilityBase::str(float fValue) {
00140     std::stringstream sts;
00141     sts.imbue(std::locale(sts.getloc(), new std::numpunct<char>()));
00142     
00143     sts << fValue;
00144     
00145     return sts.str();
00146   }
00147   
00148   std::string UtilityBase::str(double dValue) {
00149     std::stringstream sts;
00150     sts.imbue(std::locale(sts.getloc(), new std::numpunct<char>()));
00151     
00152     sts << dValue;
00153     
00154     return sts.str();
00155   }
00156   
00157   std::string UtilityBase::str(int nValue) {
00158     std::stringstream sts;
00159     sts.imbue(std::locale(sts.getloc(), new std::numpunct<char>()));
00160     
00161     sts << nValue;
00162     
00163     return sts.str();
00164   }
00165   
00166   void UtilityBase::setOnlyDisplayImportant(bool bOnly) {
00167     m_bOnlyDisplayImportant = bOnly;
00168   }
00169   
00170   bool UtilityBase::onlyDisplayImportant() {
00171     return m_bOnlyDisplayImportant;
00172   }
00173 }


beliefstate
Author(s): Jan Winkler
autogenerated on Sun Oct 5 2014 22:30:15