diff options
author | Andres Mejia <amejia004@gmail.com> | 2013-02-09 19:13:53 -0500 |
---|---|---|
committer | Andres Mejia <amejia004@gmail.com> | 2013-02-09 19:13:53 -0500 |
commit | 258fd85505e7c9b52394f18565148e03d17c1c32 (patch) | |
tree | cc7e56cfe4c3010a9dfa434e74b1daab24070887 /libarchive/archive_util.c | |
parent | 7da8d576848982f2b0264febf89cb8e3f3580af2 (diff) | |
download | libarchive-258fd85505e7c9b52394f18565148e03d17c1c32.tar.gz |
Add a convenience function to sort a list of strings.
This is useful for sorting a list of filepaths to
multivolume RARs for example.
Diffstat (limited to 'libarchive/archive_util.c')
-rw-r--r-- | libarchive/archive_util.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/libarchive/archive_util.c b/libarchive/archive_util.c index 34d8081c..57db5a65 100644 --- a/libarchive/archive_util.c +++ b/libarchive/archive_util.c @@ -54,6 +54,8 @@ __FBSDID("$FreeBSD: head/lib/libarchive/archive_util.c 201098 2009-12-28 02:58:1 #define O_CLOEXEC 0 #endif +static int archive_utility_string_sort_helper(char **, unsigned int); + /* Generic initialization of 'struct archive' objects. */ int __archive_clean(struct archive *a) @@ -499,3 +501,43 @@ __archive_ensure_cloexec_flag(int fd) } #endif } + +/* + * Utility function to sort a group of strings using quicksort. + */ +static int +archive_utility_string_sort_helper(char **strings, unsigned int n) +{ + unsigned int i, j, pivot; + char *tmp; + + if (n <= 1) + return (ARCHIVE_OK); + + pivot = 0; + for (i = 1; i < n; i++) + { + if (strcmp(strings[i], strings[pivot]) < 0) + { + tmp = strings[i]; + for (j = i; j > pivot; j--) + { + strings[j] = strings[j - 1]; + } + strings[pivot] = tmp; + pivot++; + } + } + archive_utility_string_sort_helper(strings, pivot + 1); + archive_utility_string_sort_helper(strings + pivot + 1, n - (pivot + 1)); + return (ARCHIVE_OK); +} + +int +archive_utility_string_sort(char **strings) +{ + unsigned int size = 0; + while (strings[size] != NULL) + size++; + return archive_utility_string_sort_helper(strings, size); +} |