This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.
Dependencies: mbed DebugLibrary
NetServices/services/http/client/data/HTTPMap.cpp@0:f30e2135b0db, 2011-02-09 (annotated)
- Committer:
- Yann
- Date:
- Wed Feb 09 13:57:49 2011 +0000
- Revision:
- 0:f30e2135b0db
V0.0.0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yann | 0:f30e2135b0db | 1 | |
Yann | 0:f30e2135b0db | 2 | /* |
Yann | 0:f30e2135b0db | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
Yann | 0:f30e2135b0db | 4 | |
Yann | 0:f30e2135b0db | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
Yann | 0:f30e2135b0db | 6 | of this software and associated documentation files (the "Software"), to deal |
Yann | 0:f30e2135b0db | 7 | in the Software without restriction, including without limitation the rights |
Yann | 0:f30e2135b0db | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Yann | 0:f30e2135b0db | 9 | copies of the Software, and to permit persons to whom the Software is |
Yann | 0:f30e2135b0db | 10 | furnished to do so, subject to the following conditions: |
Yann | 0:f30e2135b0db | 11 | |
Yann | 0:f30e2135b0db | 12 | The above copyright notice and this permission notice shall be included in |
Yann | 0:f30e2135b0db | 13 | all copies or substantial portions of the Software. |
Yann | 0:f30e2135b0db | 14 | |
Yann | 0:f30e2135b0db | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Yann | 0:f30e2135b0db | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Yann | 0:f30e2135b0db | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Yann | 0:f30e2135b0db | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Yann | 0:f30e2135b0db | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Yann | 0:f30e2135b0db | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Yann | 0:f30e2135b0db | 21 | THE SOFTWARE. |
Yann | 0:f30e2135b0db | 22 | */ |
Yann | 0:f30e2135b0db | 23 | |
Yann | 0:f30e2135b0db | 24 | #include "HTTPMap.h" |
Yann | 0:f30e2135b0db | 25 | #include "../../util/url.h" |
Yann | 0:f30e2135b0db | 26 | |
Yann | 0:f30e2135b0db | 27 | //#define __DEBUG |
Yann | 0:f30e2135b0db | 28 | #include "dbg/dbg.h" |
Yann | 0:f30e2135b0db | 29 | |
Yann | 0:f30e2135b0db | 30 | HTTPMap::HTTPMap(const string& keyValueSep /*= "="*/, const string& pairSep /*= "&"*/) : |
Yann | 0:f30e2135b0db | 31 | HTTPData(), Dictionary(), m_buf(), m_len(0), m_chunked(false), m_keyValueSep(keyValueSep), m_pairSep(pairSep) |
Yann | 0:f30e2135b0db | 32 | { |
Yann | 0:f30e2135b0db | 33 | |
Yann | 0:f30e2135b0db | 34 | } |
Yann | 0:f30e2135b0db | 35 | |
Yann | 0:f30e2135b0db | 36 | HTTPMap::~HTTPMap() |
Yann | 0:f30e2135b0db | 37 | { |
Yann | 0:f30e2135b0db | 38 | |
Yann | 0:f30e2135b0db | 39 | } |
Yann | 0:f30e2135b0db | 40 | |
Yann | 0:f30e2135b0db | 41 | void HTTPMap::clear() |
Yann | 0:f30e2135b0db | 42 | { |
Yann | 0:f30e2135b0db | 43 | m_buf.clear(); |
Yann | 0:f30e2135b0db | 44 | } |
Yann | 0:f30e2135b0db | 45 | |
Yann | 0:f30e2135b0db | 46 | int HTTPMap::read(char* buf, int len) |
Yann | 0:f30e2135b0db | 47 | { |
Yann | 0:f30e2135b0db | 48 | memcpy( (void*)buf, (void*)m_buf.data(), len ); |
Yann | 0:f30e2135b0db | 49 | m_buf.erase(0, len); //Erase these chars |
Yann | 0:f30e2135b0db | 50 | return len; |
Yann | 0:f30e2135b0db | 51 | } |
Yann | 0:f30e2135b0db | 52 | |
Yann | 0:f30e2135b0db | 53 | int HTTPMap::write(const char* buf, int len) |
Yann | 0:f30e2135b0db | 54 | { |
Yann | 0:f30e2135b0db | 55 | m_buf.append( buf, len ); |
Yann | 0:f30e2135b0db | 56 | if( ( m_chunked && !len ) || |
Yann | 0:f30e2135b0db | 57 | ( !m_chunked && ( m_buf.length() >= m_len ) ) ) //Last chunk or EOF |
Yann | 0:f30e2135b0db | 58 | { |
Yann | 0:f30e2135b0db | 59 | parseString(); |
Yann | 0:f30e2135b0db | 60 | } |
Yann | 0:f30e2135b0db | 61 | return len; |
Yann | 0:f30e2135b0db | 62 | } |
Yann | 0:f30e2135b0db | 63 | |
Yann | 0:f30e2135b0db | 64 | string HTTPMap::getDataType() //Internet media type for Content-Type header |
Yann | 0:f30e2135b0db | 65 | { |
Yann | 0:f30e2135b0db | 66 | return "application/x-www-form-urlencoded"; |
Yann | 0:f30e2135b0db | 67 | } |
Yann | 0:f30e2135b0db | 68 | |
Yann | 0:f30e2135b0db | 69 | void HTTPMap::setDataType(const string& type) //Internet media type from Content-Type header |
Yann | 0:f30e2135b0db | 70 | { |
Yann | 0:f30e2135b0db | 71 | //Do not really care here |
Yann | 0:f30e2135b0db | 72 | } |
Yann | 0:f30e2135b0db | 73 | |
Yann | 0:f30e2135b0db | 74 | int HTTPMap::getDataLen() //For Content-Length header |
Yann | 0:f30e2135b0db | 75 | { |
Yann | 0:f30e2135b0db | 76 | //This will be called before any read occurs, so let's generate our string object |
Yann | 0:f30e2135b0db | 77 | //Generate string |
Yann | 0:f30e2135b0db | 78 | generateString(); |
Yann | 0:f30e2135b0db | 79 | return m_len; |
Yann | 0:f30e2135b0db | 80 | } |
Yann | 0:f30e2135b0db | 81 | |
Yann | 0:f30e2135b0db | 82 | bool HTTPMap::getIsChunked() //For Transfer-Encoding header |
Yann | 0:f30e2135b0db | 83 | { |
Yann | 0:f30e2135b0db | 84 | return false; //We don't want to chunk this |
Yann | 0:f30e2135b0db | 85 | } |
Yann | 0:f30e2135b0db | 86 | |
Yann | 0:f30e2135b0db | 87 | void HTTPMap::setIsChunked(bool chunked) //From Transfer-Encoding header |
Yann | 0:f30e2135b0db | 88 | { |
Yann | 0:f30e2135b0db | 89 | m_chunked = chunked; |
Yann | 0:f30e2135b0db | 90 | } |
Yann | 0:f30e2135b0db | 91 | |
Yann | 0:f30e2135b0db | 92 | void HTTPMap::setDataLen(int len) //From Content-Length header, or if the transfer is chunked, next chunk length |
Yann | 0:f30e2135b0db | 93 | { |
Yann | 0:f30e2135b0db | 94 | m_len += len; //Useful so that we can parse string when last byte is written if not chunked |
Yann | 0:f30e2135b0db | 95 | m_buf.reserve( m_len ); |
Yann | 0:f30e2135b0db | 96 | } |
Yann | 0:f30e2135b0db | 97 | |
Yann | 0:f30e2135b0db | 98 | void HTTPMap::generateString() |
Yann | 0:f30e2135b0db | 99 | { |
Yann | 0:f30e2135b0db | 100 | Dictionary::iterator it; |
Yann | 0:f30e2135b0db | 101 | bool first = true; |
Yann | 0:f30e2135b0db | 102 | while( !Dictionary::empty() ) |
Yann | 0:f30e2135b0db | 103 | { |
Yann | 0:f30e2135b0db | 104 | if(!first) |
Yann | 0:f30e2135b0db | 105 | { |
Yann | 0:f30e2135b0db | 106 | m_buf.append( m_pairSep ); |
Yann | 0:f30e2135b0db | 107 | } |
Yann | 0:f30e2135b0db | 108 | else |
Yann | 0:f30e2135b0db | 109 | { |
Yann | 0:f30e2135b0db | 110 | first = false; |
Yann | 0:f30e2135b0db | 111 | } |
Yann | 0:f30e2135b0db | 112 | it = Dictionary::begin(); |
Yann | 0:f30e2135b0db | 113 | m_buf.append( Url::encode( (*it).first ) ); |
Yann | 0:f30e2135b0db | 114 | m_buf.append( m_keyValueSep ); |
Yann | 0:f30e2135b0db | 115 | m_buf.append( Url::encode( (*it).second ) ); |
Yann | 0:f30e2135b0db | 116 | Dictionary::erase(it); //Free memory as we process data |
Yann | 0:f30e2135b0db | 117 | } |
Yann | 0:f30e2135b0db | 118 | m_len = m_buf.length(); |
Yann | 0:f30e2135b0db | 119 | DBG("\r\nData [len %d]:\r\n%s\r\n", m_len, m_buf.c_str()); |
Yann | 0:f30e2135b0db | 120 | } |
Yann | 0:f30e2135b0db | 121 | |
Yann | 0:f30e2135b0db | 122 | void HTTPMap::parseString() |
Yann | 0:f30e2135b0db | 123 | { |
Yann | 0:f30e2135b0db | 124 | string key; |
Yann | 0:f30e2135b0db | 125 | string value; |
Yann | 0:f30e2135b0db | 126 | |
Yann | 0:f30e2135b0db | 127 | int pos = 0; |
Yann | 0:f30e2135b0db | 128 | |
Yann | 0:f30e2135b0db | 129 | int key_pos; |
Yann | 0:f30e2135b0db | 130 | int key_len; |
Yann | 0:f30e2135b0db | 131 | |
Yann | 0:f30e2135b0db | 132 | int value_pos; |
Yann | 0:f30e2135b0db | 133 | int value_len; |
Yann | 0:f30e2135b0db | 134 | |
Yann | 0:f30e2135b0db | 135 | bool eof = false; |
Yann | 0:f30e2135b0db | 136 | while(!eof) |
Yann | 0:f30e2135b0db | 137 | { |
Yann | 0:f30e2135b0db | 138 | key_pos = pos; |
Yann | 0:f30e2135b0db | 139 | value_pos = m_buf.find(m_keyValueSep, pos); |
Yann | 0:f30e2135b0db | 140 | if(value_pos < 0) |
Yann | 0:f30e2135b0db | 141 | { |
Yann | 0:f30e2135b0db | 142 | //Parse error, break |
Yann | 0:f30e2135b0db | 143 | break; |
Yann | 0:f30e2135b0db | 144 | } |
Yann | 0:f30e2135b0db | 145 | |
Yann | 0:f30e2135b0db | 146 | key_len = value_pos - key_pos; |
Yann | 0:f30e2135b0db | 147 | |
Yann | 0:f30e2135b0db | 148 | value_pos+= m_keyValueSep.length(); |
Yann | 0:f30e2135b0db | 149 | |
Yann | 0:f30e2135b0db | 150 | pos = m_buf.find( m_pairSep, value_pos ); |
Yann | 0:f30e2135b0db | 151 | if(pos < 0) //Not found |
Yann | 0:f30e2135b0db | 152 | { |
Yann | 0:f30e2135b0db | 153 | pos = m_buf.length(); |
Yann | 0:f30e2135b0db | 154 | eof = true; |
Yann | 0:f30e2135b0db | 155 | } |
Yann | 0:f30e2135b0db | 156 | |
Yann | 0:f30e2135b0db | 157 | value_len = pos - value_pos; |
Yann | 0:f30e2135b0db | 158 | |
Yann | 0:f30e2135b0db | 159 | pos += m_pairSep.length(); |
Yann | 0:f30e2135b0db | 160 | |
Yann | 0:f30e2135b0db | 161 | //Append elenent |
Yann | 0:f30e2135b0db | 162 | Dictionary::operator[]( Url::decode( m_buf.substr(key_pos, key_len) ) ) = Url::decode( m_buf.substr(value_pos, value_len) ); |
Yann | 0:f30e2135b0db | 163 | } |
Yann | 0:f30e2135b0db | 164 | |
Yann | 0:f30e2135b0db | 165 | m_buf.clear(); //Free data |
Yann | 0:f30e2135b0db | 166 | |
Yann | 0:f30e2135b0db | 167 | } |