#include "mediapipe/framework/deps/file_helpers.h"
#include "absl/strings/str_cat.h"
#ifdef _WIN32
#include <Windows.h>
#include <direct.h>
#include <codecvt>
#include <locale>
#else
#include <dirent.h>
#endif
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <cerrno>
#include <string>
#include "absl/status/status.h"
#include "mediapipe/framework/deps/file_path.h"
#include "mediapipe/framework/port/canonical_errors.h"
#include "mediapipe/framework/port/status.h"
#include "mediapipe/framework/port/status_builder.h"
#include "mediapipe/framework/port/status_macros.h"
namespace mediapipe {
namespace file {
namespace {
#ifndef _WIN32
class DirectoryListing { … };
#else
#if defined(UNICODE)
using PathString = std::wstring;
PathString Utf8ToNative(const std::string& string) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter;
return converter.from_bytes(string.data(), string.data() + string.size());
}
std::string NativeToUtf8(const PathString& string) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter;
return converter.to_bytes(string.data(), string.data() + string.size());
}
#define FILE_PATH_LITERAL_INTERNAL …
#define FILE_PATH_LITERAL …
#else
using PathString = std::string;
PathString Utf8ToNative(const std::string& string) { return string; }
std::string NativeToUtf8(const PathString& string) { return string; }
#define FILE_PATH_LITERAL …
#endif
class DirectoryListing {
public:
explicit DirectoryListing(const std::string& directory)
: directory_(Utf8ToNative(directory)) {
PathString search_string = directory_ + Utf8ToNative("\\*.*");
find_handle_ = FindFirstFile(search_string.c_str(), &find_data_);
}
~DirectoryListing() {
if (find_handle_ != INVALID_HANDLE_VALUE) {
FindClose(find_handle_);
}
}
bool HasNextEntry() { return find_handle_ != INVALID_HANDLE_VALUE; }
std::string NextEntry() {
if (HasNextEntry()) {
PathString result =
directory_ + Utf8ToNative("\\") + PathString(find_data_.cFileName);
ReadNextEntry();
return NativeToUtf8(result);
} else {
return std::string();
}
}
private:
void ReadNextEntry() {
int find_result = FindNextFile(find_handle_, &find_data_);
while (find_result != 0 &&
(PathString(find_data_.cFileName) == FILE_PATH_LITERAL(".") ||
PathString(find_data_.cFileName) == FILE_PATH_LITERAL(".."))) {
find_result = FindNextFile(find_handle_, &find_data_);
}
if (find_result == 0) {
FindClose(find_handle_);
find_handle_ = INVALID_HANDLE_VALUE;
}
}
const PathString directory_;
HANDLE find_handle_ = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA find_data_;
};
#endif
}
absl::Status GetContents(absl::string_view file_name, std::string* output,
bool read_as_binary) { … }
absl::Status SetContents(absl::string_view file_name,
absl::string_view content) { … }
absl::Status AppendStringToFile(absl::string_view file_name,
absl::string_view contents) { … }
absl::Status MatchInTopSubdirectories(const std::string& parent_directory,
const std::string& file_name,
std::vector<std::string>* results) { … }
absl::Status MatchFileTypeInDirectory(const std::string& directory,
const std::string& file_suffix,
std::vector<std::string>* results) { … }
absl::Status Exists(absl::string_view file_name) { … }
absl::Status IsDirectory(absl::string_view file_name) { … }
#ifndef _WIN32
int mkdir(std::string path) { … }
#else
int mkdir(std::string path) { return _mkdir(path.c_str()); }
#endif
absl::Status RecursivelyCreateDir(absl::string_view path) { … }
}
}