bincimap

Log | Files | Refs | LICENSE

io.h (4311B)


      1 /* -*- Mode: c++; -*- */
      2 /*  --------------------------------------------------------------------
      3  *  Filename:
      4  *    src/io/io.h
      5  *  
      6  *  Description:
      7  *    Declaration of the IO class.
      8  *
      9  *  Authors:
     10  *    Andreas Aardal Hanssen <andreas-binc curly bincimap spot org>
     11  *
     12  *  Bugs:
     13  *
     14  *  ChangeLog:
     15  *
     16  *  --------------------------------------------------------------------
     17  *  Copyright 2002-2005 Andreas Aardal Hanssen
     18  *
     19  *  This program is free software; you can redistribute it and/or modify
     20  *  it under the terms of the GNU General Public License as published by
     21  *  the Free Software Foundation; either version 2 of the License, or
     22  *  (at your option) any later version.
     23  *
     24  *  This program is distributed in the hope that it will be useful,
     25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     27  *  GNU General Public License for more details.
     28  *
     29  *  You should have received a copy of the GNU General Public License
     30  *  along with this program; if not, write to the Free Software
     31  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
     32  *  --------------------------------------------------------------------
     33  */
     34 #ifndef io_h_included
     35 #define io_h_included
     36 #ifdef HAVE_CONFIG_H
     37 #include <config.h>
     38 #endif
     39 
     40 #include <deque>
     41 #include <string>
     42 #include <iostream>
     43 #include <iomanip>
     44 #include <map>
     45 
     46 #include <errno.h>
     47 #include <stdio.h>
     48 #include <syslog.h>
     49 
     50 #include <ctype.h>
     51 #include <sys/time.h>
     52 #include <sys/types.h>
     53 #include <unistd.h>
     54 
     55 #include "convert.h"
     56 
     57 // #define DEBUG
     58 
     59 namespace Binc {
     60 
     61   //----------------------------------------------------------------------
     62   class IO {
     63   public:
     64     enum {
     65       MODE_PLAIN = 0x01,
     66       MODE_SYSLOG = 0x02
     67     };
     68 
     69   public:
     70 
     71     //--
     72     void clear(void);
     73     void noFlushOnEndl(void);
     74     void flushOnEndl(void);
     75     void flushContent(void);
     76     void resetInput(void);
     77     void disableInputLimit(void);
     78     void enableInputLimit(void);
     79 
     80     void enable(void);
     81     void disable(void);
     82 
     83     void enableLogPrefix(void);
     84     void setModeSyslog(const std::string &servicename, int facility);
     85     void setFd(FILE *fpout);
     86 
     87     virtual void writeStr(const std::string s);
     88     virtual int readChar(int timeout = 0, bool retry = true);
     89     virtual int readStr(std::string &data, int bytes = -1, int timeout = 0, bool retry = true);
     90     virtual int fillBuffer(int timeout, bool retry);
     91 
     92     void unReadChar(int c_in);
     93     void unReadChar(const std::string &s_in);
     94     virtual int pending(void) const;
     95 
     96     inline void setBufferSize(int s) { buffersize = s; }
     97     inline void setTransferTimeout(int s) { transfertimeout = s; }
     98     inline void setLogPrefix(const std::string s_in) { logprefix = s_in; }
     99 
    100     template <class T> IO &operator << (const T &o);
    101     IO &operator << (std::ostream &(*man)(std::ostream &));
    102 
    103     const std::string &getLastError(void) const;
    104     void setLastError(const std::string &e);
    105 
    106     //--
    107     IO();
    108     IO(FILE *fp);
    109     virtual ~IO();
    110 
    111   protected:
    112     BincStream outputBuffer;
    113 
    114     std::deque<char> inputBuffer;
    115     std::string logprefix;
    116     int buffersize;
    117     int transfertimeout;
    118     int seqnr;
    119     int pid;
    120     int mode;
    121 
    122     int inputsize;
    123     bool inputlimit;
    124 
    125     bool flushesOnEndl;
    126     bool useLogPrefix;
    127     bool enabled;
    128 
    129     std::string lastError;
    130 
    131     FILE *fpout;
    132 
    133     //--
    134     int select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, int &timeout);
    135   };
    136 
    137   //----------------------------------------------------------------------
    138   class IOFactory {
    139   private:
    140     std::map<int, IO *> ioers;
    141 
    142     //--
    143     IOFactory(void);
    144 
    145   public:
    146     void assign(int, IO *);
    147     IO &get(int);
    148 
    149     //--
    150     static IOFactory &getInstance(void);
    151     ~IOFactory(void);
    152   };
    153 }
    154 
    155 //------------------------------------------------------------------------
    156 template <class T> Binc::IO &Binc::IO::operator << (const T &o)
    157 {
    158   using namespace ::std;
    159   
    160   if (useLogPrefix && outputBuffer.getSize() == 0) {
    161     outputBuffer << pid;
    162     outputBuffer << " ";
    163     outputBuffer << seqnr++;
    164     if (logprefix != "")
    165       outputBuffer << " [" << logprefix << "]";
    166     outputBuffer << " ";
    167   }
    168   
    169   outputBuffer << o;
    170   
    171   if ((int) outputBuffer.getSize() >= buffersize)
    172     flushContent();
    173   
    174   return *this;
    175 }
    176 
    177 #endif