9 #include "wvprotostream.h"
16 WvProtoStream::WvProtoStream(
WvStream *_cloned,
WvLog *_debuglog)
20 logp =
new WvLog(_debuglog->
split(WvLog::Debug4));
29 WvProtoStream::~WvProtoStream()
41 if (logp && log_enable)
44 logp->
write(buf, size);
54 static unsigned char whitespace[] =
" \t\r\n";
58 tokbuf.
get(tokbuf.
match(whitespace,
sizeof(whitespace)));
61 len = tokbuf.
notmatch(whitespace,
sizeof(whitespace));
62 return len ?
new Token(tokbuf.
get(len), len) : NULL;
66 WvString WvProtoStream::next_token_str()
68 Token *t = next_token();
77 WvString WvProtoStream::token_remaining()
87 WvProtoStream::TokenList *WvProtoStream::tokenize()
89 TokenList *tl =
new TokenList;
92 while ((t = next_token()) != NULL)
95 if (logp && log_enable && !tl->isempty())
98 TokenList::Iter i(*tl);
99 for (i.rewind(); i.next(); )
100 (*logp)(
"(%s) ", i.data);
112 size_t WvProtoStream::list_to_array(TokenList *tl, Token **array)
114 size_t total = tl->count(), count;
117 *array =
new Token[total];
119 TokenList::Iter i(*tl);
120 for (count = 0, i.rewind(); i.next(); count++)
123 (*array)[count].fill((
unsigned char *)(
const char *)t.data, t.length);
139 if (!line)
return NULL;
141 char *newline = strdup(line);
144 tokbuf.put(line, strlen(line));
146 if (logp && log_enable)
170 if ( (!case_sensitive && !strcasecmp(t.data, *i))
171 || ( case_sensitive && !strcmp(t.data, *i)) )
179 void WvProtoStream::do_state(Token &)
184 void WvProtoStream::switch_state(
int newstate)
212 WvProtoStream::Token::Token()
218 WvProtoStream::Token::Token(
const unsigned char *_data,
size_t _length)
220 fill(_data, _length);
224 void WvProtoStream::Token::fill(
const unsigned char *_data,
229 data.setsize(length + 1);
230 memcpy(data.edit(), _data, length);
231 data.edit()[length] = 0;
235 WvProtoStream::Token::~Token()
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
void zap()
Clears the buffer.
size_t used() const
Returns the number of elements in the buffer currently available for reading.
size_t notmatch(const void *bytelist, size_t numbytes)
Returns the number of leading buffer elements that do not match any of those in the list.
size_t match(const void *bytelist, size_t numbytes)
Returns the number of leading buffer elements that match any of those in the list.
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
WvLog split(LogLevel _loglevel) const
split off a new WvLog object with the requested loglevel.
virtual void execute()
pass input through to the state machine, one line at a time
virtual size_t uwrite(const void *buffer, size_t size)
override uwrite() so we can log all output
int tokanal(const Token &t, const char **lookup, bool case_sensitive=false)
Convert token strings to enum values.
WvStreamClone simply forwards all requests to the "cloned" stream.
virtual void close()
Close this stream.
virtual void execute()
The callback() function calls execute(), and then calls the user- specified callback if one is define...
virtual size_t uwrite(const void *buf, size_t size)
unbuffered I/O functions; these ignore the buffer, which is handled by write().
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
virtual size_t write(const void *buf, size_t count)
Write data to the stream.
char * getline(time_t wait_msec=0, char separator='\n', int readahead=1024)
Read up to one line of data from the stream and return a pointer to the internal buffer containing th...
WvString is an implementation of a simple and efficient printable-string class.
int lookup(const char *str, const char *const *table, bool case_sensitive=false)
Finds a string in an array and returns its index.
char * trim_string(char *string)
Trims whitespace from the beginning and end of the character string, including carriage return / line...