aboutsummaryrefslogtreecommitdiff
path: root/bowl.c
blob: 0f43db2b1c395b457c082a4250257dc65aacd09b (plain)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
// Include our header file
#include "bowl.h"

// Runtime includes
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <stdbool.h>

#define RDB_REC_DEF_SIZE    2
#define RDB_REC_MULTIPLY    2

#define ORIGINAL            (short) 0
#define SHALLOW             (short) 1
#define DEEP                (short) 2

/*** Forward declared functions ***/

int list_search(struct bowl**, struct bowl*, struct bowl*);

void list_print(struct bowl *data, const char *offset);

/******/

dt_err bowl_malloc(struct bowl *(*data))
{
    (*data) = (struct bowl*) malloc(sizeof(struct bowl));
    if(*data == NULL) {
        printf("Creating bowl object FAILED");
        return MALLOC_FAILED;
    }

    memset(*data, 0, sizeof(struct bowl));

    (*data)->type = UNSET;
    return SUCCESS;
}

dt_err bowl_resettype(struct bowl *data)
{
    if(data->type == LITERAL) {
        if(data->payload.literal) free(data->payload.literal);

    } else if(data->type == LIST || data->type == PAIR) {

        /* Iterate over all children and clear them */
        int i;
        dt_err err;
        for(i = 0; i < data->size; i++) {
            err = bowl_free(data->payload.list[i]);
            if(err) return err;
        }
    }

    /* Set the data type to unset */
    data->type = UNSET;
    data->encset = 0;
    data->size = 0;
    data->used = 0;

    /* Forcibly clean union memory to avoid bleeding data */
    memset(&data->payload, 0, sizeof(data->payload));

    return SUCCESS;
}

dt_err bowl_addliteral(struct bowl *data, const char *literal)
{
    /* Make sure we are a literal or unset data object */
    if(data->type != UNSET)
        if(data->type != LITERAL) return INVALID_PAYLOAD;

    size_t length = REAL_STRLEN(literal);

    /* Get rid of previous data */
    if(data->payload.literal) free(data->payload.literal);

    /* Allocate space for the data */
    char *tmp = (char *) malloc(sizeof(char) * length);
    if(tmp == NULL) {
        printf("Allocating space for literal data FAILED");
        return MALLOC_FAILED;
    }

    /* Copy the string over and store it in the union */
    strcpy(tmp, literal);
    data->payload.literal = tmp;
    data->type = LITERAL;
    data->size = length;
    data->used = length;

    return SUCCESS;
}


dt_err bowl_addpointer(struct bowl *data, void *ptr)
{
    if(data->type != UNSET)
        if(data->type != POINTER) return INVALID_PAYLOAD;

    data->payload.pointer = ptr;
    data->type = POINTER;
    data->size = sizeof(ptr);
    data->used = sizeof(ptr);

    return SUCCESS;
}


dt_err bowl_addnumeral(struct bowl *data, long numeral)
{
    /* Make sure we are a literal or unset data object */
    if(data->type != UNSET)
        if(data->type != NUMERIC) return INVALID_PAYLOAD;

    data->payload.numeral = numeral;
    data->type = NUMERIC;
    data->size = sizeof(int);
    data->used = sizeof(int);
    return SUCCESS;
}


dt_err bowl_addboolean(struct bowl *data, bool b)
{
    /* Make sure we are a literal or unset data object */
    if(data->type != UNSET)
        if(data->type != BOOLEAN) return INVALID_PAYLOAD;

    data->payload.boolean = b;
    data->type = BOOLEAN;
    data->size = sizeof(bool);
    data->used = sizeof(bool);
    return SUCCESS;
}


dt_err bowl_addlist(struct bowl *data, struct bowl *(*new_data))
{
    /* Make sure we are a literal or unset data object */
    if(data->type != UNSET)
        if(data->type != LIST) return INVALID_PAYLOAD;

    dt_err err;

    /* This means elements already exist */
    if(data->size > 0) {

        /* Used should never > size */
        if(data->used >= data->size) {
            data->size += RDB_REC_MULTIPLY;

            // TODO Use Realloc
            struct bowl **tmp = (struct bowl**) malloc(sizeof(struct bowl*) * data->size);
            memcpy(tmp, data->payload.list, sizeof(struct bowl*) * data->used);

            /* Free the list WITHOUT the children! */
            free(data->payload.list);
            data->payload.list = tmp;
        }

    /* This means the data object is new */
    } else {
        struct bowl **tmp = (struct bowl**) malloc(sizeof(struct bowl*) * RDB_REC_DEF_SIZE);
        data->payload.list = tmp;
        data->type = LIST;
        data->used = 0;
        data->size = RDB_REC_DEF_SIZE;
    }

    err = bowl_malloc(new_data);
    if(err) return err;

    /* Reference the slot, assign it, then move our ctr */
    data->payload.list[data->used++] = *new_data;

    return SUCCESS;
}


