Poco学习随笔——Path类

POCO库的一些学习、整理和总结:

POCO库的文件操作主要是2个类:Path和File。

PS:不同操作系统中数据代表的数据不同,POCO目前支持Windows、UNIX相关系统和OpenVMS。我只关心UNIX和Windows,还有很多函数一看就知道意思,在这里就不多做解释了。

#include <Poco/Path>

#include <Poco/File>

一、 Path由以下几点组成:

1. node name

 

UNIX下,此字段没有意义;

2. device name

windows 驱动盘符,比如C盘等;

UNIX下,无意义;

3. directory list

4. file name

5. style

操作系统类型

6. Kind

absolute或者relative, file或者directory;

二、 路径的表示

1. absolute path(绝对路径)

2. relative path(相对路径)

注:2种路径方式可以互相转换。

三、 例子

---------------------------

Path: C:\Windows\system32\cmd.exe
Style: Windows
Kind: absolute, to file
Node Name: –
Device Name: C
Directory List: Windows, system32
File Name: cmd.exe
File Version: –

--------------------------

Path: \\www\site\index.html
Style: Windows
Kind: absolute, to file
Node Name: www
Device Name: –
Directory List: site
File Name: index.html
File Version: –

--------------------------

Path: /usr/local/include/Poco/Foundation.h
Style: Unix
Kind: absolute, to file
Node Name: –
Device Name: –
Directory List: usr, local, include, Poco
File Name: index.html
File Version: –

四、 Path的构造

1. 一点点的给Path的实例添加设置值;

带一个Bool的参数:true=absolute, false = relative;

之后:

void setNode(const std::string& node)
void setDevice(const std::string& device)
void pushDirectory(const std::string& name)
void setFileName(const std::string& name)

2. 实例化的时候制定路径和系统类型:

> PATH_UNIX
> PATH_WINDOWS
> PATH_VMS
> PATH_NATIVE(当前系统)
> PATH_GUESS(POCO自动匹配)

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);

六、 get方法

std::string toString();
std::string toString(Style style);

const std::string& getNode();
const std::string& getDevice();
const std::string& directory(int n) (also operator []);// 返回第n层次的目录 从0开始计算


const std::string& getFileName();

int depth() const; // 目录深度

std::string getBaseName() const // 基本文件名,不包括扩展名,不如cmd.exe, 就是“cmd”
void setBaseName(const std::string& baseName)
std::string getExtension() const // 扩展文件名,不包括基本文件名,不如cmd.exe, 就是“exe”
void setExtension(const std::string& extension)

例子:

 1 #include "Poco/Path.h"
 2 using Poco::Path;
 3 int main(int argc, char** argv)
 4 {
 5     Path p("c:\\projects\\poco\\build_vs80.cmd",          Path::PATH_WINDOWS);
 6     std::string device(p.getDevice()); // "c"
 7     int n = p.depth(); // 2
 8     std::string dir1(p.directory(0)); // "projects"
 9     std::string dir2(p[1]); // "poco"
10     std::string fileName(p[2]); // "build_vs80.cmd"
11     fileName = p.getFileName();
12     std::string baseName(p.getBaseName()); // "build_vs80"
13     std::string extension(p.getExtension()); // "cmd"
14     p.setBaseName("build_vs71");
15     fileName = p.getFileName(); // "build_vs71.cmd"
16     return 0;
17 }

七、 操作

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); // 如果path是绝对路径则替换,否则是append

bool isAbsolute() const; // 绝对路径为true

bool isRelative() const; // 相对路径为true

bool isDirectory() const; // 目录

bool isFile() const; // 文件

八、 静态方法

std::string current(); // 返回当前路径

std::string home(); // 返回程序的home路径

std::string temp(); // 返回临时文件路径

std::string null(); // /dev/null

std::string expand(const std::string& path); // 解析环境变量,跟系统有关windows %VAR%,UNIX $VAR

void listRoots(std::vector<std::string>& roots); // 包括跟级别的目录,windows磁盘驱动器名称,UNIX包括“/”

bool find(const std::string& pathList, const std::string& name, Path& path) // 查找pathList路径(windows用“;”,UNIX用“:”分割)中是否有name文件存在,如果有返回true,path被填充,如果没有返回false,path没有值

 

 

转载于:https://www.cnblogs.com/xi52qian/archive/2013/03/13/2955452.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值