MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/util/string.h
Go to the documentation of this file.
00001 /************************************************************************
00002  * MechSys - Open Library for Mechanical Systems                        *
00003  * Copyright (C) 2005 Dorival M. Pedroso, Raul Durand                   *
00004  * Copyright (C) 2009 Sergio Galindo                                    *
00005  *                                                                      *
00006  * This program is free software: you can redistribute it and/or modify *
00007  * it under the terms of the GNU General Public License as published by *
00008  * the Free Software Foundation, either version 3 of the License, or    *
00009  * any later version.                                                   *
00010  *                                                                      *
00011  * This program is distributed in the hope that it will be useful,      *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
00014  * GNU General Public License for more details.                         *
00015  *                                                                      *
00016  * You should have received a copy of the GNU General Public License    *
00017  * along with this program. If not, see <http://www.gnu.org/licenses/>  *
00018  ************************************************************************/
00019 
00020 #ifndef MECHSYS_STRING_H
00021 #define MECHSYS_STRING_H
00022 
00023 // STL
00024 #include <string>
00025 #include <boost/algorithm/string.hpp> // for to_upper
00026 #include <cstdarg> // for va_list, va_start, va_end
00027 #include <cstdio>  // for vsnprintf
00028 #include <stdio.h> // for printf
00029 
00030 // color codes for the terminal
00031 #define TERM_RST           ""
00032 #define TERM_CLR_BLACK     ""
00033 #define TERM_CLR_BLACK_H   ""
00034 #define TERM_CLR_WHITE     ""
00035 #define TERM_CLR_WHITE_H   ""
00036 #define TERM_CLR_RED       ""
00037 #define TERM_CLR_RED_H     ""
00038 #define TERM_CLR_GREEN     ""
00039 #define TERM_CLR_GREEN_H   ""
00040 #define TERM_CLR_YELLOW    ""
00041 #define TERM_CLR_YELLOW_H  ""
00042 #define TERM_CLR_BLUE      ""
00043 #define TERM_CLR_BLUE_H    ""
00044 #define TERM_CLR_MAGENTA   ""
00045 #define TERM_CLR_MAGENTA_H ""
00046 #define TERM_CLR_CYAN      ""
00047 #define TERM_CLR_CYAN_H    ""
00048 #define TERM_WHITE_BLUE    ""
00049 #define TERM_YELLOW_BLUE   ""
00050 #define TERM_YELLOW_BLACK  ""
00051 #define TERM_BLACK_WHITE   ""
00052 
00053 #ifdef TERM_WHITEBG
00054   #define TERM_CLR1  TERM_CLR_BLACK_H
00055   #define TERM_CLR2  TERM_CLR_BLACK
00056   #define TERM_CLR3  TERM_CLR_BLUE
00057   #define TERM_CLR4  TERM_CLR_CYAN
00058   #define TERM_CLR5  TERM_CLR_MAGENTA
00059   #define TERM_RED   TERM_CLR_RED_H
00060   #define TERM_GREEN TERM_CLR_GREEN
00061 #else
00062   #ifdef TERM_NOCOLORS
00063     #define TERM_CLR1  ""
00064     #define TERM_CLR2  ""
00065     #define TERM_CLR3  ""
00066     #define TERM_CLR4  ""
00067     #define TERM_CLR5  ""
00068     #define TERM_RED   ""
00069     #define TERM_GREEN ""
00070   #else
00071     #define TERM_CLR1  TERM_CLR_YELLOW_H
00072     #define TERM_CLR2  TERM_CLR_WHITE_H
00073     #define TERM_CLR3  TERM_CLR_BLUE_H
00074     #define TERM_CLR4  TERM_CLR_CYAN_H
00075     #define TERM_CLR5  TERM_CLR_MAGENTA_H
00076     #define TERM_RED   TERM_CLR_RED_H
00077     #define TERM_GREEN TERM_CLR_GREEN_H
00078   #endif
00079 #endif
00080 
00081 class String : public std::string
00082 {
00083 public:
00084     // Constructors
00085     String () {}
00086     String (std::string const & Other) : std::string(Other) {}
00087     String (char        const * Other) : std::string(Other) {}
00088 
00089     // Methods
00090     int          Printf     (String const & Fmt, ...);                                                        
00091     int          PrintfV    (String const & Fmt, va_list ArgList) { return _set_msg (Fmt.c_str(), ArgList); } 
00092     char const * CStr       ()                              const { return this->c_str(); }                   
00093     void         TextFmt    (char const * NF);                                                                
00094     void         Split      (String & Left, String & Right, char const * Separator=" ");                      
00095     bool         HasWord    (String const & Word) { return (find(Word)!=npos); }                              
00096     void         GetFNKey   (String & FNKey);                                                                 
00097     void         ToUpper    ()       { boost::to_upper ((*this)); }                                           
00098     void         ToLower    ()       { boost::to_lower ((*this)); }                                           
00099     String       ToUpperCpy () const { String tmp((*this)); tmp.ToUpper(); return tmp; }                      
00100     String       ToLowerCpy () const { String tmp((*this)); tmp.ToLower(); return tmp; }                      
00101 
00102     // For compatibility with wxWidgets
00103     String       & ToStdString()       { return (*this); }
00104     String const & ToStdString() const { return (*this); }
00105 
00106 #ifdef USE_BOOST_PYTHON
00107     BPy::str PyStr() const { return BPy::str(CStr()); }
00108 #endif
00109 
00110 private:
00111     int _set_msg (char const * Fmt, va_list ArgList);
00112 };
00113 
00114 
00116 
00117 
00118 inline int String::Printf (String const & Fmt, ...)
00119 {
00120     int len;
00121     va_list       arg_list;
00122     va_start     (arg_list, Fmt);
00123     len=_set_msg (Fmt.c_str(), arg_list);
00124     va_end       (arg_list);
00125     return len;
00126 }
00127 
00128 inline void String::TextFmt (char const * NF)
00129 {
00130     // number format for text
00131     this->clear();
00132     this->append(NF);
00133     size_t pos;
00134     pos=this->find("g"); while (pos!=String::npos) { this->replace(pos,1,"s"); pos=this->find("g",pos+1); }
00135     pos=this->find("f"); while (pos!=String::npos) { this->replace(pos,1,"s"); pos=this->find("f",pos+1); }
00136     pos=this->find("e"); while (pos!=String::npos) { this->replace(pos,1,"s"); pos=this->find("e",pos+1); }
00137 }
00138 
00139 inline void String::Split (String & Left, String & Right, char const * Separator)
00140 {
00141     size_t pos = find(Separator);
00142     Left  = substr (0,pos);
00143     if (pos==npos) Right = "";
00144     else           Right = substr (pos+String(Separator).size());
00145 }
00146 
00147 inline void String::GetFNKey (String & FNKey)
00148 {
00149     size_t pos = rfind(".");
00150     FNKey = substr (0,pos);
00151 }
00152 
00153 inline int String::_set_msg (char const * Fmt, va_list ArgList)
00154 {
00155     const int size = 4048; // TODO: remove this limitation by using a loop and reallocating space
00156     char      buffer[size];
00157     int       len = std::vsnprintf(buffer, size, Fmt, ArgList);
00158     this->clear();
00159     if (len<0) this->append("String::_set_msg: INTERNAL ERROR: std::vsnprintf FAILED");
00160     else
00161     {
00162         buffer[len]='\0';
00163         if (len>size) this->append("String::_set_msg: INTERNAL ERROR: std::vsnprintf MESSAGE TRUNCATED: ");
00164         this->append(buffer);
00165     }
00166     return len;
00167 }
00168 
00169 #endif // MECHSYS_STRING_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines