common_util 1.1.0
Loading...
Searching...
No Matches
fileutil.h
Go to the documentation of this file.
1
19#pragma once
20
21#include <string>
22#include <cstdio>
23#include <cstdint>
24#include "filetype.h"
25#include "filepath.h"
26
27namespace cutl
28{
29
35 {
36 public:
42 explicit file_guard(FILE *file);
43
49
55 FILE *getfd() const;
56
57 private:
58 FILE *file_;
59 };
60
67 std::string filetype_flag(filetype type);
68
75 bool createfile(const filepath &path);
76
84 bool createlink(const filepath &referenece, const filepath &filepath);
85
86 // recursive: create parent directories if not exist, like the 'mkdir -p' command
95 bool createdir(const filepath &path, bool recursive = false);
96
103 bool removefile(const filepath &path);
104
113 bool removedir(const filepath &path, bool recursive = false);
114
125 std::string readtext(const filepath &path, uint64_t max_read_size = 4096);
126
134 bool writetext(const filepath &path, const std::string &content);
135
148 uint64_t filesize(const filepath &filepath, bool link_target = false);
149
156 uint64_t dirsize(const filepath &dirpath);
157
168 filetype type = filetype::ft_all,
169 bool recursive = false);
170
180 filevec find_files(const filepath &dirpath, const std::string &name, bool recursive = false);
181
191 filevec find_files_by_extension(const filepath &dirpath, const std::string &extension, bool recursive = false);
192
204 bool copyfile(const filepath &srcpath, const filepath &dstpath, bool attributes = false);
205
206 // copy directory recursively
215 bool copydir(const filepath &srcdir, const filepath &dstdir);
216
217} // namespace cutl
The file guard class to manage the FILE pointer automatically. file_guard object can close the FILE p...
Definition fileutil.h:35
file_guard(FILE *file)
Construct a new file guard object.
FILE * getfd() const
Get the FILE pointer.
~file_guard()
Destroy the file guard object.
The class for file path operations.
Definition filepath.h:34
The class for file path operations.
Define the structs about fileutil, such as filetype and file_entity.
std::vector< file_entity > filevec
The vector of file_entity.
Definition filetype.h:75
filetype
The type of file.
Definition filetype.h:38
bool copyfile(const filepath &srcpath, const filepath &dstpath, bool attributes=false)
Copy a file or symbolic link(shortcut on windows).
bool createlink(const filepath &referenece, const filepath &filepath)
Create a symbolic link or shortcut.
filevec find_files(const filepath &dirpath, const std::string &name, bool recursive=false)
Find all files in a directory by name.
bool createfile(const filepath &path)
Create a new file.
std::string readtext(const filepath &path, uint64_t max_read_size=4096)
Read the text content of a file.
bool removedir(const filepath &path, bool recursive=false)
Remove a directory.
filevec find_files_by_extension(const filepath &dirpath, const std::string &extension, bool recursive=false)
Find all files in a directory by extension.
std::string filetype_flag(filetype type)
Get the file type flag string.
bool removefile(const filepath &path)
Remove a regular file or symbolic link(shortcut on windows).
filevec list_files(const filepath &dirpath, filetype type=filetype::ft_all, bool recursive=false)
List all files in a directory.
bool createdir(const filepath &path, bool recursive=false)
Create a new directory.
uint64_t dirsize(const filepath &dirpath)
Get the size of a directory, include all files and subdirectories.
uint64_t filesize(const filepath &filepath, bool link_target=false)
Get the size of a file.
bool copydir(const filepath &srcdir, const filepath &dstdir)
Copy a directory recursively.
bool writetext(const filepath &path, const std::string &content)
Write the text content to a file.