370c58fa732c17b26eda0b37a4b6c925e4cef65b
[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 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <ctype.h>
24 #include "lc_opts_t.h"
25
26 int lc_opt_type_scan(void *dest, lc_opt_type_t type, const char *str)
27 {
28         static const char *fmts[] = {
29                 "", "%s", "%i", "%f"
30         };
31
32         static const struct {
33                 const char *str;
34                 int val;
35         } bool_vals[] = {
36                 { "true", 1 },
37                 { "on", 1 },
38                 { "yes", 1 },
39                 { "false", 0 },
40                 { "no", 0 },
41                 { "off", 0 }
42         };
43
44         int res = 0;
45
46         switch(type) {
47                 case lc_opt_type_int:
48                 case lc_opt_type_double:
49                 case lc_opt_type_string:
50                         res = sscanf(str, fmts[type], dest);
51                         break;
52                 case lc_opt_type_boolean:
53                         {
54                                 size_t i, n;
55                                 int *data = dest;
56                                 char buf[10];
57
58                                 strncpy(buf, str, sizeof(buf));
59                                 for(i = 0, n = strlen(buf); i < n; ++i)
60                                         buf[i] = tolower(buf[i]);
61
62                                 for(i = 0; i < LC_ARRSIZE(bool_vals); ++i) {
63                                         if(strcmp(buf, bool_vals[i].str) == 0) {
64                                                 res = 1;
65                                                 *data = bool_vals[i].val;
66                                                 break;
67                                         }
68                                 }
69                         }
70                         break;
71                 default:
72                         break;
73         }
74
75         return res;
76 }
77
78 int lc_opt_type_print(char *buf, size_t n, lc_opt_type_t type, void *data)
79 {
80         int res = 0;
81
82         switch(type) {
83                 case lc_opt_type_int:
84                         {
85                                 int i = *((int *) data);
86                                 res = snprintf(buf, n, "%d", i);
87                         }
88                         break;
89                 case lc_opt_type_double:
90                         {
91                                 double d = *((double *) data);
92                                 res = snprintf(buf, n, "%f", d);
93                         }
94                         break;
95                 case lc_opt_type_string:
96                         res = snprintf(buf, n, "%s", (const char*) data);
97                         break;
98                 case lc_opt_type_boolean:
99                         res = snprintf(buf, n, "%s", *((int *) data) ? "yes" : "no");
100                         break;
101                 default:
102                         res = 0;
103         }
104
105         return res;
106 }