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
00031 template <char TDelimiter = '\\', unsigned int TMaxLength = 64, char TPadChar = ' '>
00032 class String : public std::string
00033 {
00034
00035 GDCM_STATIC_ASSERT( TPadChar == ' ' || TPadChar == 0 );
00036
00037 public:
00038
00039 typedef std::string::value_type value_type;
00040 typedef std::string::pointer pointer;
00041 typedef std::string::reference reference;
00042 typedef std::string::const_reference const_reference;
00043 typedef std::string::size_type size_type;
00044 typedef std::string::difference_type difference_type;
00045 typedef std::string::iterator iterator;
00046 typedef std::string::const_iterator const_iterator;
00047 typedef std::string::reverse_iterator reverse_iterator;
00048 typedef std::string::const_reverse_iterator const_reverse_iterator;
00049
00051 String(): std::string() {}
00052 String(const value_type* s): std::string(s)
00053 {
00054 if( size() % 2 )
00055 {
00056 push_back( TPadChar );
00057 }
00058 }
00059 String(const value_type* s, size_type n): std::string(s, n)
00060 {
00061
00062 if( n % 2 )
00063 {
00064 push_back( TPadChar );
00065 }
00066 }
00067 String(const std::string& s, size_type pos=0, size_type n=npos):
00068 std::string(s, pos, n)
00069 {
00070
00071 if( size() % 2 )
00072 {
00073 push_back( TPadChar );
00074 }
00075 }
00076
00078 operator const char *() { return this->c_str(); }
00079
00081 bool IsValid() const {
00082
00083 size_type l = size();
00084 if( l > TMaxLength ) return false;
00085 return true;
00086 }
00087
00088 gdcm::String<TDelimiter, TMaxLength, TPadChar> Truncate() const {
00089 if( IsValid() ) return *this;
00090 std::string str = *this;
00091 str.resize( TMaxLength );
00092 return str;
00093 }
00094
00097 std::string Trim() const {
00098 std::string str = *this;
00099 std::string::size_type pos1 = str.find_first_not_of(' ');
00100 std::string::size_type pos2 = str.find_last_not_of(' ');
00101 str = str.substr( (pos1 == std::string::npos) ? 0 : pos1,
00102 (pos2 == std::string::npos) ? (str.size() - 1) : (pos2 - pos1 + 1));
00103 return str;
00104 }
00105
00106 };
00107 template <char TDelimiter, unsigned int TMaxLength, char TPadChar>
00108 inline std::istream& operator>>(std::istream &is, String<TDelimiter,TMaxLength,TPadChar> &ms)
00109 {
00110 if(is)
00111 {
00112 std::getline(is, ms, TDelimiter);
00113
00114
00115 is.putback( TDelimiter );
00116 }
00117 return is;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126 }
00127
00128 #endif //GDCMSTRING_H
00129