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
00061 #if defined(PATH_MAX)
00062 #if PATH_MAX > 512
00063 #define KEYDATA_PATH_SIZE 512
00064 #else
00065 #define KEYDATA_PATH_SIZE PATH_MAX
00066 #endif
00067 #else
00068 #define KEYDATA_PATH_SIZE 256
00069 #endif
00070
00071 #ifdef CCXX_NAMESPACES
00072 namespace ost {
00073 #endif
00074
00090 class __EXPORT MemPager
00091 {
00092 private:
00093 friend class String;
00094
00095 size_t pagesize;
00096 unsigned int pages;
00097
00098 struct _page
00099 {
00100 struct _page *next;
00101 size_t used;
00102 } *page;
00103
00104 protected:
00114 virtual void* first(size_t size);
00115
00123 virtual void* alloc(size_t size);
00124
00134 char* first(char *str);
00135
00145 char* alloc(const char *str);
00146
00156 MemPager(size_t pagesize = 4096);
00157
00161 void purge(void);
00162
00166 virtual ~MemPager();
00167
00168 public:
00175 inline int getPages(void)
00176 {return pages;};
00177 };
00178
00187 class __EXPORT SharedMemPager : public MemPager, public Mutex
00188 {
00189 protected:
00196 SharedMemPager(size_t pagesize = 4096, const char *name = NULL);
00197
00201 void purge(void);
00202
00209 void* first(size_t size);
00210
00217 void* alloc(size_t size);
00218 };
00219
00287 class __EXPORT Keydata : protected MemPager
00288 {
00289 public:
00290 #if defined(__GNUC__) || !defined(__hpux)
00291 #pragma pack(1)
00292 #endif
00293
00294 struct Keyval
00295 {
00296 Keyval *next;
00297 char val[1];
00298 };
00299
00300 struct Keysym
00301 {
00302 Keysym *next;
00303 Keyval *data;
00304 const char **list;
00305 short count;
00306 char sym[1];
00307 };
00308
00309 struct Define
00310 {
00311 char *keyword;
00312 char *value;
00313 };
00314
00315 #if defined(__GNUC__) || !defined(__hpux)
00316 #pragma pack()
00317 #endif
00318
00319 private:
00320 static std::ifstream *cfgFile;
00321 static char lastpath[KEYDATA_PATH_SIZE + 1];
00322 static int count;
00323 static int sequence;
00324
00325 int link;
00326
00327 Keysym *keys[KEYDATA_INDEX_SIZE];
00328
00335 unsigned getIndex(const char *sym);
00336
00337 protected:
00338 Keysym* getSymbol(const char *sym, bool create);
00339
00352 void load(const char *keypath,
00353 const char *environment = "CONFIG_KEYDATA");
00354
00370 void loadPrefix(const char *prefix,
00371 const char *keypath, const char *environment = "CONFIG_KEYDATA");
00372
00381 void load(Define *pairs);
00382
00383 public:
00387 Keydata();
00388
00397 Keydata(const char *keypath, const char *environment="CONFIG_KEYDATA");
00398
00404 virtual ~Keydata();
00405
00413 void unlink(void);
00414
00423 int getCount(const char *sym);
00424
00432 const char* getFirst(const char *sym);
00433
00441 const char* getLast(const char *sym);
00442
00451 unsigned getIndex(char **data, unsigned max);
00452
00459 unsigned getCount(void);
00460
00469 void setValue(const char *sym, const char *data);
00470
00478 const char * const* getList(const char *sym);
00479
00486 void clrValue(const char *sym);
00487
00492 inline const char *operator[](const char *keyword)
00493 {return getLast(keyword);};
00494
00498 static void end(void);
00499
00504 friend inline void endKeydata(void)
00505 {Keydata::end();};
00506 };
00507
00551 class __EXPORT StringTokenizer {
00552 public:
00558 static const char * const SPACE;
00559
00569
00570 class NoSuchElementException { };
00571
00576 class __EXPORT iterator {
00577 friend class StringTokenizer;
00578 private:
00579 const StringTokenizer *myTok;
00580 const char *start;
00581 const char *tokEnd;
00582 const char *endp;
00583 char *token;
00584
00585
00586 iterator(const StringTokenizer &tok, const char *end)
00587 : myTok(&tok),tokEnd(0),endp(end),token(0) {}
00588
00589 iterator(const StringTokenizer &tok)
00590 : myTok(&tok),tokEnd(0),endp(myTok->str-1),token(0) {
00591 ++(*this);
00592 }
00593
00594 public:
00595 iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
00596
00597
00598 virtual ~iterator() { if (token) *token='\0'; delete [] token; }
00599
00603
00604 iterator(const iterator& i) :
00605 myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
00606 endp(i.endp),token(0) {}
00607
00611
00612 iterator &operator = (const iterator &i) {
00613 myTok = i.myTok;
00614 start = i.start; endp = i.endp; tokEnd = i.tokEnd;
00615 if ( token )
00616 delete [] token;
00617 token = 0;
00618 return *this;
00619 }
00620
00624 iterator &operator ++ () THROWS (NoSuchElementException);
00625
00634 const char* operator * () THROWS (NoSuchElementException);
00635
00642 inline char nextDelimiter() const {
00643 return (tokEnd) ? *tokEnd : '\0';
00644 }
00645
00650
00651 inline bool operator == (const iterator &other) const {
00652 return (endp == other.endp);
00653 }
00654
00659
00660 inline bool operator != (const iterator &other) const {
00661 return (endp != other.endp);
00662 }
00663 };
00664 private:
00665 friend class StringTokenizer::iterator;
00666 const char *str;
00667 const char *delim;
00668 bool skipAll, trim;
00669 iterator itEnd;
00670
00671 public:
00710 StringTokenizer (const char *str,
00711 const char *delim,
00712 bool skipAllDelim = false,
00713 bool trim = false);
00714
00724 StringTokenizer (const char *s);
00725
00729 iterator begin() const {
00730 return iterator(*this);
00731 }
00732
00737 void setDelimiters (const char *d) {
00738 delim = d;
00739 }
00740
00745 iterator begin(const char *d) {
00746 delim = d;
00747 return iterator(*this);
00748 }
00749
00753 const iterator& end() const { return itEnd; }
00754 };
00755
00756 #ifdef CCXX_NAMESPACES
00757 }
00758 #endif
00759
00760 #endif
00761