aJson is the attempt to port a complete JSON implementation to Arduino. It is based on the cJSON implementation, reduced in size and removing one or two feature. The current mbed implementation only supports FILE* as input so you will have to use a temporary file for parsing your json input. https://github.com/interactive-matter/aJson
utility/stringbuffer.c@1:6df1d1f1b372, 2012-08-28 (annotated)
- Committer:
- mimil
- Date:
- Tue Aug 28 08:16:29 2012 +0000
- Revision:
- 1:6df1d1f1b372
- Parent:
- 0:428cf9a51873
first release of aJSON library for mbed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mimil | 1:6df1d1f1b372 | 1 | /* |
mimil | 1:6df1d1f1b372 | 2 | * aJson |
mimil | 1:6df1d1f1b372 | 3 | * stringbuffer.c |
mimil | 1:6df1d1f1b372 | 4 | * |
mimil | 1:6df1d1f1b372 | 5 | * http://interactive-matter.org/ |
mimil | 1:6df1d1f1b372 | 6 | * |
mimil | 1:6df1d1f1b372 | 7 | * This file is part of aJson. |
mimil | 1:6df1d1f1b372 | 8 | * |
mimil | 1:6df1d1f1b372 | 9 | * aJson is free software: you can redistribute it and/or modify |
mimil | 1:6df1d1f1b372 | 10 | * it under the terms of the GNU General Public License as published by |
mimil | 1:6df1d1f1b372 | 11 | * the Free Software Foundation, either version 3 of the License, or |
mimil | 1:6df1d1f1b372 | 12 | * (at your option) any later version. |
mimil | 1:6df1d1f1b372 | 13 | * |
mimil | 1:6df1d1f1b372 | 14 | * aJson is distributed in the hope that it will be useful, |
mimil | 1:6df1d1f1b372 | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
mimil | 1:6df1d1f1b372 | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
mimil | 1:6df1d1f1b372 | 17 | * GNU General Public License for more details. |
mimil | 1:6df1d1f1b372 | 18 | * You should have received a copy of the GNU General Public License |
mimil | 1:6df1d1f1b372 | 19 | * along with aJson. If not, see <http://www.gnu.org/licenses/>. |
mimil | 1:6df1d1f1b372 | 20 | * |
mimil | 1:6df1d1f1b372 | 21 | * Created on: 14.10.2010 |
mimil | 1:6df1d1f1b372 | 22 | * Author: marcus |
mimil | 1:6df1d1f1b372 | 23 | */ |
mimil | 1:6df1d1f1b372 | 24 | #include <stdlib.h> |
mimil | 1:6df1d1f1b372 | 25 | #include <string.h> |
mimil | 1:6df1d1f1b372 | 26 | #include "stringbuffer.h" |
mimil | 1:6df1d1f1b372 | 27 | |
mimil | 1:6df1d1f1b372 | 28 | //Default buffer size for strings |
mimil | 1:6df1d1f1b372 | 29 | #define BUFFER_SIZE 256 |
mimil | 1:6df1d1f1b372 | 30 | //there is a static buffer allocated, which is used to decode strings to |
mimil | 1:6df1d1f1b372 | 31 | //strings cannot be longer than the buffer |
mimil | 1:6df1d1f1b372 | 32 | char global_buffer[BUFFER_SIZE]; |
mimil | 1:6df1d1f1b372 | 33 | |
mimil | 1:6df1d1f1b372 | 34 | string_buffer* |
mimil | 1:6df1d1f1b372 | 35 | stringBufferCreate(void) |
mimil | 1:6df1d1f1b372 | 36 | { |
mimil | 1:6df1d1f1b372 | 37 | string_buffer* result = (string_buffer*)malloc(sizeof(string_buffer)); |
mimil | 1:6df1d1f1b372 | 38 | if (result == NULL) |
mimil | 1:6df1d1f1b372 | 39 | { |
mimil | 1:6df1d1f1b372 | 40 | return NULL; |
mimil | 1:6df1d1f1b372 | 41 | } |
mimil | 1:6df1d1f1b372 | 42 | result->string = global_buffer; |
mimil | 1:6df1d1f1b372 | 43 | memset((void*) global_buffer, 0, BUFFER_SIZE); |
mimil | 1:6df1d1f1b372 | 44 | //unused - but will be usefull after realloc got fixd |
mimil | 1:6df1d1f1b372 | 45 | /* if (result->string==NULL) { |
mimil | 1:6df1d1f1b372 | 46 | free(result); |
mimil | 1:6df1d1f1b372 | 47 | return NULL; |
mimil | 1:6df1d1f1b372 | 48 | } |
mimil | 1:6df1d1f1b372 | 49 | result->memory=BUFFER_DEFAULT_SIZE;*/ |
mimil | 1:6df1d1f1b372 | 50 | result->memory = BUFFER_SIZE; |
mimil | 1:6df1d1f1b372 | 51 | result->string_length = 0; |
mimil | 1:6df1d1f1b372 | 52 | return result; |
mimil | 1:6df1d1f1b372 | 53 | } |
mimil | 1:6df1d1f1b372 | 54 | |
mimil | 1:6df1d1f1b372 | 55 | char |
mimil | 1:6df1d1f1b372 | 56 | stringBufferAdd(char value, string_buffer* buffer) |
mimil | 1:6df1d1f1b372 | 57 | { |
mimil | 1:6df1d1f1b372 | 58 | if (buffer->string_length >= buffer->memory) |
mimil | 1:6df1d1f1b372 | 59 | { |
mimil | 1:6df1d1f1b372 | 60 | //this has to be enabled after realloc works |
mimil | 1:6df1d1f1b372 | 61 | /*char* new_string = (char*) realloc((void*) buffer->string, (buffer->memory |
mimil | 1:6df1d1f1b372 | 62 | + BUFFER_DEFAULT_SIZE) * sizeof(char)); |
mimil | 1:6df1d1f1b372 | 63 | if (new_string == NULL) |
mimil | 1:6df1d1f1b372 | 64 | { |
mimil | 1:6df1d1f1b372 | 65 | free(buffer->string); |
mimil | 1:6df1d1f1b372 | 66 | buffer->string = NULL; |
mimil | 1:6df1d1f1b372 | 67 | return -1; |
mimil | 1:6df1d1f1b372 | 68 | } else { |
mimil | 1:6df1d1f1b372 | 69 | buffer->string = new_string; |
mimil | 1:6df1d1f1b372 | 70 | } |
mimil | 1:6df1d1f1b372 | 71 | buffer->memory += BUFFER_DEFAULT_SIZE;*/ |
mimil | 1:6df1d1f1b372 | 72 | //in the meantime we just drop it |
mimil | 1:6df1d1f1b372 | 73 | return 0; //EOF would be a better choice - but that breaks json decoding |
mimil | 1:6df1d1f1b372 | 74 | } |
mimil | 1:6df1d1f1b372 | 75 | buffer->string[buffer->string_length] = value; |
mimil | 1:6df1d1f1b372 | 76 | buffer->string_length += 1; |
mimil | 1:6df1d1f1b372 | 77 | return 0; |
mimil | 1:6df1d1f1b372 | 78 | } |
mimil | 1:6df1d1f1b372 | 79 | |
mimil | 1:6df1d1f1b372 | 80 | char* |
mimil | 1:6df1d1f1b372 | 81 | stringBufferToString(string_buffer* buffer) |
mimil | 1:6df1d1f1b372 | 82 | { |
mimil | 1:6df1d1f1b372 | 83 | //this is the realloc dependent function - it does not work |
mimil | 1:6df1d1f1b372 | 84 | // char* result = buffer->string; |
mimil | 1:6df1d1f1b372 | 85 | //ensure that the string ends with 0 |
mimil | 1:6df1d1f1b372 | 86 | if (buffer->string_length == 0 || buffer->string[(buffer->string_length - 1)] |
mimil | 1:6df1d1f1b372 | 87 | != 0) |
mimil | 1:6df1d1f1b372 | 88 | { |
mimil | 1:6df1d1f1b372 | 89 | stringBufferAdd(0, buffer); |
mimil | 1:6df1d1f1b372 | 90 | } |
mimil | 1:6df1d1f1b372 | 91 | /* char* string = realloc(result, buffer->string_length); |
mimil | 1:6df1d1f1b372 | 92 | if (string==NULL) { |
mimil | 1:6df1d1f1b372 | 93 | free(result); |
mimil | 1:6df1d1f1b372 | 94 | } |
mimil | 1:6df1d1f1b372 | 95 | buffer->string=NULL; |
mimil | 1:6df1d1f1b372 | 96 | free(buffer); |
mimil | 1:6df1d1f1b372 | 97 | return string;*/ |
mimil | 1:6df1d1f1b372 | 98 | char* result = (char*)malloc(buffer->string_length * sizeof(char)); |
mimil | 1:6df1d1f1b372 | 99 | if (result == NULL) |
mimil | 1:6df1d1f1b372 | 100 | { |
mimil | 1:6df1d1f1b372 | 101 | return NULL; |
mimil | 1:6df1d1f1b372 | 102 | } |
mimil | 1:6df1d1f1b372 | 103 | strcpy(result, global_buffer); |
mimil | 1:6df1d1f1b372 | 104 | buffer->string = NULL; |
mimil | 1:6df1d1f1b372 | 105 | free(buffer); |
mimil | 1:6df1d1f1b372 | 106 | return result; |
mimil | 1:6df1d1f1b372 | 107 | } |
mimil | 1:6df1d1f1b372 | 108 | |
mimil | 1:6df1d1f1b372 | 109 | void |
mimil | 1:6df1d1f1b372 | 110 | stringBufferFree(string_buffer* buffer) |
mimil | 1:6df1d1f1b372 | 111 | { |
mimil | 1:6df1d1f1b372 | 112 | if (buffer == NULL) |
mimil | 1:6df1d1f1b372 | 113 | { |
mimil | 1:6df1d1f1b372 | 114 | //hmm it was null before - whatever |
mimil | 1:6df1d1f1b372 | 115 | return; |
mimil | 1:6df1d1f1b372 | 116 | } |
mimil | 1:6df1d1f1b372 | 117 | //this is not needed in this realloc free concept |
mimil | 1:6df1d1f1b372 | 118 | /* |
mimil | 1:6df1d1f1b372 | 119 | if (buffer->string!=NULL) { |
mimil | 1:6df1d1f1b372 | 120 | free(buffer->string); |
mimil | 1:6df1d1f1b372 | 121 | } |
mimil | 1:6df1d1f1b372 | 122 | */ |
mimil | 1:6df1d1f1b372 | 123 | free(buffer); |
mimil | 1:6df1d1f1b372 | 124 | } |
mimil | 1:6df1d1f1b372 | 125 |