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
18 extern "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 <dirent.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>		/* size_t */
33 #include <sys/stat.h>
34 
35 #ifdef FREECIV_HAVE_SYS_TYPES_H
36 #include <sys/types.h>
37 #endif
38 
39 #ifdef TRUE
40 #undef TRUE
41 #endif
42 
43 #ifdef FALSE
44 #undef FALSE
45 #endif
46 
47 #define TRUE true
48 #define FALSE false
49 
50 #ifndef __cplusplus
51 #if __BEOS__
52 #include <posix/be_prim.h>
53 #define __bool_true_false_are_defined 1
54 #else
55 #ifdef FREECIV_HAVE_STDBOOL_H
56 #include <stdbool.h>
57 #else /* Implement <stdbool.h> ourselves */
58 #undef bool
59 #undef true
60 #undef false
61 #undef __bool_true_false_are_defined
62 #define bool unsigned int
63 #define true  1
64 #define false 0
65 #define __bool_true_false_are_defined 1
66 #endif /* ! FREECIV_HAVE_STDBOOL_H */
67 #endif /* ! __BEOS__ */
68 #endif /* __cplusplus */
69 
70 /* intptr_t header */
71 /* Prefer full inttypes.h if present. */
72 #ifdef FREECIV_HAVE_INTTYPES_H
73 #include <inttypes.h>
74 #else
75 #ifdef FREECIV_HAVE_STDINT_H
76 #include <stdint.h>
77 #endif /* FREECIV_HAVE_STDINT_H */
78 #endif /* FREECIV_HAVE_INTTYPES_H */
79 
80 /* Want to use GCC's __attribute__ keyword to check variadic
81  * parameters to printf-like functions, without upsetting other
82  * compilers: put any required defines magic here.
83  * If other compilers have something equivalent, could also
84  * work that out here.   Should this use configure stuff somehow?
85  * --dwp
86  */
87 #if defined(__GNUC__)
88 #define fc__attribute(x)  __attribute__(x)
89 #else
90 #define fc__attribute(x)
91 #endif
92 
93 /* __attribute__((warn_unused_result)) requires at least gcc 3.4 */
94 #if defined(__GNUC__)
95 #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
96 #define fc__warn_unused_result  __attribute__((warn_unused_result))
97 #endif
98 #endif
99 #ifndef fc__warn_unused_result
100 #define fc__warn_unused_result
101 #endif
102 
103 /* TODO: C++17 compilers (also other than g++) could use [[fallthrough]]
104    for C++ code */
105 #if defined(__GNUC__) && __GNUC__ >= 7
106 #define fc__fallthrough __attribute__((fallthrough))
107 #else
108 #define fc__fallthrough
109 #endif
110 
111 #ifdef FREECIV_MSWINDOWS
112 typedef long int fc_errno;
113 #else
114 typedef int fc_errno;
115 #endif
116 
117 #ifdef FREECIV_RETURN_VALUE_AFTER_EXIT
118 #define RETURN_VALUE_AFTER_EXIT(_val_) return _val_ ;
119 #else
120 #define RETURN_VALUE_AFTER_EXIT(_val_)
121 #endif
122 
123 int fc_strcasecmp(const char *str0, const char *str1);
124 int fc_strncasecmp(const char *str0, const char *str1, size_t n);
125 int fc_strncasequotecmp(const char *str0, const char *str1, size_t n);
126 
127 size_t effectivestrlenquote(const char *str);
128 
129 char *fc_strcasestr(const char *haystack, const char *needle);
130 
131 int fc_strcoll(const char *str0, const char *str1);
132 int fc_stricoll(const char *str0, const char *str1);
133 
134 FILE *fc_fopen(const char *filename, const char *opentype);
135 #ifdef FREECIV_HAVE_LIBZ
136 #include <zlib.h>
137 gzFile fc_gzopen(const char *filename, const char *opentype);
138 #endif
139 DIR *fc_opendir(const char *dir_to_open);
140 int fc_remove(const char *filename);
141 int fc_stat(const char *filename, struct stat *buf);
142 
143 fc_errno fc_get_errno(void);
144 const char *fc_strerror(fc_errno err);
145 void fc_usleep(unsigned long usec);
146 
147 bool fc_strrep(char *str, size_t len, const char *search,
148                const char *replace);
149 char *fc_strrep_resize(char *str, size_t *len, const char *search,
150                        const char *replace)
151                        fc__warn_unused_result;
152 
153 size_t fc_strlcpy(char *dest, const char *src, size_t n);
154 size_t fc_strlcat(char *dest, const char *src, size_t n);
155 
156 /* convenience macros for use when dest is a char ARRAY: */
157 #define sz_strlcpy(dest,src) ((void) fc_strlcpy((dest), (src), sizeof(dest)))
158 #define sz_strlcat(dest,src) ((void) fc_strlcat((dest), (src), sizeof(dest)))
159 
160 int fc_snprintf(char *str, size_t n, const char *format, ...)
161      fc__attribute((__format__ (__printf__, 3, 4)))
162      fc__attribute((nonnull (1, 3)));
163 int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap )
164      fc__attribute((nonnull (1, 3)));
165 int cat_snprintf(char *str, size_t n, const char *format, ...)
166      fc__attribute((__format__ (__printf__, 3, 4)))
167      fc__attribute((nonnull (1, 3)));
168 
169 int fc_gethostname(char *buf, size_t len);
170 
171 #ifdef FREECIV_SOCKET_ZERO_NOT_STDIN
172 /* Support for console I/O in case FREECIV_SOCKET_ZERO_NOT_STDIN. */
173 void fc_init_console(void);
174 char *fc_read_console(void);
175 #endif /* FREECIV_SOCKET_ZERO_NOT_STDIN */
176 
177 bool is_reg_file_for_access(const char *name, bool write_access);
178 
179 int fc_break_lines(char *str, size_t desired_len);
180 
181 bool fc_isalnum(char c);
182 bool fc_isalpha(char c);
183 bool fc_isdigit(char c);
184 bool fc_isprint(char c);
185 bool fc_isspace(char c);
186 bool fc_isupper(char c);
187 char fc_toupper(char c);
188 char fc_tolower(char c);
189 
190 const char *fc_basename(const char *path);
191 
192 void make_escapes(const char *str, char *buf, size_t buf_len);
193 void remove_escapes(const char *str, bool full_escapes,
194                     char *buf, size_t buf_len);
195 
196 int fc_at_quick_exit(void (*func)(void));
197 
198 #ifdef __cplusplus
199 }
200 #endif /* __cplusplus */
201 
202 #endif  /* FC__SUPPORT_H */
203