Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
m2mstringbufferbase.cpp
00001 /* 00002 * Copyright (c) 2016 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed-client/m2mstringbufferbase.h" 00018 00019 #include "mbed-client/m2mstring.h" 00020 00021 #include <assert.h> 00022 #include <string.h> 00023 00024 00025 bool StringBufferBase::ensure_space(size_t max_size, size_t required_size) const 00026 { 00027 const size_t space_left = max_size - _curr_size; 00028 00029 bool space_available = false; 00030 00031 if (required_size <= space_left) { 00032 00033 space_available = true; 00034 } 00035 return space_available; 00036 } 00037 00038 bool StringBufferBase::append(char *buff, size_t max_size, char data) 00039 { 00040 bool space_available = ensure_space(max_size, 1 + 1); // there must be space for trailing zero too 00041 if (space_available) { 00042 buff[_curr_size++] = data; 00043 buff[_curr_size] = '\0'; 00044 assert(_curr_size < max_size); 00045 } 00046 return space_available; 00047 } 00048 00049 bool StringBufferBase::append(char *buff, size_t max_size, const char *data) 00050 { 00051 00052 const size_t string_len = strlen(data); 00053 bool space_available = ensure_space(max_size, string_len + 1); 00054 if (space_available) { 00055 memcpy(buff + _curr_size, data, string_len + 1); // copy the zero terminator too 00056 _curr_size += string_len; 00057 assert(_curr_size < max_size); 00058 } 00059 return space_available; 00060 } 00061 00062 bool StringBufferBase::append(char *buff, size_t max_size, const char *data, size_t data_len) 00063 { 00064 bool space_available = true; 00065 if (data_len > 0) { 00066 space_available = ensure_space(max_size, data_len + 1); 00067 if (space_available) { 00068 memcpy(buff + _curr_size, data, data_len); 00069 _curr_size += data_len; 00070 // Todo: should the code actually check, if the data already contained zero or not? 00071 buff[_curr_size] = '\0'; 00072 assert(_curr_size < max_size); 00073 } 00074 } 00075 return space_available; 00076 } 00077 00078 bool StringBufferBase::append_int(char *buff, size_t max_size, uint16_t data) 00079 { 00080 // max len of "-9223372036854775808" plus zero termination 00081 char conv_buff[20+1]; 00082 00083 // re-use the String's functionality, a more optimal version would use snprintf() or int size specific converter 00084 unsigned int len = m2m::itoa_c(data, conv_buff); 00085 00086 return append(buff, max_size, conv_buff, len); 00087 } 00088 00089 int StringBufferBase::find_last_of(const char *buff, char search_char) const 00090 { 00091 int last_index = -1; 00092 // search from the end of string, return upon first found matching char 00093 for (int index = (int)_curr_size; index >= 0; index--) { 00094 if (buff[index] == search_char) { 00095 last_index = index; 00096 break; 00097 } 00098 } 00099 00100 return last_index; 00101 }
Generated on Mon Aug 29 2022 19:53:40 by
