gdcmString.h
Go to the documentation of this file.
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifndef GDCMSTRING_H
#define GDCMSTRING_H
#include "gdcmTypes.h"
#include "gdcmStaticAssert.h"
namespace gdcm
{
template <char TDelimiter = '\\', unsigned int TMaxLength = 64, char TPadChar = ' '>
class /*GDCM_EXPORT*/ String : public std::string /* PLEASE do not export me */
{
// UI wants \0 for pad character, while ASCII ones wants space char... do not allow anything else
GDCM_STATIC_ASSERT( TPadChar == ' ' || TPadChar == 0 );
public:
// typedef are not inherited:
typedef std::string::value_type value_type;
typedef std::string::pointer pointer;
typedef std::string::reference reference;
typedef std::string::const_reference const_reference;
typedef std::string::size_type size_type;
typedef std::string::difference_type difference_type;
typedef std::string::iterator iterator;
typedef std::string::const_iterator const_iterator;
typedef std::string::reverse_iterator reverse_iterator;
typedef std::string::const_reverse_iterator const_reverse_iterator;
String(): std::string() {}
String(const value_type* s): std::string(s)
{
if( size() % 2 )
{
push_back( TPadChar );
}
}
String(const value_type* s, size_type n): std::string(s, n)
{
// We are being passed a const char* pointer, so s[n] == 0 (guaranteed!)
if( n % 2 )
{
push_back( TPadChar );
}
}
String(const std::string& s, size_type pos=0, size_type n=npos):
std::string(s, pos, n)
{
// FIXME: some users might already have padded the string 's' with a trailing \0...
if( size() % 2 )
{
push_back( TPadChar );
}
}
operator const char *() const { return this->c_str(); }
bool IsValid() const {
// Check Length:
size_type l = size();
if( l > TMaxLength ) return false;
return true;
}
gdcm::String<TDelimiter, TMaxLength, TPadChar> Truncate() const {
if( IsValid() ) return *this;
std::string str = *this; // copy
str.resize( TMaxLength );
return str;
}
std::string Trim() const {
std::string str = *this; // copy
std::string::size_type pos1 = str.find_first_not_of(' ');
std::string::size_type pos2 = str.find_last_not_of(' ');
str = str.substr( (pos1 == std::string::npos) ? 0 : pos1,
(pos2 == std::string::npos) ? (str.size() - 1) : (pos2 - pos1 + 1));
return str;
}
static std::string Trim(const char *input) {
if( !input ) return "";
std::string str = input;
std::string::size_type pos1 = str.find_first_not_of(' ');
std::string::size_type pos2 = str.find_last_not_of(' ');
str = str.substr( (pos1 == std::string::npos) ? 0 : pos1,
(pos2 == std::string::npos) ? (str.size() - 1) : (pos2 - pos1 + 1));
return str;
}
};
template <char TDelimiter, unsigned int TMaxLength, char TPadChar>
inline std::istream& operator>>(std::istream &is, String<TDelimiter,TMaxLength,TPadChar> &ms)
{
if(is)
{
std::getline(is, ms, TDelimiter);
// no such thing as std::get where the delim char would be left, so I need to manually add it back...
// hopefully this is the right thing to do (no overhead)
if( !is.eof() ) is.putback( TDelimiter );
}
return is;
}
//template <char TDelimiter = EOF, unsigned int TMaxLength = 64, char TPadChar = ' '>
//String String::Trim() const
//{
// String s;
// return s;
//}
} // end namespace gdcm
#endif //GDCMSTRING_H
gdcm::String
String.
Definition gdcmString.h:32
gdcm::String::String
String(const std::string &s, size_type pos=0, size_type n=npos)
Definition gdcmString.h:66
gdcm::String::String
String(const value_type *s, size_type n)
Definition gdcmString.h:58
gdcm::String::value_type
std::string::value_type value_type
Definition gdcmString.h:38
gdcm::String::size_type
std::string::size_type size_type
Definition gdcmString.h:42
gdcm::String::Trim
static std::string Trim(const char *input)
Definition gdcmString.h:105
gdcm::String::Trim
std::string Trim() const
Definition gdcmString.h:96
gdcm::String::const_reference
std::string::const_reference const_reference
Definition gdcmString.h:41
gdcm::String::Truncate
gdcm::String< TDelimiter, TMaxLength, TPadChar > Truncate() const
Definition gdcmString.h:87
gdcm::String::IsValid
bool IsValid() const
return if string is valid
Definition gdcmString.h:80
gdcm::String::String
String(const value_type *s)
Definition gdcmString.h:51
gdcm::String::String
String()
String constructors.
Definition gdcmString.h:50
gdcm::String::iterator
std::string::iterator iterator
Definition gdcmString.h:44
gdcm::String::pointer
std::string::pointer pointer
Definition gdcmString.h:39
gdcm::String::reverse_iterator
std::string::reverse_iterator reverse_iterator
Definition gdcmString.h:46
gdcm::String::difference_type
std::string::difference_type difference_type
Definition gdcmString.h:43
gdcm::String::const_reverse_iterator
std::string::const_reverse_iterator const_reverse_iterator
Definition gdcmString.h:47
gdcm::String::const_iterator
std::string::const_iterator const_iterator
Definition gdcmString.h:45
gdcm::String::reference
std::string::reference reference
Definition gdcmString.h:40
gdcmStaticAssert.h
GDCM_STATIC_ASSERT
#define GDCM_STATIC_ASSERT(B)
The GDCM_JOIN + LINE is needed to create a uniq identifier.
Definition gdcmStaticAssert.h:36
gdcmTypes.h
gdcm
Definition gdcmASN1.h:21
gdcm::operator>>
std::istream & operator>>(std::istream &is, String< TDelimiter, TMaxLength, TPadChar > &ms)
Definition gdcmString.h:116