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_MISSING_H_
00051 #include <cc++/missing.h>
00052 #endif
00053
00054 #ifndef CCXX_THREAD_H_
00055 #include <cc++/thread.h>
00056 #endif
00057
00058 #define KEYDATA_INDEX_SIZE 97
00059 #define KEYDATA_PAGER_SIZE 512
00060 #define KEYDATA_PATH_SIZE 256
00061
00062 #ifdef CCXX_NAMESPACES
00063 namespace ost {
00064 #endif
00065
00081 class __EXPORT MemPager
00082 {
00083 private:
00084 friend class String;
00085
00086 unsigned int pagesize;
00087 unsigned int pages;
00088
00089 struct _page
00090 {
00091 struct _page *next;
00092 int used;
00093 } *page;
00094
00095 protected:
00105 virtual void* first(size_t size);
00106
00114 virtual void* alloc(size_t size);
00115
00125 char* first(char *str);
00126
00136 char* alloc(const char *str);
00137
00147 MemPager(int pagesize = 4096);
00148
00152 void purge(void);
00153
00157 virtual ~MemPager();
00158
00159 public:
00166 inline int getPages(void)
00167 {return pages;};
00168 };
00169
00178 class __EXPORT SharedMemPager : public MemPager, public Mutex
00179 {
00180 protected:
00186 SharedMemPager(int pagesize = 4096, const char *name = NULL);
00187
00191 void purge(void);
00192
00199 void* first(size_t size);
00200
00207 void* alloc(size_t size);
00208 };
00209
00277 class __EXPORT Keydata : protected MemPager
00278 {
00279 public:
00280 #if defined(__GNUC__) || !defined(__hpux)
00281 #pragma pack(1)
00282 #endif
00283
00284 struct Keyval
00285 {
00286 Keyval *next;
00287 char val[1];
00288 };
00289
00290 struct Keysym
00291 {
00292 Keysym *next;
00293 Keyval *data;
00294 const char **list;
00295 short count;
00296 char sym[1];
00297 };
00298
00299 struct Define
00300 {
00301 char *keyword;
00302 char *value;
00303 };
00304
00305 #if defined(__GNUC__) || !defined(__hpux)
00306 #pragma pack()
00307 #endif
00308
00309 private:
00310 static std::ifstream *cfgFile;
00311 static char lastpath[KEYDATA_PATH_SIZE + 1];
00312 static int count;
00313 static int sequence;
00314
00315 int link;
00316
00317 Keysym *keys[KEYDATA_INDEX_SIZE];
00318
00325 unsigned getIndex(const char *sym);
00326
00327 protected:
00328 Keysym* getSymbol(const char *sym, bool create);
00329
00342 void load(const char *keypath,
00343 const char *environment = "CONFIG_KEYDATA");
00344
00360 void loadPrefix(const char *prefix,
00361 const char *keypath, const char *environment = "CONFIG_KEYDATA");
00362
00371 void load(Define *pairs);
00372
00373 public:
00377 Keydata();
00378
00387 Keydata(const char *keypath, const char *environment="CONFIG_KEYDATA");
00388
00394 virtual ~Keydata();
00395
00403 void unlink(void);
00404
00413 int getCount(const char *sym);
00414
00422 const char* getFirst(const char *sym);
00423
00431 const char* getLast(const char *sym);
00432
00441 unsigned getIndex(char **data, unsigned max);
00442
00449 unsigned getCount(void);
00450
00459 void setValue(const char *sym, const char *data);
00460
00468 const char * const* getList(const char *sym);
00469
00476 void clrValue(const char *sym);
00477
00482 inline const char *operator[](const char *keyword)
00483 {return getLast(keyword);};
00484
00488 static void end(void);
00489
00494 friend inline void endKeydata(void)
00495 {Keydata::end();};
00496 };
00497
00541 class __EXPORT StringTokenizer {
00542 public:
00548 static const char * const SPACE;
00549
00559
00560 class NoSuchElementException { };
00561
00566 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 if ( token )
00606 delete [] token;
00607 token = 0;
00608 return *this;
00609 }
00610
00614 iterator &operator ++ () THROWS (NoSuchElementException);
00615
00624 const char* operator * () THROWS (NoSuchElementException);
00625
00632 inline char nextDelimiter() const {
00633 return (tokEnd) ? *tokEnd : '\0';
00634 }
00635
00640
00641 inline bool operator == (const iterator &other) const {
00642 return (endp == other.endp);
00643 }
00644
00649
00650 inline bool operator != (const iterator &other) const {
00651 return (endp != other.endp);
00652 }
00653 };
00654 private:
00655 friend class StringTokenizer::iterator;
00656 const char *str;
00657 const char *delim;
00658 bool skipAll, trim;
00659 iterator itEnd;
00660
00661 public:
00700 StringTokenizer (const char *str,
00701 const char *delim,
00702 bool skipAllDelim = false,
00703 bool trim = false);
00704
00714 StringTokenizer (const char *s);
00715
00719 iterator begin() const {
00720 return iterator(*this);
00721 }
00722
00727 void setDelimiters (const char *d) {
00728 delim = d;
00729 }
00730
00735 iterator begin(const char *d) {
00736 delim = d;
00737 return iterator(*this);
00738 }
00739
00743 const iterator& end() const { return itEnd; }
00744 };
00745
00746 #ifdef CCXX_NAMESPACES
00747 };
00748 #endif
00749
00750 #endif
00751