/* Contrôle de liste chainé */ #include "strct.h" #include <stdio.h> #include <stdlib.h> /** Crée une nouvelle liste chainé **/ tpl_data new_tpl() { tpl_data data; data = malloc(sizeof(struct tpl_data_s)); data->key = 0; data->next = 0; return data; } /** Affiche sur la sortie standard les valeurs de la liste **/ void tpl_debug(tpl_data data) { fputs("*****TPL VARLIST*****\n", stderr); if (data->key == 0) { fputs("Empty data\n", stderr); } else { while (data != 0) { fprintf(stderr, "%s => %s\n", data->key, data->value); data = data->next; } } fputs("*****END TPL VARLIST*****\n", stderr); } /** Ajoute / modifie une valeur dans la liste **/ void tpl_updt(tpl_data data, char* key, char* value) { if (data->key == 0) { data->key = key; data->value = value; } else { /* Find the right data */ while (strcmp(data->key, key) != 0 && data->next != 0) { data = data->next; } /* Yes, we found it ! */ if (strcmp(data->key, key) == 0) { free(data->value); data->value = value; } /* No, we had to add it */ else { tpl_data new = new_tpl(); new->key = key; new->value = value; data->next = new; } } } char* tpl_get(tpl_data data, char* key) { while (data != 0 && data->key != 0 && strcmp(data->key, key) != 0) { data = data->next; } if (data == 0) { return ""; } else { return data->value; } }