Files
c-utils/cfileop.h
2021-12-16 22:13:30 +08:00

111 lines
3.0 KiB
C

#ifndef _UTIL_CFILEOP_H
#define _UTIL_CFILEOP_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
/**
* @brief Check file exists
* @param fn File name
* @returns 1 if file is exists otherwise 0
*/
int fileop_exists(const char* fn);
/**
* @brief Remove file
* @param fn File name
* @returns 1 if successed otherwise 0
*/
int fileop_remove(const char* fn);
/**
* @brief Get directory name from path
* @param fn Path
* @returns Directory. If path does not contain a slash, will return "". Need free memory by using free.
*/
char* fileop_dirname(const char* fn);
/**
* @brief Detect if path is a url.
* @param fn Path
* @param re result
* @return 1 if successed otherwise 0
*/
int fileop_is_url(const char* fn, int* re);
/**
* @brief Get file name from path
* @param fn Path
* @return File name. Need free memory by calling free.
*/
char* fileop_basename(const char* fn);
/**
* @brief Parse size string
* @param size Such as "10KiB", "10", "34B", "48K"
* @param fs size
* @param is_byte Whether to return bits or bytes
* @return 1 if successed otherwise 0
*/
int fileop_parse_size(const char* size, size_t* fs, int is_byte);
/**
* @brief Opens a file.
* @param fn File name.
* @param fd file descriptor. -1 if error occured.
* @param oflag The kind of operations allowed.
* @param shflag The kind of sharing allowed. (Windows Only) 0 will be replace with 0x10.
* @param pmode Permission mode. (Windows Only)
* @return errno
*/
int fileop_open(const char* fn, int* fd, int oflag, int shflag, int pmode);
/**
* @brief Check if path is absoulte path.
* @param path The path.
* @return 1 if path is absoulte path otherwise 0
*/
int fileop_isabs(const char* path);
/**
* @brief Join two more pathname components
* @param path
* @param path2
* @return Result. Need free memory by using free.
*/
char* fileop_join(const char* path, const char* path2);
/**
* @brief check if path is an existing directory.
* @param path Path
* @param result 1 if path is an existing directory otherwise 0
* @return 0 if error occured otherwise 1
*/
int fileop_isdir(const char* path, int* result);
/**
* @brief Creates a new directory.
* @param path Path for a new directory.
* @param mode Directory permission. (Linux only)
* @return 1 if created successfully otherwise 0
*/
int fileop_mkdir(const char* path, int mode);
/**
* @brief Sets the date and time that the specified file or directory was created, last accessed, or last modified.
* @param path The path of directory or file
* @param ctime creation date (Windows Only)
* @param actime Last access date
* @param modtime Last modification date
* @return 1 if successed otherwise 0.
*/
int fileop_set_file_time(const char* path, time_t ctime, time_t actime, time_t modtime);
#if _WIN32
#define fileop_fdopen _fdopen
#else
#define fileop_fdopen fdopen
#endif
#if _WIN32
#define fileop_close(fd) (!_close(fd))
#else
#define fileop_close(fd) (!close(fd))
#endif
#define fileop_fclose(stream) (!fclose(stream))
#ifdef __cplusplus
}
#endif
#endif