dt_err bowl_addpair(struct bowl *data, struct bowl *(*key), struct bowl *(*value))
{
    /* Make sure we are a literal or unset data object */
    if(data->type != UNSET) return INVALID_PAYLOAD;

    dt_err err;

    /* Malloc two nodes */
    err = bowl_malloc(key);
    if(err) goto cleanup;

    err = bowl_malloc(value);
    if(err) goto cleanup;

    /** Malloc space for PAIR */
    data->size = 2;
    struct bowl **tmp = (struct bowl**) malloc(sizeof(struct bowl*) * data->size);
    if(!tmp) goto cleanup;

    data->payload.list = tmp;

    { /* Assign data to new array */
        data->payload.list[data->used] = *key;
        data->used++;
        data->payload.list[data->used] = *value;
        data->used++;
    }

    /* Assign our new type and return */
    data->type = PAIR;
    return SUCCESS;

    /* Code we run when we can't allocate structs anymore */
    cleanup:
    free(*key);
    free(*value);
    free(tmp);
    return MALLOC_FAILED;
}


dt_err bowl_split_trees(struct bowl *data, struct bowl *sp)
{
    /* Make sure we are a literal or unset data object */
    if(data->type == UNSET) return INVALID_PAYLOAD;

    /* Check that sp is really a child of data */
    struct bowl *dp;
    int ret = list_search(&dp, data, sp);
    if(ret != 0) return DATA_NOT_RELATED;
    if(dp == NULL) return NODE_NOT_FOUND;

    /* Find the exact list reference and remove it */
    int i;
    for(i = 0; i < dp->used; i++) {
        if(dp->payload.list[i] == NULL) continue;

        /* Manually remove the entry */
        if(dp->payload.list[i] == sp) {
            dp->used--;
            dp->payload.list[i] = NULL;
        }
    }

    return SUCCESS;
}


dt_err bowl_merge_trees(struct bowl *data, struct bowl *merge)
{
    /* REALLY make sure the type is correct */
    if(data->type == UNSET) return INVALID_PARAMS;
    if(!(data->type == LIST || data->type == PAIR))
        return INVALID_PAYLOAD;

    /* This means elements already exist */
    if(data->size > 0) {

        /* Used should never > size */
        if(data->used >= data->size) {
            data->size += RDB_REC_MULTIPLY;

            struct bowl **tmp = (struct bowl**) malloc(sizeof(struct bowl*) * data->size);
            memcpy(tmp, data->payload.list, sizeof(struct bowl*) * data->used);

            /* Free the list WITHOUT the children! */
            free(data->payload.list);
            data->payload.list = tmp;
        }

        /* This means the data object is new */
    } else {
        struct bowl **tmp = (struct bowl**) malloc(sizeof(struct bowl*) * data->size);
        data->payload.list = tmp;
        data->type = LIST;
        data->used = 0;
        data->size = RDB_REC_DEF_SIZE;
    }

    /* Reference the slot, assign it, then move our ctr */
    data->payload.list[data->used] = merge;
    data->used++;

    return SUCCESS;
}


