1 /* ====================================================================
2 * Copyright 1999 Web Juice, LLC. All rights reserved.
3 *
4 * varlist.c
5 *
6 * Functions for manipulating the variable list structure in the template
7 * library.
8 *
9 * ==================================================================== */
10
11 #include <string.h>
12 #include <stdlib.h>
13
14 #include <varlist.h>
15 #include <template.h>
16
17
18
19 /* ====================================================================
20 * NAME: varlist_init
21 *
22 * DESCRIPTION: Initializes and returns a pointer to a new varlist
23 * structure.
24 *
25 * RETURN VALUES: Returns NULL if the memory allocation fails; otherwise
26 * returns a pointer to a varlist structure.
27 *
28 * BUGS: Hopefully none.
29 * ==================================================================== */
30 varlist_p
varlist_init(void)31 varlist_init(void)
32 {
33 varlist_p variable_list;
34
35 variable_list = (varlist_p)malloc(sizeof(varlist));
36 if (variable_list == NULL)
37 {
38 template_errno = TMPL_EMALLOC;
39 return NULL;
40 }
41
42 variable_list->next = NULL;
43 variable_list->name = NULL;
44 variable_list->value = NULL;
45
46 return(variable_list);
47 }
48
49
50
51 /* ====================================================================
52 * NAME: varlist_destroy
53 *
54 * DESCRIPTION: Frees up all memory associated with a varlist.
55 *
56 * RETURN VALUES: None.
57 *
58 * BUGS: Because a free()d pointer still *looks* valid, it is
59 * difficult to protect against the problems that arise
60 * if the user calls this function too early.
61 * ==================================================================== */
62 void
varlist_destroy(varlist_p variable_list)63 varlist_destroy(varlist_p variable_list)
64 {
65 varlist_p next;
66
67 if (variable_list == NULL)
68 {
69 return;
70 }
71
72 next = variable_list->next;
73
74 variable_list->next = NULL;
75 if (variable_list->name != NULL)
76 {
77 free(variable_list->name);
78 }
79 if (variable_list->value != NULL)
80 {
81 free(variable_list->value);
82 }
83 free(variable_list);
84
85 varlist_destroy(next);
86 }
87
88
89
90 /* ====================================================================
91 * NAME: varlist_get_value
92 *
93 * DESCRIPTION: Returns the value of the variable stored under the given
94 * name, if it exists.
95 *
96 * RETURN VALUES: This function will return NULL if there are any problems
97 * or if there is no value. It returns a string containing
98 * the value on success.
99 *
100 * BUGS: Hopefully none.
101 * ==================================================================== */
102 char *
varlist_get_value(varlist_p variable_list,char * name)103 varlist_get_value(varlist_p variable_list, char *name)
104 {
105 varlist_p current = variable_list;
106
107 while (current != NULL)
108 {
109 if ((current->name != NULL) && (current->value != NULL)
110 && (strcmp(current->name, name) == 0))
111 {
112 return(current->value);
113 }
114 current = current->next;
115 }
116 template_errno = TMPL_ENOVALUE;
117 return NULL;
118 }
119
120
121
122 /* ====================================================================
123 * NAME: varlist_set_value
124 *
125 * DESCRIPTION: Set a variable in the given varlist to be equal to
126 * the value given.
127 *
128 * RETURN VALUES: Returns 0 if there's any trouble. Returns 1 if the
129 * variable was successfully set.
130 *
131 * BUGS: Hopefully none.
132 * ==================================================================== */
133 int
varlist_set_value(varlist_p * variable_list,char * name,char * value)134 varlist_set_value(varlist_p *variable_list, char *name, char *value)
135 {
136 varlist_p new = NULL;
137 int length;
138
139 /* Make sure that the name and value aren't NULL */
140 if ((name == NULL) || (value == NULL))
141 {
142 template_errno = TMPL_ENULLARG;
143 return 0;
144 }
145
146 new = varlist_init();
147 if (new == NULL)
148 {
149 return 0;
150 }
151
152 length = strlen(name);
153 new->name = (char *)malloc(length + 1);
154 strncpy(new->name, name, length);
155 new->name[length] = '\0';
156
157 length = strlen(value);
158 new->value = (char *)malloc(length + 1);
159 strncpy(new->value, value, length);
160 new->value[length] = '\0';
161
162 new->next = *variable_list;
163
164 *variable_list = new;
165
166 return 1;
167 }
168