libassa 3.5.1
Loading...
Searching...
No Matches
CharInBuffer.cpp
Go to the documentation of this file.
1// -*- c++ -*-
2//------------------------------------------------------------------------------
3// CharInBuffer.cpp
4//------------------------------------------------------------------------------
5// Copyright (C) 2002 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
13#include <errno.h>
14
15#include "assa/CharInBuffer.h"
16#include "assa/MemDump.h"
17#include "assa/Logger.h"
18
19using namespace ASSA;
20
21/*******************************************************************************
22 Member functions
23*******************************************************************************/
25CharInBuffer (size_t size_, const string& delimiter_)
26 : m_state (start), m_max_size (size_), m_delimiter (delimiter_)
27{
28 trace_with_mask ("CharInBuffer::CharInBuffer", CHARINBUF);
29
30 if (m_max_size == 0 || m_delimiter.length () == 0) {
31 state (error);
32 }
33 state (waiting);
34}
35
36const char*
39{
40 static const char* vmsg[] =
41 { "start", "waiting", "complete", "error", "unknown state" };
42
44 return vmsg [sizeof (vmsg)-1];
45 }
46 return vmsg [state_];
47}
48
49void
51dump () const
52{
53 DL((CHARINBUF,"== CharInBuffer state ==\n"));
54 DL((CHARINBUF,"m_state = %s\n", state_name (m_state)));
55 DL((CHARINBUF,"m_max_size = %d\n", m_max_size));
56
57 MemDump::dump_to_log (TRACE, "m_delimiter:\n",
58 m_delimiter.c_str (), m_delimiter.length ());
59
60 MemDump::dump_to_log (TRACE, "m_buffer:\n",
61 m_buffer.c_str (), m_buffer.length ());
62
63 DL((CHARINBUF,"========================\n"));
64}
65
66namespace ASSA {
67
79Socket&
81{
82 trace_with_mask ("Socket >> CharInBuffer", CHARINBUF);
83 register char c;
84
85 if (b_.state () != CharInBuffer::waiting) {
86 DL((CHARINBUF,"Wrong state %s\n", b_.state_name (b_.state ())));
87 return s_;
88 }
89
90 while (s_.read (&c, 1) == 1)
91 {
92 b_.m_buffer += c;
93
94 if (b_.m_buffer.size() < b_.m_delimiter.size()) { // Bug # 1252926
95 continue;
96 }
97
98 if (b_.m_buffer.substr (
99 b_.m_buffer.size ()-b_.m_delimiter.size ()) == b_.m_delimiter)
100 {
101 b_.chop ();
102 b_.m_state = CharInBuffer::complete;
103 return s_;
104 }
105
106 if (b_.m_buffer.length () >= b_.m_max_size) {
107 b_.m_state = CharInBuffer::error;
108 break;
109 }
110 }
111
112 if (!s_) { // EOF or error
113 b_.state (CharInBuffer::error);
114 }
115
116 return s_;
117}
118} // end namespace ASSA
A bucket for collecting character-based stream records of certain length or terminated by designated ...
An abstraction to message logging facility.
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
A Hex/Ascii memory dump of similar to od(1) UNIX utility.
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.
CharInBuffer(size_t size_, const string &delimiter_)
Constructor.
std::string m_delimiter
Delimiter. Multibyte delimiter is allowed.
state_t
States: start, waiting, complete, error.
@ complete
matched end-of-record - full record
@ error
overflow or Socket I/O error
@ waiting
incomplete record is in the buffer
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.
static void dump_to_log(unsigned long mask_, const char *info_, const char *msg_, int len_)
Write hex/ascii dump of a memory region to log file.
Definition MemDump.cpp:111
@ TRACE
Function call trace
Definition LogMask.h:26
@ CHARINBUF
Class CharInBuffer messages
Definition LogMask.h:50
Socket & operator>>(Socket &s_, CharInBuffer &b_)
Regardless of the delimeter size, which can be >1, add the character received to the buffer and compa...