Add.
[shishi.git] / db / setup.c
blobacda9328513217394a2a3b207f79d1cf41e4e658
1 /* init.c --- Initialization functions for the Shisa library.
2 * Copyright (C) 2002, 2003, 2006 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "info.h"
24 /**
25 * shisa:
27 * Initializes the Shisa library. If this function fails, it may
28 * print diagnostic errors to stderr.
30 * Return value: Returns Shisa library handle, or %NULL on error.
31 **/
32 Shisa *
33 shisa (void)
35 Shisa *dbh;
37 dbh = xcalloc (1, sizeof (*dbh));
39 bindtextdomain (PACKAGE, LOCALEDIR);
40 textdomain (PACKAGE);
42 return dbh;
45 /**
46 * shisa_done:
47 * @handle: shisa handle as allocated by shisa().
49 * Deallocates the shisa library handle. The handle must not be used
50 * in any calls to shisa functions after this.
51 **/
52 void
53 shisa_done (Shisa * dbh)
55 _Shisa_db *db;
56 size_t i;
58 for (i = 0, db = dbh->dbs; i < dbh->ndbs; i++, db++)
59 db->backend->done (dbh, db->state);
61 if (dbh->dbs)
62 free (dbh->dbs);
64 free (dbh);
67 /**
68 * shisa_init:
69 * @dbh: pointer to library handle to be created.
71 * Create a Shisa library handle, using shisa(), and read the system
72 * configuration file from their default locations. The paths to the
73 * default system configuration file is decided at compile time
74 * ($sysconfdir/shisa.conf).
76 * The handle is allocated regardless of return values, except for
77 * SHISA_INIT_ERROR which indicates a problem allocating the handle.
78 * (The other error conditions comes from reading the files.)
80 * Return value: Returns %SHISA_OK iff successful.
81 **/
82 int
83 shisa_init (Shisa ** dbh)
85 return shisa_init_with_paths (dbh, NULL);
88 /**
89 * shisa_init_with_paths:
90 * @dbh: pointer to library handle to be created.
91 * @file: Filename of system configuration, or NULL.
93 * Create a Shisa library handle, using shisa(), and read the system
94 * configuration file indicated location (or the default location, if
95 * %NULL). The paths to the default system configuration file is
96 * decided at compile time ($sysconfdir/shisa.conf).
98 * The handle is allocated regardless of return values, except for
99 * SHISA_INIT_ERROR which indicates a problem allocating the handle.
100 * (The other error conditions comes from reading the files.)
102 * Return value: Returns %SHISA_OK iff successful.
105 shisa_init_with_paths (Shisa ** dbh, const char *file)
107 int rc;
109 if (!dbh || !(*dbh = shisa ()))
110 return SHISA_INIT_ERROR;
112 if (!file)
113 file = shisa_cfg_default_systemfile (*dbh);
115 rc = shisa_cfg_from_file (*dbh, file);
116 if (rc != SHISA_OK && rc != SHISA_CFG_NO_FILE)
117 return rc;
119 if ((*dbh)->ndbs == 0)
121 rc = shisa_cfg (*dbh, "db file " DEFAULTDBPATH);
122 if (rc != SHISA_OK)
123 return rc;
126 return SHISA_OK;