Freeciv21
Develop your civilization from humble roots to a global empire
support.h File Reference
#include <cinttypes>
#include <cstdio>
#include <QString>
+ Include dependency graph for support.h:

Go to the source code of this file.

Macros

#define fc__attribute(x)
 
#define fc__warn_unused_result
 
#define fc__fallthrough
 
#define fc_malloc(sz)   malloc(sz)
 
#define fc_realloc(ptr, sz)   realloc(ptr, sz)
 
#define NFCPP_FREE(ptr)
 
#define NFC_FREE(ptr)
 
#define NFCN_FREE(ptr)
 
#define VOIDNFCN_FREE(ptr)
 
#define NFCNPP_FREE(ptr)
 
#define FCPP_FREE(ptr)
 
#define FC_FREE(ptr)
 
#define fc_strdup(str)   real_fc_strdup((str), "strdup", __FC_LINE__, __FILE__)
 
#define sz_strlcpy(dest, src)    ((void) fc_strlcpy((dest), (src), sizeof(dest)))
 
#define sz_strlcat(dest, src)    ((void) fc_strlcat((dest), (src), sizeof(dest)))
 

Typedefs

typedef int fc_errno
 

Functions

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. More...
 
int fc_strcasecmp (const char *str0, const char *str1)
 Compare strings like strcmp(), but ignoring case. More...
 
int fc_strncasecmp (const char *str0, const char *str1, size_t n)
 Compare strings like strncmp(), but ignoring case. More...
 
int fc_strncasequotecmp (const char *str0, const char *str1, size_t n)
 Compare strings like strncasecmp() but ignoring surrounding quotes in either string. More...
 
size_t effectivestrlenquote (const char *str)
 Count length of string without possible surrounding quotes. More...
 
int fc_strcoll (const char *str0, const char *str1)
 Wrapper function for strcoll(). More...
 
int fc_stricoll (const char *str0, const char *str1)
 Wrapper function for stricoll(). More...
 
FILE * fc_fopen (const char *filename, const char *opentype)
 Wrapper function for fopen() with filename conversion to local encoding on Windows. More...
 
int fc_remove (const char *filename)
 Wrapper function for remove() with filename conversion to local encoding on Windows. More...
 
int fc_stat (const char *filename, struct stat *buf)
 Wrapper function for stat() with filename conversion to local encoding on Windows. More...
 
fc_errno fc_get_errno ()
 Returns last error code. More...
 
const char * fc_strerror (fc_errno err)
 Return a string which describes a given error (errno-style.) The string is converted as necessary from the local_encoding to internal_encoding, for inclusion in translations. More...
 
void fc_usleep (unsigned long usec)
 Suspend execution for the specified number of microseconds. More...
 
bool fc_strrep (char *str, size_t len, const char *search, const char *replace)
 Replace 'search' by 'replace' within 'str'. More...
 
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-friendly version of strncpy(), in particular easier to use safely and correctly, and ensuring nul-terminated results while being able to detect truncation. More...
 
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-friendly version of strncat(), in particular easier to use safely and correctly, and ensuring nul-terminated results while being able to detect truncation. More...
 
