パスを取り扱う Poco::Pathを紹介します。
PathTest.cpp
・Static なメソッドの使用結果を表示。
・ディレクトリのパスを相対パスで作り、その特性を表示。
・相対ディレクトリパスにファイル名と拡張子を追加してファイルパスを作り、その特性を表示。
・ファイルシステムの全てのルートを調べ、リストアップ。
・上記相対ディレクトリパス/ファイルパスを絶対パスに変換し、それぞれの特性を表示。
#include <Poco/Path.h> #include <Poco/Format.h> #include <string> #include <vector> #include <algorithm> #include "ScopedLogMessage.h" #include "PrepareConsoleLogger.h" class ListRoots { public: ListRoots(ScopedLogMessage& msg) : m_msg(msg) , m_count(0) { } void operator()(std::string root) { m_msg.Message(Poco::format(" root[%z]: %s", m_count++, root)); } private: ScopedLogMessage& m_msg; std::size_t m_count; }; inline std::string IsTrue(bool isTrue) { return std::string(isTrue ? "true":"false"); }; void ShowPathCharacteristics(ScopedLogMessage& msg, const Poco::Path& path) { msg.Message(Poco::format(" isAbsolute() : %s", IsTrue(path.isAbsolute()))); msg.Message(Poco::format(" isRelative() : %s", IsTrue(path.isRelative()))); msg.Message(Poco::format(" isDirectory(): %s", IsTrue(path.isDirectory()))); msg.Message(Poco::format(" isFile() : %s", IsTrue(path.isFile()))); }; int main(int /*argc*/, char** /*argv*/) { PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION); ScopedLogMessage msg("PathTest ", "start", "end"); msg.Message(""); msg.Message("--- Static methods ---"); msg.Message(Poco::format(" home() (Home directory): %s", Poco::Path::home())); msg.Message(Poco::format(" expand(\"%s\") : %s", std::string("~/"), Poco::Path::expand("~/"))); msg.Message(Poco::format(" current() (Current directory): %s", Poco::Path::current())); msg.Message(Poco::format(" temp() (Temporary directory): %s", Poco::Path::temp())); msg.Message(Poco::format(" separator() : %c", Poco::Path::separator())); msg.Message(Poco::format(" pathSeparator() : %c", Poco::Path::pathSeparator())); msg.Message(Poco::format(" null() (Null device): %s", Poco::Path::null())); msg.Message(" listRoots(roots)"); std::vector<std::string> roots; Poco::Path::listRoots(roots); std::for_each(roots.begin(), roots.end(), ListRoots(msg)); msg.Message(""); msg.Message("--- Relative path ---"); Poco::Path dirPath("./"); msg.Message(Poco::format(" dirPath(\"%s\") : %s", std::string("./"), dirPath.toString())); ShowPathCharacteristics(msg, dirPath); Poco::Path filePath(dirPath); msg.Message(Poco::format(" filePath(dirPath) : %s", dirPath.toString())); filePath.setFileName("temp"); msg.Message(Poco::format(" filePath.setFileName(\"%s\"): %s", std::string("temp"), filePath.toString())); filePath.setExtension("txt"); msg.Message(Poco::format(" filePath.setExtension(\"%s\"): %s", std::string("txt"), filePath.toString())); ShowPathCharacteristics(msg, filePath); msg.Message(""); msg.Message("--- Absolute path ---"); Poco::Path absDirPath(dirPath.makeAbsolute()); msg.Message(Poco::format(" absDirPath(dirPath.makeAbsolute()) : %s", absDirPath.toString())); ShowPathCharacteristics(msg, absDirPath); Poco::Path absFilePath(filePath.makeAbsolute()); msg.Message(Poco::format(" absFilePath(filePath.makeAbsolute()): %s", absFilePath.toString())); ShowPathCharacteristics(msg, absFilePath); msg.Message(""); return 0; } |
Results of execution
・On Linux
[0] PathTest start [0] [0] --- Static methods --- [0] home() (Home directory): /home/setsu/ [0] expand("~/") : /home/setsu/ [0] current() (Current directory): /home/setsu/Desktop/Poco_Web/Path/ [0] temp() (Temporary directory): /tmp/ [0] separator() : / [0] pathSeparator() : : [0] null() (Null device): /dev/null [0] listRoots(roots) [0] root[0]: / [0] [0] --- Relative path --- [0] dirPath("./") : [0] isAbsolute() : false [0] isRelative() : true [0] isDirectory(): true [0] isFile() : false [0] filePath(dirPath) : [0] filePath.setFileName("temp"): temp [0] filePath.setExtension("txt"): temp.txt [0] isAbsolute() : false [0] isRelative() : true [0] isDirectory(): false [0] isFile() : true [0] [0] --- Absolute path --- [0] absDirPath(dirPath.makeAbsolute()) : /home/setsu/Desktop/Poco_Web/Path/ [0] isAbsolute() : true [0] isRelative() : false [0] isDirectory(): true [0] isFile() : false [0] absFilePath(filePath.makeAbsolute()): /home/setsu/Desktop/Poco_Web/Path/temp.txt [0] isAbsolute() : true [0] isRelative() : false [0] isDirectory(): false [0] isFile() : true [0] [0] PathTest end |
・On Macintosh OSX 10.6.3
[0] PathTest start [0] [0] --- Static methods --- [0] home() (Home directory): /Users/Setsu/ [0] expand("~/") : /Users/Setsu/ [0] current() (Current directory): /Users/Setsu/Desktop/Poco_Web/Path/ [0] temp() (Temporary directory): /var/folders/iR/iRcZrcWkGCe6fDFVNkPVjE+++TI/-Tmp-/ [0] separator() : / [0] pathSeparator() : : [0] null() (Null device): /dev/null [0] listRoots(roots) [0] root[0]: / [0] [0] --- Relative path --- [0] dirPath("./") : [0] isAbsolute() : false [0] isRelative() : true [0] isDirectory(): true [0] isFile() : false [0] filePath(dirPath) : [0] filePath.setFileName("temp"): temp [0] filePath.setExtension("txt"): temp.txt [0] isAbsolute() : false [0] isRelative() : true [0] isDirectory(): false [0] isFile() : true [0] [0] --- Absolute path --- [0] absDirPath(dirPath.makeAbsolute()) : /Users/Setsu/Desktop/Poco_Web/Path/ [0] isAbsolute() : true [0] isRelative() : false [0] isDirectory(): true [0] isFile() : false [0] absFilePath(filePath.makeAbsolute()): /Users/Setsu/Desktop/Poco_Web/Path/temp.txt [0] isAbsolute() : true [0] isRelative() : false [0] isDirectory(): false [0] isFile() : true [0] [0] PathTest end |
・On Windows XP sp3
[0] PathTest start [0] [0] --- Static methods --- [0] home() (Home directory): C:\Documents and Settings\setsu\ [0] expand("~/") : ~/ [0] current() (Current directory): c:\Documents and Settings\setsu\デスクトップ\Poco_Web\Path\ [0] temp() (Temporary directory): C:\DOCUME~1\setsu\LOCALS~1\Temp\ [0] separator() : \ [0] pathSeparator() : ; [0] null() (Null device): NUL: [0] listRoots(roots) [0] root[0]: A:\ [0] root[1]: C:\ [0] root[2]: D:\ [0] root[3]: Z:\ [0] [0] --- Relative path --- [0] dirPath("./") : [0] isAbsolute() : false [0] isRelative() : true [0] isDirectory(): true [0] isFile() : false [0] filePath(dirPath) : [0] filePath.setFileName("temp"): temp [0] filePath.setExtension("txt"): temp.txt [0] isAbsolute() : false [0] isRelative() : true [0] isDirectory(): false [0] isFile() : true [0] [0] --- Absolute path --- [0] absDirPath(dirPath.makeAbsolute()) : c:\Documents and Settings\setsu\デスクトップ\Poco_Web\Path\ [0] isAbsolute() : true [0] isRelative() : false [0] isDirectory(): true [0] isFile() : false [0] absFilePath(filePath.makeAbsolute()): c:\Documents and Settings\setsu\デスクトップ\Poco_Web\Path\temp.txt [0] isAbsolute() : true [0] isRelative() : false [0] isDirectory(): false [0] isFile() : true [0] [0] PathTest end |
Downloads
・ここをクリックすると、makefile や VC++ プロジェクトなど一式がダウンロードできます。
(2013.05.31 updated)
・2010年5月17日からのダウンロード数:1160
Subversion
・フリーの Subversion ホスティングサービス Assemblaで、ソースコードを管理しています。
Reference
・POCO流ファイル処理あれこれ
・http://pocoproject.org にある Files のプレセンテーション。(PDF)
![]() |
Copyright © 2010 Round Square Inc. All rights reserved. |
---|
0 Comments.