From d163028054aa001f01037e70abcd3825fbcee6f3 Mon Sep 17 00:00:00 2001 From: q66 Date: Tue, 8 Sep 2015 00:49:50 +0100 Subject: [PATCH] directory stream optimization --- ostd/filesystem.hh | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/ostd/filesystem.hh b/ostd/filesystem.hh index ea6e1a4..8bbf9c1 100644 --- a/ostd/filesystem.hh +++ b/ostd/filesystem.hh @@ -244,9 +244,12 @@ struct DirectoryStream { memcpy(buf, &path[0], path.size()); buf[path.size()] = '\0'; p_d = opendir(buf); + if (!pop_front() || !skip_dots()) { + close(); + return false; + } p_path = path; - pop_front(); - return is_open(); + return true; } bool is_open() const { return p_d != nullptr; } @@ -265,7 +268,7 @@ struct DirectoryStream { struct dirent rdv; struct dirent *rd; while (pop_front(td, &rdv, &rd)) - ++ret; + ret += strcmp(rd->d_name, ".") && strcmp(rd->d_name, ".."); closedir(td); return ret; } @@ -300,10 +303,6 @@ private: if (!d) return false; if (readdir_r(d, dev, de)) return false; - while (*de && (!strcmp((*de)->d_name, ".") || - !strcmp((*de)->d_name, ".."))) - if (readdir_r(d, dev, de)) - return false; return !!*de; } @@ -311,6 +310,14 @@ private: return pop_front(p_d, &p_dev, &p_de); } + bool skip_dots() { + while (p_de && (!strcmp(p_de->d_name, ".") || + !strcmp(p_de->d_name, ".."))) + if (readdir_r(p_d, &p_dev, &p_de)) + return false; + return !!p_de; + } + FileInfo front() const { if (!p_de) return FileInfo();