Older/ToolKit/Util/File.h

155 lines
4.6 KiB
C
Raw Normal View History

2024-09-28 23:55:00 +08:00
/*
* Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
*
* This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef SRC_UTIL_FILE_H_
#define SRC_UTIL_FILE_H_
#include <cstdio>
#include <cstdlib>
#include <string>
#include "util.h"
#include <functional>
#if defined(__linux__)
#include <limits.h>
#endif
#if defined(_WIN32)
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif // !PATH_MAX
struct dirent{
long d_ino; /* inode number*/
off_t d_off; /* offset to this dirent*/
unsigned short d_reclen; /* length of this d_name*/
unsigned char d_type; /* the type of d_name*/
char d_name[1]; /* file name (null-terminated)*/
};
typedef struct _dirdesc {
int dd_fd; /** file descriptor associated with directory */
long dd_loc; /** offset in current buffer */
long dd_size; /** amount of data returned by getdirentries */
char *dd_buf; /** data buffer */
int dd_len; /** size of data buffer */
long dd_seek; /** magic cookie returned by getdirentries */
HANDLE handle;
struct dirent *index;
} DIR;
# define __dirfd(dp) ((dp)->dd_fd)
int mkdir(const char *path, int mode);
DIR *opendir(const char *);
int closedir(DIR *);
struct dirent *readdir(DIR *);
#endif // defined(_WIN32)
#if defined(_WIN32) || defined(_WIN64)
#define fseek64 _fseeki64
#define ftell64 _ftelli64
#else
#define fseek64 fseek
#define ftell64 ftell
#endif
namespace toolkit {
class File {
public:
//创建路径
static bool create_path(const std::string &file, unsigned int mod);
//新建文件,目录文件夹自动生成
static FILE *create_file(const std::string &file, const std::string &mode);
//判断是否为目录
static bool is_dir(const std::string &path);
//判断是否是特殊目录(. or ..
static bool is_special_dir(const std::string &path);
//删除目录或文件
static int delete_file(const std::string &path, bool del_empty_dir = false, bool backtrace = true);
//判断文件是否存在
static bool fileExist(const std::string &path);
/**
* string
* @param path
* @return
*/
static std::string loadFile(const std::string &path);
/**
*
* @param data
* @param path
* @return
*/
static bool saveFile(const std::string &data, const std::string &path);
/**
*
* @param path
* @return
*/
static std::string parentDir(const std::string &path);
/**
* "../"
* @param path "../"
* @param current_path
* @param can_access_parent 访
* @return "../"
*/
static std::string absolutePath(const std::string &path, const std::string &current_path, bool can_access_parent = false);
/**
*
* @param path
* @param cb path为绝对路径isDir为该路径是否为文件夹true代表继续扫描
* @param enter_subdirectory
* @param show_hidden_file
*/
static void scanDir(const std::string &path, const std::function<bool(const std::string &path, bool isDir)> &cb,
bool enter_subdirectory = false, bool show_hidden_file = false);
/**
*
* @param fp
* @param remain_size true:false:
*/
static uint64_t fileSize(FILE *fp, bool remain_size = false);
/**
*
* @param path
* @return
* @warning
*/
static uint64_t fileSize(const std::string &path);
/**
*
* @param dir
* @param backtrace
*/
static void deleteEmptyDir(const std::string &dir, bool backtrace = true);
private:
File();
~File();
};
} /* namespace toolkit */
#endif /* SRC_UTIL_FILE_H_ */