第三方框架改为仓内子集并完善构建忽略规则
完整 NIM_Duilib_Framework 仓库移至父目录,third_party 仅保留实际使用的 base、duilib、bin/resources 与许可证;.gitignore 覆盖 MSBuild 与打包产物。
This commit is contained in:
1 parent
249e8548de
commit
e1e26acc49
391 files changed
+342850
No files matched your search
@@ -0,0 +1,19 @@
|
||||
#ifndef BASE_FILE_FILE_PATH_H_
|
||||
#define BASE_FILE_FILE_PATH_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
|
||||
#if defined(OS_POSIX)
|
||||
typedef std::string PathString;
|
||||
#elif defined(OS_WIN)
|
||||
typedef std::wstring PathString;
|
||||
#endif
|
||||
|
||||
typedef PathString::value_type PathChar;
|
||||
|
||||
} // namespace nbase
|
||||
|
||||
#endif // BASE_FILE_FILE_PATH_H_
|
||||
@@ -0,0 +1,243 @@
|
||||
// Copyright (c) 2011, NetEase Inc. All rights reserved.
|
||||
//
|
||||
// Author: rl
|
||||
// Date: 2011/6/12
|
||||
//
|
||||
// Modified by wrt(guangguang)
|
||||
// Date: 2013/9/17
|
||||
//
|
||||
// Utilities for file and filepath operation
|
||||
|
||||
#include "base/file/file_util.h"
|
||||
#include <memory>
|
||||
#include "base/util/string_util.h"
|
||||
#include "base/memory/deleter.h"
|
||||
|
||||
#if defined(OS_POSIX)
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#endif // OS_POSIX
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
|
||||
#if defined(OS_POSIX)
|
||||
const PathChar kEndChar = '\0';
|
||||
const PathChar kFilePathSeparators[] = "/";
|
||||
const PathChar kFilePathCurrentDirectory[] = ".";
|
||||
const PathChar kFilePathParentDirectory[] = "..";
|
||||
const PathChar kFilePathExtensionSeparator = '.';
|
||||
#elif defined(OS_WIN)
|
||||
const PathChar kEndChar = L'\0';
|
||||
const PathChar kFilePathSeparators[] = L"\\/";
|
||||
const PathChar kFilePathCurrentDirectory[] = L".";
|
||||
const PathChar kFilePathParentDirectory[] = L"..";
|
||||
const PathChar kFilePathExtensionSeparator = L'.';
|
||||
#endif // OS_WIN
|
||||
|
||||
bool IsFilePathSeparator(const PathChar separator)
|
||||
{
|
||||
if (separator == kEndChar)
|
||||
return false;
|
||||
|
||||
size_t len = sizeof(kFilePathSeparators)/sizeof(PathChar);
|
||||
for (size_t i = 0; i < len; i++)
|
||||
{
|
||||
if (separator == kFilePathSeparators[i])
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IsFilePathSeparator(const PathString &separator)
|
||||
{
|
||||
if (separator.empty())
|
||||
return false;
|
||||
PathChar c = separator[0];
|
||||
return IsFilePathSeparator(c);
|
||||
}
|
||||
|
||||
bool FilePathExtension(const PathString &filepath_in, PathString &extension_out)
|
||||
{
|
||||
if (filepath_in.size() == 0)
|
||||
return false;
|
||||
bool ret = false;
|
||||
PathString file_name;
|
||||
if (FilePathApartFileName(filepath_in, file_name))
|
||||
{
|
||||
size_t pos = file_name.rfind(kFilePathExtensionSeparator);
|
||||
if (pos != PathString::npos)
|
||||
{
|
||||
extension_out = file_name.substr(pos, PathString::npos);
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool FilePathApartDirectory(const PathString &filepath_in,
|
||||
PathString &directory_out)
|
||||
{
|
||||
size_t index = filepath_in.size() - 1;
|
||||
if (index <= 0 || filepath_in.size() == 0)
|
||||
return false;
|
||||
for (; index != 0; index--)
|
||||
{
|
||||
if (IsFilePathSeparator(filepath_in[index]))
|
||||
{
|
||||
if (index == filepath_in.size() - 1)
|
||||
directory_out = filepath_in;
|
||||
else
|
||||
directory_out = filepath_in.substr(0, index + 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FilePathApartFileName(const PathString &filepath_in,
|
||||
PathString &filename_out)
|
||||
{
|
||||
if (filepath_in.size() == 0)
|
||||
return false;
|
||||
bool ret = true;
|
||||
size_t separator_pos = PathString::npos;
|
||||
size_t separators_count = sizeof(kFilePathSeparators) / sizeof(PathChar);
|
||||
for (size_t index = 0; index < separators_count; index++)
|
||||
{
|
||||
separator_pos = filepath_in.rfind(kFilePathSeparators[index]);
|
||||
if (separator_pos != PathString::npos)
|
||||
break;
|
||||
}
|
||||
if (separator_pos++ != PathString::npos && separator_pos < filepath_in.size())
|
||||
filename_out = filepath_in.substr(separator_pos);
|
||||
else if (separator_pos >= filepath_in.size())
|
||||
ret = false;
|
||||
else
|
||||
filename_out = filepath_in;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename CharType>
|
||||
bool ParsePathComponentsT(const CharType *path,
|
||||
const CharType *seperators,
|
||||
std::list<std::basic_string<CharType> > &components)
|
||||
{
|
||||
components.clear();
|
||||
if (path == NULL)
|
||||
return false;
|
||||
const CharType *prev = NULL;
|
||||
const CharType *next = path;
|
||||
const CharType *c;
|
||||
while (*next)
|
||||
{
|
||||
prev = next;
|
||||
// find the first seperator
|
||||
for (;;)
|
||||
{
|
||||
for (c = seperators; *c && *next != *c; c++);
|
||||
if (*c || !*next)
|
||||
break;
|
||||
next++;
|
||||
}
|
||||
components.push_back(std::basic_string<CharType>(prev, next - prev));
|
||||
if (*next)
|
||||
components.back().push_back(*seperators);
|
||||
// skip duplicated seperators
|
||||
for (++next;;)
|
||||
{
|
||||
for (c = seperators; *c && *next != *c; c++);
|
||||
if (!*c)
|
||||
break;
|
||||
next++;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ParsePathComponents(const PathChar *path,
|
||||
std::list<PathString> &components)
|
||||
{
|
||||
return ParsePathComponentsT<PathChar>(path,
|
||||
kFilePathSeparators,
|
||||
components);
|
||||
}
|
||||
|
||||
bool IsDirectoryComponent(const PathString &component)
|
||||
{
|
||||
if (component.empty())
|
||||
return false;
|
||||
#if defined(OS_WIN)
|
||||
return *component.rbegin() == kFilePathSeparators[0] ||
|
||||
*component.rbegin() == kFilePathSeparators[1];
|
||||
#else
|
||||
return *component.rbegin() == kFilePathSeparators[0];
|
||||
#endif // OS_WIN
|
||||
}
|
||||
|
||||
bool FilePathCompose(const PathString &directory_in,
|
||||
const PathString &filename_in,
|
||||
PathString &filepath_out)
|
||||
{
|
||||
PathString directory;
|
||||
if (!FilePathApartDirectory(directory_in, directory))
|
||||
return false;
|
||||
filepath_out = directory + filename_in;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FilePathIsExist(const PathString &filepath_in, bool is_directory)
|
||||
{
|
||||
return FilePathIsExist((const PathChar *)filepath_in.c_str(), is_directory);
|
||||
}
|
||||
|
||||
FILE* OpenFile(const PathString &filepath, const PathChar *mode)
|
||||
{
|
||||
return OpenFile(filepath.c_str(), mode);
|
||||
}
|
||||
|
||||
bool CloseFile(FILE *file)
|
||||
{
|
||||
if (NULL == file)
|
||||
return true;
|
||||
return fclose(file) == 0;
|
||||
}
|
||||
|
||||
int ReadFile(const PathString &filepath, void *data_out, size_t size)
|
||||
{
|
||||
return ReadFile(filepath.c_str(), data_out, size);
|
||||
}
|
||||
|
||||
int WriteFile(const PathString &filepath, const std::string &data)
|
||||
{
|
||||
return WriteFile(filepath.c_str(), data.c_str(), data.size());
|
||||
}
|
||||
|
||||
bool ReadFileToString(const PathString &filepath, std::string &out)
|
||||
{
|
||||
std::unique_ptr<FILE, nbase::DeleterFileClose> file;
|
||||
#if defined(OS_WIN)
|
||||
file.reset(OpenFile(filepath.c_str(), L"rb"));
|
||||
#else
|
||||
file.reset(OpenFile(filepath.c_str(), "rb"));
|
||||
#endif
|
||||
|
||||
if (!file)
|
||||
return false;
|
||||
|
||||
size_t file_size = (size_t)GetFileSize(filepath);
|
||||
if (file_size > std::numeric_limits<size_t>::max()) //x64编译条件下,size_t的最大值转为int64_t会溢出,因此用无符号
|
||||
return false; // Too large
|
||||
|
||||
bool read_ok = true;
|
||||
out.resize(file_size);
|
||||
if (!out.empty()) {
|
||||
read_ok = fread(&out[0], 1, out.size(), file.get()) == out.size();
|
||||
}
|
||||
|
||||
return read_ok;
|
||||
}
|
||||
|
||||
} // namespace nbase
|
||||
@@ -0,0 +1,104 @@
|
||||
// Utilities for file and filepath operation
|
||||
|
||||
#ifndef BASE_FILE_FILE_UTIL_H_
|
||||
#define BASE_FILE_FILE_UTIL_H_
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include "base/base_export.h"
|
||||
#include "base/base_types.h"
|
||||
#include "base/file/file_path.h"
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
|
||||
// Null-terminated array of separators used to separate components in path.
|
||||
// Each character in this array is a valid separator
|
||||
extern const PathChar kFilePathSeparators[];
|
||||
// A special path component meaning "this directory."
|
||||
extern const PathChar kFilePathCurrentDirectory[];
|
||||
// A special path component meaning "the parent directory."
|
||||
extern const PathChar kFilePathParentDirectory[];
|
||||
// The character used to identify a file extension.
|
||||
extern const PathChar kFilePathExtensionSeparator;
|
||||
|
||||
// Check the character is filepath separator
|
||||
BASE_EXPORT bool IsFilePathSeparator(const PathChar separator);
|
||||
BASE_EXPORT bool IsFilePathSeparator(const PathString &separator);
|
||||
|
||||
// Get the file extension from filepath
|
||||
BASE_EXPORT bool FilePathExtension(const PathString &filepath_in,
|
||||
PathString &extension_out);
|
||||
|
||||
// Get the directory from the whole filepath
|
||||
BASE_EXPORT bool FilePathApartDirectory(const PathString &filepath_in,
|
||||
PathString &directory_out);
|
||||
// Get the filename from the whole filepath
|
||||
BASE_EXPORT bool FilePathApartFileName(const PathString &filepath_in,
|
||||
PathString &filename_out);
|
||||
|
||||
// Parse and extract all components of a path
|
||||
BASE_EXPORT bool ParsePathComponents(const PathChar *PathChar,
|
||||
std::list<PathString> &components);
|
||||
|
||||
// Check if |component| is a directory
|
||||
BASE_EXPORT bool IsDirectoryComponent(const PathString &component);
|
||||
|
||||
// Compose the filepath from directory and filename
|
||||
BASE_EXPORT bool FilePathCompose(const PathString &directory_in,
|
||||
const PathString &filename_in,
|
||||
PathString &filepath_out);
|
||||
|
||||
// Get the current filepath
|
||||
BASE_EXPORT bool FilePathCurrentDirectory(PathString &directory_out);
|
||||
|
||||
// Check the filepath is exist
|
||||
// If |is_directory| is true, check a directory, or check the path
|
||||
BASE_EXPORT bool FilePathIsExist(const PathChar *filepath_in,
|
||||
bool is_directory);
|
||||
BASE_EXPORT bool FilePathIsExist(const PathString &filepath_in,
|
||||
bool is_directory);
|
||||
|
||||
// Create a directory, all subdirs will be created if not existing
|
||||
BASE_EXPORT bool CreateDirectory(const PathChar* full_path);
|
||||
BASE_EXPORT bool CreateDirectory(const PathString& full_path);
|
||||
|
||||
// Wrapper for fopen-like calls. Returns non-NULL FILE* on success
|
||||
BASE_EXPORT FILE* OpenFile(const PathChar *filepath, const PathChar *mode);
|
||||
BASE_EXPORT FILE* OpenFile(const PathString &filepath, const PathChar *mode);
|
||||
|
||||
// Closes file opened by OpenFile. Returns true on success
|
||||
BASE_EXPORT bool CloseFile(FILE *file);
|
||||
|
||||
// Reads the given number of bytes from the file into the buffer
|
||||
BASE_EXPORT int ReadFile(const PathChar *filepath,
|
||||
void *data_out,
|
||||
size_t size);
|
||||
BASE_EXPORT int ReadFile(const PathString &filepath,
|
||||
void *data_out,
|
||||
size_t size);
|
||||
|
||||
|
||||
// Read the file at |path| into |contents|, returning true on success.
|
||||
BASE_EXPORT bool ReadFileToString(const PathString &filepath,
|
||||
std::string &contents_out);
|
||||
|
||||
// Writes the content of given buffer into the file
|
||||
BASE_EXPORT int WriteFile(const PathChar *filepath,
|
||||
const void *data,
|
||||
size_t size);
|
||||
BASE_EXPORT int WriteFile(const PathString &filepath, const std::string &data);
|
||||
|
||||
// Copies a single file.
|
||||
BASE_EXPORT bool CopyFile(const PathString &from_path,
|
||||
const PathString &to_path);
|
||||
|
||||
// Deletes the given path.
|
||||
BASE_EXPORT bool DeleteFile(const PathString &filepath);
|
||||
|
||||
// Get file size.
|
||||
BASE_EXPORT int64_t GetFileSize(const PathString &filepath);
|
||||
|
||||
} // namespace nbase
|
||||
|
||||
#endif // BASE_FILE_FILE_UTIL_H_
|
||||
@@ -0,0 +1,236 @@
|
||||
// Copyright (c) 2011, NetEase Inc. All rights reserved.
|
||||
//
|
||||
// Author: rl
|
||||
// Date: 2011/6/12
|
||||
//
|
||||
// Modified by wrt(guangguang)
|
||||
// Date: 2013/9/5
|
||||
//
|
||||
// Utilities for file and filepath operation
|
||||
|
||||
#include "file_util.h"
|
||||
#include "base/util/string_util.h"
|
||||
|
||||
#if defined(OS_POSIX)
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/errno.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
|
||||
typedef struct stat stat_wrapper_t;
|
||||
static int CallStat(const char *path, stat_wrapper_t *sb)
|
||||
{
|
||||
return stat(path, sb);
|
||||
}
|
||||
|
||||
static int CallLstat(const char *path, stat_wrapper_t *sb)
|
||||
{
|
||||
return lstat(path, sb);
|
||||
}
|
||||
|
||||
bool FilePathCurrentDirectory(std::string &directory_out)
|
||||
{
|
||||
char directory[512] = "";
|
||||
if (!getcwd(directory, sizeof(directory)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
directory_out = directory;
|
||||
directory_out.append(1, kFilePathSeparators[0]);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FilePathIsExist(const char *filepath_in, bool is_directory)
|
||||
{
|
||||
if (!is_directory)
|
||||
return access(filepath_in, F_OK) == 0;
|
||||
else
|
||||
{
|
||||
DIR *directory = opendir(filepath_in);
|
||||
if (directory != NULL)
|
||||
{
|
||||
closedir(directory);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CreateDirectory(const std::string& full_path)
|
||||
{
|
||||
return CreateDirectory(full_path.c_str());
|
||||
}
|
||||
|
||||
bool CreateDirectory(const char* full_path)
|
||||
{
|
||||
if (full_path == NULL)
|
||||
return false;
|
||||
|
||||
std::list<std::string> subpaths;
|
||||
ParsePathComponents(full_path, subpaths);
|
||||
if (subpaths.empty())
|
||||
return false;
|
||||
|
||||
// Collect a list of all parent directories.
|
||||
auto curr = subpaths.begin();
|
||||
for (auto prev = curr++; curr != subpaths.end(); curr++) {
|
||||
*curr = *prev + *curr;
|
||||
prev = curr;
|
||||
}
|
||||
|
||||
// Iterate through the parents and create the missing ones.
|
||||
for (auto i = subpaths.begin(); i != subpaths.end(); ++i) {
|
||||
if (FilePathIsExist(i->c_str(), true))
|
||||
continue;
|
||||
if (mkdir(i->c_str(), 0700) == 0)
|
||||
continue;
|
||||
// Mkdir failed, but it might have failed with EEXIST, or some other
|
||||
// error due to the the directory appearing out of thin air. This can
|
||||
// occur if two processes are trying to create the same file system tree
|
||||
// at the same time. Check to see if it exists and make sure it is a
|
||||
// directory.
|
||||
if (!FilePathIsExist(i->c_str(), true))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
FILE* OpenFile(const char *filepath, const char *mode)
|
||||
{
|
||||
FILE* file = NULL;
|
||||
do
|
||||
{
|
||||
file = fopen(filepath, mode);
|
||||
} while (!file && errno == EINTR);
|
||||
return file;
|
||||
}
|
||||
|
||||
int ReadFile(const char *filepath, void *data_out, size_t size)
|
||||
{
|
||||
int fd = open(filepath, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
ssize_t bytes_read = read(fd, data_out, size);
|
||||
if (int ret = close(fd) < 0)
|
||||
return ret;
|
||||
return bytes_read;
|
||||
}
|
||||
|
||||
int WriteFile(const char *filepath, const void *data, size_t size)
|
||||
{
|
||||
int fd = creat(filepath, 0666);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
ssize_t bytes_written_total = 0;
|
||||
for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
|
||||
bytes_written_total += bytes_written_partial)
|
||||
{
|
||||
bytes_written_partial =
|
||||
write(fd, data + bytes_written_total,
|
||||
size - bytes_written_total);
|
||||
if (bytes_written_partial < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (int ret = close(fd) < 0)
|
||||
return ret;
|
||||
|
||||
return bytes_written_total;
|
||||
}
|
||||
|
||||
bool CopyFile(const std::string &from_path, const std::string &to_path)
|
||||
{
|
||||
int infile = open(from_path.c_str(), O_RDONLY);
|
||||
if (infile < 0)
|
||||
return false;
|
||||
|
||||
int outfile = creat(to_path.c_str(), 0666);
|
||||
if (outfile < 0)
|
||||
{
|
||||
close(infile);
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t kBufferSize = 32768;
|
||||
std::vector<char> buffer(kBufferSize);
|
||||
bool result = true;
|
||||
|
||||
while (result)
|
||||
{
|
||||
ssize_t bytes_read = read(infile, &buffer[0], buffer.size());
|
||||
if (bytes_read < 0)
|
||||
{
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
if (bytes_read == 0)
|
||||
break;
|
||||
// Allow for partial writes
|
||||
ssize_t bytes_written_per_read = 0;
|
||||
do
|
||||
{
|
||||
ssize_t bytes_written_partial = write(
|
||||
outfile,
|
||||
&buffer[bytes_written_per_read],
|
||||
bytes_read - bytes_written_per_read);
|
||||
if (bytes_written_partial < 0)
|
||||
{
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
bytes_written_per_read += bytes_written_partial;
|
||||
} while (bytes_written_per_read < bytes_read);
|
||||
}
|
||||
|
||||
if (close(infile) < 0)
|
||||
result = false;
|
||||
if (close(outfile) < 0)
|
||||
result = false;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool DeleteFile(const std::string &filepath)
|
||||
{
|
||||
const char* path_str = filepath.c_str();
|
||||
stat_wrapper_t file_info;
|
||||
int test = CallStat(path_str, &file_info);
|
||||
if (test != 0)
|
||||
{
|
||||
// The Windows version defines this condition as success.
|
||||
bool ret = (errno == ENOENT || errno == ENOTDIR);
|
||||
return ret;
|
||||
}
|
||||
if (!S_ISDIR(file_info.st_mode))
|
||||
return (unlink(path_str) == 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int64_t GetFileSize(const std::string &filepath)
|
||||
{
|
||||
const char* path_str = filepath.c_str();
|
||||
stat_wrapper_t file_info;
|
||||
int test = CallStat(path_str, &file_info);
|
||||
if (test != 0)
|
||||
return -1;
|
||||
if (!S_ISDIR(file_info.st_mode))
|
||||
return file_info.st_size;
|
||||
return -1;
|
||||
}
|
||||
|
||||
} // namespace nbase
|
||||
|
||||
#endif // OS_POSIX
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
// Copyright (c) 2011, NetEase Inc. All rights reserved.
|
||||
//
|
||||
// Author: rl
|
||||
// Date: 2011/6/12
|
||||
//
|
||||
// Modified by wrt(guangguang)
|
||||
// Date: 2013/9/5
|
||||
//
|
||||
// Utilities for file and filepath operation
|
||||
|
||||
#include "file_util.h"
|
||||
#include "base/util/string_util.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
#include <windows.h>
|
||||
#include "base/win32/scoped_win_handle.h"
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
|
||||
bool FilePathCurrentDirectory(PathString &directory_out)
|
||||
{
|
||||
PathChar directory[MAX_PATH];
|
||||
directory[0] = 0;
|
||||
DWORD len = ::GetCurrentDirectoryW(MAX_PATH, directory);
|
||||
if (len == 0 || len > MAX_PATH)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
directory_out = directory;
|
||||
directory_out.append(1, kFilePathSeparators[0]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FilePathIsExist(const PathChar *filepath_in, bool is_directory)
|
||||
{
|
||||
const DWORD file_attr = ::GetFileAttributesW(filepath_in);
|
||||
if (file_attr != INVALID_FILE_ATTRIBUTES)
|
||||
{
|
||||
if (is_directory)
|
||||
return (file_attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CreateDirectory(const PathString& full_path)
|
||||
{
|
||||
return CreateDirectory(full_path.c_str());
|
||||
}
|
||||
|
||||
bool CreateDirectory(const PathChar* full_path)
|
||||
{
|
||||
if (full_path == nullptr)
|
||||
return false;
|
||||
|
||||
if (FilePathIsExist(full_path, true))
|
||||
return true;
|
||||
|
||||
std::list<PathString> subpaths;
|
||||
ParsePathComponents(full_path, subpaths);
|
||||
if (subpaths.empty())
|
||||
return false;
|
||||
|
||||
// Collect a list of all parent directories.
|
||||
auto curr = subpaths.begin();
|
||||
for (auto prev = curr++; curr != subpaths.end(); curr++) {
|
||||
*curr = *prev + *curr;
|
||||
prev = curr;
|
||||
}
|
||||
|
||||
// Iterate through the parents and create the missing ones.
|
||||
for (auto i = subpaths.begin(); i != subpaths.end(); ++i) {
|
||||
if (FilePathIsExist(i->c_str(), true))
|
||||
continue;
|
||||
if (!::CreateDirectoryW(i->c_str(), NULL)) {
|
||||
DWORD error = ::GetLastError();
|
||||
if (error == ERROR_ALREADY_EXISTS &&
|
||||
FilePathIsExist(i->c_str(), true)) {
|
||||
continue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
FILE* OpenFile(const PathChar *filepath, const PathChar *mode)
|
||||
{
|
||||
return _wfsopen(filepath, mode, _SH_DENYNO);
|
||||
}
|
||||
|
||||
int ReadFile(const PathChar *filepath, void *data_out, size_t size)
|
||||
{
|
||||
win32::ScopedWinHandle file(CreateFileW(filepath,
|
||||
GENERIC_READ,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_SEQUENTIAL_SCAN,
|
||||
NULL));
|
||||
if (!file)
|
||||
return -1;
|
||||
|
||||
DWORD read;
|
||||
if (::ReadFile(file, data_out, (DWORD)size, &read, NULL) &&
|
||||
static_cast<int>(read) == size)
|
||||
return read;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int WriteFile(const PathChar *filepath, const void *data, size_t size)
|
||||
{
|
||||
win32::ScopedWinHandle file(CreateFileW(filepath,
|
||||
GENERIC_WRITE,
|
||||
0,
|
||||
NULL,
|
||||
CREATE_ALWAYS,
|
||||
0,
|
||||
NULL));
|
||||
if (!file)
|
||||
return -1;
|
||||
|
||||
DWORD written;
|
||||
BOOL result = ::WriteFile(file, data, (DWORD)size, &written, NULL);
|
||||
if (result && static_cast<int>(written) == size)
|
||||
return written;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool CopyFile(const PathString &from_path, const PathString &to_path)
|
||||
{
|
||||
if (from_path.size() >= MAX_PATH ||
|
||||
to_path.size() >= MAX_PATH) {
|
||||
return false;
|
||||
}
|
||||
return (::CopyFileW(from_path.c_str(), to_path.c_str(),
|
||||
false) != 0);
|
||||
}
|
||||
|
||||
bool DeleteFile(const PathString &filepath)
|
||||
{
|
||||
if (::DeleteFileW(filepath.c_str()) != 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t GetFileSize(const PathString &filepath)
|
||||
{
|
||||
WIN32_FIND_DATAW file_data;
|
||||
HANDLE file = FindFirstFileW(filepath.c_str(), &file_data);
|
||||
|
||||
if (file == INVALID_HANDLE_VALUE)
|
||||
return -1;
|
||||
LARGE_INTEGER li = { file_data.nFileSizeLow, (LONG)file_data.nFileSizeHigh };
|
||||
FindClose(file);
|
||||
return li.QuadPart;
|
||||
}
|
||||
|
||||
} // namespace nbase
|
||||
|
||||
#endif // OS_WIN
|
||||
|
||||
Reference in new issue
Block a user