diff options
author | Guy Harris <guy@alum.mit.edu> | 2018-07-06 02:06:56 -0700 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2018-07-06 09:07:52 +0000 |
commit | aab5ad074e88e3c03e438b2b9ff503191c6f6458 (patch) | |
tree | 7310dc5e287603159a2e3f3f1a198e3550a72526 /file.c | |
parent | 140f95a0ca95a1316d4b617998a6f36ea52431b6 (diff) |
Fix the calculation of a file's "basename".
Strip off only extensions that correspond to file types we know about;
QFileInfo::baseName() strips off *all* extensions, where "extension" is
"anything preceded by a .", so it turns foo.bar.pcap.gz into foo, not
foo.bar. We don't want that; instead, we strip off only those
extensions that correspond to file types we know how to read, so we'd
strip off .pcap.gz in foo.bar.pcap.gz, and strip off .pcap in
foo.bar.pcap, leaving foo.bar in both cases.
Change-Id: I5385921ad2f0fef815d52e9902fef15735fd9dae
Reviewed-on: https://code.wireshark.org/review/28636
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'file.c')
-rw-r--r-- | file.c | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -1023,6 +1023,52 @@ cf_get_display_name(capture_file *cf) return displayname; } +gchar * +cf_get_basename(capture_file *cf) +{ + gchar *displayname; + + /* Return a name to use in the GUI for the basename for files to + which we save statistics */ + if (!cf->is_tempfile) { + /* Get the last component of the file name, and use that. */ + if (cf->filename) { + displayname = g_filename_display_basename(cf->filename); + + /* If the file name ends with any extension that corresponds + to a file type we support - including compressed versions + of those files - strip it off. */ + size_t displayname_len = strlen(displayname); + GSList *extensions = wtap_get_all_file_extensions_list(); + GSList *suffix; + for (suffix = extensions; suffix != NULL; suffix = g_slist_next(suffix)) { + /* Does the file name end with that extension? */ + const char *extension = (char *)suffix->data; + size_t extension_len = strlen(extension); + if (displayname_len > extension_len && + displayname[displayname_len - extension_len - 1] == '.' && + strcmp(&displayname[displayname_len - extension_len], extension) == 0) { + /* Yes. Strip the extension off, and return the result. */ + displayname[displayname_len - extension_len - 1] = '\0'; + return displayname; + } + } + } else { + displayname=g_strdup(""); + } + } else { + /* The file we read is a temporary file from a live capture or + a merge operation; we don't mention its name, but, if it's + from a capture, give the source of the capture. */ + if (cf->source) { + displayname = g_strdup(cf->source); + } else { + displayname = g_strdup(""); + } + } + return displayname; +} + void cf_set_tempfile_source(capture_file *cf, gchar *source) { if (cf->source) { g_free(cf->source); |