aboutsummaryrefslogtreecommitdiff
path: root/src/list.c
blob: a0b9f5c6f1c260dbf9f54877e7a659f295614693 (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
/*
 * msg
 * Copyright (C) 2025  Raghuram Subramani <raghus2247@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <list.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <util.h>

list_t *
list_create(size_t element_size)
{
    list_t *list = malloc(sizeof(list_t));
    if (list == NULL)
        return NULL;

    list->element_size = element_size;
    list->size = 0;
    list->max = START_SIZE;
    list->elements = (uint8_t *) calloc(list->max, list->element_size);

    return list;
}

void
list_add(list_t *list, void *element)
{
    if (list->size == list->max) {
        list->max += INCREMENT_BY;
        list->elements = (uint8_t *) realloc(list->elements,
                                             list->element_size * list->max);
        if (list->elements == NULL) {
            /* TODO: Handle error */
            printf("Failed to reallocate array\n");
            return;
        }
    }

    void *new_element = list->elements + list->element_size * list->size;
    new_element = memcpy(new_element, element, list->element_size);

    if (new_element == NULL)
        printf("Failed to add a new element\n");

    list->size++;
}

void
list_wrap_and_add(list_t *list, void *element)
{

    ptr_wrapper_t *wrapped = wrap_ptr(element);
    list_add(list, wrapped);
    free(wrapped);
}

void *
list_get(list_t *list, size_t i)
{
    return list->elements + (i * list->element_size);
}

void
list_delete(list_t *list)
{
    free(list->elements);
    free(list);
}

void *
list_find_corresponding_value_from_ptr_wrapper(list_t *keys,
                                               list_t *values,
                                               char *key)
{
    for (size_t i = 0; i < keys->size; i++) {
        ptr_wrapper_t *wrapper = list_get(keys, i);
        if (strcmp(wrapper->ptr, key) == 0) {
            return list_get(values, i);
        }
    }

    return NULL;
}