FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:35:07 2017 +0000
Revision:
0:a2cb7295a1f7
Initial commit

Who changed what in which revision?

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