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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/* eurephiadm.c -- CLI based admin program for eurephia
*
* GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <eurephia_context.h>
#include <eurephia_nullsafe.h>
#include <eurephia_log.h>
#include <eurephiadb_driver.h>
#include <eurephiadb.h>
#define MAX_ARGUMENTS 64
eurephiaCTX *eurephiaCTX_init(FILE *log, const int loglevel, const char *dbi) {
eurephiaCTX *ctx = NULL;
ctx = (eurephiaCTX *) malloc(sizeof(eurephiaCTX)+2);
assert(ctx != NULL);
memset(ctx, 0, sizeof(eurephiaCTX)+2);
ctx->log = log;
ctx->loglevel = loglevel;
if( !eDBlink_init(ctx, dbi, 2) ){
eurephia_log(ctx, LOG_PANIC, 0, "Could not load the database driver");
free_nullsafe(ctx);
return NULL;
}
return ctx;
}
void eurephiaCTX_destroy(eurephiaCTX *ctx) {
if( ctx == NULL ) {
return;
}
if( (ctx->dbc != NULL) && (ctx->dbc->dbhandle != NULL) ) {
eDBdisconnect(ctx);
}
if( ctx->eurephia_driver != NULL ) {
eDBlink_close(ctx);
}
if( ctx->log != NULL ) {
fflush(ctx->log);
// Do not close log file if we're on stdout or stderr
if( (ctx->log != stderr) && (ctx->log != stdout) ) {
eurephia_log(ctx, LOG_INFO, 2, "Closing log file");
fclose(ctx->log);
}
ctx->log = NULL;
ctx->loglevel = 0;
}
free_nullsafe(ctx);
}
int eurephia_ConnectDB(eurephiaCTX *ctx, const char *argstr) {
char *delims = " ";
char *cp = NULL;
const char *dbargv[MAX_ARGUMENTS];
int dbargc = 0;
if( (argstr == NULL) || (strlen(argstr) < 1) ) {
eurephia_log(ctx, LOG_FATAL, 0, "No database connection string given");
return 0;
}
// Split up argstr into separate arguments
cp = strdup(argstr);
assert(cp != NULL);
dbargc = 0;
dbargv[dbargc] = strtok(cp, delims);
while( (dbargv[dbargc] != NULL) && (dbargc <= MAX_ARGUMENTS) ) {
fprintf(stderr, "[%i] |%s|\n", dbargc, dbargv[dbargc]);
dbargv[++dbargc] = strtok(NULL, delims);
}
if( !eDBconnect(ctx, dbargc, dbargv) ) {
eurephia_log(ctx, LOG_PANIC, 0, "Could not connect to the database");
eDBlink_close(ctx);
return 0;
}
return 1;
}
int main(int argc, char **argv) {
eurephiaCTX *ctx = NULL;
ctx = eurephiaCTX_init(stderr, 10, "../database/sqlite/edb-sqlite.so");
if( ctx == NULL ) {
fprintf(stderr, "Could not initialise a eurephia context.\n");
return 1;
}
if( !eurephia_ConnectDB(ctx, argv[1]) ) {
return 2;
}
eurephiaCTX_destroy(ctx);
return 0;
}
|