downstream: cosmetic changes (tabulation etc)
[profile/ivi/weston-ivi-shell.git] / shared / str-util.c
1 /*
2  * Copyright © 2014 Intel Corporation.
3  *
4  * Contact: Imran Zaman <imran.zaman@linux.intel.com>
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and
7  * its documentation for any purpose is hereby granted without fee, provided
8  * that the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of the copyright holders not be used in
11  * advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission.  The copyright holders make
13  * no representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  */
24
25 #include "config.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <ctype.h>
31 #include <limits.h>
32
33 #ifdef IN_WESTON
34 #include <wayland-util.h>
35 #else
36 #define WL_EXPORT
37 #endif
38
39 #include "str-util.h"
40
41 static bool
42 convert_strtol(const char *str, char **endptr, int base, long *val)
43 {
44        char *end = NULL;
45        long v;
46        int prev_errno = errno;
47
48        if (!str || !val)
49                return false;
50        if (!endptr)
51                endptr = &end;
52
53        errno = 0;
54        v = strtol(str, endptr, base);
55        if (errno != 0 || *endptr == str || **endptr != '\0')
56                return false;
57
58        errno = prev_errno;
59        *val = v;
60        return true;
61 }
62
63 static bool
64 convert_strtoul (const char *str, char **endptr, int base, unsigned long *val)
65 {
66        char *end = NULL;
67        unsigned long v;
68        int i = 0;
69        int prev_errno = errno;
70
71        if (!str || !val)
72                return false;
73
74        /* check for negative numbers */
75        while (str[i]) {
76                if (!isspace(str[i])) {
77                        if (str[i] == '-')
78                                return false;
79                        else
80                                break;
81                }
82                i++;
83        }
84
85        if (!endptr)
86                endptr = &end;
87
88        errno = 0;
89        v = strtoul(str, endptr, base);
90        if (errno != 0 || *endptr == str || **endptr != '\0')
91                return false;
92
93        errno = prev_errno;
94        *val = v;
95        return true;
96 }
97
98 WL_EXPORT bool
99 weston_strtoi(const char *str, char **endptr, int base, int *val)
100 {
101         long v;
102
103         if (!convert_strtol(str, endptr, base, &v) || v > INT_MAX
104                         || v < INT_MIN)
105                 return false;
106
107         *val = (int)v;
108         return true;
109 }
110
111 WL_EXPORT bool
112 weston_strtol(const char *str, char **endptr, int base, long *val)
113 {
114         return convert_strtol(str, endptr, base, val);
115 }
116
117 WL_EXPORT bool
118 weston_strtoui(const char *str, char **endptr, int base, unsigned int *val)
119 {
120         unsigned long v;
121
122         if (!convert_strtoul(str, endptr, base, &v) || v > UINT_MAX)
123                 return false;
124
125         *val = (unsigned int)v;
126         return true;
127 }
128
129 WL_EXPORT bool
130 weston_strtoul(const char *str, char **endptr, int base, unsigned long *val)
131 {
132         return convert_strtoul(str, endptr, base, val);
133 }