leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

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