GNU CommonC++
|
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation. 00002 // 00003 // This program is free software; you can redistribute it and/or modify 00004 // it under the terms of the GNU General Public License as published by 00005 // the Free Software Foundation; either version 2 of the License, or 00006 // (at your option) any later version. 00007 // 00008 // This program is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00016 // 00017 // As a special exception, you may use this file as part of a free software 00018 // library without restriction. Specifically, if other files instantiate 00019 // templates or use macros or inline functions from this file, or you compile 00020 // this file and link it with other files to produce an executable, this 00021 // file does not by itself cause the resulting executable to be covered by 00022 // the GNU General Public License. This exception does not however 00023 // invalidate any other reasons why the executable file might be covered by 00024 // the GNU General Public License. 00025 // 00026 // This exception applies only to the code released under the name GNU 00027 // Common C++. If you copy code from other releases into a copy of GNU 00028 // Common C++, as the General Public License permits, the exception does 00029 // not apply to the code that you add in this way. To avoid misleading 00030 // anyone as to the status of such modified files, you must delete 00031 // this exception notice from them. 00032 // 00033 // If you write modifications of your own for GNU Common C++, it is your choice 00034 // whether to permit this exception to apply to your modifications. 00035 // If you do not wish that, delete this exception notice. 00036 // 00037 00043 #ifndef CCXX_STRING_H_ 00044 #define CCXX_STRING_H_ 00045 00046 #ifndef CCXX_MISSING_H_ 00047 #include <cc++/missing.h> 00048 #endif 00049 00050 #ifndef CCXX_STRCHAR_H_ 00051 #include <cc++/strchar.h> 00052 #endif 00053 00054 #ifdef CCXX_NAMESPACES 00055 namespace ost { 00056 #endif 00057 00058 class MemPager; 00059 00076 class __EXPORT String 00077 { 00078 protected: 00079 static const unsigned minsize; 00080 static const unsigned slotsize; 00081 static const unsigned pagesize; 00082 static const unsigned slotlimit; 00083 static const unsigned slotcount; 00084 00085 friend class StringObject; 00086 00087 private: 00088 friend class MemPager; 00089 00090 static MemPager *pager; 00091 static char **idx; 00092 00093 #ifdef CCXX_PACKED 00094 #pragma pack(1) 00095 #endif 00096 00097 union { 00098 struct { 00099 char *text; 00100 size_t size; 00101 size_t length; 00102 } bigstring; 00103 struct { 00104 char text[(sizeof(char *) + (sizeof(size_t) * 2) + 1)]; 00105 char length : 6; 00106 bool big : 1; 00107 } ministring; 00108 } content; 00109 00110 #ifdef CCXX_PACKED 00111 #pragma pack() 00112 #endif 00113 00114 protected: 00121 inline bool isBig(void) const 00122 {return content.ministring.big;}; 00123 00132 const char *set(const char *str, size_t len = 0); 00133 00140 void set(const String &str); 00141 00142 #ifdef HAVE_SNPRINTF 00143 00150 const char *set(size_t size, const char *format, ...); 00151 #endif 00152 00159 void copy(const String &str); 00160 00164 void init(void); 00165 00173 static char *getSpace(size_t size); 00174 00182 size_t setSize(size_t size); 00183 00189 void setLength(size_t len); 00190 00201 virtual int compare(const char *text, size_t len = 0, size_t index = 0) const; 00202 00212 size_t search(const char *text, size_t clen = 0, size_t offset = 0) const; 00213 00214 public: 00215 static const size_t npos; 00216 00217 typedef size_t size_type; 00218 00222 String(); 00223 00229 String(const String &original); 00230 00236 String(const char *str); 00237 00243 String(std::string string); 00244 00252 String(const String &str, size_t offset, size_t len = npos); 00253 00254 #ifdef HAVE_SNPRINTF 00255 00261 String(size_t size, const char *format, ...); 00262 #else 00263 00270 String(size_t count, const char *str); 00271 #endif 00272 00279 String(size_t count, const char fill = ' '); 00280 00284 virtual ~String(); 00285 00293 const char *getIndex(size_t index) const; 00294 00300 char *getText(void) const; 00301 00307 long getValue(long defvalue = 0l) const; 00308 00314 bool getBool(bool defbool = false) const; 00315 00321 const size_t getLength(void) const; 00322 00328 const size_t getSize(void) const; 00329 00335 bool isEmpty(void) const; 00336 00342 void resize(size_t size); 00343 00347 void clear(void); 00348 00354 char at(ssize_t offset) const; 00355 00364 unsigned count(const String &s, size_t offset = 0) const; 00365 00375 unsigned count(const char *s, size_t offset = 0, size_t len = 0) const; 00376 00384 String token(const char *delim = " \t\n\r", size_t offset = 0); 00385 00394 size_t find(const String &s, size_t offset = 0, unsigned instance = 1) const; 00395 00403 size_t rfind(const String &s, size_t offset = 0) const; 00404 00414 size_t find(const char *s, size_t offset = 0, size_t len = 0, unsigned count = 1) const; 00415 00424 size_t rfind(const char *s, size_t offset = 0, size_t len = 0) const; 00425 00431 inline void trim(const char *cs) 00432 {setLength(strtrim(cs, getText(), getLength()));}; 00433 00439 inline void chop(const char *cs) 00440 {setLength(strchop(cs, getText(), getLength()));}; 00441 00447 void strip(const char *cs); 00448 00454 inline void chop(size_t chars) 00455 {erase(0, chars);}; 00456 00462 void trim(size_t count); 00463 00470 void erase(size_t start, size_t len = npos); 00471 00479 void insert(size_t start, const char *text, size_t len = 0); 00480 00487 void insert(size_t start, const String &str); 00488 00498 void replace(size_t start, size_t len, const char *text, size_t count = 0); 00499 00508 void replace(size_t start, size_t len, const String &string); 00509 00519 inline size_t find(unsigned instance, const char *text, size_t offset = 0, size_t len = 0) const 00520 {return find(text, offset, len, instance);}; 00521 00530 inline size_t find(unsigned instance, const String &string, size_t offset = 0) const 00531 {return find(string, offset, instance);}; 00532 00541 inline String substr(size_t start, size_t len) const 00542 {return String(*this, start, len);}; 00543 00551 inline const char *(index)(size_t ind) const 00552 {return getIndex(ind);}; 00553 00558 inline void compact(void) 00559 {resize(getLength() + 1);}; 00560 00566 inline char *c_str(void) const 00567 {return getText();}; 00568 00574 inline operator char *() const 00575 {return getText();}; 00576 00582 inline bool operator!(void) const 00583 {return isEmpty();}; 00584 00590 inline char *text(void) const 00591 {return getText();}; 00592 00598 inline char *data(void) const 00599 {return getText();}; 00600 00606 inline size_t length(void) const 00607 {return strlen(getText());}; 00608 00614 inline size_t size(void) const 00615 {return getLength();}; 00616 00622 inline size_t capacity(void) const 00623 {return getSize();}; 00624 00628 bool empty(void) const 00629 {return isEmpty();}; 00630 00637 void append(const char *str, size_t count = 0); 00638 00639 #ifdef HAVE_SNPRINTF 00640 00646 void append(size_t size, const char *format, ...); 00647 #endif 00648 00656 void append(const char *str, size_t offset, size_t count); 00657 00663 void add(char c); 00664 00670 void append(const String &str); 00671 00677 inline const char operator[](unsigned ind) const 00678 {return at(ind);}; 00679 00683 inline const char *operator =(const char *str) 00684 {return set(str);}; 00685 00689 friend __EXPORT String operator+(const String &s1, const String &s2); 00690 00691 friend __EXPORT String operator+(const String &s1, const char *s2); 00692 00693 friend __EXPORT String operator+(const char *s1, const String &s2); 00694 00695 friend __EXPORT String operator+(const String &s1, const char c2); 00696 00697 friend __EXPORT String operator+(const char c1, const String &s2); 00698 00702 inline String &operator+=(const String &str) 00703 {append(str); return *this;}; 00704 00708 inline String &operator+=(char c) 00709 {add(c); return *this;}; 00710 00714 inline String &operator+=(const char *str) 00715 {append(str); return *this;}; 00716 00720 inline String &operator+=(const std::string &str) 00721 {append(str.c_str()); return *this;}; 00722 00733 friend __EXPORT std::istream &getline(std::istream &is, String &str, char delim = '\n', size_t size = 0); 00734 00739 friend __EXPORT std::ostream &operator<<(std::ostream &os, const String &str); 00740 00744 inline friend std::istream &operator>>(std::istream &is, String &str) 00745 {return getline(is, str);}; 00746 00747 #ifdef HAVE_SNPRINTF 00748 00756 friend __EXPORT int strprintf(String &str, size_t size, const char *format, ...); 00757 #endif 00758 00759 bool operator<(const String &str) const; 00760 bool operator<(const char *str) const; 00761 bool operator>(const String &str) const; 00762 bool operator>(const char *str) const; 00763 bool operator<=(const String &str) const; 00764 bool operator<=(const char *str) const; 00765 bool operator>=(const String &str) const; 00766 bool operator>=(const char *str) const; 00767 bool operator==(const String &str) const; 00768 bool operator==(const char *str) const; 00769 bool operator!=(const String &str) const; 00770 bool operator!=(const char *str) const; 00771 00772 #ifdef HAVE_SNPRINTF 00773 00777 inline String &operator+=(int i) 00778 {append(16, "%d", i); return *this;}; 00779 00780 inline String &operator+=(unsigned int i) 00781 {append(16, "%u", i); return *this;}; 00782 00783 inline String &operator+=(long l) 00784 {append(16, "%l", l); return *this;}; 00785 00786 inline String &operator+=(unsigned long l) 00787 {append(16, "%ul", l); return *this;}; 00788 00789 inline String &operator+=(float f) 00790 {append(32, "%f", f); return *this;}; 00791 00792 inline String &operator+=(double d) 00793 {append(32, "%f", d); return *this;}; 00794 00795 inline String &operator+=(short s) 00796 {append(8, "%hd", s); return *this;}; 00797 00798 inline String &operator+=(unsigned short s) 00799 {append(8, "%hu", s); return *this;}; 00800 00801 00805 inline String &operator=(int i) 00806 {set(16, "%d", i); return *this;}; 00807 00808 inline String &operator=(unsigned int i) 00809 {set(16, "%u", i); return *this;}; 00810 00811 inline String &operator=(long l) 00812 {set(16, "%l", l); return *this;}; 00813 00814 inline String &operator=(unsigned long l) 00815 {set(16, "%ul", l); return *this;}; 00816 00817 inline String &operator=(float f) 00818 {set(32, "%f", f); return *this;}; 00819 00820 inline String &operator=(double d) 00821 {set(32, "%f", d); return *this;}; 00822 00823 inline String &operator=(short s) 00824 {set(8, "%hd", s); return *this;}; 00825 00826 inline String &operator=(unsigned short s) 00827 {set(8, "%hu", s); return *this;}; 00828 #endif 00829 00830 inline String &operator=(const String &original) 00831 {copy(original); return *this;}; 00832 00836 bool operator*=(const String &str) const; 00837 00841 bool operator*=(const char *str) const; 00842 }; 00843 00844 class __EXPORT SString : public String, protected std::streambuf, public std::ostream 00845 { 00846 protected: 00852 int overflow(int c); 00853 00854 public: 00858 SString(); 00859 00863 SString(const SString &from); 00864 00868 ~SString(); 00869 }; 00870 00880 class __EXPORT StringObject 00881 { 00882 public: 00886 void *operator new(size_t size) NEW_THROWS; 00887 00891 void operator delete(void *obj); 00892 }; 00893 00894 #ifdef CCXX_NAMESPACES 00895 } 00896 #endif 00897 00898 #endif