diff options
-rw-r--r-- | ui/gtk/capture_file_dlg.c | 76 | ||||
-rw-r--r-- | wiretap/file_access.c | 83 | ||||
-rw-r--r-- | wiretap/wtap.def | 1 | ||||
-rw-r--r-- | wiretap/wtap.h | 9 |
4 files changed, 108 insertions, 61 deletions
diff --git a/ui/gtk/capture_file_dlg.c b/ui/gtk/capture_file_dlg.c index 11b0304757..4cef1b866b 100644 --- a/ui/gtk/capture_file_dlg.c +++ b/ui/gtk/capture_file_dlg.c @@ -1102,16 +1102,6 @@ file_save_cmd_cb(GtkWidget *w _U_, gpointer data _U_) { file_save_as_cmd(after_save_no_action, NULL, FALSE); } -static gboolean -can_save_with_wiretap(int ft) -{ - /* To save a file with Wiretap, Wiretap has to handle that format, - and its code to handle that format must be able to write a file - with this file's encapsulation type. */ - return wtap_dump_can_open(ft) && wtap_dump_can_write_encap(ft, cfile.lnk_t); -} - - /* Attach a list of the valid 'save as' file types to a combo_box by checking what Wiretap supports. Make the default type the first in the list. @@ -1119,57 +1109,23 @@ can_save_with_wiretap(int ft) static void set_file_type_list(GtkWidget *combo_box, int default_file_type) { - int ft; - int other_file_type = -1; - - /* Can we save this file in the default file type? */ - if (!can_save_with_wiretap(default_file_type)) { - /* No - can we save it as a pcap-NG file? */ - if (can_save_with_wiretap(WTAP_FILE_PCAPNG)) { - /* Yes - default to pcap-NG, instead. */ - default_file_type = WTAP_FILE_PCAPNG; - } else { - /* OK, find the first file type we *can* save it as. */ - for (ft = 0; ft < WTAP_NUM_FILE_TYPES; ft++) { - if (can_save_with_wiretap(ft)) { - /* OK, got it. */ - default_file_type = ft; - } - } - } - } - - /* We should not get here unless there's at least one file type - we can save this as - otherwise, the "Save As..." menu item - should be disabled. */ - - /* Put the default file format first in the list. */ - ws_combo_box_append_text_and_pointer(GTK_COMBO_BOX(combo_box), - wtap_file_type_string(default_file_type), - GINT_TO_POINTER(default_file_type)); - /* If it's pcap, put pcap-NG right after it; otherwise, if it's pcap-NG, - put pcap right after it. */ - if (default_file_type == WTAP_FILE_PCAP) { - if (can_save_with_wiretap(WTAP_FILE_PCAPNG)) - other_file_type = WTAP_FILE_PCAPNG; - } else if (default_file_type == WTAP_FILE_PCAPNG) { - if (can_save_with_wiretap(WTAP_FILE_PCAP)) - other_file_type = WTAP_FILE_PCAP; - } - if (other_file_type != -1) { - ws_combo_box_append_text_and_pointer(GTK_COMBO_BOX(combo_box), - wtap_file_type_string(other_file_type), - GINT_TO_POINTER(other_file_type)); - } - - /* Add all the other file types. */ - for (ft = 0; ft < WTAP_NUM_FILE_TYPES; ft++) { - if (ft == default_file_type || ft == other_file_type) - continue; /* we've already done this one */ - if (can_save_with_wiretap(ft)) { - /* OK, we can write it out in this type. */ - ws_combo_box_append_text_and_pointer(GTK_COMBO_BOX(combo_box), wtap_file_type_string(ft), GINT_TO_POINTER(ft)); + GArray *savable_file_types; + guint i; + int ft; + + savable_file_types = wtap_get_savable_file_types(default_file_type, cfile.lnk_t); + + if (savable_file_types != NULL) { + /* OK, we have at least one file type we can save this file as. + (If we didn't, we shouldn't have gotten here in the first + place.) Add them all to the combo box. */ + for (i = 0; i < savable_file_types->len; i++) { + ft = g_array_index(savable_file_types, int, i); + ws_combo_box_append_text_and_pointer(GTK_COMBO_BOX(combo_box), + wtap_file_type_string(ft), + GINT_TO_POINTER(ft)); } + g_array_free(savable_file_types, TRUE); } } diff --git a/wiretap/file_access.c b/wiretap/file_access.c index a6c5f43f62..c6b77cc391 100644 --- a/wiretap/file_access.c +++ b/wiretap/file_access.c @@ -683,6 +683,89 @@ int wtap_get_num_file_types(void) return wtap_num_file_types; } +/* + * Get a GArray of WTAP_FILE_ values for file types that can be used + * to save a file of a given type with a given WTAP_ENCAP_ type. + */ +static gboolean +can_save_with_wiretap(int ft, int encap) +{ + /* + * To save a file in a given file format, we have to handle that + * format, and the code to handle that format must be able to + * write a file with that file's encapsulation type. + */ + return wtap_dump_can_open(ft) && + wtap_dump_can_write_encap(ft, encap); +} + +GArray * +wtap_get_savable_file_types(int file_type, int file_encap) +{ + GArray *savable_file_types; + int ft; + int default_file_type = -1; + int other_file_type = -1; + + /* Can we save this file in its own file type? */ + if (can_save_with_wiretap(file_type, file_encap)) { + /* Yes - make that the default file type. */ + default_file_type = file_type; + } else { + /* No - can we save it as a pcap-NG file? */ + if (can_save_with_wiretap(WTAP_FILE_PCAPNG, file_encap)) { + /* Yes - default to pcap-NG, instead. */ + default_file_type = WTAP_FILE_PCAPNG; + } else { + /* OK, find the first file type we *can* save it as. */ + default_file_type = -1; + for (ft = 0; ft < WTAP_NUM_FILE_TYPES; ft++) { + if (can_save_with_wiretap(ft, file_encap)) { + /* OK, got it. */ + default_file_type = ft; + } + } + } + } + + if (default_file_type == -1) { + /* We don't support writing this file as any file type. */ + return NULL; + } + + /* Allocate the array. */ + savable_file_types = g_array_new(FALSE, FALSE, (guint)sizeof (int)); + + /* Put the default file format first in the list. */ + g_array_append_val(savable_file_types, default_file_type); + + /* If it's pcap, put pcap-NG right after it; otherwise, if it's + pcap-NG, put pcap right after it. */ + if (default_file_type == WTAP_FILE_PCAP) { + if (can_save_with_wiretap(WTAP_FILE_PCAPNG, file_encap)) + other_file_type = WTAP_FILE_PCAPNG; + } else if (default_file_type == WTAP_FILE_PCAPNG) { + if (can_save_with_wiretap(WTAP_FILE_PCAP, file_encap)) + other_file_type = WTAP_FILE_PCAP; + } + if (other_file_type != -1) + g_array_append_val(savable_file_types, other_file_type); + + /* Add all the other file types that work. */ + for (ft = 0; ft < WTAP_NUM_FILE_TYPES; ft++) { + if (ft == WTAP_FILE_UNKNOWN) + continue; /* not a real file type */ + if (ft == default_file_type || ft == other_file_type) + continue; /* we've already done this one */ + if (can_save_with_wiretap(ft, file_encap)) { + /* OK, we can write it out in this type. */ + g_array_append_val(savable_file_types, ft); + } + } + + return savable_file_types; +} + /* Name that should be somewhat descriptive. */ const char *wtap_file_type_string(int filetype) { diff --git a/wiretap/wtap.def b/wiretap/wtap.def index afb4ad00b6..7b7c99682e 100644 --- a/wiretap/wtap.def +++ b/wiretap/wtap.def @@ -45,6 +45,7 @@ wtap_dump_set_addrinfo_list wtap_encap_short_string wtap_encap_string wtap_file_encap +wtap_get_savable_file_types wtap_get_file_extensions_list wtap_free_file_extensions_list wtap_file_size diff --git a/wiretap/wtap.h b/wiretap/wtap.h index cd3ddfa81f..7227d3aa7f 100644 --- a/wiretap/wtap.h +++ b/wiretap/wtap.h @@ -951,14 +951,21 @@ struct addrinfo; gboolean wtap_dump_set_addrinfo_list(wtap_dumper *wdh, struct addrinfo *addrinfo_list); gboolean wtap_dump_close(wtap_dumper *, int *); +/* + * Get a GArray of WTAP_FILE_ values for file types that can be used + * to save a file of a given type with a given WTAP_ENCAP_ type. + */ +GArray *wtap_get_savable_file_types(int file_type, int file_encap); + /*** various string converter functions ***/ const char *wtap_file_type_string(int filetype); const char *wtap_file_type_short_string(int filetype); int wtap_short_string_to_file_type(const char *short_name); +/*** various file extension functions ***/ +const char *wtap_default_file_extension(int filetype); GSList *wtap_get_file_extensions_list(int filetype); void wtap_free_file_extensions_list(GSList *extensions); -const char *wtap_default_file_extension(int filetype); const char *wtap_encap_string(int encap); const char *wtap_encap_short_string(int encap); |