GDCM: gdcmString.h Source File

GDCM 3.2.8

gdcmString.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: GDCM (Grassroots DICOM). A DICOM library
4
5 Copyright (c) 2006-2011 Mathieu Malaterre
6 All rights reserved.
7 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notice for more information.
12
13=========================================================================*/
14#ifndef GDCMSTRING_H
15#define GDCMSTRING_H
16
17#include "gdcmTypes.h"
18#include "gdcmStaticAssert.h"
19
20namespace gdcm
21{
22
30template <char TDelimiter = '\\', unsigned int TMaxLength = 64, char TPadChar = ' '>
31 class /*GDCM_EXPORT*/ String : public std::string /* PLEASE do not export me */
32{
33 // UI wants \0 for pad character, while ASCII ones wants space char... do not allow anything else
34 GDCM_STATIC_ASSERT( TPadChar == ' ' || TPadChar == 0 );
35
36public:
37 // typedef are not inherited:
38 typedef std::string::value_type value_type;
39 typedef std::string::pointer pointer;
40 typedef std::string::reference reference;
41 typedef std::string::const_reference const_reference;
42 typedef std::string::size_type size_type;
43 typedef std::string::difference_type difference_type;
44 typedef std::string::iterator iterator;
45 typedef std::string::const_iterator const_iterator;
46 typedef std::string::reverse_iterator reverse_iterator;
47 typedef std::string::const_reverse_iterator const_reverse_iterator;
48
50 String(): std::string() {}
51 String(const value_type* s): std::string(s)
52 {
53 if( size() % 2 )
54 {
55 push_back( TPadChar );
56 }
57 }
58 String(const value_type* s, size_type n): std::string(s, n)
59 {
60 // We are being passed a const char* pointer, so s[n] == 0 (guaranteed!)
61 if( n % 2 )
62 {
63 push_back( TPadChar );
64 }
65 }
66 String(const std::string& s, size_type pos=0, size_type n=npos):
67 std::string(s, pos, n)
68 {
69 // FIXME: some users might already have padded the string 's' with a trailing \0...
70 if( size() % 2 )
71 {
72 push_back( TPadChar );
73 }
74 }
75
77 operator const char *() const { return this->c_str(); }
78
80 bool IsValid() const {
81 // Check Length:
82 size_type l = size();
83 if( l > TMaxLength ) return false;
84 return true;
85 }
86
87 gdcm::String<TDelimiter, TMaxLength, TPadChar> Truncate() const {
88 if( IsValid() ) return *this;
89 std::string str = *this; // copy
90 str.resize( TMaxLength );
91 return str;
92 }
93
96 std::string Trim() const {
97 std::string str = *this; // copy
98 std::string::size_type pos1 = str.find_first_not_of(' ');
99 std::string::size_type pos2 = str.find_last_not_of(' ');
100 str = str.substr( (pos1 == std::string::npos) ? 0 : pos1,
101 (pos2 == std::string::npos) ? (str.size() - 1) : (pos2 - pos1 + 1));
102 return str;
103 }
104
105 static std::string Trim(const char *input) {
106 if( !input ) return "";
107 std::string str = input;
108 std::string::size_type pos1 = str.find_first_not_of(' ');
109 std::string::size_type pos2 = str.find_last_not_of(' ');
110 str = str.substr( (pos1 == std::string::npos) ? 0 : pos1,
111 (pos2 == std::string::npos) ? (str.size() - 1) : (pos2 - pos1 + 1));
112 return str;
113 }
114};
115template <char TDelimiter, unsigned int TMaxLength, char TPadChar>
116 inline std::istream& operator>>(std::istream &is, String<TDelimiter,TMaxLength,TPadChar> &ms)
117{
118 if(is)
119 {
120 std::getline(is, ms, TDelimiter);
121 // no such thing as std::get where the delim char would be left, so I need to manually add it back...
122 // hopefully this is the right thing to do (no overhead)
123 if( !is.eof() ) is.putback( TDelimiter );
124 }
125 return is;
126}
127//template <char TDelimiter = EOF, unsigned int TMaxLength = 64, char TPadChar = ' '>
128//String String::Trim() const
129//{
130// String s;
131// return s;
132//}
133
134} // end namespace gdcm
135
136#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