GDCM
2.0.18
|
00001 /*========================================================================= 00002 00003 Program: GDCM (Grassroots DICOM). A DICOM library 00004 00005 Copyright (c) 2006-2011 Mathieu Malaterre 00006 All rights reserved. 00007 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details. 00008 00009 This software is distributed WITHOUT ANY WARRANTY; without even 00010 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00011 PURPOSE. See the above copyright notice for more information. 00012 00013 =========================================================================*/ 00014 #ifndef GDCMSTRING_H 00015 #define GDCMSTRING_H 00016 00017 #include "gdcmTypes.h" 00018 #include "gdcmStaticAssert.h" 00019 00020 namespace gdcm 00021 { 00022 00030 template <char TDelimiter = '\\', unsigned int TMaxLength = 64, char TPadChar = ' '> 00031 class /*GDCM_EXPORT*/ String : public std::string /* PLEASE do not export me */ 00032 { 00033 // UI wants \0 for pad character, while ASCII ones wants space char... do not allow anything else 00034 GDCM_STATIC_ASSERT( TPadChar == ' ' || TPadChar == 0 ); 00035 00036 public: 00037 // typedef are not inherited: 00038 typedef std::string::value_type value_type; 00039 typedef std::string::pointer pointer; 00040 typedef std::string::reference reference; 00041 typedef std::string::const_reference const_reference; 00042 typedef std::string::size_type size_type; 00043 typedef std::string::difference_type difference_type; 00044 typedef std::string::iterator iterator; 00045 typedef std::string::const_iterator const_iterator; 00046 typedef std::string::reverse_iterator reverse_iterator; 00047 typedef std::string::const_reverse_iterator const_reverse_iterator; 00048 00050 String(): std::string() {} 00051 String(const value_type* s): std::string(s) 00052 { 00053 if( size() % 2 ) 00054 { 00055 push_back( TPadChar ); 00056 } 00057 } 00058 String(const value_type* s, size_type n): std::string(s, n) 00059 { 00060 // We are being passed a const char* pointer, so s[n] == 0 (garanteed!) 00061 if( n % 2 ) 00062 { 00063 push_back( TPadChar ); 00064 } 00065 } 00066 String(const std::string& s, size_type pos=0, size_type n=npos): 00067 std::string(s, pos, n) 00068 { 00069 // FIXME: some users might already have padded the string 's' with a trailing \0... 00070 if( size() % 2 ) 00071 { 00072 push_back( TPadChar ); 00073 } 00074 } 00075 00077 operator const char *() const { return this->c_str(); } 00078 00080 bool IsValid() const { 00081 // Check Length: 00082 size_type l = size(); 00083 if( l > TMaxLength ) return false; 00084 return true; 00085 } 00086 00087 gdcm::String<TDelimiter, TMaxLength, TPadChar> Truncate() const { 00088 if( IsValid() ) return *this; 00089 std::string str = *this; // copy 00090 str.resize( TMaxLength ); 00091 return str; 00092 } 00093 00096 std::string Trim() const { 00097 std::string str = *this; // copy 00098 std::string::size_type pos1 = str.find_first_not_of(' '); 00099 std::string::size_type pos2 = str.find_last_not_of(' '); 00100 str = str.substr( (pos1 == std::string::npos) ? 0 : pos1, 00101 (pos2 == std::string::npos) ? (str.size() - 1) : (pos2 - pos1 + 1)); 00102 return str; 00103 } 00104 00105 }; 00106 template <char TDelimiter, unsigned int TMaxLength, char TPadChar> 00107 inline std::istream& operator>>(std::istream &is, String<TDelimiter,TMaxLength,TPadChar> &ms) 00108 { 00109 if(is) 00110 { 00111 std::getline(is, ms, TDelimiter); 00112 // no such thing as std::get where the delim char would be left, so I need to manually add it back... 00113 // hopefully this is the right thing to do (no overhead) 00114 is.putback( TDelimiter ); 00115 } 00116 return is; 00117 } 00118 //template <char TDelimiter = EOF, unsigned int TMaxLength = 64, char TPadChar = ' '> 00119 //String String::Trim() const 00120 //{ 00121 // String s; 00122 // return s; 00123 //} 00124 00125 } // end namespace gdcm 00126 00127 #endif //GDCMSTRING_H