GDCM 2.0.17
|
00001 /*========================================================================= 00002 00003 Program: GDCM (Grassroots DICOM). A DICOM library 00004 Module: $URL$ 00005 00006 Copyright (c) 2006-2010 Mathieu Malaterre 00007 All rights reserved. 00008 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 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 /*GDCM_EXPORT*/ String : public std::string /* PLEASE do not export me */ 00033 { 00034 // UI wants \0 for pad character, while ASCII ones wants space char... do not allow anything else 00035 GDCM_STATIC_ASSERT( TPadChar == ' ' || TPadChar == 0 ); 00036 00037 public: 00038 // typedef are not inherited: 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 // We are being passed a const char* pointer, so s[n] == 0 (garanteed!) 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 // FIXME: some users might already have padded the string 's' with a trailing \0... 00071 if( size() % 2 ) 00072 { 00073 push_back( TPadChar ); 00074 } 00075 } 00076 00078 operator const char *() const { return this->c_str(); } 00079 00081 bool IsValid() const { 00082 // Check Length: 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; // copy 00091 str.resize( TMaxLength ); 00092 return str; 00093 } 00094 00097 std::string Trim() const { 00098 std::string str = *this; // copy 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 // no such thing as std::get where the delim char would be left, so I need to manually add it back... 00114 // hopefully this is the right thing to do (no overhead) 00115 is.putback( TDelimiter ); 00116 } 00117 return is; 00118 } 00119 //template <char TDelimiter = EOF, unsigned int TMaxLength = 64, char TPadChar = ' '> 00120 //String String::Trim() const 00121 //{ 00122 // String s; 00123 // return s; 00124 //} 00125 00126 } // end namespace gdcm 00127 00128 #endif //GDCMSTRING_H