chiark / gitweb /
add generic string lookup macros
[elogind.git] / util.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #ifndef fooutilhfoo
4 #define fooutilhfoo
5
6 #include <inttypes.h>
7 #include <time.h>
8 #include <sys/time.h>
9 #include <stdbool.h>
10 #include <stdlib.h>
11
12 typedef uint64_t usec_t;
13
14 #define MSEC_PER_SEC  1000ULL
15 #define USEC_PER_SEC  1000000ULL
16 #define USEC_PER_MSEC 1000ULL
17 #define NSEC_PER_SEC  1000000000ULL
18 #define NSEC_PER_MSEC 1000000ULL
19 #define NSEC_PER_USEC 1000ULL
20
21 /* What is interpreted as whitespace? */
22 #define WHITESPACE " \t\n\r"
23
24 usec_t now(clockid_t clock);
25
26 usec_t timespec_load(const struct timespec *ts);
27 struct timespec *timespec_store(struct timespec *ts, usec_t u);
28
29 usec_t timeval_load(const struct timeval *tv);
30 struct timeval *timeval_store(struct timeval *tv, usec_t u);
31
32 #define streq(a,b) (strcmp((a),(b)) == 0)
33
34 #define new(t, n) ((t*) malloc(sizeof(t)*(n)))
35
36 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
37
38 #define malloc0(n) (calloc((n), 1))
39
40 static inline const char* yes_no(bool b) {
41         return b ? "yes" : "no";
42 }
43
44 static inline const char* strempty(const char *s) {
45         return s ? s : "";
46 }
47
48 static inline const char* strnull(const char *s) {
49         return s ? s : "(null)";
50 }
51
52 static inline const char *strna(const char *s) {
53         return s ? s : "n/a";
54 }
55
56 static inline bool is_path_absolute(const char *p) {
57         return *p == '/';
58 }
59
60 bool endswith(const char *s, const char *postfix);
61 bool startswith(const char *s, const char *prefix);
62
63 int close_nointr(int fd);
64 void close_nointr_nofail(int fd);
65
66 int parse_boolean(const char *v);
67
68 int safe_atou(const char *s, unsigned *ret_u);
69 int safe_atoi(const char *s, int *ret_i);
70
71 int safe_atolu(const char *s, unsigned long *ret_u);
72 int safe_atoli(const char *s, long int *ret_i);
73
74 int safe_atollu(const char *s, unsigned long long *ret_u);
75 int safe_atolli(const char *s, long long int *ret_i);
76
77 char *split_spaces(const char *c, size_t *l, char **state);
78 char *split_quoted(const char *c, size_t *l, char **state);
79
80 #define FOREACH_WORD(word, length, s, state)                            \
81         for ((state) = NULL, (word) = split_spaces((s), &(l), &(state)); (word); (word) = split_spaces((s), &(l), &(state)))
82
83 #define FOREACH_WORD_QUOTED(word, length, s, state)                     \
84         for ((state) = NULL, (word) = split_quoted((s), &(l), &(state)); (word); (word) = split_quoted((s), &(l), &(state)))
85
86 pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
87
88 int write_one_line_file(const char *fn, const char *line);
89 int read_one_line_file(const char *fn, char **line);
90
91 char *strappend(const char *s, const char *suffix);
92
93 int readlink_malloc(const char *p, char **r);
94
95 char *file_name_from_path(const char *p);
96 bool is_path(const char *p);
97
98 bool path_is_absolute(const char *p);
99 char *path_make_absolute(const char *p, const char *prefix);
100
101 int reset_all_signal_handlers(void);
102
103 char *strstrip(char *s);
104 char *file_in_same_dir(const char *path, const char *filename);
105
106 char hexchar(int x);
107 int unhexchar(char c);
108 char octchar(int x);
109 int unoctchar(char c);
110
111 char *cescape(const char *s);
112 char *cunescape(const char *s);
113
114 char *path_kill_slashes(char *path);
115
116 bool path_startswith(const char *path, const char *prefix);
117
118 char *ascii_strlower(char *path);
119
120 char *xescape(const char *s, const char *bad);
121
122 #define DEFINE_STRING_TABLE_LOOKUP(name,type)                           \
123         const char *name##_to_string(type i) {                          \
124                 if (i < 0 || i >= (type) ELEMENTSOF(name##_table))      \
125                         return NULL;                                    \
126                 return name##_table[i];                                 \
127         }                                                               \
128         type name##_from_string(const char *s) {                        \
129                 type i;                                                 \
130                 assert(s);                                              \
131                 for (i = 0; i < (type)ELEMENTSOF(name##_table); i++)    \
132                         if (streq(name##_table[i], s))                  \
133                                 return i;                               \
134                 return (type) -1;                                       \
135         }                                                               \
136         struct __useless_struct_to_allow_trailing_semicolon__
137
138
139 const char *ioprio_class_to_string(int i);
140 int ioprio_class_from_string(const char *s);
141
142 const char *sigchld_code_to_string(int i);
143 int sigchld_code_from_string(const char *s);
144
145 const char *log_facility_to_string(int i);
146 int log_facility_from_string(const char *s);
147
148 const char *log_level_to_string(int i);
149 int log_level_from_string(const char *s);
150
151 const char *sched_policy_to_string(int i);
152 int sched_policy_from_string(const char *s);
153
154 const char *rlimit_to_string(int i);
155 int rlimit_from_string(const char *s);
156
157 #endif