CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/File/FileUtil.h
Views: 1401
// Copyright (C) 2003 Dolphin Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official SVN repository and contact information can be found at15// http://code.google.com/p/dolphin-emu/1617#pragma once1819#include <fstream>20#include <cstdio>21#include <string>22#include <time.h>23#include <cstdint>2425#include "Common/File/Path.h"2627// Some functions here support Android content URIs. These are marked as such.2829#ifdef _MSC_VER30inline struct tm* localtime_r(const time_t *clock, struct tm *result) {31if (localtime_s(result, clock) == 0)32return result;33return NULL;34}35#endif3637namespace File {3839// Mostly to handle UTF-8 filenames better on Windows.40FILE *OpenCFile(const Path &filename, const char *mode);4142// Reminiscent of PSP's FileAccess enum, due to its use in emulating it.43enum OpenFlag {44OPEN_NONE = 0,45OPEN_READ = 1,46OPEN_WRITE = 2,47OPEN_APPEND = 4,48OPEN_CREATE = 8,49OPEN_TRUNCATE = 16,50// EXCL?51};5253// TODO: This currently only handles Android Content URIs, but might port the rest54// of DirectoryFileSystem::Open here eventually for symmetry.55int OpenFD(const Path &filename, OpenFlag flags);5657// Resolves symlinks and similar.58std::string ResolvePath(const std::string &path);5960// Returns true if file filename exists61bool Exists(const Path &path);6263// Returns true if file filename exists in directory path.64bool ExistsInDir(const Path &path, const std::string &filename);6566// Returns true if filename exists, and is a directory67// Supports Android content URIs.68bool IsDirectory(const Path &filename);6970// Returns struct with modification date of file71bool GetModifTime(const Path &filename, tm &return_time);7273// Returns the size of filename (64bit)74uint64_t GetFileSize(const Path &filename);7576// Overloaded GetSize, accepts FILE*77uint64_t GetFileSize(FILE *f);7879// Computes the recursive size of a directory. Warning: Might be slow!80uint64_t ComputeRecursiveDirectorySize(const Path &path);8182// Returns true if successful, or path already exists.83bool CreateDir(const Path &filename);8485void ChangeMTime(const Path &path, time_t mtime);8687// Creates the full path of fullPath returns true on success88bool CreateFullPath(const Path &fullPath);8990// Deletes a given file by name, return true on success91// Doesn't support deleting a directory (although it will work on some platforms - ideally shouldn't)92bool Delete(const Path &filename);9394// Deletes a directory by name, returns true on success95// Directory must be empty.96bool DeleteDir(const Path &filename);9798// Deletes the given directory and anything under it. Returns true on success.99bool DeleteDirRecursively(const Path &directory);100101// Renames/moves file srcFilename to destFilename, returns true on success102// Will usually only work with in the same partition or other unit of storage,103// so you might have to fall back to copy/delete.104bool Rename(const Path &srcFilename, const Path &destFilename);105106// copies file srcFilename to destFilename, returns true on success107bool Copy(const Path &srcFilename, const Path &destFilename);108109// Tries to rename srcFilename to destFilename, if that fails,110// it tries to copy and delete the src if succeeded. If that fails too,111// returns false, otherwise returns true.112bool Move(const Path &srcFilename, const Path &destFilename);113114// Move file, but only if it can be done quickly (rename or similar).115bool MoveIfFast(const Path &srcFilename, const Path &destFilename);116117// creates an empty file filename, returns true on success118bool CreateEmptyFile(const Path &filename);119120// Opens ini file (cheats, texture replacements etc.)121// TODO: Belongs in System or something.122bool OpenFileInEditor(const Path &fileName);123124// Uses some heuristics to determine if this is a folder that we would want to125// write to.126bool IsProbablyInDownloadsFolder(const Path &folder);127128// TODO: Belongs in System or something.129const Path &GetExeDirectory();130131const Path GetCurDirectory();132133// simple wrapper for cstdlib file functions to134// hopefully will make error checking easier135// and make forgetting an fclose() harder136class IOFile {137public:138IOFile() {}139IOFile(const Path &filename, const char openmode[]);140~IOFile();141142// Prevent copies.143IOFile(const IOFile &) = delete;144void operator=(const IOFile &) = delete;145146bool Open(const Path &filename, const char openmode[]);147bool Close();148149template <typename T>150bool ReadArray(T* data, size_t length)151{152if (!IsOpen() || length != std::fread(data, sizeof(T), length, m_file))153m_good = false;154155return m_good;156}157158template <typename T>159bool WriteArray(const T* data, size_t length)160{161if (!IsOpen() || length != std::fwrite(data, sizeof(T), length, m_file))162m_good = false;163164return m_good;165}166167bool ReadBytes(void* data, size_t length)168{169return ReadArray(reinterpret_cast<char*>(data), length);170}171172bool WriteBytes(const void* data, size_t length)173{174return WriteArray(reinterpret_cast<const char*>(data), length);175}176177bool IsOpen() const { return nullptr != m_file; }178179// m_good is set to false when a read, write or other function fails180bool IsGood() const { return m_good; }181operator bool() const { return IsGood() && IsOpen(); }182183std::FILE* ReleaseHandle();184185std::FILE* GetHandle() { return m_file; }186187void SetHandle(std::FILE* file);188189bool Seek(int64_t off, int origin);190uint64_t Tell();191uint64_t GetSize();192bool Resize(uint64_t size);193bool Flush();194195// clear error state196void Clear() {197m_good = true;198#undef clearerr199std::clearerr(m_file);200}201202private:203std::FILE *m_file = nullptr;204bool m_good = true;205};206207// TODO: Refactor, this was moved from the old file_util.cpp.208209// Whole-file reading/writing210bool WriteStringToFile(bool textFile, const std::string &str, const Path &filename);211bool WriteDataToFile(bool textFile, const void* data, size_t size, const Path &filename);212213bool ReadFileToStringOptions(bool textFile, bool allowShort, const Path &path, std::string *str);214215// Wrappers that clarify the intentions.216inline bool ReadBinaryFileToString(const Path &path, std::string *str) {217return ReadFileToStringOptions(false, false, path, str);218}219inline bool ReadSysTextFileToString(const Path &path, std::string *str) {220return ReadFileToStringOptions(true, true, path, str);221}222inline bool ReadTextFileToString(const Path &path, std::string *str) {223return ReadFileToStringOptions(true, false, path, str);224}225226// Return value must be delete[]-d.227uint8_t *ReadLocalFile(const Path &path, size_t *size);228229} // namespace230231232