Freeciv-3.3
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
149#ifdef FREECIV_NO_CONST_VAR_ARG
150#define VAR_ARG_CONST
151#else
152#define VAR_ARG_CONST const
153#endif
154
155int fc_strcasecmp(const char *str0, const char *str1);
156int fc_strncasecmp(const char *str0, const char *str1, size_t n);
157int fc_strncasequotecmp(const char *str0, const char *str1, size_t n);
158
159/* TODO: Make UTF-8 aware */
160#define fc_strncmp(_s1_, _s2_, _len_) strncmp(_s1_, _s2_, _len_)
161
162void fc_support_init(void);
163void fc_support_free(void);
165
166size_t effectivestrlenquote(const char *str);
167
168char *fc_strcasestr(const char *haystack, const char *needle);
169
170int fc_strcoll(const char *str0, const char *str1);
171int fc_stricoll(const char *str0, const char *str1);
172
173FILE *fc_fopen(const char *filename, const char *opentype);
174#ifdef FREECIV_HAVE_LIBZ
175#include <zlib.h>
176gzFile fc_gzopen(const char *filename, const char *opentype);
177#endif
178int fc_remove(const char *filename);
179int fc_stat(const char *filename, struct stat *buf);
180
182const char *fc_strerror(fc_errno err);
183void fc_usleep(unsigned long usec);
184
185bool fc_strrep(char *str, size_t len, const char *search,
186 const char *replace);
187char *fc_strrep_resize(char *str, size_t *len, const char *search,
188 const char *replace)
190
191size_t fc_strlcpy(char *dest, const char *src, size_t n);
192size_t fc_strlcat(char *dest, const char *src, size_t n);
193
194/* Convenience macros for use when dest is a char ARRAY: */
195#define sz_strlcpy(dest,src) ((void) fc_strlcpy((dest), (src), sizeof(dest)))
196#define sz_strlcat(dest,src) ((void) fc_strlcat((dest), (src), sizeof(dest)))
197
198int fc_snprintf(char *str, size_t n, const char *format, ...)
201int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap )
202 fc__attribute((nonnull (1, 3)));
203int cat_snprintf(char *str, size_t n, const char *format, ...)
205 fc__attribute((nonnull (1, 3)));
206
207int fc_gethostname(char *buf, size_t len);
208
209#ifdef FREECIV_SOCKET_ZERO_NOT_STDIN
210/* Support for console I/O in case FREECIV_SOCKET_ZERO_NOT_STDIN. */
211void fc_init_console(void);
212char *fc_read_console(void);
213#endif /* FREECIV_SOCKET_ZERO_NOT_STDIN */
214
215bool is_reg_file_for_access(const char *name, bool write_access);
216
217int fc_break_lines(char *str, size_t desired_len);
218
219bool fc_isalnum(char c);
220bool fc_isalpha(char c);
221bool fc_isdigit(char c);
222bool fc_isprint(char c);
223bool fc_isspace(char c);
224bool fc_isupper(char c);
225char fc_toupper(char c);
226char fc_tolower(char c);
227
228const char *fc_basename(const char *path);
229
230struct tm *fc_localtime(const time_t *timep, struct tm *result);
231
232/************************************************************************/
235static inline bool is_bigendian(void)
236{
237#ifdef WORDS_BIGENDIAN
238 return TRUE;
239#else /* WORDS_BIGENDIAN */
240 return FALSE;
241#endif /* WORDS_BIGENDIAN */
242}
243
244void make_escapes(const char *str, char *buf, size_t buf_len);
245void remove_escapes(const char *str, bool full_escapes,
246 char *buf, size_t buf_len);
247
248int fc_at_quick_exit(void (*func)(void));
249
250#ifdef __cplusplus
251}
252#endif /* __cplusplus */
253
254#endif /* FC__SUPPORT_H */
#define str
Definition astring.c:76
#define n
Definition astring.c:77
char * incite_cost
Definition comments.c:76
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:1207
int fc_gethostname(char *buf, size_t len)
Definition support.c:1007
bool fc_isprint(char c)
Definition support.c:1229
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:777
void make_escapes(const char *str, char *buf, size_t buf_len)
Definition support.c:294
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
void fc_usleep(unsigned long usec)
Definition support.c:639
static bool is_bigendian(void)
Definition support.h:235
void fc_support_init(void)
Definition support.c:1329
int fc_snprintf(char *str, size_t n, const char *format,...) fc__attribute((__format__(__printf__
bool fc_isspace(char c)
Definition support.c:1240
const char * fc_strerror(fc_errno err)
Definition support.c:609
size_t fc_strlcat(char *dest, const char *src, size_t n)
Definition support.c:822
char * fc_strcasestr(const char *haystack, const char *needle)
Definition support.c:437
bool fc_strrep(char *str, size_t len, const char *search, const char *replace)
Definition support.c:720
struct tm * fc_localtime(const time_t *timep, struct tm *result)
Definition support.c:1301
FILE * fc_fopen(const char *filename, const char *opentype)
Definition support.c:505
int fc_break_lines(char *str, size_t desired_len)
Definition support.c:1135
fc_errno fc_get_errno(void)
Definition support.c:592
int int cat_snprintf(char *str, size_t n, const char *format,...) fc__attribute((__format__(__printf__
void fc_support_free(void)
Definition support.c:1347
void remove_escapes(const char *str, bool full_escapes, char *buf, size_t buf_len)
Definition support.c:330
bool fc_isdigit(char c)
Definition support.c:1218
int fc_strcoll(const char *str0, const char *str1)
Definition support.c:471
#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:574
bool is_reg_file_for_access(const char *name, bool write_access)
Definition support.c:1120
bool fc_isalnum(char c)
Definition support.c:1196
bool fc_isupper(char c)
Definition support.c:1251
const char * fc_basename(const char *path)
Definition support.c:1287
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:377
size_t effectivestrlenquote(const char *str)
Definition support.c:356
char fc_tolower(char c)
Definition support.c:1273
int fc_at_quick_exit(void(*func)(void))
Definition support.c:1317
int fc_stricoll(const char *str0, const char *str1)
Definition support.c:485
int fc_remove(const char *filename)
Definition support.c:555
int fc_strncasecmp(const char *str0, const char *str1, size_t n)
Definition support.c:235
char * fc_strrep_resize(char *str, size_t *len, const char *search, const char *replace) fc__warn_unused_result
Definition support.c:681
bool are_support_services_available(void)
Definition support.c:1369
int fc_errno
Definition support.h:140
char fc_toupper(char c)
Definition support.c:1262