dt_err bowl_copy_deep(struct bowl *data, struct bowl *(*copy))
{
    if(data == NULL) return INVALID_PARAMS;
    dt_err err = SUCCESS;

    int it_type = -1;
    bowl_t type = data->type;

    /* Check if we're the first call */
    if((*copy) == NULL) bowl_malloc(copy);
    (*copy)->copy = DEEP;

    switch(type) {
        case LITERAL:
            bowl_addliteral(*copy, data->payload.literal);
            break;

        case NUMERIC:
            bowl_addnumeral(*copy, data->payload.numeral);
            break;

        case BOOLEAN:
            bowl_addboolean(*copy, data->payload.boolean);
            break;

        case LIST:
        {
            int i;
            int num = (int) data->used;

            for(i = 0; i < num; i++) {
                struct bowl *node = data->payload.list[i];

                struct bowl *new;
                bowl_addlist(*copy, &new);
                bowl_copy_deep(node, &new);
            }

            break;
        }

        case PAIR:
        {
            struct bowl *key, *val;
            bowl_addpair(*copy, &key, &val);

            struct bowl *orig_key = data->payload.list[0];
            struct bowl *orig_val = data->payload.list[1];

            bowl_copy_deep(orig_key, &key);
            bowl_copy_deep(orig_val, &val);

            break;
        }

        case POINTER:
            bowl_addpointer(*copy, data->payload.pointer);
            break;

        default:
            err = INVALID_PAYLOAD;
            break;
    }

    return err;
}


dt_err bowl_parent(struct bowl *root, struct bowl *data, struct bowl **parent)
{
    if(root == NULL || data == NULL) return INVALID_PARAMS;

    /* Blank the search pointer for easy error checking */
    (*parent) = NULL;

    switch(root->type) {

        /* Dead-end data stores automatically return @{NODE_NOT_FOUND} */
        case POINTER:
        case LITERAL:
        case BOOLEAN:
        case NUMERIC:
            return NODE_NOT_FOUND;

        case PAIR:
        case LIST:
            {
                int i;
                for(i = 0; i < root->used; i++) {

                    /* Check if the node we're looking at is what we're searching for */
                    if(root->payload.list[i] == data) {
                        (*parent) = root;
                        return SUCCESS;
                    }

                    dt_err err = bowl_parent(root->payload.list[i], data, parent);
                    if(err == SUCCESS) return SUCCESS;
                }
            }
            break;

        default:
            return INVALID_PAYLOAD;
    }

    return NODE_NOT_FOUND;
}


dt_err bowl_copy(struct bowl *data, struct bowl *(*copy))
{
    if(data == NULL) return INVALID_PARAMS;
    dt_err err = SUCCESS;

    /* Allocate a new node */
    err = bowl_malloc(copy);
    if(err) goto exit;

    /* Mark as shallow copy */
    (*copy)->copy = SHALLOW;

    /* Find out how to handle specific payloads */
    switch(data->type) {
        case LITERAL:
            err = bowl_addliteral(*copy, data->payload.literal);
            break;

        case NUMERIC:
            err = bowl_addnumeral(*copy, data->payload.numeral);
            break;

        case BOOLEAN:
            err = bowl_addboolean(*copy, data->payload.boolean);
            break;

        case LIST:
            (*copy)->type = LIST;
            (*copy)->payload.list = (struct bowl**) malloc(sizeof(struct bowl*) * data->size);
            memcpy((*copy)->payload.list, data->payload.list, sizeof(struct bowl*) * data->used);
            break;

        case PAIR:
            (*copy)->type = PAIR;
            (*copy)->payload.list = (struct bowl**) malloc(sizeof(struct bowl*) * data->size);
            memcpy((*copy)->payload.list, data->payload.list, sizeof(struct bowl*) * data->used);
            break;

        case POINTER:
            (*copy)->type = POINTER;
            memcpy((*copy)->payload.pointer, data->payload.pointer, sizeof(void*));
            break;

        default:
            return INVALID_PAYLOAD;
    }

    exit:
    return err;
}


dt_err bowl_search_payload(struct bowl *data, struct bowl *(*found), void *payload, bowl_t type)
{
    if(data == NULL) return INVALID_PARAMS;

    /* Make sure our pointer is clean */
    *found = NULL;

    if(data->type == LIST|| data->type == PAIR) {

        int i;
        for(i = 0; i < data->used; i++) {
            dt_err err = bowl_search_payload(data->payload.list[i], found, payload, type);
            if(err == SUCCESS) return SUCCESS;
        }

    } else {

        /* Check the type aligns */
        if(data->type != type) return NODE_NOT_FOUND;

        switch(type) {
            case LITERAL:
                if(strcmp(data->payload.literal, (char*) payload) == 0)
                    *found = data;
                break;

            case NUMERIC:
                if(data->payload.numeral == (long) payload)
                    *found = data;
                break;

            case BOOLEAN:
                if(data->payload.boolean == (bool) payload)
                    *found = data;
                break;

            case POINTER:
                if(data->payload.pointer == payload)
                    *found = data;
                break;

            default: return NODE_NOT_FOUND;
        }

    }

    return (*found == NULL) ? NODE_NOT_FOUND : SUCCESS;
}

