| 1 | /* |
| 2 | * Copyright (C) 2002,2003 Dizzy |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include "common/setup_before.h" |
| 20 | #include "compat/strcasecmp.h" |
| 21 | #include <cstring> |
| 22 | #include <cstdlib> |
| 23 | |
| 24 | #include "storage.h" |
| 25 | #include "storage_file.h" |
| 26 | #ifdef WITH_SQL |
| 27 | #include "storage_sql.h" |
| 28 | #endif |
| 29 | |
| 30 | #include "compat/strdup.h" |
| 31 | #include "common/eventlog.h" |
| 32 | #include "common/xalloc.h" |
| 33 | #include "common/setup_after.h" |
| 34 | |
| 35 | namespace pvpgn |
| 36 | { |
| 37 | |
| 38 | namespace bnetd |
| 39 | { |
| 40 | |
| 41 | t_storage *storage = NULL; |
| 42 | |
| 43 | extern int storage_init(const char *spath) |
| 44 | { |
| 45 | char *p; |
| 46 | int res; |
| 47 | char dstr[256]; |
| 48 | |
| 49 | if (spath == NULL) { |
| 50 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL spath"); |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | if ((p = std::strchr((char *)spath, ':')) == NULL) { |
| 55 | eventlog(eventlog_level_error, __FUNCTION__, "malformed storage_path , driver not found"); |
| 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | std::strcpy(dstr, "file"); |
| 60 | #ifdef WITH_SQL |
| 61 | std::strcat(dstr, ", sql"); |
| 62 | #endif |
| 63 | eventlog(eventlog_level_info, __FUNCTION__, "initializing storage layer (available drivers: {})", dstr); |
| 64 | |
| 65 | *p = '\0'; |
| 66 | if (strcasecmp(spath, "file") == 0) { |
| 67 | storage = &storage_file; |
| 68 | res = storage->init(p + 1); |
| 69 | if (!res) |
| 70 | eventlog(eventlog_level_info, __FUNCTION__, "using file storage driver"); |
| 71 | } |
| 72 | #ifdef WITH_SQL |
| 73 | else if (strcasecmp(spath, "sql") == 0) { |
| 74 | storage = &storage_sql; |
| 75 | res = storage->init(p + 1); |
| 76 | if (!res) |
| 77 | eventlog(eventlog_level_info, __FUNCTION__, "using sql storage driver"); |
| 78 | } |
| 79 | #endif |
| 80 | else { |
| 81 | eventlog(eventlog_level_fatal, __FUNCTION__, "no known driver specified ({})", spath); |
| 82 | res = -1; |
| 83 | } |
| 84 | |
| 85 | return res; |
| 86 | } |
| 87 | |
| 88 | extern void storage_close(void) |
| 89 | { |
| 90 | storage->close(); |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | } |