Freeciv21
Develop your civilization from humble roots to a global empire
shared.h File Reference
#include "support.h"
#include <QFileInfoList>
#include <QString>
#include <QStringList>
#include <time.h>
+ Include dependency graph for shared.h:

Go to the source code of this file.

Classes

struct  cf_sequence
 

Macros

#define MAX_LEN_ADDR   256
 
#define MAX_LEN_PATH   4095
 
#define FC_INFINITY   (1000 * 1000 * 1000)
 
#define BAD_HEURISTIC_INIT(_ini_val_)   = _ini_val_
 
#define BOOL_TO_TRISTATE(tri)   ((tri) ? TRI_YES : TRI_NO)
 
#define MAX(x, y)   (((x) > (y)) ? (x) : (y))
 
#define MIN(x, y)   (((x) < (y)) ? (x) : (y))
 
#define CLIP(lower, current, upper)    ((current) < (lower) ? (lower) : (current) > (upper) ? (upper) : (current))
 
#define ABS(x)   (((x) >= 0) ? (x) : -(x))
 
#define FC_WRAP(value, range)
 
#define BOOL_VAL(x)   ((x) != 0)
 
#define XOR(p, q)   (BOOL_VAL(p) != BOOL_VAL(q))
 
#define EQ(p, q)   (BOOL_VAL(p) == BOOL_VAL(q))
 
#define DIVIDE(n, d)   ((n) / (d) - (((n) < 0 && (n) % (d) < 0) ? 1 : 0))
 
#define MAX_UINT32   0xFFFFFFFF
 
#define MAX_UINT16   0xFFFF
 
#define MAX_UINT8   0xFF
 
#define ARRAY_SIZE(x)   (*(&(x) + 1) - (x))
 
#define ADD_TO_POINTER(p, n)   ((void *) ((char *) (p) + (n)))
 
#define FC_MEMBER(type, member)   (((type *) nullptr)->member)
 
#define FC_MEMBER_OFFSETOF(type, member)   ((size_t) &FC_MEMBER(type, member))
 
#define FC_MEMBER_SIZEOF(type, member)   sizeof(FC_MEMBER(type, member))
 
#define FC_MEMBER_ARRAY_SIZE(type, member)    ARRAY_SIZE(FC_MEMBER(type, member))
 
#define FC_INT_TO_PTR(i)   ((void *) (intptr_t)(i))
 
#define FC_PTR_TO_INT(p)   ((int) (intptr_t)(p))
 
#define FC_UINT_TO_PTR(u)   ((void *) (intptr_t)(u))
 
#define FC_PTR_TO_UINT(p)   ((unsigned int) (intptr_t)(p))
 
#define FC_SIZE_TO_PTR(s)   ((void *) (intptr_t)(s))
 
#define FC_PTR_TO_SIZE(p)   ((size_t)(intptr_t)(p))
 
#define INITIALIZE_ARRAY(array, size, value)
 
#define PARENT_DIR_OPERATOR   ".."
 
#define sz_loud_strlcpy(buffer, str, errmsg)    loud_strlcpy(buffer, str, sizeof(buffer), errmsg)
 

Typedefs

typedef const char *(* m_pre_accessor_fn_t) (int)
 
typedef int(* m_pre_strncmp_fn_t) (const char *, const char *, size_t n)
 
typedef size_t() m_strlen_fn_t(const char *str)
 

Enumerations

enum  fc_tristate { TRI_NO , TRI_YES , TRI_MAYBE }
 
enum  m_pre_result {
  M_PRE_EXACT , M_PRE_ONLY , M_PRE_AMBIGUOUS , M_PRE_EMPTY ,
  M_PRE_LONG , M_PRE_FAIL , M_PRE_LAST
}
 
enum  cf_type {
  CF_BOOLEAN , CF_TRANS_BOOLEAN , CF_CHARACTER , CF_INTEGER ,
  CF_HEXA , CF_FLOAT , CF_POINTER , CF_STRING ,
  CF_LAST = -1
}
 

Functions

enum fc_tristate fc_tristate_and (enum fc_tristate one, enum fc_tristate two)
 An AND function for fc_tristate. More...
 
const char * big_int_to_text (unsigned int mantissa, unsigned int exponent)
 Returns a statically allocated string containing a nicely-formatted version of the given number according to the user's locale. More...
 
const char * int_to_text (unsigned int number)
 Return a prettily formatted string containing the given number. More...
 
bool is_ascii_name (const char *name)
 This is used in sundry places to make sure that names of cities, players etc. More...
 
bool is_base64url (const char *s)
 Check for valid base64url. More...
 
bool is_safe_filename (const QString &name)
 Check if the name is safe security-wise. More...
 
void randomize_base64url_string (char *s, size_t n)
 generate a random string meeting criteria such as is_ascii_name(), is_base64url(), and is_safe_filename(). More...
 
char * skip_leading_spaces (char *s)
 Returns 's' incremented to first non-space character. More...
 
void remove_leading_spaces (char *s)
 Removes leading spaces in string pointed to by 's'. More...
 
void remove_trailing_spaces (char *s)
 Terminates string pointed to by 's' to remove traling spaces; Note 's' must point to writeable memory! More...
 
void remove_leading_trailing_spaces (char *s)
 Removes leading and trailing spaces in string pointed to by 's'. More...
 
bool check_strlen (const char *str, size_t len, const char *errmsg)
 Check the length of the given string. More...
 
size_t loud_strlcpy (char *buffer, const char *str, size_t len, const char *errmsg)
 Call check_strlen() on str and then strlcpy() it into buffer. More...
 
bool str_to_int (const char *str, int *pint)
 Convert 'str' to it's int reprentation if possible. More...
 
char * user_username (char *buf, size_t bufsz)
 Returns string which gives user's username, as specified by $USER or as given in password file for this user's uid, or a made up name if we can't get either of the above. More...
 
QString freeciv_storage_dir ()
 Returns string which gives freeciv storage dir. More...
 
const QStringList & get_data_dirs ()
 Returns a list of data directory paths, in the order in which they should be searched. More...
 
const QStringList & get_save_dirs ()
 Returns a list of save directory paths, in the order in which they should be searched. More...
 
const QStringList & get_scenario_dirs ()
 Returns a list of scenario directory paths, in the order in which they should be searched. More...
 
QVector< QString > * fileinfolist (const QStringList &dirs, const char *suffix)
 Returns a string vector storing the filenames in the data directories matching the given suffix. More...
 
QFileInfoList find_files_in_path (const QStringList &path, const QString &pattern, bool nodups)
 Search for file names matching the pattern in the provided list of directories. More...
 
QString fileinfoname (const QStringList &dirs, const QString &filename)
 Returns a filename to access the specified file from a directory by searching all specified directories for the file. More...
 
void init_nls ()
 Setup for Native Language Support, if configured to use it. More...
 
void free_nls ()
 Free memory allocated by Native Language Support. More...
 
char * setup_langname ()
 Language environmental variable (with emulation). More...
 
void dont_run_as_root (const char *argv0, const char *fallback)
 If we have root privileges, die with an error. More...
 
const char * m_pre_description (enum m_pre_result result)
 Return a description string of the result. More...
 
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)
 See match_prefix_full(). More...
 
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)
 Given n names, with maximum length max_len_name, accessed by accessor_fn(0) to accessor_fn(n-1), look for matching prefix according to given comparison function. More...
 
char * get_multicast_group (bool ipv6_preferred)
 Returns string which gives the multicast group IP address for finding servers on the LAN, as specified by $FREECIV_MULTICAST_GROUP. More...
 
void free_multicast_group ()
 Free multicast group resources. More...
 
QString interpret_tilde (const QString &filename)
 Interpret ~ in filename as home dir. More...
 
