summaryrefslogtreecommitdiff
path: root/Build/source/texk/gregorio/gregorio-src/src/support.h
blob: 28b4e11bec18c54eb59376554677c0262b248a5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Gregorio is a program that translates gabc files to GregorioTeX
 * This header prototypes the miscellaneous support functions.
 *
 * Copyright (C) 2015-2019 The Gregorio Project (see CONTRIBUTORS.md)
 * 
 * This file is part of Gregorio.
 *
 * Gregorio is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gregorio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gregorio.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef SUPPORT_H
#define SUPPORT_H

#include <stdlib.h>
#include <limits.h>
#ifdef USE_KPSE
#include <kpathsea/kpathsea.h>
#endif
#include "bool.h"
#include "config.h"
#include "messages.h"

#define GREGORIO_SZ_MAX (~((size_t)0))
#define MAX_BUF_GROWTH \
    ((size_t)((((INT_MAX < GREGORIO_SZ_MAX)? INT_MAX : GREGORIO_SZ_MAX) >> 1) + 1))

void gregorio_snprintf(char *s, size_t size, const char *format, ...)
        __attribute__((__format__ (__printf__, 3, 4)));
void *gregorio_malloc(size_t size) __attribute__((malloc));
void *gregorio_calloc(size_t nmemb, size_t size) __attribute__((malloc));
void *gregorio_realloc(void *ptr, size_t size)
    __attribute__((warn_unused_result));
char *gregorio_strdup(const char *s) __attribute__((malloc));
void *_gregorio_grow_buffer(void *buffer, size_t *nmemb, size_t size)
    __attribute__((warn_unused_result));
void gregorio_support_init(const char *program, const char *argv0);
void gregorio_print_version(const char *copyright);
char **gregorio_kpse_find(const char *filename);
void gregorio_exit(int status) __attribute__((noreturn));

#define gregorio_grow_buffer(BUFFER, NMEMB, TYPE) \
    ((TYPE *)_gregorio_grow_buffer(BUFFER, NMEMB, sizeof(TYPE)))

#ifdef USE_KPSE
bool gregorio_read_ok(const char *filename, gregorio_verbosity verbosity);
bool gregorio_write_ok(const char *filename, gregorio_verbosity verbosity);

#define gregorio_check_file_access(DIR, FILENAME, LEVEL, ON_NOT_ALLOWED) \
    if (!gregorio_ ## DIR ## _ok(FILENAME, VERBOSITY_ ## LEVEL)) { \
        ON_NOT_ALLOWED; \
    }
#define gregorio_kpse_find_or_else(VAR, FILENAME, ON_FAIL) \
    VAR = gregorio_kpse_find(FILENAME); \
    if (!VAR) { \
        ON_FAIL; \
    }
#else
#define gregorio_check_file_access(DIRECTION, FILENAME, LEVEL, ON_NOT_ALLOWED)
#define gregorio_kpse_find_or_else(VAR, FILENAME, ON_FAIL) \
    VAR = gregorio_kpse_find(FILENAME); \
    gregorio_not_null(VAR, gregorio_kpse_find, ON_FAIL)
#endif

#endif