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-2001 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>
00053 #include <iostream>
00054 
00055 #define KEYDATA_INDEX_SIZE      97
00056 #define KEYDATA_PAGER_SIZE      512
00057 #define KEYDATA_PATH_SIZE       256
00058 
00059 #pragma pack(1)
00060 
00061 typedef struct _keyval
00062 {
00063         struct _keyval *next;
00064         char val[1];
00065 }       keyval_t;
00066 
00067 typedef struct _keysym
00068 {
00069         struct _keysym *next;
00070         struct _keyval *data;
00071         const char **list;
00072         short count;
00073         char sym[1];
00074 }       keysym_t;
00075 
00076 typedef struct
00077 {
00078         char *keyword;
00079         char *value;
00080 } KEYDEF;
00081 
00082 #pragma pack()
00083 
00084 #ifdef  __WIN32__
00085 class __EXPORT MemPager;
00086 class __EXPORT SharedMemPager;
00087 class __EXPORT Keydata;
00088 #endif
00089 
00105 class MemPager
00106 {
00107 private:
00108         unsigned int pagesize;
00109         unsigned int pages;
00110 
00111         struct _page
00112         {
00113                 struct _page *next;
00114                 int used;
00115         } *page;
00116 
00117 protected:
00127         virtual void *first(size_t size);
00128 
00136         virtual void *alloc(size_t size);
00137 
00147         char *first(char *str);
00148 
00158         char *alloc(char *str);
00159 
00169         MemPager(int pagesize = 4096);
00170 
00174         void purge(void);
00175 
00179         virtual ~MemPager();
00180 
00181 public:
00188         inline int getPages(void)
00189                 {return pages;};
00190 };
00191 
00200 class SharedMemPager : public MemPager, public Mutex
00201 {
00202 protected:
00208         SharedMemPager(int pg = 4096);
00209 
00213         void purge(void);
00214 
00221         void *first(size_t size);
00222 
00229         void *alloc(size_t size);
00230 };
00231 
00280 class Keydata : protected MemPager
00281 {
00282 private:
00283         static std::ifstream cfgFile;
00284         static char lastpath[KEYDATA_PATH_SIZE + 1];
00285         static int count, sequence;
00286 
00287         int link;
00288 
00289         keysym_t *keys[KEYDATA_INDEX_SIZE];
00290 
00297         unsigned getIndex(const char *sym);
00298 
00299 protected:
00300         keysym_t *getSymbol(const char *sym, bool create);
00301 
00313         void Load(const char *keypath, 
00314                   const char *environment = "CONFIG_KEYDATA");
00315 
00324         void Load(KEYDEF *pairs);
00325         
00326 public:
00330         Keydata();
00331 
00338         Keydata(const char *keypath, const char *environment="CONFIG_KEYDATA");
00339 
00345         virtual ~Keydata();
00346 
00354         void Unlink(void);
00355 
00364         int getCount(const char *sym);
00365 
00373         const char *getFirst(const char *sym);
00374 
00382         const char *getLast(const char *sym);
00383 
00392         int getIndex(char **data, int max);
00393 
00402         void setValue(const char *sym, const char *data);
00403 
00411         const char * const *getList(const char *sym);
00412 
00419         void clrValue(const char *sym);
00420 
00425         inline const char *operator[](const char *keyword)
00426                 {return getLast(keyword);};
00427 
00432         friend void endKeydata(void);   
00433 };
00434 
00478 class StringTokenizer {
00479 public:
00485         static const char * const SPACE;
00486 
00496         // maybe move more global ?
00497         class NoSuchElementException { };
00498 
00503         class iterator {
00504                 friend class StringTokenizer;  // access our private constructors
00505         private:
00506                 const StringTokenizer *myTok; // my StringTokenizer
00507                 const char *start;      // start of current token
00508                 const char *tokEnd;     // end of current token (->nxDelimiter)
00509                 const char *endp;       // one before next token
00510                 char *token;            // allocated token, if requested
00511 
00512                 // for initialization of the itEnd iterator
00513                 iterator(const StringTokenizer &tok, const char *end) 
00514                         : myTok(&tok),tokEnd(0),endp(end),token(0) {}
00515 
00516                 iterator(const StringTokenizer &tok)
00517                         : myTok(&tok),tokEnd(0),endp(myTok->str-1),token(0) {
00518                         ++(*this); // init first token.
00519                 }
00520         public:
00521                 iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
00522                 
00523                 // see also: comment in implementation of operator++
00524                 ~iterator() { if (token) *token='\0'; delete token; }
00525                 
00529                 // everything, but not responsible for the allocated token.
00530                 iterator(const iterator& i) :
00531                         myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
00532                         endp(i.endp),token(0) {}
00533                 
00537                 // everything, but not responsible for the allocated token.
00538                 iterator &operator = (const iterator &i) {
00539                         myTok = i.myTok; 
00540                         start = i.start; endp = i.endp; tokEnd = i.tokEnd;
00541                         token = 0;
00542                         return *this;
00543                 }
00544 
00548                 iterator &operator ++ () THROWS (NoSuchElementException);
00549 
00558                 const char*  operator *  () THROWS (NoSuchElementException);
00559                 
00566                 inline char nextDelimiter() const {
00567                         return (tokEnd) ? *tokEnd : '\0';
00568                 }
00569                 
00574                 // only compare the end-position. speed.
00575                 inline bool operator == (const iterator &other) const { 
00576                         return (endp == other.endp);
00577                 }
00578 
00583                 // only compare the end position. speed.
00584                 inline bool operator != (const iterator &other) const { 
00585                         return (endp != other.endp);
00586                 }
00587         };
00588 private:
00589         friend class StringTokenizer::iterator;
00590         const char *str;
00591         const char *delim;
00592         bool skipAll, trim;
00593         iterator itEnd;
00594 
00595 public:
00634         StringTokenizer (const char *str,
00635                          const char *delim,
00636                          bool skipAllDelim = false,
00637                          bool trim = false);
00638         
00648         StringTokenizer (const char *s);
00649 
00653         iterator begin() const { 
00654                 return iterator(*this); 
00655         }
00656         
00661         void setDelimiters (const char *d) {
00662                 delim = d;
00663         }
00664         
00669         iterator begin(const char *d) { 
00670                 delim = d;
00671                 return iterator(*this);
00672         }
00673 
00677         const iterator& end() const { return itEnd; }
00678 };
00679 
00680 #ifdef  __CCXX_NAMESPACE_H__
00681 #undef  __CCXX_NAMESPACE_H__
00682 #include <cc++/namespace.h>
00683 #endif
00684 
00685 #endif
00686 

Generated at Tue Oct 16 11:17:16 2001 for CommonC++ by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001