Freeciv-3.1: /fast/freeciv/nightly/S3_1/utility/support.h Source File

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
115 typedef 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
124int fc_strcasecmp(const char *str0, const char *str1);
125int fc_strncasecmp(const char *str0, const char *str1, size_t n);
126int fc_strncasequotecmp(const char *str0, const char *str1, size_t n);
127
128void fc_support_init(void);
129void fc_support_free(void);
130bool are_support_services_available(void);
131
132size_t effectivestrlenquote(const char *str);
133
134char *fc_strcasestr(const char *haystack, const char *needle);
135
136int fc_strcoll(const char *str0, const char *str1);
137int fc_stricoll(const char *str0, const char *str1);
138
139FILE *fc_fopen(const char *filename, const char *opentype);
140#ifdef FREECIV_HAVE_LIBZ
141#include <zlib.h>
142gzFile fc_gzopen(const char *filename, const char *opentype);
143#endif
144int fc_remove(const char *filename);
145int fc_stat(const char *filename, struct stat *buf);
146
147 fc_errno fc_get_errno(void);
148const char *fc_strerror(fc_errno err);
149void fc_usleep(unsigned long usec);
150
151bool fc_strrep(char *str, size_t len, const char *search,
152 const char *replace);
153char *fc_strrep_resize(char *str, size_t *len, const char *search,
154 const char *replace)
155 fc__warn_unused_result;
156
157size_t fc_strlcpy(char *dest, const char *src, size_t n);
158size_t fc_strlcat(char *dest, const char *src, size_t n);
159
160/* convenience macros for use when dest is a char ARRAY: */
161#define sz_strlcpy(dest,src) ((void) fc_strlcpy((dest), (src), sizeof(dest)))
162#define sz_strlcat(dest,src) ((void) fc_strlcat((dest), (src), sizeof(dest)))
163
164 int fc_snprintf(char *str, size_t n, const char *format, ...)
165 fc__attribute((__format__ (__printf__, 3, 4)))
166 fc__attribute((nonnull (1, 3)));
167 int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap )
168 fc__attribute((nonnull (1, 3)));
169 int cat_snprintf(char *str, size_t n, const char *format, ...)
170 fc__attribute((__format__ (__printf__, 3, 4)))
171 fc__attribute((nonnull (1, 3)));
172
173int fc_gethostname(char *buf, size_t len);
174
175#ifdef FREECIV_SOCKET_ZERO_NOT_STDIN
176/* Support for console I/O in case FREECIV_SOCKET_ZERO_NOT_STDIN. */
177void fc_init_console(void);
178char *fc_read_console(void);
179#endif /* FREECIV_SOCKET_ZERO_NOT_STDIN */
180
181bool is_reg_file_for_access(const char *name, bool write_access);
182
183int fc_break_lines(char *str, size_t desired_len);
184
185bool fc_isalnum(char c);
186bool fc_isalpha(char c);
187bool fc_isdigit(char c);
188bool fc_isprint(char c);
189bool fc_isspace(char c);
190bool fc_isupper(char c);
191char fc_toupper(char c);
192char fc_tolower(char c);
193
194const char *fc_basename(const char *path);
195
196struct tm *fc_localtime(const time_t *timep, struct tm *result);
197
198/************************************************************************/
201 static inline bool is_bigendian(void)
202{
203#ifdef WORDS_BIGENDIAN
204 return TRUE;
205#else /* WORDS_BIGENDIAN */
206 return FALSE;
207#endif /* WORDS_BIGENDIAN */
208}
209
210void make_escapes(const char *str, char *buf, size_t buf_len);
211void remove_escapes(const char *str, bool full_escapes,
212 char *buf, size_t buf_len);
213
214int fc_at_quick_exit(void (*func)(void));
215
216#ifdef __cplusplus
217}
218#endif /* __cplusplus */
219
220#endif /* FC__SUPPORT_H */
str
#define str
Definition astring.c:76
n
#define n
Definition astring.c:77
name
const char * name
Definition inputfile.c:127
func
get_token_fn_t func
Definition inputfile.c:128
len
int len
Definition packhand.c:125
fc_isalpha
bool fc_isalpha(char c)
Definition support.c:1216
fc_gethostname
int fc_gethostname(char *buf, size_t len)
Definition support.c:1016
fc_isprint
bool fc_isprint(char c)
Definition support.c:1238
fc_strlcpy
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:787
make_escapes
void make_escapes(const char *str, char *buf, size_t buf_len)
Definition support.c:297
fc_strcasecmp
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
fc_usleep
void fc_usleep(unsigned long usec)
Definition support.c:640
is_bigendian
static bool is_bigendian(void)
Definition support.h:201
fc_support_init
void fc_support_init(void)
Definition support.c:1338
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:1249
fc_strerror
const char * fc_strerror(fc_errno err)
Definition support.c:610
fc_strlcat
size_t fc_strlcat(char *dest, const char *src, size_t n)
Definition support.c:832
fc_strcasestr
char * fc_strcasestr(const char *haystack, const char *needle)
Definition support.c:439
fc_strrep
bool fc_strrep(char *str, size_t len, const char *search, const char *replace)
Definition support.c:731
fc_localtime
struct tm * fc_localtime(const time_t *timep, struct tm *result)
Definition support.c:1310
fc_fopen
FILE * fc_fopen(const char *filename, const char *opentype)
Definition support.c:506
fc_break_lines
int fc_break_lines(char *str, size_t desired_len)
Definition support.c:1144
fc_get_errno
fc_errno fc_get_errno(void)
Definition support.c:593
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:1356
remove_escapes
void remove_escapes(const char *str, bool full_escapes, char *buf, size_t buf_len)
Definition support.c:333
fc_isdigit
bool fc_isdigit(char c)
Definition support.c:1227
fc_strcoll
int fc_strcoll(const char *str0, const char *str1)
Definition support.c:472
fc__attribute
#define fc__attribute(x)
Definition support.h:89
fc__warn_unused_result
#define fc__warn_unused_result
Definition support.h:99
fc_stat
int fc_stat(const char *filename, struct stat *buf)
Definition support.c:575
is_reg_file_for_access
bool is_reg_file_for_access(const char *name, bool write_access)
Definition support.c:1129
fc_isalnum
bool fc_isalnum(char c)
Definition support.c:1205
fc_isupper
bool fc_isupper(char c)
Definition support.c:1260
fc_basename
const char * fc_basename(const char *path)
Definition support.c:1296
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:379
effectivestrlenquote
size_t effectivestrlenquote(const char *str)
Definition support.c:359
fc_tolower
char fc_tolower(char c)
Definition support.c:1282
fc_at_quick_exit
int fc_at_quick_exit(void(*func)(void))
Definition support.c:1326
fc_stricoll
int fc_stricoll(const char *str0, const char *str1)
Definition support.c:486
fc_remove
int fc_remove(const char *filename)
Definition support.c:556
fc_strncasecmp
int fc_strncasecmp(const char *str0, const char *str1, size_t n)
Definition support.c:238
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:693
are_support_services_available
bool are_support_services_available(void)
Definition support.c:1378
fc_errno
int fc_errno
Definition support.h:115
fc_toupper
char fc_toupper(char c)
Definition support.c:1271