本文共 8743 字,大约阅读时间需要 29 分钟。
在Poco库中,封装了一些类去完成文件系统的操作,这些类包括:
1. Poco::Path
2. Poco::File
3. Poco::TemporaryFile
4. Poco::DirectoryIterator
5. Poco::Glob
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: Poco\Foundation\ Style: Windows Kind: relative, to directory Node Name: – Device Name: – Directory List: Poco, Foundation File Name: – 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: ../bin/ Style: Unix Kind: relative, to directory Node Name: – Device Name: – Directory List: .., bin File Name: – File Version: – Path: VMS001::DSK001:[POCO.INCLUDE.POCO]POCO.H;2 Style: OpenVMS Kind: absolute, to file Node Name: VMS001 Device Name: DSK001 Directory List: POCO, INCLUDE, POCO File Name: POCO.H File Version: 2
#include "Poco/Path.h"using namespace std;int main(int argc, char** argv){ Poco::Path p(true); // path will be absolute p.setNode("VMS001"); p.setDevice("DSK001"); p.pushDirectory("POCO"); p.pushDirectory("INCLUDE"); p.pushDirectory("POCO"); p.setFileName("POCO.H"); std::string s(p.toString(Poco::Path::PATH_VMS)); // "VMS001::DSK001:[POCO.INCLUDE.POCO]POCO.H" p.clear(); // start over with a clean state p.pushDirectory("projects"); p.pushDirectory("poco"); s = p.toString(Poco::Path::PATH_WINDOWS); // "projects\poco\" cout<从一个字符串中解析路径名<
#include "Poco/Path.h" using Poco::Path; int main(int argc, char** argv) { //creating a path will work independent of the OS Path p("C:\\Windows\\system32\\cmd.exe"); Path p("/bin/sh"); p = "projects\\poco"; p = "projects/poco"; p.parse("/usr/include/stdio.h", Path::PATH_UNIX); bool ok = p.tryParse("/usr/*/stdio.h"); ok = p.tryParse("/usr/include/stdio.h", Path::PATH_UNIX); ok = p.tryParse("/usr/include/stdio.h", Path::PATH_WINDOWS); ok = p.tryParse("DSK$PROJ:[POCO]BUILD.COM", Path::PATH_GUESS); return 0; }Poco::Path类提供了函数用于转换成为字符串:
#include "Poco/Path.h" using Poco::Path; int main(int argc, char** argv) { Path p("c:\\projects\\poco\\build_vs80.cmd", Path::PATH_WINDOWS); std::string device(p.getDevice()); // "c" int n = p.depth(); // 2 std::string dir1(p.directory(0)); // "projects" std::string dir2(p[1]); // "poco" std::string fileName(p[2]); // "build_vs80.cmd" fileName = p.getFileName(); std::string baseName(p.getBaseName()); // "build_vs80" std::string extension(p.getExtension()); // "cmd" p.setBaseName("build_vs71"); fileName = p.getFileName(); // "build_vs71.cmd" return 0; }路径操作:
#include "Poco/Path.h" using Poco::Path; int main(int argc, char** argv) { Path p("/usr/include/stdio.h", Path::PATH_UNIX); Path parent(p.parent()); std::string s(parent.toString(Path::PATH_UNIX)); // "/usr/include/" Path p1("stdlib.h"); Path p2("/opt/Poco/include/Poco.h", Path::PATH_UNIX); p.resolve(p1); s = p.toString(Path::PATH_UNIX); // "/usr/include/stdlib.h" p.resolve(p2); s = p.toString(Path::PATH_UNIX); // "/opt/Poco/include/Poco.h" return 0; }特殊的目录和文件
#include "Poco/Path.h" #include路径和环境变量using Poco::Path; int main(int argc, char** argv) { std::cout << "cwd: " << Path::current() << std::endl << "home: " << Path::home() << std::endl << "temp: " << Path::temp() << std::endl << "null: " << Path::null() << std::endl; return 0; }
#include "Poco/Path.h" using Poco::Path; int main(int argc, char** argv) { std::string config("%HOMEDRIVE%%HOMEPATH%\\config.ini"); // std::string config("$HOME/config.ini"); std::string expConfig(Path::expand(config)); return 0; }文件系统主目录:
#include "Poco/Path.h" #include "Poco/Environment.h" using Poco::Path; using Poco::Environment; int main(int argc, char** argv) { std::string shellName("cmd.exe"); // Windows // std::string shellName("sh"); // Unix std::string path(Environment::get("PATH")); Path shellPath; bool found = Path::find(path, shellName, shellPath); std::string s(shellPath.toString()); return 0; }
转载地址:http://drpmi.baihongyu.com/