Toyomasa Watarai / simple-mbed-cloud-client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 06:30:39 2018 +0000
Revision:
0:276e7a263c35
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:276e7a263c35 1 /*
MACRUM 0:276e7a263c35 2 * Copyright (c) 2016 ARM Limited. All rights reserved.
MACRUM 0:276e7a263c35 3 * SPDX-License-Identifier: Apache-2.0
MACRUM 0:276e7a263c35 4 * Licensed under the Apache License, Version 2.0 (the License); you may
MACRUM 0:276e7a263c35 5 * not use this file except in compliance with the License.
MACRUM 0:276e7a263c35 6 * You may obtain a copy of the License at
MACRUM 0:276e7a263c35 7 *
MACRUM 0:276e7a263c35 8 * http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:276e7a263c35 9 *
MACRUM 0:276e7a263c35 10 * Unless required by applicable law or agreed to in writing, software
MACRUM 0:276e7a263c35 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
MACRUM 0:276e7a263c35 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:276e7a263c35 13 * See the License for the specific language governing permissions and
MACRUM 0:276e7a263c35 14 * limitations under the License.
MACRUM 0:276e7a263c35 15 */
MACRUM 0:276e7a263c35 16
MACRUM 0:276e7a263c35 17 #include "mbed-client/m2mstringbufferbase.h"
MACRUM 0:276e7a263c35 18
MACRUM 0:276e7a263c35 19 #include "mbed-client/m2mstring.h"
MACRUM 0:276e7a263c35 20
MACRUM 0:276e7a263c35 21 #include <assert.h>
MACRUM 0:276e7a263c35 22 #include <string.h>
MACRUM 0:276e7a263c35 23
MACRUM 0:276e7a263c35 24
MACRUM 0:276e7a263c35 25 bool StringBufferBase::ensure_space(size_t max_size, size_t required_size) const
MACRUM 0:276e7a263c35 26 {
MACRUM 0:276e7a263c35 27 const size_t space_left = max_size - _curr_size;
MACRUM 0:276e7a263c35 28
MACRUM 0:276e7a263c35 29 bool space_available = false;
MACRUM 0:276e7a263c35 30
MACRUM 0:276e7a263c35 31 if (required_size <= space_left) {
MACRUM 0:276e7a263c35 32
MACRUM 0:276e7a263c35 33 space_available = true;
MACRUM 0:276e7a263c35 34 }
MACRUM 0:276e7a263c35 35 return space_available;
MACRUM 0:276e7a263c35 36 }
MACRUM 0:276e7a263c35 37
MACRUM 0:276e7a263c35 38 bool StringBufferBase::append(char *buff, size_t max_size, char data)
MACRUM 0:276e7a263c35 39 {
MACRUM 0:276e7a263c35 40 bool space_available = ensure_space(max_size, 1 + 1); // there must be space for trailing zero too
MACRUM 0:276e7a263c35 41 if (space_available) {
MACRUM 0:276e7a263c35 42 buff[_curr_size++] = data;
MACRUM 0:276e7a263c35 43 buff[_curr_size] = '\0';
MACRUM 0:276e7a263c35 44 assert(_curr_size < max_size);
MACRUM 0:276e7a263c35 45 }
MACRUM 0:276e7a263c35 46 return space_available;
MACRUM 0:276e7a263c35 47 }
MACRUM 0:276e7a263c35 48
MACRUM 0:276e7a263c35 49 bool StringBufferBase::append(char *buff, size_t max_size, const char *data)
MACRUM 0:276e7a263c35 50 {
MACRUM 0:276e7a263c35 51
MACRUM 0:276e7a263c35 52 const size_t string_len = strlen(data);
MACRUM 0:276e7a263c35 53 bool space_available = ensure_space(max_size, string_len + 1);
MACRUM 0:276e7a263c35 54 if (space_available) {
MACRUM 0:276e7a263c35 55 memcpy(buff + _curr_size, data, string_len + 1); // copy the zero terminator too
MACRUM 0:276e7a263c35 56 _curr_size += string_len;
MACRUM 0:276e7a263c35 57 assert(_curr_size < max_size);
MACRUM 0:276e7a263c35 58 }
MACRUM 0:276e7a263c35 59 return space_available;
MACRUM 0:276e7a263c35 60 }
MACRUM 0:276e7a263c35 61
MACRUM 0:276e7a263c35 62 bool StringBufferBase::append(char *buff, size_t max_size, const char *data, size_t data_len)
MACRUM 0:276e7a263c35 63 {
MACRUM 0:276e7a263c35 64 bool space_available = true;
MACRUM 0:276e7a263c35 65 if (data_len > 0) {
MACRUM 0:276e7a263c35 66 space_available = ensure_space(max_size, data_len + 1);
MACRUM 0:276e7a263c35 67 if (space_available) {
MACRUM 0:276e7a263c35 68 memcpy(buff + _curr_size, data, data_len);
MACRUM 0:276e7a263c35 69 _curr_size += data_len;
MACRUM 0:276e7a263c35 70 // Todo: should the code actually check, if the data already contained zero or not?
MACRUM 0:276e7a263c35 71 buff[_curr_size] = '\0';
MACRUM 0:276e7a263c35 72 assert(_curr_size < max_size);
MACRUM 0:276e7a263c35 73 }
MACRUM 0:276e7a263c35 74 }
MACRUM 0:276e7a263c35 75 return space_available;
MACRUM 0:276e7a263c35 76 }
MACRUM 0:276e7a263c35 77
MACRUM 0:276e7a263c35 78 bool StringBufferBase::append_int(char *buff, size_t max_size, uint16_t data)
MACRUM 0:276e7a263c35 79 {
MACRUM 0:276e7a263c35 80 // max len of "-9223372036854775808" plus zero termination
MACRUM 0:276e7a263c35 81 char conv_buff[20+1];
MACRUM 0:276e7a263c35 82
MACRUM 0:276e7a263c35 83 // re-use the String's functionality, a more optimal version would use snprintf() or int size specific converter
MACRUM 0:276e7a263c35 84 unsigned int len = m2m::itoa_c(data, conv_buff);
MACRUM 0:276e7a263c35 85
MACRUM 0:276e7a263c35 86 return append(buff, max_size, conv_buff, len);
MACRUM 0:276e7a263c35 87 }
MACRUM 0:276e7a263c35 88
MACRUM 0:276e7a263c35 89 int StringBufferBase::find_last_of(const char *buff, char search_char) const
MACRUM 0:276e7a263c35 90 {
MACRUM 0:276e7a263c35 91 int last_index = -1;
MACRUM 0:276e7a263c35 92 // search from the end of string, return upon first found matching char
MACRUM 0:276e7a263c35 93 for (int index = (int)_curr_size; index >= 0; index--) {
MACRUM 0:276e7a263c35 94 if (buff[index] == search_char) {
MACRUM 0:276e7a263c35 95 last_index = index;
MACRUM 0:276e7a263c35 96 break;
MACRUM 0:276e7a263c35 97 }
MACRUM 0:276e7a263c35 98 }
MACRUM 0:276e7a263c35 99
MACRUM 0:276e7a263c35 100 return last_index;
MACRUM 0:276e7a263c35 101 }