This commit is contained in:
2025-09-24 10:53:28 +08:00
commit f8e4df77fb
856 changed files with 140098 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
#ifndef __UT_DIRECTORY_HPP__
#define __UT_DIRECTORY_HPP__
#include <unitree/common/filesystem/filesystem.hpp>
namespace unitree
{
namespace common
{
typedef std::pair<int32_t,std::string> DIRENT_INFO;
typedef std::pair<std::string,std::string> SYMLNK_INFO;
class Directory
{
public:
Directory();
Directory(const std::string& dirName);
~Directory();
void Open();
void Open(const std::string& dirName);
bool IsOpen();
void ListFile(std::vector<std::string>& fileNameList, const std::string& regExpress);
void ListFile(std::vector<std::string>& fileNameList, bool recurse = true, bool absolute = true);
void ListDir(std::vector<std::string>& dirNameList, bool recurse = true, bool absolute = true);
void List(std::list<std::string>& fileNameList, std::list<std::string>& dirNameList,
std::list<SYMLNK_INFO>& symlinkList);
void Cleanup();
bool CopyTo(const std::string& dirName, bool deeply = false);
void Close();
private:
void CheckOpen();
void OpenInner();
private:
DIR *mDIR;
std::string mDirName;
};
typedef std::shared_ptr<Directory> DirectoryPtr;
bool ExistDirectory(const std::string& dirName);
void CreateDirectory(const std::string& dirName, bool recurse = true,
uint32_t mode = UT_OPEN_MODE_RWX);
void ListDirectory(const std::string& dirName, std::vector<std::string>& fileNameList,
const std::string& regExpress);
void ListDirectory(const std::string& dirName, std::vector<std::string>& fileNameList, bool recurse = true, bool absolute = true);
void ListChildDirectory(const std::string& dirName, std::vector<std::string>& dirNameList, bool recurse = true, bool absolute = true);
void RemoveDirectory(const std::string& dirName, bool recurse = true);
void CopyDirectory(const std::string& dirName, const std::string& destDirName, bool deeply);
}
}
#endif//__UT_DIRECTORY_HPP__

View File

@@ -0,0 +1,127 @@
#ifndef __UT_FILE_HPP__
#define __UT_FILE_HPP__
#include <unitree/common/filesystem/filesystem.hpp>
namespace unitree
{
namespace common
{
class File
{
public:
File();
File(const std::string& fileName);
File(const std::string& fileName, int32_t flag);
File(const std::string& fileName, int32_t flag, uint32_t mode);
virtual ~File();
int32_t GetFd() const;
bool IsOpen() const;
void Open();
void Open(int32_t flag, uint32_t mode = UT_OPEN_MODE_NONE);
void Open(const std::string& fileName, int32_t flag = UT_OPEN_FLAG_R,
uint32_t mode = UT_OPEN_MODE_NONE);
void Append(const char* s, int64_t len);
int64_t Write(const char* s, int64_t len);
int64_t Write(const std::string& s);
int64_t Read(char* s, int64_t len);
int64_t Read(std::string& s, int64_t len);
int64_t ReadAll(std::string& s);
void SeekBegin();
void SeekEnd();
void Seek(int64_t offset, int64_t whence);
void Sync();
void Truncate(int64_t len);
void Close();
int64_t Size() const;
private:
void CheckOpen();
void OpenInner();
private:
std::string mFileName;
int32_t mFd;
int32_t mFlag;
uint32_t mMode;
};
typedef std::shared_ptr<File> FilePtr;
class MMReadFile
{
public:
MMReadFile();
MMReadFile(const std::string& fileName);
virtual ~MMReadFile();
int32_t GetFd() const;
bool IsOpen() const;
void Open();
void Open(const std::string& fileName);
int64_t Size() const;
int64_t Read(char* s, int64_t len);
int64_t Read(std::string& s, int64_t len);
int64_t ReadAll(std::string& s);
void Seek(int64_t offset);
void Close();
public:
void* MMRead(int64_t len, int64_t& canreadlen);
void MMClose(void* ptr, int64_t len);
private:
void Init();
private:
std::string mFileName;
int32_t mFd;
int64_t mOffset;
int64_t mSize;
};
typedef std::shared_ptr<MMReadFile> MMReadFilePtr;
//Functions
bool ExistFile(const std::string& fileName);
std::string LoadFile(const std::string& fileName);
std::string LoadFileEx(const std::string& fileName);
void SaveFile(const std::string& fileName, const char* s, int64_t len,
uint32_t mode = UT_OPEN_MODE_RW);
void SaveFile(const std::string& fileName, const std::string& s,
uint32_t mode = UT_OPEN_MODE_RW);
void AppendFile(const std::string& fileName, const char* s, int64_t len,
uint32_t mode = UT_OPEN_MODE_RW);
void AppendFile(const std::string& fileName, const std::string& s,
uint32_t mode = UT_OPEN_MODE_RW);
int64_t GetFileSize(const std::string& fileName);
void RemoveFile(const std::string& fileName);
int64_t MMLoadFile(const std::string& fileName, std::string& s);
bool CopyFile(const std::string& fileName, const std::string& destFileName, bool deeply = false);
}
}
#endif//__UT_FILE_HPP__

