Nyx Node
Loading...
Searching...
No Matches
json_string.c
1/* NyxNode
2 * Author: Jérôme ODIER <jerome.odier@lpsc.in2p3.fr>
3 * SPDX-License-Identifier: GPL-2.0-only (Mongoose backend) or GPL-3.0+
4 */
5
6/*--------------------------------------------------------------------------------------------------------------------*/
7
8#include <string.h>
9
10#include "../nyx_node_internal.h"
11
12/*--------------------------------------------------------------------------------------------------------------------*/
13
15{
16 /*----------------------------------------------------------------------------------------------------------------*/
17
19
20 /*----------------------------------------------------------------------------------------------------------------*/
21
22 object->base = NYX_OBJECT(NYX_TYPE_STRING);
23
24 /*----------------------------------------------------------------------------------------------------------------*/
25
26 object->managed = false;
27 object->length = 0x000000000000;
28 object->value = (str_t) /* NOSONAR */ "";
29
30 /*----------------------------------------------------------------------------------------------------------------*/
31
32 return object;
33}
34
35/*--------------------------------------------------------------------------------------------------------------------*/
36
37void nyx_string_free(nyx_string_t *object)
38{
39 if(object->managed)
40 {
41 nyx_memory_free(object->value);
42 }
43
44 nyx_memory_free(object);
45}
46
47/*--------------------------------------------------------------------------------------------------------------------*/
48
49bool nyx_string_is_managed(const nyx_string_t *object)
50{
51 return object->managed;
52}
53
54/*--------------------------------------------------------------------------------------------------------------------*/
55
57{
58 return object->value;
59}
60
61/*--------------------------------------------------------------------------------------------------------------------*/
62
63void nyx_string_get_buff(const nyx_string_t *object, size_t *result_size, buff_t *result_buff)
64{
65 if(result_size != NULL) {
66 *result_size = object->length;
67 }
68
69 if(result_buff != NULL) {
70 *result_buff = object->value;
71 }
72}
73
74/*--------------------------------------------------------------------------------------------------------------------*/
75
77{
78 if(value == NULL)
79 {
80 NYX_LOG_ERROR("Null string not allowed");
81
82 return false;
83 }
84
85 bool modified = strcmp(object->value, value) != 0;
86
87 if(modified || object->managed)
88 {
89 /*------------------------------------------------------------------------------------------------------------*/
90
91 if(object->managed)
92 {
93 nyx_memory_free(object->value);
94 }
95
96 /*------------------------------------------------------------------------------------------------------------*/
97
98 object->managed = managed;
99 object->length = strlen(value);
100 object->value = (str_t) /* NOSONAR */ value;
101
102 /*------------------------------------------------------------------------------------------------------------*/
103
104 return modified;
105 }
106
107 return false;
108}
109
110/*--------------------------------------------------------------------------------------------------------------------*/
111
112bool nyx_string_set_buff(nyx_string_t *object, size_t size, BUFF_t buff, bool managed)
113{
114 if(buff == NULL)
115 {
116 NYX_LOG_ERROR("Null buffer not allowed");
117
118 return false;
119 }
120
121 bool modified = object->length != size || memcmp(object->value, buff, size) != 0;
122
123 if(modified || object->managed)
124 {
125 /*------------------------------------------------------------------------------------------------------------*/
126
127 if(object->managed)
128 {
129 nyx_memory_free(object->value);
130 }
131
132 /*------------------------------------------------------------------------------------------------------------*/
133
134 object->managed = managed;
135 object->length = /*--*/(size);
136 object->value = (str_t) /* NOSONAR */ buff;
137
138 /*------------------------------------------------------------------------------------------------------------*/
139
140 return modified;
141 }
142
143 return true;
144}
145
146/*--------------------------------------------------------------------------------------------------------------------*/
147
148size_t nyx_string_length(const nyx_string_t *object)
149{
150 return object->length;
151}
152
153/*--------------------------------------------------------------------------------------------------------------------*/
154
156{
157 nyx_string_builder_t *sb = nyx_string_builder_new();
158
159 nyx_string_builder_append(sb, NYX_SB_NO_ESCAPE, "\"");
160 nyx_string_builder_append(sb, NYX_SB_ESCAPE_JSON, object->value);
161 nyx_string_builder_append(sb, NYX_SB_NO_ESCAPE, "\"");
162
163 str_t result = nyx_string_builder_to_string(sb);
164
165 nyx_string_builder_free(sb);
166
167 return result;
168}
169
170/*--------------------------------------------------------------------------------------------------------------------*/
171
173{
174 nyx_string_builder_t *sb = nyx_string_builder_new();
175
176 ///_string_builder_append(sb, NYX_SB_NO_ESCAPE, "\"");
177 nyx_string_builder_append(sb, NYX_SB_NO_ESCAPE, object->value);
178 ///_string_builder_append(sb, NYX_SB_NO_ESCAPE, "\"");
179
180 str_t result = nyx_string_builder_to_string(sb);
181
182 nyx_string_builder_free(sb);
183
184 return result;
185}
186
187/*--------------------------------------------------------------------------------------------------------------------*/
#define NYX_LOG_ERROR(fmt,...)
Logs an error message.
Definition nyx_node.h:219
#define STR_t
Alias for const char *.
Definition nyx_node.h:71
__NYX_NULLABLE__ buff_t nyx_memory_alloc(__NYX_ZEROABLE__ size_t size)
Similar to libc malloc except that a memory overflow causes the node to stop.
__NYX_ZEROABLE__ size_t nyx_memory_free(__NYX_NULLABLE__ buff_t buff)
Similar to libc free except that it returns the amount of memory freed.
#define buff_t
Alias for void *.
Definition nyx_node.h:67
#define BUFF_t
Alias for const void *.
Definition nyx_node.h:68
#define str_t
Alias for char *.
Definition nyx_node.h:70
@ NYX_TYPE_STRING
String object.
Definition nyx_node.h:437
str_t value
C string payload.
Definition nyx_node.h:919
bool managed
true if the value is freed with this object.
Definition nyx_node.h:917
size_t nyx_string_length(const nyx_string_t *object)
Returns the number of content bytes of the provided JSON string object.
bool nyx_string_set(nyx_string_t *object, STR_t value, bool managed)
Sets the text value of the provided JSON string object.
Definition json_string.c:76
str_t nyx_string_to_string(const nyx_string_t *object)
Returns a C string, with the special character escaping, representing the provided JSON string object...
str_t nyx_string_to_cstring(const nyx_string_t *object)
Returns a C string, without special character escaping, representing the provided JSON string object.
nyx_string_t * nyx_string_new(void)
Allocates a new JSON string object.
Definition json_string.c:14
STR_t nyx_string_get(const nyx_string_t *object)
Gets the text value of the provided JSON string object.
Definition json_string.c:56
bool nyx_string_set_buff(nyx_string_t *object, size_t size, BUFF_t buff, bool managed)
Sets the content of the provided JSON string object from a byte buffer.
Struct describing a JSON string object.
Definition nyx_node.h:914