mirror of
https://onedev.site.tesses.net/tesses-framework
synced 2026-02-08 15:55:46 +00:00
Add fshelpers and temp
This commit is contained in:
@@ -16,23 +16,23 @@ namespace Tesses::Framework::Date
|
||||
int minute=0;
|
||||
int second=0;
|
||||
bool isLocal=false;
|
||||
int64_t ToEpochNoConvert();
|
||||
int64_t ToEpochNoConvert() const;
|
||||
void FromEpochNoConvert(int64_t gmt);
|
||||
public:
|
||||
DateTime();
|
||||
DateTime(int year, int month, int day, int hour, int minute, int seconds, bool isLocal=true);
|
||||
DateTime(int64_t epoch);
|
||||
int Year();
|
||||
int Month();
|
||||
int Day();
|
||||
int Hour();
|
||||
int Minute();
|
||||
int Second();
|
||||
int DayOfWeek();
|
||||
bool IsLocal();
|
||||
int64_t ToEpoch();
|
||||
DateTime ToLocal();
|
||||
DateTime ToUTC();
|
||||
int Year() const;
|
||||
int Month() const;
|
||||
int Day() const;
|
||||
int Hour() const;
|
||||
int Minute() const;
|
||||
int Second() const;
|
||||
int DayOfWeek() const;
|
||||
bool IsLocal() const;
|
||||
int64_t ToEpoch() const;
|
||||
DateTime ToLocal() const;
|
||||
DateTime ToUTC() const;
|
||||
void SetToLocal();
|
||||
void SetToUTC();
|
||||
void SetYear(int y);
|
||||
@@ -52,15 +52,105 @@ namespace Tesses::Framework::Date
|
||||
static DateTime Now();
|
||||
static DateTime NowUTC();
|
||||
|
||||
std::string ToString();
|
||||
std::string ToString(std::string fmt);
|
||||
std::string ToString() const;
|
||||
std::string ToString(std::string fmt) const;
|
||||
|
||||
|
||||
std::string ToHttpDate();
|
||||
std::string ToHttpDate() const;
|
||||
static bool TryParseHttpDate(std::string txt, DateTime& dt);
|
||||
|
||||
operator std::string() const
|
||||
{
|
||||
return ToString();
|
||||
}
|
||||
|
||||
|
||||
operator int64_t() const
|
||||
{
|
||||
return ToEpoch();
|
||||
}
|
||||
};
|
||||
class TimeSpan {
|
||||
int64_t totalSeconds;
|
||||
public:
|
||||
TimeSpan();
|
||||
TimeSpan(int64_t totalSeconds);
|
||||
TimeSpan(int hours, int minutes, int seconds);
|
||||
TimeSpan(int days,int hours, int minutes, int seconds);
|
||||
|
||||
|
||||
void Set(int days, int hours, int minutes, int seconds);
|
||||
void Set(int hours, int minutes, int seconds);
|
||||
|
||||
void SetDays(int d);
|
||||
void SetHours(int h);
|
||||
void SetMinutes(int m);
|
||||
void SetSeconds(int s);
|
||||
|
||||
|
||||
int Days() const;
|
||||
int Hours() const;
|
||||
int Minutes() const;
|
||||
int Seconds() const;
|
||||
|
||||
|
||||
int64_t TotalSeconds() const;
|
||||
int64_t TotalMinutes() const;
|
||||
int64_t TotalHours() const;
|
||||
|
||||
|
||||
void SetTotalSeconds(int64_t totalSeconds);
|
||||
void SetTotalMinutes(int64_t totalMinutes);
|
||||
void SetTotalHours(int64_t totalHours);
|
||||
|
||||
void AddSeconds(int64_t seconds);
|
||||
void AddMinutes(int64_t minutes);
|
||||
void AddHours(int64_t hours);
|
||||
void AddDays(int64_t days);
|
||||
|
||||
std::string ToString(bool slim=true) const;
|
||||
|
||||
static bool TryParse(std::string text, TimeSpan& span);
|
||||
|
||||
operator int64_t() const
|
||||
{
|
||||
return TotalSeconds();
|
||||
}
|
||||
|
||||
operator std::string()
|
||||
{
|
||||
return ToString();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
DateTime operator+(const DateTime& dt, const TimeSpan& ts)
|
||||
{
|
||||
DateTime dt2(dt.ToEpoch() + ts.TotalSeconds());
|
||||
if(dt.IsLocal())
|
||||
dt2.SetToLocal();
|
||||
return dt2;
|
||||
}
|
||||
DateTime operator+(const TimeSpan& ts,const DateTime& dt)
|
||||
{
|
||||
DateTime dt2(dt.ToEpoch() + ts.TotalSeconds());
|
||||
if(dt.IsLocal())
|
||||
dt2.SetToLocal();
|
||||
return dt2;
|
||||
}
|
||||
TimeSpan operator+(const TimeSpan& ts, const TimeSpan& ts2)
|
||||
{
|
||||
return (int64_t)ts + (int64_t)ts2;
|
||||
}
|
||||
DateTime operator-(const DateTime& dt, const TimeSpan& ts)
|
||||
{
|
||||
DateTime dt2(dt.ToEpoch() - ts.TotalSeconds());
|
||||
if(dt.IsLocal())
|
||||
dt2.SetToLocal();
|
||||
return dt2;
|
||||
}
|
||||
TimeSpan operator-(const DateTime& dt, const DateTime& dt2)
|
||||
{
|
||||
return (int64_t)dt - (int64_t)dt2;
|
||||
}
|
||||
|
||||
};
|
||||
33
include/TessesFramework/Filesystem/FSHelpers.hpp
Normal file
33
include/TessesFramework/Filesystem/FSHelpers.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include "TempFS.hpp"
|
||||
#include "VFSFix.hpp"
|
||||
|
||||
namespace Tesses::Framework::Filesystem::Helpers
|
||||
{
|
||||
void ReadAllText(std::shared_ptr<VFS> vfs, VFSPath path, std::string& text);
|
||||
void ReadAllLines(std::shared_ptr<VFS> vfs, VFSPath path, std::vector<std::string>& lines);
|
||||
void ReadAllBytes(std::shared_ptr<VFS> vfs, VFSPath path, std::vector<uint8_t>& array);
|
||||
std::string ReadAllText(std::shared_ptr<VFS> vfs, VFSPath path);
|
||||
std::vector<std::string> ReadAllLines(std::shared_ptr<VFS> vfs, VFSPath path);
|
||||
std::vector<uint8_t> ReadAllBytes(std::shared_ptr<VFS> vfs, VFSPath path);
|
||||
void WriteAllText(std::shared_ptr<VFS> vfs, VFSPath path, const std::string& text);
|
||||
void WriteAllLines(std::shared_ptr<VFS> vfs, VFSPath path, const std::vector<std::string>& parts);
|
||||
void WriteAllBytes(std::shared_ptr<VFS> vfs, VFSPath path, const std::vector<uint8_t>& bytes);
|
||||
|
||||
void CopyStreamProgress(std::shared_ptr<Streams::Stream> src,std::shared_ptr<Streams::Stream> dest, std::function<void(int64_t offset, int64_t length)> progress);
|
||||
|
||||
void CopyFile(std::shared_ptr<VFS> vfsSrc, VFSPath pathSrc, std::shared_ptr<VFS> vfsDest, VFSPath pathDest, bool overwrite=true);
|
||||
void CopyFile(std::shared_ptr<VFS> vfsSrc, VFSPath pathSrc, std::shared_ptr<VFS> vfsDest, VFSPath pathDest, std::function<void(int64_t offset, int64_t length)> progress, bool overwrite=true);
|
||||
void CopyDirectory(std::shared_ptr<VFS> vfsSrc, VFSPath pathSrc, std::shared_ptr<VFS> vfsDest, VFSPath pathDest, bool overwrite=true);
|
||||
void CopyDirectory(std::shared_ptr<VFS> vfsSrc, VFSPath pathSrc, std::shared_ptr<VFS> vfsDest, VFSPath pathDest, std::function<void(int64_t offset, int64_t length, VFSPath currentFile)> progress, bool overwrite=true);
|
||||
|
||||
inline std::shared_ptr<TempFS> CreateTempFS(bool deleteOnDestroy=true)
|
||||
{
|
||||
return std::make_shared<TempFS>(deleteOnDestroy);
|
||||
}
|
||||
inline std::shared_ptr<TempFS> CreateTempFS(std::shared_ptr<VFS> vfs,bool deleteOnDestroy=true)
|
||||
{
|
||||
return std::make_shared<TempFS>(vfs,deleteOnDestroy);
|
||||
}
|
||||
|
||||
}
|
||||
51
include/TessesFramework/Filesystem/TempFS.hpp
Normal file
51
include/TessesFramework/Filesystem/TempFS.hpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
#include "../Common.hpp"
|
||||
#include "VFS.hpp"
|
||||
#include "VFSFix.hpp"
|
||||
namespace Tesses::Framework::Filesystem
|
||||
{
|
||||
void UniqueString(std::string& text);
|
||||
|
||||
class TempFS : public VFS
|
||||
{
|
||||
std::shared_ptr<VFS> vfs;
|
||||
bool deleteOnDestroy;
|
||||
std::shared_ptr<VFS> parent;
|
||||
std::string tmp_str;
|
||||
public:
|
||||
std::string TempDirectoryName();
|
||||
TempFS(bool deleteOnDestroy=true);
|
||||
TempFS(std::shared_ptr<VFS> vfs,bool deleteOnDestroy=true);
|
||||
|
||||
std::shared_ptr<Tesses::Framework::Streams::Stream> OpenFile(VFSPath path, std::string mode);
|
||||
void CreateDirectory(VFSPath path);
|
||||
void DeleteDirectory(VFSPath path);
|
||||
bool SpecialFileExists(VFSPath path);
|
||||
bool FileExists(VFSPath path);
|
||||
bool RegularFileExists(VFSPath path);
|
||||
bool SymlinkExists(VFSPath path);
|
||||
bool CharacterDeviceExists(VFSPath path);
|
||||
bool BlockDeviceExists(VFSPath path);
|
||||
bool SocketFileExists(VFSPath path);
|
||||
bool FIFOFileExists(VFSPath path);
|
||||
bool DirectoryExists(VFSPath path);
|
||||
void DeleteFile(VFSPath path);
|
||||
void CreateSymlink(VFSPath existingFile, VFSPath symlinkFile);
|
||||
VFSPathEnumerator EnumeratePaths(VFSPath path);
|
||||
void CreateHardlink(VFSPath existingFile, VFSPath newName);
|
||||
void MoveFile(VFSPath src, VFSPath dest);
|
||||
void MoveDirectory(VFSPath src, VFSPath dest);
|
||||
void DeleteDirectoryRecurse(VFSPath path);
|
||||
VFSPath ReadLink(VFSPath path);
|
||||
std::string VFSPathToSystem(VFSPath path);
|
||||
VFSPath SystemToVFSPath(std::string path);
|
||||
|
||||
void GetDate(VFSPath path, Date::DateTime& lastWrite, Date::DateTime& lastAccess);
|
||||
void SetDate(VFSPath path, Date::DateTime lastWrite, Date::DateTime lastAccess);
|
||||
bool StatVFS(VFSPath path, StatVFSData& vfsData);
|
||||
|
||||
void Chmod(VFSPath path, uint32_t mode);
|
||||
void Close();
|
||||
~TempFS();
|
||||
};
|
||||
}
|
||||
@@ -29,6 +29,8 @@ namespace Tesses::Framework::Filesystem
|
||||
static std::vector<std::string> SplitPath(std::string path);
|
||||
std::vector<std::string> path;
|
||||
VFSPath();
|
||||
VFSPath(const char* path) : VFSPath((std::string)path)
|
||||
{}
|
||||
VFSPath(std::vector<std::string> path);
|
||||
VFSPath(std::string path);
|
||||
VFSPath(VFSPath p, std::string subent);
|
||||
@@ -36,22 +38,27 @@ namespace Tesses::Framework::Filesystem
|
||||
|
||||
|
||||
|
||||
VFSPath GetParent();
|
||||
VFSPath CollapseRelativeParents();
|
||||
std::string GetFileName();
|
||||
bool HasExtension();
|
||||
std::string GetExtension();
|
||||
VFSPath GetParent() const;
|
||||
VFSPath CollapseRelativeParents() const;
|
||||
std::string GetFileName() const;
|
||||
bool HasExtension() const;
|
||||
std::string GetExtension() const;
|
||||
void ChangeExtension(std::string ext);
|
||||
void RemoveExtension();
|
||||
std::string ToString();
|
||||
std::string ToString() const;
|
||||
|
||||
operator std::string() const
|
||||
{
|
||||
return ToString();
|
||||
}
|
||||
|
||||
static VFSPath GetAbsoluteCurrentDirectory();
|
||||
static void SetAbsoluteCurrentDirectory(VFSPath path);
|
||||
VFSPath MakeAbsolute();
|
||||
VFSPath MakeAbsolute() const;
|
||||
|
||||
VFSPath MakeAbsolute(VFSPath curDir);
|
||||
VFSPath MakeRelative();
|
||||
VFSPath MakeRelative(VFSPath toMakeRelativeTo);
|
||||
VFSPath MakeAbsolute(VFSPath curDir) const;
|
||||
VFSPath MakeRelative() const;
|
||||
VFSPath MakeRelative(VFSPath toMakeRelativeTo) const;
|
||||
};
|
||||
VFSPath operator/(VFSPath p, VFSPath p2);
|
||||
VFSPath operator/(VFSPath p, std::string p2);
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace Tesses::Framework::Platform::Environment
|
||||
|
||||
|
||||
namespace SpecialFolders {
|
||||
Tesses::Framework::Filesystem::VFSPath GetTemp();
|
||||
Tesses::Framework::Filesystem::VFSPath GetHomeFolder();
|
||||
Tesses::Framework::Filesystem::VFSPath GetDownloads();
|
||||
Tesses::Framework::Filesystem::VFSPath GetMusic();
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "Filesystem/NullFilesystem.hpp"
|
||||
#include "Filesystem/MountableFilesystem.hpp"
|
||||
#include "Filesystem/MemoryFilesystem.hpp"
|
||||
#include "Filesystem/FSHelpers.hpp"
|
||||
#include "Crypto/ClientTLSStream.hpp"
|
||||
#include "Crypto/MbedHelpers.hpp"
|
||||
#include "Lazy.hpp"
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace Tesses::Framework::TextStreams
|
||||
int32_t ReadChar();
|
||||
std::string ReadLine();
|
||||
bool ReadLine(std::string& str);
|
||||
void ReadAllLines(std::vector<std::string>& lines);
|
||||
std::string ReadToEnd();
|
||||
void ReadToEnd(std::string& str);
|
||||
void CopyTo(TextWriter& writer, size_t bufSz=1024);
|
||||
|
||||
Reference in New Issue
Block a user