OFFIS DCMTK  Version 3.6.0
ofstring.h
1 /*
2  *
3  * Copyright (C) 1997-2010, OFFIS e.V.
4  * All rights reserved. See COPYRIGHT file for details.
5  *
6  * This software and supporting documentation were developed by
7  *
8  * OFFIS e.V.
9  * R&D Division Health
10  * Escherweg 2
11  * D-26121 Oldenburg, Germany
12  *
13  *
14  * Module: ofstd
15  *
16  * Author: Andrew Hewett
17  *
18  * Purpose: A simple string class
19  *
20  * Last Update: $Author: joergr $
21  * Update Date: $Date: 2010-10-14 13:15:50 $
22  * CVS/RCS Revision: $Revision: 1.30 $
23  * Status: $State: Exp $
24  *
25  * CVS/RCS Log at end of file
26  *
27  */
28 
29 #ifndef OFSTRING_H
30 #define OFSTRING_H
31 
32 #include "dcmtk/config/osconfig.h" /* include OS specific configuration first */
33 
34 #include "dcmtk/ofstd/oftypes.h" /* for OFBool */
35 #include "dcmtk/ofstd/ofcast.h"
36 
37 
38 // makes sure that resulting C string is never NULL
39 #define OFSTRING_GUARD(c_string) ((c_string != NULL) ? (c_string) : "")
40 
41 
42 #ifdef HAVE_STD_STRING
43 /*
44 ** Use the ANSI Standard string class
45 */
46 
47 #include <string>
48 
49 #define OFString std::string
50 #define OFString_npos std::string::npos
51 
52 #else /* not HAVE_STD_STRING */
53 
54 /*
55 ** Declare our own string class
56 */
57 
58 #define INCLUDE_CASSERT
59 #define INCLUDE_CSTRING
60 #define INCLUDE_CSTDLIB
61 #define INCLUDE_LIBC
62 #define INCLUDE_UNISTD
63 #include "dcmtk/ofstd/ofstdinc.h"
64 
65 #include "dcmtk/ofstd/ofstream.h"
66 #include "dcmtk/ofstd/oftypes.h"
67 
68 /*
69 ** Error macros
70 */
71 #define OFSTRING_OUTOFRANGE(cond) assert (!(cond))
72 #define OFSTRING_LENGTHERROR(cond) assert (!(cond))
73 #define OFSTRING_MEMORYALLOCERROR(cond) assert (!(cond))
74 
80 static const size_t OFString_npos = (OFstatic_cast(size_t, -1));
81 
82 
86 class OFString
87 {
88 public:
89  /*
90  * The SunOS C++ 2.0.1 does not allow static const members.
91  * We would like to define:
92  * static const size_t npos = ((size_t)-1);
93  * but cannot so an alternative OFString_npos is defined outside
94  * the class (see above).
95  */
96 
99  OFString();
100 
110  OFString(const OFString& str, size_t pos = 0, size_t n = OFString_npos);
111 
120  OFString(const char* s, size_t n);
121 
126  OFString(const char* s);
127 
133  OFString(size_t rep, char c);
134 
137  ~OFString();
138 
143  OFString& operator=(const OFString& rhs);
144 
149  OFString& operator=(const char* s);
150 
155  OFString& operator=(char s);
156 
161  OFString& operator+=(const OFString& rhs);
162 
167  OFString& operator+=(const char* s);
168 
173  OFString& operator+=(char s);
174 
184  OFString& append(const OFString& str, size_t pos = 0, size_t n = OFString_npos);
185 
191  OFString& append(const char* s, size_t n);
192 
197  OFString& append(const char* s);
198 
204  OFString& append(size_t rep, char c);
205 
215  OFString& assign(const OFString& str, size_t pos, size_t n);
216 
221  OFString& assign(const OFString& str);
222 
228  OFString& assign(const char* s, size_t n);
229 
234  OFString& assign(const char* s);
235 
241  OFString& assign(size_t rep, char c);
242 
253  OFString& insert(size_t pos1, const OFString& str,
254  size_t pos2 = 0, size_t n = OFString_npos);
255 
263  OFString& insert(size_t pos, const char* s, size_t n);
264 
271  OFString& insert(size_t pos, const char* s);
272 
280  OFString& insert(size_t pos, size_t rep, char c);
281 
287  OFString& erase(size_t pos = 0, size_t n = OFString_npos);
288 
304  OFString& replace(size_t pos1, size_t n1, const OFString& str,
305  size_t pos2 = 0, size_t n2 = OFString_npos);
306 
315  OFString& replace(size_t pos, size_t n, const char* s, size_t n2);
316 
324  OFString& replace(size_t pos, size_t n, const char* s);
325 
334  OFString& replace(size_t pos, size_t n, size_t rep, char s);
335 
342  const char& at(size_t pos) const
343  {
344  OFSTRING_OUTOFRANGE (pos >= this->size());
345  return this->theCString[pos];
346  }
347 
354  char& at(size_t pos)
355  {
356  OFSTRING_OUTOFRANGE (pos >= this->size());
357  return this->theCString[pos];
358  }
359 
365  char operator[] (size_t pos) const
366  {
367  if (pos == this->size()) return '\0';
368  else
369  {
370  OFSTRING_OUTOFRANGE (pos > this->size());
371  return this->theCString[pos];
372  }
373  }
374 
381  char& operator[] (size_t pos)
382  {
383  OFSTRING_OUTOFRANGE (pos >= this->size());
384  return this->theCString[pos];
385  }
386 
392  const char* c_str() const
393  {
394  return (this->theCString)?(this->theCString):("");
395  }
396 
404  const char* data() const;
405 
410  size_t size() const
411  {
412  return this->theSize;
413  }
414 
419  size_t length() const
420  {
421  return this->size();
422  }
423 
427  OFBool empty() const
428  {
429  return (this->size() == 0)?(OFTrue):(OFFalse);
430  }
431 
437  void resize(size_t n, char c = '\0');
438 
442  size_t capacity() const
443  {
444  return this->theCapacity;
445  }
446 
450  size_t max_size() const
451  {
452  return ((OFString_npos - 1)/sizeof(char));
453  }
454 
457  void clear()
458  {
459  this->erase();
460  }
461 
470  void reserve(size_t res_arg);
471 
482  size_t copy(char* s, size_t n, size_t pos = 0) const;
483 
489  OFString substr(size_t pos = 0, size_t n = OFString_npos) const;
490 
495  void swap(OFString& s);
496 
507  int compare(const OFString& str) const;
508 
516  int compare(size_t pos1, size_t n1, const OFString& str) const;
517 
527  int compare(size_t pos1, size_t n1, const OFString& str, size_t pos2, size_t n2) const;
528 
534  int compare(const char* s) const;
535 
544  int compare(size_t pos1, size_t n1, const char* s, size_t n2 = OFString_npos) const;
545 
555  size_t find(const OFString& pattern, size_t pos = 0) const;
556 
567  size_t find(const char* pattern, size_t pos, size_t n) const;
568 
578  size_t find(const char* pattern, size_t pos = 0) const;
579 
589  size_t find(char pattern, size_t pos = 0) const;
590 
600  size_t rfind(const OFString& pattern, size_t pos = OFString_npos) const;
601 
612  size_t rfind(const char* pattern, size_t pos, size_t n) const;
613 
623  size_t rfind(const char* pattern, size_t pos = OFString_npos) const;
624 
634  size_t rfind(char pattern, size_t pos = OFString_npos) const;
635 
645  size_t find_first_of(const OFString& str, size_t pos = 0) const;
646 
657  size_t find_first_of(const char* s, size_t pos, size_t n) const;
658 
668  size_t find_first_of(const char* s, size_t pos = 0) const;
669 
679  size_t find_first_of(char s, size_t pos = 0) const;
680 
689  size_t find_last_of(const OFString& str, size_t pos = OFString_npos) const;
690 
700  size_t find_last_of(const char* s, size_t pos, size_t n) const;
701 
710  size_t find_last_of(const char* s, size_t pos = OFString_npos) const;
711 
720  size_t find_last_of(char s, size_t pos = OFString_npos) const;
721 
730  size_t find_first_not_of(const OFString& str, size_t pos = 0) const;
731 
741  size_t find_first_not_of(const char* s, size_t pos, size_t n) const;
742 
751  size_t find_first_not_of(const char* s, size_t pos = 0) const;
752 
761  size_t find_first_not_of(char c, size_t pos = 0) const;
762 
772  size_t find_last_not_of(const OFString& str, size_t pos = OFString_npos) const;
773 
784  size_t find_last_not_of(const char* s, size_t pos, size_t n) const;
785 
795  size_t find_last_not_of(const char* s, size_t pos = OFString_npos) const;
796 
806  size_t find_last_not_of(char c, size_t pos = OFString_npos) const;
807 
809  typedef size_t size_type;
810 
812  typedef char value_type;
813 
818  typedef const char* iterator;
819 
822 
826  iterator begin() const { return theCString; }
827 
831  iterator end() const { return begin() + length(); }
832 
833 private:
835  char* theCString;
836 
838  size_t theSize;
839 
841  size_t theCapacity;
842 
843 };
844 
845 
851 STD_NAMESPACE ostream& operator<< (STD_NAMESPACE ostream& o, const OFString& s);
852 
859 STD_NAMESPACE istream& operator>> (STD_NAMESPACE istream& i, OFString& s);
860 
866 OFString operator+ (const OFString& lhs, const OFString& rhs);
867 
873 OFString operator+ (const char* lhs, const OFString& rhs);
874 
880 OFString operator+ (char lhs, const OFString& rhs);
881 
887 OFString operator+ (const OFString& lhs, const char* rhs);
888 
894 OFString operator+ (const OFString& lhs, char rhs);
895 
901 OFBool operator== (const OFString& lhs, const OFString& rhs);
902 
908 OFBool operator== (const char* lhs, const OFString& rhs);
909 
915 OFBool operator== (char lhs, const OFString& rhs);
916 
922 OFBool operator== (const OFString& lhs, const char* rhs);
923 
929 OFBool operator== (const OFString& lhs, char rhs);
930 
936 OFBool operator< (const OFString& lhs, const OFString& rhs);
937 
943 OFBool operator< (const char* lhs, const OFString& rhs);
944 
950 OFBool operator< (char lhs, const OFString& rhs);
951 
957 OFBool operator< (const OFString& lhs, const char* rhs);
958 
964 OFBool operator< (const OFString& lhs, char rhs);
965 
971 OFBool operator<= (const OFString& lhs, const OFString& rhs);
972 
978 OFBool operator<= (const char* lhs, const OFString& rhs);
979 
985 OFBool operator<= (char lhs, const OFString& rhs);
986 
992 OFBool operator<= (const OFString& lhs, const char* rhs);
993 
999 OFBool operator<= (const OFString& lhs, char rhs);
1000 
1006 OFBool operator!= (const OFString& lhs, const OFString& rhs);
1007 
1013 OFBool operator!= (const char* lhs, const OFString& rhs);
1014 
1020 OFBool operator!= (char lhs, const OFString& rhs);
1021 
1027 OFBool operator!= (const OFString& lhs, const char* rhs);
1028 
1034 OFBool operator!= (const OFString& lhs, char rhs);
1035 
1041 OFBool operator> (const OFString& lhs, const OFString& rhs);
1042 
1048 OFBool operator> (const char* lhs, const OFString& rhs);
1049 
1055 OFBool operator> (char lhs, const OFString& rhs);
1056 
1062 OFBool operator> (const OFString& lhs, const char* rhs);
1063 
1069 OFBool operator> (const OFString& lhs, char rhs);
1070 
1076 OFBool operator>= (const OFString& lhs, const OFString& rhs);
1077 
1083 OFBool operator>= (const char* lhs, const OFString& rhs);
1084 
1090 OFBool operator>= (char lhs, const OFString& rhs);
1091 
1097 OFBool operator>= (const OFString& lhs, const char* rhs);
1098 
1104 OFBool operator>= (const OFString& lhs, char rhs);
1105 
1106 #endif /* HAVE_STD_STRING */
1107 
1108 #endif /* OFSTRING_H */
1109 
1110 
1111 /*
1112 ** CVS/RCS Log:
1113 ** $Log: ofstring.h,v $
1114 ** Revision 1.30 2010-10-14 13:15:50 joergr
1115 ** Updated copyright header. Added reference to COPYRIGHT file.
1116 **
1117 ** Revision 1.29 2010-08-19 12:07:55 uli
1118 ** Made OFString follow the C++ standard for std::string::assign().
1119 **
1120 ** Revision 1.28 2010-07-26 07:31:17 joergr
1121 ** Fixed typo (and revised documentation on the OFSTRING_GUARD macro).
1122 **
1123 ** Revision 1.27 2010-07-21 14:25:10 joergr
1124 ** Introduced new guard macro that makes sure that a C string is never NULL.
1125 ** Useful when passing a C string to a OFString constructor or an output stream.
1126 **
1127 ** Revision 1.26 2010-04-26 12:22:30 uli
1128 ** Fixed a some minor doxygen warnings.
1129 **
1130 ** Revision 1.25 2009-09-28 14:07:34 joergr
1131 ** Introduced new member variable that stores the current length of the string.
1132 ** This yields in a significant performance improvement when compiled in debug
1133 ** mode.
1134 **
1135 ** Revision 1.24 2009-08-19 10:42:42 joergr
1136 ** Added iterator declarations and required methods.
1137 **
1138 ** Revision 1.23 2009-08-07 14:31:08 joergr
1139 ** Fixed incorrect implementation of find_first_not_of() and find_last_not_of().
1140 **
1141 ** Revision 1.22 2007/02/20 13:12:59 joergr
1142 ** Fixed wrong comment in compare() method.
1143 **
1144 ** Revision 1.21 2006/08/14 16:42:26 meichel
1145 ** Updated all code in module ofstd to correctly compile if the standard
1146 ** namespace has not included into the global one with a "using" directive.
1147 **
1148 ** Revision 1.20 2005/12/08 16:06:07 meichel
1149 ** Changed include path schema for all DCMTK header files
1150 **
1151 ** Revision 1.19 2004/08/03 11:45:42 meichel
1152 ** Headers libc.h and unistd.h are now included via ofstdinc.h
1153 **
1154 ** Revision 1.18 2004/01/16 10:30:12 joergr
1155 ** Removed acknowledgements with e-mail addresses from CVS log.
1156 **
1157 ** Revision 1.17 2003/08/07 11:44:55 joergr
1158 ** Slightly modified header comments to conform to doxygen syntax.
1159 **
1160 ** Revision 1.16 2003/07/09 13:57:43 meichel
1161 ** Adapted type casts to new-style typecast operators defined in ofcast.h
1162 **
1163 ** Revision 1.15 2003/07/04 13:31:51 meichel
1164 ** Fixed issues with compiling with HAVE_STD_STRING
1165 **
1166 ** Revision 1.14 2003/06/12 13:13:51 joergr
1167 ** Fixed inconsistent API documentation reported by Doxygen.
1168 **
1169 ** Revision 1.13 2002/11/27 11:23:06 meichel
1170 ** Adapted module ofstd to use of new header file ofstdinc.h
1171 **
1172 ** Revision 1.12 2002/04/16 13:36:03 joergr
1173 ** Added configurable support for C++ ANSI standard includes (e.g. streams).
1174 **
1175 ** Revision 1.11 2001/12/04 16:48:16 meichel
1176 ** Completed doc++ documentation, fixed bug in OFString::copy.
1177 **
1178 ** Revision 1.10 2001/11/02 13:18:53 meichel
1179 ** Removed character sequences that could be interpreted as ISO C++ trigraphs
1180 **
1181 ** Revision 1.9 2001/06/01 15:51:35 meichel
1182 ** Updated copyright header
1183 **
1184 ** Revision 1.8 2000/03/08 16:36:02 meichel
1185 ** Updated copyright header.
1186 **
1187 ** Revision 1.7 2000/02/23 15:13:44 meichel
1188 ** Corrected macro for Borland C++ Builder 4 workaround.
1189 **
1190 ** Revision 1.6 2000/02/01 10:09:37 meichel
1191 ** Avoiding to include <stdlib.h> as extern "C" on Borland C++ Builder 4,
1192 ** workaround for bug in compiler header files.
1193 **
1194 ** Revision 1.5 1998/11/27 12:42:52 joergr
1195 ** Added copyright message to source files and changed CVS header.
1196 **
1197 ** Revision 1.4 1997/09/01 10:00:12 hewett
1198 ** Added absent $ terminator to RCS/CVS Revision keyword in header.
1199 **
1200 ** Revision 1.3 1997/07/14 13:37:31 meichel
1201 ** Simplified OFString code to allow compilation with Sun CC 2.0.1
1202 **
1203 ** Revision 1.2 1997/07/07 14:05:24 hewett
1204 ** Renamed the constant OFnpos to OFString_npos to look more like
1205 ** the real ANSI constant string::npos.
1206 **
1207 ** Revision 1.1 1997/07/07 11:52:18 meichel
1208 ** Added string class OFString to ofstd library.
1209 ** This class implements a subset of the ANSI C++ "string" class.
1210 **
1211 **
1212 */
void clear()
empty the string of all contents
Definition: ofstring.h:457
iterator begin() const
returns a constant iterator that points to the beginning of the string
Definition: ofstring.h:826
char * theCString
the "C" string pointer
Definition: ofstring.h:835
OFBool empty() const
return true if the string is empty, false otherwise.
Definition: ofstring.h:427
size_t size() const
returns a count of the number of char-like objects currently in the string.
Definition: ofstring.h:410
OFString & operator=(const OFString &rhs)
assigns the input string to the current string.
size_t copy(char *s, size_t n, size_t pos=0) const
replaces the string designated by s with a copy of a range of characters from the current string...
size_t rfind(const OFString &pattern, size_t pos=OFString_npos) const
scans the current string backwards, and finds the first occurrence of pattern in the string (from the...
OFString & replace(size_t pos1, size_t n1, const OFString &str, size_t pos2=0, size_t n2=OFString_npos)
replaces a range of characters in the current string with a range of characters taken from the input ...
char & at(size_t pos)
returns a non-const reference to the character at position pos of the current string.
Definition: ofstring.h:354
const char * data() const
if size() is nonzero, this function returns a pointer to the initial element of an array whose first ...
size_t find_first_not_of(const OFString &str, size_t pos=0) const
determines the first location loc, between pos and the end of the current string, such that the chara...
void reserve(size_t res_arg)
directive that informs a string of a planned change in size, so that it can manage the storage alloca...
OFString substr(size_t pos=0, size_t n=OFString_npos) const
returns a copy the substring consisting of at most n characters starting at position pos of the curre...
void resize(size_t n, char c= '\0')
if n <= size(), truncates the string to length n else it pads the extra locations with c...
size_t capacity() const
returns the size of the allocated storage in the string.
Definition: ofstring.h:442
size_t find_last_not_of(const OFString &str, size_t pos=OFString_npos) const
scans the current string up to the position pos and determines the highest location, loc, such that the character at loc does not match any character from the set of characters.
const char * iterator
this typedef can be used to iterate over an string.
Definition: ofstring.h:818
~OFString()
destructor
OFString & operator+=(const OFString &rhs)
Appends the input string to the current string.
const char & at(size_t pos) const
returns a constant reference to the character at position pos of the current string.
Definition: ofstring.h:342
size_t size_type
type that is used for lengths and offsets
Definition: ofstring.h:809
OFString & insert(size_t pos1, const OFString &str, size_t pos2=0, size_t n=OFString_npos)
Inserts at most n characters, starting at position pos2 of the input string str, into the current str...
size_t theSize
the length of theCString
Definition: ofstring.h:838
size_t find_first_of(const OFString &str, size_t pos=0) const
determines the first location, loc, between pos and the end of the current string, such that the character at loc matches at least one character from the set of characters.
OFString & erase(size_t pos=0, size_t n=OFString_npos)
Removes up to n characters from the string starting from position pos.
int compare(const OFString &str) const
determines the effective length rlen of the strings to compare as the smallest of size() and str...
size_t length() const
returns a count of the number of char-like objects currently in the string.
Definition: ofstring.h:419
char operator[](size_t pos) const
returns the element at position pos of the current string.
Definition: ofstring.h:365
iterator const_iterator
this is just an alias for iterator since iterator is already "const"
Definition: ofstring.h:821
size_t find_last_of(const OFString &str, size_t pos=OFString_npos) const
determines the highest location, loc, up to pos, such that the character at loc matches at least one ...
OFString & assign(const OFString &str, size_t pos, size_t n)
Assigns characters from the input string str to the current string object.
size_t find(const OFString &pattern, size_t pos=0) const
determines the earliest occurrence of the input pattern in the current string object, starting from position pos in the current string.
OFString()
Default constructor.
size_t theCapacity
the capacity of str
Definition: ofstring.h:841
a simple string class that implements a subset of std::string.
Definition: ofstring.h:86
OFString & append(const OFString &str, size_t pos=0, size_t n=OFString_npos)
Appends characters from the input string str to the current string object.
void swap(OFString &s)
swaps the contents of the two strings.
const char * c_str() const
returns a pointer to the initial element of an array of length size()+1 whose first size() elements e...
Definition: ofstring.h:392
char value_type
type that is contained in this
Definition: ofstring.h:812
size_t max_size() const
returns the maximum size of a string which could possibly by allocated.
Definition: ofstring.h:450
iterator end() const
returns a constant iterator that points after the last element of the string
Definition: ofstring.h:831


Generated on Wed Jan 4 2017 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.6