00001 /* 00002 * 00003 * Copyright (C) 1997-2010, OFFIS e.V. 00004 * All rights reserved. See COPYRIGHT file for details. 00005 * 00006 * This software and supporting documentation were developed by 00007 * 00008 * OFFIS e.V. 00009 * R&D Division Health 00010 * Escherweg 2 00011 * D-26121 Oldenburg, Germany 00012 * 00013 * 00014 * Module: ofstd 00015 * 00016 * Author: Andrew Hewett 00017 * 00018 * Purpose: A simple string class 00019 * 00020 * Last Update: $Author: joergr $ 00021 * Update Date: $Date: 2010-10-14 13:15:50 $ 00022 * CVS/RCS Revision: $Revision: 1.30 $ 00023 * Status: $State: Exp $ 00024 * 00025 * CVS/RCS Log at end of file 00026 * 00027 */ 00028 00029 #ifndef OFSTRING_H 00030 #define OFSTRING_H 00031 00032 #include "dcmtk/config/osconfig.h" /* include OS specific configuration first */ 00033 00034 #include "dcmtk/ofstd/oftypes.h" /* for OFBool */ 00035 #include "dcmtk/ofstd/ofcast.h" 00036 00037 00038 // makes sure that resulting C string is never NULL 00039 #define OFSTRING_GUARD(c_string) ((c_string != NULL) ? (c_string) : "") 00040 00041 00042 #ifdef HAVE_STD_STRING 00043 /* 00044 ** Use the ANSI Standard string class 00045 */ 00046 00047 #include <string> 00048 00049 #define OFString std::string 00050 #define OFString_npos std::string::npos 00051 00052 #else /* not HAVE_STD_STRING */ 00053 00054 /* 00055 ** Declare our own string class 00056 */ 00057 00058 #define INCLUDE_CASSERT 00059 #define INCLUDE_CSTRING 00060 #define INCLUDE_CSTDLIB 00061 #define INCLUDE_LIBC 00062 #define INCLUDE_UNISTD 00063 #include "dcmtk/ofstd/ofstdinc.h" 00064 00065 #include "dcmtk/ofstd/ofstream.h" 00066 #include "dcmtk/ofstd/oftypes.h" 00067 00068 /* 00069 ** Error macros 00070 */ 00071 #define OFSTRING_OUTOFRANGE(cond) assert (!(cond)) 00072 #define OFSTRING_LENGTHERROR(cond) assert (!(cond)) 00073 #define OFSTRING_MEMORYALLOCERROR(cond) assert (!(cond)) 00074 00080 static const size_t OFString_npos = (OFstatic_cast(size_t, -1)); 00081 00082 00086 class OFString 00087 { 00088 public: 00089 /* 00090 * The SunOS C++ 2.0.1 does not allow static const members. 00091 * We would like to define: 00092 * static const size_t npos = ((size_t)-1); 00093 * but cannot so an alternative OFString_npos is defined outside 00094 * the class (see above). 00095 */ 00096 00099 OFString(); 00100 00110 OFString(const OFString& str, size_t pos = 0, size_t n = OFString_npos); 00111 00120 OFString(const char* s, size_t n); 00121 00126 OFString(const char* s); 00127 00133 OFString(size_t rep, char c); 00134 00137 ~OFString(); 00138 00143 OFString& operator=(const OFString& rhs); 00144 00149 OFString& operator=(const char* s); 00150 00155 OFString& operator=(char s); 00156 00161 OFString& operator+=(const OFString& rhs); 00162 00167 OFString& operator+=(const char* s); 00168 00173 OFString& operator+=(char s); 00174 00184 OFString& append(const OFString& str, size_t pos = 0, size_t n = OFString_npos); 00185 00191 OFString& append(const char* s, size_t n); 00192 00197 OFString& append(const char* s); 00198 00204 OFString& append(size_t rep, char c); 00205 00215 OFString& assign(const OFString& str, size_t pos, size_t n); 00216 00221 OFString& assign(const OFString& str); 00222 00228 OFString& assign(const char* s, size_t n); 00229 00234 OFString& assign(const char* s); 00235 00241 OFString& assign(size_t rep, char c); 00242 00253 OFString& insert(size_t pos1, const OFString& str, 00254 size_t pos2 = 0, size_t n = OFString_npos); 00255 00263 OFString& insert(size_t pos, const char* s, size_t n); 00264 00271 OFString& insert(size_t pos, const char* s); 00272 00280 OFString& insert(size_t pos, size_t rep, char c); 00281 00287 OFString& erase(size_t pos = 0, size_t n = OFString_npos); 00288 00304 OFString& replace(size_t pos1, size_t n1, const OFString& str, 00305 size_t pos2 = 0, size_t n2 = OFString_npos); 00306 00315 OFString& replace(size_t pos, size_t n, const char* s, size_t n2); 00316 00324 OFString& replace(size_t pos, size_t n, const char* s); 00325 00334 OFString& replace(size_t pos, size_t n, size_t rep, char s); 00335 00342 const char& at(size_t pos) const 00343 { 00344 OFSTRING_OUTOFRANGE (pos >= this->size()); 00345 return this->theCString[pos]; 00346 } 00347 00354 char& at(size_t pos) 00355 { 00356 OFSTRING_OUTOFRANGE (pos >= this->size()); 00357 return this->theCString[pos]; 00358 } 00359 00365 char operator[] (size_t pos) const 00366 { 00367 if (pos == this->size()) return '\0'; 00368 else 00369 { 00370 OFSTRING_OUTOFRANGE (pos > this->size()); 00371 return this->theCString[pos]; 00372 } 00373 } 00374 00381 char& operator[] (size_t pos) 00382 { 00383 OFSTRING_OUTOFRANGE (pos >= this->size()); 00384 return this->theCString[pos]; 00385 } 00386 00392 const char* c_str() const 00393 { 00394 return (this->theCString)?(this->theCString):(""); 00395 } 00396 00404 const char* data() const; 00405 00410 size_t size() const 00411 { 00412 return this->theSize; 00413 } 00414 00419 size_t length() const 00420 { 00421 return this->size(); 00422 } 00423 00427 OFBool empty() const 00428 { 00429 return (this->size() == 0)?(OFTrue):(OFFalse); 00430 } 00431 00437 void resize(size_t n, char c = '\0'); 00438 00442 size_t capacity() const 00443 { 00444 return this->theCapacity; 00445 } 00446 00450 size_t max_size() const 00451 { 00452 return ((OFString_npos - 1)/sizeof(char)); 00453 } 00454 00457 void clear() 00458 { 00459 this->erase(); 00460 } 00461 00470 void reserve(size_t res_arg); 00471 00482 size_t copy(char* s, size_t n, size_t pos = 0) const; 00483 00489 OFString substr(size_t pos = 0, size_t n = OFString_npos) const; 00490 00495 void swap(OFString& s); 00496 00507 int compare(const OFString& str) const; 00508 00516 int compare(size_t pos1, size_t n1, const OFString& str) const; 00517 00527 int compare(size_t pos1, size_t n1, const OFString& str, size_t pos2, size_t n2) const; 00528 00534 int compare(const char* s) const; 00535 00544 int compare(size_t pos1, size_t n1, const char* s, size_t n2 = OFString_npos) const; 00545 00555 size_t find(const OFString& pattern, size_t pos = 0) const; 00556 00567 size_t find(const char* pattern, size_t pos, size_t n) const; 00568 00578 size_t find(const char* pattern, size_t pos = 0) const; 00579 00589 size_t find(char pattern, size_t pos = 0) const; 00590 00600 size_t rfind(const OFString& pattern, size_t pos = OFString_npos) const; 00601 00612 size_t rfind(const char* pattern, size_t pos, size_t n) const; 00613 00623 size_t rfind(const char* pattern, size_t pos = OFString_npos) const; 00624 00634 size_t rfind(char pattern, size_t pos = OFString_npos) const; 00635 00645 size_t find_first_of(const OFString& str, size_t pos = 0) const; 00646 00657 size_t find_first_of(const char* s, size_t pos, size_t n) const; 00658 00668 size_t find_first_of(const char* s, size_t pos = 0) const; 00669 00679 size_t find_first_of(char s, size_t pos = 0) const; 00680 00689 size_t find_last_of(const OFString& str, size_t pos = OFString_npos) const; 00690 00700 size_t find_last_of(const char* s, size_t pos, size_t n) const; 00701 00710 size_t find_last_of(const char* s, size_t pos = OFString_npos) const; 00711 00720 size_t find_last_of(char s, size_t pos = OFString_npos) const; 00721 00730 size_t find_first_not_of(const OFString& str, size_t pos = 0) const; 00731 00741 size_t find_first_not_of(const char* s, size_t pos, size_t n) const; 00742 00751 size_t find_first_not_of(const char* s, size_t pos = 0) const; 00752 00761 size_t find_first_not_of(char c, size_t pos = 0) const; 00762 00772 size_t find_last_not_of(const OFString& str, size_t pos = OFString_npos) const; 00773 00784 size_t find_last_not_of(const char* s, size_t pos, size_t n) const; 00785 00795 size_t find_last_not_of(const char* s, size_t pos = OFString_npos) const; 00796 00806 size_t find_last_not_of(char c, size_t pos = OFString_npos) const; 00807 00809 typedef size_t size_type; 00810 00812 typedef char value_type; 00813 00818 typedef const char* iterator; 00819 00821 typedef iterator const_iterator; 00822 00826 iterator begin() const { return theCString; } 00827 00831 iterator end() const { return begin() + length(); } 00832 00833 private: 00835 char* theCString; 00836 00838 size_t theSize; 00839 00841 size_t theCapacity; 00842 00843 }; 00844 00845 00851 STD_NAMESPACE ostream& operator<< (STD_NAMESPACE ostream& o, const OFString& s); 00852 00859 STD_NAMESPACE istream& operator>> (STD_NAMESPACE istream& i, OFString& s); 00860 00866 OFString operator+ (const OFString& lhs, const OFString& rhs); 00867 00873 OFString operator+ (const char* lhs, const OFString& rhs); 00874 00880 OFString operator+ (char lhs, const OFString& rhs); 00881 00887 OFString operator+ (const OFString& lhs, const char* rhs); 00888 00894 OFString operator+ (const OFString& lhs, char rhs); 00895 00901 OFBool operator== (const OFString& lhs, const OFString& rhs); 00902 00908 OFBool operator== (const char* lhs, const OFString& rhs); 00909 00915 OFBool operator== (char lhs, const OFString& rhs); 00916 00922 OFBool operator== (const OFString& lhs, const char* rhs); 00923 00929 OFBool operator== (const OFString& lhs, char rhs); 00930 00936 OFBool operator< (const OFString& lhs, const OFString& rhs); 00937 00943 OFBool operator< (const char* lhs, const OFString& rhs); 00944 00950 OFBool operator< (char lhs, const OFString& rhs); 00951 00957 OFBool operator< (const OFString& lhs, const char* rhs); 00958 00964 OFBool operator< (const OFString& lhs, char rhs); 00965 00971 OFBool operator<= (const OFString& lhs, const OFString& rhs); 00972 00978 OFBool operator<= (const char* lhs, const OFString& rhs); 00979 00985 OFBool operator<= (char lhs, const OFString& rhs); 00986 00992 OFBool operator<= (const OFString& lhs, const char* rhs); 00993 00999 OFBool operator<= (const OFString& lhs, char rhs); 01000 01006 OFBool operator!= (const OFString& lhs, const OFString& rhs); 01007 01013 OFBool operator!= (const char* lhs, const OFString& rhs); 01014 01020 OFBool operator!= (char lhs, const OFString& rhs); 01021 01027 OFBool operator!= (const OFString& lhs, const char* rhs); 01028 01034 OFBool operator!= (const OFString& lhs, char rhs); 01035 01041 OFBool operator> (const OFString& lhs, const OFString& rhs); 01042 01048 OFBool operator> (const char* lhs, const OFString& rhs); 01049 01055 OFBool operator> (char lhs, const OFString& rhs); 01056 01062 OFBool operator> (const OFString& lhs, const char* rhs); 01063 01069 OFBool operator> (const OFString& lhs, char rhs); 01070 01076 OFBool operator>= (const OFString& lhs, const OFString& rhs); 01077 01083 OFBool operator>= (const char* lhs, const OFString& rhs); 01084 01090 OFBool operator>= (char lhs, const OFString& rhs); 01091 01097 OFBool operator>= (const OFString& lhs, const char* rhs); 01098 01104 OFBool operator>= (const OFString& lhs, char rhs); 01105 01106 #endif /* HAVE_STD_STRING */ 01107 01108 #endif /* OFSTRING_H */ 01109 01110 01111 /* 01112 ** CVS/RCS Log: 01113 ** $Log: ofstring.h,v $ 01114 ** Revision 1.30 2010-10-14 13:15:50 joergr 01115 ** Updated copyright header. Added reference to COPYRIGHT file. 01116 ** 01117 ** Revision 1.29 2010-08-19 12:07:55 uli 01118 ** Made OFString follow the C++ standard for std::string::assign(). 01119 ** 01120 ** Revision 1.28 2010-07-26 07:31:17 joergr 01121 ** Fixed typo (and revised documentation on the OFSTRING_GUARD macro). 01122 ** 01123 ** Revision 1.27 2010-07-21 14:25:10 joergr 01124 ** Introduced new guard macro that makes sure that a C string is never NULL. 01125 ** Useful when passing a C string to a OFString constructor or an output stream. 01126 ** 01127 ** Revision 1.26 2010-04-26 12:22:30 uli 01128 ** Fixed a some minor doxygen warnings. 01129 ** 01130 ** Revision 1.25 2009-09-28 14:07:34 joergr 01131 ** Introduced new member variable that stores the current length of the string. 01132 ** This yields in a significant performance improvement when compiled in debug 01133 ** mode. 01134 ** 01135 ** Revision 1.24 2009-08-19 10:42:42 joergr 01136 ** Added iterator declarations and required methods. 01137 ** 01138 ** Revision 1.23 2009-08-07 14:31:08 joergr 01139 ** Fixed incorrect implementation of find_first_not_of() and find_last_not_of(). 01140 ** 01141 ** Revision 1.22 2007/02/20 13:12:59 joergr 01142 ** Fixed wrong comment in compare() method. 01143 ** 01144 ** Revision 1.21 2006/08/14 16:42:26 meichel 01145 ** Updated all code in module ofstd to correctly compile if the standard 01146 ** namespace has not included into the global one with a "using" directive. 01147 ** 01148 ** Revision 1.20 2005/12/08 16:06:07 meichel 01149 ** Changed include path schema for all DCMTK header files 01150 ** 01151 ** Revision 1.19 2004/08/03 11:45:42 meichel 01152 ** Headers libc.h and unistd.h are now included via ofstdinc.h 01153 ** 01154 ** Revision 1.18 2004/01/16 10:30:12 joergr 01155 ** Removed acknowledgements with e-mail addresses from CVS log. 01156 ** 01157 ** Revision 1.17 2003/08/07 11:44:55 joergr 01158 ** Slightly modified header comments to conform to doxygen syntax. 01159 ** 01160 ** Revision 1.16 2003/07/09 13:57:43 meichel 01161 ** Adapted type casts to new-style typecast operators defined in ofcast.h 01162 ** 01163 ** Revision 1.15 2003/07/04 13:31:51 meichel 01164 ** Fixed issues with compiling with HAVE_STD_STRING 01165 ** 01166 ** Revision 1.14 2003/06/12 13:13:51 joergr 01167 ** Fixed inconsistent API documentation reported by Doxygen. 01168 ** 01169 ** Revision 1.13 2002/11/27 11:23:06 meichel 01170 ** Adapted module ofstd to use of new header file ofstdinc.h 01171 ** 01172 ** Revision 1.12 2002/04/16 13:36:03 joergr 01173 ** Added configurable support for C++ ANSI standard includes (e.g. streams). 01174 ** 01175 ** Revision 1.11 2001/12/04 16:48:16 meichel 01176 ** Completed doc++ documentation, fixed bug in OFString::copy. 01177 ** 01178 ** Revision 1.10 2001/11/02 13:18:53 meichel 01179 ** Removed character sequences that could be interpreted as ISO C++ trigraphs 01180 ** 01181 ** Revision 1.9 2001/06/01 15:51:35 meichel 01182 ** Updated copyright header 01183 ** 01184 ** Revision 1.8 2000/03/08 16:36:02 meichel 01185 ** Updated copyright header. 01186 ** 01187 ** Revision 1.7 2000/02/23 15:13:44 meichel 01188 ** Corrected macro for Borland C++ Builder 4 workaround. 01189 ** 01190 ** Revision 1.6 2000/02/01 10:09:37 meichel 01191 ** Avoiding to include <stdlib.h> as extern "C" on Borland C++ Builder 4, 01192 ** workaround for bug in compiler header files. 01193 ** 01194 ** Revision 1.5 1998/11/27 12:42:52 joergr 01195 ** Added copyright message to source files and changed CVS header. 01196 ** 01197 ** Revision 1.4 1997/09/01 10:00:12 hewett 01198 ** Added absent $ terminator to RCS/CVS Revision keyword in header. 01199 ** 01200 ** Revision 1.3 1997/07/14 13:37:31 meichel 01201 ** Simplified OFString code to allow compilation with Sun CC 2.0.1 01202 ** 01203 ** Revision 1.2 1997/07/07 14:05:24 hewett 01204 ** Renamed the constant OFnpos to OFString_npos to look more like 01205 ** the real ANSI constant string::npos. 01206 ** 01207 ** Revision 1.1 1997/07/07 11:52:18 meichel 01208 ** Added string class OFString to ofstd library. 01209 ** This class implements a subset of the ANSI C++ "string" class. 01210 ** 01211 ** 01212 */