This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).
Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn
The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/
mbed-client/source/m2mstring.cpp@2:b894b3508057, 2017-09-05 (annotated)
- Committer:
- Ren Boting
- Date:
- Tue Sep 05 11:56:13 2017 +0900
- Revision:
- 2:b894b3508057
- Parent:
- 0:29983394c6b6
Update all libraries and reform main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
edamame22 | 0:29983394c6b6 | 1 | /* |
edamame22 | 0:29983394c6b6 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
edamame22 | 0:29983394c6b6 | 3 | * SPDX-License-Identifier: Apache-2.0 |
edamame22 | 0:29983394c6b6 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
edamame22 | 0:29983394c6b6 | 5 | * not use this file except in compliance with the License. |
edamame22 | 0:29983394c6b6 | 6 | * You may obtain a copy of the License at |
edamame22 | 0:29983394c6b6 | 7 | * |
edamame22 | 0:29983394c6b6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
edamame22 | 0:29983394c6b6 | 9 | * |
edamame22 | 0:29983394c6b6 | 10 | * Unless required by applicable law or agreed to in writing, software |
edamame22 | 0:29983394c6b6 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
edamame22 | 0:29983394c6b6 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
edamame22 | 0:29983394c6b6 | 13 | * See the License for the specific language governing permissions and |
edamame22 | 0:29983394c6b6 | 14 | * limitations under the License. |
edamame22 | 0:29983394c6b6 | 15 | */ |
edamame22 | 0:29983394c6b6 | 16 | #include "mbed-client/m2mstring.h" |
edamame22 | 0:29983394c6b6 | 17 | #include <string.h> // strlen |
edamame22 | 0:29983394c6b6 | 18 | #include <stdlib.h> // malloc, realloc |
edamame22 | 0:29983394c6b6 | 19 | #include <assert.h> |
edamame22 | 0:29983394c6b6 | 20 | #include <algorithm> // min |
edamame22 | 0:29983394c6b6 | 21 | |
edamame22 | 0:29983394c6b6 | 22 | namespace m2m { |
edamame22 | 0:29983394c6b6 | 23 | |
edamame22 | 0:29983394c6b6 | 24 | const String::size_type String::npos = static_cast<size_t>(-1); |
edamame22 | 0:29983394c6b6 | 25 | |
edamame22 | 0:29983394c6b6 | 26 | char* String::strdup(const char* s) |
edamame22 | 0:29983394c6b6 | 27 | { |
edamame22 | 0:29983394c6b6 | 28 | const size_t len = strlen(s)+1; |
edamame22 | 0:29983394c6b6 | 29 | char *p2 = static_cast<char*>(malloc(len)); |
edamame22 | 0:29983394c6b6 | 30 | memcpy(p2, s, len); |
edamame22 | 0:29983394c6b6 | 31 | allocated_ = len; |
edamame22 | 0:29983394c6b6 | 32 | size_ = len-1; |
edamame22 | 0:29983394c6b6 | 33 | return p2; |
edamame22 | 0:29983394c6b6 | 34 | } |
edamame22 | 0:29983394c6b6 | 35 | |
edamame22 | 0:29983394c6b6 | 36 | String::String() |
edamame22 | 0:29983394c6b6 | 37 | : p( strdup("") ) |
edamame22 | 0:29983394c6b6 | 38 | { |
edamame22 | 0:29983394c6b6 | 39 | } |
edamame22 | 0:29983394c6b6 | 40 | |
edamame22 | 0:29983394c6b6 | 41 | String::~String() |
edamame22 | 0:29983394c6b6 | 42 | { |
edamame22 | 0:29983394c6b6 | 43 | free(p); |
edamame22 | 0:29983394c6b6 | 44 | p = 0; |
edamame22 | 0:29983394c6b6 | 45 | } |
edamame22 | 0:29983394c6b6 | 46 | |
edamame22 | 0:29983394c6b6 | 47 | String::String(const String& s) |
edamame22 | 0:29983394c6b6 | 48 | : p(0) |
edamame22 | 0:29983394c6b6 | 49 | { |
edamame22 | 0:29983394c6b6 | 50 | if( &s != NULL ) { |
edamame22 | 0:29983394c6b6 | 51 | p = static_cast<char*>(malloc(s.size_ + 1)); |
edamame22 | 0:29983394c6b6 | 52 | |
edamame22 | 0:29983394c6b6 | 53 | allocated_ = s.size_ + 1; |
edamame22 | 0:29983394c6b6 | 54 | size_ = s.size_; |
edamame22 | 0:29983394c6b6 | 55 | memcpy(p, s.p, size_ + 1); |
edamame22 | 0:29983394c6b6 | 56 | } |
edamame22 | 0:29983394c6b6 | 57 | } |
edamame22 | 0:29983394c6b6 | 58 | |
edamame22 | 0:29983394c6b6 | 59 | String::String(const char* s) |
edamame22 | 0:29983394c6b6 | 60 | : p(strdup(s)) |
edamame22 | 0:29983394c6b6 | 61 | { |
edamame22 | 0:29983394c6b6 | 62 | } |
edamame22 | 0:29983394c6b6 | 63 | |
edamame22 | 0:29983394c6b6 | 64 | String::String(const char* str, size_t n) |
edamame22 | 0:29983394c6b6 | 65 | { |
edamame22 | 0:29983394c6b6 | 66 | p = static_cast<char*>(malloc(n + 1)); |
edamame22 | 0:29983394c6b6 | 67 | |
edamame22 | 0:29983394c6b6 | 68 | allocated_ = n + 1; |
edamame22 | 0:29983394c6b6 | 69 | size_ = n; |
edamame22 | 0:29983394c6b6 | 70 | memcpy(p, str, n); |
edamame22 | 0:29983394c6b6 | 71 | p[n] = 0; |
edamame22 | 0:29983394c6b6 | 72 | } |
edamame22 | 0:29983394c6b6 | 73 | |
edamame22 | 0:29983394c6b6 | 74 | String& String::operator=(const char* s) |
edamame22 | 0:29983394c6b6 | 75 | { |
edamame22 | 0:29983394c6b6 | 76 | if ( p != s ) { |
edamame22 | 0:29983394c6b6 | 77 | // s could point into our own string, so we have to allocate a new string |
edamame22 | 0:29983394c6b6 | 78 | const size_t len = strlen(s); |
edamame22 | 0:29983394c6b6 | 79 | char* copy = (char*) malloc( len + 1); |
edamame22 | 0:29983394c6b6 | 80 | memmove(copy, s, len+1); // trailing 0 |
edamame22 | 0:29983394c6b6 | 81 | free( p ); |
edamame22 | 0:29983394c6b6 | 82 | p = copy; |
edamame22 | 0:29983394c6b6 | 83 | size_ = len; |
edamame22 | 0:29983394c6b6 | 84 | allocated_ = len+1; |
edamame22 | 0:29983394c6b6 | 85 | } |
edamame22 | 0:29983394c6b6 | 86 | return *this; |
edamame22 | 0:29983394c6b6 | 87 | } |
edamame22 | 0:29983394c6b6 | 88 | |
edamame22 | 0:29983394c6b6 | 89 | String& String::operator=(const String& s) |
edamame22 | 0:29983394c6b6 | 90 | { |
edamame22 | 0:29983394c6b6 | 91 | return operator=(s.p); |
edamame22 | 0:29983394c6b6 | 92 | } |
edamame22 | 0:29983394c6b6 | 93 | |
edamame22 | 0:29983394c6b6 | 94 | String& String::operator+=(const String& s) |
edamame22 | 0:29983394c6b6 | 95 | { |
edamame22 | 0:29983394c6b6 | 96 | if (s.size_ > 0) { |
edamame22 | 0:29983394c6b6 | 97 | this->reserve(size_ + s.size_); |
edamame22 | 0:29983394c6b6 | 98 | memmove(p+size_, s.p, s.size_+1); // trailing 0 |
edamame22 | 0:29983394c6b6 | 99 | size_ += s.size_; |
edamame22 | 0:29983394c6b6 | 100 | } |
edamame22 | 0:29983394c6b6 | 101 | return *this; |
edamame22 | 0:29983394c6b6 | 102 | } |
edamame22 | 0:29983394c6b6 | 103 | |
edamame22 | 0:29983394c6b6 | 104 | // since p and s may overlap, we have to copy our own string first |
edamame22 | 0:29983394c6b6 | 105 | String& String::operator+=(const char* s) |
edamame22 | 0:29983394c6b6 | 106 | { |
edamame22 | 0:29983394c6b6 | 107 | const size_type lens = strlen(s); |
edamame22 | 0:29983394c6b6 | 108 | if (lens > 0) { |
edamame22 | 0:29983394c6b6 | 109 | if (size_ + lens + 1 <= allocated_) { |
edamame22 | 0:29983394c6b6 | 110 | memmove(p+size_, s, lens+1); // trailing 0 |
edamame22 | 0:29983394c6b6 | 111 | size_ += lens; |
edamame22 | 0:29983394c6b6 | 112 | } else { |
edamame22 | 0:29983394c6b6 | 113 | String s2( *this ); // copy own data |
edamame22 | 0:29983394c6b6 | 114 | s2.reserve(size_ + lens); |
edamame22 | 0:29983394c6b6 | 115 | memmove(s2.p+size_, s, lens+1); // trailing 0 |
edamame22 | 0:29983394c6b6 | 116 | s2.size_ = size_ + lens; |
edamame22 | 0:29983394c6b6 | 117 | this->swap( s2 ); |
edamame22 | 0:29983394c6b6 | 118 | } |
edamame22 | 0:29983394c6b6 | 119 | } |
edamame22 | 0:29983394c6b6 | 120 | return *this; |
edamame22 | 0:29983394c6b6 | 121 | } |
edamame22 | 0:29983394c6b6 | 122 | |
edamame22 | 0:29983394c6b6 | 123 | String& String::operator+=(const char c) |
edamame22 | 0:29983394c6b6 | 124 | { |
edamame22 | 0:29983394c6b6 | 125 | push_back(c); |
edamame22 | 0:29983394c6b6 | 126 | return *this; |
edamame22 | 0:29983394c6b6 | 127 | } |
edamame22 | 0:29983394c6b6 | 128 | |
edamame22 | 0:29983394c6b6 | 129 | void String::push_back(const char c) { |
edamame22 | 0:29983394c6b6 | 130 | |
edamame22 | 0:29983394c6b6 | 131 | if (size_ == allocated_ - 1) { |
edamame22 | 0:29983394c6b6 | 132 | size_t more = (allocated_* 3) / 2; // factor 1.5 |
edamame22 | 0:29983394c6b6 | 133 | if ( more < 4 ) more = 4; |
edamame22 | 0:29983394c6b6 | 134 | reserve( size_ + more ); |
edamame22 | 0:29983394c6b6 | 135 | } |
edamame22 | 0:29983394c6b6 | 136 | |
edamame22 | 0:29983394c6b6 | 137 | p[size_] = c; |
edamame22 | 0:29983394c6b6 | 138 | size_++; |
edamame22 | 0:29983394c6b6 | 139 | p[size_] = 0; |
edamame22 | 0:29983394c6b6 | 140 | } |
edamame22 | 0:29983394c6b6 | 141 | |
edamame22 | 0:29983394c6b6 | 142 | bool String::operator==(const char* s) const |
edamame22 | 0:29983394c6b6 | 143 | { |
edamame22 | 0:29983394c6b6 | 144 | if( s == NULL ) { |
edamame22 | 0:29983394c6b6 | 145 | if( p == NULL ) { |
edamame22 | 0:29983394c6b6 | 146 | return true; |
edamame22 | 0:29983394c6b6 | 147 | } |
edamame22 | 0:29983394c6b6 | 148 | return false; |
edamame22 | 0:29983394c6b6 | 149 | } |
edamame22 | 0:29983394c6b6 | 150 | bool ret = strcmp(p, s); |
edamame22 | 0:29983394c6b6 | 151 | return !ret; |
edamame22 | 0:29983394c6b6 | 152 | } |
edamame22 | 0:29983394c6b6 | 153 | |
edamame22 | 0:29983394c6b6 | 154 | bool String::operator==(const String& s) const |
edamame22 | 0:29983394c6b6 | 155 | { |
edamame22 | 0:29983394c6b6 | 156 | bool ret = strcmp(p, s.p); |
edamame22 | 0:29983394c6b6 | 157 | return !ret; |
edamame22 | 0:29983394c6b6 | 158 | } |
edamame22 | 0:29983394c6b6 | 159 | |
edamame22 | 0:29983394c6b6 | 160 | void String::clear() |
edamame22 | 0:29983394c6b6 | 161 | { |
edamame22 | 0:29983394c6b6 | 162 | size_ = 0; |
edamame22 | 0:29983394c6b6 | 163 | p[0] = 0; |
edamame22 | 0:29983394c6b6 | 164 | } |
edamame22 | 0:29983394c6b6 | 165 | |
edamame22 | 0:29983394c6b6 | 166 | String String::substr(const size_type pos, size_type length) const |
edamame22 | 0:29983394c6b6 | 167 | { |
edamame22 | 0:29983394c6b6 | 168 | String s; |
edamame22 | 0:29983394c6b6 | 169 | const size_type len = size_; |
edamame22 | 0:29983394c6b6 | 170 | |
edamame22 | 0:29983394c6b6 | 171 | if ( pos <= len ) { |
edamame22 | 0:29983394c6b6 | 172 | |
edamame22 | 0:29983394c6b6 | 173 | size_type remain = len - pos; |
edamame22 | 0:29983394c6b6 | 174 | |
edamame22 | 0:29983394c6b6 | 175 | if ( length > remain ) |
edamame22 | 0:29983394c6b6 | 176 | length = remain; |
edamame22 | 0:29983394c6b6 | 177 | |
edamame22 | 0:29983394c6b6 | 178 | s.reserve( length ); |
edamame22 | 0:29983394c6b6 | 179 | |
edamame22 | 0:29983394c6b6 | 180 | memcpy(s.p, p + pos, length); |
edamame22 | 0:29983394c6b6 | 181 | s.p[length] = '\0'; |
edamame22 | 0:29983394c6b6 | 182 | s.size_ = length; |
edamame22 | 0:29983394c6b6 | 183 | } |
edamame22 | 0:29983394c6b6 | 184 | return s; |
edamame22 | 0:29983394c6b6 | 185 | } |
edamame22 | 0:29983394c6b6 | 186 | |
edamame22 | 0:29983394c6b6 | 187 | |
edamame22 | 0:29983394c6b6 | 188 | // checked access, accessing the NUL at end is allowed |
edamame22 | 0:29983394c6b6 | 189 | char String::at(const size_type i) const |
edamame22 | 0:29983394c6b6 | 190 | { |
edamame22 | 0:29983394c6b6 | 191 | if ( i <= strlen(p) ) { |
edamame22 | 0:29983394c6b6 | 192 | return p[i]; |
edamame22 | 0:29983394c6b6 | 193 | } else { |
edamame22 | 0:29983394c6b6 | 194 | return '\0'; |
edamame22 | 0:29983394c6b6 | 195 | } |
edamame22 | 0:29983394c6b6 | 196 | } |
edamame22 | 0:29983394c6b6 | 197 | |
edamame22 | 0:29983394c6b6 | 198 | String& String::erase(size_type pos, size_type len) |
edamame22 | 0:29983394c6b6 | 199 | { |
edamame22 | 0:29983394c6b6 | 200 | if (len > 0) { |
edamame22 | 0:29983394c6b6 | 201 | |
edamame22 | 0:29983394c6b6 | 202 | if ( pos < size_ ) { // user must not remove trailing 0 |
edamame22 | 0:29983394c6b6 | 203 | |
edamame22 | 0:29983394c6b6 | 204 | size_type s2 = size_; |
edamame22 | 0:29983394c6b6 | 205 | size_type remain = s2 - pos - len; |
edamame22 | 0:29983394c6b6 | 206 | |
edamame22 | 0:29983394c6b6 | 207 | if (remain > 0) { |
edamame22 | 0:29983394c6b6 | 208 | // erase by overwriting |
edamame22 | 0:29983394c6b6 | 209 | memmove(p + pos, p + pos + len, remain); |
edamame22 | 0:29983394c6b6 | 210 | } |
edamame22 | 0:29983394c6b6 | 211 | |
edamame22 | 0:29983394c6b6 | 212 | //if ( remain < 0 ) remain = 0; |
edamame22 | 0:29983394c6b6 | 213 | |
edamame22 | 0:29983394c6b6 | 214 | // remove unused space |
edamame22 | 0:29983394c6b6 | 215 | this->resize( pos+remain ); |
edamame22 | 0:29983394c6b6 | 216 | |
edamame22 | 0:29983394c6b6 | 217 | } |
edamame22 | 0:29983394c6b6 | 218 | } |
edamame22 | 0:29983394c6b6 | 219 | return *this; |
edamame22 | 0:29983394c6b6 | 220 | } |
edamame22 | 0:29983394c6b6 | 221 | |
edamame22 | 0:29983394c6b6 | 222 | String& String::append( const char* str, size_type n) { |
edamame22 | 0:29983394c6b6 | 223 | if (str && n > 0) { |
edamame22 | 0:29983394c6b6 | 224 | size_t lens = strlen(str); |
edamame22 | 0:29983394c6b6 | 225 | if (n > lens) |
edamame22 | 0:29983394c6b6 | 226 | n = lens; |
edamame22 | 0:29983394c6b6 | 227 | size_t newlen = size_ + n; |
edamame22 | 0:29983394c6b6 | 228 | this->reserve( newlen ); |
edamame22 | 0:29983394c6b6 | 229 | memmove(p+size_, str, n); // p and s.p MAY overlap |
edamame22 | 0:29983394c6b6 | 230 | p[newlen] = 0; // add NUL termination |
edamame22 | 0:29983394c6b6 | 231 | size_ = newlen; |
edamame22 | 0:29983394c6b6 | 232 | } |
edamame22 | 0:29983394c6b6 | 233 | return *this; |
edamame22 | 0:29983394c6b6 | 234 | } |
edamame22 | 0:29983394c6b6 | 235 | |
edamame22 | 0:29983394c6b6 | 236 | String& String::append_raw( const char* str, size_type n) { |
edamame22 | 0:29983394c6b6 | 237 | if (str && n > 0) { |
edamame22 | 0:29983394c6b6 | 238 | size_t newlen = size_ + n; |
edamame22 | 0:29983394c6b6 | 239 | this->reserve( newlen ); |
edamame22 | 0:29983394c6b6 | 240 | memmove(p+size_, str, n); // p and s.p MAY overlap |
edamame22 | 0:29983394c6b6 | 241 | p[newlen] = 0; // add NUL termination |
edamame22 | 0:29983394c6b6 | 242 | size_ = newlen; |
edamame22 | 0:29983394c6b6 | 243 | } |
edamame22 | 0:29983394c6b6 | 244 | return *this; |
edamame22 | 0:29983394c6b6 | 245 | } |
edamame22 | 0:29983394c6b6 | 246 | |
edamame22 | 0:29983394c6b6 | 247 | void String::append_int(int param) { |
edamame22 | 0:29983394c6b6 | 248 | |
edamame22 | 0:29983394c6b6 | 249 | // max len of "-9223372036854775808" plus zero termination |
edamame22 | 0:29983394c6b6 | 250 | char conv_buff[20+1]; |
edamame22 | 0:29983394c6b6 | 251 | |
edamame22 | 0:29983394c6b6 | 252 | int len = itoa_c(param, conv_buff); |
edamame22 | 0:29983394c6b6 | 253 | append_raw(conv_buff, len); |
edamame22 | 0:29983394c6b6 | 254 | } |
edamame22 | 0:29983394c6b6 | 255 | |
edamame22 | 0:29983394c6b6 | 256 | int String::compare( size_type pos, size_type len, const String& str ) const { |
edamame22 | 0:29983394c6b6 | 257 | int r = -1; |
edamame22 | 0:29983394c6b6 | 258 | if (pos <= size_) { |
edamame22 | 0:29983394c6b6 | 259 | if ( len > size_ - pos) |
edamame22 | 0:29983394c6b6 | 260 | len = size_ - pos; // limit len to available length |
edamame22 | 0:29983394c6b6 | 261 | |
edamame22 | 0:29983394c6b6 | 262 | const size_type osize = str.size(); |
edamame22 | 0:29983394c6b6 | 263 | const size_type len2 = std::min(len, osize); |
edamame22 | 0:29983394c6b6 | 264 | r = strncmp( p + pos, str.p, len2); |
edamame22 | 0:29983394c6b6 | 265 | if (r==0) // equal so far, now compare sizes |
edamame22 | 0:29983394c6b6 | 266 | r = len < osize ? -1 : ( len == osize ? 0 : +1 ); |
edamame22 | 0:29983394c6b6 | 267 | } |
edamame22 | 0:29983394c6b6 | 268 | return r; |
edamame22 | 0:29983394c6b6 | 269 | } |
edamame22 | 0:29983394c6b6 | 270 | |
edamame22 | 0:29983394c6b6 | 271 | int String::compare( size_type pos, size_type len, const char* str ) const { |
edamame22 | 0:29983394c6b6 | 272 | int r = -1; |
edamame22 | 0:29983394c6b6 | 273 | if (pos <= size_) { |
edamame22 | 0:29983394c6b6 | 274 | |
edamame22 | 0:29983394c6b6 | 275 | if ( len > size_ - pos) |
edamame22 | 0:29983394c6b6 | 276 | len = size_ - pos; // limit len to available length |
edamame22 | 0:29983394c6b6 | 277 | |
edamame22 | 0:29983394c6b6 | 278 | const size_type osize = strlen(str); |
edamame22 | 0:29983394c6b6 | 279 | const size_type len2 = std::min(len, osize); |
edamame22 | 0:29983394c6b6 | 280 | r = strncmp( p + pos, str, len2); |
edamame22 | 0:29983394c6b6 | 281 | if (r==0) // equal so far, now compare sizes |
edamame22 | 0:29983394c6b6 | 282 | r = len < osize ? -1 : ( len == osize ? 0 : +1 ); |
edamame22 | 0:29983394c6b6 | 283 | } |
edamame22 | 0:29983394c6b6 | 284 | return r; |
edamame22 | 0:29983394c6b6 | 285 | } |
edamame22 | 0:29983394c6b6 | 286 | |
edamame22 | 0:29983394c6b6 | 287 | int String::find_last_of(char c) const { |
edamame22 | 0:29983394c6b6 | 288 | int r = -1; |
edamame22 | 0:29983394c6b6 | 289 | char *v; |
edamame22 | 0:29983394c6b6 | 290 | v = strrchr(p,c); |
edamame22 | 0:29983394c6b6 | 291 | if (v != NULL) { |
edamame22 | 0:29983394c6b6 | 292 | r = 0; |
edamame22 | 0:29983394c6b6 | 293 | char* i = p; |
edamame22 | 0:29983394c6b6 | 294 | while (v != i) { |
edamame22 | 0:29983394c6b6 | 295 | i++; |
edamame22 | 0:29983394c6b6 | 296 | r++; |
edamame22 | 0:29983394c6b6 | 297 | } |
edamame22 | 0:29983394c6b6 | 298 | } |
edamame22 | 0:29983394c6b6 | 299 | return r; |
edamame22 | 0:29983394c6b6 | 300 | } |
edamame22 | 0:29983394c6b6 | 301 | |
edamame22 | 0:29983394c6b6 | 302 | void String::new_realloc( size_type n) { |
edamame22 | 0:29983394c6b6 | 303 | if (n > 0 ) { |
edamame22 | 0:29983394c6b6 | 304 | char* pnew = static_cast<char*>(realloc(p, n)); // could return NULL |
edamame22 | 0:29983394c6b6 | 305 | if (pnew) |
edamame22 | 0:29983394c6b6 | 306 | p = pnew; |
edamame22 | 0:29983394c6b6 | 307 | } |
edamame22 | 0:29983394c6b6 | 308 | } |
edamame22 | 0:29983394c6b6 | 309 | |
edamame22 | 0:29983394c6b6 | 310 | void String::reserve( const size_type n) { |
edamame22 | 0:29983394c6b6 | 311 | if (n >= allocated_ ) { |
edamame22 | 0:29983394c6b6 | 312 | this->new_realloc(n + 1); |
edamame22 | 0:29983394c6b6 | 313 | allocated_ = n + 1; |
edamame22 | 0:29983394c6b6 | 314 | } |
edamame22 | 0:29983394c6b6 | 315 | } |
edamame22 | 0:29983394c6b6 | 316 | |
edamame22 | 0:29983394c6b6 | 317 | void String::resize( const size_type n) { |
edamame22 | 0:29983394c6b6 | 318 | this->resize( n, 0 ); |
edamame22 | 0:29983394c6b6 | 319 | } |
edamame22 | 0:29983394c6b6 | 320 | |
edamame22 | 0:29983394c6b6 | 321 | void String::resize( const size_type n, const char c) { |
edamame22 | 0:29983394c6b6 | 322 | if (n < size_ ) { |
edamame22 | 0:29983394c6b6 | 323 | p[n] = 0; |
edamame22 | 0:29983394c6b6 | 324 | size_ = n; |
edamame22 | 0:29983394c6b6 | 325 | } |
edamame22 | 0:29983394c6b6 | 326 | else if (n > size_ ) { |
edamame22 | 0:29983394c6b6 | 327 | this->reserve( n ); |
edamame22 | 0:29983394c6b6 | 328 | for (size_type i=size_; i < n; ++i ) |
edamame22 | 0:29983394c6b6 | 329 | p[i] = c; |
edamame22 | 0:29983394c6b6 | 330 | p[n] = 0; |
edamame22 | 0:29983394c6b6 | 331 | size_ = n; |
edamame22 | 0:29983394c6b6 | 332 | } |
edamame22 | 0:29983394c6b6 | 333 | } |
edamame22 | 0:29983394c6b6 | 334 | |
edamame22 | 0:29983394c6b6 | 335 | void String::swap( String& s ) { |
edamame22 | 0:29983394c6b6 | 336 | std::swap( allocated_, s.allocated_ ); |
edamame22 | 0:29983394c6b6 | 337 | std::swap( size_, s.size_ ); |
edamame22 | 0:29983394c6b6 | 338 | std::swap( p, s.p ); |
edamame22 | 0:29983394c6b6 | 339 | } |
edamame22 | 0:29983394c6b6 | 340 | |
edamame22 | 0:29983394c6b6 | 341 | |
edamame22 | 0:29983394c6b6 | 342 | // Comparison |
edamame22 | 0:29983394c6b6 | 343 | bool operator<( const String& s1, const String& s2 ) { |
edamame22 | 0:29983394c6b6 | 344 | return strcmp( s1.c_str(), s2.c_str() ) < 0; |
edamame22 | 0:29983394c6b6 | 345 | } |
edamame22 | 0:29983394c6b6 | 346 | |
edamame22 | 0:29983394c6b6 | 347 | void reverse(char s[], uint32_t length) |
edamame22 | 0:29983394c6b6 | 348 | { |
edamame22 | 0:29983394c6b6 | 349 | uint32_t i, j; |
edamame22 | 0:29983394c6b6 | 350 | char c; |
edamame22 | 0:29983394c6b6 | 351 | |
edamame22 | 0:29983394c6b6 | 352 | for (i = 0, j = length-1; i<j; i++, j--) { |
edamame22 | 0:29983394c6b6 | 353 | c = s[i]; |
edamame22 | 0:29983394c6b6 | 354 | s[i] = s[j]; |
edamame22 | 0:29983394c6b6 | 355 | s[j] = c; |
edamame22 | 0:29983394c6b6 | 356 | } |
edamame22 | 0:29983394c6b6 | 357 | } |
edamame22 | 0:29983394c6b6 | 358 | |
edamame22 | 0:29983394c6b6 | 359 | uint32_t itoa_c (int64_t n, char s[]) |
edamame22 | 0:29983394c6b6 | 360 | { |
edamame22 | 0:29983394c6b6 | 361 | int64_t sign; |
edamame22 | 0:29983394c6b6 | 362 | uint32_t i; |
edamame22 | 0:29983394c6b6 | 363 | |
edamame22 | 0:29983394c6b6 | 364 | if ((sign = n) < 0) |
edamame22 | 0:29983394c6b6 | 365 | n = -n; |
edamame22 | 0:29983394c6b6 | 366 | |
edamame22 | 0:29983394c6b6 | 367 | i = 0; |
edamame22 | 0:29983394c6b6 | 368 | |
edamame22 | 0:29983394c6b6 | 369 | do { |
edamame22 | 0:29983394c6b6 | 370 | s[i++] = n % 10 + '0'; |
edamame22 | 0:29983394c6b6 | 371 | } |
edamame22 | 0:29983394c6b6 | 372 | while ((n /= 10) > 0); |
edamame22 | 0:29983394c6b6 | 373 | |
edamame22 | 0:29983394c6b6 | 374 | if (sign < 0) |
edamame22 | 0:29983394c6b6 | 375 | s[i++] = '-'; |
edamame22 | 0:29983394c6b6 | 376 | |
edamame22 | 0:29983394c6b6 | 377 | s[i] = '\0'; |
edamame22 | 0:29983394c6b6 | 378 | |
edamame22 | 0:29983394c6b6 | 379 | m2m::reverse(s, i); |
edamame22 | 0:29983394c6b6 | 380 | return i; |
edamame22 | 0:29983394c6b6 | 381 | } |
edamame22 | 0:29983394c6b6 | 382 | |
edamame22 | 0:29983394c6b6 | 383 | uint8_t* String::convert_integer_to_array(int64_t value, uint8_t &size, uint8_t *array, uint32_t array_size) |
edamame22 | 0:29983394c6b6 | 384 | { |
edamame22 | 0:29983394c6b6 | 385 | uint8_t* buffer = NULL; |
edamame22 | 0:29983394c6b6 | 386 | size = 0; |
edamame22 | 0:29983394c6b6 | 387 | if (array) { |
edamame22 | 0:29983394c6b6 | 388 | value = String::convert_array_to_integer(array, array_size); |
edamame22 | 0:29983394c6b6 | 389 | } |
edamame22 | 0:29983394c6b6 | 390 | |
edamame22 | 0:29983394c6b6 | 391 | if(value < 0xFF) { |
edamame22 | 0:29983394c6b6 | 392 | size = 1; |
edamame22 | 0:29983394c6b6 | 393 | } else if(value < 0xFFFF) { |
edamame22 | 0:29983394c6b6 | 394 | size = 2; |
edamame22 | 0:29983394c6b6 | 395 | } else if(value < 0xFFFFFF) { |
edamame22 | 0:29983394c6b6 | 396 | size = 3; |
edamame22 | 0:29983394c6b6 | 397 | } else if(value < 0xFFFFFFFF) { |
edamame22 | 0:29983394c6b6 | 398 | size = 4; |
edamame22 | 0:29983394c6b6 | 399 | } else if(value < 0xFFFFFFFFFF) { |
edamame22 | 0:29983394c6b6 | 400 | size = 5; |
edamame22 | 0:29983394c6b6 | 401 | } else if(value < 0xFFFFFFFFFFFF) { |
edamame22 | 0:29983394c6b6 | 402 | size = 6; |
edamame22 | 0:29983394c6b6 | 403 | } else if(value < 0xFFFFFFFFFFFFFF) { |
edamame22 | 0:29983394c6b6 | 404 | size = 7; |
edamame22 | 0:29983394c6b6 | 405 | } else { |
edamame22 | 0:29983394c6b6 | 406 | size = 8; |
edamame22 | 0:29983394c6b6 | 407 | } |
edamame22 | 0:29983394c6b6 | 408 | |
edamame22 | 0:29983394c6b6 | 409 | buffer = (uint8_t*)malloc(size); |
edamame22 | 0:29983394c6b6 | 410 | if (buffer) { |
edamame22 | 0:29983394c6b6 | 411 | for (int i = 0; i < size; i++) { |
edamame22 | 0:29983394c6b6 | 412 | buffer[i] = (value >> ((size - i - 1) * 8)); |
edamame22 | 0:29983394c6b6 | 413 | } |
edamame22 | 0:29983394c6b6 | 414 | } else { |
edamame22 | 0:29983394c6b6 | 415 | size = 0; |
edamame22 | 0:29983394c6b6 | 416 | } |
edamame22 | 0:29983394c6b6 | 417 | return buffer; |
edamame22 | 0:29983394c6b6 | 418 | } |
edamame22 | 0:29983394c6b6 | 419 | |
edamame22 | 0:29983394c6b6 | 420 | int64_t String::convert_array_to_integer(uint8_t *value, uint32_t size) |
edamame22 | 0:29983394c6b6 | 421 | { |
edamame22 | 0:29983394c6b6 | 422 | int64_t temp_64 = 0; |
edamame22 | 0:29983394c6b6 | 423 | for (int i = size - 1; i >= 0; i--) { |
edamame22 | 0:29983394c6b6 | 424 | temp_64 += (uint64_t)(*value++) << i * 8; |
edamame22 | 0:29983394c6b6 | 425 | } |
edamame22 | 0:29983394c6b6 | 426 | return temp_64; |
edamame22 | 0:29983394c6b6 | 427 | } |
edamame22 | 0:29983394c6b6 | 428 | |
edamame22 | 0:29983394c6b6 | 429 | } // namespace |