bool make_dir (const QString &pathname)
 If the directory "pathname" does not exist, recursively create all directories until it does. More...
 
char scanin (char **buf, char *delimiters, char *dest, int size)
 Scan in a word or set of words from start to but not including any of the given delimiters. More...
 
void array_shuffle (int *array, int n)
 Randomize the elements of an array using the Fisher-Yates shuffle. More...
 
void format_time_duration (time_t t, char *buf, int maxlen)
 Convenience function to nicely format a time_t seconds value in to a string with hours, minutes, etc. More...
 
bool wildcard_fit_string (const char *pattern, const char *test)
 Returns TRUE if test fit the pattern. More...
 
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 static void cf_int_seq (char letter, int value, struct cf_sequence *out)
 
static struct cf_sequence cf_str_seq (char letter, const char *value)
 
static struct cf_sequence cf_end ()
 
bool formats_match (const char *format1, const char *format2)
 Returns TRUE iff both formats are compatible (if 'format1' can be used instead 'format2' and reciprocally). More...
 

Macro Definition Documentation

◆ ABS

#define ABS (   x)    (((x) >= 0) ? (x) : -(x))

Definition at line 55 of file shared.h.

◆ ADD_TO_POINTER

#define ADD_TO_POINTER (   p,
 
)    ((void *) ((char *) (p) + (n)))

Definition at line 80 of file shared.h.

◆ ARRAY_SIZE

#define ARRAY_SIZE (   x)    (*(&(x) + 1) - (x))

Definition at line 79 of file shared.h.

◆ BAD_HEURISTIC_INIT

#define BAD_HEURISTIC_INIT (   _ini_val_)    = _ini_val_

Definition at line 37 of file shared.h.

◆ BOOL_TO_TRISTATE

#define BOOL_TO_TRISTATE (   tri)    ((tri) ? TRI_YES : TRI_NO)

Definition at line 43 of file shared.h.

◆ BOOL_VAL

#define BOOL_VAL (   x)    ((x) != 0)

Definition at line 63 of file shared.h.

◆ CLIP

#define CLIP (   lower,
  current,
  upper 
)     ((current) < (lower) ? (lower) : (current) > (upper) ? (upper) : (current))

Definition at line 51 of file shared.h.

◆ DIVIDE

#define DIVIDE (   n,
 
)    ((n) / (d) - (((n) < 0 && (n) % (d) < 0) ? 1 : 0))

Definition at line 71 of file shared.h.

◆ EQ

#define EQ (   p,
 
)    (BOOL_VAL(p) == BOOL_VAL(q))

Definition at line 65 of file shared.h.

◆ FC_INFINITY

#define FC_INFINITY   (1000 * 1000 * 1000)

Definition at line 32 of file shared.h.

◆ FC_INT_TO_PTR

#define FC_INT_TO_PTR (   i)    ((void *) (intptr_t)(i))

Definition at line 88 of file shared.h.

◆ FC_MEMBER

#define FC_MEMBER (   type,
  member 
)    (((type *) nullptr)->member)

Definition at line 82 of file shared.h.

◆ FC_MEMBER_ARRAY_SIZE

#define FC_MEMBER_ARRAY_SIZE (   type,
  member 
)     ARRAY_SIZE(FC_MEMBER(type, member))

Definition at line 85 of file shared.h.

◆ FC_MEMBER_OFFSETOF

#define FC_MEMBER_OFFSETOF (   type,
  member 
)    ((size_t) &FC_MEMBER(type, member))

Definition at line 83 of file shared.h.

◆ FC_MEMBER_SIZEOF

#define FC_MEMBER_SIZEOF (   type,
  member 
)    sizeof(FC_MEMBER(type, member))

Definition at line 84 of file shared.h.

◆ FC_PTR_TO_INT

#define FC_PTR_TO_INT (   p)    ((int) (intptr_t)(p))

