diff --git a/utility/shared.cpp b/utility/shared.cpp index 5ec055b974..b7c3f170cd 100644 --- a/utility/shared.cpp +++ b/utility/shared.cpp @@ -380,28 +380,6 @@ void remove_leading_trailing_spaces(char *s) remove_trailing_spaces(s); } -/** - 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); - */ -char *end_of_strn(char *str, int *nleft) -{ - int len = qstrlen(str); - *nleft -= len; - fc_assert_ret_val(0 < (*nleft), nullptr); // space for the terminating nul - return str + len; -} - /** 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 @@ -457,89 +435,6 @@ bool str_to_int(const char *str, int *pint) && (nullptr == pint || 1 == sscanf(start, "%d", pint))); } -/** - Convert 'str' to it's unsigned int reprentation if possible. 'pint' can be - nullptr, then it will only test 'str' only contains an unsigned integer - number. - */ -bool str_to_uint(const char *str, unsigned int *pint) -{ - const char *start; - - fc_assert_ret_val(nullptr != str, false); - - while (QChar::isSpace(*str)) { - // Skip leading spaces. - str++; - } - - start = str; - if ('+' == *str) { - // Handle sign. - str++; - } - while (QChar::isDigit(*str)) { - // Digits. - str++; - } - - while (QChar::isSpace(*str)) { - // Ignore trailing spaces. - str++; - } - - return ('\0' == *str - && (nullptr == pint || 1 == sscanf(start, "%u", pint))); -} - -/** - Convert 'str' to it's float reprentation if possible. 'pfloat' can be - nullptr, then it will only test 'str' only contains a floating point number. - */ -bool str_to_float(const char *str, float *pfloat) -{ - bool dot; - const char *start; - - fc_assert_ret_val(nullptr != str, false); - - while (QChar::isSpace(*str)) { - // Skip leading spaces. - str++; - } - - start = str; - - if ('-' == *str || '+' == *str) { - // Handle sign. - str++; - } - while (QChar::isDigit(*str)) { - // Digits. - str++; - } - - if (*str == '.') { - dot = true; - str++; - - while (QChar::isDigit(*str)) { - // Digits. - str++; - } - } else { - dot = false; - } - - while (QChar::isSpace(*str)) { - // Ignore trailing spaces. - str++; - } - - return ('\0' == *str && dot - && (nullptr == pfloat || 1 == sscanf(start, "%f", pfloat))); -} - /** Returns string which gives freeciv storage dir. Gets value once, and then caches result. @@ -1085,25 +980,6 @@ static void autocap_update(void) } #endif // FREECIV_ENABLE_NLS -/** - Switch to specified LANG - */ -void switch_lang(const char *lang) -{ -#ifdef FREECIV_ENABLE_NLS - qputenv("LANG", lang); - - (void) setlocale(LC_ALL, ""); - (void) bindtextdomain("freeciv21-core", get_locale_dir()); - - autocap_update(); - - qInfo("LANG set to %s", lang); -#else // FREECIV_ENABLE_NLS - fc_assert(false); -#endif // FREECIV_ENABLE_NLS -} - /** Setup for Native Language Support, if configured to use it. (Call this only once, or it may leak memory.) @@ -1378,23 +1254,6 @@ char *interpret_tilde_alloc(const char *filename) } } -/** - Return a pointer to the start of the file basename in filepath. - If the string contains no dir separator, it is returned itself. - */ -char *skip_to_basename(char *filepath) -{ - int j; - fc_assert_ret_val(nullptr != filepath, nullptr); - - for (j = qstrlen(filepath); j >= 0; j--) { - if (filepath[j] == '/') { - return &filepath[j + 1]; - } - } - return filepath; -} - /** If the directory "pathname" does not exist, recursively create all directories until it does. @@ -1868,48 +1727,6 @@ int fc_vsnprintcf(char *buf, size_t buf_len, const char *format, return b - buf; } -/** - Print a string with a custom format. The additional arguments are a suite - of cf_*_seq() finished by cf_end(). This return the number of printed - characters (excluding the last '\0') or -1 if the buffer is full. - - Example: - char buf[256]; - - fc_snprintcf(buf, sizeof(buf), "%y %+06y", - cf_int_seq('y', 2010), cf_end()); - // This will print "2010 +02010" into buf. - */ -int fc_snprintcf(char *buf, size_t buf_len, const char *format, ...) -{ - struct cf_sequence sequences[16]; - size_t sequences_num = 0; - va_list args; - - // Collect sequence array. - va_start(args, format); - do { - sequences[sequences_num] = va_arg(args, struct cf_sequence); - if (CF_LAST == sequences[sequences_num].type) { - break; - } else { - sequences_num++; - } - } while (ARRAY_SIZE(sequences) > sequences_num); - - if (ARRAY_SIZE(sequences) <= sequences_num - && CF_LAST != va_arg(args, struct cf_sequence).type) { - qCritical("Too many custom sequences. Maybe did you forget cf_end() " - "at the end of the arguments?"); - buf[0] = '\0'; - va_end(args); - return -1; - } - va_end(args); - - return fc_vsnprintcf(buf, buf_len, format, sequences, sequences_num); -} - /** Extract the sequences of a format. Returns the number of extracted escapes. diff --git a/utility/shared.h b/utility/shared.h index 0f7abdd4c0..77cd290daf 100644 --- a/utility/shared.h +++ b/utility/shared.h @@ -122,11 +122,7 @@ size_t loud_strlcpy(char *buffer, const char *str, size_t len, #define sz_loud_strlcpy(buffer, str, errmsg) \ loud_strlcpy(buffer, str, sizeof(buffer), errmsg) -char *end_of_strn(char *str, int *nleft); - bool str_to_int(const char *str, int *pint); -bool str_to_uint(const char *str, unsigned int *pint); -bool str_to_float(const char *str, float *pfloat); /************************************************************************** ... @@ -159,7 +155,6 @@ QString fileinfoname(const QStringList &dirs, const char *filename); void init_nls(); void free_nls(); char *setup_langname(); -void switch_lang(const char *lang); void dont_run_as_root(const char *argv0, const char *fallback); @@ -202,7 +197,6 @@ char *get_multicast_group(bool ipv6_preferred); void free_multicast_group(); void interpret_tilde(char *buf, size_t buf_size, const QString &filename); char *interpret_tilde_alloc(const char *filename); -char *skip_to_basename(char *filepath); bool make_dir(const char *pathname); bool path_is_absolute(const char *filename); @@ -218,21 +212,12 @@ bool wildcard_fit_string(const char *pattern, const char *test); // Custom format strings. struct cf_sequence; -int fc_snprintcf(char *buf, size_t buf_len, const char *format, ...) - fc__attribute((nonnull(1, 3))); // Not a printf format. 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, 3, 4))); -// Tools for fc_snprintcf(). -static inline struct cf_sequence cf_bool_seq(char letter, bool value); -static inline struct cf_sequence cf_trans_bool_seq(char letter, bool value); -static inline struct cf_sequence cf_char_seq(char letter, char value); static inline void cf_int_seq(char letter, int value, struct cf_sequence *out); -static inline struct cf_sequence cf_hexa_seq(char letter, int value); -static inline struct cf_sequence cf_float_seq(char letter, float value); -static inline struct cf_sequence cf_ptr_seq(char letter, const void *value); static inline struct cf_sequence cf_str_seq(char letter, const char *value); static inline struct cf_sequence cf_end(); @@ -262,49 +247,6 @@ struct cf_sequence { }; }; -/**************************************************************************** - Build an argument for fc_snprintcf() of boolean type. -****************************************************************************/ -static inline struct cf_sequence cf_bool_seq(char letter, bool value) -{ - struct cf_sequence sequence; - - sequence.type = CF_BOOLEAN; - sequence.letter = letter; - sequence.bool_value = value; - - return sequence; -} - -/**************************************************************************** - Build an argument for fc_snprintcf() of boolean type (result will be - translated). -****************************************************************************/ -static inline struct cf_sequence cf_trans_bool_seq(char letter, bool value) -{ - struct cf_sequence sequence; - - sequence.type = CF_TRANS_BOOLEAN; - sequence.letter = letter; - sequence.bool_value = value; - - return sequence; -} - -/**************************************************************************** - Build an argument for fc_snprintcf() of character type (%c). -****************************************************************************/ -static inline struct cf_sequence cf_char_seq(char letter, char value) -{ - struct cf_sequence sequence; - - sequence.type = CF_CHARACTER; - sequence.letter = letter; - sequence.char_value = value; - - return sequence; -} - /**************************************************************************** Build an argument for fc_snprintcf() of integer type (%d). ****************************************************************************/ @@ -316,48 +258,6 @@ static inline void cf_int_seq(char letter, int value, out->int_value = value; } -/**************************************************************************** - Build an argument for fc_snprintcf() of hexadecimal type (%x). -****************************************************************************/ -static inline struct cf_sequence cf_hexa_seq(char letter, int value) -{ - struct cf_sequence sequence; - - sequence.type = CF_HEXA; - sequence.letter = letter; - sequence.int_value = value; - - return sequence; -} - -/**************************************************************************** - Build an argument for fc_snprintcf() of float type (%f). -****************************************************************************/ -static inline struct cf_sequence cf_float_seq(char letter, float value) -{ - struct cf_sequence sequence; - - sequence.type = CF_FLOAT; - sequence.letter = letter; - sequence.float_value = value; - - return sequence; -} - -/**************************************************************************** - Build an argument for fc_snprintcf() of pointer type (%p). -****************************************************************************/ -static inline struct cf_sequence cf_ptr_seq(char letter, const void *value) -{ - struct cf_sequence sequence; - - sequence.type = CF_POINTER; - sequence.letter = letter; - sequence.ptr_value = value; - - return sequence; -} - /**************************************************************************** Build an argument for fc_snprintcf() of string type (%s). ****************************************************************************/