1 //////////////////////////////////////////////////////////////////////////////
2 //Copyright 2008
3 //  Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
4 //////////////////////////////////////////////////////////////////////////////
5 // This file is part of Teyjus.                                             //
6 //                                                                          //
7 // Teyjus is free software: you can redistribute it and/or modify           //
8 // it under the terms of the GNU General Public License as published by     //
9 // the Free Software Foundation, either version 3 of the License, or        //
10 // (at your option) any later version.                                      //
11 //                                                                          //
12 // Teyjus is distributed in the hope that it will be useful,                //
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of           //
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
15 // GNU General Public License for more details.                             //
16 //                                                                          //
17 // You should have received a copy of the GNU General Public License        //
18 // along with Teyjus.  If not, see <http://www.gnu.org/licenses/>.          //
19 //////////////////////////////////////////////////////////////////////////////
20 #include <stdlib.h>
21 #include <string.h>
22 #include "../include/standardlib.h"
23 #include "../system/error.h"
24 #include "tree.h"
25 #include "datatypes.h"
26 
27 typedef struct{
28   char* key;
29   MarkInd data;
30 }entry;
31 
compar_fn(const void * pa,const void * pb)32 int compar_fn(const void* pa,const void* pb)
33 {
34   const char* na = ((const entry*) pa) -> key;
35   const char* nb = ((const entry*) pb) -> key;
36   return strcmp(na,nb);
37 }
38 
free_node(void * p)39 void free_node(void* p)
40 {
41   free(((entry*)p)->key);
42   free(p);
43 }
44 
LK_TREE_Add(void ** root,char * key,MarkInd ind)45 void LK_TREE_Add(void** root, char* key, MarkInd ind)
46 {
47   entry* p=(entry*)EM_malloc(sizeof(entry));
48   p->key=(char *)EM_malloc(strlen(key)+1);
49   strcpy(p->key,key);
50   p->data=ind;
51   if(!tsearch((void*)p,root,compar_fn))
52   {
53     EM_THROW(EM_OUT_OF_MEMORY);
54   }
55 }
56 
LK_TREE_Retrieve(void ** root,char * key)57 MarkInd LK_TREE_Retrieve(void **root, char* key)
58 {
59   entry** p;
60   entry e;
61 
62   e.key=key;
63   e.data.gl_flag=3;
64 
65   p=(entry**)tfind((void*)&e,root,compar_fn);
66   if(NULL==p){
67     EM_THROW(LK_LinkError);
68   }
69   return (*p)->data;
70 }
71 
LK_TREE_Empty(void ** root)72 void LK_TREE_Empty(void **root)
73 {
74   tdestroy(*root,free_node);
75   *root=NULL;
76 }
77