util.h (2299B)
1 /* 2 * Copyright (c) 2021, 2023 lumidify <nobody@lumidify.org> 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #ifndef _LTK_UTIL_H_ 18 #define _LTK_UTIL_H_ 19 20 #include <stdarg.h> 21 #include <stddef.h> 22 23 long long ltk_strtonum( 24 const char *numstr, long long minval, 25 long long maxval, const char **errstrp 26 ); 27 28 char *ltk_read_file(const char *filename, size_t *len_ret, char **errstr_ret); 29 int ltk_write_file(const char *path, const char *data, size_t len, char **errstr_ret); 30 int ltk_parse_run_cmd(const char *cmdtext, size_t len, const char *filename); 31 void ltk_grow_string(char **str, int *alloc_size, int needed); 32 char *ltk_setup_directory(void); 33 char *ltk_strcat_useful(const char *str1, const char *str2); 34 35 /* Note: these are actually implemented in ltkd.c and ltkc.c (they are just 36 declared here so they can be used by the utility functions */ 37 void ltk_log_msg(const char *mode, const char *format, va_list args); 38 void ltk_cleanup(void); 39 40 void ltk_fatal_errno(const char *format, ...); 41 void ltk_warn_errno(const char *format, ...); 42 void ltk_fatal(const char *format, ...); 43 void ltk_warn(const char *format, ...); 44 45 /* 46 * Compare the nul-terminated string 'terminated' with the char 47 * array 'array' with length 'len'. 48 * Returns non-zero if they are equal, 0 otherwise. 49 */ 50 /* Note: this doesn't work if array contains '\0'. */ 51 int str_array_equal(const char *terminated, const char *array, size_t len); 52 53 size_t prev_utf8(char *text, size_t index); 54 size_t next_utf8(char *text, size_t len, size_t index); 55 56 int set_nonblock(int fd); 57 58 #define LENGTH(X) (sizeof(X) / sizeof(X[0])) 59 60 #endif /* _LTK_UTIL_H_ */