Dunst
Lightweight notification daemon
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause */
10
11#ifndef DUNST_UTILS_H
12#define DUNST_UTILS_H
13
14#include <glib.h>
15#include <stdbool.h>
16#include <stdio.h>
17#include <string.h>
18
20#define STR_EMPTY(s) (!s || (*s == '\0'))
21
23#define STR_FULL(s) !(STR_EMPTY(s))
24
26#define STR_EQ(a, b) (g_strcmp0(a, b) == 0)
27
29#define STRN_EQ(a, b, n) (strncmp(a, b, n) == 0)
30
32#define STR_CASEQ(a, b) (strcasecmp(a, b) == 0)
33
35#define STR_NN(s) (s == NULL ? "(null)" : s)
36
38#define STR_TO(...) _STR_TO(__VA_ARGS__)
39#define _STR_TO(...) "" # __VA_ARGS__
40
42#define ASSERT_OR_RET(expr, val) \
43 do { if (!(expr)) return val; } while(0)
44
46#define S2US(s) (((gint64)(s)) * G_USEC_PER_SEC)
47
49#define US2S(s) (((gint64)(s)) / G_USEC_PER_SEC)
50
55#define BOOL2G(x) ((x) ? TRUE : FALSE)
56
57
69char *string_replace_char(char needle, char replacement, char *haystack);
70
81char *string_replace_at(char *buf, int pos, int len, const char *repl);
82
90char *string_replace_all(const char *needle, const char *replacement, char *haystack);
91
99char *string_append(char *a, const char *b, const char *sep);
100
107char *string_strip_quotes(const char *value);
108
116void string_strip_delimited(char *str, char a, char b);
117
129char **string_to_array(const char *string, const char *delimiter);
130
139char *string_to_path(char *string);
140
151bool safe_string_to_int(int *in, const char *str);
152
156bool safe_string_to_long_long(long long *in, const char *str);
157
161bool safe_string_to_double(double *in, const char *str);
162
170gint64 string_to_time(const char *string);
171
179gint64 time_monotonic_now(void);
180
186gint64 time_now(void);
187
191gint64 modification_time(const char *path);
192
198const char *user_get_home(void);
199
209bool safe_setenv(const char* key, const char* value);
210
218bool is_special_section(const char* s);
219
226bool is_deprecated_section(const char* s);
227
228const char *get_section_deprecation_message(const char *s);
229
230
238char *string_strip_brackets(const char* s);
239
240
244int string_array_length(char **s);
245
263bool is_readable_file(const char * const path);
264
275FILE *fopen_verbose(const char * const path);
276
290void add_paths_from_env(GPtrArray *arr, char *env_name, char *subdir, char *alternative);
291
295bool string_is_int(const char *str);
296
302bool is_like_path(const char *string);
303
304#endif
gint64 modification_time(const char *path)
Get the modification time of the file at path.
Definition utils.c:341
gint64 time_monotonic_now(void)
Get the current monotonic time.
Definition utils.c:315
bool safe_setenv(const char *key, const char *value)
Try to set an environment variable safely.
Definition utils.c:365
char * string_replace_at(char *buf, int pos, int len, const char *repl)
Replace a substring inside a string with another string.
Definition utils.c:37
bool string_is_int(const char *str)
Check if string contains digits.
Definition utils.c:485
bool is_like_path(const char *string)
Check if the strings looks like a path.
Definition utils.c:495
FILE * fopen_verbose(const char *const path)
Open files verbosely.
Definition utils.c:458
bool safe_string_to_long_long(long long *in, const char *str)
Same as safe_string_to_int, but then for a long.
Definition utils.c:210
const char * user_get_home(void)
Retrieve the HOME directory of the user running dunst.
Definition utils.c:350
int string_array_length(char **s)
Returns the length of a string array, -1 if the input is NULL.
Definition utils.c:166
bool is_readable_file(const char *const path)
Check if file is readable.
Definition utils.c:437
gint64 time_now(void)
Get the current real time.
Definition utils.c:333
char * string_strip_brackets(const char *s)
Strips a string of it's brackets if the first and last character are a bracket.
Definition utils.c:425
bool safe_string_to_double(double *in, const char *str)
Same as safe_string_to_int, but then for a double.
Definition utils.c:245
bool safe_string_to_int(int *in, const char *str)
Convert string to int in a safe way.
Definition utils.c:229
void string_strip_delimited(char *str, char a, char b)
Strip content between two delimiter characters.
Definition utils.c:126
char * string_to_path(char *string)
Replace tilde and path-specific values with it's equivalents.
Definition utils.c:178
char * string_strip_quotes(const char *value)
Strip quotes from a string, ignoring inner quotes.
Definition utils.c:111
bool is_deprecated_section(const char *s)
This function tells if a section is deprecated.
Definition utils.c:407
char * string_replace_all(const char *needle, const char *replacement, char *haystack)
Replace all occurences of a substring.
Definition utils.c:66
bool is_special_section(const char *s)
Some sections are handled differently in dunst.
Definition utils.c:398
char * string_append(char *a, const char *b, const char *sep)
Append b to string a, then concatenate both with sep (if they are non-empty).
Definition utils.c:92
void add_paths_from_env(GPtrArray *arr, char *env_name, char *subdir, char *alternative)
Adds the contents of env_name with subdir to the array, interpreting the environment variable as a co...
Definition utils.c:472
char ** string_to_array(const char *string, const char *delimiter)
Parse a string into a dynamic array of tokens, using the delimiter string.
Definition utils.c:154
char * string_replace_char(char needle, char replacement, char *haystack)
Replaces all occurrences of the char needle with the char replacement in haystack.
Definition utils.c:27
gint64 string_to_time(const char *string)
Convert time units (ms, s, m) to the internal gint64 microseconds format.
Definition utils.c:263