diff options
author | Guy Harris <guy@alum.mit.edu> | 2015-12-13 13:45:24 -0800 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2015-12-13 21:46:05 +0000 |
commit | b0caa0616bc2991468f0119573b9c509edf4287f (patch) | |
tree | 0175d80c3ad314979588d5675bbb81244cb559b1 /ui/recent.c | |
parent | 260704695dff7db0b0b2432d3b0cdc8512888f70 (diff) | |
download | wireshark-b0caa0616bc2991468f0119573b9c509edf4287f.tar.gz |
Use common code for Boolean and enumerated recent values.
Cleans up the code a bit, and means the comment written out for
enumerated values will contain the write list of value strings.
Change-Id: Iad484e5da9349ab48e3c55e69f671b0a68b5cc6d
Reviewed-on: https://code.wireshark.org/review/12604
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'ui/recent.c')
-rw-r--r-- | ui/recent.c | 266 |
1 files changed, 109 insertions, 157 deletions
diff --git a/ui/recent.c b/ui/recent.c index ef4940f821..d22fec3705 100644 --- a/ui/recent.c +++ b/ui/recent.c @@ -209,6 +209,18 @@ window_geom_load(const gchar *name, } } +/* parse values of particular types */ +static void +parse_recent_boolean(const gchar *value_string, gboolean *valuep) +{ + if (g_ascii_strcasecmp(value_string, "true") == 0) { + *valuep = TRUE; + } + else { + *valuep = FALSE; + } +} + /** Read in a single geometry key value pair from the recent file. * * @param name the geom_name of the window @@ -250,12 +262,7 @@ window_geom_recent_read_pair(const char *name, geom.height = (gint)strtol(value, NULL, 10); geom.set_size = TRUE; } else if (strcmp(key, "maximized") == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - geom.maximized = TRUE; - } - else { - geom.maximized = FALSE; - } + parse_recent_boolean(value, &geom.maximized); geom.set_maximized = TRUE; } else { /* @@ -539,7 +546,40 @@ cfilter_recent_write_all(FILE *rf) } } -/* Attempt to Write out "recent common" to the user's recent common file. +/* Write out recent settings of particular types. */ +static void +write_recent_boolean(FILE *rf, const char *description, const char *name, + gboolean value) +{ + fprintf(rf, "\n# %s.\n", description); + fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); + fprintf(rf, "%s: %s\n", name, value == TRUE ? "TRUE" : "FALSE"); +} + +static void +write_recent_enum(FILE *rf, const char *description, const char *name, + const value_string *values, guint value) +{ + const char *if_invalid = NULL; + const value_string *valp; + + fprintf(rf, "\n# %s.\n", description); + fprintf(rf, "# One of: "); + valp = values; + while (valp->strptr != NULL) { + if (if_invalid == NULL) + if_invalid = valp->strptr; + fprintf(rf, "%s", valp->strptr); + valp++; + if (valp->strptr != NULL) + fprintf(rf, ", "); + } + fprintf(rf, "\n"); + fprintf(rf, "%s: %s\n", name, + val_to_str(value, values, if_invalid != NULL ? if_invalid : "Unknown")); +} + +/* Attempt to write out "recent common" to the user's recent common file. If we got an error report it with a dialog box and return FALSE, otherwise return TRUE. */ gboolean @@ -614,10 +654,9 @@ write_recent(void) fprintf(rf, RECENT_GUI_GEOMETRY_MAIN_HEIGHT ": %d\n", recent.gui_geometry_main_height); - fprintf(rf, "\n# Main window maximized.\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_GUI_GEOMETRY_MAIN_MAXIMIZED ": %s\n", - recent.gui_geometry_main_maximized == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "Main window maximized", + RECENT_GUI_GEOMETRY_MAIN_MAXIMIZED, + recent.gui_geometry_main_maximized); fprintf(rf, "\n# Statusbar left pane size.\n"); fprintf(rf, "# Decimal number.\n"); @@ -640,15 +679,13 @@ write_recent(void) fprintf(rf, RECENT_GUI_GEOMETRY_WLAN_STATS_PANE ": %d\n", recent.gui_geometry_wlan_stats_pane); - fprintf(rf, "\n# Warn if running with elevated permissions (e.g. as root).\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_KEY_PRIVS_WARN_IF_ELEVATED ": %s\n", - recent.privs_warn_if_elevated == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "Warn if running with elevated permissions (e.g. as root)", + RECENT_KEY_PRIVS_WARN_IF_ELEVATED, + recent.privs_warn_if_elevated); - fprintf(rf, "\n# Warn if npf.sys isn't loaded on Windows >= 6.0.\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_KEY_PRIVS_WARN_IF_NO_NPF ": %s\n", - recent.privs_warn_if_no_npf == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "Warn if npf.sys isn't loaded on Windows >= 6.0", + RECENT_KEY_PRIVS_WARN_IF_NO_NPF, + recent.privs_warn_if_no_npf); window_geom_recent_write_all(rf); @@ -706,67 +743,55 @@ write_profile_recent(void) "# So be careful, if you want to make manual changes here.\n" "\n", rf); - fprintf(rf, "\n# Main Toolbar show (hide).\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_KEY_MAIN_TOOLBAR_SHOW ": %s\n", - recent.main_toolbar_show == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "Main Toolbar show (hide)", + RECENT_KEY_MAIN_TOOLBAR_SHOW, + recent.main_toolbar_show); - fprintf(rf, "\n# Filter Toolbar show (hide).\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_KEY_FILTER_TOOLBAR_SHOW ": %s\n", - recent.filter_toolbar_show == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "Filter Toolbar show (hide)", + RECENT_KEY_FILTER_TOOLBAR_SHOW, + recent.filter_toolbar_show); - fprintf(rf, "\n# Wireless Settings Toolbar show (hide).\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_KEY_WIRELESS_TOOLBAR_SHOW ": %s\n", - recent.wireless_toolbar_show == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "Wireless Settings Toolbar show (hide)", + RECENT_KEY_WIRELESS_TOOLBAR_SHOW, + recent.wireless_toolbar_show); #ifdef HAVE_AIRPCAP - fprintf(rf, "\n# Show (hide) old AirPcap driver warning dialog box.\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_KEY_DRIVER_CHECK_SHOW ": %s\n", - recent.airpcap_driver_check_show == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "Show (hide) old AirPcap driver warning dialog box", + RECENT_KEY_DRIVER_CHECK_SHOW, + recent.airpcap_driver_check_show); #endif - fprintf(rf, "\n# Packet list show (hide).\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_KEY_PACKET_LIST_SHOW ": %s\n", - recent.packet_list_show == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "Packet list show (hide)", + RECENT_KEY_PACKET_LIST_SHOW, + recent.packet_list_show); - fprintf(rf, "\n# Tree view show (hide).\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_KEY_TREE_VIEW_SHOW ": %s\n", - recent.tree_view_show == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "Tree view show (hide)", + RECENT_KEY_TREE_VIEW_SHOW, + recent.tree_view_show); - fprintf(rf, "\n# Byte view show (hide).\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_KEY_BYTE_VIEW_SHOW ": %s\n", - recent.byte_view_show == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "Byte view show (hide)", + RECENT_KEY_BYTE_VIEW_SHOW, + recent.byte_view_show); - fprintf(rf, "\n# Statusbar show (hide).\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_KEY_STATUSBAR_SHOW ": %s\n", - recent.statusbar_show == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "Statusbar show (hide)", + RECENT_KEY_STATUSBAR_SHOW, + recent.statusbar_show); - fprintf(rf, "\n# Packet list colorize (hide).\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_KEY_PACKET_LIST_COLORIZE ": %s\n", - recent.packet_list_colorize == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "Packet list colorize (hide)", + RECENT_KEY_PACKET_LIST_COLORIZE, + recent.packet_list_colorize); - fprintf(rf, "\n# Timestamp display format.\n"); - fprintf(rf, "# One of: RELATIVE, ABSOLUTE, ABSOLUTE_WITH_YMD, ABSOLUTE_WITH_YDOY, DELTA, DELTA_DIS, EPOCH, UTC, UTC_WITH_YMD, UTC_WITH_YDOY\n"); - fprintf(rf, RECENT_GUI_TIME_FORMAT ": %s\n", - val_to_str(recent.gui_time_format, ts_type_values, "RELATIVE")); + write_recent_enum(rf, "Timestamp display format", + RECENT_GUI_TIME_FORMAT, ts_type_values, + recent.gui_time_format); - fprintf(rf, "\n# Timestamp display precision.\n"); - fprintf(rf, "# One of: AUTO, SEC, DSEC, CSEC, MSEC, USEC, NSEC\n"); - fprintf(rf, RECENT_GUI_TIME_PRECISION ": %s\n", - val_to_str(recent.gui_time_precision, ts_precision_values, "AUTO")); + write_recent_enum(rf, "Timestamp display precision", + RECENT_GUI_TIME_PRECISION, ts_precision_values, + recent.gui_time_precision); - fprintf(rf, "\n# Seconds display format.\n"); - fprintf(rf, "# One of: SECONDS, HOUR_MIN_SEC\n"); - fprintf(rf, RECENT_GUI_SECONDS_FORMAT ": %s\n", - val_to_str(recent.gui_seconds_format, ts_seconds_values, "SECONDS")); + write_recent_enum(rf, "Seconds display format", + RECENT_GUI_SECONDS_FORMAT, ts_seconds_values, + recent.gui_seconds_format); fprintf(rf, "\n# Zoom level.\n"); fprintf(rf, "# A decimal number.\n"); @@ -807,10 +832,9 @@ write_profile_recent(void) fprintf(rf, RECENT_GUI_ENDPOINT_TABS ": %s\n", string_list); g_free(string_list); - fprintf(rf, "\n# For RLC stats, whether to use RLC PDUs found inside MAC frames.\n"); - fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); - fprintf(rf, RECENT_GUI_RLC_PDUS_FROM_MAC_FRAMES ": %s\n", - recent.gui_rlc_use_pdus_from_mac == TRUE ? "TRUE" : "FALSE"); + write_recent_boolean(rf, "For RLC stats, whether to use RLC PDUs found inside MAC frames", + RECENT_GUI_RLC_PDUS_FROM_MAC_FRAMES, + recent.gui_rlc_use_pdus_from_mac); if (get_last_open_dir() != NULL) { fprintf(rf, "\n# Last directory navigated to in File Open dialog.\n"); @@ -840,13 +864,7 @@ read_set_recent_common_pair_static(gchar *key, const gchar *value, char *p; if (strcmp(key, RECENT_GUI_GEOMETRY_MAIN_MAXIMIZED) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.gui_geometry_main_maximized = TRUE; - } - else { - recent.gui_geometry_main_maximized = FALSE; - } - + parse_recent_boolean(value, &recent.gui_geometry_main_maximized); } else if (strcmp(key, RECENT_GUI_GEOMETRY_MAIN_X) == 0) { num = strtol(value, &p, 0); if (p == value || *p != '\0') @@ -908,19 +926,9 @@ read_set_recent_common_pair_static(gchar *key, const gchar *value, window_geom_recent_read_pair(win, sub_key, value); } } else if (strcmp(key, RECENT_KEY_PRIVS_WARN_IF_ELEVATED) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.privs_warn_if_elevated = TRUE; - } - else { - recent.privs_warn_if_elevated = FALSE; - } + parse_recent_boolean(value, &recent.privs_warn_if_elevated); } else if (strcmp(key, RECENT_KEY_PRIVS_WARN_IF_NO_NPF) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.privs_warn_if_no_npf = TRUE; - } - else { - recent.privs_warn_if_no_npf = FALSE; - } + parse_recent_boolean(value, &recent.privs_warn_if_no_npf); } return PREFS_SET_OK; @@ -940,69 +948,24 @@ read_set_recent_pair_static(gchar *key, const gchar *value, int cust_format_len = (int) strlen(cust_format); if (strcmp(key, RECENT_KEY_MAIN_TOOLBAR_SHOW) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.main_toolbar_show = TRUE; - } - else { - recent.main_toolbar_show = FALSE; - } + parse_recent_boolean(value, &recent.main_toolbar_show); } else if (strcmp(key, RECENT_KEY_FILTER_TOOLBAR_SHOW) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.filter_toolbar_show = TRUE; - } - else { - recent.filter_toolbar_show = FALSE; - } + parse_recent_boolean(value, &recent.filter_toolbar_show); /* check both the old and the new keyword */ } else if (strcmp(key, RECENT_KEY_WIRELESS_TOOLBAR_SHOW) == 0 || (strcmp(key, "gui.airpcap_toolbar_show") == 0)) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.wireless_toolbar_show = TRUE; - } - else { - recent.wireless_toolbar_show = FALSE; - } + parse_recent_boolean(value, &recent.wireless_toolbar_show); } else if (strcmp(key, RECENT_KEY_DRIVER_CHECK_SHOW) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.airpcap_driver_check_show = TRUE; - } - else { - recent.airpcap_driver_check_show = FALSE; - } + parse_recent_boolean(value, &recent.airpcap_driver_check_show); } else if (strcmp(key, RECENT_KEY_PACKET_LIST_SHOW) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.packet_list_show = TRUE; - } - else { - recent.packet_list_show = FALSE; - } + parse_recent_boolean(value, &recent.packet_list_show); } else if (strcmp(key, RECENT_KEY_TREE_VIEW_SHOW) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.tree_view_show = TRUE; - } - else { - recent.tree_view_show = FALSE; - } + parse_recent_boolean(value, &recent.tree_view_show); } else if (strcmp(key, RECENT_KEY_BYTE_VIEW_SHOW) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.byte_view_show = TRUE; - } - else { - recent.byte_view_show = FALSE; - } + parse_recent_boolean(value, &recent.byte_view_show); } else if (strcmp(key, RECENT_KEY_STATUSBAR_SHOW) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.statusbar_show = TRUE; - } - else { - recent.statusbar_show = FALSE; - } + parse_recent_boolean(value, &recent.statusbar_show); } else if (strcmp(key, RECENT_KEY_PACKET_LIST_COLORIZE) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.packet_list_colorize = TRUE; - } - else { - recent.packet_list_colorize = FALSE; - } + parse_recent_boolean(value, &recent.packet_list_colorize); } else if (strcmp(key, RECENT_GUI_TIME_FORMAT) == 0) { recent.gui_time_format = (ts_type)str_to_val(value, ts_type_values, TS_RELATIVE); @@ -1023,13 +986,7 @@ read_set_recent_pair_static(gchar *key, const gchar *value, return PREFS_SET_SYNTAX_ERR; /* number was bad */ recent.gui_bytes_view = (gint)num; } else if (strcmp(key, RECENT_GUI_GEOMETRY_MAIN_MAXIMIZED) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.gui_geometry_main_maximized = TRUE; - } - else { - recent.gui_geometry_main_maximized = FALSE; - } - + parse_recent_boolean(value, &recent.gui_geometry_main_maximized); } else if (strcmp(key, RECENT_GUI_GEOMETRY_MAIN_UPPER_PANE) == 0) { num = strtol(value, &p, 0); if (p == value || *p != '\0') @@ -1051,12 +1008,7 @@ read_set_recent_pair_static(gchar *key, const gchar *value, } else if (strcmp(key, RECENT_GUI_ENDPOINT_TABS) == 0) { recent.endpoint_tabs = prefs_get_string_list(value); } else if (strcmp(key, RECENT_GUI_RLC_PDUS_FROM_MAC_FRAMES) == 0) { - if (g_ascii_strcasecmp(value, "true") == 0) { - recent.gui_rlc_use_pdus_from_mac = TRUE; - } - else { - recent.gui_rlc_use_pdus_from_mac = FALSE; - } + parse_recent_boolean(value, &recent.gui_rlc_use_pdus_from_mac); } else if (strcmp(key, RECENT_KEY_COL_WIDTH) == 0) { col_l = prefs_get_string_list(value); if (col_l == NULL) |