source: rtos_arduino/trunk/arduino_lib/libraries/Milkcocoa_Arduino_SDK/stringbuffer.c@ 144

Last change on this file since 144 was 144, checked in by ertl-honda, 8 years ago

Milkcocoa用のMQTTライブラリの追加と,ESP8266用ライブラリの名称変更.

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