1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * $Id: conf.c,v 1.3 2005/02/19 20:58:32 nedko Exp $
7 * Mediadatabase configuration file handling.
10 * Nedko Arnaudov <nedko@users.sourceforge.net>
13 * GNU GENERAL PUBLIC LICENSE version 2
15 *****************************************************************************/
21 #include "../result.h"
27 #define CONF_FILE ".mediadatabase"
28 #define CFL_SECTION_GLOBAL NULL
29 #define CFL_SECTION_CUI "cui"
30 #define CFL_SECTION_MYSQL "MySQL"
31 #define CFL_SECTION_SQLITE "SQLite"
32 #define CFL_VALUE_MYSQL_HOST "host"
33 #define CFL_VALUE_MYSQL_USER "user"
34 #define CFL_VALUE_MYSQL_PASS "pass"
35 #define CFL_VALUE_MYSQL_DB "db"
36 #define CFL_VALUE_MOUNT "mount"
37 #define CFL_VALUE_UNMOUNT "unmount"
38 #define CFL_VALUE_MOUNTDIR "mountdir"
39 #define CFL_VALUE_SQLITE_DB "dbfile"
40 #define CFL_VALUE_BACKEND "backend"
42 static cflh_t g_hConf
= NULL
;
45 conf_get_full_name(char ** ppszConf
)
51 pszHOME
= getenv("HOME");
55 mediadb_error_callback(
56 MEDIADB_ERROR_CRITICAL
,
57 "HOME environment variable not set.");
61 s
= strlen(pszHOME
) + 1 + strlen(CONF_FILE
) + 1;
63 pszConf
= (char *)malloc(s
);
66 mediadb_error_callback(
67 MEDIADB_ERROR_CRITICAL
,
72 sprintf(pszConf
, "%s/%s", pszHOME
, CONF_FILE
);
87 cfl_value_type_t CFLValueType
;
94 r
= conf_get_full_name(&pszConf
);
95 if (MEDIADB_IS_ERROR(r
))
100 hFile
= fopen(pszConf
, "r");
103 mediadb_error_printf(
104 MEDIADB_ERROR_CRITICAL
,
105 "Cannot open configuration file \"%s\".",
111 g_hConf
= cfl_alloc();
114 mediadb_error_callback(
115 MEDIADB_ERROR_CRITICAL
,
121 nRet
= cfl_init_from_file(
124 CFL_FILE_TYPE_DEFAULT
);
127 mediadb_error_printf(
128 MEDIADB_ERROR_CRITICAL
,
129 "cfl_init_from_file() failed. (%s)",
130 cfl_errcode_to_message(nRet
));
134 pszCFLValue
= cfl_value_get_by_name(
140 if (pszCFLValue
!= NULL
&&
141 CFLValueType
== CFL_TYPE_STRING
)
143 r
= disk_set_mount_command(pszCFLValue
);
144 if (MEDIADB_IS_ERROR(r
))
150 pszCFLValue
= cfl_value_get_by_name(
156 if (pszCFLValue
!= NULL
&&
157 CFLValueType
== CFL_TYPE_STRING
)
159 r
= disk_set_unmount_command(pszCFLValue
);
160 if (MEDIADB_IS_ERROR(r
))
166 pszCFLValue
= cfl_value_get_by_name(
172 if (pszCFLValue
!= NULL
&&
173 CFLValueType
== CFL_TYPE_STRING
)
175 r
= disk_set_path(pszCFLValue
);
176 if (MEDIADB_IS_ERROR(r
))
182 pszCFLValue
= cfl_value_get_by_name(
185 CFL_VALUE_MYSQL_HOST
,
188 if (pszCFLValue
!= NULL
&&
189 CFLValueType
== CFL_TYPE_STRING
)
191 r
= db_set_mysql_host(pszCFLValue
);
192 if (MEDIADB_IS_ERROR(r
))
198 pszCFLValue
= cfl_value_get_by_name(
204 if (pszCFLValue
!= NULL
&&
205 CFLValueType
== CFL_TYPE_STRING
)
207 r
= db_set_mysql_database(pszCFLValue
);
208 if (MEDIADB_IS_ERROR(r
))
214 pszCFLValue
= cfl_value_get_by_name(
217 CFL_VALUE_MYSQL_USER
,
220 if (pszCFLValue
!= NULL
&&
221 CFLValueType
== CFL_TYPE_STRING
)
223 r
= db_set_mysql_user(pszCFLValue
);
224 if (MEDIADB_IS_ERROR(r
))
230 pszCFLValue
= cfl_value_get_by_name(
233 CFL_VALUE_MYSQL_PASS
,
236 if (pszCFLValue
!= NULL
&&
237 CFLValueType
== CFL_TYPE_STRING
)
239 r
= db_set_mysql_pass(pszCFLValue
);
240 if (MEDIADB_IS_ERROR(r
))
246 pszCFLValue
= cfl_value_get_by_name(
252 if (pszCFLValue
!= NULL
&&
253 CFLValueType
== CFL_TYPE_STRING
)
255 r
= db_set_sqlite_database(pszCFLValue
);
256 if (MEDIADB_IS_ERROR(r
))
262 pszCFLValue
= cfl_value_get_by_name(
268 if (pszCFLValue
!= NULL
&&
269 CFLValueType
== CFL_TYPE_STRING
)
271 if (strcmp(pszCFLValue
, CFL_SECTION_SQLITE
) == 0)
275 else if (strcmp(pszCFLValue
, CFL_SECTION_MYSQL
) == 0)
304 unsigned int nDBType
;
311 mediadb_error_printf(
312 MEDIADB_ERROR_CRITICAL
,
313 "Cannot write configuration file without first parsing it.");
314 return MEDIADB_UNEXP
;
317 r
= conf_get_full_name(&pszConf
);
318 if (MEDIADB_IS_ERROR(r
))
323 hFile
= fopen(pszConf
, "w");
326 mediadb_error_printf(
327 MEDIADB_ERROR_CRITICAL
,
328 "Cannot open configuration file \"%s\".",
334 nRet
= cfl_value_set_by_name(
339 (void *)disk_get_mount_command());
342 mediadb_error_printf(
343 MEDIADB_ERROR_CRITICAL
,
344 "cfl_value_set_by_name() failed with %d",
350 nRet
= cfl_value_set_by_name(
355 (void *)disk_get_unmount_command());
358 mediadb_error_printf(
359 MEDIADB_ERROR_CRITICAL
,
360 "cfl_value_set_by_name() failed with %d",
366 nRet
= cfl_value_set_by_name(
371 (void *)disk_get_path());
374 mediadb_error_printf(
375 MEDIADB_ERROR_CRITICAL
,
376 "cfl_value_set_by_name() failed with %d",
382 nRet
= cfl_value_set_by_name(
385 CFL_VALUE_MYSQL_HOST
,
387 (void *)db_get_mysql_host());
390 mediadb_error_printf(
391 MEDIADB_ERROR_CRITICAL
,
392 "cfl_value_set_by_name() failed with %d",
398 nRet
= cfl_value_set_by_name(
401 CFL_VALUE_MYSQL_USER
,
403 (void *)db_get_mysql_user());
406 mediadb_error_printf(
407 MEDIADB_ERROR_CRITICAL
,
408 "cfl_value_set_by_name() failed with %d",
414 nRet
= cfl_value_set_by_name(
417 CFL_VALUE_MYSQL_PASS
,
419 (void *)db_get_mysql_pass());
422 mediadb_error_printf(
423 MEDIADB_ERROR_CRITICAL
,
424 "cfl_value_set_by_name() failed with %d",
430 nRet
= cfl_value_set_by_name(
435 (void *)db_get_mysql_database());
438 mediadb_error_printf(
439 MEDIADB_ERROR_CRITICAL
,
440 "cfl_value_set_by_name() failed with %d",
446 nRet
= cfl_value_set_by_name(
451 (void *)db_get_sqlite_database());
454 mediadb_error_printf(
455 MEDIADB_ERROR_CRITICAL
,
456 "cfl_value_set_by_name() failed with %d",
462 nDBType
= db_get_type();
463 if (nDBType
== MEDIADB_DBTYPE_MYSQL
)
465 nRet
= cfl_value_set_by_name(
470 (void *)CFL_SECTION_MYSQL
);
473 mediadb_error_printf(
474 MEDIADB_ERROR_CRITICAL
,
475 "cfl_value_set_by_name() failed with %d",
481 else if (nDBType
== MEDIADB_DBTYPE_SQLITE
)
483 nRet
= cfl_value_set_by_name(
488 (void *)CFL_SECTION_SQLITE
);
491 mediadb_error_printf(
492 MEDIADB_ERROR_CRITICAL
,
493 "cfl_value_set_by_name() failed with %d",
501 mediadb_error_printf(
502 MEDIADB_ERROR_CRITICAL
,
503 "unknown backend type %u",
509 cfl_write(g_hConf
, hFile
);
537 /*****************************************************************************
541 * !!! WARNING !!! Following lines are automatically updated by the CVS system.
544 * Revision 1.3 2005/02/19 20:58:32 nedko
545 * Implement conf file writing.
547 * Revision 1.2 2004/05/21 23:42:49 nedko
548 * mediadb_error_callback() now tells if error is critical.
550 * Revision 1.1 2004/05/16 19:01:17 nedko
551 * libfrontend holds code common to frontends but not in libdb.
553 *****************************************************************************/