Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
console::BasicPath< CharT > Class Template Reference

文件路径封装类,提供便捷的文件读写和路径操作。 More...

#include <file.h>

Public Types

using Bytes = std::vector<unsigned char>
 字节向量类型

Public Member Functions

 BasicPath (const CharT *str)
 构造函数,接受一个字符指针作为路径字符串。
 BasicPath (const string_type &str)
 构造函数,接受一个字符串作为路径字符串。
BasicPath & operator/= (const BasicPath &p)
 重载运算符,用于路径拼接。
string_type read_text () const
 读取文本文件内容。
Bytes read_binary () const
 读取二进制文件内容。
std::vector< string_type > read_lines () const
 读取文本文件的行内容。
template<class T>
T read_POD () const
 读取 POD 类型数据。
template<class T>
T unsafe_read_POD () const
 读取 POD 类型数据(不安全)。
void write_text (const string_type &text) const
 写入文本内容。
void write_binary (const Bytes &bts) const
 写入二进制内容。
void write_lines (const std::vector< string_type > &lines) const
 写入行内容。
template<class T>
void write_POD (const T &data) const
 写入 POD 类型数据。
template<class T>
void unsafe_write_POD (const T &data) const
 写入 POD 类型数据(不安全)。
bool exists () const
 判断文件是否存在。
void touch () const
 创建文件(若不存在)。
void ensure () const
 确保文件存在(若不存在则创建)。
void remove () const
 删除文件(若存在)。
const string_type & str () const
 获取文件路径字符串。
std::fstream stream (std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out) const
 以指定模式打开文件流。

Private Types

using string_type = std::basic_string<CharT>
 字符串类型

Private Member Functions

std::string narrow () const
 将宽字符路径转换为窄字符路径。

Static Private Member Functions

template<class T = CharT>
static std::enable_if< std::is_same< T, char >::value, string_type >::type widen_or_pass (const std::string &s)
 根据字符类型选择宽字符或窄字符转换函数。
template<class T = CharT>
static std::enable_if< std::is_same< T, wchar_t >::value, string_type >::type widen_or_pass (const std::string &s)
 根据字符类型选择宽字符或窄字符转换函数。
template<class T = CharT>
static std::enable_if< std::is_same< T, char >::value, void >::type write_to_stream (std::ofstream &fout, const string_type &text)
 根据字符类型选择宽字符或窄字符转换函数。
template<class T = CharT>
static std::enable_if< std::is_same< T, wchar_t >::value, void >::type write_to_stream (std::ofstream &fout, const string_type &text)
 根据字符类型选择宽字符或窄字符转换函数。

Private Attributes

string_type path
 路径字符串

Friends

BasicPath operator/ (const BasicPath &p1, const BasicPath &p2)
 重载运算符,用于路径拼接。

Detailed Description

template<class CharT = char>
class console::BasicPath< CharT >

文件路径封装类,提供便捷的文件读写和路径操作。

Template Parameters
CharT字符类型(char 或 wchar_t)。

Member Typedef Documentation

◆ Bytes

template<class CharT = char>
using console::BasicPath< CharT >::Bytes = std::vector<unsigned char>

字节向量类型

◆ string_type

template<class CharT = char>
using console::BasicPath< CharT >::string_type = std::basic_string<CharT>
private

字符串类型

Constructor & Destructor Documentation

◆ BasicPath() [1/2]

template<class CharT = char>
console::BasicPath< CharT >::BasicPath ( const CharT * str)
inline

构造函数,接受一个字符指针作为路径字符串。

Parameters
str路径字符串。

◆ BasicPath() [2/2]

template<class CharT = char>
console::BasicPath< CharT >::BasicPath ( const string_type & str)
inline

构造函数,接受一个字符串作为路径字符串。

Parameters
str路径字符串。

Member Function Documentation

◆ ensure()

template<class CharT = char>
void console::BasicPath< CharT >::ensure ( ) const
inline

确保文件存在(若不存在则创建)。

Note
若文件已存在,不会覆盖原有内容。

◆ exists()

template<class CharT = char>
bool console::BasicPath< CharT >::exists ( ) const
inline

判断文件是否存在。

Returns
存在返回 true,否则返回 false。
Note
若是目录,返回 fasle。

◆ narrow()

template<class CharT = char>
std::string console::BasicPath< CharT >::narrow ( ) const
inlineprivate

将宽字符路径转换为窄字符路径。

Returns
窄字符路径字符串。

◆ operator/=()

template<class CharT = char>
BasicPath & console::BasicPath< CharT >::operator/= ( const BasicPath< CharT > & p)
inline

重载运算符,用于路径拼接。

Parameters
p路径字符串。
Returns
拼接后的路径字符串。

