| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #include "stringchicken.h"
- #include <stddef.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /*
- parser that can read K=V, maybe also K:V? although would just be configurable seperator
- I know python uses dictionary which uses a hash table so key -> hash -> value table
- for this assignment, I could use 2 arrays? though that's... a bit dangerous? or at least tricky, in terms of reassignments.
- let's just go with a struct with 2 dynamic arrays where the indices are aligned
- ik neem dus eerst 1 die het omzet naar een struct van strings, dan een struct van de KV values.
- */
- struct stringchicken *create_string_chicken(){
- struct stringchicken *mystruct = malloc(sizeof(struct stringchicken));
- mystruct->length = 0;
- mystruct->capacity = 2;
- mystruct->data = malloc(sizeof(char*) * mystruct->capacity);
- return mystruct;
- }
- void push_SC(char *string_input, struct stringchicken *struct_input){
- if (struct_input->length == struct_input->capacity) {
- struct_input->capacity*=2;
- char **tempptr = realloc(struct_input->data, sizeof(char*) * struct_input->capacity);
- if (tempptr == NULL) {
- exit(1);
- }
- struct_input->data = tempptr;
- }
-
- char *newstring = malloc(strnlen(string_input+1, sizeof(char)*101));
- strcpy(newstring,string_input);
- struct_input->data[struct_input->length] = newstring;
- struct_input->length++;
- }
- void free_SC(struct stringchicken *struct_input){
- for (int i = 0; i < struct_input->length; i++) {
- free(struct_input->data[i]);
- }
- free(struct_input->data);
- free(struct_input);
- }
- void print_SC(struct stringchicken *input_struct){
- for (size_t i = 0; i < input_struct->length; i++) {
- printf("line:%zu equals %s", i, input_struct->data[i]);
- }
- }
|