source: mainline/uspace/app/hdisk/input.c@ 996df189

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 996df189 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
RevLine 
[256cbfe]1/*
[6453e306]2 * Copyright (c) 2012-2013 Dominik Taborsky
[256cbfe]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
[6453e306]29/** @addtogroup hdisk
[256cbfe]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
[6453e306]42static int convert(tinput_t *, conv_f, void *);
43
44int get_input_line(tinput_t *in, char **str)
[256cbfe]45{
[6453e306]46 int rc = tinput_read(in, str);
47 if (rc == ENOENT)
[256cbfe]48 return EINTR;
[6453e306]49
50 if (rc != EOK)
[256cbfe]51 return rc;
[6453e306]52
[256cbfe]53 /* Check for empty input. */
54 if (str_cmp(*str, "") == 0) {
55 free(*str);
56 *str = NULL;
57 return EINVAL;
58 }
[6453e306]59
[256cbfe]60 return EOK;
61}
62
[6453e306]63uint8_t get_input_uint8(tinput_t *in)
[256cbfe]64{
65 uint32_t val;
[6453e306]66 int rc = convert(in, (conv_f) str_uint8_t, &val);
[9bda5d90]67 if (rc != EOK) {
68 errno = rc;
69 return 0;
70 }
[256cbfe]71
[6453e306]72 errno = EOK;
[256cbfe]73 return val;
74}
75
[6453e306]76uint32_t get_input_uint32(tinput_t *in)
[256cbfe]77{
78 uint32_t val;
[6453e306]79 int rc = convert(in, (conv_f) str_uint32_t, &val);
[9bda5d90]80 if (rc != EOK) {
81 errno = rc;
82 return 0;
83 }
[256cbfe]84
[6453e306]85 errno = EOK;
[256cbfe]86 return val;
87}
88
[6453e306]89uint64_t get_input_uint64(tinput_t *in)
[256cbfe]90{
91 uint64_t val;
[6453e306]92 int rc = convert(in, (conv_f) str_uint64_t, &val);
[9bda5d90]93 if (rc != EOK) {
94 errno = rc;
95 return 0;
96 }
[256cbfe]97
[6453e306]98 errno = EOK;
[256cbfe]99 return val;
100}
101
[6453e306]102size_t get_input_size_t(tinput_t *in)
[256cbfe]103{
104 size_t val;
[6453e306]105 int rc = convert(in, (conv_f) str_size_t, &val);
[256cbfe]106 if (rc != EOK) {
[9bda5d90]107 errno = rc;
108 return 0;
[256cbfe]109 }
[9bda5d90]110
[256cbfe]111 errno = EOK;
112 return val;
113}
114
[6453e306]115static int convert(tinput_t *in, conv_f str_f, void *val)
[256cbfe]116{
[6453e306]117 char *str;
118 int rc = get_input_line(in, &str);
[256cbfe]119 if (rc != EOK) {
120 printf("Error reading input.\n");
121 return rc;
122 }
123
124 rc = str_f(str, NULL, 10, true, val);
[6453e306]125 if (rc != EOK)
[256cbfe]126 printf("Invalid value.\n");
127
128 free(str);
129 return rc;
130}
Note: See TracBrowser for help on using the repository browser.