Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/remove functions12 #1142

Merged
merged 2 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 0 additions & 224 deletions utility/registry_ini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2000,59 +2000,6 @@ int *secfile_lookup_int_vec(const struct section_file *secfile, size_t *dim,
return vec;
}

/**
Lookup a floating point value in the secfile. Returns TRUE on success.
*/
bool secfile_lookup_float(const struct section_file *secfile, float *fval,
const char *path, ...)
{
char fullpath[MAX_LEN_SECPATH];
const struct entry *pentry;
va_list args;

SECFILE_RETURN_VAL_IF_FAIL(secfile, nullptr, nullptr != secfile, false);

va_start(args, path);
fc_vsnprintf(fullpath, sizeof(fullpath), path, args);
va_end(args);

if (!(pentry = secfile_entry_by_path(secfile, fullpath))) {
SECFILE_LOG(secfile, nullptr, "\"%s\" entry doesn't exist.", fullpath);
return false;
}

return entry_float_get(pentry, fval);
}

/**
Lookup a floating point value in the secfile. On failure, use the default
value.
*/
float secfile_lookup_float_default(const struct section_file *secfile,
float def, const char *path, ...)
{
char fullpath[MAX_LEN_SECPATH];
const struct entry *pentry;
float fval;
va_list args;

SECFILE_RETURN_VAL_IF_FAIL(secfile, nullptr, nullptr != secfile, def);

va_start(args, path);
fc_vsnprintf(fullpath, sizeof(fullpath), path, args);
va_end(args);

if (!(pentry = secfile_entry_by_path(secfile, fullpath))) {
return def;
}

if (entry_float_get(pentry, &fval)) {
return fval;
}

return def;
}

/**
Lookup a string value in the secfile. Returns nullptr on error.
*/
Expand Down Expand Up @@ -2654,58 +2601,6 @@ int secfile_lookup_enum_default_data(const struct section_file *secfile,
return value;
}

/**
Lookup a vector in the secfile. Returns nullptr on error. This vector
is not owned by the registry module, and should be free by the user.
*/
int *secfile_lookup_enum_vec_data(const struct section_file *secfile,
size_t *dim, bool bitwise,
secfile_enum_name_data_fn_t name_fn,
secfile_data_t data, const char *path, ...)
{
char fullpath[MAX_LEN_SECPATH];
size_t i = 0;
int *vec;
va_list args;

SECFILE_RETURN_VAL_IF_FAIL(secfile, nullptr, nullptr != secfile, nullptr);
SECFILE_RETURN_VAL_IF_FAIL(secfile, nullptr, nullptr != dim, nullptr);
SECFILE_RETURN_VAL_IF_FAIL(secfile, nullptr, nullptr != name_fn, nullptr);

va_start(args, path);
fc_vsnprintf(fullpath, sizeof(fullpath), path, args);
va_end(args);

// Check size.
while (nullptr
!= secfile_entry_lookup(secfile, "%s,%d", fullpath,
static_cast<int>(i))) {
i++;
}
*dim = i;

if (0 == i) {
// Doesn't exist.
SECFILE_LOG(secfile, nullptr, "\"%s\" entry doesn't exist.", fullpath);
return nullptr;
}

vec = new int[i];
for (i = 0; i < *dim; i++) {
if (!secfile_lookup_enum_data(secfile, vec + i, bitwise, name_fn, data,
"%s,%d", fullpath, static_cast<int>(i))) {
SECFILE_LOG(secfile, nullptr,
"An error occurred when looking up to \"%s,%d\" entry.",
fullpath, (int) i);
delete[] vec;
*dim = 0;
return nullptr;
}
}

return vec;
}

/**
Returns the first section matching the name.
*/
Expand Down Expand Up @@ -2876,69 +2771,6 @@ void section_clear_all(struct section *psection)
}
}

