1 | /*
|
---|
2 | * Copyright (c) 2012, 2013 Dominik Taborsky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup hdisk
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <str.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <stdlib.h>
|
---|
38 |
|
---|
39 | #include "input.h"
|
---|
40 |
|
---|
41 | typedef int (*conv_f)(const char *, char **, unsigned int, bool, void *);
|
---|
42 | static int convert(tinput_t * in, conv_f str_f, void * val);
|
---|
43 |
|
---|
44 | int get_input_line(tinput_t * in, char ** str)
|
---|
45 | {
|
---|
46 | int rc;
|
---|
47 | rc = tinput_read(in, str);
|
---|
48 | if (rc == ENOENT) {
|
---|
49 | /* User requested exit */
|
---|
50 | return EINTR;
|
---|
51 | }
|
---|
52 | if (rc != EOK) {
|
---|
53 | /* Error in communication with console */
|
---|
54 | return rc;
|
---|
55 | }
|
---|
56 | /* Check for empty input. */
|
---|
57 | if (str_cmp(*str, "") == 0) {
|
---|
58 | free(*str);
|
---|
59 | *str = NULL;
|
---|
60 | return EINVAL;
|
---|
61 | }
|
---|
62 |
|
---|
63 | return EOK;
|
---|
64 | }
|
---|
65 |
|
---|
66 | uint8_t get_input_uint8(tinput_t * in)
|
---|
67 | {
|
---|
68 | uint32_t val;
|
---|
69 | /*char * str;
|
---|
70 |
|
---|
71 | rc = get_input_line(in, &str);
|
---|
72 | if (rc != EOK) {
|
---|
73 | printf("Error reading input.\n");
|
---|
74 | return 0;
|
---|
75 | }
|
---|
76 |
|
---|
77 | rc = str_uint8_t(str, NULL, 10, true, &val);
|
---|
78 | if( != EOK) {
|
---|
79 | printf("Invalid value.\n");
|
---|
80 | return 0;
|
---|
81 | }
|
---|
82 |
|
---|
83 | free(str);*/
|
---|
84 |
|
---|
85 | convert(in, (conv_f) str_uint8_t, &val);
|
---|
86 |
|
---|
87 | return val;
|
---|
88 | }
|
---|
89 |
|
---|
90 | uint32_t get_input_uint32(tinput_t * in)
|
---|
91 | {
|
---|
92 | uint32_t val;
|
---|
93 | /*char * str;
|
---|
94 |
|
---|
95 | rc = get_input_line(in, &str);
|
---|
96 | if (rc != EOK) {
|
---|
97 | printf("Error reading input.\n");
|
---|
98 | return 0;
|
---|
99 | }
|
---|
100 |
|
---|
101 | rc = str_uint32_t(str, NULL, 10, true, &val);
|
---|
102 | if( != EOK) {
|
---|
103 | printf("Invalid value.\n");
|
---|
104 | return 0;
|
---|
105 | }
|
---|
106 |
|
---|
107 | free(str);*/
|
---|
108 |
|
---|
109 | convert(in, (conv_f) str_uint32_t, &val);
|
---|
110 |
|
---|
111 | return val;
|
---|
112 | }
|
---|
113 |
|
---|
114 | uint64_t get_input_uint64(tinput_t * in)
|
---|
115 | {
|
---|
116 | uint64_t val;
|
---|
117 | /*char * str;
|
---|
118 |
|
---|
119 | rc = get_input_line(in, &str);
|
---|
120 | if (rc != EOK) {
|
---|
121 | printf("Error reading input.\n");
|
---|
122 | return 0;
|
---|
123 | }
|
---|
124 |
|
---|
125 | rc = str_uint32_t(str, NULL, 10, true, &val);
|
---|
126 | if( != EOK) {
|
---|
127 | printf("Invalid value.\n");
|
---|
128 | return 0;
|
---|
129 | }
|
---|
130 |
|
---|
131 | free(str);*/
|
---|
132 |
|
---|
133 | convert(in, (conv_f) str_uint64_t, &val);
|
---|
134 |
|
---|
135 | return val;
|
---|
136 | }
|
---|
137 |
|
---|
138 | size_t get_input_size_t(tinput_t * in)
|
---|
139 | {
|
---|
140 | int rc;
|
---|
141 | size_t val;
|
---|
142 |
|
---|
143 | char * str;
|
---|
144 | rc = get_input_line(in, &str);
|
---|
145 | if (rc != EOK) {
|
---|
146 | printf("Error reading input.\n");
|
---|
147 | return 0;
|
---|
148 | }
|
---|
149 |
|
---|
150 | rc = str_size_t(str, NULL, 10, true, &val);
|
---|
151 | if (rc != EOK) {
|
---|
152 | printf("Invalid value.\n");
|
---|
153 | return 0;
|
---|
154 | }
|
---|
155 | free(str);
|
---|
156 | /*
|
---|
157 | rc = convert(in, (conv_f) str_size_t, &val);
|
---|
158 | if (rc != EOK) {
|
---|
159 | return -1;
|
---|
160 | }
|
---|
161 | */
|
---|
162 | errno = EOK;
|
---|
163 | return val;
|
---|
164 | }
|
---|
165 |
|
---|
166 | static int convert(tinput_t * in, conv_f str_f, void * val)
|
---|
167 | {
|
---|
168 | int rc;
|
---|
169 | char * str;
|
---|
170 |
|
---|
171 | rc = get_input_line(in, &str);
|
---|
172 | if (rc != EOK) {
|
---|
173 | printf("Error reading input.\n");
|
---|
174 | return rc;
|
---|
175 | }
|
---|
176 |
|
---|
177 | rc = str_f(str, NULL, 10, true, val);
|
---|
178 | if(rc != EOK) {
|
---|
179 | printf("Invalid value.\n");
|
---|
180 | }
|
---|
181 |
|
---|
182 | free(str);
|
---|
183 | return rc;
|
---|
184 | }
|
---|
185 |
|
---|