libassa 3.5.1
Loading...
Searching...
No Matches
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
91
93 state_t state () const { return m_state; }
94
95private:
97 static const char* state_name (state_t state_);
98
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
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.
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
CharInBuffer is a bucket for the character-based streams/messages.
static const char * state_name(state_t state_)
Report the state name.
std::string m_buffer
Buffer to store the bytes received.
state_t m_state
Internal state of an object.
void chop()
Remove the delimiter from the end of the buffer.
friend ASSA::Socket & operator>>(ASSA::Socket &, ASSA::CharInBuffer &)
Read bytes from Socket stream until either record delimiter is detected, or EOF occured,...
const char * c_str() const
Get the constant character pointer to the buffer.
std::string m_delimiter
Delimiter. Multibyte delimiter is allowed.
size_t size() const
Bytes in the buffer so far.
void state(state_t new_state_)
Go to the new state.
state_t
States: start, waiting, complete, error.
@ start
start state
@ complete
matched end-of-record - full record
@ error
overflow or Socket I/O error
@ waiting
incomplete record is in the buffer
size_t length() const
Bytes in the buffer so far.
void reset()
Discard all accumulated characters and be ready to receive a new message.
size_t m_max_size
Maximum allowable size (delimiter included) before overflow occurs.
state_t state() const
Report the current state of the object.
void dump() const
Write the state of an object to the log file.