common_util
Loading...
Searching...
No Matches
Classes | Functions
fileutil.h File Reference

The functions and classes about filesystem, such as file and directory operations. More...

#include <string>
#include <cstdio>
#include "filetype.h"
#include "filepath.h"

Go to the source code of this file.

Classes

class  cutl::file_guard
 The file guard class to manage the FILE pointer automatically. file_guard object can close the FILE pointer automatically when his scope is exit. More...
 

Functions

std::string cutl::filetype_flag (filetype type)
 Get the file type flag string.
 
bool cutl::createfile (const filepath &path)
 Create a new file.
 
bool cutl::createlink (const filepath &referenece, const filepath &filepath)
 Create a symbolic link or shortcut.
 
bool cutl::createdir (const filepath &path, bool recursive=false)
 Create a new directory.
 
bool cutl::removefile (const filepath &path)
 Remove a regular file or symbolic link(shortcut on windows).
 
bool cutl::removedir (const filepath &path, bool recursive=false)
 Remove a directory.
 
std::string cutl::readtext (const filepath &path, uint64_t max_read_size=4096)
 Read the text content of a file.
 
bool cutl::writetext (const filepath &path, const std::string &content)
 Write the text content to a file.
 
uint64_t cutl::filesize (const filepath &filepath, bool link_target=false)
 Get the size of a file.
 
uint64_t cutl::dirsize (const filepath &dirpath)
 Get the size of a directory, include all files and subdirectories.
 
filevec cutl::list_files (const filepath &dirpath, filetype type=filetype::all, bool recursive=false)
 List all files in a directory.
 
filevec cutl::find_files (const filepath &dirpath, const std::string &name, bool recursive=false)
 Find all files in a directory by name.
 
filevec cutl::find_files_by_extension (const filepath &dirpath, const std::string &extension, bool recursive=false)
 Find all files in a directory by extension.
 
bool cutl::copyfile (const filepath &srcpath, const filepath &dstpath, bool attributes=false)
 Copy a file or symbolic link(shortcut on windows).
 
bool cutl::copydir (const filepath &srcdir, const filepath &dstdir)
 Copy a directory recursively.
 

Detailed Description

The functions and classes about filesystem, such as file and directory operations.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations.

Author
spencer.luo
Date
2024-05-13

Function Documentation

◆ copydir()

bool cutl::copydir ( const filepath & srcdir,
const filepath & dstdir )

Copy a directory recursively.

Parameters
srcdirthe filepath of the source directory to be copied
dstdirthe filepath of the destination directory to be copied to
Returns
true if the directory is copied successfully, false otherwise.
Note
If the destination directory already exists, it will be overwritten.

◆ copyfile()

bool cutl::copyfile ( const filepath & srcpath,
const filepath & dstpath,
bool attributes = false )

Copy a file or symbolic link(shortcut on windows).

Parameters
srcpaththe filepath of the source file or symbolic link to be copied
dstpaththe filepath of the destination file or symbolic link to be copied to
attributeswhether to copy the file attributes, default is false. If true, means copy the file attributes, like the 'cp -p' command.
Returns
true if the file or symbolic link is copied successfully, false otherwise.
Note
If the destination file or directory already exists, it will be overwritten.
Returns
false

◆ createdir()

bool cutl::createdir ( const filepath & path,
bool recursive = false )

Create a new directory.

Parameters
paththe filepath of the new directory to be created
recursivewhether to create parent directories, default is false. If true, means create parent directories if not exist, like the 'mkdir -p' command.
Returns
true if the directory is created successfully, false otherwise.

◆ createfile()

bool cutl::createfile ( const filepath & path)

Create a new file.

Parameters
paththe filepath of the new file to be created
Returns
true if the file is created successfully, false otherwise.

◆ createlink()

bool cutl::createlink ( const filepath & referenece,
const filepath & filepath )

Create a symbolic link or shortcut.

Parameters
referenecethe real path referenced by the symbolic link or shortcut
filepaththe filepath of the symbolic link or shortcut to be created
Returns
true if the symbolic link or shortcut is created successfully, false otherwise.

◆ dirsize()

uint64_t cutl::dirsize ( const filepath & dirpath)

Get the size of a directory, include all files and subdirectories.

Parameters
dirpaththe filepath of the directory to be checked
Returns
the total size of the directory in bytes

◆ filesize()

uint64_t cutl::filesize ( const filepath & filepath,
bool link_target = false )

Get the size of a file.

Parameters
filepaththe filepath of the file to be checked
link_targetwhether to get the size of the file pointed by symbolic link, default is false. If link_target is true, the function will get the size of the file pointed by symbolic link, not the symbolic link itself.
Note
link_target parameter only works on Unix-like systems, not support on Windows.
Returns
file size in bytes

◆ filetype_flag()

std::string cutl::filetype_flag ( filetype type)

Get the file type flag string.

Parameters
typefile type
Returns
the flag string of the file type

◆ find_files()

filevec cutl::find_files ( const filepath & dirpath,
const std::string & name,
bool recursive = false )

Find all files in a directory by name.

Parameters
dirpaththe filepath of the directory to be searched
namethe name of the file to be found, substring match is supported.
recursivewhether to search the whole directory recursively, default is false. If true, means search the whole directory recursively, like the 'find' command.
Returns
filevec the vector of file_entity, filepaths in the directory.

◆ find_files_by_extension()

filevec cutl::find_files_by_extension ( const filepath & dirpath,
const std::string & extension,
bool recursive = false )

Find all files in a directory by extension.

Parameters
dirpaththe filepath of the directory to be searched
extensionthe extension of the file to be found, with dot, like ".txt".
recursivewhether to search the whole directory recursively, default is false. If true, means search the whole directory recursively, like the 'find' command.
Returns
filevec the vector of file_entity, filepaths in the directory.

◆ list_files()

filevec cutl::list_files ( const filepath & dirpath,
filetype type = filetype::all,
bool recursive = false )

List all files in a directory.

Parameters
dirpaththe filepath of the directory to be listed
typefile type to be listed, default is all types.
recursivewhether to list the whole directory recursively, default is false. If true, means list the whole directory recursively, like the 'ls -R' command.
Returns
filevec the vector of file_entity, filepaths in the directory.

◆ readtext()

std::string cutl::readtext ( const filepath & path,
uint64_t max_read_size = 4096 )

Read the text content of a file.

Parameters
paththe filepath of the file to be read
max_read_sizethe maximum size to be read, default is 4096 bytes.

If the file size is larger than max_read_size, only the first max_read_size bytes will be read. otherwise, the whole file will be read.

Returns
text content of the file

◆ removedir()

bool cutl::removedir ( const filepath & path,
bool recursive = false )

Remove a directory.

Parameters
paththe filepath of the directory to be removed
recursivewhether to remove the whole directory recursively, default is false. If true, means remove the whole directory recursively, like the 'rm -rf' command.
Returns
true if the directory is removed successfully, false otherwise.

◆ removefile()

bool cutl::removefile ( const filepath & path)

Remove a regular file or symbolic link(shortcut on windows).

Parameters
paththe filepath of the file or symbolic link to be removed
Returns
true if the file or symbolic link is removed successfully, false otherwise.

◆ writetext()

bool cutl::writetext ( const filepath & path,
const std::string & content )

Write the text content to a file.

Parameters
paththe filepath of the file to be written
contenttext content to be written to the file
Returns
true if the content is written successfully, false otherwise.