Freeciv-3.2
|
#include <freeciv_config.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "log.h"
#include "support.h"
#include "speclist.h"
Go to the source code of this file.
Data Structures | |
struct | fileinfo |
struct | cf_sequence |
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 } |
#define fileinfo_list_iterate_end LIST_ITERATE_END |
Randomize the elements of an array using the Fisher-Yates shuffle.
See: http://benpfaff.org/writings/clc/shuffle.html
Definition at line 2010 of file shared.c.
Referenced by trade_generator::calculate(), genlist_shuffle(), map_generate_fair_islands(), and shuffle_players().
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 159 of file shared.c.
Referenced by int_to_text(), and population_to_text().
|
inlinestatic |
Build an argument for fc_snprintcf() of boolean type.
|
inlinestatic |
Build an argument for fc_snprintcf() of character type (c).
|
inlinestatic |
Must finish the list of the arguments of fc_snprintcf().
Definition at line 425 of file shared.h.
Referenced by generate_save_name().
|
inlinestatic |
Build an argument for fc_snprintcf() of float type (f).
|
inlinestatic |
Build an argument for fc_snprintcf() of hexadecimal type (x).
Build an argument for fc_snprintcf() of integer type (d).
Definition at line 359 of file shared.h.
Referenced by generate_save_name().
Build an argument for fc_snprintcf() of pointer type (p).
Build an argument for fc_snprintcf() of string type (s).
Definition at line 411 of file shared.h.
Referenced by generate_save_name().
|
inlinestatic |
Build an argument for fc_snprintcf() of boolean type (result will be translated).
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 495 of file shared.c.
Referenced by load_ruleset_nations(), and loud_strlcpy().
Compares two strings, in the collating order of the current locale, given pointers to the two strings (i.e., given "char *"s). Case-sensitive. Designed to be called from qsort().
Definition at line 359 of file shared.c.
Referenced by help_item_compar().
Compares two strings, in the collating order of the current locale, given pointers to the two string pointers (i.e., given "char **"s). Case-sensitive. Designed to be called from qsort().
Definition at line 369 of file shared.c.
Referenced by packet_handlers_get().
Compares two strings, in the collating order of the current locale, given pointers to the two string pointers. Case-sensitive. Designed to be called from strvec_sort().
Definition at line 379 of file shared.c.
Referenced by fileinfolist(), and report_wonders_of_the_world_long().
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 NULL. 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 1549 of file shared.c.
Referenced by client_main(), and main().
Returns pointer to '\0' at end of string 'str', and decrements nleft by the length of 'str'. This is intended to be useful to allow strcat-ing without traversing the whole string each time, while still keeping track of the buffer length. Eg: char buf[128]; int n = sizeof(buf); char *p = buf;
fc_snprintf(p, n, "foo%p", p); p = end_of_strn(p, &n); fc_strlcpy(p, "yyy", n);
enum fc_tristate fc_tristate_and | ( | enum fc_tristate | one, |
enum fc_tristate | two | ||
) |
An AND function for fc_tristate.
Definition at line 124 of file shared.c.
Referenced by action_enabled_local(), and action_prob().
enum fc_tristate fc_tristate_or | ( | enum fc_tristate | one, |
enum fc_tristate | two | ||
) |
An OR function for fc_tristate.
Definition at line 140 of file shared.c.
Referenced by is_diplrel_unitany_in_range().
int int fc_vsnprintcf | ( | char * | buf, |
size_t | buf_len, | ||
const char * | format, | ||
const struct cf_sequence * | sequences, | ||
size_t | sequences_num | ||
) |
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 1027 of file shared.c.
Referenced by get_audio_speclist(), get_init_script_choices(), get_theme_list(), get_tileset_list(), themespec_try_read(), and tilespec_try_read().
struct fileinfo_list * fileinfolist_infix | ( | const struct strvec * | dirs, |
const char * | infix, | ||
bool | nodups | ||
) |
Search for filenames with the "infix" substring in the "subpath" subdirectory of the data path. "nodups" removes duplicate names. The returned list will be sorted by name first and modification time second. Returned "name"s will be truncated starting at the "infix" substring. The returned list must be freed with fileinfo_list_destroy().
Definition at line 1211 of file shared.c.
Referenced by get_modpacks_list(), popup_load_game_dialog(), save_dialog_mapimg_list(), save_dialog_savegame_list(), save_dialog_scenario_list(), show_scenarios(), update_load_page(), fc_client::update_load_page(), update_scenario_page(), and fc_client::update_scenarios_page().
Returns a filename to access the specified file from a directory by searching all specified directories for the file.
If the specified 'filename' is NULL, the returned string contains the effective path. (But this should probably only be used for debug output.)
Returns NULL if the specified filename cannot be found in any of the data directories. (A file is considered "found" if it can be read-opened.) The returned pointer points to static memory, so this function can only supply one filename at a time. Don't free that pointer.
TODO: Make this re-entrant
Definition at line 1101 of file shared.c.
Referenced by audio_play_tag(), audiospec_fullname(), boot_help_texts(), comments_load(), datafilename(), fc_icons::get_icon(), fc_icons::get_path(), fc_icons::get_pixmap(), get_token_value(), get_ui_filename(), load_command(), load_gfx_file(), load_sdl2_gfx_file(), load_sdl3_gfx_file(), lua_command(), main(), read_init_script_real(), ruledit_main::ruledit_main(), set_rulesetdir(), sg_load_savefile(), srv_prepare(), theme_read_toplevel(), themespec_gfx_filename(), tileset_scan_single_list(), tilespec_fullname(), tilespec_gfx_filename(), and valid_ruleset_filename().
Convenience function to nicely format a time_t seconds value in to a string with hours, minutes, etc.
Definition at line 1967 of file shared.c.
Referenced by unit_can_do_action_now().
Returns TRUE iff both formats are compatible (if 'format1' can be used instead 'format2' and reciprocally).
Definition at line 2445 of file shared.c.
Referenced by ruler_title_check(), and sanity_check_ruleset_data().
Free data dir name vectors.
Definition at line 866 of file shared.c.
Referenced by libfreeciv_free().
Free resources allocated for fileinfoname service
Definition at line 1158 of file shared.c.
Referenced by libfreeciv_free().
Free freeciv storage directory information
Definition at line 696 of file shared.c.
Referenced by libfreeciv_free().
Free multicast group resources
Definition at line 1705 of file shared.c.
Referenced by libfreeciv_free().
Free memory allocated by Native Language Support
Definition at line 1532 of file shared.c.
Referenced by libfreeciv_free().
Free user home directory information
Definition at line 665 of file shared.c.
Referenced by libfreeciv_free().
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 678 of file shared.c.
Referenced by client_start_server(), fcmp_parse_cmdline(), get_challenge_fullname(), mr_menu::save_image(), send_client_wants_hack(), server_sniff_all_input(), and tab_misc::tab_misc().
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 893 of file shared.c.
Referenced by audio_play_tag(), audiospec_fullname(), boot_help_texts(), comments_load(), datafilename(), get_audio_speclist(), get_gui_specific_themes_directories(), fc_icons::get_icon(), get_init_script_choices(), get_modpacks_list(), fc_icons::get_path(), fc_icons::get_pixmap(), get_theme_list(), get_tileset_list(), get_token_value(), get_ui_filename(), load_gfx_file(), load_sdl2_gfx_file(), load_sdl3_gfx_file(), lua_command(), main(), qtg_get_gui_specific_themes_directories(), read_init_script_real(), ruledit_main::ruledit_main(), set_rulesetdir(), srv_prepare(), theme_read_toplevel(), themespec_gfx_filename(), themespec_try_read(), tileset_scan_single_list(), tilespec_fullname(), tilespec_gfx_filename(), tilespec_try_read(), and valid_ruleset_filename().
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 1674 of file shared.c.
Referenced by begin_lanserver_scan(), send_lanserver_response(), and server_open_socket().
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 941 of file shared.c.
Referenced by load_command(), popup_load_game_dialog(), save_dialog_mapimg_list(), save_dialog_savegame_list(), mr_menu::save_game_as(), update_load_page(), and fc_client::update_load_page().
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 978 of file shared.c.
Referenced by load_command(), save_dialog_scenario_list(), sg_load_savefile(), show_scenarios(), update_scenario_page(), and fc_client::update_scenarios_page().
Setup for Native Language Support, if configured to use it. (Call this only once, or it may leak memory.)
Definition at line 1470 of file shared.c.
Referenced by libfreeciv_init().
Return a prettily formatted string containing the given number.
Definition at line 234 of file shared.c.
Referenced by value_units().
Interpret ~/ in filename as home dir New path is returned in buf of size buf_size
This may fail if the path is too long. It is better to use interpret_tilde_alloc().
Definition at line 1720 of file shared.c.
Referenced by lua_command(), read_init_script_real(), secfile_load_section(), secfile_save(), and write_init_script().
Interpret ~/ in filename as home dir
The new path is returned in buf, as a newly allocated buffer. The new path will always be allocated and written, even if there is no ~ present.
Definition at line 1737 of file shared.c.
Referenced by make_dir().
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 283 of file shared.c.
Referenced by is_allowed_city_name(), is_good_password(), is_valid_username(), server_player_name_is_allowed(), and user_username().
Check for valid base64url.
Definition at line 318 of file shared.c.
Referenced by sg_load_sanitycheck(), and sg_load_sanitycheck().
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 253 of file shared.c.
Referenced by api_server_save(), load_command(), lua_command(), read_init_script_real(), savename_validate(), scorefile_validate(), send_client_wants_hack(), and set_rulesetdir().
Call check_strlen() on str and then strlcpy() it into buffer.
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 1571 of file shared.c.
Referenced by cmd_reply_no_such_conn(), and cmd_reply_no_such_player().
If the directory "pathname" does not exist, recursively create all directories until it does.
TODO: Make errno available after a failure, preferably via fc_get_errno(). Currently there's things potentially messing errno between failed mkdir() and the function return.
Definition at line 1782 of file shared.c.
Referenced by create_mpdb(), img_save(), make_dir_for_file(), save_game(), save_ruleset(), send_client_wants_hack(), and server_sniff_all_input().
If the directory part of the "filename" does not exist, recursively create all directories until it does.
Definition at line 1861 of file shared.c.
Referenced by download_modpack_recursive().
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().
Definition at line 1590 of file shared.c.
Referenced by command_named(), conn_by_user_prefix(), delegate_command(), fcdb_command(), lookup_option(), lua_command(), mapimg_command(), player_by_name_prefix(), reset_command(), show_list(), and vote_command().
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-NULL, up to 'max_matches' ambiguous matching names indices will be inserted into it. If 'pnum_matches' is non-NULL, it will be set to the number of indices inserted into 'matches'.
Definition at line 1613 of file shared.c.
Referenced by check_player_or_user_name(), match_prefix(), and show_help().
Returns TRUE if the filename's path is absolute.
Definition at line 1883 of file shared.c.
Referenced by img_save(), and save_game().
Generate a random string meeting criteria such as is_ascii_name(), is_base64url(), and is_safe_filename().
Definition at line 339 of file shared.c.
Referenced by init_new_game(), sg_load_sanitycheck(), and sg_load_sanitycheck().
Removes leading spaces in string pointed to by 's'. Note 's' must point to writeable memory!
Definition at line 403 of file shared.c.
Referenced by handle_stdin_input_real(), and remove_leading_trailing_spaces().
Removes leading and trailing spaces in string pointed to by 's'. Note 's' must point to writeable memory!
Definition at line 444 of file shared.c.
Referenced by cancelvote_command(), conn_pattern_from_string(), explain_option(), handle_login_request(), kick_command(), load_city_name_list(), lookup_string(), scanin(), secfile_lookup_bitwise_enum_default_full(), secfile_lookup_bitwise_enum_full(), secfile_lookup_enum_data(), secfile_lookup_enum_default_data(), server_player_set_name_full(), setting_bitwise_validate_base(), setting_bool_validate_base(), setting_enum_validate_base(), show_list(), show_settings(), strvec_remove_empty(), and unignore_command().
Terminates string pointed to by 's' to remove trailing spaces; Note 's' must point to writeable memory!
Definition at line 421 of file shared.c.
Referenced by expand_dir(), handle_stdin_input_real(), and remove_leading_trailing_spaces().
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 NULL if there is nothing there. Removes excess white space.
This function should be safe, and dest will contain "\0" and buf == NULL 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 NULL 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 1922 of file shared.c.
Referenced by sg_load_map_owner(), sg_load_map_owner(), sg_load_map_worked(), sg_load_map_worked(), sg_load_player_vision(), and sg_load_player_vision().
Language environmental variable (with emulation).
Definition at line 1288 of file shared.c.
Referenced by init_nls(), and theme_read_toplevel().
Returns 's' incremented to first non-space character.
Definition at line 388 of file shared.c.
Referenced by aicmd_command(), chat_msg_to_all(), chat_msg_to_allies(), chat_msg_to_conn(), chat_msg_to_global_observers(), chat_msg_to_player(), expand_dir(), fcdb_command(), handle_stdin_input_real(), lua_command(), and remove_leading_spaces().
Return a pointer to the start of the file basename in filepath. If the string contains no dir separator, it is returned itself.
Definition at line 1760 of file shared.c.
Referenced by scenario_list_callback().
Convert 'str' to its float representation if possible. 'pfloat' can be NULL, then it will only test 'str' only contains a floating point number.
Definition at line 584 of file shared.c.
Referenced by entry_from_token().
Convert 'str' to its int representation if possible. 'pint' can be NULL, then it will only test 'str' only contains an integer number.
Definition at line 517 of file shared.c.
Referenced by cancelvote_command(), client_main(), debug_command(), entry_from_token(), main(), main(), option_widget_apply(), re_parse_cmdline(), rup_parse_cmdline(), set_command(), sg_load_map_owner(), sg_load_map_owner(), sg_load_map_worked(), sg_load_map_worked(), sg_load_player_vision(), sg_load_player_vision(), team_command(), text_tag_init_from_sequence(), timeout_command(), unignore_command(), and vote_command().
Convert 'str' to its unsigned int representation if possible. 'pint' can be NULL, then it will only test 'str' only contains an unsigned integer number.
Definition at line 551 of file shared.c.
Referenced by log_parse_level_str(), and parse_options().
Returns string which gives users home dir, as specified by $HOME. Gets value once, and then caches result. If $HOME is not set, give a log message and returns NULL. Note the caller should not mess with the returned string.
Definition at line 634 of file shared.c.
Referenced by expand_dir(), get_gui_specific_themes_directories(), interpret_tilde(), and interpret_tilde_alloc().
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 711 of file shared.c.
Referenced by client_main().
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 2155 of file shared.c.
Referenced by conn_pattern_match(), and wildcard_asterisk_fit().