// FIXME: This is horrible. Do via context?
static int reached = 0;
dt_err bowl_search_keypayload(struct bowl *data, struct bowl *(*found), void *payload, bowl_t type, int depth)
{
    if(data == NULL) return INVALID_PARAMS;
    if(reached++ >= depth) return QUERY_TOO_DEEP;

    /* Make sure our pointer is clean */
    *found = NULL;

    /* We can only search LISTed values or PAIRs */
    if(data->type == PAIR) {
        struct bowl *key = data->payload.list[0];

        bowl_t tt;
        int hit = -1;

        if(strcmp(key->payload.literal, (char*) payload) == 0) {
            tt = LITERAL;
            hit = 0;
        }

        if(key->payload.numeral == (long) payload) {
            tt = NUMERIC;
            hit = 0;
        }

        if(key->payload.boolean == (bool) payload) {
            tt = BOOLEAN;
            hit = 0;
        }

        if(hit == 0) *found = data->payload.list[1];

    } else if(data->type == LIST) {

        int i;
        for(i = 0; i < data->used; i++) {
            bowl_search_keypayload(data->payload.list[i], found, payload, type, depth);
        }

    } else {


    }

    if(data->type == LIST|| data->type == PAIR) {

        int i;
        for(i = 0; i < data->used; i++) {
            dt_err err = bowl_search_payload(data->payload.list[i], found, payload, type);
            if(err == SUCCESS) return SUCCESS;
        }

    } else {

        /* Check the type aligns */
        if(data->type != type) return NODE_NOT_FOUND;

        switch(type) {
            case LITERAL:
                if(strcmp(data->payload.literal, (char*) payload) == 0)
                    *found = data;
                break;

            case NUMERIC:
                if(data->payload.numeral == (long) payload)
                    *found = data;
                break;

            case BOOLEAN:
                if(data->payload.boolean == (bool) payload)
                    *found = data;

            case POINTER:
                if(data->payload.pointer == payload)
                    *found = data;
                break;

            default: return NODE_NOT_FOUND;
        }

    }

    return (*found == NULL) ? NODE_NOT_FOUND : SUCCESS;
}


void list_print(struct bowl *data, const char *offset)
{
    bowl_t type = data->type;

    switch(type) {
        case UNSET:
            printf("[NULL]\n");
            break;

        case LITERAL:
            printf("%s['%s']\n", offset, data->payload.literal);
            break;

        case NUMERIC:
            printf("%s[%lu]\n", offset, data->payload.numeral);
            break;

        case BOOLEAN:
            printf("%s['%s']\n", offset, (data->payload.boolean) ? "TRUE" : "FALSE");
            break;

        case PAIR:
        {
            bowl_t k_type = data->payload.list[0]->type;
            bowl_t v_type = data->payload.list[1]->type;

            if(k_type == LITERAL) printf("%s['%s']", offset, data->payload.list[0]->payload.literal);
            if(k_type == NUMERIC) printf("%s[%lu]", offset, data->payload.list[0]->payload.numeral);
            if(k_type == BOOLEAN) printf("%s[%s]", offset, (data->payload.list[0]->payload.boolean) ? "TRUE" : "FALSE");

            char new_offset[REAL_STRLEN(offset) + 2];
            strcpy(new_offset, offset);
            strcat(new_offset, "  ");

            if(k_type == LIST || k_type == PAIR) list_print(data->payload.list[0], new_offset);

            /* Print the value now */
            if(v_type == LITERAL) printf(" => ['%s']\n", data->payload.list[1]->payload.literal);
            if(v_type== NUMERIC) printf(" => [%lu]\n", data->payload.list[1]->payload.numeral);
            if(v_type == BOOLEAN) printf(" => [%s]\n", (data->payload.list[1]->payload.boolean) ? "TRUE" : "FALSE");

            if(v_type == LIST || k_type == PAIR) list_print(data->payload.list[1], new_offset);

            break;
        }

        case LIST:
        {
            int i;
            printf("%s[LIST]\n", offset);
            for(i = 0; i < data->used; i++) {
                bowl_t t = data->payload.list[i]->type;

                /* Calculate the new offset */
                char new_offset[REAL_STRLEN(offset) + 2];
                strcpy(new_offset, offset);
                strcat(new_offset, "  ");

                switch(t) {
                    case LITERAL:
                    case BOOLEAN:
                    case NUMERIC:
                        list_print(data->payload.list[i], new_offset);
                        continue;

                    case LIST:
                        list_print(data->payload.list[i], new_offset);
                        continue;

                    case PAIR:
                        printf("%s[PAIR] <==> ", new_offset);
                        list_print(data->payload.list[i], new_offset);
                        continue;

                    default:
                        break;
                }
            }
            break;
        }

        default:
            break;

    }
}


