Debian Codegraph @

texk/gregorio/gregorio-src/src/support.h

raw
1 /*
2 * Gregorio is a program that translates gabc files to GregorioTeX
3 * This header prototypes the miscellaneous support functions.
4 *
5 * Copyright (C) 2015-2025 The Gregorio Project (see CONTRIBUTORS.md)
6 *
7 * This file is part of Gregorio.
8 *
9 * Gregorio is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Gregorio is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Gregorio. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #ifndef SUPPORT_H
24 #define SUPPORT_H
25
26 #include <stdlib.h>
27 #include <limits.h>
28 #ifdef USE_KPSE
29 #include <kpathsea/kpathsea.h>
30 #endif
31 #include "bool.h"
32 #include "config.h"
33 #include "messages.h"
34
35 #define GREGORIO_SZ_MAX (~((size_t)0))
36 #define MAX_BUF_GROWTH \
37 ((size_t)((((INT_MAX < GREGORIO_SZ_MAX)? INT_MAX : GREGORIO_SZ_MAX) >> 1) + 1))
38
39 void gregorio_snprintf(char *s, size_t size, const char *format, ...)
40 __attribute__((__format__ (__printf__, 3, 4)));
41 void *gregorio_malloc(size_t size) __attribute__((malloc));
42 void *gregorio_calloc(size_t nmemb, size_t size) __attribute__((malloc));
43 void *gregorio_realloc(void *ptr, size_t size)
44 __attribute__((warn_unused_result));
45 char *gregorio_strdup(const char *s) __attribute__((malloc));
46 void *_gregorio_grow_buffer(void *buffer, size_t *nmemb, size_t size)
47 __attribute__((warn_unused_result));
48 void gregorio_support_init(const char *program, const char *argv0);
49 void gregorio_print_version(const char *copyright);
50 char **gregorio_kpse_find(const char *filename);
51 void gregorio_exit(int status) __attribute__((noreturn));
52
53 #define gregorio_grow_buffer(BUFFER, NMEMB, TYPE) \
54 ((TYPE *)_gregorio_grow_buffer(BUFFER, NMEMB, sizeof(TYPE)))
55
56 #ifdef USE_KPSE
57 bool gregorio_read_ok(const char *filename, gregorio_verbosity verbosity);
58 bool gregorio_write_ok(const char *filename, gregorio_verbosity verbosity);
59
60 #define gregorio_check_file_access(DIR, FILENAME, LEVEL, ON_NOT_ALLOWED) \
61 if (!gregorio_ ## DIR ## _ok(FILENAME, VERBOSITY_ ## LEVEL)) { \
62 ON_NOT_ALLOWED; \
63 }
64 #define gregorio_kpse_find_or_else(VAR, FILENAME, ON_FAIL) \
65 VAR = gregorio_kpse_find(FILENAME); \
66 if (!VAR) { \
67 ON_FAIL; \
68 }
69 #else
70 #define gregorio_check_file_access(DIRECTION, FILENAME, LEVEL, ON_NOT_ALLOWED)
71 #define gregorio_kpse_find_or_else(VAR, FILENAME, ON_FAIL) \
72 VAR = gregorio_kpse_find(FILENAME); \
73 gregorio_not_null(VAR, gregorio_kpse_find, ON_FAIL)
74 #endif
75
76 #endif