~ubuntu-branches/ubuntu/saucy/sssd/saucy

« back to all changes in this revision

Viewing changes to common/collection/collection_tools.c

  • Committer: Stéphane Graber
  • Date: 2011-06-15 16:23:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: stgraber@ubuntu.com-20110615162314-rbhoppnpaxfqo5q7
Merge 1.5.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    COLLECTION LIBRARY
3
 
 
4
 
    Additional functions for printing and debugging collections.
5
 
 
6
 
    Copyright (C) Dmitri Pal <dpal@redhat.com> 2009
7
 
 
8
 
    Collection Library is free software: you can redistribute it and/or modify
9
 
    it under the terms of the GNU Lesser General Public License as published by
10
 
    the Free Software Foundation, either version 3 of the License, or
11
 
    (at your option) any later version.
12
 
 
13
 
    Collection Library is distributed in the hope that it will be useful,
14
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
    GNU Lesser General Public License for more details.
17
 
 
18
 
    You should have received a copy of the GNU Lesser General Public License
19
 
    along with Collection Library.  If not, see <http://www.gnu.org/licenses/>.
20
 
*/
21
 
 
22
 
 
23
 
#include <stdio.h>
24
 
#include <malloc.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
 
/* Debug handle */
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
 
               *((int *)(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
 
               *((unsigned int *)(data)),
89
 
               nest_level);
90
 
        break;
91
 
    case COL_TYPE_LONG:
92
 
        printf(">%*s%s[%d] long: %ld (%d)\n",
93
 
               (nest_level -1) * 4, "",
94
 
               property,
95
 
               length,
96
 
               *((long *)(data)),
97
 
               nest_level);
98
 
        break;
99
 
    case COL_TYPE_ULONG:
100
 
        printf(">%*s%s[%d] ulong: %lu (%d)\n",
101
 
               (nest_level -1) * 4, "",
102
 
               property,
103
 
               length,
104
 
               *((unsigned long *)(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
 
        for (i = 0; i < length; i++)
133
 
            printf("%02X", ((unsigned char *)(data))[i]);
134
 
        printf(" (%d)\n", nest_level);
135
 
        break;
136
 
    case COL_TYPE_COLLECTIONREF:
137
 
        printf(">%*s%s[%d] external link: ",
138
 
               (nest_level -1) * 4, "",
139
 
               property,
140
 
               length);
141
 
        for (i = 0; i < length; i++)
142
 
            printf("%02X", ((unsigned char *)(data))[i]);
143
 
        printf(" (%d)\n", nest_level);
144
 
        break;
145
 
    case COL_TYPE_END:
146
 
        printf(">%*sEND[N/A] (%d)\n",
147
 
               (nest_level -1) * 4, "",
148
 
               nest_level);
149
 
        if (!ignore) nest_level--;
150
 
        break;
151
 
    default:
152
 
        printf("Not implemented yet.\n");
153
 
        break;
154
 
    }
155
 
    *(int *)(custom_data) = nest_level;
156
 
    TRACE_INFO_NUMBER("Nest level at the end:", nest_level);
157
 
    TRACE_FLOW_STRING("col_debug_handle", "Success exit.");
158
 
    return EOK;
159
 
}
160
 
 
161
 
/* Convenience function to debug an item */
162
 
inline int col_debug_item(struct collection_item *item)
163
 
{
164
 
    int dummy = 0;
165
 
    int nest_level = -1;
166
 
    return col_debug_handle(item->property,
167
 
                            item->property_len,
168
 
                            item->type,
169
 
                            item->data,
170
 
                            item->length,
171
 
                            (void *)(&nest_level),
172
 
                            &dummy);
173
 
}
174
 
 
175
 
 
176
 
/* Print collection for debugging purposes */
177
 
int col_debug_collection(struct collection_item *handle, int flag)
178
 
{
179
 
    int error = EOK;
180
 
    int nest_level = 0;
181
 
 
182
 
    TRACE_FLOW_STRING("col_debug_collection", "Entry.");
183
 
 
184
 
    printf("DEBUG COLLECTION %s\n", handle->property);
185
 
 
186
 
    flag |= COL_TRAVERSE_END;
187
 
 
188
 
    printf("Traverse flags %d\n", flag);
189
 
 
190
 
    /* Traverse collection */
191
 
    error = col_traverse_collection(handle, flag,
192
 
                                    col_debug_handle,
193
 
                                    (void *)(&nest_level));
194
 
    if (error) printf("Error debuging collection %d\n", error);
195
 
 
196
 
    TRACE_FLOW_STRING("col_debug_collection", "Exit.");
197
 
    return error;
198
 
}
199
 
 
200
 
 
201
 
/* Return a static string based on type of the element */
202
 
static inline const char *col_get_type(int type)
203
 
{
204
 
    switch (type) {
205
 
    case COL_TYPE_STRING:
206
 
        return COL_TYPE_NAME_STRING;
207
 
 
208
 
    case COL_TYPE_INTEGER:
209
 
        return COL_TYPE_NAME_INTEGER;
210
 
 
211
 
    case COL_TYPE_UNSIGNED:
212
 
        return COL_TYPE_NAME_UNSIGNED;
213
 
 
214
 
    case COL_TYPE_LONG:
215
 
        return COL_TYPE_NAME_LONG;
216
 
 
217
 
    case COL_TYPE_ULONG:
218
 
        return COL_TYPE_NAME_ULONG;
219
 
 
220
 
    case COL_TYPE_BINARY:
221
 
        return COL_TYPE_NAME_BINARY;
222
 
 
223
 
    case COL_TYPE_DOUBLE:
224
 
        return COL_TYPE_NAME_DOUBLE;
225
 
 
226
 
    case COL_TYPE_BOOL:
227
 
        return COL_TYPE_NAME_BOOL;
228
 
 
229
 
    default:
230
 
        return COL_TYPE_NAME_UNKNOWN;
231
 
    }
232
 
 
233
 
}
234
 
 
235
 
/* Calculate the potential size of the item */
236
 
int col_get_data_len(int type, int length)
237
 
{
238
 
    int len = 0;
239
 
 
240
 
    TRACE_FLOW_STRING("col_get_data_len", "Entry point");
241
 
 
242
 
    switch (type) {
243
 
    case COL_TYPE_INTEGER:
244
 
    case COL_TYPE_UNSIGNED:
245
 
    case COL_TYPE_LONG:
246
 
    case COL_TYPE_ULONG:
247
 
        len = 20;
248
 
        break;
249
 
 
250
 
    case COL_TYPE_STRING:
251
 
    case COL_TYPE_BINARY:
252
 
        len = length * 2 + 2;
253
 
        break;
254
 
 
255
 
    case COL_TYPE_DOUBLE:
256
 
        len = 64;
257
 
        break;
258
 
 
259
 
    case COL_TYPE_BOOL:
260
 
        len = 6;
261
 
        break;
262
 
 
263
 
    default:
264
 
        len = 0;
265
 
        break;
266
 
    }
267
 
 
268
 
    TRACE_FLOW_STRING("col_get_data_len","Exit point");
269
 
 
270
 
    return len;
271
 
}
272
 
 
273
 
/* Copy data escaping characters */
274
 
static int col_copy_esc(char *dest, const char *source, char esc)
275
 
{
276
 
    int i = 0;
277
 
    int j = 0;
278
 
 
279
 
    dest[j] = esc;
280
 
    j++;
281
 
 
282
 
    while (source[i]) {
283
 
        if ((source[i] == '\\') ||
284
 
            (source[i] == esc)) {
285
 
 
286
 
            dest[j] = '\\';
287
 
            j++;
288
 
 
289
 
        }
290
 
        dest[j] = source[i];
291
 
        i++;
292
 
        j++;
293
 
    }
294
 
    dest[j] = esc;
295
 
    j++;
296
 
 
297
 
    return j;
298
 
}
299
 
 
300
 
/* Grow buffer to accomodate more space */
301
 
int col_grow_buffer(struct col_serial_data *buf_data, int len)
302
 
{
303
 
    char *tmp;
304
 
 
305
 
    TRACE_FLOW_STRING("col_grow_buffer", "Entry point");
306
 
    TRACE_INFO_NUMBER("Current length: ", buf_data->length);
307
 
    TRACE_INFO_NUMBER("Increment length: ", len);
308
 
    TRACE_INFO_NUMBER("Expected length: ", buf_data->length+len);
309
 
    TRACE_INFO_NUMBER("Current size: ", buf_data->size);
310
 
 
311
 
    /* Grow buffer if needed */
312
 
    while (buf_data->length+len >= buf_data->size) {
313
 
        tmp = realloc(buf_data->buffer, buf_data->size + BLOCK_SIZE);
314
 
        if (tmp == NULL) {
315
 
            TRACE_ERROR_NUMBER("Error. Failed to allocate memory.", ENOMEM);
316
 
            return ENOMEM;
317
 
        }
318
 
        buf_data->buffer = tmp;
319
 
        buf_data->size += BLOCK_SIZE;
320
 
        TRACE_INFO_NUMBER("New size: ", buf_data->size);
321
 
 
322
 
    }
323
 
 
324
 
    TRACE_INFO_NUMBER("Final size: ", buf_data->size);
325
 
    TRACE_FLOW_STRING("col_grow_buffer", "Success Exit.");
326
 
    return EOK;
327
 
}
328
 
 
329
 
/* Specail function to add different formatting symbols to the output */
330
 
int col_put_marker(struct col_serial_data *buf_data, const void *data, int len)
331
 
{
332
 
    int error = EOK;
333
 
 
334
 
    TRACE_FLOW_STRING("col_put_marker", "Entry point");
335
 
    TRACE_INFO_NUMBER("Marker length: ", len);
336
 
 
337
 
    error = col_grow_buffer(buf_data, len);
338
 
    if (error) {
339
 
        TRACE_ERROR_NUMBER("col_grow_buffer failed with: ", error);
340
 
        return error;
341
 
    }
342
 
    memcpy(buf_data->buffer + buf_data->length, data, len);
343
 
    buf_data->length += len;
344
 
    buf_data->buffer[buf_data->length] = '\0';
345
 
 
346
 
    TRACE_FLOW_STRING("col_put_marker","Success exit");
347
 
    return error;
348
 
}
349
 
 
350
 
/* Add item's data */
351
 
int col_serialize(const char *property_in,
352
 
                  int property_len_in,
353
 
                  int type,
354
 
                  void *data_in,
355
 
                  int length_in,
356
 
                  void *custom_data,
357
 
                  int *dummy)
358
 
{
359
 
    int len;
360
 
    struct col_serial_data *buf_data;
361
 
    const char *property;
362
 
    const void *data;
363
 
    int  property_len;
364
 
    int length;
365
 
    int error = EOK;
366
 
    int i;
367
 
 
368
 
    TRACE_FLOW_STRING("col_serialize","Entry point");
369
 
 
370
 
    *dummy = 0;
371
 
 
372
 
    /* Check is there is buffer. If not allocate */
373
 
    buf_data = (struct col_serial_data *)custom_data;
374
 
    if (buf_data == NULL) {
375
 
        TRACE_ERROR_STRING("Error.", "Storage data is not passed in!");
376
 
        return EINVAL;
377
 
    }
378
 
    if (buf_data->buffer == NULL) {
379
 
        TRACE_INFO_STRING("First time use.", "Allocating buffer.");
380
 
        buf_data->buffer = malloc(BLOCK_SIZE);
381
 
        if (buf_data->buffer == NULL) {
382
 
            TRACE_ERROR_NUMBER("Error. Failed to allocate memory.", ENOMEM);
383
 
            return ENOMEM;
384
 
        }
385
 
        buf_data->buffer[0] = '\0';
386
 
        buf_data->length = 0;
387
 
        buf_data->size = BLOCK_SIZE;
388
 
    }
389
 
 
390
 
    TRACE_INFO_NUMBER("Buffer len: ", buf_data->length);
391
 
    TRACE_INFO_NUMBER("Buffer size: ", buf_data->size);
392
 
    TRACE_INFO_STRING("Buffer: ", buf_data->buffer);
393
 
 
394
 
    /* Check the beginning of the collection */
395
 
    if (type == COL_TYPE_COLLECTION) {
396
 
        TRACE_INFO_STRING("Serializing collection: ", property_in);
397
 
        TRACE_INFO_STRING("First header. ", "");
398
 
        error = col_put_marker(buf_data, "(", 1);
399
 
        if (error != EOK) return error;
400
 
        property = TEXT_COLLECTION;
401
 
        property_len = TEXT_COLLEN;
402
 
        data = property_in;
403
 
        length = property_len_in + 1;
404
 
        type = COL_TYPE_STRING;
405
 
        buf_data->nest_level++;
406
 
    }
407
 
    /* Check for subcollections */
408
 
    else if (type == COL_TYPE_COLLECTIONREF) {
409
 
        /* Skip */
410
 
        TRACE_FLOW_STRING("col_serialize", "skip reference return");
411
 
        return EOK;
412
 
    }
413
 
    /* Check for the end of the collection */
414
 
    else if (type == COL_TYPE_END) {
415
 
        if ((buf_data->length > 0) &&
416
 
            (buf_data->buffer[buf_data->length-1] == ',')) {
417
 
            buf_data->length--;
418
 
            buf_data->buffer[buf_data->length] = '\0';
419
 
        }
420
 
        if (buf_data->nest_level > 0) {
421
 
            buf_data->nest_level--;
422
 
            error = col_put_marker(buf_data, ")", 1);
423
 
            if (error != EOK) return error;
424
 
        }
425
 
        TRACE_FLOW_STRING("col_serialize", "end collection item processed returning");
426
 
        return EOK;
427
 
    }
428
 
    else {
429
 
        property = property_in;
430
 
        property_len = property_len_in;
431
 
        data = data_in;
432
 
        length = length_in;
433
 
    }
434
 
 
435
 
    TRACE_INFO_STRING("Property: ", property);
436
 
    TRACE_INFO_NUMBER("Property length: ", property_len);
437
 
 
438
 
    /* Start with property and "=" */
439
 
    if ((error = col_put_marker(buf_data, property, property_len)) ||
440
 
        (error = col_put_marker(buf_data, "=", 1))) {
441
 
        TRACE_ERROR_NUMBER("put_marker returned error: ", error);
442
 
        return error;
443
 
    }
444
 
    /* Get projected length of the item */
445
 
    len = col_get_data_len(type,length);
446
 
    TRACE_INFO_NUMBER("Expected data length: ",len);
447
 
    TRACE_INFO_STRING("Buffer so far: ", buf_data->buffer);
448
 
 
449
 
    /* Make sure we have enough space */
450
 
    if ((error = col_grow_buffer(buf_data, len))) {
451
 
        TRACE_ERROR_NUMBER("grow_buffer returned error: ", error);
452
 
        return error;
453
 
    }
454
 
 
455
 
    /* Add the value */
456
 
    switch (type) {
457
 
    case COL_TYPE_STRING:
458
 
        /* Escape double quotes */
459
 
        len = col_copy_esc(&buf_data->buffer[buf_data->length],
460
 
                           (const char *)(data), '"');
461
 
        break;
462
 
 
463
 
    case COL_TYPE_BINARY:
464
 
        buf_data->buffer[buf_data->length] = '\'';
465
 
        for (i = 0; i < length; i++)
466
 
            sprintf(&buf_data->buffer[buf_data->length + i *2] + 1,
467
 
                    "%02X", (unsigned int)(((const unsigned char *)(data))[i]));
468
 
        len = length * 2 + 1;
469
 
        buf_data->buffer[buf_data->length + len] = '\'';
470
 
        len++;
471
 
        break;
472
 
 
473
 
    case COL_TYPE_INTEGER:
474
 
        len = sprintf(&buf_data->buffer[buf_data->length],
475
 
                      "%d", *((const int *)(data)));
476
 
        break;
477
 
 
478
 
    case COL_TYPE_UNSIGNED:
479
 
        len = sprintf(&buf_data->buffer[buf_data->length],
480
 
                      "%u", *((const unsigned int *)(data)));
481
 
        break;
482
 
 
483
 
    case COL_TYPE_LONG:
484
 
        len = sprintf(&buf_data->buffer[buf_data->length],
485
 
                      "%ld", *((const long *)(data)));
486
 
        break;
487
 
 
488
 
    case COL_TYPE_ULONG:
489
 
        len = sprintf(&buf_data->buffer[buf_data->length],
490
 
                      "%lu", *((const unsigned long *)(data)));
491
 
        break;
492
 
 
493
 
    case COL_TYPE_DOUBLE:
494
 
        len = sprintf(&buf_data->buffer[buf_data->length],
495
 
                      "%.4f", *((const double *)(data)));
496
 
        break;
497
 
 
498
 
    case COL_TYPE_BOOL:
499
 
        len = sprintf(&buf_data->buffer[buf_data->length],
500
 
                      "%s", (*((const unsigned char *)(data))) ? "true" : "false");
501
 
        break;
502
 
 
503
 
    default:
504
 
        buf_data->buffer[buf_data->length] = '\0';
505
 
        len = 0;
506
 
        break;
507
 
    }
508
 
 
509
 
    /* Adjust length */
510
 
    buf_data->length += len;
511
 
    buf_data->buffer[buf_data->length] = '\0';
512
 
 
513
 
    /* Always put a comma at the end */
514
 
    error = col_put_marker(buf_data, ",", 1);
515
 
    if (error != EOK) {
516
 
        TRACE_ERROR_NUMBER("put_marker returned error: ", error);
517
 
        return error;
518
 
    }
519
 
 
520
 
    TRACE_INFO_STRING("Data: ", buf_data->buffer);
521
 
    TRACE_FLOW_STRING("col_serialize", "Exit point");
522
 
    return EOK;
523
 
 
524
 
}
525
 
 
526
 
/* Print the collection using default serialization */
527
 
int col_print_collection(struct collection_item *handle)
528
 
{
529
 
    struct col_serial_data buf_data;
530
 
    int error = EOK;
531
 
 
532
 
    TRACE_FLOW_STRING("col_print_collection", "Entry");
533
 
 
534
 
    printf("COLLECTION:\n");
535
 
 
536
 
    buf_data.buffer = NULL;
537
 
    buf_data.length = 0;
538
 
    buf_data.size = 0;
539
 
    buf_data.nest_level = 0;
540
 
 
541
 
    /* Traverse collection */
542
 
    error = col_traverse_collection(handle,
543
 
                                    COL_TRAVERSE_DEFAULT | COL_TRAVERSE_END ,
544
 
                                    col_serialize, (void *)(&buf_data));
545
 
    if (error)
546
 
        printf("Error traversing collection %d\n", error);
547
 
    else
548
 
        printf("%s\n", buf_data.buffer);
549
 
 
550
 
    free(buf_data.buffer);
551
 
 
552
 
    TRACE_FLOW_NUMBER("col_print_collection returning", error);
553
 
    return error;
554
 
}
555
 
 
556
 
/* Print the collection using iterator */
557
 
int col_print_collection2(struct collection_item *handle)
558
 
{
559
 
    struct collection_iterator *iterator = NULL;
560
 
    int error = EOK;
561
 
    struct collection_item *item = NULL;
562
 
    int nest_level = 0;
563
 
    int dummy = 0;
564
 
    int line = 1;
565
 
 
566
 
    TRACE_FLOW_STRING("col_print_collection2", "Entry");
567
 
 
568
 
    /* If we have something to print print it */
569
 
    if (handle == NULL) {
570
 
        TRACE_ERROR_STRING("No error list", "");
571
 
        return EINVAL;
572
 
    }
573
 
 
574
 
    /* Bind iterator */
575
 
    error = col_bind_iterator(&iterator, handle,
576
 
                              COL_TRAVERSE_DEFAULT |
577
 
                              COL_TRAVERSE_END |
578
 
                              COL_TRAVERSE_SHOWSUB);
579
 
    if (error) {
580
 
        TRACE_ERROR_NUMBER("Error (bind):", error);
581
 
        return error;
582
 
    }
583
 
 
584
 
    do {
585
 
        /* Loop through a collection */
586
 
        error = col_iterate_collection(iterator, &item);
587
 
        if (error) {
588
 
            TRACE_ERROR_NUMBER("Error (iterate):", error);
589
 
            col_unbind_iterator(iterator);
590
 
            return error;
591
 
        }
592
 
 
593
 
        /* Are we done ? */
594
 
        if (item == NULL) break;
595
 
 
596
 
        if (item->type != COL_TYPE_END) printf("%05d", line);
597
 
 
598
 
        col_debug_handle(item->property,
599
 
                         item->property_len,
600
 
                         item->type,
601
 
                         item->data,
602
 
                         item->length,
603
 
                         (void *)(&nest_level),
604
 
                         &dummy);
605
 
        line++;
606
 
    }
607
 
    while(1);
608
 
 
609
 
    /* Do not forget to unbind iterator - otherwise there will be a leak */
610
 
    col_unbind_iterator(iterator);
611
 
 
612
 
    TRACE_INFO_STRING("col_print_collection2", "Exit");
613
 
    return EOK;
614
 
}
615
 
 
616
 
/* Find and print one item using default serialization */
617
 
int col_print_item(struct collection_item *handle, const char *name)
618
 
{
619
 
    struct col_serial_data buf_data;
620
 
    int error = EOK;
621
 
 
622
 
    TRACE_FLOW_STRING("col_print_item", "Entry");
623
 
 
624
 
    printf("PRINT ITEM:\n");
625
 
 
626
 
    buf_data.buffer = NULL;
627
 
    buf_data.length = 0;
628
 
    buf_data.size = 0;
629
 
    buf_data.nest_level = 0;
630
 
 
631
 
    error =  col_get_item_and_do(handle, name, COL_TYPE_ANY,
632
 
                                 COL_TRAVERSE_DEFAULT,
633
 
                                 col_serialize, &buf_data);
634
 
    if(error) printf("Error searching collection %d\n", error);
635
 
    else {
636
 
        if (buf_data.buffer != NULL) {
637
 
            if (buf_data.length > 0) buf_data.length--;
638
 
            buf_data.buffer[buf_data.length] = '\0',
639
 
            printf("%s\n", buf_data.buffer);
640
 
            free(buf_data.buffer);
641
 
        }
642
 
        else {
643
 
            printf("Name %s is not found in the collection %s.\n",
644
 
                   name, handle->property);
645
 
        }
646
 
    }
647
 
 
648
 
    TRACE_FLOW_NUMBER("col_print_item returning", error);
649
 
    return error;
650
 
}
651
 
 
652
 
/* Function to free the list of properties. */
653
 
void col_free_property_list(char **str_list)
654
 
{
655
 
    int current = 0;
656
 
 
657
 
    TRACE_FLOW_STRING("col_free_property_list","Entry");
658
 
 
659
 
    if (str_list != NULL) {
660
 
        while(str_list[current]) {
661
 
            free(str_list[current]);
662
 
            current++;
663
 
        }
664
 
        free(str_list);
665
 
    }
666
 
 
667
 
    TRACE_FLOW_STRING("col_free_property_list","Exit");
668
 
}
669
 
 
670
 
 
671
 
/* Convert collection to list of properties */
672
 
char **col_collection_to_list(struct collection_item *handle, int *size, int *error)
673
 
{
674
 
    struct collection_iterator *iterator;
675
 
    struct collection_item *item = NULL;
676
 
    char **list;
677
 
    unsigned count;
678
 
    int err;
679
 
    int current = 0;
680
 
 
681
 
    TRACE_FLOW_STRING("col_collection_to_list","Entry");
682
 
 
683
 
    /* Get number of the subsections */
684
 
    err = col_get_collection_count(handle, &count);
685
 
    if (err) {
686
 
        TRACE_ERROR_NUMBER("Failed to get count of items from collection.", err);
687
 
        if (error) *error = err;
688
 
        return NULL;
689
 
    }
690
 
 
691
 
    /* Allocate memory for the sections */
692
 
    list = (char **)malloc(count * sizeof(char *));
693
 
    if (list == NULL) {
694
 
        TRACE_ERROR_NUMBER("Failed to get allocate memory.", ENOMEM);
695
 
        if (error) *error = ENOMEM;
696
 
        return NULL;
697
 
    }
698
 
 
699
 
    /* Now iterate to fill in the sections */
700
 
    /* Bind iterator */
701
 
    err =  col_bind_iterator(&iterator, handle, COL_TRAVERSE_ONELEVEL);
702
 
    if (err) {
703
 
        TRACE_ERROR_NUMBER("Failed to bind.", err);
704
 
        if (error) *error = err;
705
 
        free(list);
706
 
        return NULL;
707
 
    }
708
 
 
709
 
    while(1) {
710
 
        /* Loop through a collection */
711
 
        err = col_iterate_collection(iterator, &item);
712
 
        if (err) {
713
 
            TRACE_ERROR_NUMBER("Failed to iterate collection", err);
714
 
            if (error) *error = err;
715
 
            col_free_property_list(list);
716
 
            col_unbind_iterator(iterator);
717
 
            return NULL;
718
 
        }
719
 
 
720
 
        /* Are we done ? */
721
 
        if (item == NULL) break;
722
 
 
723
 
        TRACE_INFO_STRING("Property:", col_get_item_property(item, NULL));
724
 
 
725
 
        /* Skip head */
726
 
        if(col_get_item_type(item) == COL_TYPE_COLLECTION) continue;
727
 
 
728
 
 
729
 
        /* Allocate memory for the new string */
730
 
        list[current] = strdup(col_get_item_property(item, NULL));
731
 
        if (list[current] == NULL) {
732
 
            TRACE_ERROR_NUMBER("Failed to dup string.", ENOMEM);
733
 
            if (error) *error = ENOMEM;
734
 
            col_free_property_list(list);
735
 
            return NULL;
736
 
        }
737
 
        current++;
738
 
    }
739
 
 
740
 
    list[current] = NULL;
741
 
 
742
 
    /* Do not forget to unbind iterator - otherwise there will be a leak */
743
 
    col_unbind_iterator(iterator);
744
 
 
745
 
    if (size) *size = (int)(count - 1);
746
 
    if (error) *error = EOK;
747
 
 
748
 
    TRACE_FLOW_STRING("col_collection_to_list returning", ((list == NULL) ? "NULL" : list[0]));
749
 
    return list;
750
 
}