In Windows, in the Save As and Export Selected Packets dialog, append

the default extension for the file type iff

	the file type we're using has a list of extensions;

	the file has no extension or it has one but it's not one of the
	ones in the list.

*Don't* expect a file extension to be at most 5 characters plus the dot
- the extension for pcap-ng, our default capture file type, is "pcapng",
and that's 6 characters!

svn path=/trunk/; revision=42800
This commit is contained in:
Guy Harris 2012-05-23 03:05:17 +00:00
parent 26974768dd
commit e6a57c8e93
4 changed files with 72 additions and 16 deletions

View File

@ -210,7 +210,7 @@ append_file_type(QStringList &filters, int ft)
filter = wtap_file_type_string(ft);
filter += " (";
extensions_list = wtap_get_file_extensions_list(ft);
extensions_list = wtap_get_file_extensions_list(ft, TRUE);
if (extensions_list == NULL) {
/* This file type doesn't have any particular extension
conventionally used for it, so we'll just use "*.*"

View File

@ -271,6 +271,8 @@ win32_save_as_file(HWND h_wnd, action_after_save_e action_after_save, gpointer a
TCHAR file_name16[MAX_PATH] = _T("");
GString *file_name8;
gchar *file_last_dot;
GSList *extensions_list, *extension;
gboolean add_extension;
gchar *dirname;
int ofnsize;
#if (_MSC_VER >= 1500)
@ -323,12 +325,35 @@ win32_save_as_file(HWND h_wnd, action_after_save_e action_after_save, gpointer a
if (GetSaveFileName(ofn)) {
filetype = g_array_index(savable_file_types, int, ofn->nFilterIndex - 1);
/* append the default file extension if there's none given by the user */
/* (we expect a file extension to be at most 5 chars + the dot) */
/*
* Append the default file extension if there's none given by the user
* or if they gave one that's not one of the valid extensions for
* the file type.
*/
file_name8 = g_string_new(utf_16to8(file_name16));
file_last_dot = strrchr(file_name8->str,'.');
if(file_last_dot == NULL || strlen(file_name8->str)-(file_last_dot-file_name8->str) > 5+1) {
if(wtap_default_file_extension(filetype) != NULL) {
extensions_list = wtap_get_file_extensions_list(filetype, FALSE);
if (extensions_list != NULL) {
/* We have one or more extensions for this file type.
Start out assuming we need to add the default one. */
add_extension = TRUE;
if (file_last_dot != NULL) {
/* OK, see if the file has one of those extensions. */
for (extension = extensions_list; extension != NULL && file_last_dot != NULL;
extension = g_slist_next(extension)) {
if (g_ascii_strcasecmp((char *)extension->data, file_last_dot) == 0) {
/* The file name has one of the extensions for this file type */
add_extension = FALSE;
break;
}
}
}
} else {
/* We have no extensions for this file type. Don't add one. */
add_extension = FALSE;
}
if (add_extension) {
if (wtap_default_file_extension(filetype) != NULL) {
g_string_append_printf(file_name8, ".%s", wtap_default_file_extension(filetype));
}
}
@ -420,6 +445,8 @@ win32_export_specified_packets_file(HWND h_wnd) {
TCHAR file_name16[MAX_PATH] = _T("");
GString *file_name8;
gchar *file_last_dot;
GSList *extensions_list, *extension;
gboolean add_extension;
gchar *dirname;
int ofnsize;
#if (_MSC_VER >= 1500)
@ -472,12 +499,35 @@ win32_export_specified_packets_file(HWND h_wnd) {
if (GetSaveFileName(ofn)) {
filetype = g_array_index(savable_file_types, int, ofn->nFilterIndex - 1);
/* append the default file extension if there's none given by the user */
/* (we expect a file extension to be at most 5 chars + the dot) */
/*
* Append the default file extension if there's none given by the user
* or if they gave one that's not one of the valid extensions for
* the file type.
*/
file_name8 = g_string_new(utf_16to8(file_name16));
file_last_dot = strrchr(file_name8->str,'.');
if(file_last_dot == NULL || strlen(file_name8->str)-(file_last_dot-file_name8->str) > 5+1) {
if(wtap_default_file_extension(filetype) != NULL) {
extensions_list = wtap_get_file_extensions_list(filetype, FALSE);
if (extensions_list != NULL) {
/* We have one or more extensions for this file type.
Start out assuming we need to add the default one. */
add_extension = TRUE;
if (file_last_dot != NULL) {
/* OK, see if the file has one of those extensions. */
for (extension = extensions_list; extension != NULL && file_last_dot != NULL;
extension = g_slist_next(extension)) {
if (g_ascii_strcasecmp((char *)extension->data, file_last_dot) == 0) {
/* The file name has one of the extensions for this file type */
add_extension = FALSE;
break;
}
}
}
} else {
/* We have no extensions for this file type. Don't add one. */
add_extension = FALSE;
}
if (add_extension) {
if (wtap_default_file_extension(filetype) != NULL) {
g_string_append_printf(file_name8, ".%s", wtap_default_file_extension(filetype));
}
}
@ -1535,7 +1585,7 @@ append_file_type(GArray *sa, int ft)
TCHAR *str16;
guint16 zero = 0;
extensions_list = wtap_get_file_extensions_list(ft);
extensions_list = wtap_get_file_extensions_list(ft, TRUE);
if (extensions_list == NULL) {
/* This file type doesn't have any particular extension
conventionally used for it, so we'll just use "*.*"

View File

@ -841,12 +841,14 @@ static GSList *add_extensions(GSList *extensions, const gchar *extension,
}
/* Return a list of file extensions that are used by the specified file type.
This includes compressed extensions, e.g. not just "pcap" but also
"pcap.gz" if we can read gzipped files.
If include_compressed is TRUE, the list will include compressed
extensions, e.g. not just "pcap" but also "pcap.gz" if we can read
gzipped files.
All strings in the list are allocated with g_malloc() and must be freed
with g_free(). */
GSList *wtap_get_file_extensions_list(int filetype)
GSList *wtap_get_file_extensions_list(int filetype, gboolean include_compressed)
{
gchar **extensions_set, **extensionp;
gchar *extension;
@ -862,9 +864,13 @@ GSList *wtap_get_file_extensions_list(int filetype)
extensions = NULL; /* empty list, to start with */
/*
* Get the list of compressed-file extensions.
* If include_compressions is true, get the list of compressed-file
* extensions.
*/
compressed_file_extensions = wtap_get_compressed_file_extensions();
if (include_compressed)
compressed_file_extensions = wtap_get_compressed_file_extensions();
else
compressed_file_extensions = NULL;
/*
* Add the default extension, and all compressed variants of

View File

@ -1127,7 +1127,7 @@ 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);
GSList *wtap_get_file_extensions_list(int filetype, gboolean include_compressed);
void wtap_free_file_extensions_list(GSList *extensions);
const char *wtap_encap_string(int encap);