Freeciv-3.1
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#ifndef __cplusplus
50#if __BEOS__
51#include <posix/be_prim.h>
52#define __bool_true_false_are_defined 1
53#else
54#ifdef FREECIV_HAVE_STDBOOL_H
55#include <stdbool.h>
56#else /* Implement <stdbool.h> ourselves */
57#undef bool
58#undef true
59#undef false
60#undef __bool_true_false_are_defined
61#define bool unsigned int
62#define true 1
63#define false 0
64#define __bool_true_false_are_defined 1
65#endif /* ! FREECIV_HAVE_STDBOOL_H */
66#endif /* ! __BEOS__ */
67#endif /* __cplusplus */
68
69/* intptr_t header */
70/* Prefer full inttypes.h if present. */
71#ifdef FREECIV_HAVE_INTTYPES_H
72#include <inttypes.h>
73#else
74#ifdef FREECIV_HAVE_STDINT_H
75#include <stdint.h>
76#endif /* FREECIV_HAVE_STDINT_H */
77#endif /* FREECIV_HAVE_INTTYPES_H */
78
79/* Want to use GCC's __attribute__ keyword to check variadic
80 * parameters to printf-like functions, without upsetting other
81 * compilers: put any required defines magic here.
82 * If other compilers have something equivalent, could also
83 * work that out here. Should this use configure stuff somehow?
84 * --dwp
85 */
86#if defined(__GNUC__)
87#define fc__attribute(x) __attribute__(x)
88#else
89#define fc__attribute(x)
90#endif
91
92/* __attribute__((warn_unused_result)) requires at least gcc 3.4 */
93#if defined(__GNUC__)
94#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
95#define fc__warn_unused_result __attribute__((warn_unused_result))
96#endif
97#endif
98#ifndef fc__warn_unused_result
99#define fc__warn_unused_result
100#endif
101
102/* TODO: C++17 compilers (also other than g++) could use [[fallthrough]]
103 for C++ code */
104#if defined(__GNUC__) && __GNUC__ >= 7
105#define fc__fallthrough __attribute__((fallthrough))
106#elif defined (__clang__) && __clang_major__ >= 12
107#define fc__fallthrough __attribute__((fallthrough))
108#else
109#define fc__fallthrough
110#endif
111
112#ifdef FREECIV_MSWINDOWS
113typedef long int fc_errno;
114#else
115typedef int fc_errno;
116#endif
117
118#ifdef FREECIV_RETURN_VALUE_AFTER_EXIT
119#define RETURN_VALUE_AFTER_EXIT(_val_) return _val_ ;
120#else
121#define RETURN_VALUE_AFTER_EXIT(_val_)
122#endif
123
124#ifdef FREECIV_NO_CONST_VAR_ARG
125#define VAR_ARG_CONST
126#else
127#define VAR_ARG_CONST const
128#endif
129
130int fc_strcasecmp(const char *str0, const char *str1);
131int fc_strncasecmp(const char *str0, const char *str1, size_t n);
132int fc_strncasequotecmp(const char *str0, const char *str1, size_t n);
133
134void fc_support_init(void);
135void fc_support_free(void);
137
138size_t effectivestrlenquote(const char *str);
139
140char *fc_strcasestr(const char *haystack, const char *needle);
141
142int fc_strcoll(const char *str0, const char *str1);
143int fc_stricoll(const char *str0, const char *str1);
144
145FILE *fc_fopen(const char *filename, const char *opentype);
146#ifdef FREECIV_HAVE_LIBZ
147#include <zlib.h>
148gzFile fc_gzopen(const char *filename, const char *opentype);
149#endif
150int fc_remove(const char *filename);
151int fc_stat(const char *filename, struct stat *buf);
152
154const char *fc_strerror(fc_errno err);
155void fc_usleep(unsigned long usec);
156
157bool fc_strrep(char *str, size_t len, const char *search,
158 const char *replace);
159char *fc_strrep_resize(char *str, size_t *len, const char *search,
160 const char *replace)
162
163size_t fc_strlcpy(char *dest, const char *src, size_t n);
164size_t fc_strlcat(char *dest, const char *src, size_t n);
165
166/* convenience macros for use when dest is a char ARRAY: */
167#define sz_strlcpy(dest,src) ((void) fc_strlcpy((dest), (src), sizeof(dest)))
168#define sz_strlcat(dest,src) ((void) fc_strlcat((dest), (src), sizeof(dest)))
169
170int fc_snprintf(char *str, size_t n, const char *format, ...)
171 fc__attribute((__format__ (__printf__, 3, 4)))
172 fc__attribute((nonnull (1, 3)));
173int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap )
174 fc__attribute((nonnull (1, 3)));
175int cat_snprintf(char *str, size_t n, const char *format, ...)
176 fc__attribute((__format__ (__printf__, 3, 4)))
177 fc__attribute((nonnull (1, 3)));
178
179int fc_gethostname(char *buf, size_t len);
180
181#ifdef FREECIV_SOCKET_ZERO_NOT_STDIN
182/* Support for console I/O in case FREECIV_SOCKET_ZERO_NOT_STDIN. */
183void fc_init_console(void);
184char *fc_read_console(void);
185#endif /* FREECIV_SOCKET_ZERO_NOT_STDIN */
186
187bool is_reg_file_for_access(const char *name, bool write_access);
188
189int fc_break_lines(char *str, size_t desired_len);
190
191bool fc_isalnum(char c);
192bool fc_isalpha(char c);
193bool fc_isdigit(char c);
194bool fc_isprint(char c);
195bool fc_isspace(char c);
196bool fc_isupper(char c);
197char fc_toupper(char c);
198char fc_tolower(char c);
199
200const char *fc_basename(const char *path);
201
202struct tm *fc_localtime(const time_t *timep, struct tm *result);
203
204/************************************************************************/
207static inline bool is_bigendian(void)
208{
209#ifdef WORDS_BIGENDIAN
210 return TRUE;
211#else /* WORDS_BIGENDIAN */
212 return FALSE;
213#endif /* WORDS_BIGENDIAN */
214}
215
216void make_escapes(const char *str, char *buf, size_t buf_len);
217void remove_escapes(const char *str, bool full_escapes,
218 char *buf, size_t buf_len);
219
220int fc_at_quick_exit(void (*func)(void));
221
222#ifdef __cplusplus
223}
224#endif /* __cplusplus */
225
226#endif /* FC__SUPPORT_H */
#define str
Definition astring.c:76
#define n
Definition astring.c:77
const char * name
Definition inputfile.c:127
get_token_fn_t func
Definition inputfile.c:128
int len
Definition packhand.c:125
bool fc_isalpha(char c)
Definition support.c:1216
int fc_gethostname(char *buf, size_t len)
Definition support.c:1016
bool fc_isprint(char c)
Definition support.c:1238
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:787
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:640
static bool is_bigendian(void)
Definition support.h:207
void fc_support_init(void)
Definition support.c:1338
int fc_snprintf(char *str, size_t n, const char *format,...) fc__attribute((__format__(__printf__
bool fc_isspace(char c)
Definition support.c:1249
const char * fc_strerror(fc_errno err)
Definition support.c:610
size_t fc_strlcat(char *dest, const char *src, size_t n)
Definition support.c:832
char * fc_strcasestr(const char *haystack, const char *needle)
Definition support.c:439
bool fc_strrep(char *str, size_t len, const char *search, const char *replace)
Definition support.c:731
struct tm * fc_localtime(const time_t *timep, struct tm *result)
Definition support.c:1310
FILE * fc_fopen(const char *filename, const char *opentype)
Definition support.c:506
int fc_break_lines(char *str, size_t desired_len)
Definition support.c:1144
fc_errno fc_get_errno(void)
Definition support.c:593
int int cat_snprintf(char *str, size_t n, const char *format,...) fc__attribute((__format__(__printf__
void fc_support_free(void)
Definition support.c:1356
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:1227
int fc_strcoll(const char *str0, const char *str1)
Definition support.c:472
#define fc__attribute(x)
Definition support.h:89
#define fc__warn_unused_result
Definition support.h:99
int fc_stat(const char *filename, struct stat *buf)
Definition support.c:575
bool is_reg_file_for_access(const char *name, bool write_access)
Definition support.c:1129
bool fc_isalnum(char c)
Definition support.c:1205
bool fc_isupper(char c)
Definition support.c:1260
const char * fc_basename(const char *path)
Definition support.c:1296
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:379
size_t effectivestrlenquote(const char *str)
Definition support.c:359
char fc_tolower(char c)
Definition support.c:1282
int fc_at_quick_exit(void(*func)(void))
Definition support.c:1326
int fc_stricoll(const char *str0, const char *str1)
Definition support.c:486
int fc_remove(const char *filename)
Definition support.c:556
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:693
bool are_support_services_available(void)
Definition support.c:1378
int fc_errno
Definition support.h:115
char fc_toupper(char c)
Definition support.c:1271