util.h (3679B)
1 /* This program is licensed under the terms of GNU GPL v3 or (at your option) 2 * any later version. Copyright (C) 2021-2024 Страхиња Радић. 3 * See the file LICENSE for exact copyright and license details. */ 4 5 #include <stdint.h> 6 #include <sys/types.h> 7 #define ABS(a) ((a < 0) ? (-a) : (a)) 8 #define LEN(x) (sizeof(x) / sizeof(x[0])) 9 #define MAX(a, b) ((a > b) ? a : b) 10 #define MIN(a, b) ((a < b) ? a : b) 11 #define PROGRAMNAME "poe" 12 #define STRLCAT(tail, head, totallen) \ 13 do \ 14 { \ 15 if (strlcat(head, tail, totallen) >= totallen) \ 16 { \ 17 print_error(1, "strlcat:%d: Overflow", __LINE__); \ 18 exit(1); \ 19 } \ 20 } while (0) 21 #define STRLCPY(from, to, tolen) \ 22 do \ 23 { \ 24 if (strlcpy(to, from, tolen) >= tolen) \ 25 { \ 26 print_error(1, "strlcpy:%d: Overflow", __LINE__); \ 27 exit(1); \ 28 } \ 29 } while (0) 30 31 enum { 32 ERR_UNKNOWN_KEY, 33 ERR_UNKNOWN_KEY_HELP, 34 ERR_EXIT_KEY, 35 ERR_DLG_OPEN_QUIT, 36 ERR_DLG_OPEN, 37 ERR_DLG_FOCUS_EDIT, 38 ERR_EMPTY_FILE, 39 ERR_CANT_ALLOC, 40 ERR_CANT_SAVE, 41 ERR_CANT_MOVE, 42 ERR_ILLEGAL_ON_FIRST, 43 ERR_NO_PLURAL_FORMS 44 }; 45 46 #define MAXBUFLINE 4096 47 #define MAXCOPYBUF 8192 48 #define MAXDATEBUF 80 49 #define MAXFLAGSBUF (2 + 4 + 2 + 1) 50 #define MAXMSGLINE 1024 51 #define MSGDELTA 1024 52 #define MAXPATH 1024 53 /* maximum chars for UTF-8 representations of Unicode chars, per Unicode char */ 54 #define UTF8REPMAX 6 55 56 extern int utlerrno; 57 58 size_t strlcpy(char* dst, const char* src, size_t dsize); 59 size_t strlcat(char* dst, const char* src, size_t dsize); 60 size_t u8_string_to_unicode(uint32_t* us, const char* s, const size_t max); 61 size_t unicode_string_to_u8(char* s, const size_t s_size, const uint32_t* us, 62 const size_t max); 63 size_t u32_match_msgid_ending(const uint32_t* msgid, uint32_t* msgstr, 64 size_t max); 65 size_t u32_count_chars(const uint32_t* s, const uint32_t ch); 66 size_t u32_count_strings(const uint32_t* haystack, const uint32_t* needle); 67 size_t u32_encode_tabs(uint32_t* s, size_t max); 68 size_t u32_decode_tabs(uint32_t* s, size_t max); 69 size_t u32_strlen(const uint32_t* s); 70 const uint32_t* u32_strstr(const uint32_t* haystack, const uint32_t* needle); 71 const uint32_t* u32_strchr(const uint32_t* haystack, const uint32_t needle); 72 size_t u32_strncpy(uint32_t* to, const uint32_t* from, size_t max); 73 size_t u32_strncat(uint32_t* to, const uint32_t* from, size_t max); 74 size_t u32_u8_strncpy(uint32_t* to, const char* from, size_t max); 75 size_t u8_u32_strncpy(char* to, const uint32_t* from, size_t max); 76 size_t u32_lines_in_string(const uint32_t* s, const int include_eol, 77 const size_t wrap); 78 size_t u32_next_line(const uint32_t* s, const uint32_t** sptr, 79 const int include_eol, const size_t wrap); 80 const int is_word_boundary(const char ch, const int strictly_whitespace); 81 const int u32_is_word_boundary(const uint32_t ch, const int strictly_whitespace); 82 const int starts_with(const char* s, const char* with); 83 const int u32_starts_with(const uint32_t* s, const uint32_t* with); 84 const char* u8_basename(const char* path); 85 /*void poe_log(const char* msg, ...);*/