Loading...
Searching...
No Matches
CharInBuffer.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// CharInBuffer.h
//------------------------------------------------------------------------------
// Copyright (C) 2002,2005 Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#ifndef CHAR_IN_BUFFER_H
#define CHAR_IN_BUFFER_H
#include <sys/types.h>
#include "assa/Assure.h"
#include "assa/Socket.h"
#include <string>
using std::string;
namespace ASSA {
class CharInBuffer
{
public:
CharInBuffer (size_t size_, const string& delimiter_);
friend ASSA::Socket& operator>>(ASSA::Socket&, ASSA::CharInBuffer&);
operator void* () const;
const char* c_str () const { return m_buffer.c_str (); }
size_t length () const { return m_buffer.length (); }
size_t size () const { return m_buffer.size (); }
void reset ();
void dump () const;
enum state_t {
start,
waiting,
complete,
error
};
state_t state () const { return m_state; }
private:
static const char* state_name (state_t state_);
void state (state_t new_state_) { m_state = new_state_; }
void chop ();
private:
state_t m_state;
std::string m_buffer;
size_t m_max_size;
std::string m_delimiter;
};
} // end namespace ASSA
/*******************************************************************************
Inline member functions
*******************************************************************************/
using namespace ASSA;
inline
CharInBuffer::
operator void* () const
{
return (m_state == complete
? (void *) (-1) // good state
: (void *) 0); // bad state
}
inline void
CharInBuffer::
reset ()
{
m_buffer = "";
state (waiting);
}
inline void
CharInBuffer::
chop ()
{
m_buffer.replace (m_buffer.find (m_delimiter), m_delimiter.length (), "");
}
#endif /* CHAR_IN_BUFFER_H */
Assure.h
A collection of assert function wrappers.
Socket.h
Abstraction of socket data type.
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::CharInBuffer
CharInBuffer is a bucket for the character-based streams/messages.
Definition CharInBuffer.h:45
ASSA::CharInBuffer::state_name
static const char * state_name(state_t state_)
Report the state name.
Definition CharInBuffer.cpp:38
ASSA::CharInBuffer::m_buffer
std::string m_buffer
Buffer to store the bytes received.
Definition CharInBuffer.h:110
ASSA::CharInBuffer::m_state
state_t m_state
Internal state of an object.
Definition CharInBuffer.h:107
ASSA::CharInBuffer::chop
void chop()
Remove the delimiter from the end of the buffer.
Definition CharInBuffer.h:145
ASSA::CharInBuffer::operator>>
friend ASSA::Socket & operator>>(ASSA::Socket &, ASSA::CharInBuffer &)
Read bytes from Socket stream until either record delimiter is detected, or EOF occured,...
Definition CharInBuffer.cpp:80
ASSA::CharInBuffer::c_str
const char * c_str() const
Get the constant character pointer to the buffer.
Definition CharInBuffer.h:66
ASSA::CharInBuffer::m_delimiter
std::string m_delimiter
Delimiter. Multibyte delimiter is allowed.
Definition CharInBuffer.h:116
ASSA::CharInBuffer::size
size_t size() const
Bytes in the buffer so far.
Definition CharInBuffer.h:72
ASSA::CharInBuffer::state
void state(state_t new_state_)
Go to the new state.
Definition CharInBuffer.h:100
ASSA::CharInBuffer::state_t
state_t
States: start, waiting, complete, error.
Definition CharInBuffer.h:85
ASSA::CharInBuffer::start
@ start
start state
Definition CharInBuffer.h:86
ASSA::CharInBuffer::complete
@ complete
matched end-of-record - full record
Definition CharInBuffer.h:88
ASSA::CharInBuffer::error
@ error
overflow or Socket I/O error
Definition CharInBuffer.h:89
ASSA::CharInBuffer::waiting
@ waiting
incomplete record is in the buffer
Definition CharInBuffer.h:87
ASSA::CharInBuffer::length
size_t length() const
Bytes in the buffer so far.
Definition CharInBuffer.h:69
ASSA::CharInBuffer::reset
void reset()
Discard all accumulated characters and be ready to receive a new message.
Definition CharInBuffer.h:137
ASSA::CharInBuffer::m_max_size
size_t m_max_size
Maximum allowable size (delimiter included) before overflow occurs.
Definition CharInBuffer.h:113
ASSA::CharInBuffer::state
state_t state() const
Report the current state of the object.
Definition CharInBuffer.h:93
ASSA::CharInBuffer::dump
void dump() const
Write the state of an object to the log file.
Definition CharInBuffer.cpp:51
ASSA::Socket
Definition Socket.h:71
ASSA
Definition Acceptor.h:40