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
00047 #ifndef CCXX_MISC_H_
00048 #define CCXX_MISC_H_
00049
00050 #ifndef CCXX_CONFIG_H_
00051 #include <cc++/config.h>
00052 #endif
00053
00054 #ifndef CCXX_THREAD_H_
00055 #include <cc++/thread.h>
00056 #endif
00057
00058 #include <fstream>
00059 #include <iostream>
00060
00061 #define KEYDATA_INDEX_SIZE 97
00062 #define KEYDATA_PAGER_SIZE 512
00063 #define KEYDATA_PATH_SIZE 256
00064
00065 #ifdef CCXX_NAMESPACES
00066 namespace ost {
00067 #endif
00068
00069
00070 #ifdef WIN32
00071 class CCXX_CLASS_EXPORT MemPager;
00072 class CCXX_CLASS_EXPORT SharedMemPager;
00073 #endif
00074
00090 class MemPager
00091 {
00092 private:
00093 unsigned int pagesize;
00094 unsigned int pages;
00095
00096 struct _page
00097 {
00098 struct _page *next;
00099 int used;
00100 } *page;
00101
00102 protected:
00112 virtual void* first(size_t size);
00113
00121 virtual void* alloc(size_t size);
00122
00132 char* first(char *str);
00133
00143 char* alloc(char *str);
00144
00154 MemPager(int pagesize = 4096);
00155
00159 void purge(void);
00160
00164 virtual ~MemPager();
00165
00166 public:
00173 inline int getPages(void)
00174 {return pages;};
00175 };
00176
00185 class SharedMemPager : public MemPager, public Mutex
00186 {
00187 protected:
00193 SharedMemPager(int pagesize = 4096);
00194
00198 void purge(void);
00199
00206 void* first(size_t size);
00207
00214 void* alloc(size_t size);
00215 };
00216
00284 class Keydata : protected MemPager
00285 {
00286 public:
00287 #if defined(__GNUC__) || !defined(__hpux)
00288 #pragma pack(1)
00289 #endif
00290
00291 struct Keyval
00292 {
00293 Keyval *next;
00294 char val[1];
00295 };
00296
00297 struct Keysym
00298 {
00299 Keysym *next;
00300 Keyval *data;
00301 const char **list;
00302 short count;
00303 char sym[1];
00304 };
00305
00306 struct Define
00307 {
00308 char *keyword;
00309 char *value;
00310 };
00311
00312 #if defined(__GNUC__) || !defined(__hpux)
00313 #pragma pack()
00314 #endif
00315
00316 private:
00317 static std::ifstream cfgFile;
00318 static char lastpath[KEYDATA_PATH_SIZE + 1];
00319 static int count, sequence;
00320
00321 int link;
00322
00323 Keysym *keys[KEYDATA_INDEX_SIZE];
00324
00331 unsigned getIndex(const char *sym);
00332
00333 protected:
00334 CCXX_MEMBER_EXPORT(Keysym*) getSymbol(const char *sym, bool create);
00335
00348 CCXX_MEMBER_EXPORT(void) load(const char *keypath,
00349 const char *environment = "CONFIG_KEYDATA");
00350
00366 CCXX_MEMBER_EXPORT(void) loadPrefix(const char *prefix,
00367 const char *keypath, const char *environment = "CONFIG_KEYDATA");
00368
00377 CCXX_MEMBER_EXPORT(void) load(Define *pairs);
00378
00379 public:
00383 CCXX_MEMBER_EXPORT(CCXX_EMPTY) Keydata();
00384
00393 CCXX_MEMBER_EXPORT(CCXX_EMPTY) Keydata(const char *keypath, const char *environment="CONFIG_KEYDATA");
00394
00400 CCXX_MEMBER_EXPORT(virtual) ~Keydata();
00401
00409 CCXX_MEMBER_EXPORT(void) unlink(void);
00410
00419 CCXX_MEMBER_EXPORT(int) getCount(const char *sym);
00420
00428 CCXX_MEMBER_EXPORT(const char*) getFirst(const char *sym);
00429
00437 CCXX_MEMBER_EXPORT(const char*) getLast(const char *sym);
00438
00447 CCXX_MEMBER_EXPORT(unsigned) getIndex(char **data, int max);
00448
00455 CCXX_MEMBER_EXPORT(unsigned) getCount(void);
00456
00465 CCXX_MEMBER_EXPORT(void) setValue(const char *sym, const char *data);
00466
00474 CCXX_MEMBER_EXPORT(const char * const*) getList(const char *sym);
00475
00482 CCXX_MEMBER_EXPORT(void) clrValue(const char *sym);
00483
00488 inline const char *operator[](const char *keyword)
00489 {return getLast(keyword);};
00490
00495 friend CCXX_EXPORT(void) endKeydata(void);
00496 };
00497
00541 class CCXX_CLASS_EXPORT StringTokenizer {
00542 public:
00548 static const char * const SPACE;
00549
00559
00560 class NoSuchElementException { };
00561
00566 class CCXX_CLASS_EXPORT iterator {
00567 friend class StringTokenizer;
00568 private:
00569 const StringTokenizer *myTok;
00570 const char *start;
00571 const char *tokEnd;
00572 const char *endp;
00573 char *token;
00574
00575
00576 iterator(const StringTokenizer &tok, const char *end)
00577 : myTok(&tok),tokEnd(0),endp(end),token(0) {}
00578
00579 iterator(const StringTokenizer &tok)
00580 : myTok(&tok),tokEnd(0),endp(myTok->str-1),token(0) {
00581 ++(*this);
00582 }
00583
00584 public:
00585 iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
00586
00587
00588 virtual ~iterator() { if (token) *token='\0'; delete [] token; }
00589
00593
00594 iterator(const iterator& i) :
00595 myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
00596 endp(i.endp),token(0) {}
00597
00601
00602 iterator &operator = (const iterator &i) {
00603 myTok = i.myTok;
00604 start = i.start; endp = i.endp; tokEnd = i.tokEnd;
00605 token = 0;
00606 return *this;
00607 }
00608
00612 iterator &operator ++ () THROWS (NoSuchElementException);
00613
00622 const char* operator * () THROWS (NoSuchElementException);
00623
00630 inline char nextDelimiter() const {
00631 return (tokEnd) ? *tokEnd : '\0';
00632 }
00633
00638
00639 inline bool operator == (const iterator &other) const {
00640 return (endp == other.endp);
00641 }
00642
00647
00648 inline bool operator != (const iterator &other) const {
00649 return (endp != other.endp);
00650 }
00651 };
00652 private:
00653 friend class StringTokenizer::iterator;
00654 const char *str;
00655 const char *delim;
00656 bool skipAll, trim;
00657 iterator itEnd;
00658
00659 public:
00698 StringTokenizer (const char *str,
00699 const char *delim,
00700 bool skipAllDelim = false,
00701 bool trim = false);
00702
00712 StringTokenizer (const char *s);
00713
00717 iterator begin() const {
00718 return iterator(*this);
00719 }
00720
00725 void setDelimiters (const char *d) {
00726 delim = d;
00727 }
00728
00733 iterator begin(const char *d) {
00734 delim = d;
00735 return iterator(*this);
00736 }
00737
00741 const iterator& end() const { return itEnd; }
00742 };
00743
00744 #ifdef CCXX_NAMESPACES
00745 };
00746 #endif
00747
00748 #endif
00749