source: mainline/uspace/app/hdisk/input.c@ 6afc9d7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6afc9d7 was 6453e306, checked in by Martin Decky <martin@…>, 12 years ago

basic code review and coding style cleanup

  • Property mode set to 100644
File size: 3.0 KB
Line 
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#include "input.h"
39
40typedef int (*conv_f)(const char *, char **, unsigned int, bool, void *);
41
42static int convert(tinput_t *, conv_f, void *);
43
44int get_input_line(tinput_t *in, char **str)
45{
46 int rc = tinput_read(in, str);
47 if (rc == ENOENT)
48 return EINTR;
49
50 if (rc != EOK)
51 return rc;
52
53 /* Check for empty input. */
54 if (str_cmp(*str, "") == 0) {
55 free(*str);
56 *str = NULL;
57 return EINVAL;
58 }
59
60 return EOK;
61}
62
63uint8_t get_input_uint8(tinput_t *in)
64{
65 uint32_t val;
66 int rc = convert(in, (conv_f) str_uint8_t, &val);
67 if (rc != EOK) {
68 errno = rc;
69 return 0;
70 }
71
72 errno = EOK;
73 return val;
74}
75
76uint32_t get_input_uint32(tinput_t *in)
77{
78 uint32_t val;
79 int rc = convert(in, (conv_f) str_uint32_t, &val);
80 if (rc != EOK) {
81 errno = rc;
82 return 0;
83 }
84
85 errno = EOK;
86 return val;
87}
88
89uint64_t get_input_uint64(tinput_t *in)
90{
91 uint64_t val;
92 int rc = convert(in, (conv_f) str_uint64_t, &val);
93 if (rc != EOK) {
94 errno = rc;
95 return 0;
96 }
97
98 errno = EOK;
99 return val;
100}
101
102size_t get_input_size_t(tinput_t *in)
103{
104 size_t val;
105 int rc = convert(in, (conv_f) str_size_t, &val);
106 if (rc != EOK) {
107 errno = rc;
108 return 0;
109 }
110
111 errno = EOK;
112 return val;
113}
114
115static int convert(tinput_t *in, conv_f str_f, void *val)
116{
117 char *str;
118 int rc = get_input_line(in, &str);
119 if (rc != EOK) {
120 printf("Error reading input.\n");
121 return rc;
122 }
123
124 rc = str_f(str, NULL, 10, true, val);
125 if (rc != EOK)
126 printf("Invalid value.\n");
127
128 free(str);
129 return rc;
130}
Note: See TracBrowser for help on using the repository browser.