FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

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