Line data Source code
1 : /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 : /*
3 : * lt-database.c
4 : * Copyright (C) 2011-2012 Akira TAGOH
5 : *
6 : * Authors:
7 : * Akira TAGOH <akira@tagoh.org>
8 : *
9 : * You may distribute under the terms of either the GNU
10 : * Lesser General Public License or the Mozilla Public
11 : * License, as specified in the README file.
12 : */
13 : #ifdef HAVE_CONFIG_H
14 : #include "config.h"
15 : #endif
16 :
17 : #include <string.h>
18 : #include "lt-mem.h"
19 : #include "lt-ext-module.h"
20 : #include "lt-utils.h"
21 : #include "lt-database.h"
22 :
23 :
24 : /**
25 : * SECTION:lt-database
26 : * @Short_Description: convenient function sets to deal with the language tags database
27 : * @Title: Database
28 : *
29 : * This section describes convenient functions to obtain the database instance.
30 : */
31 :
32 : static lt_lang_db_t *__db_lang = NULL;
33 : static lt_extlang_db_t *__db_extlang = NULL;
34 : static lt_script_db_t *__db_script = NULL;
35 : static lt_region_db_t *__db_region = NULL;
36 : static lt_variant_db_t *__db_variant = NULL;
37 : static lt_grandfathered_db_t *__db_grandfathered = NULL;
38 : static lt_redundant_db_t *__db_redundant = NULL;
39 :
40 : static char __lt_db_datadir[LT_PATH_MAX] = { 0 };
41 :
42 :
43 : /*< private >*/
44 :
45 : /*< public >*/
46 : /**
47 : * lt_db_set_datadir:
48 : * @path: the directory where database files are installed.
49 : *
50 : * Set @path as the default location of the database files.
51 : * This has to be called before lt_db_initialize() or any
52 : * initialization for each databases.
53 : */
54 : void
55 4 : lt_db_set_datadir(const char *path)
56 : {
57 4 : if (path) {
58 4 : strncpy(__lt_db_datadir, path, LT_PATH_MAX - 1);
59 4 : __lt_db_datadir[LT_PATH_MAX - 1] = 0;
60 : } else {
61 0 : __lt_db_datadir[0] = 0;
62 : }
63 4 : }
64 :
65 : /**
66 : * lt_db_get_datadir:
67 : *
68 : * Obtain the directory where database files are installed.
69 : *
70 : * Returns: the directory name.
71 : */
72 : const char *
73 52 : lt_db_get_datadir(void)
74 : {
75 : static const char *__builtin_datadir = REGDATADIR;
76 :
77 52 : if (*__lt_db_datadir != 0)
78 52 : return __lt_db_datadir;
79 0 : return __builtin_datadir;
80 : }
81 :
82 : /**
83 : * lt_db_initialize:
84 : *
85 : * Initialize all of the language tags database instance.
86 : */
87 : void
88 4 : lt_db_initialize(void)
89 : {
90 4 : lt_db_get_lang();
91 4 : lt_db_get_extlang();
92 4 : lt_db_get_script();
93 4 : lt_db_get_region();
94 4 : lt_db_get_variant();
95 4 : lt_db_get_grandfathered();
96 4 : lt_db_get_redundant();
97 4 : lt_ext_modules_load();
98 4 : }
99 :
100 : /**
101 : * lt_db_finalize:
102 : *
103 : * Decreases the reference count of the language tags database, which was
104 : * increased with lt_db_initialize().
105 : */
106 : void
107 4 : lt_db_finalize(void)
108 : {
109 4 : lt_lang_db_unref(__db_lang);
110 4 : lt_extlang_db_unref(__db_extlang);
111 4 : lt_script_db_unref(__db_script);
112 4 : lt_region_db_unref(__db_region);
113 4 : lt_variant_db_unref(__db_variant);
114 4 : lt_grandfathered_db_unref(__db_grandfathered);
115 4 : lt_redundant_db_unref(__db_redundant);
116 4 : lt_ext_modules_unload();
117 4 : }
118 :
119 : #define DEFUNC_GET_INSTANCE(__type__) \
120 : lt_ ##__type__## _db_t * \
121 : lt_db_get_ ##__type__ (void) \
122 : { \
123 : if (!__db_ ##__type__) { \
124 : __db_ ##__type__ = lt_ ##__type__## _db_new(); \
125 : lt_mem_add_weak_pointer((lt_mem_t *)__db_ ##__type__, \
126 : (lt_pointer_t *)&__db_ ##__type__); \
127 : } else { \
128 : lt_ ##__type__## _db_ref(__db_ ##__type__); \
129 : } \
130 : \
131 : return __db_ ##__type__; \
132 : }
133 :
134 : /**
135 : * lt_db_get_lang:
136 : *
137 : * Obtains the instance of #lt_lang_db_t. This still allows to use without
138 : * lt_db_initialize(). but it will takes some time to load the database on
139 : * the memory every time.
140 : *
141 : * Returns: The instance of #lt_lang_db_t.
142 : */
143 24 : DEFUNC_GET_INSTANCE(lang)
144 : /**
145 : * lt_db_get_extlang:
146 : *
147 : * Obtains the instance of #lt_extlang_db_t. This still allows to use without
148 : * lt_db_initialize(). but it will takes some time to load the database on
149 : * the memory every time.
150 : *
151 : * Returns: The instance of #lt_extlang_db_t.
152 : */
153 8 : DEFUNC_GET_INSTANCE(extlang)
154 : /**
155 : * lt_db_get_grandfathered:
156 : *
157 : * Obtains the instance of #lt_grandfathered_db_t. This still allows to use
158 : * without lt_db_initialize(). but it will takes some time to load the database
159 : * on the memory every time.
160 : *
161 : * Returns: The instance of #lt_grandfathered_db_t.
162 : */
163 28 : DEFUNC_GET_INSTANCE(grandfathered)
164 : /**
165 : * lt_db_get_redundant:
166 : *
167 : * Obtains the instance of #lt_redundant_db_t. This still allows to use
168 : * without lt_db_initialize(). but it will takes some time to load the database
169 : * on the memory every time.
170 : *
171 : * Returns: The instance of #lt_redundant_db_t.
172 : */
173 8 : DEFUNC_GET_INSTANCE(redundant)
174 : /**
175 : * lt_db_get_region:
176 : *
177 : * Obtains the instance of #lt_region_db_t. This still allows to use without
178 : * lt_db_initialize(). but it will takes some time to load the database on
179 : * the memory every time.
180 : *
181 : * Returns: The instance of #lt_region_db_t.
182 : */
183 10 : DEFUNC_GET_INSTANCE(region)
184 : /**
185 : * lt_db_get_script:
186 : *
187 : * Obtains the instance of #lt_script_db_t. This still allows to use without
188 : * lt_db_initialize(). but it will takes some time to load the database on
189 : * the memory every time.
190 : *
191 : * Returns: The instance of #lt_script_db_t.
192 : */
193 8 : DEFUNC_GET_INSTANCE(script)
194 : /**
195 : * lt_db_get_variant:
196 : *
197 : * Obtains the instance of #lt_variant_db_t. This still allows to use without
198 : * lt_db_initialize(). but it will takes some time to load the database on
199 : * the memory every time.
200 : *
201 : * Returns: The instance of #lt_variant_db_t.
202 : */
203 4 : DEFUNC_GET_INSTANCE(variant)
|