ofstd/include/ofstring.h

00001 /* 00002 * 00003 * Copyright (C) 1997-2004, 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: joergr $ 00025 * Update Date: $Date: 2004/01/16 10:30:12 $ 00026 * CVS/RCS Revision: $Revision: 1.18 $ 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 "osconfig.h" /* include OS specific configuration first */ 00037 #include "oftypes.h" /* for OFBool */ 00038 #include "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 #include "ofstdinc.h" 00060 00061 BEGIN_EXTERN_C 00062 #ifdef HAVE_UNISTD_H 00063 #include <unistd.h> /* for NULL */ 00064 #endif 00065 #ifdef HAVE_LIBC_H 00066 #include <libc.h> 00067 #endif 00068 END_EXTERN_C 00069 00070 #include "ofstream.h" 00071 #include "oftypes.h" 00072 00073 /* 00074 ** Error macros 00075 */ 00076 #define OFSTRING_OUTOFRANGE(cond) assert (!(cond)) 00077 #define OFSTRING_LENGTHERROR(cond) assert (!(cond)) 00078 #define OFSTRING_MEMORYALLOCERROR(cond) assert (!(cond)) 00079 00085 static const size_t OFString_npos = (OFstatic_cast(size_t, -1)); 00086 00087 00091 class OFString 00092 { 00093 public: 00094 /* 00095 * The SunOS C++ 2.0.1 does not allow static const members. 00096 * We would like to define: 00097 * static const size_t npos = ((size_t)-1); 00098 * but cannot so an alternative OFString_npos is defined outside 00099 * the class (see above). 00100 */ 00101 00104 OFString(); 00105 00115 OFString(const OFString& str, size_t pos = 0, size_t n = OFString_npos); 00116 00125 OFString(const char* s, size_t n); 00126 00131 OFString(const char* s); 00132 00138 OFString(size_t rep, char c); 00139 00142 ~OFString(); 00143 00148 OFString& operator=(const OFString& rhs); 00149 00154 OFString& operator=(const char* s); 00155 00160 OFString& operator=(char s); 00161 00166 OFString& operator+=(const OFString& rhs); 00167 00172 OFString& operator+=(const char* s); 00173 00178 OFString& operator+=(char s); 00179 00189 OFString& append(const OFString& str, size_t pos = 0, size_t n = OFString_npos); 00190 00196 OFString& append(const char* s, size_t n); 00197 00202 OFString& append(const char* s); 00203 00209 OFString& append(size_t rep, char c); 00210 00220 OFString& assign(const OFString& str, size_t pos = 0, size_t n = OFString_npos); 00221 00227 OFString& assign(const char* s, size_t n); 00228 00233 OFString& assign(const char* s); 00234 00240 OFString& assign(size_t rep, char c); 00241 00252 OFString& insert(size_t pos1, const OFString& str, 00253 size_t pos2 = 0, size_t n = OFString_npos); 00254 00262 OFString& insert(size_t pos, const char* s, size_t n); 00263 00270 OFString& insert(size_t pos, const char* s); 00271 00279 OFString& insert(size_t pos, size_t rep, char c); 00280 00286 OFString& erase (size_t pos = 0, size_t n = OFString_npos); 00287 00303 OFString& replace(size_t pos1, size_t n1, const OFString& str, 00304 size_t pos2 = 0, size_t n2 = OFString_npos); 00305 00314 OFString& replace(size_t pos, size_t n, const char* s, size_t n2); 00315 00323 OFString& replace(size_t pos, size_t n, const char* s); 00324 00333 OFString& replace(size_t pos, size_t n, size_t rep, char s); 00334 00341 const char& at(size_t pos) const 00342 { 00343 OFSTRING_OUTOFRANGE (pos >= this->size()); 00344 return this->theCString[pos]; 00345 } 00346 00353 char& at(size_t pos) 00354 { 00355 OFSTRING_OUTOFRANGE (pos >= this->size()); 00356 return this->theCString[pos]; 00357 } 00358 00364 char operator[] (size_t pos) const 00365 { 00366 if (pos == this->size()) return '\0'; 00367 else 00368 { 00369 OFSTRING_OUTOFRANGE (pos > this->size()); 00370 return this->theCString[pos]; 00371 } 00372 } 00373 00380 char& operator[] (size_t pos) 00381 { 00382 OFSTRING_OUTOFRANGE (pos >= this->size()); 00383 return this->theCString[pos]; 00384 } 00385 00391 const char* c_str() const 00392 { 00393 return (this->theCString)?(this->theCString):(""); 00394 } 00395 00403 const char* data() const; 00404 00409 size_t size() const 00410 { 00411 return (this->theCString)?(strlen(this->theCString)):(0); 00412 } 00413 00418 size_t length() const 00419 { 00420 return this->size(); 00421 } 00422 00426 OFBool empty() const 00427 { 00428 return (this->size() == 0)?(OFTrue):(OFFalse); 00429 } 00430 00436 void resize (size_t n, char c = '\0'); 00437 00441 size_t capacity () const 00442 { 00443 return this->theCapacity; 00444 } 00445 00449 size_t max_size() const 00450 { 00451 return ((OFString_npos - 1)/sizeof(char)); 00452 } 00453 00456 void clear() 00457 { 00458 this->erase(); 00459 } 00460 00469 void reserve(size_t res_arg); 00470 00481 size_t copy(char* s, size_t n, size_t pos = 0) const; 00482 00488 OFString substr(size_t pos = 0, size_t n = OFString_npos) const; 00489 00494 void swap(OFString& s); 00495 00506 int compare(const OFString& str) const; 00507 00515 int compare(size_t pos1, size_t n1, const OFString& str) const; 00516 00526 int compare(size_t pos1, size_t n1, const OFString& str, 00527 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, 00545 const char* s, size_t n2 = OFString_npos) const; 00546 00556 size_t find(const OFString& pattern, size_t pos = 0) const; 00557 00568 size_t find(const char* pattern, size_t pos, size_t n) const; 00569 00579 size_t find(const char* pattern, size_t pos = 0) const; 00580 00590 size_t find(char pattern, size_t pos = 0) const; 00591 00601 size_t rfind(const OFString& pattern, size_t pos = OFString_npos) const; 00602 00613 size_t rfind(const char* pattern, size_t pos, size_t n) const; 00614 00624 size_t rfind(const char* pattern, size_t pos = OFString_npos) const; 00625 00635 size_t rfind(char pattern, size_t pos = OFString_npos) const; 00636 00646 size_t find_first_of(const OFString& str, size_t pos = 0) const; 00647 00658 size_t find_first_of(const char* s, size_t pos, size_t n) const; 00659 00669 size_t find_first_of(const char* s, size_t pos = 0) const; 00670 00680 size_t find_first_of(char s, size_t pos = 0) const; 00681 00690 size_t find_last_of(const OFString& str, size_t pos = OFString_npos) const; 00691 00701 size_t find_last_of(const char* s, size_t pos, size_t n) const; 00702 00711 size_t find_last_of(const char* s, size_t pos = OFString_npos) const; 00712 00721 size_t find_last_of(char s, size_t pos = OFString_npos) const; 00722 00731 size_t find_first_not_of(const OFString& str, size_t pos = 0) const; 00732 00742 size_t find_first_not_of(const char* s, size_t pos, size_t n) const; 00743 00752 size_t find_first_not_of(const char* s, size_t pos = 0) const; 00753 00762 size_t find_first_not_of(char c, size_t pos = 0) const; 00763 00773 size_t find_last_not_of(const OFString& str, size_t pos = OFString_npos) const; 00774 00785 size_t find_last_not_of(const char* s, size_t pos, size_t n) const; 00786 00796 size_t find_last_not_of(const char* s, size_t pos = OFString_npos) const; 00797 00807 size_t find_last_not_of(char c, size_t pos = OFString_npos) const; 00808 00809 private: 00811 char* theCString; 00812 00814 size_t theCapacity; 00815 00816 }; 00817 00818 00824 ostream& operator<< (ostream& o, const OFString& s); 00825 00832 istream& operator>> (istream& i, OFString& s); 00833 00839 OFString operator+ (const OFString& lhs, const OFString& rhs); 00840 00846 OFString operator+ (const char* lhs, const OFString& rhs); 00847 00853 OFString operator+ (char lhs, const OFString& rhs); 00854 00860 OFString operator+ (const OFString& lhs, const char* rhs); 00861 00867 OFString operator+ (const OFString& lhs, char rhs); 00868 00874 OFBool operator== (const OFString& lhs, const OFString& rhs); 00875 00881 OFBool operator== (const char* lhs, const OFString& rhs); 00882 00888 OFBool operator== (char lhs, const OFString& rhs); 00889 00895 OFBool operator== (const OFString& lhs, const char* rhs); 00896 00902 OFBool operator== (const OFString& lhs, char rhs); 00903 00909 OFBool operator< (const OFString& lhs, const OFString& rhs); 00910 00916 OFBool operator< (const char* lhs, const OFString& rhs); 00917 00923 OFBool operator< (char lhs, const OFString& rhs); 00924 00930 OFBool operator< (const OFString& lhs, const char* rhs); 00931 00937 OFBool operator< (const OFString& lhs, char rhs); 00938 00944 OFBool operator<= (const OFString& lhs, const OFString& rhs); 00945 00951 OFBool operator<= (const char* lhs, const OFString& rhs); 00952 00958 OFBool operator<= (char lhs, const OFString& rhs); 00959 00965 OFBool operator<= (const OFString& lhs, const char* rhs); 00966 00972 OFBool operator<= (const OFString& lhs, char rhs); 00973 00979 OFBool operator!= (const OFString& lhs, const OFString& rhs); 00980 00986 OFBool operator!= (const char* lhs, const OFString& rhs); 00987 00993 OFBool operator!= (char lhs, const OFString& rhs); 00994 01000 OFBool operator!= (const OFString& lhs, const char* rhs); 01001 01007 OFBool operator!= (const OFString& lhs, char rhs); 01008 01014 OFBool operator> (const OFString& lhs, const OFString& rhs); 01015 01021 OFBool operator> (const char* lhs, const OFString& rhs); 01022 01028 OFBool operator> (char lhs, const OFString& rhs); 01029 01035 OFBool operator> (const OFString& lhs, const char* rhs); 01036 01042 OFBool operator> (const OFString& lhs, char rhs); 01043 01049 OFBool operator>= (const OFString& lhs, const OFString& rhs); 01050 01056 OFBool operator>= (const char* lhs, const OFString& rhs); 01057 01063 OFBool operator>= (char lhs, const OFString& rhs); 01064 01070 OFBool operator>= (const OFString& lhs, const char* rhs); 01071 01077 OFBool operator>= (const OFString& lhs, char rhs); 01078 01079 #endif /* HAVE_STD_STRING */ 01080 01081 #endif /* OFSTRING_H */ 01082 01083 01084 /* 01085 ** CVS/RCS Log: 01086 ** $Log: ofstring.h,v $ 01087 ** Revision 1.18 2004/01/16 10:30:12 joergr 01088 ** Removed acknowledgements with e-mail addresses from CVS log. 01089 ** 01090 ** Revision 1.17 2003/08/07 11:44:55 joergr 01091 ** Slightly modified header comments to conform to doxygen syntax. 01092 ** 01093 ** Revision 1.16 2003/07/09 13:57:43 meichel 01094 ** Adapted type casts to new-style typecast operators defined in ofcast.h 01095 ** 01096 ** Revision 1.15 2003/07/04 13:31:51 meichel 01097 ** Fixed issues with compiling with HAVE_STD_STRING 01098 ** 01099 ** Revision 1.14 2003/06/12 13:13:51 joergr 01100 ** Fixed inconsistent API documentation reported by Doxygen. 01101 ** 01102 ** Revision 1.13 2002/11/27 11:23:06 meichel 01103 ** Adapted module ofstd to use of new header file ofstdinc.h 01104 ** 01105 ** Revision 1.12 2002/04/16 13:36:03 joergr 01106 ** Added configurable support for C++ ANSI standard includes (e.g. streams). 01107 ** 01108 ** Revision 1.11 2001/12/04 16:48:16 meichel 01109 ** Completed doc++ documentation, fixed bug in OFString::copy. 01110 ** 01111 ** Revision 1.10 2001/11/02 13:18:53 meichel 01112 ** Removed character sequences that could be interpreted as ISO C++ trigraphs 01113 ** 01114 ** Revision 1.9 2001/06/01 15:51:35 meichel 01115 ** Updated copyright header 01116 ** 01117 ** Revision 1.8 2000/03/08 16:36:02 meichel 01118 ** Updated copyright header. 01119 ** 01120 ** Revision 1.7 2000/02/23 15:13:44 meichel 01121 ** Corrected macro for Borland C++ Builder 4 workaround. 01122 ** 01123 ** Revision 1.6 2000/02/01 10:09:37 meichel 01124 ** Avoiding to include <stdlib.h> as extern "C" on Borland C++ Builder 4, 01125 ** workaround for bug in compiler header files. 01126 ** 01127 ** Revision 1.5 1998/11/27 12:42:52 joergr 01128 ** Added copyright message to source files and changed CVS header. 01129 ** 01130 ** Revision 1.4 1997/09/01 10:00:12 hewett 01131 ** Added absent $ terminator to RCS/CVS Revision keyword in header. 01132 ** 01133 ** Revision 1.3 1997/07/14 13:37:31 meichel 01134 ** Simplified OFString code to allow compilation with Sun CC 2.0.1 01135 ** 01136 ** Revision 1.2 1997/07/07 14:05:24 hewett 01137 ** Renamed the constant OFnpos to OFString_npos to look more like 01138 ** the real ANSI constant string::npos. 01139 ** 01140 ** Revision 1.1 1997/07/07 11:52:18 meichel 01141 ** Added string class OFString to ofstd library. 01142 ** This class implements a subset of the ANSI C++ "string" class. 01143 ** 01144 ** 01145 */


Generated on 4 Nov 2004 for OFFIS DCMTK Version 3.5.3 by Doxygen 1.3.8