wsutil: Add config_file_exists_with_entries()

The purpose of this function is to check if a configuration file exists
and has at least one entry which is not a comment.

Use this when building the list of profiles where the user can copy
configuration from, to avoid listing profiles with empty files or files
with only comments.

Change-Id: If45f52025959818fb1213ffac488cd59441e9fce
Reviewed-on: https://code.wireshark.org/review/30113
Petri-Dish: Stig Bjørlykke <stig@bjorlykke.org>
Tested-by: Petri Dish Buildbot
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
This commit is contained in:
Stig Bjørlykke 2018-10-10 14:33:40 +02:00 committed by Peter Wu
parent b6d182859e
commit 19153cf911
4 changed files with 38 additions and 1 deletions

View File

@ -10,6 +10,7 @@ libwsutil.so.0 libwsutil0 #MINVER#
cmdarg_err@Base 1.99.0
cmdarg_err_cont@Base 1.99.0
cmdarg_err_init@Base 1.99.0
config_file_exists_with_entries@Base 2.9.0
copy_file_binary_mode@Base 1.12.0~rc1
copy_persconffile_profile@Base 1.12.0~rc1
crc11_307_noreflect_noxor@Base 1.10.0

View File

@ -25,7 +25,7 @@ CopyFromProfileMenu::CopyFromProfileMenu(QString filename) :
profile_def *profile = (profile_def *) fl_entry->data;
char *profile_dir = get_profile_dir(profile->name, profile->is_global);
char *file_name = g_build_filename(profile_dir, filename_.toUtf8().constData(), NULL);
if (file_exists(file_name) && strcmp(profile_name, profile->name) != 0) {
if ((strcmp(profile_name, profile->name) != 0) && config_file_exists_with_entries(file_name, '#')) {
if (profile->is_global && !globals_started) {
if (have_profiles_) {
addSeparator();

View File

@ -2024,6 +2024,36 @@ file_exists(const char *fname)
}
}
gboolean config_file_exists_with_entries(const char *fname, char comment_char)
{
gboolean start_of_line = TRUE;
gboolean has_entries = FALSE;
FILE *file;
int c;
if (!fname) {
return FALSE;
}
if ((file = ws_fopen(fname, "r")) == NULL) {
return FALSE;
}
do {
c = ws_getc_unlocked(file);
if (start_of_line && c != comment_char && !g_ascii_isspace(c) && g_ascii_isprint(c)) {
has_entries = TRUE;
break;
}
if (c == '\n' || !g_ascii_isspace(c)) {
start_of_line = (c == '\n');
}
} while (c != EOF);
fclose(file);
return has_entries;
}
/*
* Check that the from file is not the same as to file
* We do it here so we catch all cases ...

View File

@ -286,6 +286,12 @@ WS_DLL_PUBLIC int test_for_fifo(const char *);
*/
WS_DLL_PUBLIC gboolean file_exists(const char *fname);
/*
* Check if file is existing and has text entries which does not start
* with the comment character.
*/
WS_DLL_PUBLIC gboolean config_file_exists_with_entries(const char *fname, char comment_char);
/*
* Check if two filenames are identical (with absolute and relative paths).
*/