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  __NAMESPACES__
00060 namespace ost {
00061 #endif
00062 
00063 #pragma pack(1)
00064 
00065 typedef struct _keyval
00066 {
00067         struct _keyval *next;
00068         char val[1];
00069 }       keyval_t;
00070 
00071 typedef struct _keysym
00072 {
00073         struct _keysym *next;
00074         struct _keyval *data;
00075         const char **list;
00076         short count;
00077         char sym[1];
00078 }       keysym_t;
00079 
00080 typedef struct
00081 {
00082         char *keyword;
00083         char *value;
00084 } KEYDEF;
00085 
00086 #pragma pack()
00087 
00088 #ifdef WIN32
00089 class CCXX_CLASS_EXPORT MemPager;
00090 class CCXX_CLASS_EXPORT SharedMemPager;
00091 #endif
00092 
00108 class MemPager
00109 {
00110 private:
00111         unsigned int pagesize;
00112         unsigned int pages;
00113 
00114         struct _page
00115         {
00116                 struct _page *next;
00117                 int used;
00118         } *page;
00119 
00120 protected:
00130         virtual void* first(size_t size);
00131 
00139         virtual void* alloc(size_t size);
00140 
00150         char* first(char *str);
00151 
00161         char* alloc(char *str);
00162 
00172         MemPager(int pagesize = 4096);
00173 
00177         void purge(void);
00178 
00182         virtual ~MemPager();
00183 
00184 public:
00191         inline int getPages(void)
00192                 {return pages;};
00193 };
00194 
00203 class SharedMemPager : public MemPager, public Mutex
00204 {
00205 protected:
00211         SharedMemPager(int pg = 4096);
00212 
00216         void purge(void);
00217 
00224         void* first(size_t size);
00225 
00232         void* alloc(size_t size);
00233 };
00234 
00302 class Keydata : protected MemPager
00303 {
00304 private:
00305         static std::ifstream cfgFile;
00306         static char lastpath[KEYDATA_PATH_SIZE + 1];
00307         static int count, sequence;
00308 
00309         int link;
00310 
00311         keysym_t *keys[KEYDATA_INDEX_SIZE];
00312 
00319         unsigned getIndex(const char *sym);
00320 
00321 protected:
00322         CCXX_MEMBER_EXPORT(keysym_t*) getSymbol(const char *sym, bool create);
00323 
00335         CCXX_MEMBER_EXPORT(void) Load(const char *keypath, 
00336                   const char *environment = "CONFIG_KEYDATA");
00337 
00346         CCXX_MEMBER_EXPORT(void) Load(KEYDEF *pairs);
00347         
00348 public:
00352         CCXX_MEMBER_EXPORT(CCXX_EMPTY) Keydata();
00353 
00360         CCXX_MEMBER_EXPORT(CCXX_EMPTY) Keydata(const char *keypath, const char *environment="CONFIG_KEYDATA");
00361 
00367         CCXX_MEMBER_EXPORT(virtual) ~Keydata();
00368 
00376         CCXX_MEMBER_EXPORT(void) Unlink(void);
00377 
00386         CCXX_MEMBER_EXPORT(int) getCount(const char *sym);
00387 
00395         CCXX_MEMBER_EXPORT(const char*) getFirst(const char *sym);
00396 
00404         CCXX_MEMBER_EXPORT(const char*) getLast(const char *sym);
00405 
00414         CCXX_MEMBER_EXPORT(int) getIndex(char **data, int max);
00415 
00424         CCXX_MEMBER_EXPORT(void) setValue(const char *sym, const char *data);
00425 
00433         CCXX_MEMBER_EXPORT(const char * const*) getList(const char *sym);
00434 
00441         CCXX_MEMBER_EXPORT(void) clrValue(const char *sym);
00442 
00447         inline const char *operator[](const char *keyword)
00448                 {return getLast(keyword);};
00449 
00454         friend CCXX_EXPORT(void) endKeydata(void);      
00455 };
00456 
00500 class CCXX_CLASS_EXPORT StringTokenizer {
00501 public:
00507         static const char * const SPACE;
00508 
00518         // maybe move more global ?
00519         class NoSuchElementException { };
00520 
00525         class CCXX_CLASS_EXPORT iterator {
00526                 friend class StringTokenizer;  // access our private constructors
00527         private:
00528                 const StringTokenizer *myTok; // my StringTokenizer
00529                 const char *start;      // start of current token
00530                 const char *tokEnd;     // end of current token (->nxDelimiter)
00531                 const char *endp;       // one before next token
00532                 char *token;            // allocated token, if requested
00533 
00534                 // for initialization of the itEnd iterator
00535                 iterator(const StringTokenizer &tok, const char *end) 
00536                         : myTok(&tok),tokEnd(0),endp(end),token(0) {}
00537 
00538                 iterator(const StringTokenizer &tok)
00539                         : myTok(&tok),tokEnd(0),endp(myTok->str-1),token(0) {
00540                         ++(*this); // init first token.
00541                 }
00542         public:
00543                 iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
00544                 
00545                 // see also: comment in implementation of operator++
00546                 virtual ~iterator() { if (token) *token='\0'; delete token; }
00547                 
00551                 // everything, but not responsible for the allocated token.
00552                 iterator(const iterator& i) :
00553                         myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
00554                         endp(i.endp),token(0) {}
00555                 
00559                 // everything, but not responsible for the allocated token.
00560                 iterator &operator = (const iterator &i) {
00561                         myTok = i.myTok; 
00562                         start = i.start; endp = i.endp; tokEnd = i.tokEnd;
00563                         token = 0;
00564                         return *this;
00565                 }
00566 
00570                 iterator &operator ++ () THROWS (NoSuchElementException);
00571 
00580                 const char*  operator *  () THROWS (NoSuchElementException);
00581                 
00588                 inline char nextDelimiter() const {
00589                         return (tokEnd) ? *tokEnd : '\0';
00590                 }
00591                 
00596                 // only compare the end-position. speed.
00597                 inline bool operator == (const iterator &other) const { 
00598                         return (endp == other.endp);
00599                 }
00600 
00605                 // only compare the end position. speed.
00606                 inline bool operator != (const iterator &other) const { 
00607                         return (endp != other.endp);
00608                 }
00609         };
00610 private:
00611         friend class StringTokenizer::iterator;
00612         const char *str;
00613         const char *delim;
00614         bool skipAll, trim;
00615         iterator itEnd;
00616 
00617 public:
00656         StringTokenizer (const char *str,
00657                          const char *delim,
00658                          bool skipAllDelim = false,
00659                          bool trim = false);
00660         
00670         StringTokenizer (const char *s);
00671 
00675         iterator begin() const { 
00676                 return iterator(*this); 
00677         }
00678         
00683         void setDelimiters (const char *d) {
00684                 delim = d;
00685         }
00686         
00691         iterator begin(const char *d) { 
00692                 delim = d;
00693                 return iterator(*this);
00694         }
00695 
00699         const iterator& end() const { return itEnd; }
00700 };
00701 
00702 #ifdef  __NAMESPACES__
00703 };
00704 #endif
00705 
00706 #endif
00707 

Generated at Tue Nov 20 12:34:33 2001 for CommonC++ by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001