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.
m2mstringbuffer.h
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 #ifndef __STRING_BUFFER_H__ 00017 #define __STRING_BUFFER_H__ 00018 00019 #include "mbed-client/m2mstringbufferbase.h" 00020 00021 #include <assert.h> 00022 #include <stddef.h> 00023 00024 /*! \file m2mstringbuffer.h 00025 * \brief StringBuffer. 00026 * This class performs common string concatenation operations on a fixed-sized buffers. 00027 */ 00028 00029 template <int SIZE> 00030 class StringBuffer : private StringBufferBase 00031 { 00032 public: 00033 /** 00034 * Initialize a empty buffer with zero length and zero content. 00035 */ 00036 inline StringBuffer(); 00037 00038 // 00039 // This is not implemented on purpose, as the given string may conflict with 00040 // templated size. Otoh, if we used compile time assert, the overflow 00041 // could be prevented at compile time. 00042 // 00043 // inline StringBuffer(const char *initial_string); 00044 00045 /** 00046 * Verify, if the buffer has still room for given amount of bytes. 00047 * Note: the given size value must include the zero terminator as it is not 00048 * implicitly taken into account for. 00049 */ 00050 bool ensure_space(size_t required_size) const; 00051 00052 /** 00053 * Append given char to end of string. 00054 * Return false if the buffer would overflow, true otherwise. 00055 */ 00056 bool append(char data); 00057 00058 /** 00059 * Append given zero terminated string to end of buffer. 00060 * 00061 * Return false if the buffer would overflow, true otherwise. 00062 * 00063 * Note: the whole string, including the zero terminator must fit 00064 * to buffer or the append operation is not done and false is returned. 00065 */ 00066 bool append(const char *data); 00067 00068 /** 00069 * Append given block of chars to end of buffer. 00070 * 00071 * Return false if the buffer would overflow, true otherwise. 00072 * 00073 * Note: the whole string, including the zero terminator must fit 00074 * to buffer or the append operation is not done and false is returned. 00075 */ 00076 bool append(const char *data, size_t data_len); 00077 00078 /** 00079 * Convert given uint16_t into string representation and add it to the 00080 * end of buffer. 00081 * 00082 * Note: the whole string, including the zero terminator must fit 00083 * to buffer or the append operation is not done and false is returned. 00084 */ 00085 bool append_int(uint16_t data); 00086 00087 /** 00088 * Get the amount of bytes added to the buffer. 00089 * 00090 * Note: the size does not include the terminating zero, so this is 00091 * functionally equal to strlen(). 00092 */ 00093 inline size_t get_size() const; 00094 00095 // API functionality copied from m2mstring: 00096 00097 // find the index of last occurance of given char in string, or negative if not found 00098 int find_last_of(char search_char) const; 00099 00100 /** 00101 * Get a read only pointer to the data. 00102 */ 00103 inline const char* c_str() const; 00104 00105 // Add this only if needed 00106 //inline char* c_str(); 00107 private: 00108 char _buff[SIZE]; 00109 }; 00110 00111 template <int SIZE> 00112 inline StringBuffer<SIZE>::StringBuffer() 00113 { 00114 // actually a assert_compile() would be better as this is completely a code problem 00115 assert(SIZE > 0); 00116 00117 _buff[0] = '\0'; 00118 } 00119 00120 template <int SIZE> 00121 bool StringBuffer<SIZE>::ensure_space(size_t required_size) const 00122 { 00123 return StringBufferBase::ensure_space(SIZE, required_size); 00124 } 00125 00126 template <int SIZE> 00127 bool StringBuffer<SIZE>::append(const char *data) 00128 { 00129 return StringBufferBase::append(_buff, SIZE, data); 00130 } 00131 00132 template <int SIZE> 00133 bool StringBuffer<SIZE>::append(const char *data, size_t data_len) 00134 { 00135 return StringBufferBase::append(_buff, SIZE, data, data_len); 00136 } 00137 00138 template <int SIZE> 00139 inline bool StringBuffer<SIZE>::append(char data) 00140 { 00141 return StringBufferBase::append(_buff, SIZE, data); 00142 } 00143 00144 template <int SIZE> 00145 bool StringBuffer<SIZE>::append_int(uint16_t data) 00146 { 00147 return StringBufferBase::append_int(_buff, SIZE, data); 00148 } 00149 00150 template <int SIZE> 00151 int StringBuffer<SIZE>::find_last_of(char search_char) const 00152 { 00153 return StringBufferBase::find_last_of(_buff, search_char); 00154 } 00155 00156 template <int SIZE> 00157 inline const char* StringBuffer<SIZE>::c_str() const 00158 { 00159 return _buff; 00160 } 00161 00162 template <int SIZE> 00163 inline size_t StringBuffer<SIZE>::get_size() const 00164 { 00165 return _curr_size; 00166 } 00167 00168 #endif // !__STRING_BUFFER_H__
Generated on Mon Aug 29 2022 19:53:40 by
