libassa 3.5.1
CharInBuffer.h
Go to the documentation of this file.
1// -*- c++ -*-
2//------------------------------------------------------------------------------
3// CharInBuffer.h
4//------------------------------------------------------------------------------
5// Copyright (C) 2002,2005 Vladislav Grinchenko
6//
7// This library is free software; you can redistribute it and/or
8// modify it under the terms of the GNU Library General Public
9// License as published by the Free Software Foundation; either
10// version 2 of the License, or (at your option) any later version.
11//------------------------------------------------------------------------------
12#ifndef CHAR_IN_BUFFER_H
13#define CHAR_IN_BUFFER_H
14
21#include <sys/types.h>
22
23#include "assa/Assure.h"
24#include "assa/Socket.h"
25
26#include <string>
27using std::string;
28
29namespace ASSA {
30
45{
46public:
51 CharInBuffer (size_t size_, const string& delimiter_);
52
58
63 operator void* () const;
64
66 const char* c_str () const { return m_buffer.c_str (); }
67
69 size_t length () const { return m_buffer.length (); }
70
72 size_t size () const { return m_buffer.size (); }
73
77 void reset ();
78
80 void dump () const;
81
85 enum state_t {
89 error
90 };
91
93 state_t state () const { return m_state; }
94
95private:
97 static const char* state_name (state_t state_);
98
100 void state (state_t new_state_) { m_state = new_state_; }
101
103 void chop ();
104
105private:
108
110 std::string m_buffer;
111
114
116 std::string m_delimiter;
117};
118
119} // end namespace ASSA
120
121/*******************************************************************************
122 Inline member functions
123*******************************************************************************/
124using namespace ASSA;
125
126inline
127CharInBuffer::
128operator void* () const
129{
130 return (m_state == complete
131 ? (void *) (-1) // good state
132 : (void *) 0); // bad state
133}
134
135inline void
136CharInBuffer::
137reset ()
138{
139 m_buffer = "";
140 state (waiting);
141}
142
143inline void
145chop ()
146{
147 m_buffer.replace (m_buffer.find (m_delimiter), m_delimiter.length (), "");
148}
149
150#endif /* CHAR_IN_BUFFER_H */
A collection of assert function wrappers.
Abstraction of socket data type.
CharInBuffer is a bucket for the character-based streams/messages.
Definition: CharInBuffer.h:45
static const char * state_name(state_t state_)
Report the state name.
std::string m_buffer
Buffer to store the bytes received.
Definition: CharInBuffer.h:110
state_t m_state
Internal state of an object.
Definition: CharInBuffer.h:107
void chop()
Remove the delimiter from the end of the buffer.
Definition: CharInBuffer.h:145
friend ASSA::Socket & operator>>(ASSA::Socket &, ASSA::CharInBuffer &)
Read bytes from Socket stream until either record delimiter is detected, or EOF occured,...
CharInBuffer(size_t size_, const string &delimiter_)
Constructor.
const char * c_str() const
Get the constant character pointer to the buffer.
Definition: CharInBuffer.h:66
std::string m_delimiter
Delimiter. Multibyte delimiter is allowed.
Definition: CharInBuffer.h:116
size_t size() const
Bytes in the buffer so far.
Definition: CharInBuffer.h:72
void state(state_t new_state_)
Go to the new state.
Definition: CharInBuffer.h:100
state_t
States: start, waiting, complete, error.
Definition: CharInBuffer.h:85
@ start
start state
Definition: CharInBuffer.h:86
@ complete
matched end-of-record - full record
Definition: CharInBuffer.h:88
@ error
overflow or Socket I/O error
Definition: CharInBuffer.h:89
@ waiting
incomplete record is in the buffer
Definition: CharInBuffer.h:87
size_t length() const
Bytes in the buffer so far.
Definition: CharInBuffer.h:69
void reset()
Discard all accumulated characters and be ready to receive a new message.
Definition: CharInBuffer.h:137
size_t m_max_size
Maximum allowable size (delimiter included) before overflow occurs.
Definition: CharInBuffer.h:113
state_t state() const
Report the current state of the object.
Definition: CharInBuffer.h:93
void dump() const
Write the state of an object to the log file.
Definition: Acceptor.h:40