main soggy/cotton / src / cotton.c
 1#include "cotton.h"
 2#include <stdio.h>
 3#include <stdlib.h>
 4#include <string.h>
 5#include <time.h>
 6
 7void cotton_init
 8(Cotton *c)
 9{
10    memset(c, 0, sizeof(Cotton));
11
12    srand((unsigned int)time(NULL));
13
14    c->c_cottolette = COTTOLETTE[1];
15}
16
17void cotton_store_var
18(Cotton *cotton, const char *name, const char *value)
19{
20    for (int i = 0; i < cotton->var_count; i++) {
21        if (strcmp(cotton->vars[i].name, name) == 0) {
22            strncpy(cotton->vars[i].value, value, VAR_VAL_LEN - 1);
23            cotton->vars[i].value[VAR_VAL_LEN - 1] = '\0';
24            return;
25        }
26    }
27
28    if (cotton->var_count < VARS) { 
29        strncpy(cotton->vars[cotton->var_count].name, name, VAR_NAME_LEN - 1);
30        cotton->vars[cotton->var_count].name[VAR_NAME_LEN - 1] = '\0';
31
32        strncpy(cotton->vars[cotton->var_count].value, value, VAR_VAL_LEN - 1);
33        cotton->vars[cotton->var_count].value[VAR_VAL_LEN - 1] = '\0';
34
35        cotton->var_count++;
36    } else {
37        fprintf(stderr, "cotton ran out of variable slots :( - max ammount is %d\n", VARS);
38    }
39}