Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

CharChopper.h

00001 #ifndef _STR_CharChopper_H_  // -*-c++-*-
00002 
00003 /*
00004  * Copyright 1991-2002 Eric M. Hopper <hopper@omnifarious.org>
00005  * 
00006  *     This program is free software; you can redistribute it and/or modify it
00007  *     under the terms of the GNU Lesser General Public License as published
00008  *     by the Free Software Foundation; either version 2 of the License, or
00009  *     (at your option) any later version.
00010  * 
00011  *     This program is distributed in the hope that it will be useful, but
00012  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *     Lesser General Public License for more details.
00015  * 
00016  *     You should have received a copy of the GNU Lesser General Public
00017  *     License along with this program; if not, write to the Free Software
00018  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #ifdef __GNUG__
00022 #  pragma interface
00023 #endif
00024 
00025 /* $Header: /home/hopper/src/cvs/C++/StrMod/StrMod/CharChopper.h,v 1.9 2002/11/25 05:40:05 hopper Exp $ */
00026 
00027 // For a log, see ../Changelog
00028 //
00029 // Revision 1.1  1996/09/02 23:28:27  hopper
00030 // Added CharChopper class so users would have a simple class that would
00031 // break up and recombine streams using whatever character they chose as
00032 // a separator.
00033 //
00034 
00035 #include <cstddef>
00036 #include <cassert>
00037 #include <StrMod/StreamProcessor.h>
00038 #include <StrMod/StrChunkPtrT.h>
00039 #include <StrMod/BufferChunk.h>
00040 #include <StrMod/GroupChunk.h>
00041 
00042 #define _STR_CharChopper_H_
00043 
00044 namespace strmod {
00045 namespace strmod {
00046 
00047 class GroupVector;
00048 
00049 /** \class CharChopper CharChopper.h StrMod/CharChopper.h
00050  * \brief Chops up the data in chunks delimited by a character.
00051  *
00052  * If an object of this class is constructed with a 'g', and you have
00053  * an incoming stream of data that arrives in the following chunks:
00054  *
00055  * \code <This ghost> < went to the gilded post t> <o admire the gold.> \endcode
00056  *
00057  * You'll end up with an outgoing stream of data that looks like this:
00058  *
00059  * \code <This g> <host went to the g> <ilded post to admire the g> \endcode
00060  *
00061  * It will hold the last few characters <old.>, waiting for another 'g' before
00062  * it send them off again.
00063  */
00064 class CharChopper : public StreamProcessor
00065 {
00066  public:
00067    static const STR_ClassIdent identifier;
00068 
00069    /** Make a CharChopper
00070     * @param chopchar The character to split by.
00071     *
00072     * The character can be anything, including '\\0'.
00073     */
00074    CharChopper(char chopchar) : chopchar_(chopchar)    { }
00075    // Derived class destructor doesn't do anything base class one doesn't do.
00076 
00077    inline virtual int AreYouA(const lcore::ClassIdent &cid) const;
00078 
00079  private:
00080    const char chopchar_;
00081    StrChunkPtrT<GroupChunk> groupdata_;
00082    StrChunkPtrT<BufferChunk> curdata_;
00083    size_t usedsize_;
00084    enum { INYes, INNo, INMaybe } incoming_is_bc_;
00085 
00086    virtual const lcore::ClassIdent *i_GetIdent() const  { return &identifier; }
00087 
00088    virtual void processIncoming();
00089 
00090    void addChunk(const StrChunkPtr &chnk);
00091    inline void checkIncoming();
00092    inline void zeroIncoming();
00093    inline void replaceIncoming(const StrChunkPtr &data);
00094 
00095  private:
00096    CharChopper(const CharChopper &b);
00097 };
00098 
00099 //-----------------------------inline functions--------------------------------
00100 
00101 inline int CharChopper::AreYouA(const lcore::ClassIdent &cid) const
00102 {
00103    return((identifier == cid) || StreamProcessor::AreYouA(cid));
00104 }
00105 
00106 inline void CharChopper::checkIncoming()
00107 {
00108    assert(incoming_);
00109    if (incoming_is_bc_ == INMaybe)
00110    {
00111       if (incoming_->AreYouA(BufferChunk::identifier))
00112       {
00113          incoming_is_bc_ = INYes;
00114       }
00115       else
00116       {
00117          incoming_is_bc_ = INNo;
00118       }
00119    }
00120 }
00121 
00122 inline void CharChopper::zeroIncoming()
00123 {
00124    incoming_.ReleasePtr();
00125    incoming_is_bc_ = INMaybe;
00126 }
00127 
00128 inline void CharChopper::replaceIncoming(const StrChunkPtr &data)
00129 {
00130    incoming_ = data;
00131 }
00132 
00133 };  // namespace strmod
00134 };  // namespace strmod
00135 
00136 #endif

Generated on Wed Jan 29 00:32:43 2003 for libNet by doxygen1.3-rc1