Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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 }