cgma
CubitString.hpp
Go to the documentation of this file.
00001 //- Class: CubitString
00002 //- 
00003 //- Description: This file defines the CubitString class which is a 
00004 //- simple implementation of a character string. Basic functionality 
00005 //- is provided as well as equality/inequality tests, subscripting into 
00006 //- a string, searching a string for another string.
00007 //-
00008 
00009 #if !defined(CUBITSTRING_HPP)
00010 #define CUBITSTRING_HPP
00011 
00012 #include <sstream>
00013 #include <vector>
00014 #include <string>
00015 
00016 #include "CGMUtilConfigure.h"
00017 
00018 // GCC compiler checking of format arguments
00019 // Note:  For class member functions, the first argument is the
00020 //        implicit 'this' pointer, so if the format string is the
00021 //        first explicit argument A should be 2, not 1.
00022 #ifdef __GNUC__
00023 # define CUBIT_STRING_PRINTF_FORMAT(A,B) __attribute__ ((format (printf, A, B)))
00024 #else
00025 # define CUBIT_STRING_PRINTF_FORMAT(A,B)
00026 #endif
00027 
00028 #if defined(_MSC_VER)
00029 #pragma warning(push)
00030 #pragma warning(disable : 4251)  // hide warnings about template dll exports
00031 #endif
00032 
00034 class CUBIT_UTIL_EXPORT CubitString
00035 {
00036 public:
00038   static const size_t npos;
00039 
00040   enum CaseSensitivity
00041   {
00042     CaseInsensitive,
00043     CaseSensitive
00044   };
00045 
00047   CubitString();
00048 
00050   ~CubitString();
00051 
00053   CubitString(const CubitString& s);
00054 
00056   CubitString(const char* s);
00057 
00058   //- Create a string from a std::string
00059   CubitString(const std::string& s);
00060 
00061   CubitString(int i,char c);
00062   //- Create a string from a char
00063 
00065   template <typename T>
00066   static CubitString number(const T& i)
00067   {
00068     std::stringstream si;
00069     si << i;
00070     return CubitString(si.str().c_str());
00071   }
00072 
00074   template <typename T>
00075   static T toNumber(const CubitString& str, bool *ok = NULL)
00076   {
00077     std::istringstream ss(str.c_str());
00078     T result;
00079     bool temp_ok = ss >> result;
00080     if(ok)
00081       (*ok) = temp_ok;
00082     return result;
00083   }
00084 
00090   static CubitString number(double f, unsigned int s_length = 0, unsigned int sig_digits = 0);
00091 
00093   static CubitString format(const char* fmt, ...) CUBIT_STRING_PRINTF_FORMAT(1,2);
00094 
00095   CubitString& operator=(const CubitString& s);
00096   
00097   CubitString& operator+=(const CubitString& s);
00098 
00099   CUBIT_UTIL_EXPORT friend CubitString operator+(const CubitString& s1, const CubitString& s2);
00100 
00101   CUBIT_UTIL_EXPORT friend bool operator<=(const CubitString&, const CubitString&);
00102   CUBIT_UTIL_EXPORT friend bool operator>=(const CubitString&, const CubitString&);
00103   CUBIT_UTIL_EXPORT friend bool operator<(const CubitString&, const CubitString&);
00104   CUBIT_UTIL_EXPORT friend bool operator>(const CubitString&, const CubitString&);
00105   
00106   bool operator==(const CubitString &s) const;
00107   bool operator!=(const CubitString &s) const;
00108   //- Predicates: Compare equality/inequality with other CubitStrings or
00109   //- with pointers to a list of characters.
00110 
00113   bool compare(const CubitString& s, CaseSensitivity cs = CaseSensitive);
00114   // note case insensitive comparison with unicode is not yet supported
00115 
00117   char get_at(size_t pos) const;
00119   void put_at(size_t pos, char c);
00120 
00122   CubitString substr(size_t first, size_t count = CubitString::npos) const;
00123 
00125   void erase(size_t pos, size_t len = npos);
00126 
00128   void replace(size_t pos, size_t len, const CubitString& str);
00129 
00131   void replace(const CubitString& to_find, const CubitString& to_replace);
00132 
00134   void to_lower();
00135   static void to_lower(char *string);
00137   void to_upper();
00138   static void to_upper(char *string);
00139 
00141   void trim();
00142 
00145   void simplify();
00146 
00148   void tokenize( char delimiter, std::vector<CubitString> &strings ) const;
00149 
00150   
00152   size_t find(const CubitString& s, size_t pos = 0) const;
00153   size_t find_first_of(const CubitString& s, size_t pos = 0) const;
00154   size_t find_first(char c, size_t pos = 0) const;
00155   size_t find_last (char c, size_t pos = CubitString::npos) const;
00156 
00157 
00159   bool ends_with(const CubitString& str) const;
00161   bool starts_with(const CubitString& str) const;
00162 
00164   bool is_empty() const;
00165 
00167   size_t length() const;
00168 
00170   const char *c_str() const;
00171   
00173   const std::string& str() const;
00174 
00176   void clear();
00177 
00179   void resize(size_t n);
00181   void resize(size_t n, char c);
00182 
00183   // UTF-16/UTF-8 unicode conversions to support using Unicode aware Windows APIs.
00184   // Note: wchar_t on non-Windows platforms is UTF-32
00185 #if defined(_WIN32)
00186   static std::wstring toUtf16(const char* str);
00187   static std::wstring toUtf16(const CubitString& str);
00188   static CubitString toUtf8(const wchar_t* str);
00189 #endif
00190 
00191 #if defined(_MSC_VER)
00192   static std::wstring toNative(const char* str);
00193   static std::wstring toNative(const CubitString& str);
00194 #else
00195   static CubitString toNative(const char* str);
00196   static CubitString toNative(const CubitString& str);
00197 #endif
00198 
00199   static std::wstring toWide(const CubitString& str);
00200   static CubitString toNarrow(const std::wstring& str);
00201   
00202 private:
00203   std::string rep;
00204 };
00205 
00206 #if defined(_MSC_VER)
00207 #pragma warning(pop)
00208 #endif
00209 
00210 #endif
00211 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines