Freeciv21
Develop your civilization from humble roots to a global empire
support.h
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 #pragma once
14 
15 #include <cinttypes>
16 #include <cstdio>
17 
18 // Qt
19 #include <QString>
20 
21 /* Want to use GCC's __attribute__ keyword to check variadic
22  * parameters to printf-like functions, without upsetting other
23  * compilers: put any required defines magic here.
24  * If other compilers have something equivalent, could also
25  * work that out here. Should this use configure stuff somehow?
26  * --dwp
27  */
28 #if defined(__GNUC__)
29 #define fc__attribute(x) __attribute__(x)
30 #else
31 #define fc__attribute(x)
32 #endif
33 
34 // __attribute__((warn_unused_result)) requires at least gcc 3.4
35 #if defined(__GNUC__)
36 #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
37 #define fc__warn_unused_result __attribute__((warn_unused_result))
38 #endif
39 #endif
40 #ifndef fc__warn_unused_result
41 #define fc__warn_unused_result
42 #endif
43 
44 /* TODO: C++17 compilers (also other than g++) could use [[fallthrough]]
45  for C++ code */
46 #if defined(__GNUC__) && __GNUC__ >= 7
47 #define fc__fallthrough __attribute__((fallthrough))
48 #else
49 #define fc__fallthrough
50 #endif
51 
52 #ifdef FREECIV_MSWINDOWS
53 typedef long int fc_errno;
54 #else
55 typedef int fc_errno;
56 #endif
57 
58 #define fc_malloc(sz) malloc(sz)
59 #define fc_realloc(ptr, sz) realloc(ptr, sz)
60 
61 #define NFCPP_FREE(ptr) \
62  do { \
63  if (ptr) { \
64  delete[](ptr); \
65  } \
66  } while (false)
67 
68 #define NFC_FREE(ptr) \
69  do { \
70  if (ptr) { \
71  delete (ptr); \
72  } \
73  } while (false)
74 
75 #define NFCN_FREE(ptr) \
76  do { \
77  if (ptr) { \
78  delete (ptr); \
79  (ptr) = nullptr; \
80  } \
81  } while (false)
82 
83 #define VOIDNFCN_FREE(ptr) \
84  do { \
85  if (ptr) { \
86  ::operator delete(ptr); \
87  (ptr) = nullptr; \
88  } \
89  } while (false)
90 
91 #define NFCNPP_FREE(ptr) \
92  do { \
93  if (ptr) { \
94  delete[](ptr); \
95  (ptr) = nullptr; \
96  } \
97  } while (false)
98 
99 #define FCPP_FREE(ptr) \
100  do { \
101  delete[](ptr); \
102  (ptr) = NULL; \
103  } while (false)
104 
105 #define FC_FREE(ptr) \
106  do { \
107  delete (ptr); \
108  (ptr) = NULL; \
109  } while (false)
110 
111 #define fc_strdup(str) real_fc_strdup((str), "strdup", __FC_LINE__, __FILE__)
112 
113 char *real_fc_strdup(const char *str, const char *called_as, int line,
114  const char *file) fc__warn_unused_result;
115 
116 int fc_strcasecmp(const char *str0, const char *str1);
117 int fc_strncasecmp(const char *str0, const char *str1, size_t n);
118 int fc_strncasequotecmp(const char *str0, const char *str1, size_t n);
119 
120 size_t effectivestrlenquote(const char *str);
121 
122 int fc_strcoll(const char *str0, const char *str1);
123 int fc_stricoll(const char *str0, const char *str1);
124 
125 FILE *fc_fopen(const char *filename, const char *opentype);
126 int fc_remove(const char *filename);
127 int fc_stat(const char *filename, struct stat *buf);
128 
130 const char *fc_strerror(fc_errno err);
131 void fc_usleep(unsigned long usec);
132 
133 bool fc_strrep(char *str, size_t len, const char *search,
134  const char *replace);
135 
136 size_t fc_strlcpy(char *dest, const char *src, size_t n);
137 size_t fc_strlcat(char *dest, const char *src, size_t n);
138 
139 // convenience macros for use when dest is a char ARRAY:
140 #define sz_strlcpy(dest, src) \
141  ((void) fc_strlcpy((dest), (src), sizeof(dest)))
142 #define sz_strlcat(dest, src) \
143  ((void) fc_strlcat((dest), (src), sizeof(dest)))
144 
145 int fc_snprintf(char *str, size_t n, const char *format, ...)
146  fc__attribute((__format__(__printf__, 3, 4)))
147  fc__attribute((nonnull(1, 3)));
148 int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
149  fc__attribute((nonnull(1, 3)));
150 int cat_snprintf(char *str, size_t n, const char *format, ...)
151  fc__attribute((__format__(__printf__, 3, 4)))
152  fc__attribute((nonnull(1, 3)));
153 
154 int fc_gethostname(char *buf, size_t len);
155 
156 int fc_break_lines(char *str, size_t desired_len);
157 
158 void make_escapes(const char *str, char *buf, size_t buf_len);
159 QString remove_escapes(const QString &str, bool full_escapes);
160 
161 int fc_at_quick_exit(void (*func)());
get_token_fn_t func
Definition: inputfile.cpp:119
int len
Definition: packhand.cpp:127
int fc_gethostname(char *buf, size_t len)
Call gethostname() if supported, else just returns -1.
Definition: support.cpp:586
size_t fc_strlcpy(char *dest, const char *src, size_t n)
fc_strlcpy() provides utf-8 version of (non-standard) function strlcpy() It is intended as more user-...
Definition: support.cpp:412
void make_escapes(const char *str, char *buf, size_t buf_len)
Copies a string and convert the following characters:
Definition: support.cpp:114
char * real_fc_strdup(const char *str, const char *called_as, int line, const char *file) fc__warn_unused_result
Function used by fc_strdup macro, strdup() replacement No need to check return value.
Definition: support.cpp:75
int fc_strcasecmp(const char *str0, const char *str1)
Compare strings like strcmp(), but ignoring case.
Definition: support.cpp:89
void fc_usleep(unsigned long usec)
Suspend execution for the specified number of microseconds.
Definition: support.cpp:349
int fc_snprintf(char *str, size_t n, const char *format,...) fc__attribute((__format__(__printf__
size_t fc_strlcat(char *dest, const char *src, size_t n)
fc_strlcat() provides utf-8 version of (non-standard) function strlcat() It is intended as more user-...
Definition: support.cpp:448
bool fc_strrep(char *str, size_t len, const char *search, const char *replace)
Replace 'search' by 'replace' within 'str'.
Definition: support.cpp:356
const char * fc_strerror(fc_errno err)
Return a string which describes a given error (errno-style.) The string is converted as necessary fro...
Definition: support.cpp:328
QString remove_escapes(const QString &str, bool full_escapes)
Copies a string.
Definition: support.cpp:149
int fc_break_lines(char *str, size_t desired_len)
Replace the spaces by line breaks when the line lenght is over the desired one.
Definition: support.cpp:597
int int cat_snprintf(char *str, size_t n, const char *format,...) fc__attribute((__format__(__printf__
int fc_strcoll(const char *str0, const char *str1)
Wrapper function for strcoll().
Definition: support.cpp:227
#define fc__attribute(x)
Definition: support.h:31
#define fc__warn_unused_result
Definition: support.h:41
int fc_stat(const char *filename, struct stat *buf)
Wrapper function for stat() with filename conversion to local encoding on Windows.
Definition: support.cpp:293
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap) fc__attribute((nonnull(1
int fc_at_quick_exit(void(*func)())
Set quick_exit() callback if possible.
Definition: support.cpp:657
int fc_strncasequotecmp(const char *str0, const char *str1, size_t n)
Compare strings like strncasecmp() but ignoring surrounding quotes in either string.
Definition: support.cpp:209
FILE * fc_fopen(const char *filename, const char *opentype)
Wrapper function for fopen() with filename conversion to local encoding on Windows.
Definition: support.cpp:255
size_t effectivestrlenquote(const char *str)
Count length of string without possible surrounding quotes.
Definition: support.cpp:189
int fc_stricoll(const char *str0, const char *str1)
Wrapper function for stricoll().
Definition: support.cpp:235
int fc_remove(const char *filename)
Wrapper function for remove() with filename conversion to local encoding on Windows.
Definition: support.cpp:274
int fc_strncasecmp(const char *str0, const char *str1, size_t n)
Compare strings like strncmp(), but ignoring case.
Definition: support.cpp:100
fc_errno fc_get_errno()
Returns last error code.
Definition: support.cpp:311
int fc_errno
Definition: support.h:55