a7fe96845137d6ac499a2e1413319de286a8cf15
[libfirm] / ir / libcore / lc_type.c
1 /*
2   libcore: library for basic data structures and algorithms.
3   Copyright (C) 2005  IPD Goos, Universit"at Karlsruhe, Germany
4
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19 #include "config.h"
20
21 #include <ctype.h>
22 #include "lc_opts_t.h"
23
24 int lc_opt_type_scan(void *dest, lc_opt_type_t type, const char *str)
25 {
26         static const char *fmts[] = {
27                 "", "%s", "%i", "%f"
28         };
29
30         static const struct {
31                 const char *str;
32                 int val;
33         } bool_vals[] = {
34                 { "true", 1 },
35                 { "on", 1 },
36                 { "yes", 1 },
37                 { "false", 0 },
38                 { "no", 0 },
39                 { "off", 0 }
40         };
41
42         int res = 0;
43
44         switch(type) {
45                 case lc_opt_type_int:
46                 case lc_opt_type_double:
47                 case lc_opt_type_string:
48                         res = sscanf(str, fmts[type], dest);
49                         break;
50                 case lc_opt_type_boolean:
51                         {
52                                 size_t i, n;
53                                 int *data = dest;
54                                 char buf[10];
55
56                                 strncpy(buf, str, sizeof(buf));
57                                 for(i = 0, n = strlen(buf); i < n; ++i)
58                                         buf[i] = tolower(buf[i]);
59
60                                 for(i = 0; i < LC_ARRSIZE(bool_vals); ++i) {
61                                         if(strcmp(buf, bool_vals[i].str) == 0) {
62                                                 res = 1;
63                                                 *data = bool_vals[i].val;
64                                                 break;
65                                         }
66                                 }
67                         }
68                         break;
69                 default:
70                         break;
71         }
72
73         return res;
74 }
75
76 int lc_opt_type_print(char *buf, size_t n, lc_opt_type_t type, void *data)
77 {
78         int res = 0;
79
80         switch(type) {
81                 case lc_opt_type_int:
82                         {
83                                 int i = *((int *) data);
84                                 res = snprintf(buf, n, "%d", i);
85                         }
86                         break;
87                 case lc_opt_type_double:
88                         {
89                                 double d = *((double *) data);
90                                 res = snprintf(buf, n, "%f", d);
91                         }
92                         break;
93                 case lc_opt_type_string:
94                         res = snprintf(buf, n, "%s", (const char*) data);
95                         break;
96                 case lc_opt_type_boolean:
97                         res = snprintf(buf, n, "%s", *((int *) data) ? "yes" : "no");
98                         break;
99                 default:
100                         res = 0;
101         }
102
103         return res;
104 }