Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   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_CONFIG_H_
00045 #include <cc++/config.h>
00046 #endif
00047 
00048 #ifndef __CCXX_THREAD_H__
00049 #include <cc++/thread.h>
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 #ifdef  CCXX_NAMESPACES
00060 namespace ost {
00061 #endif
00062 
00063 #if defined(__GNUC__) || !defined(__hpux)
00064 #pragma pack(1)
00065 #endif
00066 
00067 typedef struct _keyval
00068 {
00069         struct _keyval *next;
00070         char val[1];
00071 }       keyval_t;
00072 
00073 typedef struct _keysym
00074 {
00075         struct _keysym *next;
00076         struct _keyval *data;
00077         const char **list;
00078         short count;
00079         char sym[1];
00080 }       keysym_t;
00081 
00082 typedef struct
00083 {
00084         char *keyword;
00085         char *value;
00086 } KEYDEF;
00087 
00088 #if defined(__GNUC__) || !defined(__hpux)
00089 #pragma pack()
00090 #endif
00091 
00092 #ifdef WIN32
00093 class CCXX_CLASS_EXPORT MemPager;
00094 class CCXX_CLASS_EXPORT SharedMemPager;
00095 #endif
00096 
00112 class MemPager
00113 {
00114 private:
00115         unsigned int pagesize;
00116         unsigned int pages;
00117 
00118         struct _page
00119         {
00120                 struct _page *next;
00121                 int used;
00122         } *page;
00123 
00124 protected:
00134         virtual void* first(size_t size);
00135 
00143         virtual void* alloc(size_t size);
00144 
00154         char* first(char *str);
00155 
00165         char* alloc(char *str);
00166 
00176         MemPager(int pagesize = 4096);
00177 
00181         void purge(void);
00182 
00186         virtual ~MemPager();
00187 
00188 public:
00195         inline int getPages(void)
00196                 {return pages;};
00197 };
00198 
00207 class SharedMemPager : public MemPager, public Mutex
00208 {
00209 protected:
00215         SharedMemPager(int pg = 4096);
00216 
00220         void purge(void);
00221 
00228         void* first(size_t size);
00229 
00236         void* alloc(size_t size);
00237 };
00238 
00306 class Keydata : protected MemPager
00307 {
00308 private:
00309         static std::ifstream cfgFile;
00310         static char lastpath[KEYDATA_PATH_SIZE + 1];
00311         static int count, sequence;
00312 
00313         int link;
00314 
00315         keysym_t *keys[KEYDATA_INDEX_SIZE];
00316 
00323         unsigned getIndex(const char *sym);
00324 
00325 protected:
00326         CCXX_MEMBER_EXPORT(keysym_t*) getSymbol(const char *sym, bool create);
00327 
00339         CCXX_MEMBER_EXPORT(void) Load(const char *keypath, 
00340                   const char *environment = "CONFIG_KEYDATA");
00341 
00350         CCXX_MEMBER_EXPORT(void) Load(KEYDEF *pairs);
00351         
00352 public:
00356         CCXX_MEMBER_EXPORT(CCXX_EMPTY) Keydata();
00357 
00364         CCXX_MEMBER_EXPORT(CCXX_EMPTY) Keydata(const char *keypath, const char *environment="CONFIG_KEYDATA");
00365 
00371         CCXX_MEMBER_EXPORT(virtual) ~Keydata();
00372 
00380         CCXX_MEMBER_EXPORT(void) Unlink(void);
00381 
00390         CCXX_MEMBER_EXPORT(int) getCount(const char *sym);
00391 
00399         CCXX_MEMBER_EXPORT(const char*) getFirst(const char *sym);
00400 
00408         CCXX_MEMBER_EXPORT(const char*) getLast(const char *sym);
00409 
00418         CCXX_MEMBER_EXPORT(int) getIndex(char **data, int max);
00419 
00428         CCXX_MEMBER_EXPORT(void) setValue(const char *sym, const char *data);
00429 
00437         CCXX_MEMBER_EXPORT(const char * const*) getList(const char *sym);
00438 
00445         CCXX_MEMBER_EXPORT(void) clrValue(const char *sym);
00446 
00451         inline const char *operator[](const char *keyword)
00452                 {return getLast(keyword);};
00453 
00458         friend CCXX_EXPORT(void) endKeydata(void);      
00459 };
00460 
00504 class CCXX_CLASS_EXPORT StringTokenizer {
00505 public:
00511         static const char * const SPACE;
00512 
00522         // maybe move more global ?
00523         class NoSuchElementException { };
00524 
00529         class CCXX_CLASS_EXPORT iterator {
00530                 friend class StringTokenizer;  // access our private constructors
00531         private:
00532                 const StringTokenizer *myTok; // my StringTokenizer
00533                 const char *start;      // start of current token
00534                 const char *tokEnd;     // end of current token (->nxDelimiter)
00535                 const char *endp;       // one before next token
00536                 char *token;            // allocated token, if requested
00537 
00538                 // for initialization of the itEnd iterator
00539                 iterator(const StringTokenizer &tok, const char *end) 
00540                         : myTok(&tok),tokEnd(0),endp(end),token(0) {}
00541 
00542                 iterator(const StringTokenizer &tok)
00543                         : myTok(&tok),tokEnd(0),endp(myTok->str-1),token(0) {
00544                         ++(*this); // init first token.
00545                 }
00546         public:
00547                 iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
00548                 
00549                 // see also: comment in implementation of operator++
00550                 virtual ~iterator() { if (token) *token='\0'; delete token; }
00551                 
00555                 // everything, but not responsible for the allocated token.
00556                 iterator(const iterator& i) :
00557                         myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
00558                         endp(i.endp),token(0) {}
00559                 
00563                 // everything, but not responsible for the allocated token.
00564                 iterator &operator = (const iterator &i) {
00565                         myTok = i.myTok; 
00566                         start = i.start; endp = i.endp; tokEnd = i.tokEnd;
00567                         token = 0;
00568                         return *this;
00569                 }
00570 
00574                 iterator &operator ++ () THROWS (NoSuchElementException);
00575 
00584                 const char*  operator *  () THROWS (NoSuchElementException);
00585                 
00592                 inline char nextDelimiter() const {
00593                         return (tokEnd) ? *tokEnd : '\0';
00594                 }
00595                 
00600                 // only compare the end-position. speed.
00601                 inline bool operator == (const iterator &other) const { 
00602                         return (endp == other.endp);
00603                 }
00604 
00609                 // only compare the end position. speed.
00610                 inline bool operator != (const iterator &other) const { 
00611                         return (endp != other.endp);
00612                 }
00613         };
00614 private:
00615         friend class StringTokenizer::iterator;
00616         const char *str;
00617         const char *delim;
00618         bool skipAll, trim;
00619         iterator itEnd;
00620 
00621 public:
00660         StringTokenizer (const char *str,
00661                          const char *delim,
00662                          bool skipAllDelim = false,
00663                          bool trim = false);
00664         
00674         StringTokenizer (const char *s);
00675 
00679         iterator begin() const { 
00680                 return iterator(*this); 
00681         }
00682         
00687         void setDelimiters (const char *d) {
00688                 delim = d;
00689         }
00690         
00695         iterator begin(const char *d) { 
00696                 delim = d;
00697                 return iterator(*this);
00698         }
00699 
00703         const iterator& end() const { return itEnd; }
00704 };
00705 
00706 #ifdef  CCXX_NAMESPACES
00707 };
00708 #endif
00709 
00710 #endif
00711 

Generated at Wed Dec 5 07:05:25 2001 for CommonC++ by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001