common_util
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 "filetype.h"
24#include "filepath.h"
25
26namespace cutl
27{
28
34 {
35 public:
41 explicit file_guard(FILE *file);
42
48
54 FILE *getfd() const;
55
56 private:
57 FILE *file_;
58 };
59
66 std::string filetype_flag(filetype type);
67
74 bool createfile(const filepath &path);
75
83 bool createlink(const filepath &referenece, const filepath &filepath);
84
85 // recursive: create parent directories if not exist, like the 'mkdir -p' command
94 bool createdir(const filepath &path, bool recursive = false);
95
102 bool removefile(const filepath &path);
103
112 bool removedir(const filepath &path, bool recursive = false);
113
124 std::string readtext(const filepath &path, uint64_t max_read_size = 4096);
125
133 bool writetext(const filepath &path, const std::string &content);
134
147 uint64_t filesize(const filepath &filepath, bool link_target = false);
148
155 uint64_t dirsize(const filepath &dirpath);
156
166 filevec list_files(const filepath &dirpath, filetype type = filetype::all, bool recursive = false);
167
177 filevec find_files(const filepath &dirpath, const std::string &name, bool recursive = false);
178
188 filevec find_files_by_extension(const filepath &dirpath, const std::string &extension, bool recursive = false);
189
201 bool copyfile(const filepath &srcpath, const filepath &dstpath, bool attributes = false);
202
203 // copy directory recursively
212 bool copydir(const filepath &srcdir, const filepath &dstdir);
213
214} // namespace cutl
The file guard class to manage the FILE pointer automatically. file_guard object can close the FILE p...
Definition fileutil.h:34
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.
filevec list_files(const filepath &dirpath, filetype type=filetype::all, bool recursive=false)
List all files in a directory.
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).
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.