void bowl_print(struct bowl *data)
{
    list_print(data, "");
}

dt_err bowl_get(struct bowl *data, void *(*val))
{
    if(data->type == LITERAL) *val = data->payload.literal;
    if(data->type == NUMERIC) *val = &data->payload.numeral;
    if(data->type == BOOLEAN) *val = &data->payload.boolean;
    if(data->type == LIST || data->type == PAIR) *val = (struct bowl*) data->payload.list;
    return SUCCESS;
}


dt_err bowl_free(struct bowl *data)
{
    if(data == NULL) return SUCCESS;
    if(data->copy == SHALLOW) return NODE_NOT_ORIGINAL;

    if(data->type == LITERAL) {
        if(data->payload.literal) free(data->payload.literal);

    } else if(data->type == LIST || data->type == PAIR) {
        int i;
        dt_err err;
        for(i = 0; i < data->used; i++) {

            err = bowl_free(data->payload.list[i]);
            if(err) return err;
        }

        free(data->payload.list);

    } else if(data->type == POINTER) {
        if(data->copy != SHALLOW && data->payload.pointer)
            free(data->payload.pointer);
    }

    free(data);
    return SUCCESS;
}


dt_err bowl_free_shallow(struct bowl *data)
{
    if(data == NULL) return SUCCESS;

    if(data->type == LITERAL) {
        if(data->payload.literal) free(data->payload.literal);
    } else if(data->type == LIST || data->type == PAIR) {
        int i;
        dt_err err;
        for(i = 0; i < data->size; i++) {
            err = bowl_free(data->payload.list[i]);
            if(err) return err;
        }

        free(data->payload.list);
    }

    free(data);
    return SUCCESS;
}


const char *bowl_dtype(struct bowl *data)
{
    switch(data->type) {
        case LITERAL:       return "Literal";
        case NUMERIC:       return "Numeric";
        case BOOLEAN:       return "Boolean";
        case LIST:          return "List";
        case PAIR:          return "Pair";
        case POINTER:       return "Pointer";
        default:            return "Unknown";
    }
}


/**************** PRIVATE UTILITY FUNCTIONS ******************/


/**
 * Steps down the list hirarchy of a dyntree node to
 * find a sub-child target. Returns 0 if it can be found.
 *
 * @param data
 * @param target
 * @return
 */
int list_search(struct bowl **direct_parent, struct bowl *data, struct bowl *target)
{
    /* Check if data is actually valid */
    if(data == NULL) return 1;

    /* Compare the pointers :) */
    if(data == target) return 0;

    int res = 1;
    if(data->type == LIST || data->type == PAIR) {
        int i;
        for(i = 0; i < data->used; i++) {
            res = list_search(direct_parent, data->payload.list[i], target);
            if(res == 0) {

                /* Save the node that contains our child for later */
                (*direct_parent) = data;
                return res;
            }
        }
    }

    return res;
}


/**
 * Small utility function that checks if a datablock is valid to write into.
 * Potentially releases previously owned memory to prevent memory leaks
 *
 * @param data The bowl object to check
 * @return
 */
dt_err data_check(struct bowl *data)
{
    /* Check if the data block has children */
    if(data->type == LIST)
    {
        printf("Won't override heap payload with data!");
        return INVALID_PAYLOAD;
    }

    /* Free the existing string */
    if(data->type == LITERAL)
    {
        if(data->payload.literal) free(data->payload.literal);
    }

    return SUCCESS;
}