Avoid randomness when processing directories.

Order of files in file system order is random (maybe different for every
user). Determinsm is good, i.e. when loading Nasal scripts in a fixed,
known sequence, or config files, where the later may overrule settings of
the earlier.
This commit is contained in:
ThorstenB 2012-10-13 12:29:04 +02:00
parent f5cc151618
commit 1dfac0a8b9

View File

@ -44,6 +44,7 @@
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> // for std::sort
using std::string;
@ -124,6 +125,11 @@ Dir Dir::tempDir(const std::string& templ)
#endif
}
static bool pathSortPredicate(const SGPath& p1, const SGPath& p2)
{
return p1.file() < p2.file();
}
PathList Dir::children(int types, const std::string& nameFilter) const
{
PathList result;
@ -241,6 +247,11 @@ PathList Dir::children(int types, const std::string& nameFilter) const
closedir(dp);
#endif
// File system order is random. Make things deterministic,
// so it's the same for every user.
std::sort(result.begin(), result.end(), pathSortPredicate);
return result;
}