string.h

00001 /*************************************************************************************
00002  * MechSys - A C++ library to simulate (Continuum) Mechanical Systems                *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>       *
00004  * Copyright (C) 2005 Raul Dario Durand Farfan  <raul.durand at gmail.com>           *
00005  *                                                                                   *
00006  * This file is part of MechSys.                                                     *
00007  *                                                                                   *
00008  * MechSys is free software; you can redistribute it and/or modify it under the      *
00009  * terms of the GNU General Public License as published by the Free Software         *
00010  * Foundation; either version 2 of the License, or (at your option) any later        *
00011  * version.                                                                          *
00012  *                                                                                   *
00013  * MechSys is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   *
00015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.          *
00016  *                                                                                   *
00017  * You should have received a copy of the GNU General Public License along with      *
00018  * MechSys; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  * Fifth Floor, Boston, MA 02110-1301, USA                                           *
00020  *************************************************************************************/
00021 
00022 #ifndef MECHSYS_STRING_H
00023 #define MECHSYS_STRING_H
00024 
00025 #ifdef USE_WXSTRING
00026     #include <string>
00027     #include <wx/string.h>
00028     #include <wx/intl.h> // for _() macro
00029 
00030     class String : public wxString
00031     {
00032     public:
00033         String(){}
00034         String(std::string const & Other)
00035         {
00036             char const * ascii_str = Other.c_str();
00037             wxString string(ascii_str, wxConvUTF8);
00038             this->clear();
00039             this->append(string);
00040         }
00041         String(const char * Other)
00042         {
00043             wxString string(Other, wxConvUTF8);
00044             this->clear();
00045             this->append(string);
00046         }
00047         String(wxChar const * psz)
00048         {
00049             this->clear();
00050             this->append(psz);
00051         }
00052         std::string GetSTL() const
00053         {
00054             #if wxUSE_UNICODE
00055                 return std::string(this->mb_str(wxConvUTF8));
00056             #else
00057                 return std::string(this->c_str());
00058             #endif
00059         }
00060         String & operator= (wxChar const * psz)
00061         {
00062             (*this) = psz;
00063             return (*this);
00064         }
00065     private:
00066     }; // class String
00067 
00068     bool operator== (String const & A, char const * B_ascii)
00069     {
00070         return A.GetSTL()==B_ascii;
00071     }
00072 
00073     #include <sstream>
00074     std::istream & operator>> (std::istream & input, String & S)
00075     {
00076         std::string str;
00077         input >> str;
00078         S = str;
00079         return input;
00080     }
00081 
00082 #else // (do not) USE_WXSTRING
00083     #include <string>
00084     #include <cstdarg> // for va_list, va_start, va_end
00085     #include <cstdlib> // for vsnprintf
00086 
00087     #define _(STRING)  (STRING)  // for transation
00088     #define _T(STRING) (STRING)  // NO transation
00089 
00090     class String : public std::string
00091     {
00092     public:
00093         String(){}
00094         String(std::string const & Other)
00095         {
00096             this->clear();
00097             this->append(Other);
00098         }
00099         String(const char * Msg)
00100         {
00101             this->clear();
00102             this->append(Msg);
00103         }
00104         int Printf(String const & Fmt, ...)
00105         {
00106             int len;
00107             va_list       arg_list;
00108             va_start     (arg_list, Fmt);
00109             len=_set_msg (Fmt.c_str(), arg_list);
00110             va_end       (arg_list);
00111             return len;
00112         }
00113         int PrintfV(String const & Fmt, va_list ArgList)
00114         {
00115             return _set_msg (Fmt.c_str(), ArgList);
00116         }
00117         std::string GetSTL() const
00118         {
00119             return std::string((*this));
00120         }
00121     private:
00122         int _set_msg(char const * Fmt, va_list ArgList)
00123         {
00124             const int size = 4048; // TODO: remove this limitation by using a loop and reallocating space
00125             char      buffer[size];
00126             int       len = std::vsnprintf(buffer, size, Fmt, ArgList);
00127             this->clear();
00128             if (len<0) this->append("String::_set_msg: INTERNAL ERROR: std::vsnprintf FAILED");
00129             else
00130             {
00131                 buffer[len]='\0';
00132                 if (len>size) this->append("String::_set_msg: INTERNAL ERROR: std::vsnprintf MESSAGE TRUNCATED: ");
00133                 this->append(buffer);
00134             }
00135             return len;
00136         }
00137     }; // class String
00138 
00139 #endif // (do/do not) USE_WXSTRING
00140 
00141 #endif // MECHSYS_STRING_H
00142 
00143 // vim:fdm=marker

Generated on Wed Jan 24 15:56:27 2007 for MechSys by  doxygen 1.4.7