1 /*
2    Copyright (c) 2003 Perry Rapp
3    "The MIT license"
4    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 */
8 
9 /*=============================================================
10  * lldatabase.c -- Interface to lifelines databases
11  *   Created: 2003/10 by Perry Rapp
12  *==============================================================*/
13 
14 #include "llstdlib.h"
15 #include "gedcom.h"
16 #include "gedcomi.h"
17 #include "btree.h"
18 #include "vtable.h"
19 #include "dbcontext.h"
20 
21 
22 /*********************************************
23  * global/exported variables
24  *********************************************/
25 
26 LLDATABASE def_lldb = 0;
27 BTREE BTR=NULL;	/* database */
28 
29 /*********************************************
30  * external/imported variables
31  *********************************************/
32 
33 extern STRING readpath,readpath_file;
34 
35 /*********************************************
36  * local types
37  *********************************************/
38 
39 struct tag_lldatabase {
40 	BTREE btree;
41 };
42 
43 /*********************************************
44  * local & exported function definitions
45  * body of module
46  *********************************************/
47 
48 /*========================================
49  * lldb_alloc -- Create lldb structure
50  * This does not actually create or open a database.
51  * It only creates this structure to hold the errors
52  * and point to the database when it is opened (or
53  * created) later.
54  *======================================*/
55 LLDATABASE
lldb_alloc(void)56 lldb_alloc (void)
57 {
58 	LLDATABASE lldb = (LLDATABASE)stdalloc(sizeof(*lldb));
59 	memset(lldb, 0, sizeof(*lldb));
60 	return lldb;
61 }
62 /*========================================
63  * lldb_set_btree -- Make this lldb point to
64  * an actual btree database
65  *======================================*/
66 void
lldb_set_btree(LLDATABASE lldb,void * btree)67 lldb_set_btree (LLDATABASE lldb, void *btree)
68 {
69 	ASSERT(lldb);
70 	ASSERT(!lldb->btree);
71 	lldb->btree = btree;
72 	BTR = btree;
73 }
74 /*========================================
75  * lldb_close -- Close any database contained.
76  *  Free LLDATABASE structure.
77  *  Safe to call even if not opened
78  *======================================*/
lldb_close(LLDATABASE * plldb)79 void lldb_close (LLDATABASE *plldb)
80 {
81 	LLDATABASE lldb=0;
82 	ASSERT(plldb);
83 	lldb = *plldb;
84 
85 	if (!lldb) return;
86 
87 	if (tagtable)
88 		destroy_table(tagtable);
89 	tagtable = 0;
90 	/* TODO: reverse the rest of init_lifelines_postdb -- Perry, 2002.06.05 */
91 	if (placabbvs) {
92 		destroy_table(placabbvs);
93 		placabbvs = NULL;
94 	}
95 	free_caches();
96 	check_node_leaks();
97 	check_record_leaks();
98 	closexref();
99 	ASSERT(BTR == lldb->btree);
100 	if (lldb->btree) {
101 		closebtree(lldb->btree);
102 		lldb->btree = 0;
103 		BTR = 0;
104 	}
105 	dbnotify_close();
106 	transl_free_predefined_xlats(); /* clear any active legacy translation tables */
107 	strfree(&readpath_file);
108 	strfree(&readpath);
109 	stdfree(lldb);
110 	*plldb = 0;
111 }
112