Definition at line 89 of file shared.h.

◆ FC_PTR_TO_SIZE

#define FC_PTR_TO_SIZE (   p)    ((size_t)(intptr_t)(p))

Definition at line 93 of file shared.h.

◆ FC_PTR_TO_UINT

#define FC_PTR_TO_UINT (   p)    ((unsigned int) (intptr_t)(p))

Definition at line 91 of file shared.h.

◆ FC_SIZE_TO_PTR

#define FC_SIZE_TO_PTR (   s)    ((void *) (intptr_t)(s))

Definition at line 92 of file shared.h.

◆ FC_UINT_TO_PTR

#define FC_UINT_TO_PTR (   u)    ((void *) (intptr_t)(u))

Definition at line 90 of file shared.h.

◆ FC_WRAP

#define FC_WRAP (   value,
  range 
)
Value:
((value) < 0 ? ((value) % (range) != 0 ? (value) % (range) + (range) : 0) \
: ((value) >= (range) ? (value) % (range) : (value)))

Definition at line 59 of file shared.h.

◆ INITIALIZE_ARRAY

#define INITIALIZE_ARRAY (   array,
  size,
  value 
)
Value:
{ \
int _ini_index; \
\
for (_ini_index = 0; _ini_index < (size); _ini_index++) { \
(array)[_ini_index] = (value); \
} \
}
size_t size
Definition: specvec.h:64

Definition at line 99 of file shared.h.

◆ MAX

#define MAX (   x,
 
)    (((x) > (y)) ? (x) : (y))

Definition at line 48 of file shared.h.

◆ MAX_LEN_ADDR

#define MAX_LEN_ADDR   256

Definition at line 27 of file shared.h.

◆ MAX_LEN_PATH

#define MAX_LEN_PATH   4095

Definition at line 28 of file shared.h.

◆ MAX_UINT16

#define MAX_UINT16   0xFFFF

Definition at line 74 of file shared.h.

◆ MAX_UINT32

#define MAX_UINT32   0xFFFFFFFF

Definition at line 73 of file shared.h.

◆ MAX_UINT8

#define MAX_UINT8   0xFF

Definition at line 75 of file shared.h.

◆ MIN

#define MIN (   x,
 
)    (((x) < (y)) ? (x) : (y))

Definition at line 49 of file shared.h.

◆ PARENT_DIR_OPERATOR

#define PARENT_DIR_OPERATOR   ".."

Definition at line 108 of file shared.h.

◆ sz_loud_strlcpy

#define sz_loud_strlcpy (   buffer,
  str,
  errmsg 
)     loud_strlcpy(buffer, str, sizeof(buffer), errmsg)

Definition at line 127 of file shared.h.

◆ XOR

#define XOR (   p,
 
)    (BOOL_VAL(p) != BOOL_VAL(q))

Definition at line 64 of file shared.h.

Typedef Documentation

◆ m_pre_accessor_fn_t

typedef const char*(* m_pre_accessor_fn_t) (int)

Definition at line 165 of file shared.h.

◆ m_pre_strncmp_fn_t

typedef int(* m_pre_strncmp_fn_t) (const char *, const char *, size_t n)

Definition at line 168 of file shared.h.

◆ m_strlen_fn_t

typedef size_t() m_strlen_fn_t(const char *str)

Definition at line 171 of file shared.h.

Enumeration Type Documentation

◆ cf_type

enum cf_type
Enumerator
CF_BOOLEAN 
CF_TRANS_BOOLEAN 
CF_CHARACTER 
CF_INTEGER 
CF_HEXA 
CF_FLOAT 
CF_POINTER 
CF_STRING 
CF_LAST 

Definition at line 212 of file shared.h.

◆ fc_tristate

Enumerator
TRI_NO 
TRI_YES 
TRI_MAYBE 

Definition at line 42 of file shared.h.

◆ m_pre_result

Enumerator
M_PRE_EXACT 
M_PRE_ONLY 
M_PRE_AMBIGUOUS 
M_PRE_EMPTY 
M_PRE_LONG 
M_PRE_FAIL 
M_PRE_LAST 

Definition at line 152 of file shared.h.

Function Documentation

◆ array_shuffle()

void array_shuffle ( int *  array,
int  n 
)

Randomize the elements of an array using the Fisher-Yates shuffle.

see: http://benpfaff.org/writings/clc/shuffle.html

Definition at line 1228 of file shared.cpp.

Referenced by shuffle_players().

◆ big_int_to_text()

const char* big_int_to_text ( unsigned int  mantissa,
unsigned int  exponent 
)

Returns a statically allocated string containing a nicely-formatted version of the given number according to the user's locale.

(Only works for numbers >= zero.) The number is given in scientific notation as mantissa * 10^exponent.

Definition at line 117 of file shared.cpp.

Referenced by int_to_text(), and population_to_text().

◆ cf_end()

static struct cf_sequence cf_end ( )
inlinestatic

Definition at line 241 of file shared.h.

Referenced by generate_save_name().

◆ cf_int_seq()

static void cf_int_seq ( char  letter,
int  value,
struct cf_sequence out 
)
inlinestatic

Definition at line 241 of file shared.h.

Referenced by generate_save_name().

◆ cf_str_seq()

static struct cf_sequence cf_str_seq ( char  letter,
const char *  value 
)
inlinestatic

Definition at line 241 of file shared.h.

Referenced by generate_save_name().

◆ check_strlen()

bool check_strlen ( const char *  str,
size_t  len,
const char *  errmsg 
)

Check the length of the given string.

If the string is too long, log errmsg, which should be a string in printf-format taking up to two arguments: the string and the length.

Definition at line 364 of file shared.cpp.

Referenced by load_ruleset_nations(), and loud_strlcpy().

◆ dont_run_as_root()

void dont_run_as_root ( const char *  argv0,
const char *  fallback 
)

If we have root privileges, die with an error.

(Eg, for security reasons.) Param argv0 should be argv[0] or similar; fallback is used instead if argv0 is nullptr. But don't die on systems where the user is always root... (a general test for this would be better). Doesn't use log_*() because gets called before logging is setup.

Definition at line 948 of file shared.cpp.

Referenced by client_main(), and main().

◆ fc_tristate_and()

enum fc_tristate fc_tristate_and ( enum fc_tristate  one,
enum fc_tristate  two 
)

An AND function for fc_tristate.

Definition at line 93 of file shared.cpp.

◆ fc_vsnprintcf()

int fc_vsnprintcf ( char *  buf,
size_t  buf_len,
const char *  format,
const struct cf_sequence sequences,
size_t  sequences_num 
)

◆ fileinfolist()

QVector<QString>* fileinfolist ( const QStringList &  dirs,
const char *  suffix 
)

Returns a string vector storing the filenames in the data directories matching the given suffix.

The list is allocated when the function is called; it should either be stored permanently or destroyed (with strvec_destroy()).

The suffixes are removed from the filenames before the list is returned.

Definition at line 623 of file shared.cpp.

Referenced by get_init_script_choices(), get_musicset_list(), get_soundset_list(), get_tileset_list(), and tilespec_try_read().

◆ fileinfoname()

QString fileinfoname ( const QStringList &  dirs,
const QString &  filename 
)

Returns a filename to access the specified file from a directory by searching all specified directories for the file.

Returns an empty string if the specified filename cannot be found in any of the data directories.

Definition at line 661 of file shared.cpp.

Referenced by audio_play_tag(), audiospec_fullname(), boot_help_texts(), comments_load(), datafilename(), fcIcons::getIcon(), fcIcons::getPath(), fcIcons::getPixmap(), load_command(), load_gfx_file(), lua_command(), read_init_script_real(), set_rulesetdir(), sg_load_savefile(), tileset_read_toplevel(), tilespec_fullname(), and valid_ruleset_filename().

◆ find_files_in_path()

QFileInfoList find_files_in_path ( const QStringList &  path,
const QString &  pattern,
bool  nodups 
)

Search for file names matching the pattern in the provided list of directories.

"nodups" removes duplicates. The returned list will be sorted by modification time.

Definition at line 681 of file shared.cpp.

Referenced by load_fonts(), show_scenarios(), fc_client::start_tutorial(), page_load::update_load_page(), and page_scenario::update_scenarios_page().

◆ format_time_duration()

void format_time_duration ( time_t  t,
char *  buf,
int  maxlen 
)

Convenience function to nicely format a time_t seconds value in to a string with hours, minutes, etc.

Definition at line 1187 of file shared.cpp.

Referenced by popup_info_text(), timeout_show_command(), and unit_can_do_action_now().

◆ formats_match()

bool formats_match ( const char *  format1,
const char *  format2 
)

Returns TRUE iff both formats are compatible (if 'format1' can be used instead 'format2' and reciprocally).

Definition at line 1621 of file shared.cpp.

Referenced by ruler_title_check(), and sanity_check_ruleset_data().

◆ free_multicast_group()

void free_multicast_group ( )

Free multicast group resources.

Definition at line 1091 of file shared.cpp.

Referenced by free_libfreeciv().

◆ free_nls()

void free_nls ( )

Free memory allocated by Native Language Support.

Definition at line 931 of file shared.cpp.

Referenced by client_exit(), fcmp_deinit(), main(), and server_quit().

◆ freeciv_storage_dir()

QString freeciv_storage_dir ( )

Returns string which gives freeciv storage dir.

Gets value once, and then caches result. Note the caller should not mess with the returned string.

Definition at line 419 of file shared.cpp.

Referenced by client_start_server(), default_data_path(), default_save_path(), fcmp_parse_cmdline(), get_challenge_fullname(), freeciv::server::init_interactive(), fc_client::load_modpack(), mr_menu::save_image(), send_client_wants_hack(), and freeciv::server::~server().

◆ get_data_dirs()

const QStringList& get_data_dirs ( )

Returns a list of data directory paths, in the order in which they should be searched.

These paths are specified internally or may be set as the environment variable $FREECIV_DATA PATH (a separated list of directories, where the separator itself is specified internally, platform-dependent). '~' at the start of a component (provided followed by '/' or '\0') is expanded as $HOME.

The returned pointer is static and shouldn't be modified, nor destroyed by the user caller.

Definition at line 533 of file shared.cpp.

Referenced by audio_play_tag(), audiospec_fullname(), boot_help_texts(), comments_load(), datafilename(), get_gui_specific_themes_directories(), get_init_script_choices(), get_musicset_list(), get_soundset_list(), get_tileset_list(), fcIcons::getIcon(), fcIcons::getPath(), fcIcons::getPixmap(), insert_generated_text(), load_fonts(), load_gfx_file(), lua_command(), main(), read_init_script_real(), set_rulesetdir(), tileset_read_toplevel(), tilespec_fullname(), tilespec_try_read(), ui_main(), and valid_ruleset_filename().

◆ get_multicast_group()

char* get_multicast_group ( bool  ipv6_preferred)

Returns string which gives the multicast group IP address for finding servers on the LAN, as specified by $FREECIV_MULTICAST_GROUP.

Gets value once, and then caches result.

Definition at line 1065 of file shared.cpp.

Referenced by fcUdpScan::begin_scan(), send_lanserver_response(), and server_open_socket().

◆ get_save_dirs()

const QStringList& get_save_dirs ( )

Returns a list of save directory paths, in the order in which they should be searched.

These paths are specified internally or may be set as the environment variable $FREECIV_SAVE_PATH (a separated list of directories, where the separator itself is specified internally, platform-dependent). '~' at the start of a component (provided followed by '/' or '\0') is expanded as $HOME.

