Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | 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 #define KEYDATA_PATH_SIZE       256
00061 
00062 #ifdef  CCXX_NAMESPACES
00063 namespace ost {
00064 #endif
00065 
00081 class __EXPORT MemPager
00082 {
00083 private:
00084         friend class String;
00085 
00086         unsigned int pagesize;
00087         unsigned int pages;
00088 
00089         struct _page
00090         {
00091                 struct _page *next;
00092                 int used;
00093         } *page;
00094 
00095 protected:
00105         virtual void* first(size_t size);
00106 
00114         virtual void* alloc(size_t size);
00115 
00125         char* first(char *str);
00126 
00136         char* alloc(const char *str);
00137 
00147         MemPager(int pagesize = 4096);
00148 
00152         void purge(void);
00153 
00157         virtual ~MemPager();
00158 
00159 public:
00166         inline int getPages(void)
00167                 {return pages;};
00168 };
00169 
00178 class __EXPORT SharedMemPager : public MemPager, public Mutex
00179 {
00180 protected:
00186         SharedMemPager(int pagesize = 4096, const char *name = NULL);
00187 
00191         void purge(void);
00192 
00199         void* first(size_t size);
00200 
00207         void* alloc(size_t size);
00208 };
00209 
00277 class __EXPORT Keydata : protected MemPager
00278 {
00279 public:
00280 #if defined(__GNUC__) || !defined(__hpux)
00281 #pragma pack(1)
00282 #endif
00283 
00284         struct Keyval
00285         {
00286                 Keyval *next;
00287                 char val[1];
00288         };
00289 
00290         struct Keysym
00291         {
00292                 Keysym *next;
00293                 Keyval *data;
00294                 const char **list;
00295                 short count;
00296                 char sym[1];
00297         };
00298 
00299         struct Define
00300         {
00301                 char *keyword;
00302                 char *value;
00303         };
00304 
00305 #if defined(__GNUC__) || !defined(__hpux)
00306 #pragma pack()
00307 #endif
00308 
00309 private:
00310         static std::ifstream *cfgFile;
00311         static char lastpath[KEYDATA_PATH_SIZE + 1];
00312         static int count;
00313         static int sequence;
00314 
00315         int link;
00316 
00317         Keysym *keys[KEYDATA_INDEX_SIZE];
00318 
00325         unsigned getIndex(const char *sym);
00326 
00327 protected:
00328         Keysym* getSymbol(const char *sym, bool create);
00329 
00342         void load(const char *keypath, 
00343                   const char *environment = "CONFIG_KEYDATA");
00344 
00360         void loadPrefix(const char *prefix,
00361                   const char *keypath, const char *environment = "CONFIG_KEYDATA");
00362 
00371         void load(Define *pairs);
00372         
00373 public:
00377         Keydata();
00378 
00387         Keydata(const char *keypath, const char *environment="CONFIG_KEYDATA");
00388 
00394         virtual ~Keydata();
00395 
00403         void unlink(void);
00404 
00413         int getCount(const char *sym);
00414 
00422         const char* getFirst(const char *sym);
00423 
00431         const char* getLast(const char *sym);
00432 
00441         unsigned getIndex(char **data, unsigned max);
00442 
00449         unsigned getCount(void);
00450 
00459         void setValue(const char *sym, const char *data);
00460 
00468         const char * const* getList(const char *sym);
00469 
00476         void clrValue(const char *sym);
00477 
00482         inline const char *operator[](const char *keyword)
00483                 {return getLast(keyword);};
00484 
00488         static void end(void);
00489 
00494         friend inline void endKeydata(void)
00495                 {Keydata::end();};
00496 };
00497 
00541 class __EXPORT StringTokenizer {
00542 public:
00548         static const char * const SPACE;
00549 
00559         // maybe move more global ?
00560         class NoSuchElementException { };
00561 
00566         class __EXPORT iterator {
00567                 friend class StringTokenizer;  // access our private constructors
00568         private:
00569                 const StringTokenizer *myTok; // my StringTokenizer
00570                 const char *start;      // start of current token
00571                 const char *tokEnd;     // end of current token (->nxDelimiter)
00572                 const char *endp;       // one before next token
00573                 char *token;            // allocated token, if requested
00574 
00575                 // for initialization of the itEnd iterator
00576                 iterator(const StringTokenizer &tok, const char *end) 
00577                         : myTok(&tok),tokEnd(0),endp(end),token(0) {}
00578 
00579                 iterator(const StringTokenizer &tok)
00580                         : myTok(&tok),tokEnd(0),endp(myTok->str-1),token(0) {
00581                         ++(*this); // init first token.
00582                 }
00583 
00584         public:
00585                 iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
00586 
00587                 // see also: comment in implementation of operator++
00588                 virtual ~iterator() { if (token) *token='\0'; delete [] token; }
00589                 
00593                 // everything, but not responsible for the allocated token.
00594                 iterator(const iterator& i) :
00595                         myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
00596                         endp(i.endp),token(0) {}
00597 
00601                 // everything, but not responsible for the allocated token.
00602                 iterator &operator = (const iterator &i) {
00603                         myTok = i.myTok; 
00604                         start = i.start; endp = i.endp; tokEnd = i.tokEnd;
00605                         if ( token )
00606                                 delete [] token;
00607                         token = 0;
00608                         return *this;
00609                 }
00610 
00614                 iterator &operator ++ () THROWS (NoSuchElementException);
00615 
00624                 const char*  operator *  () THROWS (NoSuchElementException);
00625                 
00632                 inline char nextDelimiter() const {
00633                         return (tokEnd) ? *tokEnd : '\0';
00634                 }
00635                 
00640                 // only compare the end-position. speed.
00641                 inline bool operator == (const iterator &other) const { 
00642                         return (endp == other.endp);
00643                 }
00644 
00649                 // only compare the end position. speed.
00650                 inline bool operator != (const iterator &other) const { 
00651                         return (endp != other.endp);
00652                 }
00653         };
00654 private:
00655         friend class StringTokenizer::iterator;
00656         const char *str;
00657         const char *delim;
00658         bool skipAll, trim;
00659         iterator itEnd;
00660 
00661 public:
00700         StringTokenizer (const char *str,
00701                          const char *delim,
00702                          bool skipAllDelim = false,
00703                          bool trim = false);
00704         
00714         StringTokenizer (const char *s);
00715 
00719         iterator begin() const { 
00720                 return iterator(*this);
00721         }
00722         
00727         void setDelimiters (const char *d) {
00728                 delim = d;
00729         }
00730         
00735         iterator begin(const char *d) { 
00736                 delim = d;
00737                 return iterator(*this);
00738         }
00739 
00743         const iterator& end() const { return itEnd; }
00744 };
00745 
00746 #ifdef  CCXX_NAMESPACES
00747 };
00748 #endif
00749 
00750 #endif
00751 

Generated on Fri Feb 27 11:37:10 2004 for GNU CommonC++ by doxygen 1.3.5