Yate
|
00001 /* 00002 * yateclass.h 00003 * This file is part of the YATE Project http://YATE.null.ro 00004 * 00005 * Base classes and types, not related to the engine or telephony 00006 * 00007 * Yet Another Telephony Engine - a fully featured software PBX and IVR 00008 * Copyright (C) 2004-2006 Null Team 00009 * 00010 * This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU General Public License 00021 * along with this program; if not, write to the Free Software 00022 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 00023 */ 00024 00025 #ifndef __YATECLASS_H 00026 #define __YATECLASS_H 00027 00028 #ifndef __cplusplus 00029 #error C++ is required 00030 #endif 00031 00032 #include <sys/types.h> 00033 #include <stddef.h> 00034 #include <unistd.h> 00035 #include <errno.h> 00036 00037 #ifndef _WORDSIZE 00038 #if defined(__arch64__) || defined(__x86_64__) \ 00039 || defined(__amd64__) || defined(__ia64__) \ 00040 || defined(__alpha__) || defined(__sparcv9) 00041 #define _WORDSIZE 64 00042 #else 00043 #define _WORDSIZE 32 00044 #endif 00045 #endif 00046 00047 #ifndef _WINDOWS 00048 #if defined(WIN32) || defined(_WIN32) 00049 #define _WINDOWS 00050 #endif 00051 #endif 00052 00053 #ifdef _WINDOWS 00054 00055 #include <windows.h> 00056 #include <io.h> 00057 #include <direct.h> 00058 00062 typedef signed __int8 int8_t; 00063 typedef unsigned __int8 u_int8_t; 00064 typedef unsigned __int8 uint8_t; 00065 typedef signed __int16 int16_t; 00066 typedef unsigned __int16 u_int16_t; 00067 typedef unsigned __int16 uint16_t; 00068 typedef signed __int32 int32_t; 00069 typedef unsigned __int32 u_int32_t; 00070 typedef unsigned __int32 uint32_t; 00071 typedef signed __int64 int64_t; 00072 typedef unsigned __int64 u_int64_t; 00073 typedef unsigned __int64 uint64_t; 00074 00075 typedef int pid_t; 00076 typedef int socklen_t; 00077 typedef unsigned long in_addr_t; 00078 00079 #ifndef strcasecmp 00080 #define strcasecmp _stricmp 00081 #endif 00082 00083 #ifndef strncasecmp 00084 #define strncasecmp _strnicmp 00085 #endif 00086 00087 #define vsnprintf _vsnprintf 00088 #define snprintf _snprintf 00089 #define strdup _strdup 00090 #define open _open 00091 #define dup2 _dup2 00092 #define read _read 00093 #define write _write 00094 #define close _close 00095 #define getpid _getpid 00096 #define chdir _chdir 00097 #define mkdir(p,m) _mkdir(p) 00098 #define unlink _unlink 00099 00100 #define O_RDWR _O_RDWR 00101 #define O_RDONLY _O_RDONLY 00102 #define O_WRONLY _O_WRONLY 00103 #define O_APPEND _O_APPEND 00104 #define O_BINARY _O_BINARY 00105 #define O_EXCL _O_EXCL 00106 #define O_CREAT _O_CREAT 00107 #define O_TRUNC _O_TRUNC 00108 #define O_NOCTTY 0 00109 00110 #define S_IRUSR _S_IREAD 00111 #define S_IWUSR _S_IWRITE 00112 #define S_IXUSR 0 00113 #define S_IRWXU (_S_IREAD|_S_IWRITE) 00114 00115 #ifdef LIBYATE_EXPORTS 00116 #define YATE_API __declspec(dllexport) 00117 #else 00118 #ifndef LIBYATE_STATIC 00119 #define YATE_API __declspec(dllimport) 00120 #endif 00121 #endif 00122 00123 #define FMT64 "%I64d" 00124 #define FMT64U "%I64u" 00125 00126 #else /* _WINDOWS */ 00127 00128 #include <sys/time.h> 00129 #include <sys/socket.h> 00130 00131 #if defined(__FreeBSD__) 00132 #include <netinet/in_systm.h> 00133 #endif 00134 00135 #include <netinet/in.h> 00136 #include <netinet/ip.h> 00137 #include <netinet/tcp.h> 00138 #include <arpa/inet.h> 00139 #include <netdb.h> 00140 00144 #ifndef SOCKET 00145 typedef int SOCKET; 00146 #endif 00147 #ifndef HANDLE 00148 typedef int HANDLE; 00149 #endif 00150 00151 #ifndef O_BINARY 00152 #define O_BINARY 0 00153 #endif 00154 00155 #if _WORDSIZE == 64 00156 #define FMT64 "%ld" 00157 #define FMT64U "%lu" 00158 #else 00159 #define FMT64 "%lld" 00160 #define FMT64U "%llu" 00161 #endif 00162 00163 #endif /* ! _WINDOWS */ 00164 00165 #ifndef IPTOS_LOWDELAY 00166 #define IPTOS_LOWDELAY 0x10 00167 #define IPTOS_THROUGHPUT 0x08 00168 #define IPTOS_RELIABILITY 0x04 00169 #endif 00170 #ifndef IPTOS_MINCOST 00171 #define IPTOS_MINCOST 0x02 00172 #endif 00173 00174 #ifndef YATE_API 00175 #define YATE_API 00176 #endif 00177 00178 #ifdef _WINDOWS 00179 #undef RAND_MAX 00180 #define RAND_MAX 2147483647 00181 extern "C" { 00182 YATE_API long int random(); 00183 YATE_API void srandom(unsigned int seed); 00184 } 00185 #endif 00186 00190 namespace TelEngine { 00191 00192 #ifdef HAVE_GCC_FORMAT_CHECK 00193 #define FORMAT_CHECK(f) __attribute__((format(printf,(f),(f)+1))) 00194 #else 00195 #define FORMAT_CHECK(f) 00196 #endif 00197 00202 YATE_API void abortOnBug(); 00203 00208 YATE_API bool abortOnBug(bool doAbort); 00209 00215 enum DebugLevel { 00216 DebugFail = 0, 00217 DebugGoOn = 2, 00218 DebugStub = 4, 00219 DebugWarn = 5, 00220 DebugMild = 6, 00221 DebugCall = 7, 00222 DebugNote = 8, 00223 DebugInfo = 9, 00224 DebugAll = 10 00225 }; 00226 00231 YATE_API int debugLevel(); 00232 00238 YATE_API int debugLevel(int level); 00239 00245 YATE_API bool debugAt(int level); 00246 00253 YATE_API const char* debugColor(int level); 00254 00260 class YATE_API DebugEnabler 00261 { 00262 public: 00268 inline DebugEnabler(int level = TelEngine::debugLevel(), bool enabled = true) 00269 : m_level(DebugFail), m_enabled(enabled), m_chain(0), m_name(0) 00270 { debugLevel(level); } 00271 00272 inline ~DebugEnabler() 00273 { m_name = 0; m_chain = 0; } 00274 00279 inline int debugLevel() const 00280 { return m_chain ? m_chain->debugLevel() : m_level; } 00281 00287 int debugLevel(int level); 00288 00293 inline bool debugEnabled() const 00294 { return m_chain ? m_chain->debugEnabled() : m_enabled; } 00295 00300 inline void debugEnabled(bool enable) 00301 { m_enabled = enable; m_chain = 0; } 00302 00307 inline const char* debugName() const 00308 { return m_name; } 00309 00315 bool debugAt(int level) const; 00316 00321 inline bool debugChained() const 00322 { return m_chain != 0; } 00323 00328 inline void debugChain(const DebugEnabler* chain = 0) 00329 { m_chain = (chain != this) ? chain : 0; } 00330 00335 void debugCopy(const DebugEnabler* original = 0); 00336 00337 protected: 00342 inline void debugName(const char* name) 00343 { m_name = name; } 00344 00345 private: 00346 int m_level; 00347 bool m_enabled; 00348 const DebugEnabler* m_chain; 00349 const char* m_name; 00350 }; 00351 00352 #if 0 /* for documentation generator */ 00353 00358 void DDebug(int level, const char* format, ...); 00359 00365 void DDebug(const char* facility, int level, const char* format, ...); 00366 00372 void DDebug(const DebugEnabler* local, int level, const char* format, ...); 00373 00379 void XDebug(int level, const char* format, ...); 00380 00386 void XDebug(const char* facility, int level, const char* format, ...); 00387 00393 void XDebug(const DebugEnabler* local, int level, const char* format, ...); 00394 00400 void NDebug(int level, const char* format, ...); 00401 00407 void NDebug(const char* facility, int level, const char* format, ...); 00408 00414 void NDebug(const DebugEnabler* local, int level, const char* format, ...); 00415 #endif 00416 00417 #ifdef _DEBUG 00418 #undef DEBUG 00419 #define DEBUG 00420 #endif 00421 00422 #ifdef XDEBUG 00423 #undef DEBUG 00424 #define DEBUG 00425 #endif 00426 00427 #ifdef DEBUG 00428 #define DDebug Debug 00429 #else 00430 #ifdef _WINDOWS 00431 #define DDebug 00432 #else 00433 #define DDebug(arg...) 00434 #endif 00435 #endif 00436 00437 #ifdef XDEBUG 00438 #define XDebug Debug 00439 #else 00440 #ifdef _WINDOWS 00441 #define XDebug 00442 #else 00443 #define XDebug(arg...) 00444 #endif 00445 #endif 00446 00447 #ifndef NDEBUG 00448 #define NDebug Debug 00449 #else 00450 #ifdef _WINDOWS 00451 #define NDebug 00452 #else 00453 #define NDebug(arg...) 00454 #endif 00455 #endif 00456 00462 YATE_API void Debug(int level, const char* format, ...) FORMAT_CHECK(2); 00463 00470 YATE_API void Debug(const char* facility, int level, const char* format, ...) FORMAT_CHECK(3); 00471 00478 YATE_API void Debug(const DebugEnabler* local, int level, const char* format, ...) FORMAT_CHECK(3); 00479 00484 YATE_API void Output(const char* format, ...) FORMAT_CHECK(1); 00485 00492 class YATE_API Debugger 00493 { 00494 public: 00498 enum Formatting { 00499 None = 0, 00500 Relative, // from program start 00501 Absolute, // from EPOCH (1-1-1970) 00502 Textual, // absolute GMT in YYYYMMDDhhmmss.uuuuuu format 00503 }; 00504 00510 Debugger(const char* name, const char* format = 0, ...); 00511 00518 Debugger(int level, const char* name, const char* format = 0, ...); 00519 00523 ~Debugger(); 00524 00529 static void setOutput(void (*outFunc)(const char*,int) = 0); 00530 00535 static void setIntOut(void (*outFunc)(const char*,int) = 0); 00536 00542 static void enableOutput(bool enable = true, bool colorize = false); 00543 00548 static void setFormatting(Formatting format); 00549 00550 private: 00551 const char* m_name; 00552 }; 00553 00558 struct TokenDict { 00562 const char* token; 00563 00567 int value; 00568 }; 00569 00570 class String; 00571 class Mutex; 00572 00573 #if 0 /* for documentation generator */ 00574 00579 void YCLASS(class type,class base); 00580 00587 void YCLASS2(class type,class base1,class base2); 00588 00596 void YCLASS3(class type,class base1,class base2,class base3); 00597 00603 void YCLASSIMP(class type,class base); 00604 00611 void YCLASSIMP2(class type,class base1,class base2); 00612 00620 void YCLASSIMP3(class type,class base1,class base2,class base3); 00621 00628 class* YOBJECT(class type,GenObject* pntr); 00629 #endif 00630 00631 #define YCLASS(type,base) \ 00632 public: virtual void* getObject(const String& name) const \ 00633 { return (name == #type) ? const_cast<type*>(this) : base::getObject(name); } 00634 00635 #define YCLASS2(type,base1,base2) \ 00636 public: virtual void* getObject(const String& name) const \ 00637 { if (name == #type) return const_cast<type*>(this); \ 00638 void* tmp = base1::getObject(name); \ 00639 return tmp ? tmp : base2::getObject(name); } 00640 00641 #define YCLASS3(type,base1,base2,base3) \ 00642 public: virtual void* getObject(const String& name) const \ 00643 { if (name == #type) return const_cast<type*>(this); \ 00644 void* tmp = base1::getObject(name); \ 00645 if (tmp) return tmp; \ 00646 tmp = base2::getObject(name); \ 00647 return tmp ? tmp : base3::getObject(name); } 00648 00649 #define YCLASSIMP(type,base) \ 00650 void* type::getObject(const String& name) const \ 00651 { return (name == #type) ? const_cast<type*>(this) : base::getObject(name); } 00652 00653 #define YCLASSIMP2(type,base1,base2) \ 00654 void* type::getObject(const String& name) const \ 00655 { if (name == #type) return const_cast<type*>(this); \ 00656 void* tmp = base1::getObject(name); \ 00657 return tmp ? tmp : base2::getObject(name); } 00658 00659 #define YCLASSIMP3(type,base1,base2,base3) \ 00660 void* type::getObject(const String& name) const \ 00661 { if (name == #type) return const_cast<type*>(this); \ 00662 void* tmp = base1::getObject(name); \ 00663 if (tmp) return tmp; \ 00664 tmp = base2::getObject(name); \ 00665 return tmp ? tmp : base3::getObject(name); } 00666 00667 #define YOBJECT(type,pntr) (static_cast<type*>((pntr) ? (pntr)->getObject(#type) : 0)) 00668 00672 class YATE_API GenObject 00673 { 00674 public: 00678 virtual ~GenObject() { } 00679 00686 virtual bool alive() const; 00687 00691 virtual void destruct(); 00692 00699 virtual const String& toString() const; 00700 00706 virtual void* getObject(const String& name) const; 00707 }; 00708 00714 inline void destruct(GenObject* obj) 00715 { if (obj) obj->destruct(); } 00716 00723 template <class Obj> void destruct(Obj*& obj) 00724 { if (obj) { obj->destruct(); obj = 0; } } 00725 00730 class YATE_API RefObject : public GenObject 00731 { 00732 public: 00737 RefObject() 00738 : m_refcount(1) { } 00739 00743 virtual ~RefObject(); 00744 00751 virtual bool alive() const; 00752 00757 bool ref(); 00758 00767 bool deref(); 00768 00773 inline int refcount() const 00774 { return m_refcount; } 00775 00780 virtual void destruct(); 00781 00786 static Mutex& refMutex(); 00787 00788 protected: 00794 virtual void zeroRefs(); 00795 00804 virtual bool zeroRefsTest(); 00805 00811 bool refInternal(); 00812 00818 bool resurrect(); 00819 00825 virtual void destroyed(); 00826 00827 private: 00828 int m_refcount; 00829 }; 00830 00836 class YATE_API RefPointerBase 00837 { 00838 protected: 00842 inline RefPointerBase() 00843 : m_pointer(0) { } 00844 00851 void assign(RefObject* oldptr, RefObject* newptr, void* pointer); 00852 00856 void* m_pointer; 00857 }; 00858 00862 template <class Obj = RefObject> class RefPointer : public RefPointerBase 00863 { 00864 protected: 00869 inline Obj* pointer() const 00870 { return static_cast<Obj*>(m_pointer); } 00871 00876 inline void assign(Obj* object = 0) 00877 { RefPointerBase::assign(pointer(),object,object); } 00878 00879 public: 00883 inline RefPointer() 00884 { } 00885 00890 inline RefPointer(const RefPointer<Obj>& value) 00891 : RefPointerBase() 00892 { assign(value); } 00893 00898 inline RefPointer(Obj* object) 00899 { assign(object); } 00900 00904 inline ~RefPointer() 00905 { assign(); } 00906 00910 inline RefPointer<Obj>& operator=(const RefPointer<Obj>& value) 00911 { assign(value.pointer()); return *this; } 00912 00916 inline RefPointer<Obj>& operator=(Obj* object) 00917 { assign(object); return *this; } 00918 00923 inline operator Obj*() const 00924 { return pointer(); } 00925 00929 inline Obj* operator->() const 00930 { return pointer(); } 00931 00935 inline Obj& operator*() const 00936 { return *pointer(); } 00937 }; 00938 00942 template <class Obj = GenObject> class GenPointer : public GenObject 00943 { 00944 private: 00948 Obj* m_pointer; 00949 00950 public: 00954 inline GenPointer() 00955 : m_pointer(0) 00956 { } 00957 00962 inline GenPointer(const GenPointer<Obj>& value) 00963 : m_pointer(value) 00964 { } 00965 00970 inline GenPointer(Obj* object) 00971 : m_pointer(object) 00972 { } 00973 00977 inline GenPointer<Obj>& operator=(const GenPointer<Obj>& value) 00978 { m_pointer = value; return *this; } 00979 00983 inline GenPointer<Obj>& operator=(Obj* object) 00984 { m_pointer = object; return *this; } 00985 00990 inline operator Obj*() const 00991 { return m_pointer; } 00992 00996 inline Obj* operator->() const 00997 { return m_pointer; } 00998 01002 inline Obj& operator*() const 01003 { return *m_pointer; } 01004 }; 01005 01010 class YATE_API ObjList : public GenObject 01011 { 01012 public: 01016 ObjList(); 01017 01021 virtual ~ObjList(); 01022 01028 virtual void* getObject(const String& name) const; 01029 01034 unsigned int length() const; 01035 01040 unsigned int count() const; 01041 01046 inline GenObject* get() const 01047 { return m_obj; } 01048 01055 GenObject* set(const GenObject* obj, bool delold = true); 01056 01061 inline ObjList* next() const 01062 { return m_next; } 01063 01068 ObjList* last() const; 01069 01074 ObjList* skipNull() const; 01075 01080 ObjList* skipNext() const; 01081 01087 GenObject* at(int index) const; 01088 01094 ObjList* operator+(int index) const; 01095 01101 inline GenObject* operator[](signed int index) const 01102 { return at(index); } 01103 01109 inline GenObject* operator[](unsigned int index) const 01110 { return at(index); } 01111 01117 GenObject* operator[](const String& str) const; 01118 01124 ObjList* find(const GenObject* obj) const; 01125 01131 ObjList* find(const String& str) const; 01132 01138 int index(const GenObject* obj) const; 01139 01145 int index(const String& str) const; 01146 01153 ObjList* insert(const GenObject* obj, bool compact = true); 01154 01161 ObjList* append(const GenObject* obj, bool compact = true); 01162 01168 GenObject* remove(bool delobj = true); 01169 01176 GenObject* remove(GenObject* obj, bool delobj = true); 01177 01184 GenObject* remove(const String& str, bool delobj = true); 01185 01189 void clear(); 01190 01195 inline bool autoDelete() 01196 { return m_delete; } 01197 01202 inline void setDelete(bool autodelete) 01203 { m_delete = autodelete; } 01204 01209 static const ObjList& empty(); 01210 01211 private: 01212 ObjList* m_next; 01213 GenObject* m_obj; 01214 bool m_delete; 01215 }; 01216 01225 class YATE_API Array : public RefObject 01226 { 01227 public: 01233 Array(int columns = 0, int rows = 0); 01234 01238 virtual ~Array(); 01239 01245 virtual void* getObject(const String& name) const; 01246 01253 bool addRow(ObjList* row = 0, int index = -1); 01254 01261 bool addColumn(ObjList* column = 0, int index = -1); 01262 01268 bool delRow(int index); 01269 01275 bool delColumn(int index); 01276 01283 GenObject* get(int column, int row) const; 01284 01291 GenObject* take(int column, int row); 01292 01300 bool set(GenObject* obj, int column, int row); 01301 01306 inline int getRows() const 01307 { return m_rows; } 01308 01313 inline int getColumns() const 01314 { return m_columns; } 01315 01316 private: 01317 int m_rows; 01318 int m_columns; 01319 ObjList m_obj; 01320 }; 01321 01322 class Regexp; 01323 class StringMatchPrivate; 01324 01332 class YATE_API String : public GenObject 01333 { 01334 public: 01338 String(); 01339 01345 String(const char* value, int len = -1); 01346 01352 explicit String(char value, unsigned int repeat = 1); 01353 01358 explicit String(int value); 01359 01364 explicit String(unsigned int value); 01365 01370 explicit String(bool value); 01371 01376 String(const String& value); 01377 01382 String(const String* value); 01383 01387 virtual ~String(); 01388 01394 virtual void* getObject(const String& name) const; 01395 01400 static const String& empty(); 01401 01407 inline static const char* boolText(bool value) 01408 { return value ? "true" : "false"; } 01409 01414 inline const char* c_str() const 01415 { return m_string; } 01416 01421 inline const char* safe() const 01422 { return m_string ? m_string : ""; } 01423 01429 inline const char* safe(const char* defStr) const 01430 { return m_string ? m_string : (defStr ? defStr : ""); } 01431 01436 inline unsigned int length() const 01437 { return m_length; } 01438 01443 inline bool null() const 01444 { return !m_string; } 01445 01453 static int lenUtf8(const char* value, unsigned int maxSeq = 4, bool overlong = false); 01454 01461 inline int lenUtf8(unsigned int maxSeq = 4, bool overlong = false) const 01462 { return lenUtf8(m_string,maxSeq,overlong); } 01463 01464 01472 int fixUtf8(const char* replace = 0, unsigned int maxSeq = 4, bool overlong = false); 01473 01478 unsigned int hash() const; 01479 01485 static unsigned int hash(const char* value); 01486 01490 void clear(); 01491 01497 char at(int index) const; 01498 01505 String substr(int offs, int len = -1) const; 01506 01510 String& trimBlanks(); 01511 01516 String& trimSpaces(); 01517 01522 virtual const String& toString() const; 01523 01530 int toInteger(int defvalue = 0, int base = 0) const; 01531 01539 int toInteger(const TokenDict* tokens, int defvalue = 0, int base = 0) const; 01540 01546 double toDouble(double defvalue = 0.0) const; 01547 01553 bool toBoolean(bool defvalue = false) const; 01554 01559 bool isBoolean() const; 01560 01565 String& toUpper(); 01566 01571 String& toLower(); 01572 01578 inline char operator[](signed int index) const 01579 { return at(index); } 01580 01586 inline char operator[](unsigned int index) const 01587 { return at(index); } 01588 01593 inline operator const char*() const 01594 { return m_string; }; 01595 01602 String& assign(const char* value, int len = -1); 01603 01610 String& assign(char value, unsigned int repeat = 1); 01611 01620 String& hexify(void* data, unsigned int len, char sep = 0, bool upCase = false); 01621 01625 inline String& operator=(const String& value) 01626 { return operator=(value.c_str()); } 01627 01632 inline String& operator=(const String* value) 01633 { return operator=(value ? value->c_str() : ""); } 01634 01639 String& operator=(const char* value); 01640 01644 String& operator=(char value); 01645 01649 String& operator=(int value); 01650 01654 String& operator=(unsigned int value); 01655 01659 inline String& operator=(bool value) 01660 { return operator=(boolText(value)); } 01661 01666 String& operator+=(const char* value); 01667 01671 String& operator+=(char value); 01672 01676 String& operator+=(int value); 01677 01681 String& operator+=(unsigned int value); 01682 01686 inline String& operator+=(bool value) 01687 { return operator+=(boolText(value)); } 01688 01692 bool operator==(const char* value) const; 01693 01697 bool operator!=(const char* value) const; 01698 01702 bool operator==(const String& value) const; 01703 01707 bool operator!=(const String& value) const; 01708 01712 bool operator&=(const char* value) const; 01713 01717 bool operator|=(const char* value) const; 01718 01722 inline String& operator<<(const char* value) 01723 { return operator+=(value); } 01724 01728 inline String& operator<<(char value) 01729 { return operator+=(value); } 01730 01734 inline String& operator<<(int value) 01735 { return operator+=(value); } 01736 01740 inline String& operator<<(unsigned int value) 01741 { return operator+=(value); } 01742 01746 inline String& operator<<(bool value) 01747 { return operator+=(value); } 01748 01753 String& operator>>(const char* skip); 01754 01758 String& operator>>(char& store); 01759 01763 String& operator>>(int& store); 01764 01768 String& operator>>(unsigned int& store); 01769 01773 String& operator>>(bool& store); 01774 01781 String& append(const char* value, const char* separator = 0, bool force = false); 01782 01789 String& append(const ObjList* list, const char* separator = 0, bool force = false); 01790 01797 inline String& append(const ObjList& list, const char* separator = 0, bool force = false) 01798 { return append(&list,separator,force); } 01799 01805 String& append(double value, unsigned int decimals = 3); 01806 01813 int find(char what, unsigned int offs = 0) const; 01814 01821 int find(const char* what, unsigned int offs = 0) const; 01822 01828 int rfind(char what) const; 01829 01837 bool startsWith(const char* what, bool wordBreak = false, bool caseInsensitive = false) const; 01838 01846 bool endsWith(const char* what, bool wordBreak = false, bool caseInsensitive = false) const; 01847 01859 bool startSkip(const char* what, bool wordBreak = true, bool caseInsensitive = false); 01860 01866 virtual bool matches(const String& value) const 01867 { return operator==(value); } 01868 01874 bool matches(Regexp& rexp); 01875 01881 int matchOffset(int index = 0) const; 01882 01888 int matchLength(int index = 0) const; 01889 01895 inline String matchString(int index = 0) const 01896 { return substr(matchOffset(index),matchLength(index)); } 01897 01903 String replaceMatches(const String& templ) const; 01904 01909 int matchCount() const; 01910 01917 ObjList* split(char separator, bool emptyOK = true) const; 01918 01925 static String msgEscape(const char* str, char extraEsc = 0); 01926 01932 inline String msgEscape(char extraEsc = 0) const 01933 { return msgEscape(c_str(),extraEsc); } 01934 01942 static String msgUnescape(const char* str, int* errptr = 0, char extraEsc = 0); 01943 01950 inline String msgUnescape(int* errptr = 0, char extraEsc = 0) const 01951 { return msgUnescape(c_str(),errptr,extraEsc); } 01952 01959 static String sqlEscape(const char* str, char extraEsc = 0); 01960 01966 inline String sqlEscape(char extraEsc = 0) const 01967 { return sqlEscape(c_str(),extraEsc); } 01968 01976 static String uriEscape(const char* str, char extraEsc = 0, const char* noEsc = 0); 01977 01984 inline String uriEscape(char extraEsc = 0, const char* noEsc = 0) const 01985 { return uriEscape(c_str(),extraEsc,noEsc); } 01986 01993 static String uriUnescape(const char* str, int* errptr = 0); 01994 02000 inline String uriUnescape(int* errptr = 0) const 02001 { return uriUnescape(c_str(),errptr); } 02002 02003 protected: 02007 virtual void changed(); 02008 02009 private: 02010 void clearMatches(); 02011 char* m_string; 02012 unsigned int m_length; 02013 // I hope every C++ compiler now knows about mutable... 02014 mutable unsigned int m_hash; 02015 StringMatchPrivate* m_matches; 02016 }; 02017 02023 inline const char* c_str(const String* str) 02024 { return str ? str->c_str() : (const char*)0; } 02025 02031 inline const char* c_safe(const char* str) 02032 { return str ? str : ""; } 02033 02039 inline const char* c_safe(const String* str) 02040 { return str ? str->safe() : ""; } 02041 02047 inline bool null(const char* str) 02048 { return !(str && *str); } 02049 02055 inline bool null(const String* str) 02056 { return !str || str->null(); } 02057 02061 YATE_API String operator+(const String& s1, const String& s2); 02062 02066 YATE_API String operator+(const String& s1, const char* s2); 02067 02071 YATE_API String operator+(const char* s1, const String& s2); 02072 02077 inline const char *strcpy(String& dest, const char* src) 02078 { dest = src; return dest.c_str(); } 02079 02084 inline const char *strcat(String& dest, const char* src) 02085 { dest += src; return dest.c_str(); } 02086 02095 YATE_API int lookup(const char* str, const TokenDict* tokens, int defvalue = 0, int base = 0); 02096 02103 YATE_API const char* lookup(int value, const TokenDict* tokens, const char* defvalue = 0); 02104 02105 02110 class YATE_API Regexp : public String 02111 { 02112 friend class String; 02113 public: 02117 Regexp(); 02118 02125 Regexp(const char* value, bool extended = false, bool insensitive = false); 02126 02131 Regexp(const Regexp& value); 02132 02136 virtual ~Regexp(); 02137 02141 inline Regexp& operator=(const char* value) 02142 { String::operator=(value); return *this; } 02143 02148 bool compile(); 02149 02155 bool matches(const char* value) const; 02156 02162 virtual bool matches(const String& value) const 02163 { return Regexp::matches(value.safe()); } 02164 02170 void setFlags(bool extended, bool insensitive); 02171 02176 bool isExtended() const; 02177 02182 bool isCaseInsensitive() const; 02183 02184 protected: 02188 virtual void changed(); 02189 02190 private: 02191 void cleanup(); 02192 bool matches(const char *value, StringMatchPrivate *matchlist); 02193 void* m_regexp; 02194 int m_flags; 02195 }; 02196 02201 class YATE_API NamedString : public String 02202 { 02203 public: 02209 NamedString(const char* name, const char* value = 0); 02210 02215 inline const String& name() const 02216 { return m_name; } 02217 02222 virtual const String& toString() const; 02223 02229 virtual void* getObject(const String& name) const; 02230 02234 inline NamedString& operator=(const char* value) 02235 { String::operator=(value); return *this; } 02236 02237 private: 02238 NamedString(); // no default constructor please 02239 String m_name; 02240 }; 02241 02248 class YATE_API NamedPointer : public NamedString 02249 { 02250 public: 02257 NamedPointer(const char* name, GenObject* data = 0, const char* value = 0); 02258 02262 virtual ~NamedPointer(); 02263 02268 inline GenObject* userData() const 02269 { return m_data; } 02270 02276 GenObject* takeData(); 02277 02283 void userData(GenObject* data); 02284 02290 inline void* userObject(const String& name) const 02291 { return m_data ? m_data->getObject(name) : 0; } 02292 02296 inline NamedPointer& operator=(const char* value) 02297 { NamedString::operator=(value); return *this; } 02298 02304 virtual void* getObject(const String& name) const; 02305 02306 protected: 02310 virtual void changed(); 02311 02312 private: 02313 NamedPointer(); // no default constructor please 02314 GenObject* m_data; 02315 }; 02316 02324 class YATE_API HashList : public GenObject 02325 { 02326 public: 02331 HashList(unsigned int size = 17); 02332 02336 virtual ~HashList(); 02337 02343 virtual void* getObject(const String& name) const; 02344 02349 inline unsigned int length() const 02350 { return m_size; } 02351 02356 unsigned int count() const; 02357 02364 inline ObjList* getList(unsigned int index) const 02365 { return (index < m_size) ? m_lists[index] : 0; } 02366 02372 inline ObjList* getHashList(unsigned int hash) const 02373 { return getList(hash % m_size); } 02374 02380 inline ObjList* getHashList(const String& str) const 02381 { return getHashList(str.hash()); } 02382 02388 GenObject* operator[](const String& str) const; 02389 02395 ObjList* find(const GenObject* obj) const; 02396 02402 ObjList* find(const String& str) const; 02403 02409 ObjList* append(const GenObject* obj); 02410 02417 GenObject* remove(GenObject* obj, bool delobj = true); 02418 02422 void clear(); 02423 02430 bool resync(GenObject* obj); 02431 02437 bool resync(); 02438 02439 private: 02440 unsigned int m_size; 02441 ObjList** m_lists; 02442 }; 02443 02450 class YATE_API ListIterator 02451 { 02452 public: 02458 ListIterator(ObjList& list); 02459 02465 ListIterator(HashList& list); 02466 02470 ~ListIterator(); 02471 02476 inline unsigned int length() const 02477 { return m_length; } 02478 02485 GenObject* get(unsigned int index) const; 02486 02499 GenObject* get(); 02500 02505 inline bool eof() const 02506 { return m_current >= m_length; } 02507 02511 inline void reset() 02512 { m_current = 0; } 02513 02514 private: 02515 ObjList* m_objList; 02516 HashList* m_hashList; 02517 GenObject** m_objects; 02518 unsigned int m_length; 02519 unsigned int m_current; 02520 }; 02521 02526 class YATE_API Time 02527 { 02528 public: 02532 inline Time() 02533 : m_time(now()) 02534 { } 02535 02540 inline Time(u_int64_t usec) 02541 : m_time(usec) 02542 { } 02543 02548 inline explicit Time(const struct timeval* tv) 02549 : m_time(fromTimeval(tv)) 02550 { } 02551 02556 inline explicit Time(const struct timeval& tv) 02557 : m_time(fromTimeval(tv)) 02558 { } 02559 02564 inline ~Time() 02565 { } 02566 02571 inline u_int32_t sec() const 02572 { return (u_int32_t)((m_time+500000) / 1000000); } 02573 02578 inline u_int64_t msec() const 02579 { return (m_time+500) / 1000; } 02580 02585 inline u_int64_t usec() const 02586 { return m_time; } 02587 02591 inline operator u_int64_t() const 02592 { return m_time; } 02593 02597 inline Time& operator=(u_int64_t usec) 02598 { m_time = usec; return *this; } 02599 02603 inline Time& operator+=(int64_t delta) 02604 { m_time += delta; return *this; } 02605 02609 inline Time& operator-=(int64_t delta) 02610 { m_time -= delta; return *this; } 02611 02616 inline void toTimeval(struct timeval* tv) const 02617 { toTimeval(tv, m_time); } 02618 02624 static void toTimeval(struct timeval* tv, u_int64_t usec); 02625 02631 static u_int64_t fromTimeval(const struct timeval* tv); 02632 02638 inline static u_int64_t fromTimeval(const struct timeval& tv) 02639 { return fromTimeval(&tv); } 02640 02645 static u_int64_t now(); 02646 02651 static u_int64_t msecNow(); 02652 02657 static u_int32_t secNow(); 02658 02672 static unsigned int toEpoch(int year, unsigned int month, unsigned int day, 02673 unsigned int hour, unsigned int minute, unsigned int sec, int offset = 0); 02674 02686 static bool toDateTime(unsigned int epochTimeSec, int& year, unsigned int& month, 02687 unsigned int& day, unsigned int& hour, unsigned int& minute, unsigned int& sec); 02688 02694 static inline bool isLeap(unsigned int year) 02695 { return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)); } 02696 02697 private: 02698 u_int64_t m_time; 02699 }; 02700 02705 class YATE_API DataBlock : public GenObject 02706 { 02707 public: 02708 02712 DataBlock(); 02713 02717 DataBlock(const DataBlock& value); 02718 02725 DataBlock(void* value, unsigned int len, bool copyData = true); 02726 02730 virtual ~DataBlock(); 02731 02737 virtual void* getObject(const String& name) const; 02738 02742 static const DataBlock& empty(); 02743 02748 inline void* data() const 02749 { return m_data; } 02750 02757 inline unsigned char* data(unsigned int offs, unsigned int len = 1) const 02758 { return (offs + len <= m_length) ? (static_cast<unsigned char*>(m_data) + offs) : 0; } 02759 02766 inline int at(unsigned int offs, int defvalue = -1) const 02767 { return (offs < m_length) ? static_cast<unsigned char*>(m_data)[offs] : defvalue; } 02768 02773 inline bool null() const 02774 { return !m_data; } 02775 02780 inline unsigned int length() const 02781 { return m_length; } 02782 02787 void clear(bool deleteData = true); 02788 02795 DataBlock& assign(void* value, unsigned int len, bool copyData = true); 02796 02802 inline void append(void* value, unsigned int len) { 02803 DataBlock tmp(value,len,false); 02804 append(tmp); 02805 tmp.clear(false); 02806 } 02807 02812 void append(const DataBlock& value); 02813 02818 void append(const String& value); 02819 02824 void insert(const DataBlock& value); 02825 02830 void truncate(unsigned int len); 02831 02836 void cut(int len); 02837 02843 inline int operator[](signed int index) const 02844 { return at(index); } 02845 02851 inline int operator[](unsigned int index) const 02852 { return at(index); } 02853 02857 DataBlock& operator=(const DataBlock& value); 02858 02862 inline DataBlock& operator+=(const DataBlock& value) 02863 { append(value); return *this; } 02864 02868 inline DataBlock& operator+=(const String& value) 02869 { append(value); return *this; } 02870 02879 bool convert(const DataBlock& src, const String& sFormat, 02880 const String& dFormat, unsigned maxlen = 0); 02881 02892 bool unHexify(const char* data, unsigned int len, char sep = 0); 02893 02899 String sqlEscape(char extraEsc) const; 02900 02901 private: 02902 void* m_data; 02903 unsigned int m_length; 02904 }; 02905 02910 class YATE_API MD5 02911 { 02912 public: 02916 MD5(); 02917 02922 MD5(const MD5& original); 02923 02929 MD5(const void* buf, unsigned int len); 02930 02935 MD5(const DataBlock& data); 02936 02941 MD5(const String& str); 02942 02946 ~MD5(); 02947 02951 MD5& operator=(const MD5& original); 02952 02956 void clear(); 02957 02962 void finalize(); 02963 02970 bool update(const void* buf, unsigned int len); 02971 02977 inline bool update(const DataBlock& data) 02978 { return update(data.data(), data.length()); } 02979 02985 inline bool update(const String& str) 02986 { return update(str.c_str(), str.length()); } 02987 02991 inline MD5& operator<<(const String& value) 02992 { update(value); return *this; } 02993 02997 inline MD5& operator<<(const DataBlock& data) 02998 { update(data); return *this; } 02999 03003 MD5& operator<<(const char* value); 03004 03010 const unsigned char* rawDigest(); 03011 03016 inline static unsigned int rawLength() 03017 { return 16; } 03018 03024 const String& hexDigest(); 03025 03026 private: 03027 void init(); 03028 void* m_private; 03029 String m_hex; 03030 unsigned char m_bin[16]; 03031 }; 03032 03037 class YATE_API SHA1 03038 { 03039 public: 03043 SHA1(); 03044 03049 SHA1(const SHA1& original); 03050 03056 SHA1(const void* buf, unsigned int len); 03057 03062 SHA1(const DataBlock& data); 03063 03068 SHA1(const String& str); 03069 03073 ~SHA1(); 03074 03078 SHA1& operator=(const SHA1& original); 03079 03083 void clear(); 03084 03089 void finalize(); 03090 03097 bool update(const void* buf, unsigned int len); 03098 03104 inline bool update(const DataBlock& data) 03105 { return update(data.data(), data.length()); } 03106 03112 inline bool update(const String& str) 03113 { return update(str.c_str(), str.length()); } 03114 03118 inline SHA1& operator<<(const String& value) 03119 { update(value); return *this; } 03120 03124 inline SHA1& operator<<(const DataBlock& data) 03125 { update(data); return *this; } 03126 03130 SHA1& operator<<(const char* value); 03131 03137 const unsigned char* rawDigest(); 03138 03143 inline static unsigned int rawLength() 03144 { return 20; } 03145 03151 const String& hexDigest(); 03152 03153 private: 03154 void init(); 03155 void* m_private; 03156 String m_hex; 03157 unsigned char m_bin[20]; 03158 }; 03159 03164 class YATE_API Base64 : public DataBlock 03165 { 03166 public: 03170 inline Base64() 03171 {} 03172 03179 inline Base64(void* src, unsigned int len, bool copyData = true) 03180 : DataBlock(src,len,copyData) 03181 {} 03182 03192 void encode(String& dest, unsigned int lineLen = 0, bool lineAtEnd = false); 03193 03205 bool decode(DataBlock& dest, bool liberal = true); 03206 03210 inline Base64& operator<<(const String& value) 03211 { append(value); return *this; } 03212 03216 inline Base64& operator<<(const DataBlock& data) 03217 { append(data); return *this; } 03218 03222 inline Base64& operator<<(const char* value) 03223 { return operator<<(String(value)); } 03224 }; 03225 03230 class YATE_API NamedList : public String 03231 { 03232 public: 03237 NamedList(const char* name); 03238 03243 NamedList(const NamedList& original); 03244 03251 NamedList(const char* name, const NamedList& original, const String& prefix); 03252 03258 virtual void* getObject(const String& name) const; 03259 03264 inline unsigned int length() const 03265 { return m_params.length(); } 03266 03271 inline unsigned int count() const 03272 { return m_params.count(); } 03273 03277 inline void clearParams() 03278 { m_params.clear(); } 03279 03284 NamedList& addParam(NamedString* param); 03285 03291 NamedList& addParam(const char* name, const char* value); 03292 03297 NamedList& setParam(NamedString* param); 03298 03304 NamedList& setParam(const char* name, const char* value); 03305 03311 NamedList& clearParam(const String& name, char childSep = 0); 03312 03317 NamedList& clearParam(NamedString* param); 03318 03325 NamedList& copyParam(const NamedList& original, const String& name, char childSep = 0); 03326 03331 NamedList& copyParams(const NamedList& original); 03332 03339 NamedList& copyParams(const NamedList& original, ObjList* list, char childSep = 0); 03340 03347 NamedList& copyParams(const NamedList& original, const String& list, char childSep = 0); 03348 03354 NamedList& copySubParams(const NamedList& original, const String& prefix); 03355 03361 int getIndex(const NamedString* param) const; 03362 03368 int getIndex(const String& name) const; 03369 03375 NamedString* getParam(const String& name) const; 03376 03382 NamedString* getParam(unsigned int index) const; 03383 03389 const String& operator[](const String& name) const; 03390 03397 const char* getValue(const String& name, const char* defvalue = 0) const; 03398 03405 int getIntValue(const String& name, int defvalue = 0) const; 03406 03414 int getIntValue(const String& name, const TokenDict* tokens, int defvalue = 0) const; 03415 03422 double getDoubleValue(const String& name, double defvalue = 0.0) const; 03423 03430 bool getBoolValue(const String& name, bool defvalue = false) const; 03431 03439 int replaceParams(String& str, bool sqlEsc = false, char extraEsc = 0) const; 03440 03449 void dump(String& str, const char* separator, char quote = 0, bool force = false) const; 03450 03455 static const NamedList& empty(); 03456 03457 private: 03458 NamedList(); // no default constructor please 03459 NamedList& operator=(const NamedList& value); // no assignment please 03460 ObjList m_params; 03461 }; 03462 03468 class YATE_API URI : public String 03469 { 03470 public: 03474 URI(); 03475 03480 URI(const URI& uri); 03481 03486 URI(const String& uri); 03487 03492 URI(const char* uri); 03493 03502 URI(const char* proto, const char* user, const char* host, int port = 0, const char* desc = 0); 03503 03507 void parse() const; 03508 03513 inline URI& operator=(const URI& value) 03514 { String::operator=(value); return *this; } 03515 03520 inline URI& operator=(const String& value) 03521 { String::operator=(value); return *this; } 03522 03527 inline URI& operator=(const char* value) 03528 { String::operator=(value); return *this; } 03529 03534 inline const String& getDescription() const 03535 { parse(); return m_desc; } 03536 03541 inline const String& getProtocol() const 03542 { parse(); return m_proto; } 03543 03548 inline const String& getUser() const 03549 { parse(); return m_user; } 03550 03555 inline const String& getHost() const 03556 { parse(); return m_host; } 03557 03562 inline int getPort() const 03563 { parse(); return m_port; } 03564 03569 inline const String& getExtra() const 03570 { parse(); return m_extra; } 03571 03572 protected: 03578 virtual void changed(); 03579 mutable bool m_parsed; 03580 mutable String m_desc; 03581 mutable String m_proto; 03582 mutable String m_user; 03583 mutable String m_host; 03584 mutable String m_extra; 03585 mutable int m_port; 03586 }; 03587 03588 class MutexPrivate; 03589 class SemaphorePrivate; 03590 class ThreadPrivate; 03591 03596 class YATE_API Lockable 03597 { 03598 public: 03602 virtual ~Lockable(); 03603 03609 virtual bool lock(long maxwait = -1) = 0; 03610 03615 virtual bool unlock() = 0; 03616 03622 virtual bool locked() const = 0; 03623 03629 virtual bool check(long maxwait = -1); 03630 03637 virtual bool unlockAll(); 03638 03644 static void wait(unsigned long maxwait); 03645 03650 static unsigned long wait(); 03651 03658 static void startUsingNow(); 03659 }; 03660 03665 class YATE_API Mutex : public Lockable 03666 { 03667 friend class MutexPrivate; 03668 public: 03675 Mutex(bool recursive = false, const char* name = 0); 03676 03681 Mutex(const Mutex& original); 03682 03686 ~Mutex(); 03687 03692 Mutex& operator=(const Mutex& original); 03693 03699 virtual bool lock(long maxwait = -1); 03700 03705 virtual bool unlock(); 03706 03712 virtual bool locked() const; 03713 03718 const char* owner() const; 03719 03724 bool recursive() const; 03725 03730 static int count(); 03731 03736 static int locks(); 03737 03742 static bool efficientTimedLock(); 03743 03744 private: 03745 MutexPrivate* privDataCopy() const; 03746 MutexPrivate* m_private; 03747 }; 03748 03753 class YATE_API Semaphore : public Lockable 03754 { 03755 friend class SemaphorePrivate; 03756 public: 03762 Semaphore(unsigned int maxcount = 1, const char* name = 0); 03763 03768 Semaphore(const Semaphore& original); 03769 03773 ~Semaphore(); 03774 03779 Semaphore& operator=(const Semaphore& original); 03780 03786 virtual bool lock(long maxwait = -1); 03787 03792 virtual bool unlock(); 03793 03799 virtual bool locked() const; 03800 03805 static int count(); 03806 03811 static int locks(); 03812 03817 static bool efficientTimedLock(); 03818 03819 private: 03820 SemaphorePrivate* privDataCopy() const; 03821 SemaphorePrivate* m_private; 03822 }; 03823 03829 class YATE_API Lock 03830 { 03831 public: 03837 inline Lock(Lockable& lck, long maxwait = -1) 03838 { m_lock = lck.lock(maxwait) ? &lck : 0; } 03839 03845 inline Lock(Lockable* lck, long maxwait = -1) 03846 { m_lock = (lck && lck->lock(maxwait)) ? lck : 0; } 03847 03851 inline ~Lock() 03852 { if (m_lock) m_lock->unlock(); } 03853 03858 inline Lockable* locked() const 03859 { return m_lock; } 03860 03864 inline void drop() 03865 { if (m_lock) m_lock->unlock(); m_lock = 0; } 03866 03867 private: 03868 Lockable* m_lock; 03869 03871 inline void* operator new(size_t); 03872 03874 inline void* operator new[](size_t); 03875 03877 inline Lock(const Lock&); 03878 }; 03879 03886 class YATE_API Lock2 03887 { 03888 public: 03895 inline Lock2(Mutex* mx1, Mutex* mx2, long maxwait = -1) 03896 : m_mx1(0), m_mx2(0) 03897 { lock(mx1,mx2,maxwait); } 03898 03905 inline Lock2(Mutex& mx1, Mutex& mx2, long maxwait = -1) 03906 : m_mx1(0), m_mx2(0) 03907 { lock(&mx1,&mx2,maxwait); } 03908 03912 inline ~Lock2() 03913 { drop(); } 03914 03919 inline bool locked() const 03920 { return m_mx1 != 0; } 03921 03929 bool lock(Mutex* mx1, Mutex* mx2, long maxwait = -1); 03930 03938 inline bool lock(Mutex& mx1, Mutex& mx2, long maxwait = -1) 03939 { return lock(&mx1,&mx2,maxwait); } 03940 03944 void drop(); 03945 03946 private: 03947 Mutex* m_mx1; 03948 Mutex* m_mx2; 03949 03951 inline void* operator new(size_t); 03952 03954 inline void* operator new[](size_t); 03955 03957 inline Lock2(const Lock2&); 03958 }; 03959 03965 class YATE_API Runnable 03966 { 03967 public: 03972 virtual void run() = 0; 03973 03977 virtual ~Runnable(); 03978 }; 03979 03986 class YATE_API Thread : public Runnable 03987 { 03988 friend class ThreadPrivate; 03989 friend class MutexPrivate; 03990 friend class SemaphorePrivate; 03991 public: 03995 enum Priority { 03996 Lowest, 03997 Low, 03998 Normal, 03999 High, 04000 Highest 04001 }; 04002 04006 virtual void cleanup(); 04007 04012 bool startup(); 04013 04018 bool error() const; 04019 04024 bool running() const; 04025 04030 inline int locks() const 04031 { return m_locks; } 04032 04037 inline bool locked() const 04038 { return m_locking || m_locks; } 04039 04044 const char* name() const; 04045 04050 static const char* currentName(); 04051 04057 static void yield(bool exitCheck = false); 04058 04064 static void idle(bool exitCheck = false); 04065 04071 static void sleep(unsigned int sec, bool exitCheck = false); 04072 04078 static void msleep(unsigned long msec, bool exitCheck = false); 04079 04086 static void usleep(unsigned long usec, bool exitCheck = false); 04087 04092 static unsigned long idleUsec(); 04093 04098 static unsigned long idleMsec(); 04099 04105 static Thread* current(); 04106 04111 static int count(); 04112 04118 static bool check(bool exitNow = true); 04119 04123 static void exit(); 04124 04129 void cancel(bool hard = false); 04130 04135 inline bool isCurrent() const 04136 { return current() == this; } 04137 04138 04145 static Priority priority(const char* name, Priority defvalue = Normal); 04146 04152 static const char* priority(Priority prio); 04153 04158 static void killall(); 04159 04164 static void preExec(); 04165 04171 static int lastError(); 04172 04179 static inline bool errorString(String& buffer) 04180 { return errorString(buffer,lastError()); } 04181 04192 static bool errorString(String& buffer, int code); 04193 04194 protected: 04200 Thread(const char *name = 0, Priority prio = Normal); 04201 04207 Thread(const char *name, const char* prio); 04208 04212 virtual ~Thread(); 04213 04214 private: 04215 ThreadPrivate* m_private; 04216 int m_locks; 04217 bool m_locking; 04218 }; 04219 04220 class Socket; 04221 04226 class YATE_API SocketAddr : public GenObject 04227 { 04228 public: 04232 inline SocketAddr() 04233 : m_address(0), m_length(0) 04234 { } 04235 04240 inline SocketAddr(const SocketAddr& value) 04241 : GenObject(), 04242 m_address(0), m_length(0) 04243 { assign(value.address(),value.length()); } 04244 04249 SocketAddr(int family); 04250 04256 SocketAddr(const struct sockaddr* addr, socklen_t len = 0); 04257 04261 virtual ~SocketAddr(); 04262 04267 inline SocketAddr& operator=(const SocketAddr& value) 04268 { assign(value.address(),value.length()); return *this; } 04269 04275 bool operator==(const SocketAddr& other) const; 04276 04282 inline bool operator!=(const SocketAddr& other) const 04283 { return !operator==(other); } 04284 04288 void clear(); 04289 04295 bool assign(int family); 04296 04302 void assign(const struct sockaddr* addr, socklen_t len = 0); 04303 04309 bool local(const SocketAddr& remote); 04310 04315 inline bool valid() const 04316 { return m_length && m_address; } 04317 04322 inline bool null() const 04323 { return !(m_length && m_address); } 04324 04329 inline int family() const 04330 { return m_address ? m_address->sa_family : 0; } 04331 04336 inline const String& host() const 04337 { return m_host; } 04338 04343 virtual bool host(const String& name); 04344 04349 int port() const; 04350 04356 bool port(int newport); 04357 04362 inline struct sockaddr* address() const 04363 { return m_address; } 04364 04369 inline socklen_t length() const 04370 { return m_length; } 04371 04377 static bool supports(int family); 04378 04379 protected: 04383 virtual void stringify(); 04384 04385 struct sockaddr* m_address; 04386 socklen_t m_length; 04387 String m_host; 04388 }; 04389 04394 class YATE_API SocketFilter : public GenObject 04395 { 04396 friend class Socket; 04397 public: 04401 SocketFilter(); 04402 04406 virtual ~SocketFilter(); 04407 04413 virtual void* getObject(const String& name) const; 04414 04419 virtual void timerTick(const Time& when); 04420 04430 virtual bool received(void* buffer, int length, int flags, const struct sockaddr* addr, socklen_t adrlen) = 0; 04431 04436 inline Socket* socket() const 04437 { return m_socket; } 04438 04443 bool valid() const; 04444 04445 private: 04446 Socket* m_socket; 04447 }; 04448 04453 class YATE_API Stream 04454 { 04455 public: 04459 enum SeekPos { 04460 SeekBegin, // Seek from start of stream 04461 SeekEnd, // Seek from stream end 04462 SeekCurrent // Seek from current position 04463 }; 04464 04468 virtual ~Stream(); 04469 04474 inline int error() const 04475 { return m_error; } 04476 04481 virtual bool terminate() = 0; 04482 04487 virtual bool canRetry() const; 04488 04493 virtual bool valid() const = 0; 04494 04500 virtual bool setBlocking(bool block = true); 04501 04508 virtual int writeData(const void* buffer, int length) = 0; 04509 04515 int writeData(const char* str); 04516 04522 inline int writeData(const String& str) 04523 { return writeData(str.c_str(), str.length()); } 04524 04530 inline int writeData(const DataBlock& buf) 04531 { return writeData(buf.data(), buf.length()); } 04532 04539 virtual int readData(void* buffer, int length) = 0; 04540 04545 virtual int64_t length(); 04546 04553 virtual int64_t seek(SeekPos pos, int64_t offset = 0); 04554 04560 inline int64_t seek(int64_t offset) 04561 { return seek(SeekBegin,offset); } 04562 04569 static bool allocPipe(Stream*& reader, Stream*& writer); 04570 04577 static bool allocPair(Stream*& str1, Stream*& str2); 04578 04583 static bool supportsPipes(); 04584 04589 static bool supportsPairs(); 04590 04591 protected: 04595 inline Stream() 04596 : m_error(0) 04597 { } 04598 04602 inline void clearError() 04603 { m_error = 0; } 04604 04605 int m_error; 04606 }; 04607 04612 class YATE_API MemoryStream : public Stream 04613 { 04614 public: 04618 inline MemoryStream() 04619 : m_offset(0) 04620 { } 04621 04626 inline MemoryStream(const DataBlock& data) 04627 : m_data(data), m_offset(0) 04628 { } 04629 04634 inline const DataBlock& data() const 04635 { return m_data; } 04636 04641 virtual bool terminate() 04642 { return true; } 04647 virtual bool valid() const 04648 { return true; } 04649 04656 virtual int writeData(const void* buffer, int len); 04657 04664 virtual int readData(void* buffer, int len); 04665 04670 virtual int64_t length() 04671 { return m_data.length(); } 04672 04679 virtual int64_t seek(SeekPos pos, int64_t offset = 0); 04680 04681 protected: 04685 DataBlock m_data; 04686 04690 int64_t m_offset; 04691 }; 04692 04697 class YATE_API File : public Stream 04698 { 04699 public: 04703 File(); 04704 04709 File(HANDLE handle); 04710 04714 virtual ~File(); 04715 04728 virtual bool openPath(const char* name, bool canWrite = false, bool canRead = true, 04729 bool create = false, bool append = false, bool binary = false, 04730 bool pubReadable = false, bool pubWritable = false); 04731 04736 virtual bool terminate(); 04737 04742 void attach(HANDLE handle); 04743 04748 HANDLE detach(); 04749 04754 inline HANDLE handle() const 04755 { return m_handle; } 04756 04761 virtual bool canRetry() const; 04762 04767 virtual bool valid() const; 04768 04773 static HANDLE invalidHandle(); 04774 04780 virtual bool setBlocking(bool block = true); 04781 04786 virtual int64_t length(); 04787 04794 virtual int64_t seek(SeekPos pos, int64_t offset = 0); 04795 04802 virtual int writeData(const void* buffer, int length); 04803 04810 virtual int readData(void* buffer, int length); 04811 04817 bool getFileTime(unsigned int& secEpoch); 04818 04825 virtual bool md5(String& buffer); 04826 04834 static bool setFileTime(const char* name, unsigned int secEpoch, int* error = 0); 04835 04843 static bool getFileTime(const char* name, unsigned int& secEpoch, int* error = 0); 04844 04851 static bool exists(const char* name, int* error = 0); 04852 04860 static bool rename(const char* oldFile, const char* newFile, int* error = 0); 04861 04868 static bool remove(const char* name, int* error = 0); 04869 04877 static bool md5(const char* name, String& buffer, int* error = 0); 04878 04885 static bool mkDir(const char* path, int* error = 0); 04886 04893 static bool rmDir(const char* path, int* error = 0); 04894 04906 static bool listDirectory(const char* path, ObjList* dirs, ObjList* files, 04907 int* error = 0); 04908 04915 static bool createPipe(File& reader, File& writer); 04916 04917 protected: 04918 04922 void copyError(); 04923 04924 HANDLE m_handle; 04925 }; 04926 04931 class YATE_API Socket : public Stream 04932 { 04933 public: 04937 enum TOS { 04938 LowDelay = IPTOS_LOWDELAY, 04939 MaxThroughput = IPTOS_THROUGHPUT, 04940 MaxReliability = IPTOS_RELIABILITY, 04941 MinCost = IPTOS_MINCOST, 04942 }; 04943 04947 Socket(); 04948 04953 Socket(SOCKET handle); 04954 04961 Socket(int domain, int type, int protocol = 0); 04962 04966 virtual ~Socket(); 04967 04975 bool create(int domain, int type, int protocol = 0); 04976 04981 virtual bool terminate(); 04982 04987 void attach(SOCKET handle); 04988 04993 SOCKET detach(); 04994 04999 inline SOCKET handle() const 05000 { return m_handle; } 05001 05006 virtual bool canRetry() const; 05007 05012 virtual bool valid() const; 05013 05018 static SOCKET invalidHandle(); 05019 05024 static int socketError(); 05025 05034 bool setOption(int level, int name, const void* value = 0, socklen_t length = 0); 05035 05044 bool getOption(int level, int name, void* buffer, socklen_t* length); 05045 05051 bool setTOS(int tos); 05052 05058 virtual bool setBlocking(bool block = true); 05059 05067 bool setReuse(bool reuse = true, bool exclusive = false); 05068 05075 bool setLinger(int seconds = -1); 05076 05083 bool bind(struct sockaddr* addr, socklen_t addrlen); 05084 05090 inline bool bind(const SocketAddr& addr) 05091 { return bind(addr.address(), addr.length()); } 05092 05098 bool listen(unsigned int backlog = 0); 05099 05106 Socket* accept(struct sockaddr* addr = 0, socklen_t* addrlen = 0); 05107 05113 Socket* accept(SocketAddr& addr); 05114 05121 SOCKET acceptHandle(struct sockaddr* addr = 0, socklen_t* addrlen = 0); 05122 05128 static bool canSelect(SOCKET handle); 05129 05134 inline bool canSelect() const 05135 { return canSelect(handle()); } 05136 05142 Socket* peelOff(unsigned int assoc); 05143 05149 SOCKET peelOffHandle(unsigned int assoc); 05150 05157 bool connect(struct sockaddr* addr, socklen_t addrlen); 05158 05164 inline bool connect(const SocketAddr& addr) 05165 { return connect(addr.address(), addr.length()); } 05166 05173 bool shutdown(bool stopReads, bool stopWrites); 05174 05181 bool getSockName(struct sockaddr* addr, socklen_t* addrlen); 05182 05188 bool getSockName(SocketAddr& addr); 05189 05196 bool getPeerName(struct sockaddr* addr, socklen_t* addrlen); 05197 05203 bool getPeerName(SocketAddr& addr); 05204 05214 int sendTo(const void* buffer, int length, const struct sockaddr* addr, socklen_t adrlen, int flags = 0); 05215 05224 inline int sendTo(const void* buffer, int length, const SocketAddr& addr, int flags = 0) 05225 { return sendTo(buffer, length, addr.address(), addr.length(), flags); } 05226 05234 int send(const void* buffer, int length, int flags = 0); 05235 05242 virtual int writeData(const void* buffer, int length); 05243 05253 int recvFrom(void* buffer, int length, struct sockaddr* addr = 0, socklen_t* adrlen = 0, int flags = 0); 05254 05263 int recvFrom(void* buffer, int length, SocketAddr& addr, int flags = 0); 05264 05272 int recv(void* buffer, int length, int flags = 0); 05273 05280 virtual int readData(void* buffer, int length); 05281 05290 bool select(bool* readok, bool* writeok, bool* except, struct timeval* timeout = 0); 05291 05300 bool select(bool* readok, bool* writeok, bool* except, int64_t timeout); 05301 05307 bool installFilter(SocketFilter* filter); 05308 05314 void removeFilter(SocketFilter* filter, bool delobj = false); 05315 05319 void clearFilters(); 05320 05327 virtual void timerTick(const Time& when); 05328 05336 static bool createPair(Socket& sock1, Socket& sock2, int domain = AF_UNIX); 05337 05338 protected: 05339 05343 void copyError(); 05344 05351 bool checkError(int retcode, bool strict = false); 05352 05362 bool applyFilters(void* buffer, int length, int flags, const struct sockaddr* addr = 0, socklen_t adrlen = 0); 05363 05364 SOCKET m_handle; 05365 ObjList m_filters; 05366 }; 05367 05372 class YATE_API Cipher : public GenObject 05373 { 05374 public: 05378 enum Direction { 05379 Bidir, 05380 Encrypt, 05381 Decrypt, 05382 }; 05383 05388 inline static const TokenDict* directions() 05389 { return s_directions; } 05390 05397 inline static Direction direction(const char* name, Direction defdir = Bidir) 05398 { return (Direction)TelEngine::lookup(name,s_directions,defdir); } 05399 05403 virtual ~Cipher(); 05404 05410 virtual void* getObject(const String& name) const; 05411 05417 virtual bool valid(Direction dir = Bidir) const; 05418 05423 virtual unsigned int blockSize() const = 0; 05424 05429 virtual unsigned int initVectorSize() const; 05430 05436 unsigned int bufferSize(unsigned int len) const; 05437 05443 bool bufferFull(unsigned int len) const; 05444 05452 virtual bool setKey(const void* key, unsigned int len, Direction dir = Bidir) = 0; 05453 05460 inline bool setKey(const DataBlock& key, Direction dir = Bidir) 05461 { return setKey(key.data(),key.length(),dir); } 05462 05470 virtual bool initVector(const void* vect, unsigned int len, Direction dir = Bidir); 05471 05478 inline bool initVector(const DataBlock& vect, Direction dir = Bidir) 05479 { return initVector(vect.data(),vect.length(),dir); } 05480 05488 virtual bool encrypt(void* outData, unsigned int len, const void* inpData = 0) = 0; 05489 05495 inline bool encrypt(DataBlock& data) 05496 { return encrypt(data.data(),data.length()); } 05497 05505 virtual bool decrypt(void* outData, unsigned int len, const void* inpData = 0) = 0; 05506 05512 inline bool decrypt(DataBlock& data) 05513 { return decrypt(data.data(),data.length()); } 05514 05515 private: 05516 static const TokenDict s_directions[]; 05517 }; 05518 05524 class YATE_API SysUsage 05525 { 05526 public: 05530 enum Type { 05531 WallTime, 05532 UserTime, 05533 KernelTime 05534 }; 05535 05539 static void init(); 05540 05545 static u_int64_t startTime(); 05546 05552 static u_int64_t usecRunTime(Type type = WallTime); 05553 05559 static u_int64_t msecRunTime(Type type = WallTime); 05560 05566 static u_int32_t secRunTime(Type type = WallTime); 05567 05573 static double runTime(Type type = WallTime); 05574 05575 }; 05576 05577 }; // namespace TelEngine 05578 05579 #endif /* __YATECLASS_H */ 05580 05581 /* vi: set ts=8 sw=4 sts=4 noet: */