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