The returned pointer is static and shouldn't be modified, nor destroyed by the user caller.

Definition at line 563 of file shared.cpp.

Referenced by load_command(), mr_menu::save_game_as(), and page_load::update_load_page().

◆ get_scenario_dirs()

const QStringList& get_scenario_dirs ( )

Returns a list of scenario directory paths, in the order in which they should be searched.

These paths are specified internally or may be set as the environment variable $FREECIV_SCENARIO_PATH (a separated list of directories, where the separator itself is specified internally, platform-dependent). '~' at the start of a component (provided followed by '/' or '\0') is expanded as $HOME.

The returned pointer is static and shouldn't be modified, nor destroyed by the user caller.

Definition at line 594 of file shared.cpp.

Referenced by load_command(), sg_load_savefile(), show_scenarios(), fc_client::start_tutorial(), and page_scenario::update_scenarios_page().

◆ init_nls()

void init_nls ( )

Setup for Native Language Support, if configured to use it.

(Call this only once, or it may leak memory.)

Definition at line 871 of file shared.cpp.

Referenced by client_main(), fcmp_init(), main(), and srv_init().

◆ int_to_text()

const char* int_to_text ( unsigned int  number)

Return a prettily formatted string containing the given number.

Definition at line 191 of file shared.cpp.

Referenced by value_units().

◆ interpret_tilde()

QString interpret_tilde ( const QString &  filename)

Interpret ~ in filename as home dir.

Definition at line 1100 of file shared.cpp.

Referenced by lua_command(), make_dir(), read_init_script_real(), secfile_load_section(), secfile_save(), and write_init_script().

◆ is_ascii_name()

bool is_ascii_name ( const char *  name)

This is used in sundry places to make sure that names of cities, players etc.

do not contain yucky characters of various sorts. Returns TRUE iff the name is acceptable. FIXME: Not internationalised.

Definition at line 223 of file shared.cpp.

Referenced by is_allowed_city_name(), is_good_password(), server_player_name_is_allowed(), and user_username().

◆ is_base64url()

bool is_base64url ( const char *  s)

Check for valid base64url.

Definition at line 258 of file shared.cpp.

Referenced by sg_load_sanitycheck().

◆ is_safe_filename()

bool is_safe_filename ( const QString &  name)

Check if the name is safe security-wise.

This is intended to be used to make sure an untrusted filename is safe to be used.

Definition at line 210 of file shared.cpp.

Referenced by api_server_save(), load_command(), lua_command(), read_init_script_real(), savename_validate(), scorefile_validate(), send_client_wants_hack(), and set_rulesetdir().

◆ loud_strlcpy()

size_t loud_strlcpy ( char *  buffer,
const char *  str,
size_t  len,
const char *  errmsg 
)

Call check_strlen() on str and then strlcpy() it into buffer.

Definition at line 373 of file shared.cpp.

◆ m_pre_description()

const char* m_pre_description ( enum m_pre_result  result)

Return a description string of the result.

In English, form of description is suitable to substitute in, eg: prefix is (N.B.: The description is always in English, but they have all been marked for translation. If you want a localized version, use _() on the return.)

Definition at line 973 of file shared.cpp.

Referenced by cmd_reply_no_such_conn(), and cmd_reply_no_such_player().

◆ make_dir()

bool make_dir ( const QString &  pathname)

If the directory "pathname" does not exist, recursively create all directories until it does.

Definition at line 1115 of file shared.cpp.

Referenced by img_save(), options_save(), and save_ruleset().

◆ match_prefix()

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 
)

◆ match_prefix_full()

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 
)

Given n names, with maximum length max_len_name, accessed by accessor_fn(0) to accessor_fn(n-1), look for matching prefix according to given comparison function.

