chiark / gitweb /
Prep v228: Apply more cosmetic changes found in upstream.
[elogind.git] / src / basic / parse-util.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2010 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <inttypes.h>
25 #include <sys/types.h>
26
27 #include "macro.h"
28
29 #define MODE_INVALID ((mode_t) -1)
30
31 int parse_boolean(const char *v) _pure_;
32 int parse_pid(const char *s, pid_t* ret_pid);
33 int parse_mode(const char *s, mode_t *ret);
34 int parse_ifindex(const char *s, int *ret);
35
36 int parse_size(const char *t, uint64_t base, uint64_t *size);
37 // UNNEEDED int parse_range(const char *t, unsigned *lower, unsigned *upper);
38
39 // UNNEEDED #define FORMAT_BYTES_MAX 8
40 // UNNEEDED char *format_bytes(char *buf, size_t l, uint64_t t);
41
42 int safe_atou(const char *s, unsigned *ret_u);
43 int safe_atoi(const char *s, int *ret_i);
44 int safe_atollu(const char *s, unsigned long long *ret_u);
45 int safe_atolli(const char *s, long long int *ret_i);
46
47 int safe_atou8(const char *s, uint8_t *ret);
48
49 int safe_atou16(const char *s, uint16_t *ret);
50 int safe_atoi16(const char *s, int16_t *ret);
51
52 static inline int safe_atou32(const char *s, uint32_t *ret_u) {
53         assert_cc(sizeof(uint32_t) == sizeof(unsigned));
54         return safe_atou(s, (unsigned*) ret_u);
55 }
56
57 static inline int safe_atoi32(const char *s, int32_t *ret_i) {
58         assert_cc(sizeof(int32_t) == sizeof(int));
59         return safe_atoi(s, (int*) ret_i);
60 }
61
62 static inline int safe_atou64(const char *s, uint64_t *ret_u) {
63         assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
64         return safe_atollu(s, (unsigned long long*) ret_u);
65 }
66
67 static inline int safe_atoi64(const char *s, int64_t *ret_i) {
68         assert_cc(sizeof(int64_t) == sizeof(long long int));
69         return safe_atolli(s, (long long int*) ret_i);
70 }
71
72 #if LONG_MAX == INT_MAX
73 static inline int safe_atolu(const char *s, unsigned long *ret_u) {
74         assert_cc(sizeof(unsigned long) == sizeof(unsigned));
75         return safe_atou(s, (unsigned*) ret_u);
76 }
77 static inline int safe_atoli(const char *s, long int *ret_u) {
78         assert_cc(sizeof(long int) == sizeof(int));
79         return safe_atoi(s, (int*) ret_u);
80 }
81 #else
82 static inline int safe_atolu(const char *s, unsigned long *ret_u) {
83         assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
84         return safe_atollu(s, (unsigned long long*) ret_u);
85 }
86 static inline int safe_atoli(const char *s, long int *ret_u) {
87         assert_cc(sizeof(long int) == sizeof(long long int));
88         return safe_atolli(s, (long long int*) ret_u);
89 }
90 #endif
91
92 int safe_atod(const char *s, double *ret_d);