http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex1_en/
Dependencies: mbed RemoteIR SuperTweet ConfigFile EthernetNetIf
StreamFilter.cpp
00001 /** 00002 * ============================================================================= 00003 * Stream filter for Twitter. 00004 * http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex1_en/ 00005 * ============================================================================= 00006 * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems) 00007 * 00008 * Permission is hereby granted, free of charge, to any person obtaining a copy 00009 * of this software and associated documentation files (the "Software"), to deal 00010 * in the Software without restriction, including without limitation the rights 00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00012 * copies of the Software, and to permit persons to whom the Software is 00013 * furnished to do so, subject to the following conditions: 00014 * 00015 * The above copyright notice and this permission notice shall be included in 00016 * all copies or substantial portions of the Software. 00017 * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00024 * THE SOFTWARE. 00025 * ============================================================================= 00026 */ 00027 00028 #include "StreamFilter.h" 00029 00030 /** 00031 * Create. 00032 * 00033 * @param headtxt Head of a target. 00034 * @param tailtxt Tail of a target. 00035 */ 00036 StreamFilter::StreamFilter(const char *headtxt, const char *tailtxt) 00037 : headcnt(0), tailcnt(0), head(headtxt), tail(tailtxt), content("") { 00038 } 00039 00040 /** 00041 * Dispose. 00042 */ 00043 StreamFilter::~StreamFilter() { 00044 } 00045 00046 /** 00047 * Check state of done. 00048 * 00049 * @return Return true if it done. 00050 */ 00051 bool StreamFilter::done() { 00052 if ((headcnt == strlen(head)) && (tailcnt == strlen(tail))) { 00053 return true; 00054 } 00055 return false; 00056 } 00057 00058 /** 00059 * Reset state. 00060 */ 00061 void StreamFilter::reset() { 00062 headcnt = 0; 00063 tailcnt = 0; 00064 memset(content, 0, sizeof(content)); 00065 } 00066 00067 /** 00068 * Push a data to a internal buffer. 00069 * 00070 * @param c character. 00071 */ 00072 void StreamFilter::push(char c) { 00073 if (done()) { 00074 return; 00075 } 00076 00077 /* 00078 * Check a text for the header. 00079 */ 00080 if (headcnt < strlen(head)) { 00081 if (c == head[headcnt]) { 00082 headcnt++; 00083 } else { 00084 headcnt = 0; 00085 } 00086 return; 00087 } 00088 /* 00089 * Check a text for the tailer. 00090 */ 00091 if (tailcnt < strlen(tail)) { 00092 if (c == tail[tailcnt]) { 00093 tailcnt++; 00094 return; 00095 } else { 00096 tailcnt = 0; 00097 } 00098 } 00099 /* 00100 * It's a content. 00101 */ 00102 const int n = strlen(content); 00103 if (n < sizeof(content) - 1) { 00104 content[n + 0] = c; 00105 content[n + 1] = '\0'; 00106 } 00107 } 00108 00109 /** 00110 * Get a content text. 00111 * 00112 * @param buf A pointer to a buffer. 00113 * @param bufsiz Size of the buffer. 00114 * 00115 * @return true if it succeed. 00116 */ 00117 bool StreamFilter::getContent(char *buf, size_t bufsiz) { 00118 if (strlen(content) < bufsiz) { 00119 strcpy(buf, content); 00120 return true; 00121 } 00122 return false; 00123 }
Generated on Fri Jul 15 2022 13:37:25 by 1.7.2