http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex1_en/
Dependencies: mbed RemoteIR SuperTweet ConfigFile EthernetNetIf
Diff: mylib/StreamFilter/StreamFilter.cpp
- Revision:
- 0:db299c5a18ba
- Child:
- 1:c4cfd136f9c7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mylib/StreamFilter/StreamFilter.cpp Fri Oct 29 23:17:01 2010 +0000 @@ -0,0 +1,97 @@ + +#include "StreamFilter.h" + +/** + * Create. + * + * @param headtxt Head of a target. + * @param tailtxt Tail of a target. + */ +StreamFilter::StreamFilter(const char *headtxt, const char *tailtxt) + : headcnt(0), tailcnt(0), head(headtxt), tail(tailtxt), content("") { +} + +/** + * Dispose. + */ +StreamFilter::~StreamFilter() { +} + +/** + * Check state of done. + * + * @return Return true if it done. + */ +bool StreamFilter::done() { + if ((headcnt == strlen(head)) && (tailcnt == strlen(tail))) { + return true; + } + return false; +} + +/** + * Reset state. + */ +void StreamFilter::reset() { + headcnt = 0; + tailcnt = 0; + memset(content, 0, sizeof(content)); +} + +/** + * Push a data to a internal buffer. + * + * @param c character. + */ +void StreamFilter::push(char c) { + if (done()) { + return; + } + + /* + * Check a text for the header. + */ + if (headcnt < strlen(head)) { + if (c == head[headcnt]) { + headcnt++; + } else { + headcnt = 0; + } + return; + } + /* + * Check a text for the tailer. + */ + if (tailcnt < strlen(tail)) { + if (c == tail[tailcnt]) { + tailcnt++; + return; + } else { + tailcnt = 0; + } + } + /* + * It's a content. + */ + const int n = strlen(content); + if (n < sizeof(content) - 1) { + content[n + 0] = c; + content[n + 1] = '\0'; + } +} + +/** + * Get a content text. + * + * @param buf A pointer to a buffer. + * @param bufsiz Size of the buffer. + * + * @return true if it succeed. + */ +bool StreamFilter::getContent(char *buf, size_t bufsiz) { + if (strlen(content) < bufsiz) { + strcpy(buf, content); + return true; + } + return false; +}