Returns type of match or fail, and for return <= M_PRE_AMBIGUOUS sets *ind_result with matching index (or for ambiguous, first match). If max_len_name == 0, treat as no maximum. If the int array 'matches' is non-nullptr, up to 'max_matches' ambiguous matching names indices will be inserted into it. If 'pnum_matches' is non-nullptr, it will be set to the number of indices inserted into 'matches'.

Definition at line 973 of file shared.cpp.

Referenced by show_help().

◆ randomize_base64url_string()

void randomize_base64url_string ( char *  s,
size_t  n 
)

generate a random string meeting criteria such as is_ascii_name(), is_base64url(), and is_safe_filename().

Definition at line 279 of file shared.cpp.

Referenced by init_new_game(), and sg_load_sanitycheck().

◆ remove_leading_spaces()

void remove_leading_spaces ( char *  s)

Removes leading spaces in string pointed to by 's'.

Note 's' must point to writeable memory!

Definition at line 312 of file shared.cpp.

Referenced by handle_stdin_input_real(), and remove_leading_trailing_spaces().

◆ remove_leading_trailing_spaces()

◆ remove_trailing_spaces()

void remove_trailing_spaces ( char *  s)

Terminates string pointed to by 's' to remove traling spaces; Note 's' must point to writeable memory!

Definition at line 330 of file shared.cpp.

Referenced by handle_stdin_input_real(), and remove_leading_trailing_spaces().

◆ scanin()

char scanin ( char **  buf,
char *  delimiters,
char *  dest,
int  size 
)

Scan in a word or set of words from start to but not including any of the given delimiters.

The buf pointer will point past delimiter, or be set to nullptr if there is nothing there. Removes excess white space.

This function should be safe, and dest will contain "\0" and buf == nullptr on failure. We always fail gently.

Due to the way the scanning is performed, looking for a space will give you the first word even if it comes before multiple spaces.

Returns delimiter found.

Pass in nullptr for dest and -1 for size to just skip ahead. Note that if nothing is found, dest will contain the whole string, minus leading and trailing whitespace. You can scan for "" to conveniently grab the remainder of a string.

Definition at line 1142 of file shared.cpp.

Referenced by sg_load_map_owner(), sg_load_map_worked(), and sg_load_player_vision().

◆ setup_langname()

char* setup_langname ( )

Language environmental variable (with emulation).

Definition at line 717 of file shared.cpp.

Referenced by init_nls().

◆ skip_leading_spaces()

char* skip_leading_spaces ( char *  s)

◆ str_to_int()

bool str_to_int ( const char *  str,
int *  pint 
)

Convert 'str' to it's int reprentation if possible.

'pint' can be nullptr, then it will only test 'str' only contains an integer number.

Definition at line 384 of file shared.cpp.

Referenced by cancelvote_command(), debug_command(), desired_settable_option_send(), get_compression_level(), lookup_option_level(), set_command(), sg_load_map_owner(), sg_load_map_worked(), sg_load_player_vision(), team_command(), text_tag_init_from_sequence(), unignore_command(), and vote_command().

◆ user_username()

char* user_username ( char *  buf,
size_t  bufsz 
)

Returns string which gives user's username, as specified by $USER or as given in password file for this user's uid, or a made up name if we can't get either of the above.

Gets value once, and then caches result. Note the caller should not mess with returned string.

Definition at line 443 of file shared.cpp.

Referenced by client_main().

◆ wildcard_fit_string()

bool wildcard_fit_string ( const char *  pattern,
const char *  test 
)

Returns TRUE if test fit the pattern.

The pattern can contain special characters: '*': to specify a substitute for any zero or more characters. '?': to specify a substitute for any one character. '[...]': to specify a range of characters: '!': at the begenning of the range means that the matching result will be inverted 'A-Z': means any character between 'A' and 'Z'. 'agr': means 'a', 'g' or 'r'.

Definition at line 1372 of file shared.cpp.

Referenced by conn_pattern_match(), and wildcard_asterisk_fit().