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 <limits.h> 00033 #include <sys/types.h> 00034 #include <stddef.h> 00035 #include <unistd.h> 00036 #include <errno.h> 00037 00038 #ifndef _WORDSIZE 00039 #if defined(__arch64__) || defined(__x86_64__) \ 00040 || defined(__amd64__) || defined(__ia64__) \ 00041 || defined(__alpha__) || defined(__sparcv9) 00042 #define _WORDSIZE 64 00043 #else 00044 #define _WORDSIZE 32 00045 #endif 00046 #endif 00047 00048 #ifndef _WINDOWS 00049 #if defined(WIN32) || defined(_WIN32) 00050 #define _WINDOWS 00051 #endif 00052 #endif 00053 00054 #ifdef _WINDOWS 00055 00056 #include <windows.h> 00057 #include <io.h> 00058 #include <direct.h> 00059 00063 typedef signed __int8 int8_t; 00064 typedef unsigned __int8 u_int8_t; 00065 typedef unsigned __int8 uint8_t; 00066 typedef signed __int16 int16_t; 00067 typedef unsigned __int16 u_int16_t; 00068 typedef unsigned __int16 uint16_t; 00069 typedef signed __int32 int32_t; 00070 typedef unsigned __int32 u_int32_t; 00071 typedef unsigned __int32 uint32_t; 00072 typedef signed __int64 int64_t; 00073 typedef unsigned __int64 u_int64_t; 00074 typedef unsigned __int64 uint64_t; 00075 00076 typedef int pid_t; 00077 typedef int socklen_t; 00078 typedef unsigned long in_addr_t; 00079 00080 #ifndef strcasecmp 00081 #define strcasecmp _stricmp 00082 #endif 00083 00084 #ifndef strncasecmp 00085 #define strncasecmp _strnicmp 00086 #endif 00087 00088 #define vsnprintf _vsnprintf 00089 #define snprintf _snprintf 00090 #define strdup _strdup 00091 #define open _open 00092 #define dup2 _dup2 00093 #define read _read 00094 #define write _write 00095 #define close _close 00096 #define getpid _getpid 00097 #define chdir _chdir 00098 #define mkdir(p,m) _mkdir(p) 00099 #define unlink _unlink 00100 00101 #define O_RDWR _O_RDWR 00102 #define O_RDONLY _O_RDONLY 00103 #define O_WRONLY _O_WRONLY 00104 #define O_APPEND _O_APPEND 00105 #define O_BINARY _O_BINARY 00106 #define O_EXCL _O_EXCL 00107 #define O_CREAT _O_CREAT 00108 #define O_TRUNC _O_TRUNC 00109 #define O_NOCTTY 0 00110 00111 #define S_IRUSR _S_IREAD 00112 #define S_IWUSR _S_IWRITE 00113 #define S_IXUSR 0 00114 #define S_IRWXU (_S_IREAD|_S_IWRITE) 00115 00116 #ifdef LIBYATE_EXPORTS 00117 #define YATE_API __declspec(dllexport) 00118 #else 00119 #ifndef LIBYATE_STATIC 00120 #define YATE_API __declspec(dllimport) 00121 #endif 00122 #endif 00123 00124 #define FMT64 "%I64d" 00125 #define FMT64U "%I64u" 00126 00127 #else /* _WINDOWS */ 00128 00129 #include <sys/time.h> 00130 #include <sys/socket.h> 00131 00132 #if defined(__FreeBSD__) 00133 #include <netinet/in_systm.h> 00134 #endif 00135 00136 #include <netinet/in.h> 00137 #include <netinet/ip.h> 00138 #include <netinet/tcp.h> 00139 #include <arpa/inet.h> 00140 #include <netdb.h> 00141 00145 #ifndef SOCKET 00146 typedef int SOCKET; 00147 #endif 00148 #ifndef HANDLE 00149 typedef int HANDLE; 00150 #endif 00151 00152 #ifndef O_BINARY 00153 #define O_BINARY 0 00154 #endif 00155 00156 #if _WORDSIZE == 64 && !defined(__APPLE__) 00157 #define FMT64 "%ld" 00158 #define FMT64U "%lu" 00159 #else 00160 #define FMT64 "%lld" 00161 #define FMT64U "%llu" 00162 #endif 00163 00164 #endif /* ! _WINDOWS */ 00165 00166 #ifndef O_LARGEFILE 00167 #define O_LARGEFILE 0 00168 #endif 00169 00170 #ifndef IPTOS_LOWDELAY 00171 #define IPTOS_LOWDELAY 0x10 00172 #define IPTOS_THROUGHPUT 0x08 00173 #define IPTOS_RELIABILITY 0x04 00174 #endif 00175 #ifndef IPTOS_MINCOST 00176 #define IPTOS_MINCOST 0x02 00177 #endif 00178 #ifndef IPPROTO_SCTP 00179 #define IPPROTO_SCTP 132 00180 #endif 00181 00182 #ifndef YATE_API 00183 #define YATE_API 00184 #endif 00185 00186 #ifdef _WINDOWS 00187 #undef RAND_MAX 00188 #define RAND_MAX 2147483647 00189 #endif 00190 00194 namespace TelEngine { 00195 00196 #ifdef HAVE_GCC_FORMAT_CHECK 00197 #define FORMAT_CHECK(f) __attribute__((format(printf,(f),(f)+1))) 00198 #else 00199 #define FORMAT_CHECK(f) 00200 #endif 00201 00202 #ifdef HAVE_BLOCK_RETURN 00203 #define YSTRING(s) (*({static const String str(s);&str;})) 00204 #else 00205 #define YSTRING(s) (s) 00206 #endif 00207 00212 YATE_API void abortOnBug(); 00213 00218 YATE_API bool abortOnBug(bool doAbort); 00219 00225 enum DebugLevel { 00226 DebugFail = 0, 00227 DebugTest = 1, 00228 DebugGoOn = 2, 00229 DebugConf = 3, 00230 DebugStub = 4, 00231 DebugWarn = 5, 00232 DebugMild = 6, 00233 DebugCall = 7, 00234 DebugNote = 8, 00235 DebugInfo = 9, 00236 DebugAll = 10 00237 }; 00238 00243 YATE_API int debugLevel(); 00244 00250 YATE_API int debugLevel(int level); 00251 00257 YATE_API bool debugAt(int level); 00258 00265 YATE_API const char* debugColor(int level); 00266 00272 class YATE_API DebugEnabler 00273 { 00274 public: 00280 inline DebugEnabler(int level = TelEngine::debugLevel(), bool enabled = true) 00281 : m_level(DebugFail), m_enabled(enabled), m_chain(0), m_name(0) 00282 { debugLevel(level); } 00283 00284 inline ~DebugEnabler() 00285 { m_name = 0; m_chain = 0; } 00286 00291 inline int debugLevel() const 00292 { return m_chain ? m_chain->debugLevel() : m_level; } 00293 00299 int debugLevel(int level); 00300 00305 inline bool debugEnabled() const 00306 { return m_chain ? m_chain->debugEnabled() : m_enabled; } 00307 00312 inline void debugEnabled(bool enable) 00313 { m_enabled = enable; m_chain = 0; } 00314 00319 inline const char* debugName() const 00320 { return m_name; } 00321 00327 bool debugAt(int level) const; 00328 00333 inline bool debugChained() const 00334 { return m_chain != 0; } 00335 00340 inline void debugChain(const DebugEnabler* chain = 0) 00341 { m_chain = (chain != this) ? chain : 0; } 00342 00347 void debugCopy(const DebugEnabler* original = 0); 00348 00349 protected: 00354 inline void debugName(const char* name) 00355 { m_name = name; } 00356 00357 private: 00358 int m_level; 00359 bool m_enabled; 00360 const DebugEnabler* m_chain; 00361 const char* m_name; 00362 }; 00363 00364 #if 0 /* for documentation generator */ 00365 00370 void DDebug(int level, const char* format, ...); 00371 00377 void DDebug(const char* facility, int level, const char* format, ...); 00378 00384 void DDebug(const DebugEnabler* local, int level, const char* format, ...); 00385 00391 void XDebug(int level, const char* format, ...); 00392 00398 void XDebug(const char* facility, int level, const char* format, ...); 00399 00405 void XDebug(const DebugEnabler* local, int level, const char* format, ...); 00406 00412 void NDebug(int level, const char* format, ...); 00413 00419 void NDebug(const char* facility, int level, const char* format, ...); 00420 00426 void NDebug(const DebugEnabler* local, int level, const char* format, ...); 00427 #endif 00428 00429 #if defined(_DEBUG) || defined(DEBUG) || defined(XDEBUG) 00430 #undef DEBUG 00431 #define DEBUG 1 00432 #endif 00433 00434 #ifdef DEBUG 00435 #define DDebug Debug 00436 #else 00437 #ifdef _WINDOWS 00438 #define DDebug 00439 #else 00440 #define DDebug(arg...) 00441 #endif 00442 #endif 00443 00444 #ifdef XDEBUG 00445 #define XDebug Debug 00446 #else 00447 #ifdef _WINDOWS 00448 #define XDebug 00449 #else 00450 #define XDebug(arg...) 00451 #endif 00452 #endif 00453 00454 #ifndef NDEBUG 00455 #define NDebug Debug 00456 #else 00457 #ifdef _WINDOWS 00458 #define NDebug 00459 #else 00460 #define NDebug(arg...) 00461 #endif 00462 #endif 00463 00469 YATE_API void Debug(int level, const char* format, ...) FORMAT_CHECK(2); 00470 00477 YATE_API void Debug(const char* facility, int level, const char* format, ...) FORMAT_CHECK(3); 00478 00485 YATE_API void Debug(const DebugEnabler* local, int level, const char* format, ...) FORMAT_CHECK(3); 00486 00491 YATE_API void Output(const char* format, ...) FORMAT_CHECK(1); 00492 00499 class YATE_API Debugger 00500 { 00501 public: 00505 enum Formatting { 00506 None = 0, 00507 Relative, // from program start 00508 Absolute, // from EPOCH (1-1-1970) 00509 Textual, // absolute GMT in YYYYMMDDhhmmss.uuuuuu format 00510 TextLocal, // local time in YYYYMMDDhhmmss.uuuuuu format 00511 }; 00512 00518 explicit Debugger(const char* name, const char* format = 0, ...); 00519 00526 Debugger(int level, const char* name, const char* format = 0, ...); 00527 00531 ~Debugger(); 00532 00537 static void setOutput(void (*outFunc)(const char*,int) = 0); 00538 00543 static void setIntOut(void (*outFunc)(const char*,int) = 0); 00544 00550 static void enableOutput(bool enable = true, bool colorize = false); 00551 00556 static Formatting getFormatting(); 00557 00562 static void setFormatting(Formatting format); 00563 00570 static unsigned int formatTime(char* buf, Formatting format = getFormatting()); 00571 00572 private: 00573 const char* m_name; 00574 }; 00575 00580 struct TokenDict { 00584 const char* token; 00585 00589 int value; 00590 }; 00591 00592 class String; 00593 class Mutex; 00594 00595 #if 0 /* for documentation generator */ 00596 00601 constant YSTRING(const char* string); 00602 00608 void YCLASS(class type,class base); 00609 00616 void YCLASS2(class type,class base1,class base2); 00617 00625 void YCLASS3(class type,class base1,class base2,class base3); 00626 00632 void YCLASSIMP(class type,class base); 00633 00640 void YCLASSIMP2(class type,class base1,class base2); 00641 00649 void YCLASSIMP3(class type,class base1,class base2,class base3); 00650 00657 class* YOBJECT(class type,GenObject* pntr); 00658 00663 void YNOCOPY(class type); 00664 #endif 00665 00666 #define YCLASS(type,base) \ 00667 public: virtual void* getObject(const String& name) const \ 00668 { return (name == YSTRING(#type)) ? const_cast<type*>(this) : base::getObject(name); } 00669 00670 #define YCLASS2(type,base1,base2) \ 00671 public: virtual void* getObject(const String& name) const \ 00672 { if (name == YSTRING(#type)) return const_cast<type*>(this); \ 00673 void* tmp = base1::getObject(name); \ 00674 return tmp ? tmp : base2::getObject(name); } 00675 00676 #define YCLASS3(type,base1,base2,base3) \ 00677 public: virtual void* getObject(const String& name) const \ 00678 { if (name == YSTRING(#type)) return const_cast<type*>(this); \ 00679 void* tmp = base1::getObject(name); \ 00680 if (tmp) return tmp; \ 00681 tmp = base2::getObject(name); \ 00682 return tmp ? tmp : base3::getObject(name); } 00683 00684 #define YCLASSIMP(type,base) \ 00685 void* type::getObject(const String& name) const \ 00686 { return (name == YSTRING(#type)) ? const_cast<type*>(this) : base::getObject(name); } 00687 00688 #define YCLASSIMP2(type,base1,base2) \ 00689 void* type::getObject(const String& name) const \ 00690 { if (name == YSTRING(#type)) return const_cast<type*>(this); \ 00691 void* tmp = base1::getObject(name); \ 00692 return tmp ? tmp : base2::getObject(name); } 00693 00694 #define YCLASSIMP3(type,base1,base2,base3) \ 00695 void* type::getObject(const String& name) const \ 00696 { if (name == YSTRING(#type)) return const_cast<type*>(this); \ 00697 void* tmp = base1::getObject(name); \ 00698 if (tmp) return tmp; \ 00699 tmp = base2::getObject(name); \ 00700 return tmp ? tmp : base3::getObject(name); } 00701 00702 #define YOBJECT(type,pntr) (static_cast<type*>(GenObject::getObject(YSTRING(#type),pntr))) 00703 00704 #define YNOCOPY(type) private: \ 00705 type(const type&); \ 00706 void operator=(const type&) 00707 00708 00712 class YATE_API GenObject 00713 { 00714 public: 00718 virtual ~GenObject() { } 00719 00726 virtual bool alive() const; 00727 00731 virtual void destruct(); 00732 00739 virtual const String& toString() const; 00740 00746 virtual void* getObject(const String& name) const; 00747 00754 static inline void* getObject(const String& name, const GenObject* obj) 00755 { return obj ? obj->getObject(name) : 0; } 00756 }; 00757 00763 inline void destruct(GenObject* obj) 00764 { if (obj) obj->destruct(); } 00765 00772 template <class Obj> void destruct(Obj*& obj) 00773 { if (obj) { obj->destruct(); obj = 0; } } 00774 00779 class YATE_API RefObject : public GenObject 00780 { 00781 YNOCOPY(RefObject); // no automatic copies please 00782 public: 00787 RefObject(); 00788 00792 virtual ~RefObject(); 00793 00799 virtual void* getObject(const String& name) const; 00800 00807 virtual bool alive() const; 00808 00813 bool ref(); 00814 00823 bool deref(); 00824 00829 inline int refcount() const 00830 { return m_refcount; } 00831 00836 virtual void destruct(); 00837 00843 static bool efficientIncDec(); 00844 00845 protected: 00851 virtual void zeroRefs(); 00852 00858 bool resurrect(); 00859 00865 virtual void destroyed(); 00866 00867 private: 00868 int m_refcount; 00869 Mutex* m_mutex; 00870 }; 00871 00877 class YATE_API RefPointerBase 00878 { 00879 protected: 00883 inline RefPointerBase() 00884 : m_pointer(0) { } 00885 00892 void assign(RefObject* oldptr, RefObject* newptr, void* pointer); 00893 00897 void* m_pointer; 00898 }; 00899 00903 template <class Obj = RefObject> class RefPointer : public RefPointerBase 00904 { 00905 protected: 00910 inline Obj* pointer() const 00911 { return static_cast<Obj*>(m_pointer); } 00912 00917 inline void assign(Obj* object = 0) 00918 { RefPointerBase::assign(pointer(),object,object); } 00919 00920 public: 00924 inline RefPointer() 00925 { } 00926 00931 inline RefPointer(const RefPointer<Obj>& value) 00932 : RefPointerBase() 00933 { assign(value); } 00934 00939 inline RefPointer(Obj* object) 00940 { assign(object); } 00941 00945 inline ~RefPointer() 00946 { assign(); } 00947 00951 inline RefPointer<Obj>& operator=(const RefPointer<Obj>& value) 00952 { assign(value.pointer()); return *this; } 00953 00957 inline RefPointer<Obj>& operator=(Obj* object) 00958 { assign(object); return *this; } 00959 00964 inline operator Obj*() const 00965 { return pointer(); } 00966 00970 inline Obj* operator->() const 00971 { return pointer(); } 00972 00976 inline Obj& operator*() const 00977 { return *pointer(); } 00978 }; 00979 00983 template <class Obj = GenObject> class GenPointer : public GenObject 00984 { 00985 private: 00989 Obj* m_pointer; 00990 00991 public: 00995 inline GenPointer() 00996 : m_pointer(0) 00997 { } 00998 01003 inline GenPointer(const GenPointer<Obj>& value) 01004 : m_pointer(value) 01005 { } 01006 01011 inline GenPointer(Obj* object) 01012 : m_pointer(object) 01013 { } 01014 01018 inline GenPointer<Obj>& operator=(const GenPointer<Obj>& value) 01019 { m_pointer = value; return *this; } 01020 01024 inline GenPointer<Obj>& operator=(Obj* object) 01025 { m_pointer = object; return *this; } 01026 01031 inline operator Obj*() const 01032 { return m_pointer; } 01033 01037 inline Obj* operator->() const 01038 { return m_pointer; } 01039 01043 inline Obj& operator*() const 01044 { return *m_pointer; } 01045 }; 01046 01051 class YATE_API ObjList : public GenObject 01052 { 01053 YNOCOPY(ObjList); // no automatic copies please 01054 public: 01058 ObjList(); 01059 01063 virtual ~ObjList(); 01064 01070 virtual void* getObject(const String& name) const; 01071 01076 unsigned int length() const; 01077 01082 unsigned int count() const; 01083 01088 inline GenObject* get() const 01089 { return m_obj; } 01090 01097 GenObject* set(const GenObject* obj, bool delold = true); 01098 01103 inline ObjList* next() const 01104 { return m_next; } 01105 01110 ObjList* last() const; 01111 01116 ObjList* skipNull() const; 01117 01122 ObjList* skipNext() const; 01123 01129 GenObject* at(int index) const; 01130 01136 ObjList* operator+(int index) const; 01137 01143 inline GenObject* operator[](signed int index) const 01144 { return at(index); } 01145 01151 inline GenObject* operator[](unsigned int index) const 01152 { return at(index); } 01153 01159 GenObject* operator[](const String& str) const; 01160 01166 ObjList* find(const GenObject* obj) const; 01167 01173 ObjList* find(const String& str) const; 01174 01180 int index(const GenObject* obj) const; 01181 01187 int index(const String& str) const; 01188 01195 ObjList* insert(const GenObject* obj, bool compact = true); 01196 01203 ObjList* append(const GenObject* obj, bool compact = true); 01204 01210 GenObject* remove(bool delobj = true); 01211 01218 GenObject* remove(GenObject* obj, bool delobj = true); 01219 01226 GenObject* remove(const String& str, bool delobj = true); 01227 01231 void clear(); 01232 01237 inline bool autoDelete() 01238 { return m_delete; } 01239 01244 inline void setDelete(bool autodelete) 01245 { m_delete = autodelete; } 01246 01251 static const ObjList& empty(); 01252 01253 private: 01254 ObjList* m_next; 01255 GenObject* m_obj; 01256 bool m_delete; 01257 }; 01258 01263 class YATE_API ObjVector : public GenObject 01264 { 01265 YNOCOPY(ObjVector); // no automatic copies please 01266 public: 01271 inline explicit ObjVector(bool autodelete = true) 01272 : m_length(0), m_objects(0), m_delete(autodelete) 01273 { } 01274 01280 ObjVector(unsigned int maxLen, bool autodelete = true); 01281 01289 ObjVector(ObjList& list, bool move = true, unsigned int maxLen = 0, bool autodelete = true); 01290 01294 virtual ~ObjVector(); 01295 01301 virtual void* getObject(const String& name) const; 01302 01307 inline unsigned int length() const 01308 { return m_length; } 01309 01314 unsigned int count() const; 01315 01321 inline GenObject* at(int index) const 01322 { return (index >= 0 && index < (int)m_length) ? m_objects[index] : 0; } 01323 01329 inline GenObject* operator[](signed int index) const 01330 { return at(index); } 01331 01337 inline GenObject* operator[](unsigned int index) const 01338 { return at(index); } 01339 01347 unsigned int assign(ObjList& list, bool move = true, unsigned int maxLen = 0); 01348 01354 GenObject* take(unsigned int index); 01355 01362 bool set(GenObject* obj, unsigned int index); 01363 01369 int index(const GenObject* obj) const; 01370 01376 int index(const String& str) const; 01377 01381 void clear(); 01382 01387 inline bool autoDelete() 01388 { return m_delete; } 01389 01394 inline void setDelete(bool autodelete) 01395 { m_delete = autodelete; } 01396 01397 private: 01398 unsigned int m_length; 01399 GenObject** m_objects; 01400 bool m_delete; 01401 }; 01402 01411 class YATE_API Array : public RefObject 01412 { 01413 public: 01419 explicit Array(int columns = 0, int rows = 0); 01420 01424 virtual ~Array(); 01425 01431 virtual void* getObject(const String& name) const; 01432 01439 bool addRow(ObjList* row = 0, int index = -1); 01440 01447 bool addColumn(ObjList* column = 0, int index = -1); 01448 01454 bool delRow(int index); 01455 01461 bool delColumn(int index); 01462 01469 GenObject* get(int column, int row) const; 01470 01477 GenObject* take(int column, int row); 01478 01486 bool set(GenObject* obj, int column, int row); 01487 01492 inline int getRows() const 01493 { return m_rows; } 01494 01499 inline int getColumns() const 01500 { return m_columns; } 01501 01509 inline ObjList* getColumn(int column) const { 01510 if (column >= 0 || column < m_columns) 01511 return static_cast<ObjList*>(m_obj[column]); 01512 return 0; 01513 } 01514 01515 private: 01516 int m_rows; 01517 int m_columns; 01518 ObjList m_obj; 01519 }; 01520 01521 class Regexp; 01522 class StringMatchPrivate; 01523 01531 class YATE_API String : public GenObject 01532 { 01533 public: 01537 String(); 01538 01544 String(const char* value, int len = -1); 01545 01551 explicit String(char value, unsigned int repeat = 1); 01552 01557 explicit String(int value); 01558 01563 explicit String(unsigned int value); 01564 01569 explicit String(bool value); 01570 01575 String(const String& value); 01576 01581 String(const String* value); 01582 01586 virtual ~String(); 01587 01593 virtual void* getObject(const String& name) const; 01594 01599 static const String& empty(); 01600 01606 inline static const char* boolText(bool value) 01607 { return value ? "true" : "false"; } 01608 01613 inline const char* c_str() const 01614 { return m_string; } 01615 01620 inline const char* safe() const 01621 { return m_string ? m_string : ""; } 01622 01628 inline const char* safe(const char* defStr) const 01629 { return m_string ? m_string : (defStr ? defStr : ""); } 01630 01635 inline unsigned int length() const 01636 { return m_length; } 01637 01642 inline bool null() const 01643 { return !m_string; } 01644 01652 static int lenUtf8(const char* value, unsigned int maxSeq = 4, bool overlong = false); 01653 01660 inline int lenUtf8(unsigned int maxSeq = 4, bool overlong = false) const 01661 { return lenUtf8(m_string,maxSeq,overlong); } 01662 01663 01671 int fixUtf8(const char* replace = 0, unsigned int maxSeq = 4, bool overlong = false); 01672 01677 unsigned int hash() const; 01678 01684 static unsigned int hash(const char* value); 01685 01689 void clear(); 01690 01696 char at(int index) const; 01697 01704 String substr(int offs, int len = -1) const; 01705 01709 String& trimBlanks(); 01710 01715 String& trimSpaces(); 01716 01721 virtual const String& toString() const; 01722 01733 int toInteger(int defvalue = 0, int base = 0, int minvalue = INT_MIN, 01734 int maxvalue = INT_MAX, bool clamp = true) const; 01735 01743 int toInteger(const TokenDict* tokens, int defvalue = 0, int base = 0) const; 01744 01755 long int toLong(long int defvalue = 0, int base = 0, long int minvalue = LONG_MIN, 01756 long int maxvalue = LONG_MAX, bool clamp = true) const; 01757 01763 double toDouble(double defvalue = 0.0) const; 01764 01770 bool toBoolean(bool defvalue = false) const; 01771 01776 bool isBoolean() const; 01777 01782 String& toUpper(); 01783 01788 String& toLower(); 01789 01795 inline char operator[](signed int index) const 01796 { return at(index); } 01797 01803 inline char operator[](unsigned int index) const 01804 { return at(index); } 01805 01810 inline operator const char*() const 01811 { return m_string; }; 01812 01819 String& assign(const char* value, int len = -1); 01820 01827 String& assign(char value, unsigned int repeat = 1); 01828 01837 String& hexify(void* data, unsigned int len, char sep = 0, bool upCase = false); 01838 01842 inline String& operator=(const String& value) 01843 { return operator=(value.c_str()); } 01844 01849 inline String& operator=(const String* value) 01850 { return operator=(value ? value->c_str() : ""); } 01851 01856 String& operator=(const char* value); 01857 01861 String& operator=(char value); 01862 01866 String& operator=(int value); 01867 01871 String& operator=(unsigned int value); 01872 01876 inline String& operator=(bool value) 01877 { return operator=(boolText(value)); } 01878 01883 String& operator+=(const char* value); 01884 01888 String& operator+=(char value); 01889 01893 String& operator+=(int value); 01894 01898 String& operator+=(unsigned int value); 01899 01903 inline String& operator+=(bool value) 01904 { return operator+=(boolText(value)); } 01905 01909 bool operator==(const char* value) const; 01910 01914 bool operator!=(const char* value) const; 01915 01919 bool operator==(const String& value) const; 01920 01924 bool operator!=(const String& value) const; 01925 01929 bool operator&=(const char* value) const; 01930 01934 bool operator|=(const char* value) const; 01935 01939 inline String& operator<<(const char* value) 01940 { return operator+=(value); } 01941 01945 inline String& operator<<(char value) 01946 { return operator+=(value); } 01947 01951 inline String& operator<<(int value) 01952 { return operator+=(value); } 01953 01957 inline String& operator<<(unsigned int value) 01958 { return operator+=(value); } 01959 01963 inline String& operator<<(bool value) 01964 { return operator+=(value); } 01965 01970 String& operator>>(const char* skip); 01971 01975 String& operator>>(char& store); 01976 01980 String& operator>>(int& store); 01981 01985 String& operator>>(unsigned int& store); 01986 01990 String& operator>>(bool& store); 01991 01998 String& append(const char* value, const char* separator = 0, bool force = false); 01999 02006 String& append(const ObjList* list, const char* separator = 0, bool force = false); 02007 02014 inline String& append(const ObjList& list, const char* separator = 0, bool force = false) 02015 { return append(&list,separator,force); } 02016 02022 String& append(double value, unsigned int decimals = 3); 02023 02030 int find(char what, unsigned int offs = 0) const; 02031 02038 int find(const char* what, unsigned int offs = 0) const; 02039 02045 int rfind(char what) const; 02046 02054 bool startsWith(const char* what, bool wordBreak = false, bool caseInsensitive = false) const; 02055 02063 bool endsWith(const char* what, bool wordBreak = false, bool caseInsensitive = false) const; 02064 02076 bool startSkip(const char* what, bool wordBreak = true, bool caseInsensitive = false); 02077 02084 String& extractTo(const char* sep, String& store); 02085 02092 String& extractTo(const char* sep, bool& store); 02093 02101 String& extractTo(const char* sep, int& store, int base = 0); 02102 02111 String& extractTo(const char* sep, int& store, const TokenDict* tokens, int base = 0); 02112 02119 String& extractTo(const char* sep, double& store); 02120 02126 virtual bool matches(const String& value) const 02127 { return operator==(value); } 02128 02134 bool matches(const Regexp& rexp); 02135 02141 int matchOffset(int index = 0) const; 02142 02148 int matchLength(int index = 0) const; 02149 02155 inline String matchString(int index = 0) const 02156 { return substr(matchOffset(index),matchLength(index)); } 02157 02163 String replaceMatches(const String& templ) const; 02164 02169 int matchCount() const; 02170 02177 ObjList* split(char separator, bool emptyOK = true) const; 02178 02185 static String msgEscape(const char* str, char extraEsc = 0); 02186 02192 inline String msgEscape(char extraEsc = 0) const 02193 { return msgEscape(c_str(),extraEsc); } 02194 02202 static String msgUnescape(const char* str, int* errptr = 0, char extraEsc = 0); 02203 02210 inline String msgUnescape(int* errptr = 0, char extraEsc = 0) const 02211 { return msgUnescape(c_str(),errptr,extraEsc); } 02212 02219 static String sqlEscape(const char* str, char extraEsc = 0); 02220 02226 inline String sqlEscape(char extraEsc = 0) const 02227 { return sqlEscape(c_str(),extraEsc); } 02228 02236 static String uriEscape(const char* str, char extraEsc = 0, const char* noEsc = 0); 02237 02244 inline String uriEscape(char extraEsc = 0, const char* noEsc = 0) const 02245 { return uriEscape(c_str(),extraEsc,noEsc); } 02246 02253 static String uriUnescape(const char* str, int* errptr = 0); 02254 02260 inline String uriUnescape(int* errptr = 0) const 02261 { return uriUnescape(c_str(),errptr); } 02262 02263 protected: 02267 virtual void changed(); 02268 02269 private: 02270 void clearMatches(); 02271 char* m_string; 02272 unsigned int m_length; 02273 // I hope every C++ compiler now knows about mutable... 02274 mutable unsigned int m_hash; 02275 StringMatchPrivate* m_matches; 02276 }; 02277 02283 inline const char* c_str(const String* str) 02284 { return str ? str->c_str() : (const char*)0; } 02285 02291 inline const char* c_safe(const char* str) 02292 { return str ? str : ""; } 02293 02299 inline const char* c_safe(const String* str) 02300 { return str ? str->safe() : ""; } 02301 02307 inline bool null(const char* str) 02308 { return !(str && *str); } 02309 02315 inline bool null(const String* str) 02316 { return !str || str->null(); } 02317 02321 YATE_API String operator+(const String& s1, const String& s2); 02322 02326 YATE_API String operator+(const String& s1, const char* s2); 02327 02331 YATE_API String operator+(const char* s1, const String& s2); 02332 02337 inline const char *strcpy(String& dest, const char* src) 02338 { dest = src; return dest.c_str(); } 02339 02344 inline const char *strcat(String& dest, const char* src) 02345 { dest += src; return dest.c_str(); } 02346 02355 YATE_API int lookup(const char* str, const TokenDict* tokens, int defvalue = 0, int base = 0); 02356 02363 YATE_API const char* lookup(int value, const TokenDict* tokens, const char* defvalue = 0); 02364 02365 02370 class YATE_API Regexp : public String 02371 { 02372 friend class String; 02373 public: 02377 Regexp(); 02378 02385 explicit Regexp(const char* value, bool extended = false, bool insensitive = false); 02386 02391 Regexp(const Regexp& value); 02392 02396 virtual ~Regexp(); 02397 02401 inline Regexp& operator=(const char* value) 02402 { String::operator=(value); return *this; } 02403 02408 bool compile() const; 02409 02415 bool matches(const char* value) const; 02416 02422 virtual bool matches(const String& value) const 02423 { return Regexp::matches(value.safe()); } 02424 02430 void setFlags(bool extended, bool insensitive); 02431 02436 bool isExtended() const; 02437 02442 bool isCaseInsensitive() const; 02443 02444 protected: 02448 virtual void changed(); 02449 02450 private: 02451 void cleanup(); 02452 bool matches(const char* value, StringMatchPrivate* matchlist) const; 02453 mutable void* m_regexp; 02454 int m_flags; 02455 }; 02456 02461 class YATE_API CapturedEvent : public String 02462 { 02463 friend class Engine; 02464 YCLASS(CapturedEvent,String) 02465 public: 02471 inline CapturedEvent(int level, const char* text) 02472 : String(text), m_level(level) 02473 { } 02474 02479 inline CapturedEvent(const CapturedEvent& original) 02480 : String(original), m_level(original.level()) 02481 { } 02482 02487 inline int level() const 02488 { return m_level; } 02489 02490 02495 inline static bool capturing() 02496 { return s_capturing; } 02497 02502 inline static const ObjList& events() 02503 { return s_events; } 02504 02510 inline static void append(int level, const char* text) 02511 { if (text && *text) s_events.append(new CapturedEvent(level,text)); } 02512 02513 protected: 02518 inline static ObjList& eventsRw() 02519 { return s_events; } 02520 02525 inline static void capturing(bool capture) 02526 { s_capturing = capture; } 02527 02528 private: 02529 int m_level; 02530 static ObjList s_events; 02531 static bool s_capturing; 02532 }; 02533 02538 class YATE_API NamedString : public String 02539 { 02540 YNOCOPY(NamedString); // no automatic copies please 02541 public: 02547 explicit NamedString(const char* name, const char* value = 0); 02548 02553 inline const String& name() const 02554 { return m_name; } 02555 02560 virtual const String& toString() const; 02561 02567 virtual void* getObject(const String& name) const; 02568 02572 inline NamedString& operator=(const char* value) 02573 { String::operator=(value); return *this; } 02574 02575 private: 02576 NamedString(); // no default constructor please 02577 String m_name; 02578 }; 02579 02586 class YATE_API NamedPointer : public NamedString 02587 { 02588 public: 02595 explicit NamedPointer(const char* name, GenObject* data = 0, const char* value = 0); 02596 02600 virtual ~NamedPointer(); 02601 02606 inline GenObject* userData() const 02607 { return m_data; } 02608 02614 GenObject* takeData(); 02615 02621 void userData(GenObject* data); 02622 02628 inline void* userObject(const String& name) const 02629 { return m_data ? m_data->getObject(name) : 0; } 02630 02634 inline NamedPointer& operator=(const char* value) 02635 { NamedString::operator=(value); return *this; } 02636 02642 virtual void* getObject(const String& name) const; 02643 02644 protected: 02648 virtual void changed(); 02649 02650 private: 02651 NamedPointer(); // no default constructor please 02652 GenObject* m_data; 02653 }; 02654 02662 class YATE_API HashList : public GenObject 02663 { 02664 YNOCOPY(HashList); // no automatic copies please 02665 public: 02670 explicit HashList(unsigned int size = 17); 02671 02675 virtual ~HashList(); 02676 02682 virtual void* getObject(const String& name) const; 02683 02688 inline unsigned int length() const 02689 { return m_size; } 02690 02695 unsigned int count() const; 02696 02703 inline ObjList* getList(unsigned int index) const 02704 { return (index < m_size) ? m_lists[index] : 0; } 02705 02711 inline ObjList* getHashList(unsigned int hash) const 02712 { return getList(hash % m_size); } 02713 02719 inline ObjList* getHashList(const String& str) const 02720 { return getHashList(str.hash()); } 02721 02727 GenObject* operator[](const String& str) const; 02728 02734 ObjList* find(const GenObject* obj) const; 02735 02741 ObjList* find(const String& str) const; 02742 02748 ObjList* append(const GenObject* obj); 02749 02756 GenObject* remove(GenObject* obj, bool delobj = true); 02757 02761 void clear(); 02762 02769 bool resync(GenObject* obj); 02770 02776 bool resync(); 02777 02778 private: 02779 unsigned int m_size; 02780 ObjList** m_lists; 02781 }; 02782 02789 class YATE_API ListIterator 02790 { 02791 YNOCOPY(ListIterator); // no automatic copies please 02792 public: 02799 ListIterator(ObjList& list, int offset = 0); 02800 02807 ListIterator(HashList& list, int offset = 0); 02808 02812 ~ListIterator(); 02813 02818 inline unsigned int length() const 02819 { return m_length; } 02820 02824 void clear(); 02825 02831 void assign(ObjList& list, int offset = 0); 02832 02838 void assign(HashList& list, int offset = 0); 02839 02846 GenObject* get(unsigned int index) const; 02847 02860 GenObject* get(); 02861 02866 inline bool eof() const 02867 { return m_current >= m_length; } 02868 02872 inline void reset() 02873 { m_current = 0; } 02874 02875 private: 02876 ObjList* m_objList; 02877 HashList* m_hashList; 02878 GenObject** m_objects; 02879 unsigned int m_length; 02880 unsigned int m_current; 02881 }; 02882 02887 class YATE_API Time 02888 { 02889 public: 02893 inline Time() 02894 : m_time(now()) 02895 { } 02896 02901 inline Time(u_int64_t usec) 02902 : m_time(usec) 02903 { } 02904 02909 inline explicit Time(const struct timeval* tv) 02910 : m_time(fromTimeval(tv)) 02911 { } 02912 02917 inline explicit Time(const struct timeval& tv) 02918 : m_time(fromTimeval(tv)) 02919 { } 02920 02925 inline ~Time() 02926 { } 02927 02932 inline u_int32_t sec() const 02933 { return (u_int32_t)((m_time+500000) / 1000000); } 02934 02939 inline u_int64_t msec() const 02940 { return (m_time+500) / 1000; } 02941 02946 inline u_int64_t usec() const 02947 { return m_time; } 02948 02952 inline operator u_int64_t() const 02953 { return m_time; } 02954 02958 inline Time& operator=(u_int64_t usec) 02959 { m_time = usec; return *this; } 02960 02964 inline Time& operator+=(int64_t delta) 02965 { m_time += delta; return *this; } 02966 02970 inline Time& operator-=(int64_t delta) 02971 { m_time -= delta; return *this; } 02972 02977 inline void toTimeval(struct timeval* tv) const 02978 { toTimeval(tv, m_time); } 02979 02985 static void toTimeval(struct timeval* tv, u_int64_t usec); 02986 02992 static u_int64_t fromTimeval(const struct timeval* tv); 02993 02999 inline static u_int64_t fromTimeval(const struct timeval& tv) 03000 { return fromTimeval(&tv); } 03001 03006 static u_int64_t now(); 03007 03012 static u_int64_t msecNow(); 03013 03018 static u_int32_t secNow(); 03019 03033 static unsigned int toEpoch(int year, unsigned int month, unsigned int day, 03034 unsigned int hour, unsigned int minute, unsigned int sec, int offset = 0); 03035 03047 static bool toDateTime(unsigned int epochTimeSec, int& year, unsigned int& month, 03048 unsigned int& day, unsigned int& hour, unsigned int& minute, unsigned int& sec); 03049 03055 static inline bool isLeap(unsigned int year) 03056 { return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)); } 03057 03058 private: 03059 u_int64_t m_time; 03060 }; 03061 03066 class YATE_API Random 03067 { 03068 public: 03073 inline Random(u_int32_t seed = Time::now() & 0xffffffff) 03074 : m_random(seed) 03075 { } 03076 03081 inline u_int32_t get() const 03082 { return m_random; } 03083 03088 inline void set(u_int32_t seed) 03089 { m_random = seed; } 03090 03095 u_int32_t next(); 03096 03101 static long int random(); 03102 03107 static void srandom(unsigned int seed); 03108 03109 private: 03110 u_int32_t m_random; 03111 }; 03112 03117 class YATE_API DataBlock : public GenObject 03118 { 03119 public: 03120 03124 DataBlock(); 03125 03129 DataBlock(const DataBlock& value); 03130 03137 DataBlock(void* value, unsigned int len, bool copyData = true); 03138 03142 virtual ~DataBlock(); 03143 03149 virtual void* getObject(const String& name) const; 03150 03154 static const DataBlock& empty(); 03155 03160 inline void* data() const 03161 { return m_data; } 03162 03169 inline unsigned char* data(unsigned int offs, unsigned int len = 1) const 03170 { return (offs + len <= m_length) ? (static_cast<unsigned char*>(m_data) + offs) : 0; } 03171 03178 inline int at(unsigned int offs, int defvalue = -1) const 03179 { return (offs < m_length) ? static_cast<unsigned char*>(m_data)[offs] : defvalue; } 03180 03185 inline bool null() const 03186 { return !m_data; } 03187 03192 inline unsigned int length() const 03193 { return m_length; } 03194 03199 void clear(bool deleteData = true); 03200 03207 DataBlock& assign(void* value, unsigned int len, bool copyData = true); 03208 03214 inline void append(void* value, unsigned int len) { 03215 DataBlock tmp(value,len,false); 03216 append(tmp); 03217 tmp.clear(false); 03218 } 03219 03224 void append(const DataBlock& value); 03225 03230 void append(const String& value); 03231 03236 void insert(const DataBlock& value); 03237 03242 inline void resize(unsigned int len) { 03243 if (len != length()) 03244 assign(0,len); 03245 } 03246 03251 void truncate(unsigned int len); 03252 03257 void cut(int len); 03258 03264 inline int operator[](signed int index) const 03265 { return at(index); } 03266 03272 inline int operator[](unsigned int index) const 03273 { return at(index); } 03274 03278 DataBlock& operator=(const DataBlock& value); 03279 03283 inline DataBlock& operator+=(const DataBlock& value) 03284 { append(value); return *this; } 03285 03289 inline DataBlock& operator+=(const String& value) 03290 { append(value); return *this; } 03291 03300 bool convert(const DataBlock& src, const String& sFormat, 03301 const String& dFormat, unsigned maxlen = 0); 03302 03313 bool unHexify(const char* data, unsigned int len, char sep = 0); 03314 03320 String sqlEscape(char extraEsc) const; 03321 03322 private: 03323 void* m_data; 03324 unsigned int m_length; 03325 }; 03326 03331 class YATE_API MD5 03332 { 03333 public: 03337 MD5(); 03338 03343 MD5(const MD5& original); 03344 03350 MD5(const void* buf, unsigned int len); 03351 03356 MD5(const DataBlock& data); 03357 03362 MD5(const String& str); 03363 03367 ~MD5(); 03368 03372 MD5& operator=(const MD5& original); 03373 03377 void clear(); 03378 03383 void finalize(); 03384 03391 bool update(const void* buf, unsigned int len); 03392 03398 inline bool update(const DataBlock& data) 03399 { return update(data.data(), data.length()); } 03400 03406 inline bool update(const String& str) 03407 { return update(str.c_str(), str.length()); } 03408 03412 inline MD5& operator<<(const String& value) 03413 { update(value); return *this; } 03414 03418 inline MD5& operator<<(const DataBlock& data) 03419 { update(data); return *this; } 03420 03424 MD5& operator<<(const char* value); 03425 03431 const unsigned char* rawDigest(); 03432 03437 inline static unsigned int rawLength() 03438 { return 16; } 03439 03445 const String& hexDigest(); 03446 03447 private: 03448 void init(); 03449 void* m_private; 03450 String m_hex; 03451 unsigned char m_bin[16]; 03452 }; 03453 03458 class YATE_API SHA1 03459 { 03460 public: 03464 SHA1(); 03465 03470 SHA1(const SHA1& original); 03471 03477 SHA1(const void* buf, unsigned int len); 03478 03483 SHA1(const DataBlock& data); 03484 03489 SHA1(const String& str); 03490 03494 ~SHA1(); 03495 03499 SHA1& operator=(const SHA1& original); 03500 03504 void clear(); 03505 03510 void finalize(); 03511 03518 bool update(const void* buf, unsigned int len); 03519 03525 inline bool update(const DataBlock& data) 03526 { return update(data.data(), data.length()); } 03527 03533 inline bool update(const String& str) 03534 { return update(str.c_str(), str.length()); } 03535 03539 inline SHA1& operator<<(const String& value) 03540 { update(value); return *this; } 03541 03545 inline SHA1& operator<<(const DataBlock& data) 03546 { update(data); return *this; } 03547 03551 SHA1& operator<<(const char* value); 03552 03558 const unsigned char* rawDigest(); 03559 03564 inline static unsigned int rawLength() 03565 { return 20; } 03566 03572 const String& hexDigest(); 03573 03574 private: 03575 void init(); 03576 void* m_private; 03577 String m_hex; 03578 unsigned char m_bin[20]; 03579 }; 03580 03585 class YATE_API Base64 : public DataBlock 03586 { 03587 YNOCOPY(Base64); // no automatic copies please 03588 public: 03592 inline Base64() 03593 { } 03594 03601 inline Base64(void* src, unsigned int len, bool copyData = true) 03602 : DataBlock(src,len,copyData) 03603 { } 03604 03614 void encode(String& dest, unsigned int lineLen = 0, bool lineAtEnd = false); 03615 03627 bool decode(DataBlock& dest, bool liberal = true); 03628 03632 inline Base64& operator<<(const String& value) 03633 { append(value); return *this; } 03634 03638 inline Base64& operator<<(const DataBlock& data) 03639 { append(data); return *this; } 03640 03644 inline Base64& operator<<(const char* value) 03645 { return operator<<(String(value)); } 03646 }; 03647 03648 class NamedIterator; 03649 03654 class YATE_API NamedList : public String 03655 { 03656 friend class NamedIterator; 03657 public: 03662 explicit NamedList(const char* name); 03663 03668 NamedList(const NamedList& original); 03669 03676 NamedList(const char* name, const NamedList& original, const String& prefix); 03677 03683 NamedList& operator=(const NamedList& value); 03684 03690 virtual void* getObject(const String& name) const; 03691 03696 inline unsigned int length() const 03697 { return m_params.length(); } 03698 03703 inline unsigned int count() const 03704 { return m_params.count(); } 03705 03709 inline void clearParams() 03710 { m_params.clear(); } 03711 03717 NamedList& addParam(NamedString* param); 03718 03726 NamedList& addParam(const char* name, const char* value, bool emptyOK = true); 03727 03733 NamedList& setParam(NamedString* param); 03734 03741 NamedList& setParam(const char* name, const char* value); 03742 03749 NamedList& clearParam(const String& name, char childSep = 0); 03750 03756 NamedList& clearParam(NamedString* param); 03757 03765 NamedList& copyParam(const NamedList& original, const String& name, char childSep = 0); 03766 03772 NamedList& copyParams(const NamedList& original); 03773 03781 NamedList& copyParams(const NamedList& original, ObjList* list, char childSep = 0); 03782 03790 NamedList& copyParams(const NamedList& original, const String& list, char childSep = 0); 03791 03799 NamedList& copySubParams(const NamedList& original, const String& prefix, bool skipPrefix = true); 03800 03806 int getIndex(const NamedString* param) const; 03807 03813 int getIndex(const String& name) const; 03814 03820 NamedString* getParam(const String& name) const; 03821 03827 NamedString* getParam(unsigned int index) const; 03828 03834 const String& operator[](const String& name) const; 03835 03842 const char* getValue(const String& name, const char* defvalue = 0) const; 03843 03854 int getIntValue(const String& name, int defvalue = 0, int minvalue = INT_MIN, 03855 int maxvalue = INT_MAX, bool clamp = true) const; 03856 03864 int getIntValue(const String& name, const TokenDict* tokens, int defvalue = 0) const; 03865 03872 double getDoubleValue(const String& name, double defvalue = 0.0) const; 03873 03880 bool getBoolValue(const String& name, bool defvalue = false) const; 03881 03889 int replaceParams(String& str, bool sqlEsc = false, char extraEsc = 0) const; 03890 03899 void dump(String& str, const char* separator, char quote = 0, bool force = false) const; 03900 03905 static const NamedList& empty(); 03906 03907 private: 03908 NamedList(); // no default constructor please 03909 ObjList m_params; 03910 }; 03911 03917 class YATE_API NamedIterator 03918 { 03919 public: 03924 inline NamedIterator(const NamedList& list) 03925 : m_list(&list), m_item(list.m_params.skipNull()) 03926 { } 03927 03932 inline NamedIterator(const NamedIterator& original) 03933 : m_list(original.m_list), m_item(original.m_item) 03934 { } 03935 03940 inline NamedIterator& operator=(const NamedList& list) 03941 { m_list = &list; m_item = list.m_params.skipNull(); return *this; } 03942 03947 inline NamedIterator& operator=(const NamedIterator& original) 03948 { m_list = original.m_list; m_item = original.m_item; return *this; } 03949 03954 const NamedString* get(); 03955 03959 inline bool eof() const 03960 { return !m_item; } 03961 03965 inline void reset() 03966 { m_item = m_list->m_params.skipNull(); } 03967 03968 private: 03969 NamedIterator(); // no default constructor please 03970 const NamedList* m_list; 03971 const ObjList* m_item; 03972 }; 03973 03979 class YATE_API URI : public String 03980 { 03981 public: 03985 URI(); 03986 03991 URI(const URI& uri); 03992 03997 explicit URI(const String& uri); 03998 04003 explicit URI(const char* uri); 04004 04013 URI(const char* proto, const char* user, const char* host, int port = 0, const char* desc = 0); 04014 04018 void parse() const; 04019 04024 inline URI& operator=(const URI& value) 04025 { String::operator=(value); return *this; } 04026 04031 inline URI& operator=(const String& value) 04032 { String::operator=(value); return *this; } 04033 04038 inline URI& operator=(const char* value) 04039 { String::operator=(value); return *this; } 04040 04045 inline const String& getDescription() const 04046 { parse(); return m_desc; } 04047 04052 inline const String& getProtocol() const 04053 { parse(); return m_proto; } 04054 04059 inline const String& getUser() const 04060 { parse(); return m_user; } 04061 04066 inline const String& getHost() const 04067 { parse(); return m_host; } 04068 04073 inline int getPort() const 04074 { parse(); return m_port; } 04075 04080 inline const String& getExtra() const 04081 { parse(); return m_extra; } 04082 04083 protected: 04089 virtual void changed(); 04090 mutable bool m_parsed; 04091 mutable String m_desc; 04092 mutable String m_proto; 04093 mutable String m_user; 04094 mutable String m_host; 04095 mutable String m_extra; 04096 mutable int m_port; 04097 }; 04098 04099 class MutexPrivate; 04100 class SemaphorePrivate; 04101 class ThreadPrivate; 04102 04107 class YATE_API Lockable 04108 { 04109 public: 04113 virtual ~Lockable(); 04114 04120 virtual bool lock(long maxwait = -1) = 0; 04121 04126 virtual bool unlock() = 0; 04127 04133 virtual bool locked() const = 0; 04134 04140 virtual bool check(long maxwait = -1); 04141 04148 virtual bool unlockAll(); 04149 04155 static void wait(unsigned long maxwait); 04156 04161 static unsigned long wait(); 04162 04169 static void startUsingNow(); 04170 04176 static void disableSafety(); 04177 }; 04178 04183 class YATE_API Mutex : public Lockable 04184 { 04185 friend class MutexPrivate; 04186 public: 04193 explicit Mutex(bool recursive = false, const char* name = 0); 04194 04199 Mutex(const Mutex& original); 04200 04204 ~Mutex(); 04205 04210 Mutex& operator=(const Mutex& original); 04211 04217 virtual bool lock(long maxwait = -1); 04218 04223 virtual bool unlock(); 04224 04230 virtual bool locked() const; 04231 04236 const char* owner() const; 04237 04242 bool recursive() const; 04243 04248 static int count(); 04249 04254 static int locks(); 04255 04260 static bool efficientTimedLock(); 04261 04262 private: 04263 MutexPrivate* privDataCopy() const; 04264 MutexPrivate* m_private; 04265 }; 04266 04273 class YATE_API MutexPool 04274 { 04275 public: 04286 MutexPool(unsigned int len = 13, bool recursive = false, const char* name = 0); 04287 04291 ~MutexPool(); 04292 04300 inline unsigned int index(void* ptr) const 04301 { return ((unsigned int)(unsigned long)ptr) % m_length; } 04302 04310 inline Mutex* mutex(void* ptr) const 04311 { return m_data[index(ptr)]; } 04312 04318 inline Mutex* mutex(unsigned int idx) const 04319 { return m_data[idx % m_length]; } 04320 04321 private: 04322 String* m_name; // Mutex names 04323 Mutex** m_data; // The array 04324 unsigned int m_length; // Array length 04325 }; 04326 04331 class YATE_API Semaphore : public Lockable 04332 { 04333 friend class SemaphorePrivate; 04334 public: 04340 explicit Semaphore(unsigned int maxcount = 1, const char* name = 0); 04341 04346 Semaphore(const Semaphore& original); 04347 04351 ~Semaphore(); 04352 04357 Semaphore& operator=(const Semaphore& original); 04358 04364 virtual bool lock(long maxwait = -1); 04365 04370 virtual bool unlock(); 04371 04377 virtual bool locked() const; 04378 04383 static int count(); 04384 04389 static int locks(); 04390 04395 static bool efficientTimedLock(); 04396 04397 private: 04398 SemaphorePrivate* privDataCopy() const; 04399 SemaphorePrivate* m_private; 04400 }; 04401 04407 class YATE_API Lock 04408 { 04409 YNOCOPY(Lock); // no automatic copies please 04410 public: 04416 inline Lock(Lockable& lck, long maxwait = -1) 04417 { m_lock = lck.lock(maxwait) ? &lck : 0; } 04418 04424 inline Lock(Lockable* lck, long maxwait = -1) 04425 { m_lock = (lck && lck->lock(maxwait)) ? lck : 0; } 04426 04430 inline ~Lock() 04431 { if (m_lock) m_lock->unlock(); } 04432 04437 inline Lockable* locked() const 04438 { return m_lock; } 04439 04443 inline void drop() 04444 { if (m_lock) m_lock->unlock(); m_lock = 0; } 04445 04446 private: 04447 Lockable* m_lock; 04448 04450 inline void* operator new(size_t); 04451 04453 inline void* operator new[](size_t); 04454 }; 04455 04462 class YATE_API Lock2 04463 { 04464 YNOCOPY(Lock2); // no automatic copies please 04465 public: 04472 inline Lock2(Mutex* mx1, Mutex* mx2, long maxwait = -1) 04473 : m_mx1(0), m_mx2(0) 04474 { lock(mx1,mx2,maxwait); } 04475 04482 inline Lock2(Mutex& mx1, Mutex& mx2, long maxwait = -1) 04483 : m_mx1(0), m_mx2(0) 04484 { lock(&mx1,&mx2,maxwait); } 04485 04489 inline ~Lock2() 04490 { drop(); } 04491 04496 inline bool locked() const 04497 { return m_mx1 != 0; } 04498 04506 bool lock(Mutex* mx1, Mutex* mx2, long maxwait = -1); 04507 04515 inline bool lock(Mutex& mx1, Mutex& mx2, long maxwait = -1) 04516 { return lock(&mx1,&mx2,maxwait); } 04517 04521 void drop(); 04522 04523 private: 04524 Mutex* m_mx1; 04525 Mutex* m_mx2; 04526 04528 inline void* operator new(size_t); 04529 04531 inline void* operator new[](size_t); 04532 }; 04533 04539 class YATE_API Runnable 04540 { 04541 public: 04546 virtual void run() = 0; 04547 04551 virtual ~Runnable(); 04552 }; 04553 04560 class YATE_API Thread : public Runnable 04561 { 04562 friend class ThreadPrivate; 04563 friend class MutexPrivate; 04564 friend class SemaphorePrivate; 04565 YNOCOPY(Thread); // no automatic copies please 04566 public: 04570 enum Priority { 04571 Lowest, 04572 Low, 04573 Normal, 04574 High, 04575 Highest 04576 }; 04577 04581 virtual void cleanup(); 04582 04587 bool startup(); 04588 04593 bool error() const; 04594 04599 bool running() const; 04600 04605 inline int locks() const 04606 { return m_locks; } 04607 04612 inline bool locked() const 04613 { return m_locking || m_locks; } 04614 04619 const char* name() const; 04620 04625 static const char* currentName(); 04626 04632 static void yield(bool exitCheck = false); 04633 04639 static void idle(bool exitCheck = false); 04640 04646 static void sleep(unsigned int sec, bool exitCheck = false); 04647 04653 static void msleep(unsigned long msec, bool exitCheck = false); 04654 04661 static void usleep(unsigned long usec, bool exitCheck = false); 04662 04667 static unsigned long idleUsec(); 04668 04673 static unsigned long idleMsec(); 04674 04679 static void idleMsec(unsigned long msec); 04680 04686 static Thread* current(); 04687 04692 static int count(); 04693 04699 static bool check(bool exitNow = true); 04700 04704 static void exit(); 04705 04710 void cancel(bool hard = false); 04711 04716 inline bool isCurrent() const 04717 { return current() == this; } 04718 04719 04726 static Priority priority(const char* name, Priority defvalue = Normal); 04727 04733 static const char* priority(Priority prio); 04734 04739 static void killall(); 04740 04745 static void preExec(); 04746 04752 static int lastError(); 04753 04760 static inline bool errorString(String& buffer) 04761 { return errorString(buffer,lastError()); } 04762 04773 static bool errorString(String& buffer, int code); 04774 04775 protected: 04781 Thread(const char *name = 0, Priority prio = Normal); 04782 04788 Thread(const char *name, const char* prio); 04789 04793 virtual ~Thread(); 04794 04795 private: 04796 ThreadPrivate* m_private; 04797 int m_locks; 04798 bool m_locking; 04799 }; 04800 04801 class Socket; 04802 04807 class YATE_API SocketAddr : public GenObject 04808 { 04809 public: 04813 inline SocketAddr() 04814 : m_address(0), m_length(0) 04815 { } 04816 04821 inline SocketAddr(const SocketAddr& value) 04822 : GenObject(), 04823 m_address(0), m_length(0) 04824 { assign(value.address(),value.length()); } 04825 04830 explicit SocketAddr(int family); 04831 04837 SocketAddr(const struct sockaddr* addr, socklen_t len = 0); 04838 04842 virtual ~SocketAddr(); 04843 04848 inline SocketAddr& operator=(const SocketAddr& value) 04849 { assign(value.address(),value.length()); return *this; } 04850 04856 bool operator==(const SocketAddr& other) const; 04857 04863 inline bool operator!=(const SocketAddr& other) const 04864 { return !operator==(other); } 04865 04869 void clear(); 04870 04876 bool assign(int family); 04877 04883 void assign(const struct sockaddr* addr, socklen_t len = 0); 04884 04890 bool local(const SocketAddr& remote); 04891 04896 inline bool valid() const 04897 { return m_length && m_address; } 04898 04903 inline bool null() const 04904 { return !(m_length && m_address); } 04905 04910 inline int family() const 04911 { return m_address ? m_address->sa_family : 0; } 04912 04917 inline const String& host() const 04918 { return m_host; } 04919 04924 virtual bool host(const String& name); 04925 04930 int port() const; 04931 04937 bool port(int newport); 04938 04943 inline struct sockaddr* address() const 04944 { return m_address; } 04945 04950 inline socklen_t length() const 04951 { return m_length; } 04952 04958 static bool supports(int family); 04959 04960 protected: 04964 virtual void stringify(); 04965 04966 struct sockaddr* m_address; 04967 socklen_t m_length; 04968 String m_host; 04969 }; 04970 04975 class YATE_API SocketFilter : public GenObject 04976 { 04977 friend class Socket; 04978 YNOCOPY(SocketFilter); // no automatic copies please 04979 public: 04983 SocketFilter(); 04984 04988 virtual ~SocketFilter(); 04989 04995 virtual void* getObject(const String& name) const; 04996 05001 virtual void timerTick(const Time& when); 05002 05012 virtual bool received(void* buffer, int length, int flags, const struct sockaddr* addr, socklen_t adrlen) = 0; 05013 05018 inline Socket* socket() const 05019 { return m_socket; } 05020 05025 bool valid() const; 05026 05027 private: 05028 Socket* m_socket; 05029 }; 05030 05035 class YATE_API Stream 05036 { 05037 public: 05041 enum SeekPos { 05042 SeekBegin, // Seek from start of stream 05043 SeekEnd, // Seek from stream end 05044 SeekCurrent // Seek from current position 05045 }; 05046 05050 virtual ~Stream(); 05051 05056 inline int error() const 05057 { return m_error; } 05058 05063 virtual bool terminate() = 0; 05064 05069 virtual bool canRetry() const; 05070 05075 virtual bool inProgress() const; 05076 05081 virtual bool valid() const = 0; 05082 05088 virtual bool setBlocking(bool block = true); 05089 05096 virtual int writeData(const void* buffer, int length) = 0; 05097 05103 int writeData(const char* str); 05104 05110 inline int writeData(const String& str) 05111 { return writeData(str.c_str(), str.length()); } 05112 05118 inline int writeData(const DataBlock& buf) 05119 { return writeData(buf.data(), buf.length()); } 05120 05127 virtual int readData(void* buffer, int length) = 0; 05128 05133 virtual int64_t length(); 05134 05141 virtual int64_t seek(SeekPos pos, int64_t offset = 0); 05142 05148 inline int64_t seek(int64_t offset) 05149 { return seek(SeekBegin,offset); } 05150 05157 static bool allocPipe(Stream*& reader, Stream*& writer); 05158 05165 static bool allocPair(Stream*& str1, Stream*& str2); 05166 05171 static bool supportsPipes(); 05172 05177 static bool supportsPairs(); 05178 05179 protected: 05183 inline Stream() 05184 : m_error(0) 05185 { } 05186 05190 inline void clearError() 05191 { m_error = 0; } 05192 05193 int m_error; 05194 }; 05195 05200 class YATE_API MemoryStream : public Stream 05201 { 05202 YNOCOPY(MemoryStream); // no automatic copies please 05203 public: 05207 inline MemoryStream() 05208 : m_offset(0) 05209 { } 05210 05215 inline MemoryStream(const DataBlock& data) 05216 : m_data(data), m_offset(0) 05217 { } 05218 05223 inline const DataBlock& data() const 05224 { return m_data; } 05225 05230 virtual bool terminate() 05231 { return true; } 05236 virtual bool valid() const 05237 { return true; } 05238 05245 virtual int writeData(const void* buffer, int len); 05246 05253 virtual int readData(void* buffer, int len); 05254 05259 virtual int64_t length() 05260 { return m_data.length(); } 05261 05268 virtual int64_t seek(SeekPos pos, int64_t offset = 0); 05269 05270 protected: 05274 DataBlock m_data; 05275 05279 int64_t m_offset; 05280 }; 05281 05286 class YATE_API File : public Stream 05287 { 05288 YNOCOPY(File); // no automatic copies please 05289 public: 05293 File(); 05294 05299 explicit File(HANDLE handle); 05300 05304 virtual ~File(); 05305 05318 virtual bool openPath(const char* name, bool canWrite = false, bool canRead = true, 05319 bool create = false, bool append = false, bool binary = false, 05320 bool pubReadable = false, bool pubWritable = false); 05321 05326 virtual bool terminate(); 05327 05332 void attach(HANDLE handle); 05333 05338 HANDLE detach(); 05339 05344 inline HANDLE handle() const 05345 { return m_handle; } 05346 05351 virtual bool canRetry() const; 05352 05357 virtual bool valid() const; 05358 05363 static HANDLE invalidHandle(); 05364 05370 virtual bool setBlocking(bool block = true); 05371 05376 virtual int64_t length(); 05377 05384 virtual int64_t seek(SeekPos pos, int64_t offset = 0); 05385 05392 virtual int writeData(const void* buffer, int length); 05393 05400 virtual int readData(void* buffer, int length); 05401 05407 bool getFileTime(unsigned int& secEpoch); 05408 05415 virtual bool md5(String& buffer); 05416 05424 static bool setFileTime(const char* name, unsigned int secEpoch, int* error = 0); 05425 05433 static bool getFileTime(const char* name, unsigned int& secEpoch, int* error = 0); 05434 05441 static bool exists(const char* name, int* error = 0); 05442 05450 static bool rename(const char* oldFile, const char* newFile, int* error = 0); 05451 05458 static bool remove(const char* name, int* error = 0); 05459 05467 static bool md5(const char* name, String& buffer, int* error = 0); 05468 05475 static bool mkDir(const char* path, int* error = 0); 05476 05483 static bool rmDir(const char* path, int* error = 0); 05484 05496 static bool listDirectory(const char* path, ObjList* dirs, ObjList* files, 05497 int* error = 0); 05498 05505 static bool createPipe(File& reader, File& writer); 05506 05507 protected: 05508 05512 void copyError(); 05513 05514 HANDLE m_handle; 05515 }; 05516 05521 class YATE_API Socket : public Stream 05522 { 05523 YNOCOPY(Socket); // no automatic copies please 05524 public: 05528 enum TOS { 05529 LowDelay = IPTOS_LOWDELAY, 05530 MaxThroughput = IPTOS_THROUGHPUT, 05531 MaxReliability = IPTOS_RELIABILITY, 05532 MinCost = IPTOS_MINCOST, 05533 }; 05534 05538 Socket(); 05539 05544 explicit Socket(SOCKET handle); 05545 05552 Socket(int domain, int type, int protocol = 0); 05553 05557 virtual ~Socket(); 05558 05566 virtual bool create(int domain, int type, int protocol = 0); 05567 05572 virtual bool terminate(); 05573 05578 void attach(SOCKET handle); 05579 05584 SOCKET detach(); 05585 05590 inline SOCKET handle() const 05591 { return m_handle; } 05592 05597 virtual bool canRetry() const; 05598 05603 virtual bool inProgress() const; 05604 05609 virtual bool valid() const; 05610 05615 static SOCKET invalidHandle(); 05616 05621 static int socketError(); 05622 05631 virtual bool setOption(int level, int name, const void* value = 0, socklen_t length = 0); 05632 05641 virtual bool getOption(int level, int name, void* buffer, socklen_t* length); 05642 05647 virtual bool setParams(const NamedList& params) 05648 { return false; } 05649 05655 virtual bool setTOS(int tos); 05656 05662 virtual bool setBlocking(bool block = true); 05663 05671 virtual bool setReuse(bool reuse = true, bool exclusive = false); 05672 05679 virtual bool setLinger(int seconds = -1); 05680 05687 virtual bool bind(struct sockaddr* addr, socklen_t addrlen); 05688 05694 inline bool bind(const SocketAddr& addr) 05695 { return bind(addr.address(), addr.length()); } 05696 05702 virtual bool listen(unsigned int backlog = 0); 05703 05710 virtual Socket* accept(struct sockaddr* addr = 0, socklen_t* addrlen = 0); 05711 05717 Socket* accept(SocketAddr& addr); 05718 05725 SOCKET acceptHandle(struct sockaddr* addr = 0, socklen_t* addrlen = 0); 05726 05734 bool updateError(); 05735 05740 static bool efficientSelect(); 05741 05747 static bool canSelect(SOCKET handle); 05748 05753 virtual bool canSelect() const; 05754 05761 virtual bool connect(struct sockaddr* addr, socklen_t addrlen); 05762 05768 inline bool connect(const SocketAddr& addr) 05769 { return connect(addr.address(), addr.length()); } 05770 05777 virtual bool shutdown(bool stopReads, bool stopWrites); 05778 05785 virtual bool getSockName(struct sockaddr* addr, socklen_t* addrlen); 05786 05792 bool getSockName(SocketAddr& addr); 05793 05800 virtual bool getPeerName(struct sockaddr* addr, socklen_t* addrlen); 05801 05807 bool getPeerName(SocketAddr& addr); 05808 05818 virtual int sendTo(const void* buffer, int length, const struct sockaddr* addr, socklen_t adrlen, int flags = 0); 05819 05828 inline int sendTo(const void* buffer, int length, const SocketAddr& addr, int flags = 0) 05829 { return sendTo(buffer, length, addr.address(), addr.length(), flags); } 05830 05838 virtual int send(const void* buffer, int length, int flags = 0); 05839 05846 virtual int writeData(const void* buffer, int length); 05847 05857 virtual int recvFrom(void* buffer, int length, struct sockaddr* addr = 0, socklen_t* adrlen = 0, int flags = 0); 05858 05867 int recvFrom(void* buffer, int length, SocketAddr& addr, int flags = 0); 05868 05876 virtual int recv(void* buffer, int length, int flags = 0); 05877 05884 virtual int readData(void* buffer, int length); 05885 05894 virtual bool select(bool* readok, bool* writeok, bool* except, struct timeval* timeout = 0); 05895 05904 bool select(bool* readok, bool* writeok, bool* except, int64_t timeout); 05905 05911 bool installFilter(SocketFilter* filter); 05912 05918 void removeFilter(SocketFilter* filter, bool delobj = false); 05919 05923 void clearFilters(); 05924 05931 virtual void timerTick(const Time& when); 05932 05940 static bool createPair(Socket& sock1, Socket& sock2, int domain = AF_UNIX); 05941 05942 protected: 05943 05947 void copyError(); 05948 05955 bool checkError(int retcode, bool strict = false); 05956 05966 bool applyFilters(void* buffer, int length, int flags, const struct sockaddr* addr = 0, socklen_t adrlen = 0); 05967 05968 SOCKET m_handle; 05969 ObjList m_filters; 05970 }; 05971 05976 class YATE_API SctpSocket : public Socket 05977 { 05978 YNOCOPY(SctpSocket); // no automatic copies please 05979 public: 05983 inline SctpSocket() 05984 { } 05985 05990 inline explicit SctpSocket(SOCKET fd) 05991 : Socket(fd) 05992 { } 05993 05997 virtual ~SctpSocket(); 05998 06004 virtual bool bindx(ObjList& addresses) = 0; 06005 06011 virtual bool connectx(ObjList& addresses) = 0; 06012 06022 virtual int sendTo(void* buffer, int length, int stream, SocketAddr& addr, int flags) = 0; 06023 06029 virtual Socket* accept(SocketAddr& addr) 06030 { return 0; } 06031 06040 virtual int sendMsg(const void* buf, int length, int stream, int& flags) = 0; 06041 06051 virtual int recvMsg(void* buf, int length, SocketAddr& addr, int& stream, int& flags) = 0; 06052 06059 virtual bool setStreams(int inbound, int outbound) = 0; 06060 06066 virtual bool subscribeEvents() = 0; 06067 06074 virtual bool getStreams(int& inbound, int& outbound) = 0; 06075 06081 virtual bool setPayload(u_int32_t payload) = 0; 06082 }; 06083 06088 class YATE_API DnsRecord : public GenObject 06089 { 06090 YCLASS(DnsRecord,GenObject) 06091 YNOCOPY(DnsRecord); 06092 public: 06098 inline DnsRecord(int order, int pref) 06099 : m_order(order), m_pref(pref) 06100 {} 06101 06105 inline DnsRecord() 06106 : m_order(0), m_pref(0) 06107 {} 06108 06113 inline int order() const 06114 { return m_order; } 06115 06120 inline int pref() const 06121 { return m_pref; } 06122 06128 virtual void dump(String& buf, const char* sep = " "); 06129 06137 static bool insert(ObjList& list, DnsRecord* rec, bool ascPref); 06138 06139 protected: 06140 int m_order; 06141 int m_pref; 06142 }; 06143 06148 class YATE_API SrvRecord : public DnsRecord 06149 { 06150 YCLASS(SrvRecord,DnsRecord) 06151 YNOCOPY(SrvRecord); 06152 public: 06160 inline SrvRecord(int prio, int weight, const char* addr, int port) 06161 : DnsRecord(prio,weight), m_address(addr), m_port(port) 06162 {} 06163 06168 inline const String& address() const 06169 { return m_address; } 06170 06175 inline int port() const 06176 { return m_port; } 06177 06183 virtual void dump(String& buf, const char* sep = " "); 06184 06190 static void copy(ObjList& dest, const ObjList& src); 06191 06192 protected: 06193 String m_address; 06194 int m_port; 06195 06196 private: 06197 SrvRecord() {} // No default contructor 06198 }; 06199 06204 class YATE_API NaptrRecord : public DnsRecord 06205 { 06206 YCLASS(NaptrRecord,DnsRecord) 06207 YNOCOPY(NaptrRecord); 06208 public: 06218 NaptrRecord(int ord, int pref, const char* flags, const char* serv, 06219 const char* regexp, const char* next); 06220 06227 bool replace(String& str); 06228 06234 virtual void dump(String& buf, const char* sep = " "); 06235 06240 inline const String& flags() const 06241 { return m_flags; } 06242 06247 inline const String& serv() const 06248 { return m_service; } 06249 06254 inline const String& nextName() const 06255 { return m_next; } 06256 06257 protected: 06258 String m_flags; 06259 String m_service; 06260 Regexp m_regmatch; 06261 String m_template; 06262 String m_next; 06263 06264 private: 06265 NaptrRecord() {} // No default contructor 06266 }; 06267 06272 class YATE_API Resolver 06273 { 06274 public: 06278 enum Type { 06279 Unknown, 06280 Srv, // SRV (Service Location) 06281 Naptr, // NAPTR (Naming Authority Pointer) 06282 }; 06283 06290 static bool available(Type type = Unknown); 06291 06298 static bool init(int timeout = -1, int retries = -1); 06299 06308 static int query(Type type, const char* dname, ObjList& result, String* error = 0); 06309 06317 static int srvQuery(const char* dname, ObjList& result, String* error = 0); 06318 06326 static int naptrQuery(const char* dname, ObjList& result, String* error = 0); 06327 06331 static const TokenDict s_types[]; 06332 }; 06333 06338 class YATE_API Cipher : public GenObject 06339 { 06340 public: 06344 enum Direction { 06345 Bidir, 06346 Encrypt, 06347 Decrypt, 06348 }; 06349 06354 inline static const TokenDict* directions() 06355 { return s_directions; } 06356 06363 inline static Direction direction(const char* name, Direction defdir = Bidir) 06364 { return (Direction)TelEngine::lookup(name,s_directions,defdir); } 06365 06369 virtual ~Cipher(); 06370 06376 virtual void* getObject(const String& name) const; 06377 06383 virtual bool valid(Direction dir = Bidir) const; 06384 06389 virtual unsigned int blockSize() const = 0; 06390 06395 virtual unsigned int initVectorSize() const; 06396 06402 unsigned int bufferSize(unsigned int len) const; 06403 06409 bool bufferFull(unsigned int len) const; 06410 06418 virtual bool setKey(const void* key, unsigned int len, Direction dir = Bidir) = 0; 06419 06426 inline bool setKey(const DataBlock& key, Direction dir = Bidir) 06427 { return setKey(key.data(),key.length(),dir); } 06428 06436 virtual bool initVector(const void* vect, unsigned int len, Direction dir = Bidir); 06437 06444 inline bool initVector(const DataBlock& vect, Direction dir = Bidir) 06445 { return initVector(vect.data(),vect.length(),dir); } 06446 06454 virtual bool encrypt(void* outData, unsigned int len, const void* inpData = 0) = 0; 06455 06461 inline bool encrypt(DataBlock& data) 06462 { return encrypt(data.data(),data.length()); } 06463 06471 virtual bool decrypt(void* outData, unsigned int len, const void* inpData = 0) = 0; 06472 06478 inline bool decrypt(DataBlock& data) 06479 { return decrypt(data.data(),data.length()); } 06480 06481 private: 06482 static const TokenDict s_directions[]; 06483 }; 06484 06490 class YATE_API Compressor : public String 06491 { 06492 YCLASS(Compressor,String) 06493 YNOCOPY(Compressor); // no automatic copies please 06494 public: 06500 inline Compressor(const char* format, const char* name = 0) 06501 : String(name), m_format(format) 06502 {} 06503 06507 virtual ~Compressor() 06508 {} 06509 06514 inline const String& format() const 06515 { return m_format; } 06516 06524 virtual bool init(bool comp = true, bool decomp = true, 06525 const NamedList& params = NamedList::empty()) 06526 { return true; } 06527 06532 virtual void finalize(bool comp) 06533 {} 06534 06543 virtual int compress(const void* buf, unsigned int len, DataBlock& dest); 06544 06553 virtual int decompress(const void* buf, unsigned int len, DataBlock& dest); 06554 06565 virtual int writeComp(const void* buf, unsigned int len, bool flush) = 0; 06566 06575 inline int writeComp(const DataBlock& data, bool flush) 06576 { return writeComp(data.data(),data.length(),flush); } 06577 06586 inline int writeComp(const String& data, bool flush) 06587 { return writeComp(data.c_str(),data.length(),flush); } 06588 06595 virtual int readComp(DataBlock& buf, bool flush) = 0; 06596 06605 virtual int writeDecomp(const void* buf, unsigned int len, bool flush) = 0; 06606 06614 inline int writeDecomp(const DataBlock& data, bool flush) 06615 { return writeDecomp(data.data(),data.length(),flush); } 06616 06624 inline int writeDecomp(const String& data, bool flush) 06625 { return writeDecomp(data.c_str(),data.length(),flush); } 06626 06633 virtual int readDecomp(DataBlock& buf, bool flush) = 0; 06634 06635 protected: 06636 String m_format; 06637 }; 06638 06644 class YATE_API SysUsage 06645 { 06646 public: 06650 enum Type { 06651 WallTime, 06652 UserTime, 06653 KernelTime 06654 }; 06655 06659 static void init(); 06660 06665 static u_int64_t startTime(); 06666 06672 static u_int64_t usecRunTime(Type type = WallTime); 06673 06679 static u_int64_t msecRunTime(Type type = WallTime); 06680 06686 static u_int32_t secRunTime(Type type = WallTime); 06687 06693 static double runTime(Type type = WallTime); 06694 06695 }; 06696 06697 }; // namespace TelEngine 06698 06699 #endif /* __YATECLASS_H */ 06700 06701 /* vi: set ts=8 sw=4 sts=4 noet: */