POCO C++库学习和分析文件系统.docx
《POCO C++库学习和分析文件系统.docx》由会员分享,可在线阅读,更多相关《POCO C++库学习和分析文件系统.docx(21页珍藏版)》请在冰点文库上搜索。
POCOC++库学习和分析文件系统
POCOC++库学习和分析--文件系统
既然作为一个框架性的库,自然会提供对于文件系统的操作。
在Poco库中,封装了一些类去完成上述操作。
这些类包括了:
1.Poco:
:
Path
2.Poco:
:
File
3.Poco:
:
TemporaryFile
4.Poco:
:
DirectoryIterator
5.Poco:
:
Glob
这些类在实现上并没有什么特殊的注意点,主要是不同操作系统API的调用。
如果想学习API函数的话,确实是一个不错的例子。
在这里将主要介绍这些类的接口和使用,主要以翻译Poco的使用文档为主。
1.Poco:
:
Path
1.1路径:
1.在不同操作系统中,指明文件和目录所在位置的标示符是不一样的。
2.标示符的不一致,会造成代码在不同平台之间移植的困难。
3.Poco:
:
Path类抽象了不同标识符之间的区别,使程序员可以把注意力集中在业务的开发上。
4.Poco:
:
Path类支持Windows、Unix、OpenVMS操作系统。
1.2Poco路径简介:
Poco中的路径包括了:
1.一个可选的节点(node)名:
a)在Windows上,这是计算机在UNC(UniversalNamingConvention)路径中的名字
b)在OpenVMS中,这代表一个集群系统中的节点名
c)在Unix中,此名字未被使用。
2.一个可选的设备(device)名:
a)在Windows上,这是一个驱动器盘符
b)在OpenVMS上,这是存储盘符的名字
c)在Unix,此名字未被使用。
3.一个目录名的列表
4.一个文件名(包括扩展名)和版本号(OpenVMS特有)
Poco支持两种路径:
1.绝对路径
以根目录为起点的描述资源的目录
2.相对目录
以某一个确定路径为起点的描述资源的目录(通常这是用户的当前目录)
相对目录可以被转换为绝对目录(反之,并不成立)。
在Poco中路径的指向可以是一个目录也可以是一个文件。
当路径指向目录时,文件名为空。
下面是Poco中关于路径的一些例子:
[cpp] viewplaincopy
1.Path:
C:
\Windows\system32\cmd.exe
2. Style:
Windows
3. Kind:
absolute, to file
4. Node Name:
–
5. Device Name:
C
6. Directory List:
Windows, system32
7. File Name:
cmd.exe
8. File Version:
–
9.
10.Path:
Poco\Foundation\
11. Style:
Windows
12. Kind:
relative, to directory
13. Node Name:
–
14. Device Name:
–
15. Directory List:
Poco, Foundation
16. File Name:
–
17. File Version:
–
18.
19.Path:
\\www\site\index.html
20. Style:
Windows
21. Kind:
absolute, to file
22. Node Name:
www
23. Device Name:
–
24. Directory List:
site
25. File Name:
index.html
26. File Version:
–
27.
28.Path:
/usr/local/include/Poco/Foundation.h
29. Style:
Unix
30. Kind:
absolute, to file
31. Node Name:
–
32. Device Name:
–
33. Directory List:
usr, local, include, Poco
34. File Name:
index.html
35. File Version:
–
36.
37.Path:
../bin/
38. Style:
Unix
39. Kind:
relative, to directory
40. Node Name:
–
41. Device Name:
–
42. Directory List:
.., bin
43. File Name:
–
44. File Version:
–
45.
46.Path:
VMS001:
:
DSK001:
[POCO.INCLUDE.POCO]POCO.H;2
47. Style:
OpenVMS
48. Kind:
absolute, to file
49. Node Name:
VMS001
50. Device Name:
DSK001
51. Directory List:
POCO, INCLUDE, POCO
52. File Name:
POCO.H
53. File Version:
2
1.3类说明
1.Poco:
:
Path类在Poco库中代表了路径。
2.Poco:
:
Path类并不关心路径所指向的目标在文件系统中是否存在。
这个工作由Poco:
:
File类负责。
3.Poco:
:
Path支持值语义(copy函数和赋值函数),但不支持关系操作符。
构建一个路径
构建一个路径存在着两种方式:
1.从0开始构建,分别构建node、device、directory、file
2.通过一个包含着路径的字符串去解析
在构建时,可以指定路径的格式:
a)PATH_UNIX
b)PATH_WINDOWS
c)PATH_VMS
d)PATH_NATIVE(根据当前系统格式判断)
e)PATH_GUESS(让Poco库自行判断)
从0构造路径
1.创建一个空路径,使用默认的构造函数(默认情况下路径格式为"相对目录")或者构造时使用一个bool参数去指定路径格式(true=absolute,false=relative)
2.如果需要的话,使用下列赋值函数去设置节点和设备名
voidsetNode(conststd:
:
string&node)
voidsetDevice(conststd:
:
string&device)
3.添加路径名
voidpushDirectory(conststd:
:
string&name)
4.设置文件名
voidsetFileName(conststd:
:
string&name)
下面是一个例子:
[cpp] viewplaincopy
1.#include "Poco/Path.h"
2.int main(int argc, char** argv)
3.{
4. Poco:
:
Path p(true); // path will be absolute
5. p.setNode("VMS001");
6. p.setDevice("DSK001");
7. p.pushDirectory("POCO");
8. p.pushDirectory("INCLUDE");
9. p.pushDirectory("POCO");
10. p.setFileName("POCO.H");
11. std:
:
string s(p.toString(Poco:
:
Path:
:
PATH_VMS));
12. // "VMS001:
:
DSK001:
[POCO.INCLUDE.POCO]POCO.H"
13. p.clear(); // start over with a clean state
14. p.pushDirectory("projects");
15. p.pushDirectory("poco");
16. s = p.toString(Poco:
:
Path:
:
PATH_WINDOWS); // "projects\poco\"
17. s = p.toString(Poco:
:
Path:
:
PATH_UNIX); // "projects/poco/"
18. s = p.toString(); // depends on your platform
19. return 0;
20.}
从一个字符串中解析路径名
1.Poco支持从一个字符串中解析路径名
Path(conststd:
:
string&path)
Path(conststd:
:
string&path,Stylestyle)
如果函数调用时,路径格式style不被指定,将使用当前系统路径格式。
2.可以从另一个路径(指向目录名)和文件名,或者两个路径(第一个为绝对路径,第二个为相对路径)构造
Path(constPath&parent,conststd:
:
string&fileName)
Path(constPath&parent,constPath&relative)
路径也可以通过下列函数去构建
Path&assign(conststd:
:
string&path)
Path&parse(conststd:
:
string&path)
Path&assign(conststd:
:
string&path,Stylestyle)
Path&parse(conststd:
:
string&path,Stylestyle)
如果路径非法的话,会抛出Poco:
:
PathSyntaxException异常。
想要测试一个路径字符串是否合法,可以使用tryParse()函数:
booltryParse(conststd:
:
string&path)
booltryParse(conststd:
:
string&path,Stylestyle)
下面是一个例子:
[cpp] viewplaincopy
1.#include "Poco/Path.h"
2.using Poco:
:
Path;
3.int main(int argc, char** argv)
4.{
5. //creating a path will work independent of the OS
6. Path p("C:
\\Windows\\system32\\cmd.exe");
7. Path p("/bin/sh");
8. p = "projects\\poco";
9. p = "projects/poco";
10. p.parse("/usr/include/stdio.h", Path:
:
PATH_UNIX);
11. bool ok = p.tryParse("/usr/*/stdio.h");
12. ok = p.tryParse("/usr/include/stdio.h", Path:
:
PATH_UNIX);
13. ok = p.tryParse("/usr/include/stdio.h", Path:
:
PATH_WINDOWS);
14. ok = p.tryParse("DSK$PROJ:
[POCO]BUILD.COM", Path:
:
PATH_GUESS);
15. return 0;
16.}
Poco:
:
Path类提供了函数用于转换成为字符串:
std:
:
stringtoString()
std:
:
stringtoString(Stylestyle)
当然也可以使用下列函数得到路径不同部分的字符串:
conststd:
:
string&getNode()
conststd:
:
string&getDevice()
conststd:
:
string&directory(intn)(alsooperator[])
conststd:
:
string&getFileName()
可以调用下列函数获取目录的深度:
intdepth()const
通过下面的函数可以得到和设置文件的基本名和扩展名:
std:
:
stringgetBaseName()const
voidsetBaseName(conststd:
:
string&baseName)
std:
:
stringgetExtension()const
voidsetExtension(conststd:
:
string&extension)
下面是一个例子:
[cpp] viewplaincopy
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.}
路径操作:
1.Path&makeDirectory()
确保路径的结尾是一个目录名。
如果原路径有文件名存在的话,添加一个与文件名同名的目录,并清除文件名。
2.Path&makeFile()
确保路径的结尾是一个文件名。
如果原路径是一个目录名,则把最后一个目录名变成文件名,并去除最后一个目录名。
3.Path&makeParent()
Pathparent()const
使路径指向它的父目录(如果存在文件名的话,清除文件名;否则的话则移除最后一个目录名)
4.Path&makeAbsolute()
Path&makeAbsolute(constPath&base)
Pathabsolute()const
Pathabsolute(constPath&base)
转换相对路径为绝对路径
5.Path&append(constPath&path)
添加路径
6.Path&resolve(constPath&path)
如果新的路径为绝对路径,则代替现有的路径;否则则在原路径下追加
路径属性:
1.boolisAbsolute()const
如果路径为绝对路径,返回true;否则为false
2.boolisRelative()const
如果路径为相对路径,返回true;否则为false
3.boolisDirectory()const
如果路径为目录,返回true;否则为false
4.boolisFile()const
如果路径为文件,返回true;否则为false
下面是一个例子:
[cpp] viewplaincopy
1.#include "Poco/Path.h"
2.using Poco:
:
Path;
3.int main(int argc, char** argv)
4.{
5. Path p("/usr/include/stdio.h", Path:
:
PATH_UNIX);
6. Path parent(p.parent());
7. std:
:
string s(parent.toString(Path:
:
PATH_UNIX)); // "/usr/include/"
8. Path p1("stdlib.h");
9. Path p2("/opt/Poco/include/Poco.h", Path:
:
PATH_UNIX);
10. p.resolve(p1);
11. s = p.toString(Path:
:
PATH_UNIX); // "/usr/include/stdlib.h"
12. p.resolve(p2);
13. s = p.toStrin