| 1 | /* |
| 2 | * Copyright (C) 1998,1999,2000,2001 Ross Combs ([email protected]) |
| 3 | * Copyright (C) 2000,2001 Marco Ziech ([email protected]) |
| 4 | * Copyright (C) 2002,2003,2004 Dizzy |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | #include "common/setup_before.h" |
| 21 | #include "file_plain.h" |
| 22 | |
| 23 | #include <cstring> |
| 24 | #include <cerrno> |
| 25 | |
| 26 | #include "common/eventlog.h" |
| 27 | #include "common/util.h" |
| 28 | |
| 29 | #include "common/setup_after.h" |
| 30 | |
| 31 | namespace pvpgn |
| 32 | { |
| 33 | |
| 34 | namespace bnetd |
| 35 | { |
| 36 | |
| 37 | /* plain file storage API functions */ |
| 38 | |
| 39 | static t_attr * plain_read_attr(const char *filename, const char *key); |
| 40 | static int plain_read_attrs(const char *filename, t_read_attr_func cb, void *data); |
| 41 | static int plain_write_attrs(const char *filename, const t_hlist *attributes); |
| 42 | |
| 43 | /* file_engine struct populated with the functions above */ |
| 44 | |
| 45 | t_file_engine file_plain = { |
| 46 | plain_read_attr, |
| 47 | plain_read_attrs, |
| 48 | plain_write_attrs |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | static int plain_write_attrs(const char *filename, const t_hlist *attributes) |
| 53 | { |
| 54 | std::FILE * accountfile; |
| 55 | t_hlist * curr; |
| 56 | t_attr * attr; |
| 57 | char const * key; |
| 58 | char const * val; |
| 59 | |
| 60 | if (!(accountfile = std::fopen(filename, "w"))) { |
| 61 | eventlog(eventlog_level_error, __FUNCTION__, "unable to open file \"{}\" for writing (std::fopen: {})", filename, std::strerror(errno)); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | hlist_for_each(curr, attributes) { |
| 66 | attr = hlist_entry(curr, t_attr, link); |
| 67 | |
| 68 | if (attr_get_key(attr)) |
| 69 | key = escape_chars(attr_get_key(attr), std::strlen(attr_get_key(attr))); |
| 70 | else { |
| 71 | eventlog(eventlog_level_error, __FUNCTION__, "attribute with NULL key in list"); |
| 72 | key = NULL; |
| 73 | } |
| 74 | |
| 75 | if (attr_get_val(attr)) |
| 76 | val = escape_chars(attr_get_val(attr), std::strlen(attr_get_val(attr))); |
| 77 | else { |
| 78 | eventlog(eventlog_level_debug, __FUNCTION__, "attribute with NULL val in list key=\"{}\"", key ? key : "NULL"); |
| 79 | val = NULL; |
| 80 | } |
| 81 | |
| 82 | if (key && val) { |
| 83 | if (std::strncmp("BNET\\CharacterDefault\\", key, 20) == 0) { |
| 84 | eventlog(eventlog_level_debug, __FUNCTION__, "skipping attribute key=\"{}\"", key); |
| 85 | } |
| 86 | else { |
| 87 | eventlog(eventlog_level_debug, __FUNCTION__, "saving attribute key=\"{}\" val=\"{}\"", key, val); |
| 88 | std::fprintf(accountfile, "\"%s\"=\"%s\"\n", key, val); |
| 89 | } |
| 90 | } |
| 91 | else eventlog(eventlog_level_debug, __FUNCTION__, "could not save attribute key=\"{}\"", key ? key : "NULL"); |
| 92 | |
| 93 | if (key) xfree((void *)key); /* avoid warning */ |
| 94 | if (val) xfree((void *)val); /* avoid warning */ |
| 95 | |
| 96 | attr_clear_dirty(attr); |
| 97 | } |
| 98 | |
| 99 | if (std::fclose(accountfile) < 0) { |
| 100 | eventlog(eventlog_level_error, __FUNCTION__, "could not close account file \"{}\" after writing (std::fclose: {})", filename, std::strerror(errno)); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int plain_read_attrs(const char *filename, t_read_attr_func cb, void *data) |
| 108 | { |
| 109 | std::FILE * accountfile; |
| 110 | unsigned int line; |
| 111 | char const * buff; |
| 112 | unsigned int len; |
| 113 | char * esckey; |
| 114 | char * escval; |
| 115 | char * key; |
| 116 | char * val; |
| 117 | |
| 118 | if (!(accountfile = std::fopen(filename, "r"))) { |
| 119 | eventlog(eventlog_level_error, __FUNCTION__, "could not open account file \"{}\" for reading (std::fopen: {})", filename, std::strerror(errno)); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | for (line = 1; (buff = file_get_line(accountfile)); line++) { |
| 124 | if (buff[0] == '#' || buff[0] == '\0') { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | if (std::strlen(buff) < 6) /* "?"="" */ { |
| 129 | eventlog(eventlog_level_error, __FUNCTION__, "malformed line {} of account file \"{}\"", line, filename); |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | len = std::strlen(buff) - 5 + 1; /* - ""="" + NUL */ |
| 134 | esckey = (char*)xmalloc(len); |
| 135 | escval = (char*)xmalloc(len); |
| 136 | |
| 137 | if (std::sscanf(buff, "\"%[^\"]\" = \"%[^\"]\"", esckey, escval) != 2) { |
| 138 | if (std::sscanf(buff, "\"%[^\"]\" = \"\"", esckey) != 1) /* hack for an empty value field */ { |
| 139 | eventlog(eventlog_level_error, __FUNCTION__, "malformed entry on line {} of account file \"{}\"", line, filename); |
| 140 | xfree(escval); |
| 141 | xfree(esckey); |
| 142 | continue; |
| 143 | } |
| 144 | escval[0] = '\0'; |
| 145 | } |
| 146 | |
| 147 | key = unescape_chars(esckey); |
| 148 | val = unescape_chars(escval); |
| 149 | |
| 150 | /* eventlog(eventlog_level_debug,__FUNCTION__,"std::strlen(esckey)=%u (%c), len=%u",std::strlen(esckey),esckey[0],len);*/ |
| 151 | xfree(esckey); |
| 152 | xfree(escval); |
| 153 | |
| 154 | if (cb(key, val, data)) |
| 155 | eventlog(eventlog_level_error, __FUNCTION__, "got error from callback (key: '{}' val:'{}')", key, val); |
| 156 | |
| 157 | if (key) xfree((void *)key); /* avoid warning */ |
| 158 | if (val) xfree((void *)val); /* avoid warning */ |
| 159 | } |
| 160 | |
| 161 | file_get_line(NULL); // clear file_get_line buffer |
| 162 | |
| 163 | if (std::fclose(accountfile) < 0) |
| 164 | eventlog(eventlog_level_error, __FUNCTION__, "could not close account file \"{}\" after reading (std::fclose: {})", filename, std::strerror(errno)); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static t_attr * plain_read_attr(const char *filename, const char *key) |
| 170 | { |
| 171 | /* flat file storage doesnt know to read selective attributes */ |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | |
| 177 | } |