From 8b6f4ef2c275935ef1f0478bce9b3ee2bd70cf93 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sat, 20 Jan 2018 10:55:37 +0300 Subject: [PATCH] Use gboolean instead of int in file/dir sort related functions. Signed-off-by: Andrew Borodin --- lib/global.h | 2 +- lib/strutil.h | 32 ++++++++++++++++---------------- lib/strutil/strutil.c | 16 ++++++++-------- lib/strutil/strutil8bit.c | 29 +++++++++++++---------------- lib/strutil/strutilascii.c | 24 ++++++++++++------------ lib/strutil/strutilutf8.c | 23 +++++++++++------------ src/filemanager/dir.c | 4 ++-- src/setup.c | 18 ++++++++++-------- 8 files changed, 73 insertions(+), 75 deletions(-) diff --git a/lib/global.h b/lib/global.h index 1ab3ee0ef..96b20f2e3 100644 --- a/lib/global.h +++ b/lib/global.h @@ -134,7 +134,7 @@ #define TMPDIR_DEFAULT "/tmp" #define SCRIPT_SUFFIX "" #define get_default_editor() "vi" -#define OS_SORT_CASE_SENSITIVE_DEFAULT 1 +#define OS_SORT_CASE_SENSITIVE_DEFAULT TRUE #define UTF8_CHAR_LEN 6 /* Used to distinguish between a normal MC termination and */ diff --git a/lib/strutil.h b/lib/strutil.h index 4683850ee..4b3e2a207 100644 --- a/lib/strutil.h +++ b/lib/strutil.h @@ -136,20 +136,20 @@ struct str_class /*I*/ const char *(*trunc) (const char *, int); /*I*/ int (*offset_to_pos) (const char *, size_t); /*I*/ int (*column_to_pos) (const char *, size_t); - /*I*/ char *(*create_search_needle) (const char *, int); - void (*release_search_needle) (char *, int); - const char *(*search_first) (const char *, const char *, int); - const char *(*search_last) (const char *, const char *, int); + /*I*/ char *(*create_search_needle) (const char *, gboolean); + void (*release_search_needle) (char *, gboolean); + const char *(*search_first) (const char *, const char *, gboolean); + const char *(*search_last) (const char *, const char *, gboolean); int (*compare) (const char *, const char *); /*I*/ int (*ncompare) (const char *, const char *); /*I*/ int (*casecmp) (const char *, const char *); /*I*/ int (*ncasecmp) (const char *, const char *); /*I*/ int (*prefix) (const char *, const char *); /*I*/ int (*caseprefix) (const char *, const char *); - /*I*/ char *(*create_key) (const char *text, int case_sen); - /*I*/ char *(*create_key_for_filename) (const char *text, int case_sen); - /*I*/ int (*key_collate) (const char *t1, const char *t2, int case_sen); - /*I*/ void (*release_key) (char *key, int case_sen); + /*I*/ char *(*create_key) (const char *text, gboolean case_sen); + /*I*/ char *(*create_key_for_filename) (const char *text, gboolean case_sen); + /*I*/ int (*key_collate) (const char *t1, const char *t2, gboolean case_sen); + /*I*/ void (*release_key) (char *key, gboolean case_sen); /*I*/}; /*** global variables defined in .c file *********************************************************/ @@ -455,19 +455,19 @@ const char *str_trunc (const char *text, int width); * so needle can be reused * in UTF-8 return normalized form of needle */ -char *str_create_search_needle (const char *needle, int case_sen); +char *str_create_search_needle (const char *needle, gboolean case_sen); /* free needle returned by str_create_search_needle */ -void str_release_search_needle (char *needle, int case_sen); +void str_release_search_needle (char *needle, gboolean case_sen); /* search for first occurrence of search in text */ -const char *str_search_first (const char *text, const char *needle, int case_sen); +const char *str_search_first (const char *text, const char *needle, gboolean case_sen); /* search for last occurrence of search in text */ -const char *str_search_last (const char *text, const char *needle, int case_sen); +const char *str_search_last (const char *text, const char *needle, gboolean case_sen); /* case sensitive compare two strings * I @@ -507,25 +507,25 @@ int str_caseprefix (const char *text, const char *prefix); /* create a key that is used by str_key_collate * I */ -char *str_create_key (const char *text, int case_sen); +char *str_create_key (const char *text, gboolean case_sen); /* create a key that is used by str_key_collate * should aware dot '.' in text * I */ -char *str_create_key_for_filename (const char *text, int case_sen); +char *str_create_key_for_filename (const char *text, gboolean case_sen); /* compare two string using LC_COLLATE, if is possible * if case_sen is set, comparing is case sensitive, * case_sen must be same for str_create_key, str_key_collate and str_release_key * I */ -int str_key_collate (const char *t1, const char *t2, int case_sen); +int str_key_collate (const char *t1, const char *t2, gboolean case_sen); /* release_key created by str_create_key, only rigth way to release key * I */ -void str_release_key (char *key, int case_sen); +void str_release_key (char *key, gboolean case_sen); /* return TRUE if codeset_name is utf8 or utf-8 * I diff --git a/lib/strutil/strutil.c b/lib/strutil/strutil.c index 1c8ce05b2..66b1edd3c 100644 --- a/lib/strutil/strutil.c +++ b/lib/strutil/strutil.c @@ -787,7 +787,7 @@ str_trunc (const char *text, int width) /* --------------------------------------------------------------------------------------------- */ char * -str_create_search_needle (const char *needle, int case_sen) +str_create_search_needle (const char *needle, gboolean case_sen) { return used_class.create_search_needle (needle, case_sen); } @@ -795,7 +795,7 @@ str_create_search_needle (const char *needle, int case_sen) /* --------------------------------------------------------------------------------------------- */ void -str_release_search_needle (char *needle, int case_sen) +str_release_search_needle (char *needle, gboolean case_sen) { used_class.release_search_needle (needle, case_sen); } @@ -803,7 +803,7 @@ str_release_search_needle (char *needle, int case_sen) /* --------------------------------------------------------------------------------------------- */ const char * -str_search_first (const char *text, const char *search, int case_sen) +str_search_first (const char *text, const char *search, gboolean case_sen) { return used_class.search_first (text, search, case_sen); } @@ -811,7 +811,7 @@ str_search_first (const char *text, const char *search, int case_sen) /* --------------------------------------------------------------------------------------------- */ const char * -str_search_last (const char *text, const char *search, int case_sen) +str_search_last (const char *text, const char *search, gboolean case_sen) { return used_class.search_last (text, search, case_sen); } @@ -883,7 +883,7 @@ str_fix_string (char *text) /* --------------------------------------------------------------------------------------------- */ char * -str_create_key (const char *text, int case_sen) +str_create_key (const char *text, gboolean case_sen) { return used_class.create_key (text, case_sen); } @@ -891,7 +891,7 @@ str_create_key (const char *text, int case_sen) /* --------------------------------------------------------------------------------------------- */ char * -str_create_key_for_filename (const char *text, int case_sen) +str_create_key_for_filename (const char *text, gboolean case_sen) { return used_class.create_key_for_filename (text, case_sen); } @@ -899,7 +899,7 @@ str_create_key_for_filename (const char *text, int case_sen) /* --------------------------------------------------------------------------------------------- */ int -str_key_collate (const char *t1, const char *t2, int case_sen) +str_key_collate (const char *t1, const char *t2, gboolean case_sen) { return used_class.key_collate (t1, t2, case_sen); } @@ -907,7 +907,7 @@ str_key_collate (const char *t1, const char *t2, int case_sen) /* --------------------------------------------------------------------------------------------- */ void -str_release_key (char *key, int case_sen) +str_release_key (char *key, gboolean case_sen) { used_class.release_key (key, case_sen); } diff --git a/lib/strutil/strutil8bit.c b/lib/strutil/strutil8bit.c index b0bb8ef8d..32656163c 100644 --- a/lib/strutil/strutil8bit.c +++ b/lib/strutil/strutil8bit.c @@ -553,7 +553,7 @@ str_8bit_column_to_pos (const char *text, size_t pos) /* --------------------------------------------------------------------------------------------- */ static char * -str_8bit_create_search_needle (const char *needle, int case_sen) +str_8bit_create_search_needle (const char *needle, gboolean case_sen) { (void) case_sen; return (char *) needle; @@ -562,7 +562,7 @@ str_8bit_create_search_needle (const char *needle, int case_sen) /* --------------------------------------------------------------------------------------------- */ static void -str_8bit_release_search_needle (char *needle, int case_sen) +str_8bit_release_search_needle (char *needle, gboolean case_sen) { (void) case_sen; (void) needle; @@ -589,14 +589,14 @@ str_8bit_strdown (const char *str) /* --------------------------------------------------------------------------------------------- */ static const char * -str_8bit_search_first (const char *text, const char *search, int case_sen) +str_8bit_search_first (const char *text, const char *search, gboolean case_sen) { char *fold_text; char *fold_search; const char *match; - fold_text = (case_sen) ? (char *) text : str_8bit_strdown (text); - fold_search = (case_sen) ? (char *) search : str_8bit_strdown (search); + fold_text = case_sen ? (char *) text : str_8bit_strdown (text); + fold_search = case_sen ? (char *) search : str_8bit_strdown (search); match = g_strstr_len (fold_text, -1, fold_search); if (match != NULL) @@ -619,14 +619,14 @@ str_8bit_search_first (const char *text, const char *search, int case_sen) /* --------------------------------------------------------------------------------------------- */ static const char * -str_8bit_search_last (const char *text, const char *search, int case_sen) +str_8bit_search_last (const char *text, const char *search, gboolean case_sen) { char *fold_text; char *fold_search; const char *match; - fold_text = (case_sen) ? (char *) text : str_8bit_strdown (text); - fold_search = (case_sen) ? (char *) search : str_8bit_strdown (search); + fold_text = case_sen ? (char *) text : str_8bit_strdown (text); + fold_search = case_sen ? (char *) search : str_8bit_strdown (search); match = g_strrstr_len (fold_text, -1, fold_search); if (match != NULL) @@ -775,26 +775,23 @@ str_8bit_fix_string (char *text) /* --------------------------------------------------------------------------------------------- */ static char * -str_8bit_create_key (const char *text, int case_sen) +str_8bit_create_key (const char *text, gboolean case_sen) { - return (case_sen) ? (char *) text : str_8bit_strdown (text); + return case_sen ? (char *) text : str_8bit_strdown (text); } /* --------------------------------------------------------------------------------------------- */ static int -str_8bit_key_collate (const char *t1, const char *t2, int case_sen) +str_8bit_key_collate (const char *t1, const char *t2, gboolean case_sen) { - if (case_sen) - return strcmp (t1, t2); - else - return strcoll (t1, t2); + return case_sen ? strcmp (t1, t2) : strcoll (t1, t2); } /* --------------------------------------------------------------------------------------------- */ static void -str_8bit_release_key (char *key, int case_sen) +str_8bit_release_key (char *key, gboolean case_sen) { if (!case_sen) g_free (key); diff --git a/lib/strutil/strutilascii.c b/lib/strutil/strutilascii.c index 077f24094..2d6ac01df 100644 --- a/lib/strutil/strutilascii.c +++ b/lib/strutil/strutilascii.c @@ -551,7 +551,7 @@ str_ascii_column_to_pos (const char *text, size_t pos) /* --------------------------------------------------------------------------------------------- */ static char * -str_ascii_create_search_needle (const char *needle, int case_sen) +str_ascii_create_search_needle (const char *needle, gboolean case_sen) { (void) case_sen; return (char *) needle; @@ -560,7 +560,7 @@ str_ascii_create_search_needle (const char *needle, int case_sen) /* --------------------------------------------------------------------------------------------- */ static void -str_ascii_release_search_needle (char *needle, int case_sen) +str_ascii_release_search_needle (char *needle, gboolean case_sen) { (void) case_sen; (void) needle; @@ -570,14 +570,14 @@ str_ascii_release_search_needle (char *needle, int case_sen) /* --------------------------------------------------------------------------------------------- */ static const char * -str_ascii_search_first (const char *text, const char *search, int case_sen) +str_ascii_search_first (const char *text, const char *search, gboolean case_sen) { char *fold_text; char *fold_search; const char *match; - fold_text = (case_sen) ? (char *) text : g_ascii_strdown (text, -1); - fold_search = (case_sen) ? (char *) search : g_ascii_strdown (search, -1); + fold_text = case_sen ? (char *) text : g_ascii_strdown (text, -1); + fold_search = case_sen ? (char *) search : g_ascii_strdown (search, -1); match = g_strstr_len (fold_text, -1, fold_search); if (match != NULL) @@ -600,14 +600,14 @@ str_ascii_search_first (const char *text, const char *search, int case_sen) /* --------------------------------------------------------------------------------------------- */ static const char * -str_ascii_search_last (const char *text, const char *search, int case_sen) +str_ascii_search_last (const char *text, const char *search, gboolean case_sen) { char *fold_text; char *fold_search; const char *match; - fold_text = (case_sen) ? (char *) text : g_ascii_strdown (text, -1); - fold_search = (case_sen) ? (char *) search : g_ascii_strdown (search, -1); + fold_text = case_sen ? (char *) text : g_ascii_strdown (text, -1); + fold_search = case_sen ? (char *) search : g_ascii_strdown (search, -1); match = g_strrstr_len (fold_text, -1, fold_search); if (match != NULL) @@ -671,7 +671,7 @@ str_ascii_fix_string (char *text) /* --------------------------------------------------------------------------------------------- */ static char * -str_ascii_create_key (const char *text, int case_sen) +str_ascii_create_key (const char *text, gboolean case_sen) { (void) case_sen; return (char *) text; @@ -680,15 +680,15 @@ str_ascii_create_key (const char *text, int case_sen) /* --------------------------------------------------------------------------------------------- */ static int -str_ascii_key_collate (const char *t1, const char *t2, int case_sen) +str_ascii_key_collate (const char *t1, const char *t2, gboolean case_sen) { - return (case_sen) ? strcmp (t1, t2) : g_ascii_strcasecmp (t1, t2); + return case_sen ? strcmp (t1, t2) : g_ascii_strcasecmp (t1, t2); } /* --------------------------------------------------------------------------------------------- */ static void -str_ascii_release_key (char *key, int case_sen) +str_ascii_release_key (char *key, gboolean case_sen) { (void) key; (void) case_sen; diff --git a/lib/strutil/strutilutf8.c b/lib/strutil/strutilutf8.c index b745a2849..6f3469171 100644 --- a/lib/strutil/strutilutf8.c +++ b/lib/strutil/strutilutf8.c @@ -956,7 +956,7 @@ str_utf8_column_to_pos (const char *text, size_t pos) /* --------------------------------------------------------------------------------------------- */ static char * -str_utf8_create_search_needle (const char *needle, int case_sen) +str_utf8_create_search_needle (const char *needle, gboolean case_sen) { char *fold, *result; @@ -966,7 +966,6 @@ str_utf8_create_search_needle (const char *needle, int case_sen) if (case_sen) return g_utf8_normalize (needle, -1, G_NORMALIZE_ALL); - fold = g_utf8_casefold (needle, -1); result = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); g_free (fold); @@ -976,7 +975,7 @@ str_utf8_create_search_needle (const char *needle, int case_sen) /* --------------------------------------------------------------------------------------------- */ static void -str_utf8_release_search_needle (char *needle, int case_sen) +str_utf8_release_search_needle (char *needle, gboolean case_sen) { (void) case_sen; g_free (needle); @@ -985,7 +984,7 @@ str_utf8_release_search_needle (char *needle, int case_sen) /* --------------------------------------------------------------------------------------------- */ static const char * -str_utf8_search_first (const char *text, const char *search, int case_sen) +str_utf8_search_first (const char *text, const char *search, gboolean case_sen) { char *fold_text; char *deco_text; @@ -993,7 +992,7 @@ str_utf8_search_first (const char *text, const char *search, int case_sen) const char *result = NULL; const char *m; - fold_text = (case_sen) ? (char *) text : g_utf8_casefold (text, -1); + fold_text = case_sen ? (char *) text : g_utf8_casefold (text, -1); deco_text = g_utf8_normalize (fold_text, -1, G_NORMALIZE_ALL); match = deco_text; @@ -1029,7 +1028,7 @@ str_utf8_search_first (const char *text, const char *search, int case_sen) /* --------------------------------------------------------------------------------------------- */ static const char * -str_utf8_search_last (const char *text, const char *search, int case_sen) +str_utf8_search_last (const char *text, const char *search, gboolean case_sen) { char *fold_text; char *deco_text; @@ -1037,7 +1036,7 @@ str_utf8_search_last (const char *text, const char *search, int case_sen) const char *result = NULL; const char *m; - fold_text = (case_sen) ? (char *) text : g_utf8_casefold (text, -1); + fold_text = case_sen ? (char *) text : g_utf8_casefold (text, -1); deco_text = g_utf8_normalize (fold_text, -1, G_NORMALIZE_ALL); do @@ -1343,7 +1342,7 @@ str_utf8_caseprefix (const char *text, const char *prefix) /* --------------------------------------------------------------------------------------------- */ static char * -str_utf8_create_key_gen (const char *text, int case_sen, +str_utf8_create_key_gen (const char *text, gboolean case_sen, gchar * (*keygen) (const gchar * text, gssize size)) { char *result; @@ -1417,7 +1416,7 @@ str_utf8_create_key_gen (const char *text, int case_sen, /* --------------------------------------------------------------------------------------------- */ static char * -str_utf8_create_key (const char *text, int case_sen) +str_utf8_create_key (const char *text, gboolean case_sen) { return str_utf8_create_key_gen (text, case_sen, g_utf8_collate_key); } @@ -1426,7 +1425,7 @@ str_utf8_create_key (const char *text, int case_sen) #ifdef MC__USE_STR_UTF8_CREATE_KEY_FOR_FILENAME static char * -str_utf8_create_key_for_filename (const char *text, int case_sen) +str_utf8_create_key_for_filename (const char *text, gboolean case_sen) { return str_utf8_create_key_gen (text, case_sen, g_utf8_collate_key_for_filename); } @@ -1435,7 +1434,7 @@ str_utf8_create_key_for_filename (const char *text, int case_sen) /* --------------------------------------------------------------------------------------------- */ static int -str_utf8_key_collate (const char *t1, const char *t2, int case_sen) +str_utf8_key_collate (const char *t1, const char *t2, gboolean case_sen) { (void) case_sen; return strcmp (t1, t2); @@ -1444,7 +1443,7 @@ str_utf8_key_collate (const char *t1, const char *t2, int case_sen) /* --------------------------------------------------------------------------------------------- */ static void -str_utf8_release_key (char *key, int case_sen) +str_utf8_release_key (char *key, gboolean case_sen) { (void) case_sen; g_free (key); diff --git a/src/filemanager/dir.c b/src/filemanager/dir.c index 64da12141..aa5cfd018 100644 --- a/src/filemanager/dir.c +++ b/src/filemanager/dir.c @@ -68,7 +68,7 @@ static int reverse = 1; /* Are the files sorted case sensitively? */ -static int case_sensitive = OS_SORT_CASE_SENSITIVE_DEFAULT; +static gboolean case_sensitive = OS_SORT_CASE_SENSITIVE_DEFAULT; /* Are the exec_bit files top in list */ static gboolean exec_first = TRUE; @@ -391,7 +391,7 @@ sort_ext (file_entry_t * a, file_entry_t * b) b->second_sort_key = str_create_key (extension (b->fname), case_sensitive); r = str_key_collate (a->second_sort_key, b->second_sort_key, case_sensitive); - if (r) + if (r != 0) return r * reverse; else return sort_name (a, b); diff --git a/src/setup.c b/src/setup.c index 766dd5589..9f4361cfe 100644 --- a/src/setup.c +++ b/src/setup.c @@ -1456,12 +1456,13 @@ panel_load_setup (WPanel * panel, const char *section) size_t i; char *buffer, buffer2[BUF_TINY]; - panel->sort_info.reverse = mc_config_get_int (mc_global.panels_config, section, "reverse", 0); + panel->sort_info.reverse = + mc_config_get_bool (mc_global.panels_config, section, "reverse", FALSE); panel->sort_info.case_sensitive = - mc_config_get_int (mc_global.panels_config, section, "case_sensitive", - OS_SORT_CASE_SENSITIVE_DEFAULT); + mc_config_get_bool (mc_global.panels_config, section, "case_sensitive", + OS_SORT_CASE_SENSITIVE_DEFAULT); panel->sort_info.exec_first = - mc_config_get_int (mc_global.panels_config, section, "exec_first", 0); + mc_config_get_bool (mc_global.panels_config, section, "exec_first", FALSE); /* Load sort order */ buffer = mc_config_get_string (mc_global.panels_config, section, "sort_order", "name"); @@ -1514,10 +1515,11 @@ panel_save_setup (WPanel * panel, const char *section) char buffer[BUF_TINY]; size_t i; - mc_config_set_int (mc_global.panels_config, section, "reverse", panel->sort_info.reverse); - mc_config_set_int (mc_global.panels_config, section, "case_sensitive", - panel->sort_info.case_sensitive); - mc_config_set_int (mc_global.panels_config, section, "exec_first", panel->sort_info.exec_first); + mc_config_set_bool (mc_global.panels_config, section, "reverse", panel->sort_info.reverse); + mc_config_set_bool (mc_global.panels_config, section, "case_sensitive", + panel->sort_info.case_sensitive); + mc_config_set_bool (mc_global.panels_config, section, "exec_first", + panel->sort_info.exec_first); mc_config_set_string (mc_global.panels_config, section, "sort_order", panel->sort_field->id);