Freeciv-3.3
Loading...
Searching...
No Matches
shared.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#ifndef FC__SHARED_H
14#define FC__SHARED_H
15
16#ifdef __cplusplus
17extern "C" {
18#endif /* __cplusplus */
19
20#include <freeciv_config.h>
21
22#include <stdlib.h> /* size_t */
23#include <string.h> /* memset */
24#include <time.h> /* time_t */
25
26/* utility */
27#include "log.h"
28#include "support.h" /* bool, fc__attribute */
29
30/* Changing these will break network compatibility! */
31#define MAX_LEN_ADDR 256 /* See also MAXHOSTNAMELEN and RFC 1123 2.1 */
32#define MAX_LEN_PATH 4095
33
34/* Use FC_INFINITY to denote that a certain event will never occur or
35 another unreachable condition. */
36#define FC_INFINITY (1000 * 1000 * 1000)
37
38#ifndef FREECIV_TESTMATIC
39/* Initialize something for the sole purpose of silencing false compiler warning
40 * about variable possibly used uninitialized. */
41#define BAD_HEURISTIC_INIT(_ini_val_) = _ini_val_
42#else /* FREECIV_TESTMATIC */
43#define BAD_HEURISTIC_INIT(_ini_val_)
44#endif /* FREECIV_TESTMATIC */
45
47#define BOOL_TO_TRISTATE(tri) ((tri) ? TRI_YES : TRI_NO)
48
50 enum fc_tristate two);
52
53#ifndef MAX
54#define MAX(x,y) (((x)>(y))?(x):(y))
55#define MIN(x,y) (((x)<(y))?(x):(y))
56#endif
57#define CLIP(lower,current,upper) \
58 ((current)<(lower)?(lower):(current)>(upper)?(upper):(current))
59
60#ifndef ABS
61#define ABS(x) (((x) >= 0) ? (x) : -(x))
62#endif
63
64/* Note: Solaris already has a WRAP macro that is completely different. */
65#define FC_WRAP(value, range) \
66 ((value) < 0 \
67 ? ((value) % (range) != 0 ? (value) % (range) + (range) : 0) \
68 : ((value) >= (range) ? (value) % (range) : (value)))
69
70#define BOOL_VAL(x) ((x) != 0)
71#define XOR(p, q) (BOOL_VAL(p) != BOOL_VAL(q))
72#define EQ(p, q) (BOOL_VAL(p) == BOOL_VAL(q))
73
74/*
75 * DIVIDE() divides and rounds down, rather than just divides and
76 * rounds toward 0. It is assumed that the divisor is positive.
77 */
78#define DIVIDE(n, d) \
79 ( (n) / (d) - (( (n) < 0 && (n) % (d) < 0 ) ? 1 : 0) )
80
81#define MAX_UINT32 0xFFFFFFFF
82#define MAX_UINT16 0xFFFF
83#define MAX_UINT8 0xFF
84
85#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
86#define ADD_TO_POINTER(p, n) ((void *)((char *)(p)+(n)))
87
88#define FC_MEMBER(type, member) (((type *) nullptr)->member)
89#define FC_MEMBER_OFFSETOF(type, member) ((size_t) &FC_MEMBER(type, member))
90#define FC_MEMBER_SIZEOF(type, member) sizeof(FC_MEMBER(type, member))
91#define FC_MEMBER_ARRAY_SIZE(type, member) \
92 ARRAY_SIZE(FC_MEMBER(type, member))
93
94#define FC_INT_TO_PTR(i) ((void *) (intptr_t) (i))
95#define FC_PTR_TO_INT(p) ((int) (intptr_t) (p))
96
97/****************************************************************************
98 Used to initialize an array 'a' of size 'size' with value 'val' in each
99 element. Note that the value is evaluated for each element.
100****************************************************************************/
101#define INITIALIZE_ARRAY(array, size, value) \
102 { \
103 int _ini_index; \
104 \
105 for (_ini_index = 0; _ini_index < (size); _ini_index++) { \
106 (array)[_ini_index] = (value); \
107 } \
108 }
109
110#ifndef PATH_SEPARATOR
111#if defined(FREECIV_MSWINDOWS) || defined(_WIN32) || defined(__WIN32__) || defined(__EMX__) || defined(__DJGPP__)
112 /* Windows, OS/2, DOS */
113# define PATH_SEPARATOR ";"
114#else
115 /* Unix */
116# define PATH_SEPARATOR ":"
117#endif
118#endif /* PATH_SEPARATOR */
119
120#if defined(FREECIV_MSWINDOWS) || defined(_WIN32) || defined(__WIN32__) || defined(__EMX__) || defined(__DJGPP__)
121 /* Windows, OS/2, DOS */
122# define DIR_SEPARATOR "\\"
123# define DIR_SEPARATOR_CHAR '\\'
124#else
125 /* Unix */
126# define DIR_SEPARATOR_IS_DEFAULT
127# define DIR_SEPARATOR "/"
128# define DIR_SEPARATOR_CHAR '/'
129#endif
130
131#define PARENT_DIR_OPERATOR ".."
132
133const char *big_int_to_text(unsigned int mantissa, unsigned int exponent);
134const char *int_to_text(unsigned int number);
135
136bool is_ascii_name(const char *name);
137bool is_base64url(const char *s);
138bool is_safe_filename(const char *name);
139void randomize_base64url_string(char *s, size_t n);
140
141int compare_strings(const void *first, const void *second);
142int compare_strings_ptrs(const void *first, const void *second);
143int compare_strings_strvec(const char *const *first,
144 const char *const *second);
145
146char *skip_leading_spaces(char *s)
147 fc__attribute((nonnull (1)));
148void remove_leading_spaces(char *s)
149 fc__attribute((nonnull (1)));
150void remove_trailing_spaces(char *s)
151 fc__attribute((nonnull (1)));
153
154bool check_strlen(const char *str, size_t len, const char *errmsg);
155size_t loud_strlcpy(char *buffer, const char *str, size_t len,
156 const char *errmsg);
157/* Convenience macro. */
158#define sz_loud_strlcpy(buffer, str, errmsg) \
159 loud_strlcpy(buffer, str, sizeof(buffer), errmsg)
160
161char *end_of_strn(char *str, int *nleft);
162
163bool str_to_int(const char *str, int *pint)
164 fc__attribute((nonnull (1)));
165bool str_to_uint(const char *str, unsigned int *pint)
166 fc__attribute((nonnull (1)));
167bool str_to_float(const char *str, float *pfloat)
168 fc__attribute((nonnull (1)));
169
170/**************************************************************************
171 ...
172**************************************************************************/
173struct fileinfo {
174 char *name; /* Descriptive file name string */
175 char *fullname; /* Full absolute filename */
176 time_t mtime; /* Last modification time */
177};
178
179#define SPECLIST_TAG fileinfo
180#define SPECLIST_TYPE struct fileinfo
181#include "speclist.h"
182#define fileinfo_list_iterate(list, pnode) \
183 TYPED_LIST_ITERATE(struct fileinfo, list, pnode)
184#define fileinfo_list_iterate_end LIST_ITERATE_END
185
186char *user_home_dir(void);
187void free_user_home_dir(void);
188char *user_username(char *buf, size_t bufsz);
189char *freeciv_storage_dir(void);
190void free_freeciv_storage_dir(void);
191
192const struct strvec *get_data_dirs(void);
193const struct strvec *get_save_dirs(void);
194const struct strvec *get_scenario_dirs(void);
195
196void free_data_dir_names(void);
197
198struct strvec *fileinfolist(const struct strvec *dirs, const char *suffix);
199struct fileinfo_list *fileinfolist_infix(const struct strvec *dirs,
200 const char *infix, bool nodups);
201const char *fileinfoname(const struct strvec *dirs, const char *filename);
202void free_fileinfo_data(void);
203
204void init_nls(void);
205void free_nls(void);
206const char *setup_langname(void);
207void switch_lang(const char *lang);
208
209void dont_run_as_root(const char *argv0, const char *fallback);
210
211/*** Matching prefixes: ***/
212
214 M_PRE_EXACT, /* Matches with exact length */
215 M_PRE_ONLY, /* Only matching prefix */
216 M_PRE_AMBIGUOUS, /* First of multiple matching prefixes */
217 M_PRE_EMPTY, /* Prefix is empty string (no match) */
218 M_PRE_LONG, /* Prefix is too long (no match) */
219 M_PRE_FAIL, /* No match at all */
220 M_PRE_LAST /* Flag value */
222
223const char *m_pre_description(enum m_pre_result result);
224
225/* Function type to access a name from an index: */
226typedef const char *(*m_pre_accessor_fn_t)(int);
227
228/* Function type to compare prefix: */
229typedef int (*m_pre_strncmp_fn_t)(const char *, const char *, size_t n);
230
231/* Function type to calculate effective string length: */
232typedef size_t (m_strlen_fn_t)(const char *str);
233
235 size_t n_names,
236 size_t max_len_name,
239 const char *prefix,
240 int *ind_result);
242 size_t n_names,
243 size_t max_len_name,
246 const char *prefix,
247 int *ind_result,
248 int *matches,
249 int max_matches,
250 int *pnum_matches);
251
253void free_multicast_group(void);
254void interpret_tilde(char *buf, size_t buf_size, const char *filename);
255char *interpret_tilde_alloc(const char *filename);
256char *skip_to_basename(char *filepath);
257
258#define DIRMODE_DEFAULT (-1)
259
260bool make_dir(const char *pathname, int mode)
261 fc__attribute((nonnull (1)));
262bool make_dir_for_file(char *filename)
263 fc__attribute((nonnull (1)));
264
265bool path_is_absolute(const char *filename);
266
267char scanin(const char **buf, char *delimiters, char *dest, int size);
268
269void array_shuffle(int *array, int n);
270
271void format_time_duration(time_t t, char *buf, int maxlen);
272
273bool wildcard_fit_string(const char *pattern, const char *test);
274
275/* Custom format strings. */
276struct cf_sequence;
277
278int fc_snprintcf(char *buf, size_t buf_len, const char *format, ...)
279 fc__attribute((nonnull (1, 3))); /* Not a printf format. */
280int fc_vsnprintcf(char *buf, size_t buf_len, const char *format,
282 fc__attribute((nonnull (1, 3, 4)));
283
284/* Tools for fc_snprintcf(). */
288static inline void cf_int_seq(char letter, int value, struct cf_sequence *out);
294
307
310 char letter;
311 union {
316 const void *ptr_value;
317 const char *str_value;
318 };
319};
320
321/************************************************************************/
324static inline struct cf_sequence cf_bool_seq(char letter, bool value)
325{
326 struct cf_sequence sequence;
327
329 sequence.letter = letter;
330 sequence.bool_value = value;
331
332 return sequence;
333}
334
335/************************************************************************/
339static inline struct cf_sequence cf_trans_bool_seq(char letter, bool value)
340{
341 struct cf_sequence sequence;
342
344 sequence.letter = letter;
345 sequence.bool_value = value;
346
347 return sequence;
348}
349
350/************************************************************************/
353static inline struct cf_sequence cf_char_seq(char letter, char value)
354{
355 struct cf_sequence sequence;
356
358 sequence.letter = letter;
359 sequence.char_value = value;
360
361 return sequence;
362}
363
364/************************************************************************/
367static inline void cf_int_seq(char letter, int value, struct cf_sequence *out)
368{
369 out->type = CF_INTEGER;
370 out->letter = letter;
371 out->int_value = value;
372}
373
374/************************************************************************/
377static inline struct cf_sequence cf_hexa_seq(char letter, int value)
378{
379 struct cf_sequence sequence;
380
382 sequence.letter = letter;
383 sequence.int_value = value;
384
385 return sequence;
386}
387
388/************************************************************************/
391static inline struct cf_sequence cf_float_seq(char letter, float value)
392{
393 struct cf_sequence sequence;
394
396 sequence.letter = letter;
397 sequence.float_value = value;
398
399 return sequence;
400}
401
402/************************************************************************/
405static inline struct cf_sequence cf_ptr_seq(char letter, const void *value)
406{
407 struct cf_sequence sequence;
408
410 sequence.letter = letter;
411 sequence.ptr_value = value;
412
413 return sequence;
414}
415
416/************************************************************************/
419static inline struct cf_sequence cf_str_seq(char letter, const char *value)
420{
421 struct cf_sequence sequence;
422
424 sequence.letter = letter;
425 sequence.str_value = value;
426
427 return sequence;
428}
429
430/************************************************************************/
433static inline struct cf_sequence cf_end(void)
434{
435 struct cf_sequence sequence;
436
438
439 return sequence;
440}
441
442bool formats_match(const char *format1, const char *format2);
443
444#ifdef __cplusplus
445}
446#endif /* __cplusplus */
447
448#endif /* FC__SHARED_H */
#define str
Definition astring.c:76
#define n
Definition astring.c:77
const size_t buf_size
Definition audio_sdl.c:53
char * incite_cost
Definition comments.c:76
static const int bufsz
Definition helpdlg.c:70
const char * name
Definition inputfile.c:127
int len
Definition packhand.c:127
int compare_strings(const void *first, const void *second)
Definition shared.c:363
fc_tristate
Definition shared.h:46
@ TRI_YES
Definition shared.h:46
@ TRI_NO
Definition shared.h:46
@ TRI_MAYBE
Definition shared.h:46
void format_time_duration(time_t t, char *buf, int maxlen)
Definition shared.c:1968
int(* m_pre_strncmp_fn_t)(const char *, const char *, size_t n)
Definition shared.h:229
int compare_strings_ptrs(const void *first, const void *second)
Definition shared.c:373
void remove_leading_spaces(char *s) fc__attribute((nonnull(1)))
Definition shared.c:405
char * user_username(char *buf, size_t bufsz)
Definition shared.c:704
int int fc_vsnprintcf(char *buf, size_t buf_len, const char *format, const struct cf_sequence *sequences, size_t sequences_num) fc__attribute((nonnull(1
int int static struct cf_sequence cf_bool_seq(char letter, bool value)
Definition shared.h:324
enum fc_tristate fc_tristate_and(enum fc_tristate one, enum fc_tristate two)
Definition shared.c:127
size_t() m_strlen_fn_t(const char *str)
Definition shared.h:232
static struct cf_sequence cf_char_seq(char letter, char value)
Definition shared.h:353
void dont_run_as_root(const char *argv0, const char *fallback)
Definition shared.c:1542
bool make_dir(const char *pathname, int mode) fc__attribute((nonnull(1)))
Definition shared.c:1779
bool wildcard_fit_string(const char *pattern, const char *test)
Definition shared.c:2156
bool check_strlen(const char *str, size_t len, const char *errmsg)
Definition shared.c:493
const char * fileinfoname(const struct strvec *dirs, const char *filename)
Definition shared.c:1094
void remove_trailing_spaces(char *s) fc__attribute((nonnull(1)))
Definition shared.c:422
char * user_home_dir(void)
Definition shared.c:627
void free_user_home_dir(void)
Definition shared.c:658
static struct cf_sequence cf_end(void)
Definition shared.h:433
static struct cf_sequence cf_ptr_seq(char letter, const void *value)
Definition shared.h:405
void init_nls(void)
Definition shared.c:1463
const char * m_pre_description(enum m_pre_result result)
Definition shared.c:1564
static void cf_int_seq(char letter, int value, struct cf_sequence *out)
Definition shared.h:367
static struct cf_sequence cf_float_seq(char letter, float value)
Definition shared.h:391
static struct cf_sequence cf_str_seq(char letter, const char *value)
Definition shared.h:419
static struct cf_sequence cf_trans_bool_seq(char letter, bool value)
Definition shared.h:339
char * skip_leading_spaces(char *s) fc__attribute((nonnull(1)))
Definition shared.c:392
struct strvec * fileinfolist(const struct strvec *dirs, const char *suffix)
Definition shared.c:1020
void free_data_dir_names(void)
Definition shared.c:859
enum m_pre_result match_prefix_full(m_pre_accessor_fn_t accessor_fn, size_t n_names, size_t max_len_name, m_pre_strncmp_fn_t cmp_fn, m_strlen_fn_t len_fn, const char *prefix, int *ind_result, int *matches, int max_matches, int *pnum_matches)
Definition shared.c:1606
const struct strvec * get_scenario_dirs(void)
Definition shared.c:971
bool str_to_uint(const char *str, unsigned int *pint) fc__attribute((nonnull(1)))
Definition shared.c:547
cf_type
Definition shared.h:295
@ CF_POINTER
Definition shared.h:302
@ CF_HEXA
Definition shared.h:300
@ CF_CHARACTER
Definition shared.h:298
@ CF_INTEGER
Definition shared.h:299
@ CF_TRANS_BOOLEAN
Definition shared.h:297
@ CF_LAST
Definition shared.h:305
@ CF_BOOLEAN
Definition shared.h:296
@ CF_STRING
Definition shared.h:303
@ CF_FLOAT
Definition shared.h:301
size_t loud_strlcpy(char *buffer, const char *str, size_t len, const char *errmsg)
Definition shared.c:503
char * interpret_tilde_alloc(const char *filename)
Definition shared.c:1730
static struct cf_sequence cf_hexa_seq(char letter, int value)
Definition shared.h:377
void interpret_tilde(char *buf, size_t buf_size, const char *filename)
Definition shared.c:1713
int compare_strings_strvec(const char *const *first, const char *const *second)
Definition shared.c:383
bool is_base64url(const char *s)
Definition shared.c:321
void free_multicast_group(void)
Definition shared.c:1698
void free_fileinfo_data(void)
Definition shared.c:1151
char * end_of_strn(char *str, int *nleft)
Definition shared.c:478
bool path_is_absolute(const char *filename)
Definition shared.c:1884
void free_nls(void)
Definition shared.c:1525
bool make_dir_for_file(char *filename) fc__attribute((nonnull(1)))
Definition shared.c:1862
bool str_to_int(const char *str, int *pint) fc__attribute((nonnull(1)))
Definition shared.c:515
bool formats_match(const char *format1, const char *format2)
Definition shared.c:2446
const char * int_to_text(unsigned int number)
Definition shared.c:237
char scanin(const char **buf, char *delimiters, char *dest, int size)
Definition shared.c:1923
const struct strvec * get_save_dirs(void)
Definition shared.c:934
char * skip_to_basename(char *filepath)
Definition shared.c:1753
bool str_to_float(const char *str, float *pfloat) fc__attribute((nonnull(1)))
Definition shared.c:579
void array_shuffle(int *array, int n)
Definition shared.c:2011
m_pre_result
Definition shared.h:213
@ M_PRE_EXACT
Definition shared.h:214
@ M_PRE_ONLY
Definition shared.h:215
@ M_PRE_LAST
Definition shared.h:220
@ M_PRE_LONG
Definition shared.h:218
@ M_PRE_AMBIGUOUS
Definition shared.h:216
@ M_PRE_EMPTY
Definition shared.h:217
@ M_PRE_FAIL
Definition shared.h:219
enum m_pre_result match_prefix(m_pre_accessor_fn_t accessor_fn, size_t n_names, size_t max_len_name, m_pre_strncmp_fn_t cmp_fn, m_strlen_fn_t len_fn, const char *prefix, int *ind_result)
Definition shared.c:1583
bool is_ascii_name(const char *name)
Definition shared.c:286
enum fc_tristate fc_tristate_or(enum fc_tristate one, enum fc_tristate two)
Definition shared.c:143
int fc_snprintcf(char *buf, size_t buf_len, const char *format,...) fc__attribute((nonnull(1
const char * setup_langname(void)
Definition shared.c:1281
void randomize_base64url_string(char *s, size_t n)
Definition shared.c:343
char * get_multicast_group(bool ipv6_preferred)
Definition shared.c:1667
void free_freeciv_storage_dir(void)
Definition shared.c:689
const char *(* m_pre_accessor_fn_t)(int)
Definition shared.h:226
bool is_safe_filename(const char *name)
Definition shared.c:256
void remove_leading_trailing_spaces(char *s)
Definition shared.c:444
const char * big_int_to_text(unsigned int mantissa, unsigned int exponent)
Definition shared.c:162
const struct strvec * get_data_dirs(void)
Definition shared.c:886
char * freeciv_storage_dir(void)
Definition shared.c:671
void switch_lang(const char *lang)
Definition shared.c:1434
struct fileinfo_list * fileinfolist_infix(const struct strvec *dirs, const char *infix, bool nodups)
Definition shared.c:1204
size_t size
Definition specvec.h:72
const void * ptr_value
Definition shared.h:316
const char * str_value
Definition shared.h:317
int int_value
Definition shared.h:314
bool bool_value
Definition shared.h:312
char letter
Definition shared.h:310
float float_value
Definition shared.h:315
char char_value
Definition shared.h:313
enum cf_type type
Definition shared.h:309
time_t mtime
Definition shared.h:176
char * fullname
Definition shared.h:175
char * name
Definition shared.h:174
#define fc__attribute(x)
Definition support.h:99
#define bool
Definition support.h:71