-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacrofunctions.c
More file actions
164 lines (135 loc) · 3.76 KB
/
macrofunctions.c
File metadata and controls
164 lines (135 loc) · 3.76 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "macrofunctions.h"
MacroNode *init_macro_table()
{
MacroNode *head = (MacroNode *)malloc(sizeof(MacroNode)); /*Allocate size of macro list head*/
if (!head)
{
free(head);
handle_system_error(ERROR_MEMORY_ALLOCATION_FAILED);
return NULL;
}
head->macro = NULL;
head->next = NULL;
return head;
}
ErrorCode add_macro(MacroNode *head, char *name)
{
Macro *newMacro = (Macro *)malloc(sizeof(Macro)); /*Allocate size of macro*/
MacroNode *newNode = (MacroNode *)malloc(sizeof(MacroNode)); /*Allocate size of macro node*/
if (!head || !name)
return ERROR_NULL_PARAM;
if (!newMacro)
return ERROR_MEMORY_ALLOCATION_FAILED;
if (!newNode)
{
free(newMacro);
return ERROR_MEMORY_ALLOCATION_FAILED;
}
newMacro->name = (char *)malloc(strlen(name) + 1); /*Allocate size of macro name*/
if (!newMacro->name)
{
free(newMacro);
free(newNode);
return ERROR_MEMORY_ALLOCATION_FAILED;
}
strcpy(newMacro->name, name); /*Copy name to macro name*/
newMacro->content = (char *)malloc(MAX_LINE * MAX_MACRO_LINES * sizeof(char)); /*Allocate size of macro content to be 80 lines*/
if (!newMacro->content)
{
free(newMacro->name);
free(newMacro);
return ERROR_MEMORY_ALLOCATION_FAILED;
}
newMacro->content[0] = '\0'; /*Initialize content to empty string*/
newNode->macro = newMacro;
newNode->next = head->next;
head->next = newNode;
return SUCCESS;
}
void free_macro_table(MacroNode *head)
{
MacroNode *temp = NULL;
if (!head)
{
return;
}
/*Loops over macro list and free every macro*/
while (head != NULL)
{
temp = head;
head = head->next;
if (temp->macro)
{
if (temp->macro->name != NULL)
free(temp->macro->name);
if (temp->macro->content != NULL)
free(temp->macro->content);
free(temp->macro);
}
free(temp);
}
}
Macro *find_macro(MacroNode *head, char *name)
{
MacroNode *temp;
if (head == NULL)
return NULL;
temp = head->next;
/*Loop over macro list and search for macro by name*/
while (temp != NULL)
{
if (strcmp(temp->macro->name, name) == 0)
{
return temp->macro;
}
temp = temp->next;
}
return NULL;
}
Macro *get_current_macro(MacroNode *head)
{
MacroNode *temp;
if (head == NULL || head->next == NULL)
return NULL;
temp = head;
/*Get last macro in the list*/
while (temp->next != NULL)
{
temp = temp->next;
}
return temp->macro;
}
ErrorCode validate_macro_name(char *name, MacroNode *head)
{
if (name == NULL)
return ERROR_NULL_PARAM;
/*Validate macro name length*/
if (strlen(name) == 0)
return ERROR_MACRO_EMPTY_NAME;
if (strlen(name) > MAX_MACRO_NAME)
return ERROR_MACRO_NAME_TOO_LONG;
/*Check if first letter is alphabetic*/
if (!isalpha(name[0]) && name[0] != '_')
return ERROR_MACRO_INVALID_START;
if (is_reserved_word(name))
return ERROR_MACRO_RESERVED_WORD;
/*Check if macro already created*/
if (head && find_macro(head, name) != NULL)
return ERROR_MACRO_ALREADY_EXISTS;
/*Check if all chars are alphanumeric or _ */
while (name && (name[0] != '\0'))
{
if (!isalnum(name[0]) && (name[0] != '_'))
return ERROR_MACRO_NOT_ALPHANUMERIC;
name++;
}
return SUCCESS;
}
int is_macro(char *name, MacroNode *head)
{
if (!name || !head)
return FALSE;
if (find_macro(head, name) != NULL)
return TRUE;
return FALSE;
}