summaryrefslogtreecommitdiff
path: root/src/file-utils.c
diff options
context:
space:
mode:
authorxmusjackson <[email protected]>2023-05-14 20:57:35 -0500
committermouse <[email protected]>2023-05-29 09:43:42 +0800
commita043e0a81460a94e0732672afc3831689984caf8 (patch)
tree35dec7665b3b8747c172f8c478a59071e89d6dca /src/file-utils.c
parente4cdb9e1a846390bafef8e36085242f793464edd (diff)
downloadengrampa-a043e0a81460a94e0732672afc3831689984caf8.tar.bz2
engrampa-a043e0a81460a94e0732672afc3831689984caf8.tar.xz
Add "Extract to subdirectory" option
This commit adds an "Extract to subdirectory" option to the extract dialog which allows the user to extract the contents of the archive to a directory with the name of the archive (without the extension.) The user will be prompted if the directory must be created. file-utils.c: Improve remove_extension_from_path Rework this function so that it correctly parses and truncates files with multiple extensions like .tar.gz file-utils.c: Improve get_file_extension Improve this function to correctly identify and return supported compressed tar file extensions
Diffstat (limited to 'src/file-utils.c')
-rw-r--r--src/file-utils.c37
1 files changed, 17 insertions, 20 deletions
diff --git a/src/file-utils.c b/src/file-utils.c
index a2b782a..4983a7b 100644
--- a/src/file-utils.c
+++ b/src/file-utils.c
@@ -412,26 +412,16 @@ build_uri (const char *base, ...)
gchar *
remove_extension_from_path (const gchar *path)
{
- int len;
- int p;
- const char *ptr = path;
- char *new_path;
-
- if (! path)
- return NULL;
+ const char *ext;
- len = strlen (path);
- if (len == 1)
- return g_strdup (path);
+ if (path == NULL)
+ return NULL;
- p = len - 1;
- while ((p > 0) && (ptr[p] != '.'))
- p--;
- if (p == 0)
- p = len;
- new_path = g_strndup (path, (guint) p);
-
- return new_path;
+ ext = get_archive_filename_extension (path);
+ if (ext == NULL || strlen (ext) == strlen (path))
+ return g_strdup (path);
+ else
+ return g_strndup (path, strlen (path) - strlen (ext));
}
gboolean
@@ -517,6 +507,7 @@ get_file_extension (const char *filename)
int len;
int p;
const char *ext;
+ const char *tar_exts[] = {".7z", ".br", ".bz", ".bz2", ".gz", ".lrz", ".lz", ".lzma", ".lzo", ".xz", ".Z", ".xst", NULL};
if (filename == NULL)
return NULL;
@@ -532,10 +523,16 @@ get_file_extension (const char *filename)
return NULL;
ext = filename + p;
+ p = 0;
if (ext - 4 > filename) {
const char *test = ext - 4;
- if (strncmp (test, ".tar", 4) == 0)
- ext = ext - 4;
+ if (strncmp (test, ".tar", 4) == 0) {
+ while (tar_exts[p] != NULL) {
+ if (strcmp (ext, tar_exts[p]) == 0)
+ ext = ext - 4;
+ p++;
+ }
+ }
}
return ext;
}