| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <string.h> |
| 27 |
#include "trace.h" |
| 28 |
#include "collection_priv.h" |
| 29 |
#include "collection.h" |
| 30 |
#include "collection_tools.h" |
| 31 |
|
| 32 |
|
| 33 |
int col_debug_handle(const char *property, |
| 34 |
int property_len, |
| 35 |
int type, |
| 36 |
void *data, |
| 37 |
int length, |
| 38 |
void *custom_data, |
| 39 |
int *dummy) |
| 40 |
{ |
| 41 |
int i; |
| 42 |
int nest_level; |
| 43 |
int ignore = 0; |
| 44 |
|
| 45 |
TRACE_FLOW_STRING("col_debug_handle", "Entry."); |
| 46 |
|
| 47 |
|
| 48 |
nest_level = *(int *)(custom_data); |
| 49 |
if (nest_level == -1) { |
| 50 |
ignore = 1; |
| 51 |
nest_level = 1; |
| 52 |
} |
| 53 |
|
| 54 |
TRACE_INFO_NUMBER("We are getting this pointer:", custom_data); |
| 55 |
TRACE_INFO_NUMBER("Nest level:", nest_level); |
| 56 |
|
| 57 |
switch (type) { |
| 58 |
case COL_TYPE_STRING: |
| 59 |
printf(">%*s%s[%d] str: %s (%d)\n", |
| 60 |
(nest_level -1) * 4, "", |
| 61 |
property, |
| 62 |
length, |
| 63 |
(char *)(data), |
| 64 |
nest_level); |
| 65 |
break; |
| 66 |
case COL_TYPE_BINARY: |
| 67 |
printf(">%*s%s[%d] bin: ", |
| 68 |
(nest_level -1) * 4, "", |
| 69 |
property, |
| 70 |
length); |
| 71 |
for (i = 0; i < length; i++) |
| 72 |
printf("%02X", ((unsigned char *)(data))[i]); |
| 73 |
printf(" (%d)\n", nest_level); |
| 74 |
break; |
| 75 |
case COL_TYPE_INTEGER: |
| 76 |
printf(">%*s%s[%d] int: %d (%d)\n", |
| 77 |
(nest_level -1) * 4, "", |
| 78 |
property, |
| 79 |
length, |
| 80 |
*((int32_t *)(data)), |
| 81 |
nest_level); |
| 82 |
break; |
| 83 |
case COL_TYPE_UNSIGNED: |
| 84 |
printf(">%*s%s[%d] uint: %u (%d)\n", |
| 85 |
(nest_level -1) * 4, "", |
| 86 |
property, |
| 87 |
length, |
| 88 |
*((uint32_t *)(data)), |
| 89 |
nest_level); |
| 90 |
break; |
| 91 |
case COL_TYPE_LONG: |
| 92 |
printf(">%*s%s[%d] long: %lld (%d)\n", |
| 93 |
(nest_level -1) * 4, "", |
| 94 |
property, |
| 95 |
length, |
| 96 |
(long long int)(*((int64_t *)(data))), |
| 97 |
nest_level); |
| 98 |
break; |
| 99 |
case COL_TYPE_ULONG: |
| 100 |
printf(">%*s%s[%d] ulong: %llu (%d)\n", |
| 101 |
(nest_level -1) * 4, "", |
| 102 |
property, |
| 103 |
length, |
| 104 |
(long long unsigned)(*((uint64_t *)(data))), |
| 105 |
nest_level); |
| 106 |
break; |
| 107 |
case COL_TYPE_DOUBLE: |
| 108 |
printf(">%*s%s[%d] double: %.4f (%d)\n", |
| 109 |
(nest_level -1) * 4, "", |
| 110 |
property, |
| 111 |
length, |
| 112 |
*((double *)(data)), |
| 113 |
nest_level); |
| 114 |
break; |
| 115 |
case COL_TYPE_BOOL: |
| 116 |
printf(">%*s%s[%d] bool: %s (%d)\n", |
| 117 |
(nest_level -1) * 4, "", |
| 118 |
property, |
| 119 |
length, |
| 120 |
(*((unsigned char *)(data)) == '\0') ? "false" : "true", |
| 121 |
nest_level); |
| 122 |
break; |
| 123 |
case COL_TYPE_COLLECTION: |
| 124 |
if (!ignore) nest_level++; |
| 125 |
printf(">%*s%s[%d] header: count %d, ref_count %d class %d data: ", |
| 126 |
(nest_level -1) * 4, "", |
| 127 |
property, |
| 128 |
length, |
| 129 |
((struct collection_header *)(data))->count, |
| 130 |
((struct collection_header *)(data))->reference_count, |
| 131 |
((struct collection_header *)(data))->cclass); |
| 132 |
|
| 133 |
#ifdef COL_PRINT_BINARY_HEADER |
| 134 |
for (i = 0; i < length; i++) |
| 135 |
printf("%02X", ((unsigned char *)(data))[i]); |
| 136 |
#endif |
| 137 |
printf(" (%d)\n", nest_level); |
| 138 |
break; |
| 139 |
case COL_TYPE_COLLECTIONREF: |
| 140 |
printf(">%*s%s[%d] external link: ", |
| 141 |
(nest_level -1) * 4, "", |
| 142 |
property, |
| 143 |
length); |
| 144 |
for (i = 0; i < length; i++) |
| 145 |
printf("%02X", ((unsigned char *)(data))[i]); |
| 146 |
printf(" (%d)\n", nest_level); |
| 147 |
break; |
| 148 |
case COL_TYPE_END: |
| 149 |
printf(">%*sEND[N/A] (%d)\n", |
| 150 |
(nest_level -1) * 4, "", |
| 151 |
nest_level); |
| 152 |
if (!ignore) nest_level--; |
| 153 |
break; |
| 154 |
default: |
| 155 |
printf("Not implemented yet.\n"); |
| 156 |
break; |
| 157 |
} |
| 158 |
*(int *)(custom_data) = nest_level; |
| 159 |
TRACE_INFO_NUMBER("Nest level at the end:", nest_level); |
| 160 |
TRACE_FLOW_STRING("col_debug_handle", "Success exit."); |
| 161 |
return EOK; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
inline int col_debug_item(struct collection_item *item) |
| 166 |
{ |
| 167 |
int dummy = 0; |
| 168 |
int nest_level = -1; |
| 169 |
return col_debug_handle(item->property, |
| 170 |
item->property_len, |
| 171 |
item->type, |
| 172 |
item->data, |
| 173 |
item->length, |
| 174 |
(void *)(&nest_level), |
| 175 |
&dummy); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
int col_debug_collection(struct collection_item *handle, int flag) |
| 181 |
{ |
| 182 |
int error = EOK; |
| 183 |
int nest_level = 0; |
| 184 |
|
| 185 |
TRACE_FLOW_STRING("col_debug_collection", "Entry."); |
| 186 |
|
| 187 |
printf("DEBUG COLLECTION %s\n", handle->property); |
| 188 |
|
| 189 |
flag |= COL_TRAVERSE_END; |
| 190 |
|
| 191 |
printf("Traverse flags %d\n", flag); |
| 192 |
|
| 193 |
|
| 194 |
error = col_traverse_collection(handle, flag, |
| 195 |
col_debug_handle, |
| 196 |
(void *)(&nest_level)); |
| 197 |
if (error) printf("Error debugging collection %d\n", error); |
| 198 |
|
| 199 |
TRACE_FLOW_STRING("col_debug_collection", "Exit."); |
| 200 |
return error; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
static inline const char *col_get_type(int type) |
| 206 |
{ |
| 207 |
switch (type) { |
| 208 |
case COL_TYPE_STRING: |
| 209 |
return COL_TYPE_NAME_STRING; |
| 210 |
|
| 211 |
case COL_TYPE_INTEGER: |
| 212 |
return COL_TYPE_NAME_INTEGER; |
| 213 |
|
| 214 |
case COL_TYPE_UNSIGNED: |
| 215 |
return COL_TYPE_NAME_UNSIGNED; |
| 216 |
|
| 217 |
case COL_TYPE_LONG: |
| 218 |
return COL_TYPE_NAME_LONG; |
| 219 |
|
| 220 |
case COL_TYPE_ULONG: |
| 221 |
return COL_TYPE_NAME_ULONG; |
| 222 |
|
| 223 |
case COL_TYPE_BINARY: |
| 224 |
return COL_TYPE_NAME_BINARY; |
| 225 |
|
| 226 |
case COL_TYPE_DOUBLE: |
| 227 |
return COL_TYPE_NAME_DOUBLE; |
| 228 |
|
| 229 |
case COL_TYPE_BOOL: |
| 230 |
return COL_TYPE_NAME_BOOL; |
| 231 |
|
| 232 |
default: |
| 233 |
return COL_TYPE_NAME_UNKNOWN; |
| 234 |
} |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
int col_get_data_len(int type, int length) |
| 240 |
{ |
| 241 |
int len = 0; |
| 242 |
|
| 243 |
TRACE_FLOW_STRING("col_get_data_len", "Entry point"); |
| 244 |
|
| 245 |
switch (type) { |
| 246 |
case COL_TYPE_INTEGER: |
| 247 |
case COL_TYPE_UNSIGNED: |
| 248 |
len = 11; |
| 249 |
break; |
| 250 |
case COL_TYPE_LONG: |
| 251 |
case COL_TYPE_ULONG: |
| 252 |
len = 20; |
| 253 |
break; |
| 254 |
|
| 255 |
case COL_TYPE_STRING: |
| 256 |
case COL_TYPE_BINARY: |
| 257 |
len = length * 2 + 2; |
| 258 |
break; |
| 259 |
|
| 260 |
case COL_TYPE_DOUBLE: |
| 261 |
len = 64; |
| 262 |
break; |
| 263 |
|
| 264 |
case COL_TYPE_BOOL: |
| 265 |
len = 6; |
| 266 |
break; |
| 267 |
|
| 268 |
default: |
| 269 |
len = 0; |
| 270 |
break; |
| 271 |
} |
| 272 |
|
| 273 |
TRACE_FLOW_STRING("col_get_data_len","Exit point"); |
| 274 |
|
| 275 |
return len; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
static int col_copy_esc(char *dest, const char *source, char esc) |
| 280 |
{ |
| 281 |
int i = 0; |
| 282 |
int j = 0; |
| 283 |
|
| 284 |
dest[j] = esc; |
| 285 |
j++; |
| 286 |
|
| 287 |
while (source[i]) { |
| 288 |
if ((source[i] == '\\') || |
| 289 |
(source[i] == esc)) { |
| 290 |
|
| 291 |
dest[j] = '\\'; |
| 292 |
j++; |
| 293 |
|
| 294 |
} |
| 295 |
dest[j] = source[i]; |
| 296 |
i++; |
| 297 |
j++; |
| 298 |
} |
| 299 |
dest[j] = esc; |
| 300 |
j++; |
| 301 |
|
| 302 |
return j; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
int col_grow_buffer(struct col_serial_data *buf_data, int len) |
| 307 |
{ |
| 308 |
char *tmp; |
| 309 |
|
| 310 |
TRACE_FLOW_STRING("col_grow_buffer", "Entry point"); |
| 311 |
TRACE_INFO_NUMBER("Current length: ", buf_data->length); |
| 312 |
TRACE_INFO_NUMBER("Increment length: ", len); |
| 313 |
TRACE_INFO_NUMBER("Expected length: ", buf_data->length+len); |
| 314 |
TRACE_INFO_NUMBER("Current size: ", buf_data->size); |
| 315 |
|
| 316 |
|
| 317 |
while (buf_data->length+len >= buf_data->size) { |
| 318 |
tmp = realloc(buf_data->buffer, buf_data->size + BLOCK_SIZE); |
| 319 |
if (tmp == NULL) { |
| 320 |
TRACE_ERROR_NUMBER("Error. Failed to allocate memory.", ENOMEM); |
| 321 |
return ENOMEM; |
| 322 |
} |
| 323 |
buf_data->buffer = tmp; |
| 324 |
buf_data->size += BLOCK_SIZE; |
| 325 |
TRACE_INFO_NUMBER("New size: ", buf_data->size); |
| 326 |
|
| 327 |
} |
| 328 |
|
| 329 |
TRACE_INFO_NUMBER("Final size: ", buf_data->size); |
| 330 |
TRACE_FLOW_STRING("col_grow_buffer", "Success Exit."); |
| 331 |
return EOK; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
int col_put_marker(struct col_serial_data *buf_data, const void *data, int len) |
| 336 |
{ |
| 337 |
int error = EOK; |
| 338 |
|
| 339 |
TRACE_FLOW_STRING("col_put_marker", "Entry point"); |
| 340 |
TRACE_INFO_NUMBER("Marker length: ", len); |
| 341 |
|
| 342 |
error = col_grow_buffer(buf_data, len); |
| 343 |
if (error) { |
| 344 |
TRACE_ERROR_NUMBER("col_grow_buffer failed with: ", error); |
| 345 |
return error; |
| 346 |
} |
| 347 |
memcpy(buf_data->buffer + buf_data->length, data, len); |
| 348 |
buf_data->length += len; |
| 349 |
buf_data->buffer[buf_data->length] = '\0'; |
| 350 |
|
| 351 |
TRACE_FLOW_STRING("col_put_marker","Success exit"); |
| 352 |
return error; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
int col_serialize(const char *property_in, |
| 357 |
int property_len_in, |
| 358 |
int type, |
| 359 |
void *data_in, |
| 360 |
int length_in, |
| 361 |
void *custom_data, |
| 362 |
int *dummy) |
| 363 |
{ |
| 364 |
int len; |
| 365 |
struct col_serial_data *buf_data; |
| 366 |
const char *property; |
| 367 |
const void *data; |
| 368 |
int property_len; |
| 369 |
int length; |
| 370 |
int error = EOK; |
| 371 |
int i; |
| 372 |
|
| 373 |
TRACE_FLOW_STRING("col_serialize","Entry point"); |
| 374 |
|
| 375 |
*dummy = 0; |
| 376 |
|
| 377 |
|
| 378 |
buf_data = (struct col_serial_data *)custom_data; |
| 379 |
if (buf_data == NULL) { |
| 380 |
TRACE_ERROR_STRING("Error.", "Storage data is not passed in!"); |
| 381 |
return EINVAL; |
| 382 |
} |
| 383 |
if (buf_data->buffer == NULL) { |
| 384 |
TRACE_INFO_STRING("First time use.", "Allocating buffer."); |
| 385 |
buf_data->buffer = malloc(BLOCK_SIZE); |
| 386 |
if (buf_data->buffer == NULL) { |
| 387 |
TRACE_ERROR_NUMBER("Error. Failed to allocate memory.", ENOMEM); |
| 388 |
return ENOMEM; |
| 389 |
} |
| 390 |
buf_data->buffer[0] = '\0'; |
| 391 |
buf_data->length = 0; |
| 392 |
buf_data->size = BLOCK_SIZE; |
| 393 |
} |
| 394 |
|
| 395 |
TRACE_INFO_NUMBER("Buffer len: ", buf_data->length); |
| 396 |
TRACE_INFO_NUMBER("Buffer size: ", buf_data->size); |
| 397 |
TRACE_INFO_STRING("Buffer: ", buf_data->buffer); |
| 398 |
|
| 399 |
|
| 400 |
if (type == COL_TYPE_COLLECTION) { |
| 401 |
TRACE_INFO_STRING("Serializing collection: ", property_in); |
| 402 |
TRACE_INFO_STRING("First header. ", ""); |
| 403 |
error = col_put_marker(buf_data, "(", 1); |
| 404 |
if (error != EOK) return error; |
| 405 |
property = TEXT_COLLECTION; |
| 406 |
property_len = TEXT_COLLEN; |
| 407 |
data = property_in; |
| 408 |
length = property_len_in + 1; |
| 409 |
type = COL_TYPE_STRING; |
| 410 |
buf_data->nest_level++; |
| 411 |
} |
| 412 |
|
| 413 |
else if (type == COL_TYPE_COLLECTIONREF) { |
| 414 |
|
| 415 |
TRACE_FLOW_STRING("col_serialize", "skip reference return"); |
| 416 |
return EOK; |
| 417 |
} |
| 418 |
|
| 419 |
else if (type == COL_TYPE_END) { |
| 420 |
if ((buf_data->length > 0) && |
| 421 |
(buf_data->buffer[buf_data->length-1] == ',')) { |
| 422 |
buf_data->length--; |
| 423 |
buf_data->buffer[buf_data->length] = '\0'; |
| 424 |
} |
| 425 |
if (buf_data->nest_level > 0) { |
| 426 |
buf_data->nest_level--; |
| 427 |
error = col_put_marker(buf_data, ")", 1); |
| 428 |
if (error != EOK) return error; |
| 429 |
} |
| 430 |
TRACE_FLOW_STRING("col_serialize", "end collection item processed returning"); |
| 431 |
return EOK; |
| 432 |
} |
| 433 |
else { |
| 434 |
property = property_in; |
| 435 |
property_len = property_len_in; |
| 436 |
data = data_in; |
| 437 |
length = length_in; |
| 438 |
} |
| 439 |
|
| 440 |
TRACE_INFO_STRING("Property: ", property); |
| 441 |
TRACE_INFO_NUMBER("Property length: ", property_len); |
| 442 |
|
| 443 |
|
| 444 |
if ((error = col_put_marker(buf_data, property, property_len)) || |
| 445 |
(error = col_put_marker(buf_data, "=", 1))) { |
| 446 |
TRACE_ERROR_NUMBER("put_marker returned error: ", error); |
| 447 |
return error; |
| 448 |
} |
| 449 |
|
| 450 |
len = col_get_data_len(type,length); |
| 451 |
TRACE_INFO_NUMBER("Expected data length: ",len); |
| 452 |
TRACE_INFO_STRING("Buffer so far: ", buf_data->buffer); |
| 453 |
|
| 454 |
|
| 455 |
if ((error = col_grow_buffer(buf_data, len))) { |
| 456 |
TRACE_ERROR_NUMBER("grow_buffer returned error: ", error); |
| 457 |
return error; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
switch (type) { |
| 462 |
case COL_TYPE_STRING: |
| 463 |
|
| 464 |
len = col_copy_esc(&buf_data->buffer[buf_data->length], |
| 465 |
(const char *)(data), '"'); |
| 466 |
break; |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
case COL_TYPE_BINARY: |
| 474 |
buf_data->buffer[buf_data->length] = '\''; |
| 475 |
for (i = 0; i < length; i++) |
| 476 |
sprintf(&buf_data->buffer[buf_data->length + i *2] + 1, |
| 477 |
"%02X", (unsigned int)(((const unsigned char *)(data))[i])); |
| 478 |
len = length * 2 + 1; |
| 479 |
buf_data->buffer[buf_data->length + len] = '\''; |
| 480 |
len++; |
| 481 |
break; |
| 482 |
|
| 483 |
case COL_TYPE_INTEGER: |
| 484 |
len = sprintf(&buf_data->buffer[buf_data->length], |
| 485 |
"%d", *((const int32_t *)(data))); |
| 486 |
break; |
| 487 |
|
| 488 |
case COL_TYPE_UNSIGNED: |
| 489 |
len = sprintf(&buf_data->buffer[buf_data->length], |
| 490 |
"%u", *((const uint32_t *)(data))); |
| 491 |
break; |
| 492 |
|
| 493 |
case COL_TYPE_LONG: |
| 494 |
len = sprintf(&buf_data->buffer[buf_data->length], |
| 495 |
"%lld", |
| 496 |
(long long int)(*((const int64_t *)(data)))); |
| 497 |
break; |
| 498 |
|
| 499 |
case COL_TYPE_ULONG: |
| 500 |
len = sprintf(&buf_data->buffer[buf_data->length], |
| 501 |
"%llu", |
| 502 |
(long long unsigned)(*((const uint64_t *)(data)))); |
| 503 |
break; |
| 504 |
|
| 505 |
case COL_TYPE_DOUBLE: |
| 506 |
len = sprintf(&buf_data->buffer[buf_data->length], |
| 507 |
"%.4f", *((const double *)(data))); |
| 508 |
break; |
| 509 |
|
| 510 |
case COL_TYPE_BOOL: |
| 511 |
len = sprintf(&buf_data->buffer[buf_data->length], |
| 512 |
"%s", (*((const unsigned char *)(data))) ? "true" : "false"); |
| 513 |
break; |
| 514 |
|
| 515 |
default: |
| 516 |
buf_data->buffer[buf_data->length] = '\0'; |
| 517 |
len = 0; |
| 518 |
break; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
buf_data->length += len; |
| 523 |
buf_data->buffer[buf_data->length] = '\0'; |
| 524 |
|
| 525 |
|
| 526 |
error = col_put_marker(buf_data, ",", 1); |
| 527 |
if (error != EOK) { |
| 528 |
TRACE_ERROR_NUMBER("put_marker returned error: ", error); |
| 529 |
return error; |
| 530 |
} |
| 531 |
|
| 532 |
TRACE_INFO_STRING("Data: ", buf_data->buffer); |
| 533 |
TRACE_FLOW_STRING("col_serialize", "Exit point"); |
| 534 |
return EOK; |
| 535 |
|
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
int col_print_collection(struct collection_item *handle) |
| 540 |
{ |
| 541 |
struct col_serial_data buf_data; |
| 542 |
int error = EOK; |
| 543 |
|
| 544 |
TRACE_FLOW_STRING("col_print_collection", "Entry"); |
| 545 |
|
| 546 |
printf("COLLECTION:\n"); |
| 547 |
|
| 548 |
buf_data.buffer = NULL; |
| 549 |
buf_data.length = 0; |
| 550 |
buf_data.size = 0; |
| 551 |
buf_data.nest_level = 0; |
| 552 |
|
| 553 |
|
| 554 |
error = col_traverse_collection(handle, |
| 555 |
COL_TRAVERSE_DEFAULT | COL_TRAVERSE_END , |
| 556 |
col_serialize, (void *)(&buf_data)); |
| 557 |
if (error) |
| 558 |
printf("Error traversing collection %d\n", error); |
| 559 |
else |
| 560 |
printf("%s\n", buf_data.buffer); |
| 561 |
|
| 562 |
free(buf_data.buffer); |
| 563 |
|
| 564 |
TRACE_FLOW_NUMBER("col_print_collection returning", error); |
| 565 |
return error; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
int col_print_collection2(struct collection_item *handle) |
| 570 |
{ |
| 571 |
struct collection_iterator *iterator = NULL; |
| 572 |
int error = EOK; |
| 573 |
struct collection_item *item = NULL; |
| 574 |
int nest_level = 0; |
| 575 |
int dummy = 0; |
| 576 |
int line = 1; |
| 577 |
|
| 578 |
TRACE_FLOW_STRING("col_print_collection2", "Entry"); |
| 579 |
|
| 580 |
|
| 581 |
if (handle == NULL) { |
| 582 |
TRACE_ERROR_STRING("No error list", ""); |
| 583 |
return EINVAL; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
error = col_bind_iterator(&iterator, handle, |
| 588 |
COL_TRAVERSE_DEFAULT | |
| 589 |
COL_TRAVERSE_END | |
| 590 |
COL_TRAVERSE_SHOWSUB); |
| 591 |
if (error) { |
| 592 |
TRACE_ERROR_NUMBER("Error (bind):", error); |
| 593 |
return error; |
| 594 |
} |
| 595 |
|
| 596 |
do { |
| 597 |
|
| 598 |
error = col_iterate_collection(iterator, &item); |
| 599 |
if (error) { |
| 600 |
TRACE_ERROR_NUMBER("Error (iterate):", error); |
| 601 |
col_unbind_iterator(iterator); |
| 602 |
return error; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
if (item == NULL) break; |
| 607 |
|
| 608 |
if (item->type != COL_TYPE_END) printf("%05d", line); |
| 609 |
|
| 610 |
col_debug_handle(item->property, |
| 611 |
item->property_len, |
| 612 |
item->type, |
| 613 |
item->data, |
| 614 |
item->length, |
| 615 |
(void *)(&nest_level), |
| 616 |
&dummy); |
| 617 |
line++; |
| 618 |
} |
| 619 |
while(1); |
| 620 |
|
| 621 |
|
| 622 |
col_unbind_iterator(iterator); |
| 623 |
|
| 624 |
TRACE_INFO_STRING("col_print_collection2", "Exit"); |
| 625 |
return EOK; |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
int col_print_item(struct collection_item *handle, const char *name) |
| 630 |
{ |
| 631 |
struct col_serial_data buf_data; |
| 632 |
int error = EOK; |
| 633 |
|
| 634 |
TRACE_FLOW_STRING("col_print_item", "Entry"); |
| 635 |
|
| 636 |
printf("PRINT ITEM:\n"); |
| 637 |
|
| 638 |
buf_data.buffer = NULL; |
| 639 |
buf_data.length = 0; |
| 640 |
buf_data.size = 0; |
| 641 |
buf_data.nest_level = 0; |
| 642 |
|
| 643 |
error = col_get_item_and_do(handle, name, COL_TYPE_ANY, |
| 644 |
COL_TRAVERSE_DEFAULT, |
| 645 |
col_serialize, &buf_data); |
| 646 |
if(error) printf("Error searching collection %d\n", error); |
| 647 |
else { |
| 648 |
if (buf_data.buffer != NULL) { |
| 649 |
if (buf_data.length > 0) buf_data.length--; |
| 650 |
buf_data.buffer[buf_data.length] = '\0'; |
| 651 |
printf("%s\n", buf_data.buffer); |
| 652 |
free(buf_data.buffer); |
| 653 |
} |
| 654 |
else { |
| 655 |
printf("Name %s is not found in the collection %s.\n", |
| 656 |
name, handle->property); |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
TRACE_FLOW_NUMBER("col_print_item returning", error); |
| 661 |
return error; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
void col_free_property_list(char **str_list) |
| 666 |
{ |
| 667 |
int current = 0; |
| 668 |
|
| 669 |
TRACE_FLOW_STRING("col_free_property_list","Entry"); |
| 670 |
|
| 671 |
if (str_list != NULL) { |
| 672 |
while(str_list[current]) { |
| 673 |
free(str_list[current]); |
| 674 |
current++; |
| 675 |
} |
| 676 |
free(str_list); |
| 677 |
} |
| 678 |
|
| 679 |
TRACE_FLOW_STRING("col_free_property_list","Exit"); |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
char **col_collection_to_list(struct collection_item *handle, int *size, int *error) |
| 685 |
{ |
| 686 |
struct collection_iterator *iterator; |
| 687 |
struct collection_item *item = NULL; |
| 688 |
char **list; |
| 689 |
unsigned count; |
| 690 |
int err; |
| 691 |
int current = 0; |
| 692 |
|
| 693 |
TRACE_FLOW_STRING("col_collection_to_list","Entry"); |
| 694 |
|
| 695 |
|
| 696 |
err = col_get_collection_count(handle, &count); |
| 697 |
if (err) { |
| 698 |
TRACE_ERROR_NUMBER("Failed to get count of items from collection.", err); |
| 699 |
if (error) *error = err; |
| 700 |
return NULL; |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
list = (char **)malloc(count * sizeof(char *)); |
| 705 |
if (list == NULL) { |
| 706 |
TRACE_ERROR_NUMBER("Failed to get allocate memory.", ENOMEM); |
| 707 |
if (error) *error = ENOMEM; |
| 708 |
return NULL; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
err = col_bind_iterator(&iterator, handle, COL_TRAVERSE_ONELEVEL); |
| 714 |
if (err) { |
| 715 |
TRACE_ERROR_NUMBER("Failed to bind.", err); |
| 716 |
if (error) *error = err; |
| 717 |
free(list); |
| 718 |
return NULL; |
| 719 |
} |
| 720 |
|
| 721 |
while(1) { |
| 722 |
|
| 723 |
err = col_iterate_collection(iterator, &item); |
| 724 |
if (err) { |
| 725 |
TRACE_ERROR_NUMBER("Failed to iterate collection", err); |
| 726 |
if (error) *error = err; |
| 727 |
col_free_property_list(list); |
| 728 |
col_unbind_iterator(iterator); |
| 729 |
return NULL; |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
if (item == NULL) break; |
| 734 |
|
| 735 |
TRACE_INFO_STRING("Property:", col_get_item_property(item, NULL)); |
| 736 |
|
| 737 |
|
| 738 |
if(col_get_item_type(item) == COL_TYPE_COLLECTION) continue; |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
list[current] = strdup(col_get_item_property(item, NULL)); |
| 743 |
if (list[current] == NULL) { |
| 744 |
TRACE_ERROR_NUMBER("Failed to dup string.", ENOMEM); |
| 745 |
if (error) *error = ENOMEM; |
| 746 |
col_free_property_list(list); |
| 747 |
return NULL; |
| 748 |
} |
| 749 |
current++; |
| 750 |
} |
| 751 |
|
| 752 |
list[current] = NULL; |
| 753 |
|
| 754 |
|
| 755 |
col_unbind_iterator(iterator); |
| 756 |
|
| 757 |
if (size) *size = (int)(count - 1); |
| 758 |
if (error) *error = EOK; |
| 759 |
|
| 760 |
TRACE_FLOW_STRING("col_collection_to_list returning", ((list == NULL) ? "NULL" : list[0])); |
| 761 |
return list; |
| 762 |
} |