Freeciv-3.2
Loading...
Searching...
No Matches
support.h
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifndef FC__SUPPORT_H
15#define FC__SUPPORT_H
16
17#ifdef __cplusplus
18extern "C" {
19#endif /* __cplusplus */
20
21/***********************************************************************
22 Replacements for functions which are not available on all platforms.
23 Where the functions are available natively, these are just wrappers.
24 See also mem.h, netintf.h, rand.h, and see support.c for more comments.
25***********************************************************************/
26
27#include <freeciv_config.h>
28
29#include <stdarg.h>
30#include <stdio.h>
31#include <stdlib.h> /* size_t */
32#include <sys/stat.h>
33
34#ifdef FREECIV_HAVE_SYS_TYPES_H
35#include <sys/types.h>
36#endif
37
38#ifdef TRUE
39#undef TRUE
40#endif
41
42#ifdef FALSE
43#undef FALSE
44#endif
45
46#define TRUE true
47#define FALSE false
48
49#ifdef __cplusplus
50#ifndef FREECIV_HAVE_CXX_NULLPTR
51#define nullptr 0
52#endif
53#else
54#ifndef FREECIV_HAVE_C23_NULLPTR
55#define nullptr NULL
56#endif
57#endif
58
59#ifndef __cplusplus
60#if __BEOS__
61#include <posix/be_prim.h>
62#define __bool_true_false_are_defined 1
63#else
64#ifdef FREECIV_HAVE_STDBOOL_H
65#include <stdbool.h>
66#else /* Implement <stdbool.h> ourselves */
67#undef bool
68#undef true
69#undef false
70#undef __bool_true_false_are_defined
71#define bool unsigned int
72#define true 1
73#define false 0
74#define __bool_true_false_are_defined 1
75#endif /* ! FREECIV_HAVE_STDBOOL_H */
76#endif /* ! __BEOS__ */
77#endif /* __cplusplus */
78
79/* intptr_t header */
80/* Prefer full inttypes.h if present. */
81#ifdef FREECIV_HAVE_INTTYPES_H
82#include <inttypes.h>
83#else
84#ifdef FREECIV_HAVE_STDINT_H
85#include <stdint.h>
86#endif /* FREECIV_HAVE_STDINT_H */
87#endif /* FREECIV_HAVE_INTTYPES_H */
88
89/* Want to use GCC's __attribute__ keyword to check variadic
90 * parameters to printf-like functions, without upsetting other
91 * compilers: put any required defines magic here.
92 * If other compilers have something equivalent, could also
93 * work that out here. Should this use configure stuff somehow?
94 * --dwp
95 */
96#if defined(__GNUC__)
97#define fc__attribute(x) __attribute__(x)
98#else
99#define fc__attribute(x)
100#endif
101
102/* __attribute__((warn_unused_result)) requires at least gcc 3.4 */
103#if defined(__GNUC__)
104#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
105#define fc__warn_unused_result __attribute__((warn_unused_result))
106#endif
107#endif
108#ifndef fc__warn_unused_result
109#define fc__warn_unused_result
110#endif
111
112/* TODO: C++17 compilers (also other than g++) could use [[fallthrough]]
113 for C++ code */
114#if defined(__GNUC__) && __GNUC__ >= 7
115#define fc__fallthrough __attribute__((fallthrough))
116#elif defined (__clang__) && __clang_major__ >= 12
117#define fc__fallthrough __attribute__((fallthrough))
118#else
119#define fc__fallthrough
120#endif
121
122#if defined(__GNUC__) && __GNUC__ >= 5
123#define fc__noreturn __attribute__((noreturn))
124#else
125#define fc__noreturn
126#endif
127
128#ifdef FREECIV_HAVE_UNREACHABLE
129#define fc__unreachable(_cond_) \
130 if (_cond_) { \
131 __builtin_unreachable(); \
132 }
133#else /* FREECIV_HAVE_UNREACHABLE */
134#define fc__unreachable(_cond_) fc_assert(!(_cond_))
135#endif /* FREECIV_HAVE_UNREACHABLE */
136
137#ifdef FREECIV_MSWINDOWS
138typedef long int fc_errno;
139#else
140typedef int fc_errno;
141#endif
142
143#ifdef FREECIV_RETURN_VALUE_AFTER_EXIT
144#define RETURN_VALUE_AFTER_EXIT(_val_) return _val_ ;
145#else
146#define RETURN_VALUE_AFTER_EXIT(_val_)
147#endif
148
149int fc_strcasecmp(const char *str0, const char *str1);
150int fc_strncasecmp(const char *str0, const char *str1, size_t n);
151int fc_strncasequotecmp(const char *str0, const char *str1, size_t n);
152
153/* TODO: Make UTF-8 aware */
154#define fc_strncmp(_s1_, _s2_, _len_) strncmp(_s1_, _s2_, _len_)
155
156void fc_support_init(void);
157void fc_support_free(void);
159
160size_t effectivestrlenquote(const char *str);
161
162char *fc_strcasestr(const char *haystack, const char *needle);
163
164int fc_strcoll(const char *str0, const char *str1);
165int fc_stricoll(const char *str0, const char *str1);
166
167FILE *fc_fopen(const char *filename, const char *opentype);
168#ifdef FREECIV_HAVE_LIBZ
169#include <zlib.h>
170gzFile fc_gzopen(const char *filename, const char *opentype);
171#endif
172int fc_remove(const char *filename);
173int fc_stat(const char *filename, struct stat *buf);
174
176const char *fc_strerror(fc_errno err);
177void fc_usleep(unsigned long usec);
178
179bool fc_strrep(char *str, size_t len, const char *search,
180 const char *replace);
181char *fc_strrep_resize(char *str, size_t *len, const char *search,
182 const char *replace)
184
185size_t fc_strlcpy(char *dest, const char *src, size_t n);
186size_t fc_strlcat(char *dest, const char *src, size_t n);
187
188/* Convenience macros for use when dest is a char ARRAY: */
189#define sz_strlcpy(dest,src) ((void) fc_strlcpy((dest), (src), sizeof(dest)))
190#define sz_strlcat(dest,src) ((void) fc_strlcat((dest), (src), sizeof(dest)))
191
192int fc_snprintf(char *str, size_t n, const char *format, ...)
195int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap )
196 fc__attribute((nonnull (1, 3)));
197int cat_snprintf(char *str, size_t n, const char *format, ...)
199 fc__attribute((nonnull (1, 3)));
200
201int fc_gethostname(char *buf, size_t len);
202
203#ifdef FREECIV_SOCKET_ZERO_NOT_STDIN
204/* Support for console I/O in case FREECIV_SOCKET_ZERO_NOT_STDIN. */
205void fc_init_console(void);
206char *fc_read_console(void);
207#endif /* FREECIV_SOCKET_ZERO_NOT_STDIN */
208
209bool is_reg_file_for_access(const char *name, bool write_access);
210
211int fc_break_lines(char *str, size_t desired_len);
212
213bool fc_isalnum(char c);
214bool fc_isalpha(char c);
215bool fc_isdigit(char c);
216bool fc_isprint(char c);
217bool fc_isspace(char c);
218bool fc_isupper(char c);
219char fc_toupper(char c);
220char fc_tolower(char c);
221
222const char *fc_basename(const char *path);
223
224struct tm *fc_localtime(const time_t *timep, struct tm *result);
225
226/************************************************************************/
229static inline bool is_bigendian(void)
230{
231#ifdef WORDS_BIGENDIAN
232 return TRUE;
233#else /* WORDS_BIGENDIAN */
234 return FALSE;
235#endif /* WORDS_BIGENDIAN */
236}
237
238void make_escapes(const char *str, char *buf, size_t buf_len);
239void remove_escapes(const char *str, bool full_escapes,
240 char *buf, size_t buf_len);
241
242int fc_at_quick_exit(void (*func)(void));
243
244#ifdef __cplusplus
245}
246#endif /* __cplusplus */
247
248#endif /* FC__SUPPORT_H */
#define str
Definition astring.c:76
#define n
Definition astring.c:77
char * incite_cost
Definition comments.c:74
const char * name
Definition inputfile.c:127
get_token_fn_t func
Definition inputfile.c:128
int len
Definition packhand.c:127
bool fc_isalpha(char c)
Definition support.c:1221
int fc_gethostname(char *buf, size_t len)
Definition support.c:1021
bool fc_isprint(char c)
Definition support.c:1243
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:791
void make_escapes(const char *str, char *buf, size_t buf_len)
Definition support.c:297
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
void fc_usleep(unsigned long usec)
Definition support.c:641
static bool is_bigendian(void)
Definition support.h:229
void fc_support_init(void)
Definition support.c:1343
int fc_snprintf(char *str, size_t n, const char *format,...) fc__attribute((__format__(__printf__
bool fc_isspace(char c)
Definition support.c:1254
const char * fc_strerror(fc_errno err)
Definition support.c:611
size_t fc_strlcat(char *dest, const char *src, size_t n)
Definition support.c:836
char * fc_strcasestr(const char *haystack, const char *needle)
Definition support.c:440
bool fc_strrep(char *str, size_t len, const char *search, const char *replace)
Definition support.c:734
struct tm * fc_localtime(const time_t *timep, struct tm *result)
Definition support.c:1315
FILE * fc_fopen(const char *filename, const char *opentype)
Definition support.c:507
int fc_break_lines(char *str, size_t desired_len)
Definition support.c:1149
fc_errno fc_get_errno(void)
Definition support.c:594
int int cat_snprintf(char *str, size_t n, const char *format,...) fc__attribute((__format__(__printf__
void fc_support_free(void)
Definition support.c:1361
void remove_escapes(const char *str, bool full_escapes, char *buf, size_t buf_len)
Definition support.c:333
bool fc_isdigit(char c)
Definition support.c:1232
int fc_strcoll(const char *str0, const char *str1)
Definition support.c:473
#define fc__attribute(x)
Definition support.h:99
#define fc__warn_unused_result
Definition support.h:109
int fc_stat(const char *filename, struct stat *buf)
Definition support.c:576
bool is_reg_file_for_access(const char *name, bool write_access)
Definition support.c:1134
bool fc_isalnum(char c)
Definition support.c:1210
bool fc_isupper(char c)
Definition support.c:1265
const char * fc_basename(const char *path)
Definition support.c:1301
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap) fc__attribute((nonnull(1
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
int fc_strncasequotecmp(const char *str0, const char *str1, size_t n)
Definition support.c:380
size_t effectivestrlenquote(const char *str)
Definition support.c:359
char fc_tolower(char c)
Definition support.c:1287
int fc_at_quick_exit(void(*func)(void))
Definition support.c:1331
int fc_stricoll(const char *str0, const char *str1)
Definition support.c:487
int fc_remove(const char *filename)
Definition support.c:557
int fc_strncasecmp(const char *str0, const char *str1, size_t n)
Definition support.c:238
char * fc_strrep_resize(char *str, size_t *len, const char *search, const char *replace) fc__warn_unused_result
Definition support.c:695
bool are_support_services_available(void)
Definition support.c:1383
int fc_errno
Definition support.h:140
char fc_toupper(char c)
Definition support.c:1276