Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound 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_CONFIG_H_
00051 #include <cc++/config.h>
00052 #endif
00053 
00054 #ifndef CCXX_THREAD_H_
00055 #include <cc++/thread.h>
00056 #endif
00057 
00058 #include <fstream>
00059 #include <iostream>
00060 
00061 #define KEYDATA_INDEX_SIZE      97
00062 #define KEYDATA_PAGER_SIZE      512
00063 #define KEYDATA_PATH_SIZE       256
00064 
00065 #ifdef  CCXX_NAMESPACES
00066 namespace ost {
00067 #endif
00068 
00069 
00070 #ifdef WIN32
00071 class CCXX_CLASS_EXPORT MemPager;
00072 class CCXX_CLASS_EXPORT SharedMemPager;
00073 #endif
00074 
00090 class MemPager
00091 {
00092 private:
00093         unsigned int pagesize;
00094         unsigned int pages;
00095 
00096         struct _page
00097         {
00098                 struct _page *next;
00099                 int used;
00100         } *page;
00101 
00102 protected:
00112         virtual void* first(size_t size);
00113 
00121         virtual void* alloc(size_t size);
00122 
00132         char* first(char *str);
00133 
00143         char* alloc(char *str);
00144 
00154         MemPager(int pagesize = 4096);
00155 
00159         void purge(void);
00160 
00164         virtual ~MemPager();
00165 
00166 public:
00173         inline int getPages(void)
00174                 {return pages;};
00175 };
00176 
00185 class SharedMemPager : public MemPager, public Mutex
00186 {
00187 protected:
00193         SharedMemPager(int pagesize = 4096);
00194 
00198         void purge(void);
00199 
00206         void* first(size_t size);
00207 
00214         void* alloc(size_t size);
00215 };
00216 
00284 class Keydata : protected MemPager
00285 {
00286 public:
00287 #if defined(__GNUC__) || !defined(__hpux)
00288 #pragma pack(1)
00289 #endif
00290 
00291         struct Keyval
00292         {
00293                 Keyval *next;
00294                 char val[1];
00295         };
00296 
00297         struct Keysym
00298         {
00299                 Keysym *next;
00300                 Keyval *data;
00301                 const char **list;
00302                 short count;
00303                 char sym[1];
00304         };
00305 
00306         struct Define
00307         {
00308                 char *keyword;
00309                 char *value;
00310         };
00311 
00312 #if defined(__GNUC__) || !defined(__hpux)
00313 #pragma pack()
00314 #endif
00315 
00316 private:
00317         static std::ifstream *cfgFile;
00318         static char lastpath[KEYDATA_PATH_SIZE + 1];
00319         static int count, sequence;
00320 
00321         int link;
00322 
00323         Keysym *keys[KEYDATA_INDEX_SIZE];
00324 
00331         unsigned getIndex(const char *sym);
00332 
00333 protected:
00334         CCXX_MEMBER_EXPORT(Keysym*) getSymbol(const char *sym, bool create);
00335 
00348         CCXX_MEMBER_EXPORT(void) load(const char *keypath, 
00349                   const char *environment = "CONFIG_KEYDATA");
00350 
00366         CCXX_MEMBER_EXPORT(void) loadPrefix(const char *prefix,
00367                   const char *keypath, const char *environment = "CONFIG_KEYDATA");
00368 
00377         CCXX_MEMBER_EXPORT(void) load(Define *pairs);
00378         
00379 public:
00383         CCXX_MEMBER_EXPORT(CCXX_EMPTY) Keydata();
00384 
00393         CCXX_MEMBER_EXPORT(CCXX_EMPTY) Keydata(const char *keypath, const char *environment="CONFIG_KEYDATA");
00394 
00400         CCXX_MEMBER_EXPORT(virtual) ~Keydata();
00401 
00409         CCXX_MEMBER_EXPORT(void) unlink(void);
00410 
00419         CCXX_MEMBER_EXPORT(int) getCount(const char *sym);
00420 
00428         CCXX_MEMBER_EXPORT(const char*) getFirst(const char *sym);
00429 
00437         CCXX_MEMBER_EXPORT(const char*) getLast(const char *sym);
00438 
00447         CCXX_MEMBER_EXPORT(unsigned) getIndex(char **data, unsigned max);
00448 
00455         CCXX_MEMBER_EXPORT(unsigned) getCount(void);
00456 
00465         CCXX_MEMBER_EXPORT(void) setValue(const char *sym, const char *data);
00466 
00474         CCXX_MEMBER_EXPORT(const char * const*) getList(const char *sym);
00475 
00482         CCXX_MEMBER_EXPORT(void) clrValue(const char *sym);
00483 
00488         inline const char *operator[](const char *keyword)
00489                 {return getLast(keyword);};
00490 
00495         friend CCXX_EXPORT(void) endKeydata(void);      
00496 };
00497 
00541 class CCXX_CLASS_EXPORT StringTokenizer {
00542 public:
00548         static const char * const SPACE;
00549 
00559         // maybe move more global ?
00560         class NoSuchElementException { };
00561 
00566         class CCXX_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 Thu Sep 25 13:14:31 2003 for GNU CommonC++ by doxygen 1.3.3