1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/*
*
* Copyright (C) 2012-2020 by C.H. Huang
* plushuang.tw@gmail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* ---
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU Lesser General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*
*/
#include <stddef.h> // NULL
#include <string.h>
#include <UgString.h>
#include <UgArray.h>
#include <UgEntry.h>
#if defined(_MSC_VER)
#define strtoll _strtoi64
#endif
// ----------------------------------------------------------------------------
// UgArray: for UG_ENTRY_ARRAY
void ug_array_init(void* arr, int element_size, int allocated_len)
{
UgArrayChar* array = arr;
if (allocated_len)
array->at = ug_malloc(element_size * allocated_len);
else
array->at = NULL;
array->length = 0;
array->allocated = allocated_len;
array->element_size = element_size;
}
void ug_array_clear(void* arr)
{
UgArrayChar* array = arr;
ug_free(array->at);
array->at = NULL;
array->length = 0;
array->allocated = 0;
}
void* ug_array_alloc(void* arr, int nElements)
{
UgArrayChar* array = arr;
int len;
len = array->length + nElements;
if (array->allocated < len) {
array->allocated = len * 2;
// if (array->allocated < 16)
// array->allocated = 16;
array->at = ug_realloc(array->at, array->allocated * array->element_size);
}
arr = array->at + array->length * array->element_size;
array->length += nElements;
return arr;
}
void ug_array_foreach(void* array, UgForeachFunc func, void* data)
{
char* cur;
char* end;
cur = ((UgArrayChar*)array)->at;
end = cur + ((UgArrayChar*)array)->element_size * ((UgArrayChar*)array)->length;
for (; cur < end; cur+= ((UgArrayChar*)array)->element_size)
func(cur, data);
}
void ug_array_foreach_ptr(void* array, UgForeachFunc func, void* data)
{
char* cur = ((UgArrayChar*)array)->at;
char* end = cur + ((UgArrayChar*)array)->element_size * ((UgArrayChar*)array)->length;
for (; cur < end; cur+= ((UgArrayChar*)array)->element_size)
func(*(void**)cur, data);
}
void* ug_array_find_sorted(void* array, const void* key,
UgCompareFunc compare, int* inserted_index)
{
int low;
int cur;
int high;
int diff;
void* cur_key;
low = 0;
cur = 0;
high = ((UgArrayChar*)array)->length;
while (low < high) {
// cur = low + ((high - low) / 2);
cur = low + ((high - low) >> 1);
cur_key = ((UgArrayChar*)array)->at + cur *
((UgArrayChar*)array)->element_size;
diff = compare(cur_key, key);
if (diff == 0) {
if (inserted_index)
inserted_index[0] = cur;
return ((UgArrayChar*)array)->at + cur *
((UgArrayChar*)array)->element_size;
}
else if (diff > 0)
high = cur;
else if (diff < 0)
low = cur + 1;
}
if (inserted_index) {
if (cur < low)
cur++;
inserted_index[0] = cur;
}
return NULL;
}
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
// C99 or C++ inline functions in UgArray.h
#else
void* ug_array_insert(void* array, int index, int length)
{
char* addr;
ug_array_alloc(array, length);
memmove(ug_array_addr(array, index + length),
addr = ug_array_addr(array, index),
ug_array_count(array, ug_array_length(array) - index - 1));
return (void*)addr;
}
void ug_array_erase(void* array, int index, int length)
{
memmove(ug_array_addr(array, index),
ug_array_addr(array, index + length),
ug_array_count(array, ug_array_length(array) - index - 1));
((UgArrayChar*)array)->length -= length;
}
void ug_array_sort(void* array, UgCompareFunc compare)
{
qsort( ((UgArrayChar*)array)->at, ((UgArrayChar*)array)->length,
((UgArrayChar*)array)->element_size, compare);
}
#endif // __STDC_VERSION__
int ug_array_compare_int(const void *s1, const void *s2)
{
return *(int*)s1 - *(int*)s2;
}
int ug_array_compare_string(const void *s1, const void *s2)
{
return strcmp(*(char**)s1, *(char**)s2);
}
int ug_array_compare_pointer(const void *s1, const void *s2)
{
return *(char**)s1 - *(char**)s2;
}
// ----------------------------------------------------------------------------
// UgJsonParseFunc for JSON array elements
UgJsonError ug_json_parse_array_bool(UgJson* json,
const char* name, const char* value,
void* array, void* none)
{
int boolValue;
if (json->type != UG_JSON_TRUE && json->type != UG_JSON_FALSE) {
// if (json->type >= UG_JSON_OBJECT)
// ug_json_push(json, ug_json_parse_unknown, NULL, NULL);
return UG_JSON_ERROR_TYPE_NOT_MATCH;
}
if (json->type == UG_JSON_TRUE)
boolValue = TRUE;
else
boolValue = FALSE;
((UgArrayInt*)array)->element_size = sizeof(int);
*((int*) ug_array_alloc(array, 1)) = boolValue;
return UG_JSON_ERROR_NONE;
}
UgJsonError ug_json_parse_array_int(UgJson* json,
const char* name, const char* value,
void* array, void* none)
{
if (json->type != UG_JSON_NUMBER) {
// if (json->type >= UG_JSON_OBJECT)
// ug_json_push(json, ug_json_parse_unknown, NULL, NULL);
return UG_JSON_ERROR_TYPE_NOT_MATCH;
}
((UgArrayInt*)array)->element_size = sizeof(int);
*((int*) ug_array_alloc(array, 1)) = strtol(value, NULL, 10);
return UG_JSON_ERROR_NONE;
}
UgJsonError ug_json_parse_array_uint(UgJson* json,
const char* name, const char* value,
void* array, void* none)
{
if (json->type != UG_JSON_NUMBER) {
// if (json->type >= UG_JSON_OBJECT)
// ug_json_push(json, ug_json_parse_unknown, NULL, NULL);
return UG_JSON_ERROR_TYPE_NOT_MATCH;
}
((UgArrayInt*)array)->element_size = sizeof(unsigned int);
*((unsigned int*) ug_array_alloc(array, 1)) = strtoul(value, NULL, 10);
return UG_JSON_ERROR_NONE;
}
UgJsonError ug_json_parse_array_int64(UgJson* json,
const char* name, const char* value,
void* array, void* none)
{
if (json->type != UG_JSON_NUMBER) {
// if (json->type >= UG_JSON_OBJECT)
// ug_json_push(json, ug_json_parse_unknown, NULL, NULL);
return UG_JSON_ERROR_TYPE_NOT_MATCH;
}
((UgArrayInt*)array)->element_size = sizeof(int64_t);
*((int64_t*) ug_array_alloc(array, 1)) = strtoll(value, NULL, 10);
return UG_JSON_ERROR_NONE;
}
UgJsonError ug_json_parse_array_double(UgJson* json,
const char* name, const char* value,
void* array, void* none)
{
if (json->type != UG_JSON_NUMBER) {
// if (json->type >= UG_JSON_OBJECT)
// ug_json_push(json, ug_json_parse_unknown, NULL, NULL);
return UG_JSON_ERROR_TYPE_NOT_MATCH;
}
((UgArrayInt*)array)->element_size = sizeof(double);
*((double*) ug_array_alloc(array, 1)) = strtod(value, NULL);
return UG_JSON_ERROR_NONE;
}
UgJsonError ug_json_parse_array_string(UgJson* json,
const char* name, const char* value,
void* array, void* none)
{
char* string;
if (json->type == UG_JSON_STRING)
string = ug_strdup(value);
else if (json->type == UG_JSON_NULL)
string = NULL;
else {
// if (json->type >= UG_JSON_OBJECT)
// ug_json_push(json, ug_json_parse_unknown, NULL, NULL);
return UG_JSON_ERROR_TYPE_NOT_MATCH;
}
((UgArrayInt*)array)->element_size = sizeof(char*);
*((char**) ug_array_alloc(array, 1)) = string;
return UG_JSON_ERROR_NONE;
}
// ----------------------------------------------------------------------------
// write JSON array elements
void ug_json_write_array_bool(UgJson* json, UgArrayInt* array)
{
// UgArrayInt* array = src;
int index;
for (index = 0; index < array->length; index++)
ug_json_write_bool(json, array->at[index]);
}
void ug_json_write_array_int(UgJson* json, UgArrayInt* array)
{
// UgArrayInt* array = src;
int index;
for (index = 0; index < array->length; index++)
ug_json_write_int(json, array->at[index]);
}
void ug_json_write_array_uint(UgJson* json, UgArrayUint* array)
{
int index;
for (index = 0; index < array->length; index++)
ug_json_write_uint(json, array->at[index]);
}
void ug_json_write_array_int64(UgJson* json, UgArrayInt64* array)
{
int index;
for (index = 0; index < array->length; index++)
ug_json_write_int64(json, array->at[index]);
}
void ug_json_write_array_double(UgJson* json, UgArrayDouble* array)
{
int index;
for (index = 0; index < array->length; index++)
ug_json_write_double(json, array->at[index]);
}
void ug_json_write_array_string(UgJson* json, UgArrayStr* array)
{
int index;
char* string;
for (index = 0; index < array->length; index++) {
string = array->at[index];
if (string == NULL)
ug_json_write_null(json);
else
ug_json_write_string(json, string);
}
}
|