2 * parser.c -- functions used to parse list or config file.
4 * Copyright (C) 2003 Maik Broemme <mbroemme@plusserver.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
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.
20 * $Id: parser.c,v 1.5 2004/02/12 00:47:53 mbroemme Exp $
22 #define _CRT_SECURE_NO_DEPRECATE
32 * This function deletes the specified characters, but leaves
33 * escape sequences unaffected. This means that " would be
34 * deleted but \" would not.
36 char *libmpq_conf_delete_char(char *buf
, char *chars
) {
42 /* strip out special chars like " */
43 while (temp
= strpbrk(temp
, chars
)) {
45 memmove(&temp
[0], &temp
[1], strlen(temp
));
55 * This function parses a line for the value to the given option. It
56 * return 1 on success and the byte array or 0 and null.
58 int libmpq_conf_parse_line(char *line
, char *search_value
, char *return_value
, int size
) {
67 /* check for spaces */
68 if (!isspace(*line
) && level
== 1) {
70 /* we found our value so break */
75 /* check for '=' so the value follows as next parameter */
76 if (*line
== '=' && level
== 0) {
81 /* now search for comment in this line */
82 for (i
= 0; i
< int(strlen(line
)); i
++) {
89 /* now set end of byte array behind value, but only if comment was found */
91 for (i
= pos
; i
>= 0; i
--) {
92 if (line
[i
] != ' ' && line
[i
] != '\t') {
99 /* now check if line has trailing spaces */
100 for (i
= strlen(line
); i
>= 0; i
--) {
101 if (line
[i
] != ' ' && line
[i
] != '\t') {
107 /* now check if value is quoted with "" and if there is a char behind. */
108 for (i
= strlen(line
); i
>= 0; i
--) {
109 if (line
[i
] == '"') {
115 /* return the values */
116 strncpy(return_value
, line
, size
);
121 * This function returns the value for a given option in the
122 * listdb or config file. On success it returns 1, otherwise 0.
124 int libmpq_conf_get_value(FILE *fp
, char *search_value
, void *return_value
, int type
, int size
) {
125 char buf
[LIBMPQ_CONF_BUFSIZE
];
127 int result
= LIBMPQ_TOOLS_SUCCESS
;
129 while (fgets(buf
, LIBMPQ_CONF_BUFSIZE
, fp
) != NULL
) {
132 buf
[strlen(buf
) - 1] = '\0';
134 /* skip whitespace */
135 for (line
= buf
; isspace(*line
); line
++) {
139 /* skip empty line */
140 if (line
[0] == '\0') {
145 if (line
[0] == '#') {
149 /* process the line */
150 //if (!strncasecmp(line, search_value, strlen(search_value))) {
151 if (!strcmp(line
, search_value
)) {
152 found
= libmpq_conf_parse_line(line
, search_value
, line
, LIBMPQ_CONF_BUFSIZE
);
154 libmpq_conf_delete_char(line
, "\"\\");
157 case LIBMPQ_CONF_TYPE_INT
:
159 /* if it is no valid number it is safe to return 0 */
160 *(int *)return_value
= atoi(line
);
163 strncpy((char *)return_value
, line
, size
);
167 /* value found, so rewind stream */
173 /* if value was not found */
176 case LIBMPQ_CONF_TYPE_INT
:
177 *(int *)return_value
= 0;
178 result
= LIBMPQ_CONF_EVALUE_NOT_FOUND
;
181 strncpy((char *)return_value
, "", size
);
182 result
= LIBMPQ_CONF_EVALUE_NOT_FOUND
;
186 fseek(fp
, 0L, SEEK_SET
);
192 * This function returns a pointer to a byte array, with all values
193 * found in the config file. As second value it returns th number of
194 * entries in the byte array. On success it returns 1, otherwise 0.
196 int libmpq_conf_get_array(FILE *fp
, char *search_value
, char ***filelist
, int *entries
) {
197 char buf
[LIBMPQ_CONF_BUFSIZE
];
198 char temp
[LIBMPQ_CONF_BUFSIZE
];
209 /* allocate memory for the file list */
210 (*filelist
) = (char **)malloc(LIBMPQ_CONF_FL_INCREMENT
* sizeof(char *));
212 fl_size
= LIBMPQ_CONF_FL_INCREMENT
;
214 while (fgets(buf
, LIBMPQ_CONF_BUFSIZE
, fp
) != NULL
) {
217 buf
[strlen(buf
) - 1] = '\0';
219 /* skip whitespace */
220 for (line
= buf
; isspace(*line
); line
++) {
224 /* skip empty line */
225 if (line
[0] == '\0') {
230 if (line
[0] == '#') {
234 /* check for array end ) */
240 /* process entries between () */
241 if (array_start
== 1 && array_end
== 0) {
243 /* add dummy option to use with libmpq_conf_parse_line() */
244 strncpy(temp
, "MPQ_BUFFER = ", LIBMPQ_CONF_BUFSIZE
);
245 strncat(temp
, line
, LIBMPQ_CONF_BUFSIZE
);
246 found
= libmpq_conf_parse_line(temp
, "MPQ_BUFFER", temp
, LIBMPQ_CONF_BUFSIZE
);
249 libmpq_conf_delete_char(temp
, "\"\\");
251 /* set the next filelist entry to a copy of the file */
252 (*filelist
)[fl_count
++] = _strdup(temp
);
254 /* increase the array size */
255 if (fl_count
== fl_size
) {
256 (*filelist
) = (char **)realloc((*filelist
), (fl_size
+ LIBMPQ_CONF_FL_INCREMENT
) * sizeof(char *));
257 fl_size
+= LIBMPQ_CONF_FL_INCREMENT
;
260 /* increase number of entries */
265 /* process the line and search array start */
266 //if (!strncasecmp(line, search_value, strlen(search_value))) {
267 if (!strcmp(line
, search_value
)) {
272 /* check for array start ( */
273 if (*line
== '(' && level
== 1) {
275 /* we found our value so break */
280 /* check for '=' so the value follows as next parameter */
281 if (*line
== '=' && level
== 0) {
288 /* we got all files, so rewind stream */
289 fseek(fp
, 0L, SEEK_SET
);
291 (*filelist
)[fl_count
] = NULL
;