Freeciv-3.2
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 *) NULL)->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);
147void remove_leading_spaces(char *s);
148void remove_trailing_spaces(char *s);
150
151bool check_strlen(const char *str, size_t len, const char *errmsg);
152size_t loud_strlcpy(char *buffer, const char *str, size_t len,
153 const char *errmsg);
154/* Convenience macro. */
155#define sz_loud_strlcpy(buffer, str, errmsg) \
156 loud_strlcpy(buffer, str, sizeof(buffer), errmsg)
157
158char *end_of_strn(char *str, int *nleft);
159
160bool str_to_int(const char *str, int *pint);
161bool str_to_uint(const char *str, unsigned int *pint);
162bool str_to_float(const char *str, float *pfloat);
163
164/**************************************************************************
165...
166**************************************************************************/
167struct fileinfo {
168 char *name; /* Descriptive file name string */
169 char *fullname; /* Full absolute filename */
170 time_t mtime; /* Last modification time */
171};
172
173#define SPECLIST_TAG fileinfo
174#define SPECLIST_TYPE struct fileinfo
175#include "speclist.h"
176#define fileinfo_list_iterate(list, pnode) \
177 TYPED_LIST_ITERATE(struct fileinfo, list, pnode)
178#define fileinfo_list_iterate_end LIST_ITERATE_END
179
180char *user_home_dir(void);
181void free_user_home_dir(void);
182char *user_username(char *buf, size_t bufsz);
183char *freeciv_storage_dir(void);
184void free_freeciv_storage_dir(void);
185
186const struct strvec *get_data_dirs(void);
187const struct strvec *get_save_dirs(void);
188const struct strvec *get_scenario_dirs(void);
189
190void free_data_dir_names(void);
191
192struct strvec *fileinfolist(const struct strvec *dirs, const char *suffix);
193struct fileinfo_list *fileinfolist_infix(const struct strvec *dirs,
194 const char *infix, bool nodups);
195const char *fileinfoname(const struct strvec *dirs, const char *filename);
196void free_fileinfo_data(void);
197
198void init_nls(void);
199void free_nls(void);
200const char *setup_langname(void);
201void switch_lang(const char *lang);
202
203void dont_run_as_root(const char *argv0, const char *fallback);
204
205/*** Matching prefixes: ***/
206
208 M_PRE_EXACT, /* Matches with exact length */
209 M_PRE_ONLY, /* Only matching prefix */
210 M_PRE_AMBIGUOUS, /* First of multiple matching prefixes */
211 M_PRE_EMPTY, /* Prefix is empty string (no match) */
212 M_PRE_LONG, /* Prefix is too long (no match) */
213 M_PRE_FAIL, /* No match at all */
214 M_PRE_LAST /* Flag value */
216
217const char *m_pre_description(enum m_pre_result result);
218
219/* Function type to access a name from an index: */
220typedef const char *(*m_pre_accessor_fn_t)(int);
221
222/* Function type to compare prefix: */
223typedef int (*m_pre_strncmp_fn_t)(const char *, const char *, size_t n);
224
225/* Function type to calculate effective string length: */
226typedef size_t (m_strlen_fn_t)(const char *str);
227
229 size_t n_names,
230 size_t max_len_name,
233 const char *prefix,
234 int *ind_result);
236 size_t n_names,
237 size_t max_len_name,
240 const char *prefix,
241 int *ind_result,
242 int *matches,
243 int max_matches,
244 int *pnum_matches);
245
247void free_multicast_group(void);
248void interpret_tilde(char *buf, size_t buf_size, const char *filename);
249char *interpret_tilde_alloc(const char *filename);
250char *skip_to_basename(char *filepath);
251
252bool make_dir(const char *pathname)
253 fc__attribute((nonnull (1)));
254bool make_dir_for_file(char *filename)
255 fc__attribute((nonnull (1)));
256
257bool path_is_absolute(const char *filename);
258
259char scanin(const char **buf, char *delimiters, char *dest, int size);
260
261void array_shuffle(int *array, int n);
262
263void format_time_duration(time_t t, char *buf, int maxlen);
264
265bool wildcard_fit_string(const char *pattern, const char *test);
266
267/* Custom format strings. */
268struct cf_sequence;
269
270int fc_snprintcf(char *buf, size_t buf_len, const char *format, ...)
271 fc__attribute((nonnull (1, 3))); /* Not a printf format. */
272int fc_vsnprintcf(char *buf, size_t buf_len, const char *format,
274 fc__attribute((nonnull (1, 3, 4)));
275
276/* Tools for fc_snprintcf(). */
280static inline void cf_int_seq(char letter, int value, struct cf_sequence *out);
286
299
302 char letter;
303 union {
308 const void *ptr_value;
309 const char *str_value;
310 };
311};
312
313/************************************************************************/
316static inline struct cf_sequence cf_bool_seq(char letter, bool value)
317{
318 struct cf_sequence sequence;
319
321 sequence.letter = letter;
322 sequence.bool_value = value;
323
324 return sequence;
325}
326
327/************************************************************************/
331static inline struct cf_sequence cf_trans_bool_seq(char letter, bool value)
332{
333 struct cf_sequence sequence;
334
336 sequence.letter = letter;
337 sequence.bool_value = value;
338
339 return sequence;
340}
341
342/************************************************************************/
345static inline struct cf_sequence cf_char_seq(char letter, char value)
346{
347 struct cf_sequence sequence;
348
350 sequence.letter = letter;
351 sequence.char_value = value;
352
353 return sequence;
354}
355
356/************************************************************************/
359static inline void cf_int_seq(char letter, int value, struct cf_sequence *out)
360{
361 out->type = CF_INTEGER;
362 out->letter = letter;
363 out->int_value = value;
364}
365
366/************************************************************************/
369static inline struct cf_sequence cf_hexa_seq(char letter, int value)
370{
371 struct cf_sequence sequence;
372
374 sequence.letter = letter;
375 sequence.int_value = value;
376
377 return sequence;
378}
379
380/************************************************************************/
383static inline struct cf_sequence cf_float_seq(char letter, float value)
384{
385 struct cf_sequence sequence;
386
388 sequence.letter = letter;
389 sequence.float_value = value;
390
391 return sequence;
392}
393
394/************************************************************************/
397static inline struct cf_sequence cf_ptr_seq(char letter, const void *value)
398{
399 struct cf_sequence sequence;
400
402 sequence.letter = letter;
403 sequence.ptr_value = value;
404
405 return sequence;
406}
407
408/************************************************************************/
411static inline struct cf_sequence cf_str_seq(char letter, const char *value)
412{
413 struct cf_sequence sequence;
414
416 sequence.letter = letter;
417 sequence.str_value = value;
418
419 return sequence;
420}
421
422/************************************************************************/
425static inline struct cf_sequence cf_end(void)
426{
427 struct cf_sequence sequence;
428
430
431 return sequence;
432}
433
434bool formats_match(const char *format1, const char *format2);
435
436#ifdef __cplusplus
437}
438#endif /* __cplusplus */
439
440#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:75
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:359
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:1967
int(* m_pre_strncmp_fn_t)(const char *, const char *, size_t n)
Definition shared.h:223
bool make_dir(const char *pathname) fc__attribute((nonnull(1)))
Definition shared.c:1782
int compare_strings_ptrs(const void *first, const void *second)
Definition shared.c:369
char * user_username(char *buf, size_t bufsz)
Definition shared.c:711
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:316
enum fc_tristate fc_tristate_and(enum fc_tristate one, enum fc_tristate two)
Definition shared.c:124
size_t() m_strlen_fn_t(const char *str)
Definition shared.h:226
static struct cf_sequence cf_char_seq(char letter, char value)
Definition shared.h:345
void dont_run_as_root(const char *argv0, const char *fallback)
Definition shared.c:1549
bool wildcard_fit_string(const char *pattern, const char *test)
Definition shared.c:2155
bool check_strlen(const char *str, size_t len, const char *errmsg)
Definition shared.c:495
const char * fileinfoname(const struct strvec *dirs, const char *filename)
Definition shared.c:1101
char * user_home_dir(void)
Definition shared.c:634
void remove_trailing_spaces(char *s)
Definition shared.c:421
void free_user_home_dir(void)
Definition shared.c:665
static struct cf_sequence cf_end(void)
Definition shared.h:425
static struct cf_sequence cf_ptr_seq(char letter, const void *value)
Definition shared.h:397
void init_nls(void)
Definition shared.c:1470
bool str_to_int(const char *str, int *pint)
Definition shared.c:517
bool str_to_float(const char *str, float *pfloat)
Definition shared.c:584
const char * m_pre_description(enum m_pre_result result)
Definition shared.c:1571
static void cf_int_seq(char letter, int value, struct cf_sequence *out)
Definition shared.h:359
static struct cf_sequence cf_float_seq(char letter, float value)
Definition shared.h:383
static struct cf_sequence cf_str_seq(char letter, const char *value)
Definition shared.h:411
static struct cf_sequence cf_trans_bool_seq(char letter, bool value)
Definition shared.h:331
struct strvec * fileinfolist(const struct strvec *dirs, const char *suffix)
Definition shared.c:1027
char * skip_leading_spaces(char *s)
Definition shared.c:388
bool str_to_uint(const char *str, unsigned int *pint)
Definition shared.c:551
void free_data_dir_names(void)
Definition shared.c:866
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:1613
const struct strvec * get_scenario_dirs(void)
Definition shared.c:978
cf_type
Definition shared.h:287
@ CF_POINTER
Definition shared.h:294
@ CF_HEXA
Definition shared.h:292
@ CF_CHARACTER
Definition shared.h:290
@ CF_INTEGER
Definition shared.h:291
@ CF_TRANS_BOOLEAN
Definition shared.h:289
@ CF_LAST
Definition shared.h:297
@ CF_BOOLEAN
Definition shared.h:288
@ CF_STRING
Definition shared.h:295
@ CF_FLOAT
Definition shared.h:293
size_t loud_strlcpy(char *buffer, const char *str, size_t len, const char *errmsg)
Definition shared.c:505
char * interpret_tilde_alloc(const char *filename)
Definition shared.c:1737
static struct cf_sequence cf_hexa_seq(char letter, int value)
Definition shared.h:369
void interpret_tilde(char *buf, size_t buf_size, const char *filename)
Definition shared.c:1720
int compare_strings_strvec(const char *const *first, const char *const *second)
Definition shared.c:379
bool is_base64url(const char *s)
Definition shared.c:318
void free_multicast_group(void)
Definition shared.c:1705
void free_fileinfo_data(void)
Definition shared.c:1158
char * end_of_strn(char *str, int *nleft)
Definition shared.c:480
bool path_is_absolute(const char *filename)
Definition shared.c:1883
void free_nls(void)
Definition shared.c:1532
bool make_dir_for_file(char *filename) fc__attribute((nonnull(1)))
Definition shared.c:1861
bool formats_match(const char *format1, const char *format2)
Definition shared.c:2445
const char * int_to_text(unsigned int number)
Definition shared.c:234
char scanin(const char **buf, char *delimiters, char *dest, int size)
Definition shared.c:1922
const struct strvec * get_save_dirs(void)
Definition shared.c:941
void remove_leading_spaces(char *s)
Definition shared.c:403
char * skip_to_basename(char *filepath)
Definition shared.c:1760
void array_shuffle(int *array, int n)
Definition shared.c:2010
m_pre_result
Definition shared.h:207
@ M_PRE_EXACT
Definition shared.h:208
@ M_PRE_ONLY
Definition shared.h:209
@ M_PRE_LAST
Definition shared.h:214
@ M_PRE_LONG
Definition shared.h:212
@ M_PRE_AMBIGUOUS
Definition shared.h:210
@ M_PRE_EMPTY
Definition shared.h:211
@ M_PRE_FAIL
Definition shared.h:213
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:1590
bool is_ascii_name(const char *name)
Definition shared.c:283
enum fc_tristate fc_tristate_or(enum fc_tristate one, enum fc_tristate two)
Definition shared.c:140
int fc_snprintcf(char *buf, size_t buf_len, const char *format,...) fc__attribute((nonnull(1
const char * setup_langname(void)
Definition shared.c:1288
void randomize_base64url_string(char *s, size_t n)
Definition shared.c:339
char * get_multicast_group(bool ipv6_preferred)
Definition shared.c:1674
void free_freeciv_storage_dir(void)
Definition shared.c:696
const char *(* m_pre_accessor_fn_t)(int)
Definition shared.h:220
bool is_safe_filename(const char *name)
Definition shared.c:253
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:159
const struct strvec * get_data_dirs(void)
Definition shared.c:893
char * freeciv_storage_dir(void)
Definition shared.c:678
void switch_lang(const char *lang)
Definition shared.c:1441
struct fileinfo_list * fileinfolist_infix(const struct strvec *dirs, const char *infix, bool nodups)
Definition shared.c:1211
size_t size
Definition specvec.h:72
const void * ptr_value
Definition shared.h:308
const char * str_value
Definition shared.h:309
int int_value
Definition shared.h:306
bool bool_value
Definition shared.h:304
char letter
Definition shared.h:302
float float_value
Definition shared.h:307
char char_value
Definition shared.h:305
enum cf_type type
Definition shared.h:301
time_t mtime
Definition shared.h:170
char * fullname
Definition shared.h:169
char * name
Definition shared.h:168
#define fc__attribute(x)
Definition support.h:99
#define bool
Definition support.h:71