| 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 <string.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <ctype.h> |
| 27 |
#include <time.h> |
| 28 |
#include "trace.h" |
| 29 |
|
| 30 |
|
| 31 |
#include "collection_priv.h" |
| 32 |
#include "collection.h" |
| 33 |
|
| 34 |
|
| 35 |
#define STACK_DEPTH_BLOCK 15 |
| 36 |
|
| 37 |
|
| 38 |
static int col_grow_stack(struct collection_iterator *iterator, unsigned desired) |
| 39 |
{ |
| 40 |
int grow_by = 0; |
| 41 |
struct collection_item **temp; |
| 42 |
|
| 43 |
TRACE_FLOW_STRING("col_grow_stack", "Entry."); |
| 44 |
|
| 45 |
if (desired > iterator->stack_size) { |
| 46 |
grow_by = (((desired - iterator->stack_size) / STACK_DEPTH_BLOCK) + 1) * STACK_DEPTH_BLOCK; |
| 47 |
temp = (struct collection_item **)realloc(iterator->stack, grow_by * sizeof(struct collection_item *)); |
| 48 |
if (temp == NULL) { |
| 49 |
TRACE_ERROR_NUMBER("Failed to allocate memory", ENOMEM); |
| 50 |
return ENOMEM; |
| 51 |
} |
| 52 |
iterator->stack = temp; |
| 53 |
iterator->stack_size += grow_by; |
| 54 |
} |
| 55 |
TRACE_FLOW_STRING("col_grow_stack", "Exit."); |
| 56 |
return EOK; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
int col_bind_iterator(struct collection_iterator **iterator, |
| 63 |
struct collection_item *ci, |
| 64 |
int mode_flags) |
| 65 |
{ |
| 66 |
int error; |
| 67 |
struct collection_header *header; |
| 68 |
struct collection_iterator *iter = NULL; |
| 69 |
|
| 70 |
TRACE_FLOW_STRING("col_bind_iterator", "Entry."); |
| 71 |
|
| 72 |
|
| 73 |
if ((iterator == NULL) || (ci == NULL)) { |
| 74 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 75 |
return EINVAL; |
| 76 |
} |
| 77 |
|
| 78 |
iter = (struct collection_iterator *)malloc(sizeof(struct collection_iterator)); |
| 79 |
if (iter == NULL) { |
| 80 |
TRACE_ERROR_NUMBER("Error allocating memory for the iterator.", ENOMEM); |
| 81 |
return ENOMEM; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
iter->stack = NULL; |
| 86 |
iter->stack_size = 0; |
| 87 |
iter->stack_depth = 0; |
| 88 |
iter->item_level = 0; |
| 89 |
iter->flags = mode_flags; |
| 90 |
iter->pin_level = 0; |
| 91 |
iter->can_break = 0; |
| 92 |
|
| 93 |
TRACE_INFO_NUMBER("Iterator flags", iter->flags); |
| 94 |
|
| 95 |
|
| 96 |
error = col_grow_stack(iter, 1); |
| 97 |
if(error) { |
| 98 |
free(iter); |
| 99 |
TRACE_ERROR_NUMBER("Error growing stack.", error); |
| 100 |
return error; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
error = col_allocate_item(&(iter->end_item), "", NULL, 0, COL_TYPE_END); |
| 105 |
if(error) { |
| 106 |
free(iter); |
| 107 |
TRACE_ERROR_NUMBER("Error allocating end item.", error); |
| 108 |
return error; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
header = (struct collection_header *)ci->data; |
| 113 |
header->reference_count++; |
| 114 |
iter->top = ci; |
| 115 |
iter->pin = ci; |
| 116 |
*(iter->stack) = ci; |
| 117 |
iter->stack_depth++; |
| 118 |
|
| 119 |
*iterator = iter; |
| 120 |
|
| 121 |
TRACE_FLOW_STRING("col_bind_iterator", "Exit"); |
| 122 |
return EOK; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
int col_iterate_up(struct collection_iterator *iterator, unsigned level) |
| 128 |
{ |
| 129 |
TRACE_FLOW_STRING("iterate_up", "Entry"); |
| 130 |
|
| 131 |
if (iterator == NULL) { |
| 132 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 133 |
return EINVAL; |
| 134 |
} |
| 135 |
|
| 136 |
TRACE_INFO_NUMBER("Going up:", level); |
| 137 |
TRACE_INFO_NUMBER("Current stack depth:", iterator->stack_depth); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
if (level >= iterator->stack_depth) iterator->stack_depth = 0; |
| 143 |
else iterator->stack_depth -= level; |
| 144 |
|
| 145 |
TRACE_INFO_NUMBER("Stack depth at the end:", iterator->stack_depth); |
| 146 |
TRACE_FLOW_STRING("col_iterate_up", "Exit"); |
| 147 |
return EOK; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
int col_get_iterator_depth(struct collection_iterator *iterator, int *depth) |
| 152 |
{ |
| 153 |
TRACE_FLOW_STRING("col_get_iterator_depth", "Entry"); |
| 154 |
|
| 155 |
if ((iterator == NULL) || (depth == NULL)) { |
| 156 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 157 |
return EINVAL; |
| 158 |
} |
| 159 |
|
| 160 |
*depth = iterator->stack_depth - 1; |
| 161 |
|
| 162 |
TRACE_INFO_NUMBER("Stack depth at the end:", iterator->stack_depth); |
| 163 |
TRACE_FLOW_STRING("col_get_iterator_depth","Exit"); |
| 164 |
return EOK; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
int col_get_item_depth(struct collection_iterator *iterator, int *depth) |
| 169 |
{ |
| 170 |
TRACE_FLOW_STRING("col_get_item_depth", "Entry"); |
| 171 |
|
| 172 |
if ((iterator == NULL) || (depth == NULL)) { |
| 173 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 174 |
return EINVAL; |
| 175 |
} |
| 176 |
|
| 177 |
*depth = iterator->item_level; |
| 178 |
|
| 179 |
TRACE_INFO_NUMBER("Item level at the end:", iterator->item_level); |
| 180 |
TRACE_FLOW_STRING("col_get_item_depth","Exit"); |
| 181 |
return EOK; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
void col_unbind_iterator(struct collection_iterator *iterator) |
| 188 |
{ |
| 189 |
TRACE_FLOW_STRING("col_unbind_iterator", "Entry."); |
| 190 |
if (iterator != NULL) { |
| 191 |
col_destroy_collection(iterator->top); |
| 192 |
col_delete_item(iterator->end_item); |
| 193 |
free(iterator->stack); |
| 194 |
free(iterator); |
| 195 |
} |
| 196 |
TRACE_FLOW_STRING("col_unbind_iterator", "Exit"); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
int col_iterate_collection(struct collection_iterator *iterator, |
| 201 |
struct collection_item **item) |
| 202 |
{ |
| 203 |
int error; |
| 204 |
struct collection_item *current; |
| 205 |
struct collection_item *other; |
| 206 |
|
| 207 |
TRACE_FLOW_STRING("col_iterate_collection", "Entry."); |
| 208 |
|
| 209 |
|
| 210 |
if (item == NULL) { |
| 211 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 212 |
return EINVAL; |
| 213 |
} |
| 214 |
|
| 215 |
while (1) { |
| 216 |
|
| 217 |
TRACE_INFO_NUMBER("Stack depth:", iterator->stack_depth); |
| 218 |
|
| 219 |
if (iterator->stack_depth == 0) { |
| 220 |
|
| 221 |
iterator->stack[0] = iterator->top; |
| 222 |
iterator->stack_depth++; |
| 223 |
iterator->item_level = 0; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
current = iterator->stack[iterator->stack_depth - 1]; |
| 228 |
iterator->item_level = iterator->stack_depth - 1; |
| 229 |
|
| 230 |
|
| 231 |
if (((iterator->stack_depth - 1) == iterator->pin_level) && |
| 232 |
(iterator->pin == current)) { |
| 233 |
if (iterator->can_break) { |
| 234 |
TRACE_FLOW_STRING("We are done.", ""); |
| 235 |
*item = NULL; |
| 236 |
iterator->can_break = 0; |
| 237 |
return EOK; |
| 238 |
} |
| 239 |
else iterator->can_break = 1; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
if (current != NULL) { |
| 244 |
|
| 245 |
TRACE_INFO_STRING("Current item:", current->property); |
| 246 |
TRACE_INFO_NUMBER("Current item type:", current->type); |
| 247 |
|
| 248 |
|
| 249 |
if (current->type == COL_TYPE_COLLECTIONREF) { |
| 250 |
|
| 251 |
TRACE_INFO_STRING("Current item:", "collection reference"); |
| 252 |
if ((iterator->flags & COL_TRAVERSE_IGNORE) == 0) { |
| 253 |
|
| 254 |
TRACE_INFO_STRING("Collection references are not ignored", ""); |
| 255 |
error = col_grow_stack(iterator, iterator->stack_depth + 1); |
| 256 |
if (error) { |
| 257 |
TRACE_ERROR_NUMBER("Error growing stack.", error); |
| 258 |
return error; |
| 259 |
} |
| 260 |
|
| 261 |
if ((iterator->flags & COL_TRAVERSE_ONELEVEL) == 0) { |
| 262 |
TRACE_INFO_STRING("Need to go deeper", ""); |
| 263 |
|
| 264 |
|
| 265 |
if ((iterator->flags & COL_TRAVERSE_ONLYSUB) != 0) { |
| 266 |
TRACE_INFO_STRING("Instructed to show header not reference", ""); |
| 267 |
other = *((struct collection_item **)current->data); |
| 268 |
iterator->stack[iterator->stack_depth] = other->next; |
| 269 |
iterator->item_level = iterator->stack_depth; |
| 270 |
*item = other; |
| 271 |
} |
| 272 |
|
| 273 |
else if ((iterator->flags & COL_TRAVERSE_SHOWSUB) != 0) { |
| 274 |
TRACE_INFO_STRING("Instructed to show header and reference",""); |
| 275 |
iterator->stack[iterator->stack_depth] = *((struct collection_item **)(current->data)); |
| 276 |
*item = current; |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
else if ((iterator->flags & COL_TRAVERSE_FLAT) != 0) { |
| 281 |
TRACE_INFO_STRING("Instructed not to show header and reference",""); |
| 282 |
other = *((struct collection_item **)current->data); |
| 283 |
iterator->stack[iterator->stack_depth] = other->next; |
| 284 |
iterator->stack[iterator->stack_depth - 1] = current->next; |
| 285 |
iterator->stack_depth++; |
| 286 |
|
| 287 |
continue; |
| 288 |
} |
| 289 |
|
| 290 |
else { |
| 291 |
TRACE_INFO_STRING("Instructed to show reference only", ""); |
| 292 |
other = *((struct collection_item **)current->data); |
| 293 |
TRACE_INFO_STRING("Sub collection:", other->property); |
| 294 |
TRACE_INFO_NUMBER("Sub collection type:", other->type); |
| 295 |
iterator->stack[iterator->stack_depth] = other->next; |
| 296 |
if (other->next != NULL) { |
| 297 |
TRACE_INFO_STRING("Will show this item next time:", other->next->property); |
| 298 |
TRACE_INFO_NUMBER("Will show this item next time type:", other->next->type); |
| 299 |
} |
| 300 |
*item = current; |
| 301 |
TRACE_INFO_NUMBER("Level of the reference:", iterator->item_level); |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
TRACE_INFO_STRING("We return item:", (*item)->property); |
| 306 |
TRACE_INFO_NUMBER("We return item type:", (*item)->type); |
| 307 |
TRACE_INFO_STRING("Moving to the next item on the previous item in stack", ""); |
| 308 |
iterator->stack[iterator->stack_depth - 1] = current->next; |
| 309 |
iterator->stack_depth++; |
| 310 |
|
| 311 |
} |
| 312 |
else { |
| 313 |
TRACE_INFO_STRING("Instructed to parse just one level", ""); |
| 314 |
|
| 315 |
*item = current; |
| 316 |
TRACE_INFO_STRING("Moving to the next item on one level", ""); |
| 317 |
iterator->stack[iterator->stack_depth - 1] = current->next; |
| 318 |
} |
| 319 |
break; |
| 320 |
} |
| 321 |
else { |
| 322 |
|
| 323 |
TRACE_INFO_STRING("Stepping over the reference", ""); |
| 324 |
iterator->stack[iterator->stack_depth - 1] = current->next; |
| 325 |
continue; |
| 326 |
} |
| 327 |
} |
| 328 |
else { |
| 329 |
|
| 330 |
if ((current->type == COL_TYPE_COLLECTION) && |
| 331 |
((iterator->flags & COL_TRAVERSE_FLAT) != 0) && |
| 332 |
(iterator->stack_depth > 1)) { |
| 333 |
TRACE_INFO_STRING("Header of the sub collection in flat case ", ""); |
| 334 |
iterator->stack[iterator->stack_depth - 1] = current->next; |
| 335 |
continue; |
| 336 |
} |
| 337 |
else { |
| 338 |
TRACE_INFO_STRING("Simple item", ""); |
| 339 |
*item = current; |
| 340 |
iterator->stack[iterator->stack_depth - 1] = current->next; |
| 341 |
} |
| 342 |
break; |
| 343 |
} |
| 344 |
} |
| 345 |
else { |
| 346 |
|
| 347 |
TRACE_INFO_STRING("Finished level", "moving to upper level"); |
| 348 |
iterator->stack_depth--; |
| 349 |
|
| 350 |
|
| 351 |
TRACE_INFO_NUMBER("Stack depth at the end:", iterator->stack_depth); |
| 352 |
if ((iterator->flags & COL_TRAVERSE_END) != 0) { |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
if ((((iterator->flags & COL_TRAVERSE_FLAT) != 0) && |
| 359 |
(iterator->stack_depth == 0)) || |
| 360 |
((iterator->flags & COL_TRAVERSE_FLAT) == 0)) { |
| 361 |
|
| 362 |
|
| 363 |
TRACE_INFO_STRING("Finished level", "told to return END"); |
| 364 |
*item = iterator->end_item; |
| 365 |
break; |
| 366 |
} |
| 367 |
} |
| 368 |
else { |
| 369 |
|
| 370 |
continue; |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
TRACE_FLOW_STRING("col_iterate_collection", "Exit"); |
| 376 |
return EOK; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
void col_pin_iterator(struct collection_iterator *iterator) |
| 382 |
{ |
| 383 |
TRACE_FLOW_STRING("col_iterator_add_pin", "Entry"); |
| 384 |
|
| 385 |
if ((!iterator) || (!iterator->stack)) { |
| 386 |
TRACE_FLOW_STRING("Invalid itertor", "Ingoring"); |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
while ((iterator->stack_depth) && |
| 391 |
(iterator->stack[iterator->stack_depth - 1] == NULL)) { |
| 392 |
iterator->stack_depth--; |
| 393 |
} |
| 394 |
|
| 395 |
if (iterator->stack_depth == 0) { |
| 396 |
iterator->pin = iterator->top; |
| 397 |
iterator->pin_level = 0; |
| 398 |
} |
| 399 |
else { |
| 400 |
iterator->pin = iterator->stack[iterator->stack_depth - 1]; |
| 401 |
iterator->pin_level = iterator->stack_depth - 1; |
| 402 |
} |
| 403 |
iterator->can_break = 0; |
| 404 |
|
| 405 |
TRACE_FLOW_STRING("col_iterator_add_pin", "Exit"); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
void col_rewind_iterator(struct collection_iterator *iterator) |
| 411 |
{ |
| 412 |
TRACE_FLOW_STRING("col_rewind_iterator", "Entry"); |
| 413 |
|
| 414 |
if ((!iterator) || (!iterator->stack)) { |
| 415 |
TRACE_FLOW_STRING("Invalid itertor", "Ingoring"); |
| 416 |
return; |
| 417 |
} |
| 418 |
|
| 419 |
iterator->pin = iterator->top; |
| 420 |
iterator->stack[0] = iterator->top; |
| 421 |
iterator->stack_depth = 1; |
| 422 |
iterator->item_level = 0; |
| 423 |
iterator->pin_level = 0; |
| 424 |
iterator->can_break = 0; |
| 425 |
|
| 426 |
TRACE_FLOW_STRING("col_rewind_iterator", "Exit"); |
| 427 |
} |