mirror of
https://github.com/weiju/amiga-stuff
synced 2025-11-20 00:09:58 +00:00
file requester now using the new sorting routine
This commit is contained in:
@ -59,9 +59,6 @@ static struct FileListEntry *all_volumes(int *num_entries)
|
|||||||
}
|
}
|
||||||
Permit();
|
Permit();
|
||||||
*num_entries = n;
|
*num_entries = n;
|
||||||
/* The result is an unsorted list, which is usually not what we want.
|
|
||||||
TODO: sort the list
|
|
||||||
*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#include "file_list.h"
|
#include "file_list.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
void free_file_list(struct FileListEntry *entries)
|
void free_file_list(struct FileListEntry *entries)
|
||||||
{
|
{
|
||||||
@ -44,15 +45,26 @@ static struct FileListEntry *merge(struct FileListEntry *list1, struct FileListE
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FileListEntry *sort_file_list(struct FileListEntry *list, BOOL asc)
|
static struct FileListEntry *_sort_file_list(struct FileListEntry *list, BOOL asc)
|
||||||
{
|
{
|
||||||
if (!list || !list->next) return list;
|
if (!list || !list->next) return list;
|
||||||
struct FileListEntry *list2 = splice(list);
|
struct FileListEntry *list2 = splice(list);
|
||||||
list = sort_file_list(list, asc);
|
list = _sort_file_list(list, asc);
|
||||||
list2 = sort_file_list(list2, asc);
|
list2 = _sort_file_list(list2, asc);
|
||||||
return merge(list, list2, asc);
|
return merge(list, list2, asc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct FileListEntry *sort_file_list(struct FileListEntry *list, BOOL asc)
|
||||||
|
{
|
||||||
|
struct FileListEntry *result = _sort_file_list(list, asc), *cur = result;
|
||||||
|
int index = 0;
|
||||||
|
// fix the index after sorting
|
||||||
|
while (cur) {
|
||||||
|
cur->index = index++;
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
struct FileListEntry *new_file_list_entry()
|
struct FileListEntry *new_file_list_entry()
|
||||||
{
|
{
|
||||||
return calloc(1, sizeof(struct FileListEntry));
|
return calloc(1, sizeof(struct FileListEntry));
|
||||||
|
|||||||
@ -23,6 +23,7 @@ CHIBI_TEST(TestSortListSingle)
|
|||||||
struct FileListEntry *entry = new_file_list_entry();
|
struct FileListEntry *entry = new_file_list_entry();
|
||||||
chibi_assert(sort_file_list(entry, TRUE) == entry);
|
chibi_assert(sort_file_list(entry, TRUE) == entry);
|
||||||
chibi_assert(sort_file_list(entry, FALSE) == entry);
|
chibi_assert(sort_file_list(entry, FALSE) == entry);
|
||||||
|
chibi_assert_eq_int(entry->index, 0);
|
||||||
free_file_list(entry);
|
free_file_list(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,6 +37,10 @@ CHIBI_TEST(TestSortListTwoElemsAsc)
|
|||||||
entry2->prev = entry1;
|
entry2->prev = entry1;
|
||||||
|
|
||||||
chibi_assert(sort_file_list(entry1, TRUE) == entry2);
|
chibi_assert(sort_file_list(entry1, TRUE) == entry2);
|
||||||
|
chibi_assert(entry2->prev == NULL);
|
||||||
|
chibi_assert(entry1->next == NULL);
|
||||||
|
chibi_assert_eq_int(0, entry2->index);
|
||||||
|
chibi_assert_eq_int(1, entry1->index);
|
||||||
free_file_list(entry2);
|
free_file_list(entry2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,6 +54,10 @@ CHIBI_TEST(TestSortListTwoElemsDesc)
|
|||||||
entry2->prev = entry1;
|
entry2->prev = entry1;
|
||||||
|
|
||||||
chibi_assert(sort_file_list(entry1, FALSE) == entry1);
|
chibi_assert(sort_file_list(entry1, FALSE) == entry1);
|
||||||
|
chibi_assert(entry1->prev == NULL);
|
||||||
|
chibi_assert(entry2->next == NULL);
|
||||||
|
chibi_assert_eq_int(0, entry1->index);
|
||||||
|
chibi_assert_eq_int(1, entry2->index);
|
||||||
free_file_list(entry1);
|
free_file_list(entry1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -371,6 +371,7 @@ static void reload_file_list(const char *dirpath)
|
|||||||
{
|
{
|
||||||
int num_files;
|
int num_files;
|
||||||
struct FileListEntry *dir_entries = scan_dir(dirpath, &num_files);
|
struct FileListEntry *dir_entries = scan_dir(dirpath, &num_files);
|
||||||
|
dir_entries = sort_file_list(dir_entries, TRUE);
|
||||||
if (current_files) free_file_list(current_files);
|
if (current_files) free_file_list(current_files);
|
||||||
current_files = dir_entries;
|
current_files = dir_entries;
|
||||||
num_current_files = num_files;
|
num_current_files = num_files;
|
||||||
|
|||||||
Reference in New Issue
Block a user