int fc_snprintf (char *str, size_t n, const char *format,...) fc__attribute((__format__(__printf__
 
int fc__attribute ((nonnull(1, 3)))
 
int fc_vsnprintf (char *str, size_t n, const char *format, va_list ap) fc__attribute((nonnull(1
 
int int cat_snprintf (char *str, size_t n, const char *format,...) fc__attribute((__format__(__printf__
 
int fc_gethostname (char *buf, size_t len)
 Call gethostname() if supported, else just returns -1. More...
 
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. More...
 
void make_escapes (const char *str, char *buf, size_t buf_len)
 Copies a string and convert the following characters: More...
 
QString remove_escapes (const QString &str, bool full_escapes)
 Copies a string. More...
 
int fc_at_quick_exit (void(*func)())
 Set quick_exit() callback if possible. More...
 

Macro Definition Documentation

◆ fc__attribute

#define fc__attribute (   x)

Definition at line 31 of file support.h.

◆ fc__fallthrough

#define fc__fallthrough

Definition at line 49 of file support.h.

◆ fc__warn_unused_result

#define fc__warn_unused_result

Definition at line 41 of file support.h.

◆ FC_FREE

#define FC_FREE (   ptr)
Value:
do { \
delete (ptr); \
(ptr) = NULL; \
} while (false)

Definition at line 105 of file support.h.

◆ fc_malloc

#define fc_malloc (   sz)    malloc(sz)

Definition at line 58 of file support.h.

◆ fc_realloc

#define fc_realloc (   ptr,
  sz 
)    realloc(ptr, sz)

Definition at line 59 of file support.h.

◆ fc_strdup

#define fc_strdup (   str)    real_fc_strdup((str), "strdup", __FC_LINE__, __FILE__)

Definition at line 111 of file support.h.

◆ FCPP_FREE

#define FCPP_FREE (   ptr)
Value:
do { \
delete[](ptr); \
(ptr) = NULL; \
} while (false)

Definition at line 99 of file support.h.

◆ NFC_FREE

#define NFC_FREE (   ptr)
Value:
do { \
if (ptr) { \
delete (ptr); \
} \
} while (false)

Definition at line 68 of file support.h.

◆ NFCN_FREE

#define NFCN_FREE (   ptr)
Value:
do { \
if (ptr) { \
delete (ptr); \
(ptr) = nullptr; \
} \
} while (false)

Definition at line 75 of file support.h.

◆ NFCNPP_FREE

#define NFCNPP_FREE (   ptr)
Value:
do { \
if (ptr) { \
delete[](ptr); \
(ptr) = nullptr; \
} \
} while (false)

Definition at line 91 of file support.h.

◆ NFCPP_FREE

#define NFCPP_FREE (   ptr)
Value:
do { \
if (ptr) { \
delete[](ptr); \
} \
} while (false)

Definition at line 61 of file support.h.

◆ sz_strlcat

#define sz_strlcat (   dest,
  src 
)     ((void) fc_strlcat((dest), (src), sizeof(dest)))

Definition at line 142 of file support.h.

◆ sz_strlcpy

#define sz_strlcpy (   dest,
  src 
)     ((void) fc_strlcpy((dest), (src), sizeof(dest)))

Definition at line 140 of file support.h.

◆ VOIDNFCN_FREE

#define VOIDNFCN_FREE (   ptr)
Value:
do { \
if (ptr) { \
::operator delete(ptr); \
(ptr) = nullptr; \
} \
} while (false)

Definition at line 83 of file support.h.

Typedef Documentation

◆ fc_errno

typedef int fc_errno

Definition at line 55 of file support.h.

Function Documentation

◆ cat_snprintf()

int int cat_snprintf ( char *  str,
size_t  n,
const char *  format,
  ... 
)

◆ effectivestrlenquote()

size_t effectivestrlenquote ( const char *  str)

Count length of string without possible surrounding quotes.

Definition at line 189 of file support.cpp.

Referenced by conn_by_user_prefix(), and player_by_name_prefix().

◆ fc__attribute()

int int fc__attribute ( (nonnull(1, 3))  )

◆ fc_at_quick_exit()

int fc_at_quick_exit ( void(*)()  func)

Set quick_exit() callback if possible.

Definition at line 657 of file support.cpp.

Referenced by client_main().

◆ fc_break_lines()

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.

'str' is modified. Returns number of lines in modified s.

Definition at line 597 of file support.cpp.

Referenced by show_help_command(), show_help_intro(), show_help_option(), show_nationsets(), show_ruleset_info(), and show_settings_one().

◆ fc_fopen()

FILE* fc_fopen ( const char *  filename,
const char *  opentype 
)

Wrapper function for fopen() with filename conversion to local encoding on Windows.

Definition at line 255 of file support.cpp.

Referenced by log_civ_score_now(), lua_command(), manual_command(), rank_users(), read_init_script_real(), save_script_lua(), script_server_load_file(), and write_init_script().

◆ fc_get_errno()

fc_errno fc_get_errno ( )

Returns last error code.

Definition at line 311 of file support.cpp.

Referenced by main().

◆ fc_gethostname()

int fc_gethostname ( char *  buf,
size_t  len 
)

Call gethostname() if supported, else just returns -1.

Definition at line 586 of file support.cpp.

Referenced by establish_new_connection(), send_lanserver_response(), and send_to_metaserver().

◆ fc_remove()

int fc_remove ( const char *  filename)

Wrapper function for remove() with filename conversion to local encoding on Windows.

Definition at line 274 of file support.cpp.

Referenced by handle_single_want_hack_reply().

◆ fc_snprintf()

int fc_snprintf ( char *  str,
size_t  n,
const char *  format,
  ... 
)

◆ fc_stat()

int fc_stat ( const char *  filename,
struct stat *  buf 
)

Wrapper function for stat() with filename conversion to local encoding on Windows.

Definition at line 293 of file support.cpp.

Referenced by load_install_info_lists(), and script_server_load_file().

◆ fc_strcasecmp()

int fc_strcasecmp ( const char *  str0,
const char *  str1 
)

Compare strings like strcmp(), but ignoring case.

Definition at line 89 of file support.cpp.

Referenced by achievement_by_rule_name(), action_by_rule_name(), advance_by_rule_name(), ai_level_help(), ai_type_by_name(), api_edit_trait_mod_set(), api_edit_unit_kill(), api_effects_city_bonus(), api_effects_player_bonus(), api_effects_world_bonus(), api_find_role_unit_type(), api_methods_nation_trait_default(), api_methods_nation_trait_max(), api_methods_nation_trait_min(), api_methods_player_has_flag(), api_methods_player_trait(), api_methods_player_trait_base(), api_methods_player_trait_current_mod(), api_methods_unit_type_has_flag(), api_methods_unit_type_has_role(), api_utilities_str2dir(), boot_help_texts(), cancelvote_command(), check_leader_names(), check_sprite_type(), city_list_find_name(), city_style_by_rule_name(), cmdlevel_command(), command_named(), compar_event_message_texts(), compat_load_020400(), compat_load_020500(), compat_load_020600(), conn_by_user(), conn_pattern_from_string(), delegate_command(), desired_settable_option_send(), diplrel_by_rule_name(), download_modpack(), download_modpack_list(), effect_edit::effect_type_menu(), event_cache_load(), extra_type_by_rule_name(), fc_cmp(), fc_stricoll(), goods_by_rule_name(), government_by_rule_name(), handle_login_request(), improvement_by_rule_name(), freeciv::layer_abstract_activities::initialize_extra(), is_default_city_name(), is_on_allowed_list(), load_action_range_max(), load_city_name_list(), load_install_info_list(), load_ruleset_buildings(), load_ruleset_effects(), load_ruleset_game(), load_ruleset_nations(), load_ruleset_techs(), load_ruleset_terrain(), load_ruleset_units(), load_tech_names(), load_terrain_names(), load_unit_names(), lookup_cbonus_list(), lookup_req_list(), mapimg_define(), metaconnection_command(), multiplier_by_rule_name(), nation_by_rule_name(), nation_group_by_rule_name(), nation_leader_by_name(), nation_set_by_rule_name(), output_type_by_identifier(), player_by_name(), player_by_user(), player_by_user_delegated(), playercolor_command(), req_edit::req_range_menu(), req_edit::req_type_menu(), rscompat_names(), rscompat_old_effect_3_1(), rscompat_req_name_3_1(), save_game_ruleset(), secfile_lookup_enum_data(), server_player_name_is_allowed(), set_ai_level_named(), setting_ruleset_one(), settings_game_load(), settings_list_cmp(), sg_load_game(), sg_load_player_cities(), sg_load_player_city(), sg_load_player_main(), sg_load_player_unit(), sg_load_player_vision_city(), sg_load_savefile(), sg_load_treaties(), specialist_by_rule_name(), style_by_rule_name(), team_slot_by_rule_name(), tech_class_by_rule_name(), technology_load(), terrain_by_rule_name(), tileset_read_toplevel(), tileset_setup_extra(), unit_class_by_rule_name(), unit_type_by_rule_name(), and universal_by_number().

◆ fc_strcoll()

int fc_strcoll ( const char *  str0,
const char *  str1 
)

Wrapper function for strcoll().

Definition at line 227 of file support.cpp.

◆ fc_strerror()

const char* fc_strerror ( fc_errno  err)

Return a string which describes a given error (errno-style.) The string is converted as necessary from the local_encoding to internal_encoding, for inclusion in translations.

May be subsequently converted back to local_encoding for display.

Note that this is not the reentrant form.

Definition at line 328 of file support.cpp.

Referenced by main().

◆ fc_stricoll()

int fc_stricoll ( const char *  str0,
const char *  str1 
)

Wrapper function for stricoll().

Definition at line 235 of file support.cpp.

Referenced by cmp_name().

◆ fc_strlcat()

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-friendly version of strncat(), in particular easier to use safely and correctly, and ensuring nul-terminated results while being able to detect truncation.

Definition at line 448 of file support.cpp.

Referenced by boot_help_texts(), get_effect_req_text(), handle_page_msg_part(), handle_ruleset_description_part(), helptext_building(), helptext_goods(), helptext_government(), helptext_specialist(), historian_generic(), load_ruleset_nations(), load_ruleset_units(), req_text_insert(), req_text_insert_nl(), setting_bitwise_to_str(), tile_info_pollution(), tileset_help(), and universal_name_translation().

◆ fc_strlcpy()

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-friendly version of strncpy(), in particular easier to use safely and correctly, and ensuring nul-terminated results while being able to detect truncation.

n is the full size of the destination buffer, including space for trailing nul, and including the pre-existing string for fc_strlcat(). Thus can eg use sizeof(buffer), or exact size malloc-ed.

Result is always nul-terminated, whether or not truncation occurs, and the return value is the qstrlen the destination would have had without truncation. I.e., a return value >= input n indicates truncation occurred.

Not sure about the asserts below, but they are easier than trying to ensure correct behaviour on strange inputs. In particular note that n == 0 is prohibited (e.g., since there must at least be room for a nul); could consider other options.

Definition at line 412 of file support.cpp.

Referenced by capitalized_string(), client_option_str_set(), cmafec_preset_add(), conn_pattern_from_string(), edit_buffer_get_status_string(), fc_gethostname(), fc_strlcat(), find_option(), fit_nationset_to_players(), get_city_dialog_production(), get_city_mapview_name_and_growth(), get_full_nation(), get_full_username(), get_unique_guest_name(), handle_chat_msg_req(), handle_ruleset_nation(), handle_ruleset_summary(), helptext_advance(), helptext_unit(), is_allowed_to_take(), load_ruleset_game(), loud_strlcpy(), mapimg_def2str(), mapimg_define(), name_and_sort_items(), package_short_unit(), package_unit(), plrdata_slot_replace(), research_pretty_name(), scan_score_log(), secfile_lookup_bitwise_enum_default_full(), secfile_lookup_bitwise_enum_full(), secfile_lookup_enum_data(), secfile_lookup_enum_default_data(), send_ruleset_choices(), server_player_name_is_allowed(), server_player_set_name_full(), settable_options_load(), setting_bitwise_to_str(), setting_bitwise_validate_base(), setting_bool_to_str(), setting_enum_to_str(), setting_game_set(), setting_set_to_default(), setting_str_set(), setting_str_to_str(), settings_game_load(), sg_save_map_startpos(), page_network::slot_connect(), mr_menu::slot_set_citybar(), tileset_read_toplevel(), try_to_connect(), and user_username().

◆ fc_strncasecmp()

int fc_strncasecmp ( const char *  str0,
const char *  str1,
size_t  n 
)

◆ fc_strncasequotecmp()

int fc_strncasequotecmp ( const char *  str0,
const char *  str1,
size_t  n 
)

Compare strings like strncasecmp() but ignoring surrounding quotes in either string.

Definition at line 209 of file support.cpp.

Referenced by conn_by_user_prefix(), and player_by_name_prefix().

◆ fc_strrep()

bool fc_strrep ( char *  str,
size_t  len,
const char *  search,
const char *  replace 
)

Replace 'search' by 'replace' within 'str'.

sizeof(str) should be large enough for the modified value of 'str'. Returns TRUE if the replacement was successful.

Definition at line 356 of file support.cpp.

◆ fc_usleep()

void fc_usleep ( unsigned long  usec)

Suspend execution for the specified number of microseconds.

Definition at line 349 of file support.cpp.

Referenced by client_start_server(), move_unit_map_canvas(), and put_nuke_mushroom_pixmaps().

◆ fc_vsnprintf()

int fc_vsnprintf ( char *  str,
size_t  n,
const char *  format,
va_list  ap 
)

◆ make_escapes()

void make_escapes ( const char *  str,
char *  buf,
size_t  buf_len 
)

Copies a string and convert the following characters:

Definition at line 114 of file support.cpp.

Referenced by entry_to_file().

◆ real_fc_strdup()

char* real_fc_strdup ( const char *  str,
const char *  called_as,
int  line,
const char *  file 
)

Function used by fc_strdup macro, strdup() replacement No need to check return value.

Definition at line 75 of file support.cpp.

◆ remove_escapes()

QString remove_escapes ( const QString &  str,
bool  full_escapes 
)

Copies a string.

Backslash followed by a genuine newline always removes the newline. If full_escapes is TRUE:

  • \\n -> newline translation.
  • Other \\c sequences (any character c) are just passed through with the \\ removed (eg, includes \\, "). See also make_escapes().

Definition at line 149 of file support.cpp.

Referenced by entry_from_token().