1 /* option.c -- Simple data structure for storing a name/value pair
2
3 Copyright 2001 Carl Worth
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <errno.h>
19
20 #include "option.h"
21
option_init(option_t * option,char * name,char * value)22 int option_init(option_t *option, char *name, char *value)
23 {
24 option->name = name;
25 option->value = value;
26
27 return 0;
28 }
29
option_deinit(option_t * option)30 void option_deinit(option_t *option)
31 {
32 option->name = NULL;
33 option->value = NULL;
34 }
35
option_list_init(option_list_t * list)36 int option_list_init(option_list_t *list)
37 {
38 list->num_options = 0;
39 list->options = NULL;
40
41 return 0;
42 }
43
option_list_append(option_list_t * list,option_t * option)44 int option_list_append(option_list_t *list, option_t *option)
45 {
46 option_t *new_options;
47
48 list->num_options++;
49
50 new_options = realloc(list->options, list->num_options * sizeof(option_t));
51 if (new_options == NULL) {
52 fprintf(stderr, "%s: Out of memory.\n", __FUNCTION__);
53 list->num_options--;
54 return ENOMEM;
55 }
56
57 list->options = new_options;
58 list->options[list->num_options - 1] = *option;
59
60 return 0;
61 }
62
option_list_deinit(option_list_t * list)63 void option_list_deinit(option_list_t *list)
64 {
65 free(list->options);
66 list->options = NULL;
67 list->num_options = 0;
68 }
69