gdcmString.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program: GDCM (Grassroots DICOM). A DICOM library
00004   Module:  $URL$
00005 
00006   Copyright (c) 2006-2009 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 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 /*GDCM_EXPORT*/ String : public std::string /* PLEASE do not export me */
00035 {
00036   // UI wants \0 for pad character, while ASCII ones wants space char... do not allow anything else
00037   GDCM_STATIC_ASSERT( TPadChar == ' ' || TPadChar == 0 );
00038 
00039   friend std::istream& operator>> <TDelimiter>(std::istream &is, String<TDelimiter>& ms);
00040 public:
00041   // typedef are not inherited:
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   // We are being passed a const char* pointer, so s[n] == 0 (garanteed!)
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   // FIXME: some users might already have padded the string 's' with a trailing \0...
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     // Check Length:
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; // copy
00094     str.resize( TMaxLength );
00095     return str;
00096   }
00097 
00100   std::string Trim() const {
00101     std::string str = *this; // copy
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     // no such thing as std::get where the delim char would be left, so I need to manually add it back...
00117     // hopefully this is the right thing to do (no overhead)
00118     is.putback( TDelimiter );
00119     }
00120   return is;
00121 }
00122 //template <char TDelimiter = EOF, unsigned int TMaxLength = 64, char TPadChar = ' '>
00123 //String String::Trim() const
00124 //{
00125 //  String s;
00126 //  return s;
00127 //}
00128 
00129 } // end namespace gdcm
00130 
00131 #endif //__gdcmString_h
00132 

Generated on Thu Jul 1 06:40:26 2010 for GDCM by doxygen 1.6.3
SourceForge.net Logo