Freeciv-3.3: /fast/freeciv/nightly/main/utility/support.h Source File

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
140 typedef 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);
158bool are_support_services_available(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
167 FILE *fc_fopen(const char *filename, const char *opentype);
168#ifdef FREECIV_HAVE_LIBZ
169#include <zlib.h>
170 gzFile 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
175 fc_errno fc_get_errno(void);
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)
183 fc__warn_unused_result;
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
192 int fc_snprintf(char *str, size_t n, const char *format, ...)
193 fc__attribute((__format__ (__printf__, 3, 4)))
194 fc__attribute((nonnull (1, 3)));
195 int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap )
196 fc__attribute((nonnull (1, 3)));
197 int cat_snprintf(char *str, size_t n, const char *format, ...)
198 fc__attribute((__format__ (__printf__, 3, 4)))
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/************************************************************************/
229 static 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 */
str
#define str
Definition astring.c:76
n
#define n
Definition astring.c:77
incite_cost
char * incite_cost
Definition comments.c:74
name
const char * name
Definition inputfile.c:127
func
get_token_fn_t func
Definition inputfile.c:128
len
int len
Definition packhand.c:127
fc_isalpha
bool fc_isalpha(char c)
Definition support.c:1207
fc_gethostname
int fc_gethostname(char *buf, size_t len)
Definition support.c:1007
fc_isprint
bool fc_isprint(char c)
Definition support.c:1229
fc_strlcpy
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:777
make_escapes
void make_escapes(const char *str, char *buf, size_t buf_len)
Definition support.c:294
fc_strcasecmp
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
fc_usleep
void fc_usleep(unsigned long usec)
Definition support.c:639
is_bigendian
static bool is_bigendian(void)
Definition support.h:229
fc_support_init
void fc_support_init(void)
Definition support.c:1329
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...) fc__attribute((__format__(__printf__
fc_isspace
bool fc_isspace(char c)
Definition support.c:1240
fc_strerror
const char * fc_strerror(fc_errno err)
Definition support.c:609
fc_strlcat
size_t fc_strlcat(char *dest, const char *src, size_t n)
Definition support.c:822
fc_strcasestr
char * fc_strcasestr(const char *haystack, const char *needle)
Definition support.c:437
fc_strrep
bool fc_strrep(char *str, size_t len, const char *search, const char *replace)
Definition support.c:720
fc_localtime
struct tm * fc_localtime(const time_t *timep, struct tm *result)
Definition support.c:1301
fc_fopen
FILE * fc_fopen(const char *filename, const char *opentype)
Definition support.c:505
fc_break_lines
int fc_break_lines(char *str, size_t desired_len)
Definition support.c:1135
fc_get_errno
fc_errno fc_get_errno(void)
Definition support.c:592
cat_snprintf
int int cat_snprintf(char *str, size_t n, const char *format,...) fc__attribute((__format__(__printf__
fc_support_free
void fc_support_free(void)
Definition support.c:1347
remove_escapes
void remove_escapes(const char *str, bool full_escapes, char *buf, size_t buf_len)
Definition support.c:330
fc_isdigit
bool fc_isdigit(char c)
Definition support.c:1218
fc_strcoll
int fc_strcoll(const char *str0, const char *str1)
Definition support.c:471
fc__attribute
#define fc__attribute(x)
Definition support.h:99
fc__warn_unused_result
#define fc__warn_unused_result
Definition support.h:109
fc_stat
int fc_stat(const char *filename, struct stat *buf)
Definition support.c:574
is_reg_file_for_access
bool is_reg_file_for_access(const char *name, bool write_access)
Definition support.c:1120
fc_isalnum
bool fc_isalnum(char c)
Definition support.c:1196
fc_isupper
bool fc_isupper(char c)
Definition support.c:1251
fc_basename
const char * fc_basename(const char *path)
Definition support.c:1287
fc_vsnprintf
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap) fc__attribute((nonnull(1
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
fc_strncasequotecmp
int fc_strncasequotecmp(const char *str0, const char *str1, size_t n)
Definition support.c:377
effectivestrlenquote
size_t effectivestrlenquote(const char *str)
Definition support.c:356
fc_tolower
char fc_tolower(char c)
Definition support.c:1273
fc_at_quick_exit
int fc_at_quick_exit(void(*func)(void))
Definition support.c:1317
fc_stricoll
int fc_stricoll(const char *str0, const char *str1)
Definition support.c:485
fc_remove
int fc_remove(const char *filename)
Definition support.c:555
fc_strncasecmp
int fc_strncasecmp(const char *str0, const char *str1, size_t n)
Definition support.c:235
fc_strrep_resize
char * fc_strrep_resize(char *str, size_t *len, const char *search, const char *replace) fc__warn_unused_result
Definition support.c:681
are_support_services_available
bool are_support_services_available(void)
Definition support.c:1369
fc_errno
int fc_errno
Definition support.h:140
fc_toupper
char fc_toupper(char c)
Definition support.c:1262