Initial version of donated sources by Avertec, 3.4p5.
[tas-yagle.git] / distrib / sources / api / api / gen_templates.c
1 #include API_H
2
3 api_define_type *create_adt(char * orig, char *dest)
4 {
5 api_define_type *adt;
6 adt=(api_define_type *)mbkalloc(sizeof(api_define_type));
7 adt->ORIG=mbkstrdup(orig);
8 adt->DEST=mbkstrdup(dest);
9 return adt;
10 }
11
12 chain_list *dup_adt_list(chain_list *cl)
13 {
14 api_define_type *adt;
15 if (cl==NULL) return cl;
16 adt=(api_define_type *)mbkalloc(sizeof(api_define_type));
17 adt->ORIG=mbkstrdup(((api_define_type *)cl->DATA)->ORIG);
18 adt->DEST=mbkstrdup(((api_define_type *)cl->DATA)->DEST);
19 return addchain(dup_adt_list(cl->NEXT), adt);
20 }
21
22 void free_adt_list(chain_list *cl)
23 {
24 chain_list *ch;
25 api_define_type *adt;
26 for (ch=cl; ch!=NULL; ch=ch->NEXT)
27 {
28 adt=(api_define_type *)ch->DATA;
29 mbkfree(adt->ORIG);
30 mbkfree(adt->DEST);
31 mbkfree(adt);
32 }
33 freechain(cl);
34 }
35
36 int gen_find_template_corresp(ht *base_ht, chain_list *defines, char *name, char *result)
37 {
38 char buf[1024], *end, *start;
39 chain_list *cl;
40 api_define_type *adt;
41 long l;
42
43
44 strcpy(buf, name);
45 start=buf;
46
47 do
48 {
49 end=strchr(start, '.');
50 if (end!=NULL) *end='\0';
51 for (cl=defines; cl!=NULL; cl=cl->NEXT)
52 {
53 adt=(api_define_type *)cl->DATA;
54 if (strcasecmp(adt->ORIG, start)==0) break;
55 }
56 if (cl!=NULL && end!=NULL)
57 {
58 if (base_ht==NULL)
59 {
60 strcpy(result, name);
61 return 0;
62 }
63 if ((l=gethtitem(base_ht, namealloc(start)))==EMPTYHT)
64 break;
65 else
66 defines=((template_corresp *)l)->defines;
67 start=end+1;
68 }
69 } while (cl!=NULL && end!=NULL);
70
71 if (end==NULL)
72 {
73 if (cl==NULL)
74 strcpy(result, start);
75 else
76 strcpy(result, adt->DEST);
77 return 0;
78 }
79 strcpy(result,"<not found>");
80 return 1;
81 }
82
83 template_corresp *gen_get_template_corresp(ht *base_ht, char *name)
84 {
85 long l;
86 if ((l=gethtitem(base_ht, namealloc(name)))==EMPTYHT) return NULL;
87 return (template_corresp *)l;
88 }
89
90 template_corresp *gen_add_template_corresp(ht *base_ht, char *new_name, char *orig_name, chain_list *defines)
91 {
92 template_corresp *tc;
93
94 tc=(template_corresp *)mbkalloc(sizeof(template_corresp));
95 tc->new_name=namealloc(new_name);
96 tc->orig_name=namealloc(orig_name);
97 tc->defines=dup_adt_list(defines);
98
99 addhtitem(base_ht, tc->new_name, (long)tc);
100
101 return tc;
102 }
103
104 int gen_find_reverse_template_corresp(chain_list *defines, char *name, char *result)
105 {
106 chain_list *cl;
107 api_define_type *adt;
108
109 for (cl=defines; cl!=NULL; cl=cl->NEXT)
110 {
111 adt=(api_define_type *)cl->DATA;
112 if (strcasecmp(adt->DEST, name)==0) break;
113 }
114
115 if (cl==NULL)
116 {
117 strcpy(result,"<not found>");
118 return 1;
119 }
120
121 strcpy(result, adt->ORIG);
122 return 0;
123 }