]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/parse-util.h
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / basic / parse-util.h
CommitLineData
6bedfcbb
LP
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
29int parse_boolean(const char *v) _pure_;
30int parse_pid(const char *s, pid_t* ret_pid);
31int parse_mode(const char *s, mode_t *ret);
32
33int parse_size(const char *t, uint64_t base, uint64_t *size);
34
35#define FORMAT_BYTES_MAX 8
36char *format_bytes(char *buf, size_t l, uint64_t t);
37
38int safe_atou(const char *s, unsigned *ret_u);
39int safe_atoi(const char *s, int *ret_i);
40int safe_atollu(const char *s, unsigned long long *ret_u);
41int safe_atolli(const char *s, long long int *ret_i);
42
43int safe_atou8(const char *s, uint8_t *ret);
44
45int safe_atou16(const char *s, uint16_t *ret);
46int safe_atoi16(const char *s, int16_t *ret);
47
48static inline int safe_atou32(const char *s, uint32_t *ret_u) {
49 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
50 return safe_atou(s, (unsigned*) ret_u);
51}
52
53static inline int safe_atoi32(const char *s, int32_t *ret_i) {
54 assert_cc(sizeof(int32_t) == sizeof(int));
55 return safe_atoi(s, (int*) ret_i);
56}
57
58static inline int safe_atou64(const char *s, uint64_t *ret_u) {
59 assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
60 return safe_atollu(s, (unsigned long long*) ret_u);
61}
62
63static inline int safe_atoi64(const char *s, int64_t *ret_i) {
64 assert_cc(sizeof(int64_t) == sizeof(long long int));
65 return safe_atolli(s, (long long int*) ret_i);
66}
67
68#if LONG_MAX == INT_MAX
69static inline int safe_atolu(const char *s, unsigned long *ret_u) {
70 assert_cc(sizeof(unsigned long) == sizeof(unsigned));
71 return safe_atou(s, (unsigned*) ret_u);
72}
73static inline int safe_atoli(const char *s, long int *ret_u) {
74 assert_cc(sizeof(long int) == sizeof(int));
75 return safe_atoi(s, (int*) ret_u);
76}
77#else
78static inline int safe_atolu(const char *s, unsigned long *ret_u) {
79 assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
80 return safe_atollu(s, (unsigned long long*) ret_u);
81}
82static inline int safe_atoli(const char *s, long int *ret_u) {
83 assert_cc(sizeof(long int) == sizeof(long long int));
84 return safe_atolli(s, (long long int*) ret_u);
85}
86#endif
87
88int safe_atod(const char *s, double *ret_d);