00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // CharInBuffer.h 00004 //------------------------------------------------------------------------------ 00005 // Copyright (C) 2002,2005 Vladislav Grinchenko 00006 // 00007 // This library is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Library General Public 00009 // License as published by the Free Software Foundation; either 00010 // version 2 of the License, or (at your option) any later version. 00011 //------------------------------------------------------------------------------ 00012 #ifndef CHAR_IN_BUFFER_H 00013 #define CHAR_IN_BUFFER_H 00014 00021 #include <sys/types.h> 00022 00023 #include "assa/Assure.h" 00024 #include "assa/Socket.h" 00025 00026 #include <string> 00027 using std::string; 00028 00029 namespace ASSA { 00030 00044 class CharInBuffer 00045 { 00046 public: 00051 CharInBuffer (size_t size_, const string& delimiter_); 00052 00057 friend ASSA::Socket& operator>>(ASSA::Socket&, ASSA::CharInBuffer&); 00058 00063 operator void* () const; 00064 00066 const char* c_str () const { return m_buffer.c_str (); } 00067 00069 size_t length () const { return m_buffer.length (); } 00070 00072 size_t size () const { return m_buffer.size (); } 00073 00077 void reset (); 00078 00080 void dump () const; 00081 00085 enum state_t { 00086 start, 00087 waiting, 00088 complete, 00089 error 00090 }; 00091 00093 state_t state () const { return m_state; } 00094 00095 private: 00097 static const char* state_name (state_t state_); 00098 00100 void state (state_t new_state_) { m_state = new_state_; } 00101 00103 void chop (); 00104 00105 private: 00107 state_t m_state; 00108 00110 std::string m_buffer; 00111 00113 size_t m_max_size; 00114 00116 std::string m_delimiter; 00117 }; 00118 00119 } // end namespace ASSA 00120 00121 /******************************************************************************* 00122 Inline member functions 00123 *******************************************************************************/ 00124 using namespace ASSA; 00125 00126 inline 00127 CharInBuffer:: 00128 operator void* () const 00129 { 00130 return (m_state == complete 00131 ? (void *) (-1) // good state 00132 : (void *) 0); // bad state 00133 } 00134 00135 inline void 00136 CharInBuffer:: 00137 reset () 00138 { 00139 m_buffer = ""; 00140 state (waiting); 00141 } 00142 00143 inline void 00144 CharInBuffer:: 00145 chop () 00146 { 00147 m_buffer.replace (m_buffer.find (m_delimiter), m_delimiter.length (), ""); 00148 } 00149 00150 #endif /* CHAR_IN_BUFFER_H */