-
Notifications
You must be signed in to change notification settings - Fork 0
/
ids_list.c
128 lines (104 loc) · 2.34 KB
/
ids_list.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Project: IFJ21 imperative language compiler
*
* Brief: Linked list for identifiers used in parser
*
* Author: David Chocholaty <xchoch09>
*
*/
#include <stdlib.h>
#include <string.h>
#include "ids_list.h"
#include "error.h"
#include "data_types.h"
void idInsert(ids_list_t** ids_list, data_type_t type, char* id)
{
ids_list_t* newId = (ids_list_t*) malloc(sizeof(ids_list_t));
if(!newId){
err = E_INTERNAL;
return;
}
newId->id = (char*) malloc(strlen(id)+1);
if (newId->id == NULL)
{
free(newId);
newId = NULL;
err = E_INTERNAL;
return;
}
strcpy(newId->id, id);
newId->type = type;
if(*ids_list == NULL){
*ids_list = newId;
(*ids_list)->next = NULL;
}else{
ids_list_t* current;
current = (*ids_list);
while(current->next != NULL){
current = current->next;
}
newId->next = NULL;
current->next = newId;
}
}
void save_ids_list(ids_list_t* orig, ids_list_t** dest)
{
ids_list_t* current = orig;
while (current != NULL)
{
idInsert(dest, current->type, current->id);
current = current->next;
}
}
char* get_last_id (ids_list_t* ids_list)
{
ids_list_t* current = ids_list;
if (current == NULL)
{
return NULL;
}
while (current->next != NULL)
{
current = current->next;
}
return current->id;
}
void delete_last_id (ids_list_t** ids_list)
{
ids_list_t* current = *ids_list;
if (current == NULL)
{
return;
}
/* Last element in list */
if (current->next == NULL)
{
free(current->id);
current->id = NULL;
free(current);
current = NULL;
*ids_list = current;
return;
}
while (current->next->next != NULL)
{
current = current->next;
}
free(current->next->id);
current->next->id = NULL;
free(current->next);
current->next = NULL;
}
void delete_ids_list(ids_list_t* ids_list)
{
ids_list_t* current = NULL;
while (ids_list != NULL)
{
current = ids_list;
ids_list = ids_list->next;
free(current->id);
current->id = NULL;
free(current);
current = NULL;
}
}