Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

misc.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2003 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 
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         // maybe move more global ?
00570         class NoSuchElementException { };
00571 
00576         class __EXPORT iterator {
00577                 friend class StringTokenizer;  // access our private constructors
00578         private:
00579                 const StringTokenizer *myTok; // my StringTokenizer
00580                 const char *start;      // start of current token
00581                 const char *tokEnd;     // end of current token (->nxDelimiter)
00582                 const char *endp;       // one before next token
00583                 char *token;            // allocated token, if requested
00584 
00585                 // for initialization of the itEnd iterator
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); // init first token.
00592                 }
00593 
00594         public:
00595                 iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
00596 
00597                 // see also: comment in implementation of operator++
00598                 virtual ~iterator() { if (token) *token='\0'; delete [] token; }
00599                 
00603                 // everything, but not responsible for the allocated token.
00604                 iterator(const iterator& i) :
00605                         myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
00606                         endp(i.endp),token(0) {}
00607 
00611                 // everything, but not responsible for the allocated token.
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                 // only compare the end-position. speed.
00651                 inline bool operator == (const iterator &other) const { 
00652                         return (endp == other.endp);
00653                 }
00654 
00659                 // only compare the end position. speed.
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 

Generated on Tue Jan 18 14:32:37 2005 for GNU CommonC++ by  doxygen 1.3.9.1