View File

@@ -0,0 +1,284 @@
#ifndef __UT_FILE_SYSTEM_HPP__
#define __UT_FILE_SYSTEM_HPP__
#include <unitree/common/decl.hpp>
#define UT_FILE_READ_SIZE 65536 //64K
#define UT_FILE_READ_BIGGER_SIZE 262144 //256K
#define UT_FD_INVALID -1
#define UT_FD_STDIN STDIN_FILENO
#define UT_FD_STDOUT STDOUT_FILENO
#define UT_FD_STDERR STDERR_FILENO
#define UT_PATH_DELIM_CHAR '/'
#define UT_PATH_DELIM_STR "/"
#define UT_PATH_TRIM_DELIM_STR " \t\r\n"
#define NOT_FLAG(f, flag) \
(((f) & (flag)) != (flag))
#define HAS_FLAG(f, flag) \
(((f) & (flag)) == (flag))
#define SET_FLAG(f, flag) \
(f) |= (flag)
#define IS_RDONLY_FLAG(f) \
((f & O_ACCMODE) == O_RDONLY)
namespace unitree
{
namespace common
{
enum
{
UT_OPEN_FLAG_R = O_RDONLY,
UT_OPEN_FLAG_W = O_WRONLY,
UT_OPEN_FLAG_RW = O_RDWR,
UT_OPEN_FLAG_T = O_TRUNC,
UT_OPEN_FLAG_A = O_APPEND,
UT_OPEN_FLAG_S = O_SYNC,
UT_OPEN_FLAG_C = O_CREAT,
UT_OPEN_FLAG_CW = O_CREAT | O_WRONLY,
UT_OPEN_FLAG_CWT = O_CREAT | O_WRONLY | O_TRUNC,
UT_OPEN_FLAG_CA = O_CREAT | O_RDWR | O_APPEND,
UT_OPEN_FLAG_CS = O_CREAT | O_RDWR | O_SYNC,
UT_OPEN_FLAG_CAS = O_CREAT | O_RDWR | O_APPEND | O_SYNC,
UT_OPEN_FLAG_CTS = O_CREAT | O_RDWR | O_TRUNC | O_SYNC
};
enum
{
UT_OPEN_MODE_NONE = 0,
UT_OPEN_MODE_RW = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH,
UT_OPEN_MODE_RWX = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
};
enum
{
UT_OPEN_MASK_000 = 0,
UT_OPEN_MASK_022 = 0022
};
typedef struct dirent DIRENT;
/*
* FileSystemHelper
*/
class FileSystemHelper
{
public:
static FileSystemHelper* Instance()
{
static FileSystemHelper inst;
return &inst;
}
/*
* File Functions
*/
int32_t Open(const std::string& fileName, int32_t flag, uint32_t mode = 0);
int64_t Write(int32_t fd, const char* s, int64_t len);
int64_t Write(int32_t fd, const std::string& s);
int64_t Read(int32_t fd, char* s, int64_t len);
int64_t Read(int32_t fd, std::string& s, int64_t len);
int64_t ReadAll(int32_t fd, std::string& s);
int64_t Seek(int32_t fd, int64_t offset, int64_t whence);
bool Stat(int32_t fd, struct stat& statbuf);
void Truncate(int32_t fd, int64_t len);
void Sync(int32_t fd);
void Close(int32_t fd);
/*
* Directory Functions
*/
DIR* Opendir(const std::string& dirName);
DIRENT* Readdir(DIR* dir);
void Closedir(DIR* dir);
void Makedir(const std::string& dirName, uint32_t mode = UT_OPEN_MODE_RWX,
bool ignoreExist = true);
void MakedirRecurse(const std::string& dirName, uint32_t mode = UT_OPEN_MODE_RWX);
/*
* stat Function
*/
bool Stat(const std::string& name, struct stat& statbuf);
bool StatL(const std::string& name, struct stat& statbuf);
bool IsFile(const struct stat& statbuf);
bool IsDirectory(const struct stat& statbuf);
bool IsSymlink(const struct stat& statbuf);
bool ExistFile(const std::string& name);
bool ExistDirectory(const std::string& name);
bool Exist(const std::string& name);
int64_t GetFileSize(const std::string& fileName);
/*
* Symlink Function
*/
std::string GetSymlink(const std::string& symlinkName);
bool Symlink(const std::string& targetFileName, const std::string& symlinkName);
/*
* Remove and Rename Function
*/
void RemoveFile(const std::string& fileName, bool ignoreNoExist = true);
void RemoveDirectory(const std::string& dirName, bool ignoreNoExist = true);
void Remove(const std::string& name, bool ignoreNoExist = true);
void Rename(const std::string& oldName, const std::string& newName);
/*
* Path proccess Function
*/
std::string& NormalizePath(std::string& s, bool withEndDelim = true);
std::string GetFatherDirectory(const std::string& s, bool withEndDelim = true);
std::string GetFileName(const std::string& s);
std::string GetLastName(const std::string& s);
/*
* Realpath
*/
std::string GetRealName(const std::string& name);
bool IsSame(const std::string& name1, const std::string& name2);
/*
* MMap Function
*/
void* MmapRead(int32_t fd, int64_t offset, int64_t len);
void Munmap(void *addr, int64_t len);
/*
* Mode and Owner
*/
bool Chmod(const std::string& name, mode_t mode);
bool Chown(const std::string& name, uid_t uid, gid_t gid);
bool ChmodL(const std::string& name, mode_t mode);
bool ChownL(const std::string& name, uid_t uid, gid_t gid);
bool UTime(const std::string& name, struct utimbuf& times);
bool UTime(const std::string& name, struct timeval times[2]);
bool Chattr(const std::string& name, const struct stat& statbuf);
/*
* ParseModeString
* @param s: like "0755", "0666", "766"
* return mode
*/
uint32_t ParseModeString(const std::string& s);
private:
FileSystemHelper();
~FileSystemHelper();
private:
int32_t mOldMask;
};
/*
* FDCloser
*/
class FDCloser
{
public:
explicit FDCloser() :
mFd(UT_FD_INVALID)
{}
explicit FDCloser(int32_t fd) :
mFd(fd)
{}
~FDCloser()
{
Close();
}
void SetFd(int32_t fd)
{
//close old fd
Close();
//set new fd
mFd = fd;
}
private:
void Close()
{
if (mFd > 0)
{
FileSystemHelper::Instance()->Close(mFd);
mFd = UT_FD_INVALID;
}
}
private:
int32_t mFd;
};
/*
* DIRCloser
*/
class DIRCloser
{
public:
explicit DIRCloser() :
mDIR(NULL)
{}
explicit DIRCloser(DIR* dir) :
mDIR(dir)
{}
~DIRCloser()
{
Close();
}
void SetDir(DIR* dir)
{
//close old dir
Close();
//set new dir
mDIR = dir;
}
private:
void Close()
{
if (mDIR != NULL)
{
FileSystemHelper::Instance()->Closedir(mDIR);
mDIR = NULL;
}
}
private:
DIR* mDIR;
};
//Function
void Remove(const std::string& name);
void Rename(const std::string& oldName, const std::string& newName);
std::string NormalizePath(const std::string& s, bool withEndDelim = false);
std::string GetFatherDirectory(const std::string& s, bool withEndDelim = false);
std::string GetFileName(const std::string& s);
std::string GetLastName(const std::string& s);
void Chattr(const std::string& name, const struct stat& statbuf);
void Copyattr(const std::string& name, const std::string& templateName);
}
}
#endif//__UT_FILE_SYSTEM_HPP__