00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef __CCXX_MISC_H__
00042 #define __CCXX_MISC_H__
00043
00044 #ifndef CCXX_CONFIG_H_
00045 #include <cc++/config.h>
00046 #endif
00047
00048 #ifndef __CCXX_THREAD_H__
00049 #include <cc++/thread.h>
00050 #endif
00051
00052 #include <fstream>
00053 #include <iostream>
00054
00055 #define KEYDATA_INDEX_SIZE 97
00056 #define KEYDATA_PAGER_SIZE 512
00057 #define KEYDATA_PATH_SIZE 256
00058
00059 #ifdef CCXX_NAMESPACES
00060 namespace ost {
00061 #endif
00062
00063 #if defined(__GNUC__) || !defined(__hpux)
00064 #pragma pack(1)
00065 #endif
00066
00067 typedef struct _keyval
00068 {
00069 struct _keyval *next;
00070 char val[1];
00071 } keyval_t;
00072
00073 typedef struct _keysym
00074 {
00075 struct _keysym *next;
00076 struct _keyval *data;
00077 const char **list;
00078 short count;
00079 char sym[1];
00080 } keysym_t;
00081
00082 typedef struct
00083 {
00084 char *keyword;
00085 char *value;
00086 } KEYDEF;
00087
00088 #if defined(__GNUC__) || !defined(__hpux)
00089 #pragma pack()
00090 #endif
00091
00092 #ifdef WIN32
00093 class CCXX_CLASS_EXPORT MemPager;
00094 class CCXX_CLASS_EXPORT SharedMemPager;
00095 #endif
00096
00112 class MemPager
00113 {
00114 private:
00115 unsigned int pagesize;
00116 unsigned int pages;
00117
00118 struct _page
00119 {
00120 struct _page *next;
00121 int used;
00122 } *page;
00123
00124 protected:
00134 virtual void* first(size_t size);
00135
00143 virtual void* alloc(size_t size);
00144
00154 char* first(char *str);
00155
00165 char* alloc(char *str);
00166
00176 MemPager(int pagesize = 4096);
00177
00181 void purge(void);
00182
00186 virtual ~MemPager();
00187
00188 public:
00195 inline int getPages(void)
00196 {return pages;};
00197 };
00198
00207 class SharedMemPager : public MemPager, public Mutex
00208 {
00209 protected:
00215 SharedMemPager(int pg = 4096);
00216
00220 void purge(void);
00221
00228 void* first(size_t size);
00229
00236 void* alloc(size_t size);
00237 };
00238
00306 class Keydata : protected MemPager
00307 {
00308 private:
00309 static std::ifstream cfgFile;
00310 static char lastpath[KEYDATA_PATH_SIZE + 1];
00311 static int count, sequence;
00312
00313 int link;
00314
00315 keysym_t *keys[KEYDATA_INDEX_SIZE];
00316
00323 unsigned getIndex(const char *sym);
00324
00325 protected:
00326 CCXX_MEMBER_EXPORT(keysym_t*) getSymbol(const char *sym, bool create);
00327
00339 CCXX_MEMBER_EXPORT(void) Load(const char *keypath,
00340 const char *environment = "CONFIG_KEYDATA");
00341
00350 CCXX_MEMBER_EXPORT(void) Load(KEYDEF *pairs);
00351
00352 public:
00356 CCXX_MEMBER_EXPORT(CCXX_EMPTY) Keydata();
00357
00364 CCXX_MEMBER_EXPORT(CCXX_EMPTY) Keydata(const char *keypath, const char *environment="CONFIG_KEYDATA");
00365
00371 CCXX_MEMBER_EXPORT(virtual) ~Keydata();
00372
00380 CCXX_MEMBER_EXPORT(void) Unlink(void);
00381
00390 CCXX_MEMBER_EXPORT(int) getCount(const char *sym);
00391
00399 CCXX_MEMBER_EXPORT(const char*) getFirst(const char *sym);
00400
00408 CCXX_MEMBER_EXPORT(const char*) getLast(const char *sym);
00409
00418 CCXX_MEMBER_EXPORT(int) getIndex(char **data, int max);
00419
00428 CCXX_MEMBER_EXPORT(void) setValue(const char *sym, const char *data);
00429
00437 CCXX_MEMBER_EXPORT(const char * const*) getList(const char *sym);
00438
00445 CCXX_MEMBER_EXPORT(void) clrValue(const char *sym);
00446
00451 inline const char *operator[](const char *keyword)
00452 {return getLast(keyword);};
00453
00458 friend CCXX_EXPORT(void) endKeydata(void);
00459 };
00460
00504 class CCXX_CLASS_EXPORT StringTokenizer {
00505 public:
00511 static const char * const SPACE;
00512
00522
00523 class NoSuchElementException { };
00524
00529 class CCXX_CLASS_EXPORT iterator {
00530 friend class StringTokenizer;
00531 private:
00532 const StringTokenizer *myTok;
00533 const char *start;
00534 const char *tokEnd;
00535 const char *endp;
00536 char *token;
00537
00538
00539 iterator(const StringTokenizer &tok, const char *end)
00540 : myTok(&tok),tokEnd(0),endp(end),token(0) {}
00541
00542 iterator(const StringTokenizer &tok)
00543 : myTok(&tok),tokEnd(0),endp(myTok->str-1),token(0) {
00544 ++(*this);
00545 }
00546 public:
00547 iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
00548
00549
00550 virtual ~iterator() { if (token) *token='\0'; delete token; }
00551
00555
00556 iterator(const iterator& i) :
00557 myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
00558 endp(i.endp),token(0) {}
00559
00563
00564 iterator &operator = (const iterator &i) {
00565 myTok = i.myTok;
00566 start = i.start; endp = i.endp; tokEnd = i.tokEnd;
00567 token = 0;
00568 return *this;
00569 }
00570
00574 iterator &operator ++ () THROWS (NoSuchElementException);
00575
00584 const char* operator * () THROWS (NoSuchElementException);
00585
00592 inline char nextDelimiter() const {
00593 return (tokEnd) ? *tokEnd : '\0';
00594 }
00595
00600
00601 inline bool operator == (const iterator &other) const {
00602 return (endp == other.endp);
00603 }
00604
00609
00610 inline bool operator != (const iterator &other) const {
00611 return (endp != other.endp);
00612 }
00613 };
00614 private:
00615 friend class StringTokenizer::iterator;
00616 const char *str;
00617 const char *delim;
00618 bool skipAll, trim;
00619 iterator itEnd;
00620
00621 public:
00660 StringTokenizer (const char *str,
00661 const char *delim,
00662 bool skipAllDelim = false,
00663 bool trim = false);
00664
00674 StringTokenizer (const char *s);
00675
00679 iterator begin() const {
00680 return iterator(*this);
00681 }
00682
00687 void setDelimiters (const char *d) {
00688 delim = d;
00689 }
00690
00695 iterator begin(const char *d) {
00696 delim = d;
00697 return iterator(*this);
00698 }
00699
00703 const iterator& end() const { return itEnd; }
00704 };
00705
00706 #ifdef CCXX_NAMESPACES
00707 };
00708 #endif
00709
00710 #endif
00711