diff options
author | rubidium <rubidium@openttd.org> | 2007-09-27 20:39:46 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2007-09-27 20:39:46 +0000 |
commit | 1dbc6bd4c5ece516932f4d2ecd8297a2307401de (patch) | |
tree | cd23e48ff6fc55a982567bdfb18ac7f30c0e88ff | |
parent | d464d7587d68abb171a7290acdd62d4aac73079b (diff) | |
download | openttd-1dbc6bd4c5ece516932f4d2ecd8297a2307401de.tar.xz |
(svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
-rw-r--r-- | src/newgrf_config.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 2642a93ba..9f952a4e8 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -371,6 +371,21 @@ static uint ScanTar(TarFileList::iterator tar) return num; } +/** + * Simple sorter for GRFS + * @param p1 the first GRFConfig * + * @param p2 the second GRFConfig * + * @return the same strcmp would return for the name of the NewGRF. + */ +static int GRFSorter(const void *p1, const void *p2) +{ + const GRFConfig *c1 = *(const GRFConfig **)p1; + const GRFConfig *c2 = *(const GRFConfig **)p2; + + return strcmp(c1->name != NULL ? c1->name : c1->filename, + c2->name != NULL ? c2->name : c2->filename); +} + /* Scan for all NewGRFs */ void ScanNewGRFFiles() { @@ -389,7 +404,29 @@ void ScanNewGRFFiles() FOR_ALL_TARS(tar) { num += ScanTar(tar); } + DEBUG(grf, 1, "Scan complete, found %d files", num); + + /* Sort the linked list using quicksort. + * For that we first have to make an array, the qsort and + * then remake the linked list. */ + GRFConfig **to_sort = MallocT<GRFConfig*>(num); + if (to_sort == NULL) return; // No memory, then don't sort + + uint i = 0; + for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) { + to_sort[i] = p; + } + /* Number of files is not necessarily right */ + num = i; + + qsort(to_sort, num, sizeof(GRFConfig*), GRFSorter); + + for (i = 1; i < num; i++) { + to_sort[i - 1]->next = to_sort[i]; + } + to_sort[num - 1]->next = NULL; + _all_grfs = to_sort[0]; } |