/**
Change the section name. Returns TRUE on success.
*/
bool section_set_name(struct section *psection, const char *name)
{
struct section_file *secfile;
struct section *pother;

SECFILE_RETURN_VAL_IF_FAIL(nullptr, psection, nullptr != psection, false);
secfile = psection->secfile;
SECFILE_RETURN_VAL_IF_FAIL(secfile, psection, nullptr != secfile, false);

if (nullptr == name || '\0' == name[0]) {
SECFILE_LOG(secfile, psection, "No new name for section \"%s\".",
psection->name);
return false;
}

if (!is_secfile_entry_name_valid(name)) {
SECFILE_LOG(secfile, psection,
"\"%s\" is not a valid section name for section \"%s\".",
name, psection->name);
return false;
}

if ((pother = secfile_section_by_name(secfile, name))
&& pother != psection) {
// We cannot duplicate sections in any case!
SECFILE_LOG(secfile, psection, "Section \"%s\" already exists.", name);
return false;
}

// Remove old references in the hash tables.
if (nullptr != secfile->hash.sections) {
secfile->hash.sections->remove(psection->name);
}
if (nullptr != secfile->hash.entries) {
entry_list_iterate(psection->entries, pentry)
{
secfile_hash_delete(secfile, pentry);
}
entry_list_iterate_end;
}

// Really rename.
free(psection->name);
psection->name = fc_strdup(name);

// Reinsert new references into the hash tables.
if (nullptr != secfile->hash.sections) {
secfile->hash.sections->insert(psection->name, psection);
}
if (nullptr != secfile->hash.entries) {
entry_list_iterate(psection->entries, pentry)
{
secfile_hash_insert(secfile, pentry);
}
entry_list_iterate_end;
}

return true;
}

/**
Returns a list containing all the entries. This list is owned by the
secfile, so don't modify or destroy it.
Expand Down Expand Up @@ -2969,37 +2801,6 @@ struct entry *section_entry_by_name(const struct section *psection,
return nullptr;
}

/**
Returns the entry matching the path.
*/
struct entry *section_entry_lookup(const struct section *psection,
const char *path, ...)
{
char fullpath[MAX_LEN_SECPATH];
struct entry *pentry;
va_list args;

SECFILE_RETURN_VAL_IF_FAIL(nullptr, psection, nullptr != psection,
nullptr);

va_start(args, path);
fc_vsnprintf(fullpath, sizeof(fullpath), path, args);
va_end(args);

if ((pentry = section_entry_by_name(psection, fullpath))) {
return pentry;
}

// Try with full path.
if ((pentry = secfile_entry_by_path(psection->secfile, fullpath))
&& psection == entry_section(pentry)) {
// Unsure this is really owned by this section.
return pentry;
}

return nullptr;
}

/**
Returns a new entry.
*/
Expand Down Expand Up @@ -3430,31 +3231,6 @@ bool entry_str_set(struct entry *pentry, const char *value)
return true;
}

/**
Returns if the string would be escaped.
*/
bool entry_str_escaped(const struct entry *pentry)
{
SECFILE_RETURN_VAL_IF_FAIL(nullptr, nullptr, nullptr != pentry, false);
SECFILE_RETURN_VAL_IF_FAIL(pentry->psection->secfile, pentry->psection,
ENTRY_STR == pentry->type, false);

return pentry->string.escaped;
}

/**
Sets if the string would be escaped. Returns TRUE on success.
*/
bool entry_str_set_escaped(struct entry *pentry, bool escaped)
{
SECFILE_RETURN_VAL_IF_FAIL(nullptr, nullptr, nullptr != pentry, false);
SECFILE_RETURN_VAL_IF_FAIL(pentry->psection->secfile, pentry->psection,
ENTRY_STR == pentry->type, false);

pentry->string.escaped = escaped;
return true;
}

/**
Sets if the string should get gettext marking. Returns TRUE on success.
*/
Expand Down
19 changes: 0 additions & 19 deletions utility/registry_ini.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,6 @@ int *secfile_lookup_int_vec(const struct section_file *secfile, size_t *dim,
const char *path, ...) fc__warn_unused_result
fc__attribute((__format__(__printf__, 3, 4)));

bool secfile_lookup_float(const struct section_file *secfile, float *fval,
const char *path, ...) fc__warn_unused_result
fc__attribute((__format__(__printf__, 3, 4)));
float secfile_lookup_float_default(const struct section_file *secfile,
float def, const char *path, ...);

const char *secfile_lookup_str(const struct section_file *secfile,
const char *path, ...) fc__warn_unused_result
fc__attribute((__format__(__printf__, 2, 3)));
Expand Down Expand Up @@ -495,12 +489,6 @@ int secfile_lookup_enum_default_data(const struct section_file *secfile,
secfile_data_t data, const char *path,
...) fc__warn_unused_result
fc__attribute((__format__(__printf__, 6, 7)));
int *secfile_lookup_enum_vec_data(const struct section_file *secfile,
size_t *dim, bool bitwise,
secfile_enum_name_data_fn_t name_fn,
secfile_data_t data, const char *path,
...) fc__warn_unused_result
fc__attribute((__format__(__printf__, 6, 7)));

