Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Unified Diff: webrtc/test/testsupport/fileutils.cc

Issue 2898753002: ReadDirectory() added in webrtc/test/testsupport/fileutils.h (Closed)
Patch Set: add trailing sep char to dir paths Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webrtc/test/testsupport/fileutils.cc
diff --git a/webrtc/test/testsupport/fileutils.cc b/webrtc/test/testsupport/fileutils.cc
index 02ef06580bbcec4203d46ac1c23f42d4e279ebd6..8e2171f700088251dbaf629a2fd39dfc6f711fb1 100644
--- a/webrtc/test/testsupport/fileutils.cc
+++ b/webrtc/test/testsupport/fileutils.cc
@@ -24,6 +24,7 @@
#include "webrtc/base/win32.h"
#define GET_CURRENT_DIR _getcwd
#else
+#include <dirent.h>
#include <unistd.h>
#define GET_CURRENT_DIR getcwd
@@ -40,6 +41,7 @@
#include <memory>
+#include "webrtc/base/checks.h"
#include "webrtc/typedefs.h" // For architecture defines
namespace webrtc {
@@ -126,7 +128,7 @@ std::string WorkingDir() {
return kRootDirName;
}
-#else // WEBRTC_ANDROID
+#else // WEBRTC_ANDROID
std::string ProjectRootPath() {
#if defined(WEBRTC_IOS)
@@ -212,6 +214,57 @@ std::string TempFilename(const std::string &dir, const std::string &prefix) {
#endif
}
+bool ReadDirectory(std::string path, std::vector<std::string>* output) {
+ RTC_DCHECK(output);
+ if (!DirExists(path)) { return false; }
nisse-webrtc 2017/05/29 07:20:40 I'm not sure what DirExists return for an empty st
AleBzk 2017/05/29 09:50:51 Done.
+ output->clear();
+#if defined(WEBRTC_WIN)
+ // Append separator character if needed.
+ size_t path_len = path.length();
+ if (path[path_len - 1] != '\\') { path += '\\'; }
nisse-webrtc 2017/05/29 07:20:40 Use path.back() instead, and delete the |path_len|
AleBzk 2017/05/29 09:50:51 Done.
+
+ // Init.
+ WIN32_FIND_DATA data;
+ HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data);
+ if (handle == INVALID_HANDLE_VALUE) { return false; }
+
+ // Populate output.
+ do {
+ const std::string name = rtc::ToUtf8(data.cFileName);
+ if (name != "." && name != "..") { output->emplace_back(path + name); }
+ } while (::FindNextFile(handle, &data) == TRUE);
+
+ // Release resources.
+ if (handle != INVALID_HANDLE_VALUE) { ::FindClose(handle); }
+#else
+ // Append separator character if needed.
+ size_t path_len = path.length();
+ if (path[path_len - 1] != '/') { path += '/'; }
+
+ // Init.
+ DIR *dir = ::opendir(path.c_str());
+ if (dir == nullptr) { return false; }
+ struct dirent* dirent = readdir(dir);
+ if (dirent == nullptr) { return false; }
+ struct stat stat;
+ if (::stat(std::string(path + dirent->d_name).c_str(), &stat) != 0) {
nisse-webrtc 2017/05/29 07:20:40 Unclear to me why you call stat (it would make sen
AleBzk 2017/05/29 09:50:51 Right! I just copied and adapted the code from web
+ return false;
+ }
+
+ // Populate output.
+ do {
+ const std::string& name = dirent->d_name;
+ if (name != "." && name != "..") { output->emplace_back(path + name); }
+ dirent = ::readdir(dir);
+ if (dirent == nullptr) { break; }
+ } while (::stat(std::string(path + dirent->d_name).c_str(), &stat) == 0);
+
+ // Release resources.
+ closedir(dir);
+#endif
+ return true;
+}
+
bool CreateDir(const std::string& directory_name) {
struct stat path_info = {0};
// Check if the path exists already:

Powered by Google App Engine
This is Rietveld 408576698