libassa  3.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
19 using namespace ASSA;
20 
21 /*******************************************************************************
22  Member functions
23 *******************************************************************************/
25 CharInBuffer (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 
36 const char*
39 {
40  static const char* vmsg[] =
41  { "start", "waiting", "complete", "error", "unknown state" };
42 
43  if (state_ < CharInBuffer::start || state_ > CharInBuffer::error) {
44  return vmsg [sizeof (vmsg)-1];
45  }
46  return vmsg [state_];
47 }
48 
49 void
51 dump () 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 
66 namespace ASSA {
67 
79 Socket&
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 ();
103  return s_;
104  }
105 
106  if (b_.m_buffer.length () >= b_.m_max_size) {
108  break;
109  }
110  }
111 
112  if (!s_) { // EOF or error
114  }
115 
116  return s_;
117 }
118 } // end namespace ASSA