// Sections functions.
struct section *secfile_section_by_name(const struct section_file *secfile,
Expand All @@ -520,15 +508,10 @@ struct section *secfile_section_new(struct section_file *secfile,
void section_destroy(struct section *psection);
void section_clear_all(struct section *psection);

bool section_set_name(struct section *psection, const char *section_name);

// Entry functions.
const struct entry_list *section_entries(const struct section *psection);
struct entry *section_entry_by_name(const struct section *psection,
const QString &entry_name);
struct entry *section_entry_lookup(const struct section *psection,
const char *path, ...)
fc__attribute((__format__(__printf__, 2, 3)));
struct entry *section_entry_int_new(struct section *psection,
const QString &entry_name, int value);
struct entry *section_entry_bool_new(struct section *psection,
Expand Down Expand Up @@ -573,6 +556,4 @@ bool entry_float_set(struct entry *pentry, float value);

bool entry_str_get(const struct entry *pentry, const char **value);
bool entry_str_set(struct entry *pentry, const char *value);
bool entry_str_escaped(const struct entry *pentry);
bool entry_str_set_escaped(struct entry *pentry, bool escaped);
bool entry_str_set_gt_marking(struct entry *pentry, bool gt_marking);
68 changes: 0 additions & 68 deletions utility/support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,25 +269,6 @@ FILE *fc_fopen(const char *filename, const char *opentype)
#endif // FREECIV_MSWINDOWS
}

/**
Wrapper function for gzopen() with filename conversion to local
encoding on Windows.
*/
gzFile fc_gzopen(const char *filename, const char *opentype)
{
#ifdef FREECIV_MSWINDOWS
gzFile result;
char *filename_in_local_encoding =
internal_to_local_string_malloc(filename);

result = gzopen(filename_in_local_encoding, opentype);
free(filename_in_local_encoding);
return result;
#else // FREECIV_MSWINDOWS
return gzopen(filename, opentype);
#endif // FREECIV_MSWINDOWS
}

/**
Wrapper function for remove() with filename conversion to local
encoding on Windows.
Expand Down Expand Up @@ -369,43 +350,6 @@ const char *fc_strerror(fc_errno err)
*/
void fc_usleep(unsigned long usec) { QThread::usleep(usec); }

/**
Replace 'search' by 'replace' within 'str'. If needed 'str' is resized
using realloc() to fit the modified string. The new pointer to the string
is returned.
*/
char *fc_strrep_resize(char *str, size_t *len, const char *search,
const char *replace)
{
size_t len_max;
bool success;

fc_assert_ret_val(str != nullptr, nullptr);
fc_assert_ret_val(len != nullptr, nullptr);
if (search == nullptr || replace == nullptr) {
return str;
}

len_max = ceil(static_cast<double>(qstrlen(str)) * qstrlen(replace)
/ qstrlen(search))
+ 1;
if ((*len) < len_max) {
/* replace string is longer than search string; allocated enough memory
* for the worst case */
(*len) = len_max;
str = static_cast<char *>(fc_realloc(str, len_max));
}

success = fc_strrep(str, (*len), search, replace);
// should never happen
fc_assert_ret_val_msg(success == true, nullptr,
"Can't replace '%s' by '%s' in '%s'. To small "
"size after reallocation: %lu.",
search, replace, str, (long unsigned int) *len);

return str;
}

/**
Replace 'search' by 'replace' within 'str'. sizeof(str) should be large
enough for the modified value of 'str'. Returns TRUE if the replacement
Expand Down Expand Up @@ -721,18 +665,6 @@ int fc_break_lines(char *str, size_t desired_len)
part of a multibyte sequence is non-ASCII.
****************************************************************************/

/**
basename() replacement that always takes const parameter.
POSIX basename() modifies its parameter, GNU one does not.
Ideally we would like to use GNU one, when available, directly
without extra string copies.
*/
const char *fc_basename(const char *path)
{
QFileInfo fi(path);
return fc_strdup(fi.fileName().toUtf8().constData());
}

/**
Set quick_exit() callback if possible.
*/
Expand Down
Loading