MboTString.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 * Copyright (c) 2001 - 2008 Marcus Boerger.  All rights reserved.
00003 *
00004 * This library is free software; you can redistribute it and/or
00005 * modify it under the terms of the GNU Lesser General Public
00006 * License as published by the Free Software Foundation; either
00007 * version 2.1 of the License, or (at your option) any later version.
00008 *
00009 * This library is distributed in the hope that it will be useful,
00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 * Lesser General Public License for more details.
00013 *
00014 * You should have received a copy of the GNU Lesser General Public
00015 * License along with this library; if not, write to the Free Software
00016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00017 * ========================================================================= */
00018 
00019 /* ------------------------------------------------------------------------ */
00020 /* Name:      MboTString.h / MboTString.cpp
00021  *
00022  * Requires:  
00023  * - Mbo.h
00024  */
00039 /* ------------------------------------------------------------------------ */
00040 
00041 #ifndef _MBOTSTRING_H
00042 #define _MBOTSTRING_H
00043 
00044 #include "Mbo.h"
00045 #include "MboDebugTrace.h"
00046 #include <locale>
00047 #include <string>
00048 #include <stdexcept>
00049 #include <stdlib.h>
00050 #if _MSC_VER
00051 # include <tchar.h>
00052 #endif
00053 
00054 namespace mbo
00055 {
00056 
00057 #ifndef _TCHAR_DEFINED
00058 # define _TCHAR_DEFINED
00059 typedef char TCHAR;
00060 #endif
00061 
00062 typedef std::basic_string<TCHAR> tstring; // ts
00063 
00064 #ifndef MBO_STRING_DEFINED
00065 #define MBO_STRING_DEFINED
00066 
00067     typedef mbo::tstring MboString; /* should equal std::string */
00068 
00069 #endif // MBO_STRING_DEFINED
00070 
00071 // Work around Mircosoft MSC.6 STL incompatibility
00072 #if defined(_MSC_VER) && _MSC_VER < 1300
00073 # define __USE_FACET , NULL, 1
00074 #else
00075 # define __USE_FACET
00076 #endif
00077 
00078 #ifdef _MT
00079 #define MBO_MT_LOCAL_OR_STATIC
00080 #else
00081 #define MBO_MT_LOCAL_OR_STATIC static
00082 #endif
00083 
00084 #ifdef _STLPORT_VERSION
00085 template<class _E>
00086 inline const std::ctype& get_facet()
00087 {
00088     MBO_MT_LOCAL_OR_STATIC const std::ctype& facet = std::use_facet<std::ctype>(std::locale::empty());
00089     return facet;
00090 }
00091 #else
00092 template<class _E>
00093 inline const std::ctype<_E>& get_facet()
00094 {
00095     MBO_MT_LOCAL_OR_STATIC const std::ctype<_E>& facet = std::use_facet<std::ctype<_E> >(std::locale::empty() __USE_FACET);
00096     return facet;
00097 }
00098 #endif
00099 
00100 template<class _E, class _Tr, class _A>
00101 inline std::basic_string<_E, _Tr, _A> StrCreate(_IN const char *sz)
00102 {
00103     const std::ctype<_E>& facet = get_facet<_E>();
00104 
00105     std::basic_string<_E, _Tr, _A> str;
00106 
00107     while (*sz)
00108     {
00109         str.append(1, facet.widen(*sz++));
00110     }
00111     return str;
00112 }
00113 
00114 template<>
00115 inline std::basic_string<char> StrCreate(_IN const char *sz)
00116 {
00117     return std::basic_string<char>(sz);
00118 }
00119 
00120 MBO_API mbo::tstring TStrCreate(_IN const char *sz);
00121 
00122 #define _TS(text) mbo::TStrCreate(text)
00123 
00124 class string_conversion_error: public mbo::runtime_error
00125 {
00126 public:
00127     string_conversion_error()
00128         : mbo::runtime_error("String cannot be converted")
00129     {
00130     }
00131 };
00132 
00133 MBO_API std::basic_string<char> WStrToStr(_IN const std::basic_string<wchar_t> & wstr);
00134 
00135 MBO_API std::basic_string<wchar_t> StrToWStr(_IN const std::basic_string<char> & str);
00136 
00137 template<typename _ValType>
00138 tstring TStrFrom(_IN const _ValType & val)
00139 {
00140     return StrFrom<tstring::value_type, _ValType>(val);
00141 }
00142 
00143 }; // namespace mbo
00144 
00145 #endif // _MBOTSTRING_H

  Hosted on code.google.com  
© Marcus Börger
Generated on Fri Jan 18 21:21:08 2008 for MBO-lib by doxygen 1.5.4