Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2012 Frediano Ziglio
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Library General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2 of the License, or (at your option) any later version.
8 : *
9 : * This library 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 GNU
12 : * Library General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Library General Public
15 : * License along with this library; if not, write to the
16 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : * Boston, MA 02111-1307, USA.
18 : */
19 : #include "common.h"
20 :
21 : static FILE *f = NULL;
22 : static char *return_value = NULL;
23 :
24 : static bool
25 24 : conf_parse(const char *option, const char *value, void *param)
26 : {
27 24 : const char *entry = (const char *) param;
28 24 : if (strcmp(option, entry) == 0)
29 24 : return_value = strdup(value);
30 24 : return true;
31 : }
32 :
33 : static void
34 24 : test(const char *section, const char *entry, const char *expected)
35 : {
36 24 : int fail = 0;
37 :
38 24 : rewind(f);
39 :
40 24 : tds_read_conf_section(f, section, conf_parse, (void *) entry);
41 24 : if (!expected && return_value) {
42 0 : fprintf(stderr, "return value %s NOT expected\n", return_value);
43 0 : fail = 1;
44 24 : } else if (expected && !return_value) {
45 0 : fprintf(stderr, "expected value %s NOT found\n", expected);
46 0 : fail = 1;
47 24 : } else if (expected && return_value) {
48 24 : if (strcmp(expected, return_value) != 0) {
49 0 : fprintf(stderr, "expected value %s got %s\n", expected, return_value);
50 0 : fail = 1;
51 : }
52 : }
53 :
54 24 : free(return_value);
55 24 : return_value = NULL;
56 24 : if (fail)
57 0 : exit(1);
58 24 : }
59 :
60 : int
61 8 : main(void)
62 : {
63 8 : const char *in_file = FREETDS_SRCDIR "/readconf.in";
64 :
65 8 : f = fopen(in_file, "r");
66 8 : if (!f)
67 0 : f = fopen("readconf.in", "r");
68 8 : if (!f) {
69 0 : fprintf(stderr, "error opening test file\n");
70 0 : exit(1);
71 : }
72 :
73 : /* option with no spaces */
74 8 : test("section1", "opt1", "value1");
75 :
76 : /* option name with spaces, different case in section name */
77 8 : test("section2", "opt two", "value2");
78 :
79 8 : test("section 3", "opt three", "value three");
80 :
81 8 : fclose(f);
82 : return 0;
83 : }
84 :
|