00001 /* 00002 * 00003 * Copyright (C) 1997-2005, OFFIS 00004 * 00005 * This software and supporting documentation were developed by 00006 * 00007 * Kuratorium OFFIS e.V. 00008 * Healthcare Information and Communication Systems 00009 * Escherweg 2 00010 * D-26121 Oldenburg, Germany 00011 * 00012 * THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND OFFIS MAKES NO WARRANTY 00013 * REGARDING THE SOFTWARE, ITS PERFORMANCE, ITS MERCHANTABILITY OR 00014 * FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR 00015 * ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND 00016 * PERFORMANCE OF THE SOFTWARE IS WITH THE USER. 00017 * 00018 * Module: ofstd 00019 * 00020 * Author: Andrew Hewett 00021 * 00022 * Purpose: A simple string class 00023 * 00024 * Last Update: $Author: meichel $ 00025 * Update Date: $Date: 2005/12/08 16:06:07 $ 00026 * CVS/RCS Revision: $Revision: 1.20 $ 00027 * Status: $State: Exp $ 00028 * 00029 * CVS/RCS Log at end of file 00030 * 00031 */ 00032 00033 #ifndef OFSTRING_H 00034 #define OFSTRING_H 00035 00036 #include "dcmtk/config/osconfig.h" /* include OS specific configuration first */ 00037 #include "dcmtk/ofstd/oftypes.h" /* for OFBool */ 00038 #include "dcmtk/ofstd/ofcast.h" 00039 00040 #ifdef HAVE_STD_STRING 00041 /* 00042 ** Use the ANSI Standard string class 00043 */ 00044 00045 #include <string> 00046 00047 #define OFString std::string 00048 #define OFString_npos std::string::npos 00049 00050 #else /* not HAVE_STD_STRING */ 00051 00052 /* 00053 ** Declare our own string class 00054 */ 00055 00056 #define INCLUDE_CASSERT 00057 #define INCLUDE_CSTRING 00058 #define INCLUDE_CSTDLIB 00059 #define INCLUDE_LIBC 00060 #define INCLUDE_UNISTD 00061 #include "dcmtk/ofstd/ofstdinc.h" 00062 00063 #include "dcmtk/ofstd/ofstream.h" 00064 #include "dcmtk/ofstd/oftypes.h" 00065 00066 /* 00067 ** Error macros 00068 */ 00069 #define OFSTRING_OUTOFRANGE(cond) assert (!(cond)) 00070 #define OFSTRING_LENGTHERROR(cond) assert (!(cond)) 00071 #define OFSTRING_MEMORYALLOCERROR(cond) assert (!(cond)) 00072 00078 static const size_t OFString_npos = (OFstatic_cast(size_t, -1)); 00079 00080 00084 class OFString 00085 { 00086 public: 00087 /* 00088 * The SunOS C++ 2.0.1 does not allow static const members. 00089 * We would like to define: 00090 * static const size_t npos = ((size_t)-1); 00091 * but cannot so an alternative OFString_npos is defined outside 00092 * the class (see above). 00093 */ 00094 00097 OFString(); 00098 00108 OFString(const OFString& str, size_t pos = 0, size_t n = OFString_npos); 00109 00118 OFString(const char* s, size_t n); 00119 00124 OFString(const char* s); 00125 00131 OFString(size_t rep, char c); 00132 00135 ~OFString(); 00136 00141 OFString& operator=(const OFString& rhs); 00142 00147 OFString& operator=(const char* s); 00148 00153 OFString& operator=(char s); 00154 00159 OFString& operator+=(const OFString& rhs); 00160 00165 OFString& operator+=(const char* s); 00166 00171 OFString& operator+=(char s); 00172 00182 OFString& append(const OFString& str, size_t pos = 0, size_t n = OFString_npos); 00183 00189 OFString& append(const char* s, size_t n); 00190 00195 OFString& append(const char* s); 00196 00202 OFString& append(size_t rep, char c); 00203 00213 OFString& assign(const OFString& str, size_t pos = 0, size_t n = OFString_npos); 00214 00220 OFString& assign(const char* s, size_t n); 00221 00226 OFString& assign(const char* s); 00227 00233 OFString& assign(size_t rep, char c); 00234 00245 OFString& insert(size_t pos1, const OFString& str, 00246 size_t pos2 = 0, size_t n = OFString_npos); 00247 00255 OFString& insert(size_t pos, const char* s, size_t n); 00256 00263 OFString& insert(size_t pos, const char* s); 00264 00272 OFString& insert(size_t pos, size_t rep, char c); 00273 00279 OFString& erase (size_t pos = 0, size_t n = OFString_npos); 00280 00296 OFString& replace(size_t pos1, size_t n1, const OFString& str, 00297 size_t pos2 = 0, size_t n2 = OFString_npos); 00298 00307 OFString& replace(size_t pos, size_t n, const char* s, size_t n2); 00308 00316 OFString& replace(size_t pos, size_t n, const char* s); 00317 00326 OFString& replace(size_t pos, size_t n, size_t rep, char s); 00327 00334 const char& at(size_t pos) const 00335 { 00336 OFSTRING_OUTOFRANGE (pos >= this->size()); 00337 return this->theCString[pos]; 00338 } 00339 00346 char& at(size_t pos) 00347 { 00348 OFSTRING_OUTOFRANGE (pos >= this->size()); 00349 return this->theCString[pos]; 00350 } 00351 00357 char operator[] (size_t pos) const 00358 { 00359 if (pos == this->size()) return '\0'; 00360 else 00361 { 00362 OFSTRING_OUTOFRANGE (pos > this->size()); 00363 return this->theCString[pos]; 00364 } 00365 } 00366 00373 char& operator[] (size_t pos) 00374 { 00375 OFSTRING_OUTOFRANGE (pos >= this->size()); 00376 return this->theCString[pos]; 00377 } 00378 00384 const char* c_str() const 00385 { 00386 return (this->theCString)?(this->theCString):(""); 00387 } 00388 00396 const char* data() const; 00397 00402 size_t size() const 00403 { 00404 return (this->theCString)?(strlen(this->theCString)):(0); 00405 } 00406 00411 size_t length() const 00412 { 00413 return this->size(); 00414 } 00415 00419 OFBool empty() const 00420 { 00421 return (this->size() == 0)?(OFTrue):(OFFalse); 00422 } 00423 00429 void resize (size_t n, char c = '\0'); 00430 00434 size_t capacity () const 00435 { 00436 return this->theCapacity; 00437 } 00438 00442 size_t max_size() const 00443 { 00444 return ((OFString_npos - 1)/sizeof(char)); 00445 } 00446 00449 void clear() 00450 { 00451 this->erase(); 00452 } 00453 00462 void reserve(size_t res_arg); 00463 00474 size_t copy(char* s, size_t n, size_t pos = 0) const; 00475 00481 OFString substr(size_t pos = 0, size_t n = OFString_npos) const; 00482 00487 void swap(OFString& s); 00488 00499 int compare(const OFString& str) const; 00500 00508 int compare(size_t pos1, size_t n1, const OFString& str) const; 00509 00519 int compare(size_t pos1, size_t n1, const OFString& str, 00520 size_t pos2, size_t n2) const; 00521 00527 int compare(const char* s) const; 00528 00537 int compare(size_t pos1, size_t n1, 00538 const char* s, size_t n2 = OFString_npos) const; 00539 00549 size_t find(const OFString& pattern, size_t pos = 0) const; 00550 00561 size_t find(const char* pattern, size_t pos, size_t n) const; 00562 00572 size_t find(const char* pattern, size_t pos = 0) const; 00573 00583 size_t find(char pattern, size_t pos = 0) const; 00584 00594 size_t rfind(const OFString& pattern, size_t pos = OFString_npos) const; 00595 00606 size_t rfind(const char* pattern, size_t pos, size_t n) const; 00607 00617 size_t rfind(const char* pattern, size_t pos = OFString_npos) const; 00618 00628 size_t rfind(char pattern, size_t pos = OFString_npos) const; 00629 00639 size_t find_first_of(const OFString& str, size_t pos = 0) const; 00640 00651 size_t find_first_of(const char* s, size_t pos, size_t n) const; 00652 00662 size_t find_first_of(const char* s, size_t pos = 0) const; 00663 00673 size_t find_first_of(char s, size_t pos = 0) const; 00674 00683 size_t find_last_of(const OFString& str, size_t pos = OFString_npos) const; 00684 00694 size_t find_last_of(const char* s, size_t pos, size_t n) const; 00695 00704 size_t find_last_of(const char* s, size_t pos = OFString_npos) const; 00705 00714 size_t find_last_of(char s, size_t pos = OFString_npos) const; 00715 00724 size_t find_first_not_of(const OFString& str, size_t pos = 0) const; 00725 00735 size_t find_first_not_of(const char* s, size_t pos, size_t n) const; 00736 00745 size_t find_first_not_of(const char* s, size_t pos = 0) const; 00746 00755 size_t find_first_not_of(char c, size_t pos = 0) const; 00756 00766 size_t find_last_not_of(const OFString& str, size_t pos = OFString_npos) const; 00767 00778 size_t find_last_not_of(const char* s, size_t pos, size_t n) const; 00779 00789 size_t find_last_not_of(const char* s, size_t pos = OFString_npos) const; 00790 00800 size_t find_last_not_of(char c, size_t pos = OFString_npos) const; 00801 00802 private: 00804 char* theCString; 00805 00807 size_t theCapacity; 00808 00809 }; 00810 00811 00817 ostream& operator<< (ostream& o, const OFString& s); 00818 00825 istream& operator>> (istream& i, OFString& s); 00826 00832 OFString operator+ (const OFString& lhs, const OFString& rhs); 00833 00839 OFString operator+ (const char* lhs, const OFString& rhs); 00840 00846 OFString operator+ (char lhs, const OFString& rhs); 00847 00853 OFString operator+ (const OFString& lhs, const char* rhs); 00854 00860 OFString operator+ (const OFString& lhs, char rhs); 00861 00867 OFBool operator== (const OFString& lhs, const OFString& rhs); 00868 00874 OFBool operator== (const char* lhs, const OFString& rhs); 00875 00881 OFBool operator== (char lhs, const OFString& rhs); 00882 00888 OFBool operator== (const OFString& lhs, const char* rhs); 00889 00895 OFBool operator== (const OFString& lhs, char rhs); 00896 00902 OFBool operator< (const OFString& lhs, const OFString& rhs); 00903 00909 OFBool operator< (const char* lhs, const OFString& rhs); 00910 00916 OFBool operator< (char lhs, const OFString& rhs); 00917 00923 OFBool operator< (const OFString& lhs, const char* rhs); 00924 00930 OFBool operator< (const OFString& lhs, char rhs); 00931 00937 OFBool operator<= (const OFString& lhs, const OFString& rhs); 00938 00944 OFBool operator<= (const char* lhs, const OFString& rhs); 00945 00951 OFBool operator<= (char lhs, const OFString& rhs); 00952 00958 OFBool operator<= (const OFString& lhs, const char* rhs); 00959 00965 OFBool operator<= (const OFString& lhs, char rhs); 00966 00972 OFBool operator!= (const OFString& lhs, const OFString& rhs); 00973 00979 OFBool operator!= (const char* lhs, const OFString& rhs); 00980 00986 OFBool operator!= (char lhs, const OFString& rhs); 00987 00993 OFBool operator!= (const OFString& lhs, const char* rhs); 00994 01000 OFBool operator!= (const OFString& lhs, char rhs); 01001 01007 OFBool operator> (const OFString& lhs, const OFString& rhs); 01008 01014 OFBool operator> (const char* lhs, const OFString& rhs); 01015 01021 OFBool operator> (char lhs, const OFString& rhs); 01022 01028 OFBool operator> (const OFString& lhs, const char* rhs); 01029 01035 OFBool operator> (const OFString& lhs, char rhs); 01036 01042 OFBool operator>= (const OFString& lhs, const OFString& rhs); 01043 01049 OFBool operator>= (const char* lhs, const OFString& rhs); 01050 01056 OFBool operator>= (char lhs, const OFString& rhs); 01057 01063 OFBool operator>= (const OFString& lhs, const char* rhs); 01064 01070 OFBool operator>= (const OFString& lhs, char rhs); 01071 01072 #endif /* HAVE_STD_STRING */ 01073 01074 #endif /* OFSTRING_H */ 01075 01076 01077 /* 01078 ** CVS/RCS Log: 01079 ** $Log: ofstring.h,v $ 01080 ** Revision 1.20 2005/12/08 16:06:07 meichel 01081 ** Changed include path schema for all DCMTK header files 01082 ** 01083 ** Revision 1.19 2004/08/03 11:45:42 meichel 01084 ** Headers libc.h and unistd.h are now included via ofstdinc.h 01085 ** 01086 ** Revision 1.18 2004/01/16 10:30:12 joergr 01087 ** Removed acknowledgements with e-mail addresses from CVS log. 01088 ** 01089 ** Revision 1.17 2003/08/07 11:44:55 joergr 01090 ** Slightly modified header comments to conform to doxygen syntax. 01091 ** 01092 ** Revision 1.16 2003/07/09 13:57:43 meichel 01093 ** Adapted type casts to new-style typecast operators defined in ofcast.h 01094 ** 01095 ** Revision 1.15 2003/07/04 13:31:51 meichel 01096 ** Fixed issues with compiling with HAVE_STD_STRING 01097 ** 01098 ** Revision 1.14 2003/06/12 13:13:51 joergr 01099 ** Fixed inconsistent API documentation reported by Doxygen. 01100 ** 01101 ** Revision 1.13 2002/11/27 11:23:06 meichel 01102 ** Adapted module ofstd to use of new header file ofstdinc.h 01103 ** 01104 ** Revision 1.12 2002/04/16 13:36:03 joergr 01105 ** Added configurable support for C++ ANSI standard includes (e.g. streams). 01106 ** 01107 ** Revision 1.11 2001/12/04 16:48:16 meichel 01108 ** Completed doc++ documentation, fixed bug in OFString::copy. 01109 ** 01110 ** Revision 1.10 2001/11/02 13:18:53 meichel 01111 ** Removed character sequences that could be interpreted as ISO C++ trigraphs 01112 ** 01113 ** Revision 1.9 2001/06/01 15:51:35 meichel 01114 ** Updated copyright header 01115 ** 01116 ** Revision 1.8 2000/03/08 16:36:02 meichel 01117 ** Updated copyright header. 01118 ** 01119 ** Revision 1.7 2000/02/23 15:13:44 meichel 01120 ** Corrected macro for Borland C++ Builder 4 workaround. 01121 ** 01122 ** Revision 1.6 2000/02/01 10:09:37 meichel 01123 ** Avoiding to include <stdlib.h> as extern "C" on Borland C++ Builder 4, 01124 ** workaround for bug in compiler header files. 01125 ** 01126 ** Revision 1.5 1998/11/27 12:42:52 joergr 01127 ** Added copyright message to source files and changed CVS header. 01128 ** 01129 ** Revision 1.4 1997/09/01 10:00:12 hewett 01130 ** Added absent $ terminator to RCS/CVS Revision keyword in header. 01131 ** 01132 ** Revision 1.3 1997/07/14 13:37:31 meichel 01133 ** Simplified OFString code to allow compilation with Sun CC 2.0.1 01134 ** 01135 ** Revision 1.2 1997/07/07 14:05:24 hewett 01136 ** Renamed the constant OFnpos to OFString_npos to look more like 01137 ** the real ANSI constant string::npos. 01138 ** 01139 ** Revision 1.1 1997/07/07 11:52:18 meichel 01140 ** Added string class OFString to ofstd library. 01141 ** This class implements a subset of the ANSI C++ "string" class. 01142 ** 01143 ** 01144 */