gdcmString.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef __gdcmString_h
00016 #define __gdcmString_h
00017
00018 #include "gdcmTypes.h"
00019 #include "gdcmStaticAssert.h"
00020
00021 namespace gdcm
00022 {
00023 template <char TDelimiter, unsigned int TMaxLength, char TPadChar> class String;
00024 template <char TDelimiter, unsigned int TMaxLength, char TPadChar> std::istream& operator>>(std::istream &is, String<TDelimiter,TMaxLength,TPadChar>& ms);
00025
00033 template <char TDelimiter = '\\', unsigned int TMaxLength = 64, char TPadChar = ' '>
00034 class String : public std::string
00035 {
00036
00037 GDCM_STATIC_ASSERT( TPadChar == ' ' || TPadChar == 0 );
00038
00039 friend std::istream& operator>> <TDelimiter>(std::istream &is, String<TDelimiter>& ms);
00040 public:
00041
00042 typedef std::string::value_type value_type;
00043 typedef std::string::pointer pointer;
00044 typedef std::string::reference reference;
00045 typedef std::string::const_reference const_reference;
00046 typedef std::string::size_type size_type;
00047 typedef std::string::difference_type difference_type;
00048 typedef std::string::iterator iterator;
00049 typedef std::string::const_iterator const_iterator;
00050 typedef std::string::reverse_iterator reverse_iterator;
00051 typedef std::string::const_reverse_iterator const_reverse_iterator;
00052
00054 String(): std::string() {}
00055 String(const value_type* s): std::string(s)
00056 {
00057 if( size() % 2 )
00058 {
00059 push_back( TPadChar );
00060 }
00061 }
00062 String(const value_type* s, size_type n): std::string(s, n)
00063 {
00064
00065 if( n % 2 )
00066 {
00067 push_back( TPadChar );
00068 }
00069 }
00070 String(const std::string& s, size_type pos=0, size_type n=npos):
00071 std::string(s, pos, n)
00072 {
00073
00074 if( size() % 2 )
00075 {
00076 push_back( TPadChar );
00077 }
00078 }
00079
00081 operator const char *() { return this->c_str(); }
00082
00084 bool IsValid() const {
00085
00086 size_type l = size();
00087 if( l > TMaxLength ) return false;
00088 return true;
00089 }
00090
00091 gdcm::String<TDelimiter, TMaxLength, TPadChar> Truncate() const {
00092 if( IsValid() ) return *this;
00093 std::string str = *this;
00094 str.resize( TMaxLength );
00095 return str;
00096 }
00097
00100 std::string Trim() const {
00101 std::string str = *this;
00102 std::string::size_type pos1 = str.find_first_not_of(' ');
00103 std::string::size_type pos2 = str.find_last_not_of(' ');
00104 str = str.substr( (pos1 == std::string::npos) ? 0 : pos1,
00105 (pos2 == std::string::npos) ? (str.size() - 1) : (pos2 - pos1 + 1));
00106 return str;
00107 }
00108
00109 };
00110 template <char TDelimiter, unsigned int TMaxLength, char TPadChar>
00111 inline std::istream& operator>>(std::istream &is, String<TDelimiter,TMaxLength,TPadChar> &ms)
00112 {
00113 if(is)
00114 {
00115 std::getline(is, ms, TDelimiter);
00116
00117
00118 is.putback( TDelimiter );
00119 }
00120 return is;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129 }
00130
00131 #endif //__gdcmString_h
00132