◆ read_binary()

template<class CharT = char>
Bytes console::BasicPath< CharT >::read_binary ( ) const
inline

读取二进制文件内容。

Returns
二进制内容。

◆ read_lines()

template<class CharT = char>
std::vector< string_type > console::BasicPath< CharT >::read_lines ( ) const
inline

读取文本文件的行内容。

Returns
行内容列表。

◆ read_POD()

template<class CharT = char>
template<class T>
T console::BasicPath< CharT >::read_POD ( ) const
inline

读取 POD 类型数据。

Returns
数据。

◆ read_text()

template<class CharT = char>
string_type console::BasicPath< CharT >::read_text ( ) const
inline

读取文本文件内容。

Returns
文本内容。

◆ remove()

template<class CharT = char>
void console::BasicPath< CharT >::remove ( ) const
inline

删除文件(若存在)。

◆ str()

template<class CharT = char>
const string_type & console::BasicPath< CharT >::str ( ) const
inline

获取文件路径字符串。

◆ stream()

template<class CharT = char>
std::fstream console::BasicPath< CharT >::stream ( std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) const
inline

以指定模式打开文件流。

Parameters
mode打开模式,默认为 std::ios_base::in | std::ios_base::out。
Returns
打开的文件流。

◆ touch()

template<class CharT = char>
void console::BasicPath< CharT >::touch ( ) const
inline

创建文件(若不存在)。

Note
若文件已存在,不会覆盖原有内容。

◆ unsafe_read_POD()

template<class CharT = char>
template<class T>
T console::BasicPath< CharT >::unsafe_read_POD ( ) const
inline

读取 POD 类型数据(不安全)。

Returns
数据。

◆ unsafe_write_POD()

template<class CharT = char>
template<class T>
void console::BasicPath< CharT >::unsafe_write_POD ( const T & data) const
inline

写入 POD 类型数据(不安全)。

Parameters
data数据。

◆ widen_or_pass() [1/2]

template<class CharT = char>
template<class T = CharT>
std::enable_if< std::is_same< T, wchar_t >::value, string_type >::type console::BasicPath< CharT >::widen_or_pass ( const std::string & s)
inlinestaticprivate

根据字符类型选择宽字符或窄字符转换函数。

Parameters
s字符串。
Returns
转换后的字符串。

◆ widen_or_pass() [2/2]

template<class CharT = char>
template<class T = CharT>
std::enable_if< std::is_same< T, char >::value, string_type >::type console::BasicPath< CharT >::widen_or_pass ( const std::string & s)
inlinestaticprivate

根据字符类型选择宽字符或窄字符转换函数。

Parameters
s字符串。
Returns
转换后的字符串。

◆ write_binary()

template<class CharT = char>
void console::BasicPath< CharT >::write_binary ( const Bytes & bts) const
inline

写入二进制内容。

Parameters
bts二进制内容。

◆ write_lines()

template<class CharT = char>
void console::BasicPath< CharT >::write_lines ( const std::vector< string_type > & lines) const
inline

写入行内容。

Parameters
lines行内容列表。

◆ write_POD()

template<class CharT = char>
template<class T>
void console::BasicPath< CharT >::write_POD ( const T & data) const
inline

写入 POD 类型数据。

Parameters
data数据。

◆ write_text()

template<class CharT = char>
void console::BasicPath< CharT >::write_text ( const string_type & text) const
inline

写入文本内容。

Parameters
text文本内容。

◆ write_to_stream() [1/2]

template<class CharT = char>
template<class T = CharT>
std::enable_if< std::is_same< T, wchar_t >::value, void >::type console::BasicPath< CharT >::write_to_stream ( std::ofstream & fout,
const string_type & text )
inlinestaticprivate

根据字符类型选择宽字符或窄字符转换函数。

Parameters
fout输出流。
text字符串。

◆ write_to_stream() [2/2]

template<class CharT = char>
template<class T = CharT>
std::enable_if< std::is_same< T, char >::value, void >::type console::BasicPath< CharT >::write_to_stream ( std::ofstream & fout,
const string_type & text )
inlinestaticprivate

根据字符类型选择宽字符或窄字符转换函数。

Parameters
fout输出流。
text字符串。

◆ operator/

template<class CharT = char>
BasicPath operator/ ( const BasicPath< CharT > & p1,
const BasicPath< CharT > & p2 )
friend

重载运算符,用于路径拼接。

Parameters
p1路径字符串。
p2路径字符串。
Returns
拼接后的路径字符串。

Member Data Documentation

◆ path

template<class CharT = char>
string_type console::BasicPath< CharT >::path
private

路径字符串


The documentation for this class was generated from the following file: