-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.c
104 lines (88 loc) · 2.25 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* app_my: damn simple MySQL application for asterisk
* Copyright (C) 2010 - Steve Frécinaux
* Licensed under the GPL2+
*/
#include <asterisk.h>
#include <asterisk/config.h>
#include <asterisk/logger.h>
#include <asterisk/utils.h>
#include <stdio.h>
#include <string.h>
#define IN_CONFIG_H
#include "config.h"
struct my_config config = { 0 };
#define CONFIG_FILE "my.conf"
void
config_init (void)
{
struct ast_config *cfg = NULL;
struct ast_variable *var = NULL;
const char *value = NULL;
memset (&config, 0, sizeof (struct my_config));
cfg = ast_config_load (CONFIG_FILE);
if (!cfg)
ast_log (LOG_WARNING, AST_MODULE ": Unable to load " CONFIG_FILE "\n");
else
var = ast_variable_browse (cfg, "global");
if (var)
value = ast_variable_retrieve (cfg, "global", "hostname");
if (!value)
{
ast_log (LOG_WARNING, "MySQL server hostname not specified. Assuming localhost\n");
value = "localhost";
}
config.hostname = ast_strdup (value);
if (var)
value = ast_variable_retrieve (cfg, "global", "port");
if (!value)
{
ast_log (LOG_WARNING, "MySQL server port not specified. Assuming 3306\n");
config.port = 3306;
}
else
{
config.port = atoi (value);
}
if (var)
value = ast_variable_retrieve (cfg, "global", "user");
if (!value)
{
ast_log (LOG_WARNING, "MySQL database user not specified. Assuming root\n");
value = "root";
}
config.username = ast_strdup (value);
if (var)
value = ast_variable_retrieve (cfg, "global", "password");
if (!value)
{
ast_log (LOG_WARNING, "MySQL database password not specified. Assuming blank\n");
value = "";
}
config.password = ast_strdup (value);
if (var)
value = ast_variable_retrieve (cfg, "global", "dbname");
if (!value)
{
ast_log(LOG_WARNING, "MySQL database not specified. Assuming beip\n");
value = "beip";
}
config.database = ast_strdup (value);
config.timeout = 10;
if (cfg)
ast_config_destroy (cfg);
}
void
config_reset (void)
{
config_clean ();
config_init ();
}
void
config_clean (void)
{
ast_free (config.hostname);
ast_free (config.username);
ast_free (config.password);
ast_free (config.database);
}
/* ex:set ts=2 et sw=2 ai: */