Google

Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

misc.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2000 Open Source Telecom Corporation.
00002 //  
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 // 
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 // 
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software 
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 // 
00017 // As a special exception to the GNU General Public License, permission is 
00018 // granted for additional uses of the text contained in its release 
00019 // of Common C++.
00020 // 
00021 // The exception is that, if you link the Common C++ library with other
00022 // files to produce an executable, this does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public License.
00024 // Your use of that executable is in no way restricted on account of
00025 // linking the Common C++ library code into it.
00026 // 
00027 // This exception does not however invalidate any other reasons why
00028 // the executable file might be covered by the GNU General Public License.
00029 // 
00030 // This exception applies only to the code released under the 
00031 // name Common C++.  If you copy code from other releases into a copy of
00032 // Common C++, as the General Public License permits, the exception does
00033 // not apply to the code that you add in this way.  To avoid misleading
00034 // anyone as to the status of such modified files, you must delete
00035 // this exception notice from them.
00036 // 
00037 // If you write modifications of your own for Common C++, it is your choice
00038 // whether to permit this exception to apply to your modifications.
00039 // If you do not wish that, delete this exception notice.  
00040 
00041 #ifndef __CCXX_MISC_H__
00042 #define __CCXX_MISC_H__
00043 
00044 #ifndef __CCXX_THREAD_H__
00045 #include <cc++/thread.h>
00046 #else
00047 #ifdef  __CCXX_NAMESPACE_H__
00048 #include <cc++/macros.h>
00049 #endif
00050 #endif
00051 
00052 #include <fstream.h>
00053 
00054 #define KEYDATA_INDEX_SIZE      97
00055 #define KEYDATA_PAGER_SIZE      512
00056 #define KEYDATA_PATH_SIZE       256
00057 
00058 #pragma pack(1)
00059 
00060 typedef struct _keyval
00061 {
00062         struct _keyval *next;
00063         char val[1];
00064 }       keyval_t;
00065 
00066 typedef struct _keysym
00067 {
00068         struct _keysym *next;
00069         struct _keyval *data;
00070         const char **list;
00071         short count;
00072         char sym[1];
00073 }       keysym_t;
00074 
00075 typedef struct
00076 {
00077         char *keyword;
00078         char *value;
00079 } KEYDEF;
00080 
00081 #pragma pack()
00082 
00083 #ifdef  __WIN32__
00084 class __EXPORT MemPager;
00085 class __EXPORT SharedMemPager;
00086 class __EXPORT Keydata;
00087 #endif
00088 
00104 class MemPager
00105 {
00106 private:
00107         unsigned int pagesize;
00108         unsigned int pages;
00109 
00110         struct _page
00111         {
00112                 struct _page *next;
00113                 int used;
00114         } *page;
00115 
00116 protected:
00126         virtual void *first(size_t size);
00127 
00135         virtual void *alloc(size_t size);
00136 
00146         char *first(char *str);
00147 
00157         char *alloc(char *str);
00158 
00168         MemPager(int pagesize = 4096);
00169 
00173         void purge(void);
00174 
00178         virtual ~MemPager();
00179 
00180 public:
00187         inline int getPages(void)
00188                 {return pages;};
00189 };
00190 
00199 class SharedMemPager : public MemPager, public Mutex
00200 {
00201 protected:
00207         SharedMemPager(int pg = 4096);
00208 
00212         void purge(void);
00213 
00220         void *first(size_t size);
00221 
00228         void *alloc(size_t size);
00229 };
00230 
00279 class Keydata : protected MemPager
00280 {
00281 private:
00282         static ifstream cfgFile;
00283         static char lastpath[KEYDATA_PATH_SIZE + 1];
00284         static int count, sequence;
00285 
00286         int link;
00287 
00288         keysym_t *keys[KEYDATA_INDEX_SIZE];
00289 
00296         unsigned getIndex(const char *sym);
00297 
00298 protected:
00299         keysym_t *getSymbol(const char *sym, bool create);
00300 
00312         void Load(const char *keypath, 
00313                   const char *environment = "CONFIG_KEYDATA");
00314 
00323         void Load(KEYDEF *pairs);
00324         
00325 public:
00329         Keydata();
00330 
00337         Keydata(const char *keypath, const char *environment="CONFIG_KEYDATA");
00338 
00344         virtual ~Keydata();
00345 
00353         void Unlink(void);
00354 
00363         int getCount(const char *sym);
00364 
00372         const char *getFirst(const char *sym);
00373 
00381         const char *getLast(const char *sym);
00382 
00391         int getIndex(char **data, int max);
00392 
00401         void setValue(const char *sym, const char *data);
00402 
00410         const char * const *getList(const char *sym);
00411 
00418         void clrValue(const char *sym);
00419 
00424         inline const char *operator[](const char *keyword)
00425                 {return getLast(keyword);};
00426 
00431         friend void endKeydata(void);   
00432 };
00433 
00477 class StringTokenizer {
00478 public:
00484         static const char * const SPACE;
00485 
00495         // maybe move more global ?
00496         class NoSuchElementException { };
00497 
00502         class iterator {
00503                 friend StringTokenizer;  // access our private constructors
00504         private:
00505                 const StringTokenizer *myTok; // my StringTokenizer
00506                 const char *start;      // start of current token
00507                 const char *tokEnd;     // end of current token (->nxDelimiter)
00508                 const char *endp;       // one before next token
00509                 char *token;            // allocated token, if requested
00510 
00511                 // for initialization of the itEnd iterator
00512                 iterator(const StringTokenizer &tok, const char *end) 
00513                         : myTok(&tok),tokEnd(0),endp(end),token(0) {}
00514 
00515                 iterator(const StringTokenizer &tok)
00516                         : myTok(&tok),tokEnd(0),endp(myTok->str-1),token(0) {
00517                         ++(*this); // init first token.
00518                 }
00519         public:
00520                 iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
00521                 
00522                 // see also: comment in implementation of operator++
00523                 ~iterator() { if (token) *token='\0'; delete token; }
00524                 
00528                 // everything, but not responsible for the allocated token.
00529                 iterator(const iterator& i) :
00530                         myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
00531                         endp(i.endp),token(0) {}
00532                 
00536                 // everything, but not responsible for the allocated token.
00537                 iterator &operator = (const iterator &i) {
00538                         myTok = i.myTok; 
00539                         start = i.start; endp = i.endp; tokEnd = i.tokEnd;
00540                         token = 0;
00541                         return *this;
00542                 }
00543 
00547                 iterator &operator ++ () THROWS (NoSuchElementException);
00548 
00557                 const char*  operator *  () THROWS (NoSuchElementException);
00558                 
00565                 inline char nextDelimiter() const {
00566                         return (tokEnd) ? *tokEnd : '\0';
00567                 }
00568                 
00573                 // only compare the end-position. speed.
00574                 inline bool operator == (const iterator &other) const { 
00575                         return (endp == other.endp);
00576                 }
00577 
00582                 // only compare the end position. speed.
00583                 inline bool operator != (const iterator &other) const { 
00584                         return (endp != other.endp);
00585                 }
00586         };
00587 private:
00588         friend iterator;
00589         const char *str;
00590         const char *delim;
00591         bool skipAll, trim;
00592         iterator itEnd;
00593 
00594 public:
00633         StringTokenizer (const char *str,
00634                          const char *delim,
00635                          bool skipAllDelim = false,
00636                          bool trim = false);
00637         
00647         StringTokenizer (const char *s);
00648 
00652         iterator begin() const { 
00653                 return iterator(*this); 
00654         }
00655         
00660         void setDelimiters (const char *d) {
00661                 delim = d;
00662         }
00663         
00668         iterator begin(const char *d) { 
00669                 delim = d;
00670                 return iterator(*this);
00671         }
00672 
00676         const iterator& end() const { return itEnd; }
00677 };
00678 
00679 #ifdef  __CCXX_NAMESPACE_H__
00680 #undef  __CCXX_NAMESPACE_H__
00681 #include <cc++/namespace.h>
00682 #endif
00683 
00684 #endif
00685 

Generated at Fri Mar 23 10:47:54 2001 for CommonC++ by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000