-
Notifications
You must be signed in to change notification settings - Fork 0
/
sat_variable.c
54 lines (41 loc) · 993 Bytes
/
sat_variable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "sat_variable.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static variable_t *variable_alloc(char *name, int id)
{
variable_t *var;
if (!(var = calloc(1, sizeof(variable_t))))
return NULL;
snprintf(var->name, sizeof(var->name), "%s", name);
var->id = id;
return var;
}
static void variable_free(variable_t *var)
{
free(var);
}
int variable_add(variable_t **variables, char *name)
{
int cnt = 1;
for ( ; *variables; variables = &(*variables)->next) {
if (!strcmp((*variables)->name, name))
return (*variables)->id;
cnt++;
}
return ((*variables) = variable_alloc(name, cnt)) ?
(*variables)->id : -1;
}
void variables_clr(variable_t **variables)
{
variable_t *tmp;
while ((tmp = *variables)) {
*variables = (*variables)->next;
variable_free(tmp);
}
}
char *literal_id2name(variable_t *variables, int id)
{
for (; variables && variables->id != id; variables = variables->next);
return variables ? variables->name : "unknown";
}