-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacro.c
55 lines (49 loc) · 1.16 KB
/
macro.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
#include <vector.h>
#include <string.h>
#include <stdio.h>
#include <jpp_macro.h>
vector_t macros;
static int compare_macro(const void *a,const void *b)
{
struct jpp_macro *m1=(struct jpp_macro*)a;
struct jpp_macro *m2=(struct jpp_macro*)b;
return strncmp(m1->name,m2->name,sizeof(m1->name));
}
struct jpp_macro *search_macro(char *name)
{
struct jpp_macro key;
strncpy(key.name,name,sizeof(key.name));
int index=vector_find(¯os,&key,compare_macro);
if(index<0){
return NULL;
}
return vector_at(¯os,index);
}
int add_macro(char *name,char *definition)
{
struct jpp_macro macro;
if(search_macro(name)!=NULL){
fprintf(stderr,"error: Redeclaration of macro `%s'\n",name);
return -1;
}
memset(¯o,0,sizeof(macro));
strncpy(macro.name,name,sizeof(macro.name));
if(definition!=NULL){
strncpy(macro.definition,definition,sizeof(macro.definition));
}
vector_pushback(¯os,¯o);
return 0;
}
int init_macro(void)
{
vector_new(¯os,sizeof(struct jpp_macro));
// TODO: Add built-in macros
add_macro("JPP_AUTHOR","Travor Liu");
add_macro("JPP_AUTHOR","Travor Liu");
return 0;
}
int destroy_macro(void)
{
vector_destroy(¯os);
return 0;
}