⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileutils.cc

📁 本人收集整理的一份c/c++跨平台网络库
💻 CC
字号:
#include <errno.h>#include <cassert>#ifdef WIN32#include "convert.h"#endif#include "pathutils.h"#include "fileutils.h"#include "stringutils.h"#include "stream.h"#include "unixfilesystem.h"#include "win32filesystem.h"#ifndef WIN32#define MAX_PATH 256#endifnamespace utils_base {//////////////////////////// Directory Iterator   ////////////////////////////// A Directoryraverser is created with a given directory. It originally points to// the first file in the directory, and can be advanecd with Next(). This allows you// to get information about each file.  // ConstructorDirectoryIterator::DirectoryIterator() : #ifdef _WIN32  handle_(INVALID_HANDLE_VALUE)#else  dir_(NULL), dirent_(NULL)#endif{}  // DestructorDirectoryIterator::~DirectoryIterator() {#ifdef WIN32  if (handle_ != INVALID_HANDLE_VALUE)    ::FindClose(handle_);#else  if (dir_)    closedir(dir_);#endif}  // Starts traversing a directory.  // dir is the directory to traverse  // returns true if the directory exists and is validbool DirectoryIterator::Iterate(const Pathname &dir) {  directory_ = dir.pathname();#ifdef WIN32  if (handle_ != INVALID_HANDLE_VALUE)    ::FindClose(handle_);  std::string d = dir.pathname() + '*';  handle_ = ::FindFirstFile(Utf16(d).AsWz(), &data_);  if (handle_ == INVALID_HANDLE_VALUE)    return false;#else  if (dir_ != NULL)    closedir(dir_);  dir_ = ::opendir(directory_.c_str());  if (dir_ == NULL)    return false;  dirent_ = readdir(dir_);  if (dirent_ == NULL)    return false;    if (::stat(std::string(directory_ + Name()).c_str(), &stat_) != 0)    return false;#endif  return true;}  // Advances to the next file  // returns true if there were more files in the directory.bool DirectoryIterator::Next() {#ifdef WIN32  return ::FindNextFile(handle_, &data_) == TRUE;#else  dirent_ = ::readdir(dir_);  if (dirent_ == NULL)    return false;  return ::stat(std::string(directory_ + Name()).c_str(), &stat_) == 0;#endif}  // returns true if the file currently pointed to is a directorybool DirectoryIterator::IsDirectory() const {#ifdef WIN32  return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE;#else  return S_ISDIR(stat_.st_mode);#endif}  // returns the name of the file currently pointed tostd::string DirectoryIterator::Name() const {#ifdef WIN32  return Utf8(data_.cFileName).AsString();#else  assert(dirent_ != NULL);  return dirent_->d_name;#endif}  // returns the size of the file currently pointed tosize_t DirectoryIterator::FileSize() const {#ifndef WIN32      	return stat_.st_size;#else	return data_.nFileSizeLow;#endif}   // returns the last modified time of this filetime_t DirectoryIterator::FileModifyTime() const {#ifdef WIN32return 0;#else  return stat_.st_mtime;#endif}Filesystem *Filesystem::default_filesystem_ = 0;  bool Filesystem::CreateFolder(const Pathname &pathname){  return EnsureDefaultFilesystem()->CreateFolderI(pathname);}FileStream *Filesystem::OpenFile(const Pathname &filename, const std::string &mode){  return EnsureDefaultFilesystem()->OpenFileI(filename, mode);}bool Filesystem::DeleteFile(const Pathname &filename){  return EnsureDefaultFilesystem()->DeleteFileI(filename);}bool Filesystem::MoveFile(const Pathname &old_path, const Pathname &new_path){  return EnsureDefaultFilesystem()->MoveFileI(old_path, new_path);}bool Filesystem::CopyFile(const Pathname &old_path, const Pathname &new_path){  return EnsureDefaultFilesystem()->CopyFileI(old_path, new_path);}bool Filesystem::IsFolder(const Pathname& pathname){  return EnsureDefaultFilesystem()->IsFolderI(pathname);}bool Filesystem::FileExists(const Pathname& pathname){  return EnsureDefaultFilesystem()->FileExistsI(pathname);}bool Filesystem::IsTemporaryPath(const Pathname& pathname){  return EnsureDefaultFilesystem()->IsTemporaryPathI(pathname);}bool Filesystem::GetTemporaryFolder(Pathname &path, bool create,			       const std::string *append){  return EnsureDefaultFilesystem()->GetTemporaryFolderI(path,create, append);}std::string Filesystem::TempFilename(const Pathname &dir, const std::string &prefix){  return EnsureDefaultFilesystem()->TempFilenameI(dir, prefix);}bool Filesystem::GetFileSize(const Pathname &dir, size_t *size){  return EnsureDefaultFilesystem()->GetFileSizeI(dir, size);}Filesystem *Filesystem::EnsureDefaultFilesystem(){  if (!default_filesystem_)#ifdef WIN32    default_filesystem_ = new Win32Filesystem();#else    default_filesystem_ = new UnixFilesystem();#endif    return default_filesystem_;}bool CreateUniqueFile(Pathname& path, bool create_empty) {  LOG(LS_INFO) << "Path " << path.pathname() << std::endl;  // If not folder is supplied, use the temporary folder  if (path.folder().empty()) {    Pathname temporary_path;    if (!Filesystem::GetTemporaryFolder(temporary_path, true, NULL)) {      printf("Get temp failed\n");      return false;    }    path.SetFolder(temporary_path.pathname());  }  // If not filename is supplied, use a temporary name  if (path.filename().empty()) {    std::string folder(path.folder());    std::string filename = Filesystem::TempFilename(folder, "gt");    path.SetFilename(filename);    if (!create_empty) {      Filesystem::DeleteFile(path.pathname());    }    return true;  }    // Otherwise, create a unique name based on the given filename  // foo.txt -> foo-N.txt  const std::string basename = path.basename();  const size_t MAX_VERSION = 100;  size_t version = 0;  while (version < MAX_VERSION) {    std::string pathname = path.pathname();     if (!Filesystem::FileExists(pathname)) {      if (create_empty) {        FileStream* fs = Filesystem::OpenFile(pathname,"w");	delete fs;      }      return true;    }    version += 1;    char version_base[MAX_PATH];    utils_base::sprintfn(version_base, ARRAY_SIZE(version_base), "%s-%u",                        basename.c_str(), version);    path.SetBasename(version_base);  }  return true;}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -