POCO文件系统

POCO库对于文件系统的操作主要有5个类来完成,分别为:Poco::Path、Poco::File、Poco::TemporaryFile、Poco::DirectoryIterator、Poco::Glob。
  1. Poco::Path
    Poco抽象了不同操作系统标志符之间的区别,使之支持Windows、Unix、OpenVMS。
    Poco中的路径有4块组成:可选的节点(node)名、可选的设备(device)名、目录名列表、文件名。对于不同的操作系统,路径的组成代表的含义不一。而对于这些组成的获取和设置可以通过下面的方法完成:
void setNode(const std::string& node)
void setDevice(const std::string& device)
void pushDirectory(const std::string& name)
void setFileName(const std::string& name)

const std::string& getNode()
const std::string& getDevice()
const std::string& directory(int n) (also operator [])
const std::string& getFileName()

在Poco中支持两种路径:绝对路径、相对路径。相对路径能够转换为绝对路径(反之不成立)。

在Poco中路径的创建也分为两种:从0开始构建,分别构建node、device、directory、file;通过一个包含着路径的字符串去解析。
在构建路径时可以指定路径的格式:PATH_UNIX、PATH_WINDOWS、PATH_VMS、PATH_NATIVE、PATH_GUESS。
通过解析字符串的方式构建路径的方法:

Path(const std::string& path)
Path(const std::string& path, Style style)
Path(const Path& parent, const std::string& fileName)
Path(const Path& parent, const Path& relative)
Path& assign(const std::string& path)
Path& parse(const std::string& path)
Path& assign(const std::string& path, Style style)
Path& parse(const std::string& path, Style style)

而对于路径合法性的检查可以使用以下方法实现:

bool tryParse(const std::string& path)
bool tryParse(const std::string& path, Style style)

Poco::Path类中还提供了一些其他方法:

std::string toString()  //转成字符串
std::string toString(Style style)
int depth() const      //获取目录的深度
std::string getBaseName() const  //获取文件基本名
void setBaseName(const std::string& baseName)   //设置文件基本名
std::string getExtension() const   //获取文件扩展名
void setExtension(const std::string& extension)  //设置文件扩展名

Path& makeDirectory()  //确保路径的结尾是一个目录名。如果原路径有文件名存在的话,添加一个与文件名同名的目录,并清除文件名
Path& makeFile()  //确保路径的结尾是一个文件名。如果原路径是一个目录名,则把最后一个目录名变成文件名,并去除最后一个目录名
Path& makeParent()、Path parent() const //使路径指向它的父目录
Path& makeAbsolute()、Path& makeAbsolute(const Path& base)、Path absolute() const、Path absolute(const Path& base)  //相对路径转换为绝对路径
Path& append(const Path& path)  //添加路径
Path& resolve(const Path& path)  //如果新的路径为绝对路径,则代替现有的路径;否则则在原路径下追加
bool isAbsolute() const  //如果路径为绝对路径,返回true;否则为false
bool isRelative() const  //如果路径为相对路径,返回true;否则为false
bool isDirectory() const  //如果路径为目录,返回true;否则为false
bool isFile() const  //如果路径为文件,返回true;否则为false
std::string expand(const std::string& path)  //返回一个对环境变量进行扩充后的路径名
void listRoots(std::vector<std::string>& roots)  //会用所有挂载到文件系统的根目录来填充字符串数组
 bool find(const std::string& pathList, const std::string& name, Path& path)  //在指定的目录集(pathList)中搜索指定名称的文件(name)

Poco::Path类还针对特殊的目录和文件提供了静态函数

std::string current()  //返回当前的工作目录
std::string home()  //返回用户的主目录
std::string temp()  //返回操作系统的零时目录
std::string null()  //返回系统的空目录(e.g., "/dev/null" or "NUL:")

2.Poco::File
Poco仅支持与文件元数据协同工作。想要操作文件中的真实数据,需要使用标准库的文件流。
使用Poco库,你可以查出一个文件或者目录是否存在,是否可读或可写,文件何时创建和被修改,文件大小等信息。
通过Poco库,也可以修改文件属性,重命名文件,拷贝文件和删除文件。
通过Poco库,可以创建空文件(原子操作)和目录。
Poco::File类中包含所有对文件的操作,提供的函数有:

bool exists() const  //如果文件存在返回true,否则为false
bool canRead() const  //如果文件可读(用户对文件拥有足够权限)返回true,否则为false
bool canWrite() const  //如果文件可写(用户对文件拥有足够权限)返回true,否则为false
bool canExecute() const  //如果文件是可执行文件,返回true,否则为false
bool isFile() const  //如果文件是常规文件(即不是目录或符号链接)返回为真,否则为false
bool isLink() const  //如果文件是符号链接,返回为真,否则为false
bool isDirectory() const  //如果文件是目录,返回为真,否则为false
bool isDevice() const  //如果文件是设备,返回为真,否则为false
bool isHidden() const  //如果文件属性为隐藏,返回为真,否则为false。(在Windows上文件属性为隐藏;Unit上使用"."开头的文件)
Poco::Timestamp created() const  //返回文件创建的日期和时间
Poco::Timestamp getLastModified() const  //返回最后访问文件的时间和日期
File::FileSize getSize() const  //返回文件大小(单位为字节)。在大多数系统中,File::FileSize被定义成一个unsigned 64-bit整数

void setLastModified(Poco::Timestamp dateTime)  //设置最后访问文件的时间
void setSize(FileSize newSize)  //设置文件大小(单位为字节)。可用于截断一个文件。
void setWritable(bool flag = true)  //如果flag == true,使文件可写;flag == false相当于文件只读
void setReadOnly(bool flag)  //同setWritable(!flag)作用相同

void copyTo(const std::string& path) const  //拷贝文件至指定目录(通常是个文件夹)
void moveTo(const std::string& path) const  //拷贝文件至指定目录(通常是个文件夹)并且删除源文件
void renameTo(const std::string& path)  //重命名文件
void remove(bool recursive = false)  //删除文件。如果文件是个目录,并且recursive == true,则递归的删除所有文件和子目录

bool createFile()  //使用原子操作创建一个新的空文件。如果文件被创建返回true,如果文件已存在返回false。如果创建失败,抛出Poco::FileException异常。
bool createDirectory()  //创建一个新目录。如果目录被创建,返回true,如果目录已存在,返回false。如果创建失败,抛出Poco::FileException(比如说父目录不存在)。
void createDirectories()  //创建目录集。当父目录不存在时,也会同时创建父目录。

void list(std::vector<std::string>& files) const
void list(std::vector<File>& files) const  //会把目录中所有的文件名填入给定的files数组。在其内部,会使用Poco::DirectoryIterator遍历目录

3.Poco::TemporaryFile
Poco::TemporaryFile继承自Poco::File。
构造函数会为临时文件自动生成一个唯一的文件名,以及放置临时文件的操作系统默认目录。当然文件本身并未被创建。
如果临时文件被创建,析构函数会删除该文件
二选一的,删除动作可以被延迟到程序结束或者不再被使用。
任何文件都可以被延迟到程序终止时才被删除。
Poco::TemporaryFile类中的成员函数有:

void keep()  //停止析构函数中删除文件的动作
void keepUntilExit()  //停止析构函数中删除文件的动作,并注册该文件,延迟到程序结束时删除
static void registerForDeletion(const std::string& path)  //注册文件,使之在程序结束时删除
static std::string tempName()  //为临时文件创建一个唯一的名字

4.Poco::DirectoryIterator
Poco::DirectoryIterator类为读取目录中的内容提供了一个迭代子风格的接口。
Poco::DirectoryIterator使用时有如下限制:
a.仅支持前向迭代操作(forward iteration (++))
b.当一个迭代子拷贝自另一个迭代子时,总是指向源迭代子的曾经指向的文件。即使源迭代子已经指向另外一个文件。
c.Poco::DirectoryIterator内部维护一个Poco::File和Poco::Path绝对路径的对象。

5.Poco::Glob
Poco::Glob支持同Unix shells类似的通配符匹配。’*’匹配任何字符串序列,’?’匹配任意单一文件,[SET]匹配任意存在于指定字符集中的单一字符,[!SET]匹配任何不存在于指定字符集中的单一字符。[SET]可以指定字符集的范围和锁包含的字符。例如[123]用来匹配数字1,2,3;[a-zA-Z]匹配任何大写或小写英文字符。Glob类支持用一个反斜杠转义的特殊字符。
Poco::Glob可以使用一个pattern,或者一个选项的标志位去创建。GLOB_DOT_SPECIAL选项适用于Unix隐藏文件。
Poco::Glob类提供的函数有:

bool match(const std::string& subject)  //如果subject对应的路径符合匹配规则,返回true,否则为false

void glob(const std::string& pattern, std::set<std::string>& files, int options = 0)
void glob(const Path& pattern, std::set<std::string>& files, int options = 0)  //用符合给定pattern的所有文件填充给定的set集
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值