ファイルとディレクトリを取り扱う Poco::File を紹介します。
FileTest.cpp
・ディレクトリを作り、その特性を表示。
・ファイルを作り、その属性を表示。
・ファイルの属性を変更して表示。
・ディレクトリを再帰的に削除。
Windows環境では ReadOnly の File は削除できず、例外が飛ぶことに注意。
#include <Poco/File.h> #include <Poco/Path.h> #include <Poco/Format.h> #include <Poco/DateTimeFormatter.h> #include <Poco/Timezone.h> #include <Poco/String.h> #include <Poco/Exception.h> #include <string> #include "ScopedLogMessage.h" #include "PrepareConsoleLogger.h" #define ShowBoolWithException(msg, text, result) \ try \ { \ msg.Message(Poco::format(text+": %s", IsTrue(result))); \ } \ catch(const Poco::Exception& exp) \ { \ msg.Message(Poco::format(text+": EXCEPTION [%s]", exp.displayText())); \ } \ #define ShowSizeWithException(msg, text, size) \ try \ { \ msg.Message(Poco::format(text+": %z", std::size_t(size))); \ } \ catch(const Poco::Exception& exp) \ { \ msg.Message(Poco::format(text+": EXCEPTION [%s]", exp.displayText())); \ } \ #define ShowDateWithException(msg, text, timestamp) \ try \ { \ ShowDateTime(msg, timestamp, text+": %s"); \ } \ catch(const Poco::Exception& exp) \ { \ msg.Message(Poco::format(text+": EXCEPTION [%s]", exp.displayText())); \ } \ inline std::string IsTrue(bool isTrue) { return std::string(isTrue ? "true":"false"); }; void ShowDateTime(ScopedLogMessage& msg, const Poco::Timestamp& timestamp, const std::string& fmt) { Poco::DateTime createdDateTime(timestamp); createdDateTime.makeLocal(Poco::Timezone::tzd()); msg.Message(Poco::format( Poco::format(fmt, Poco::DateTimeFormatter::format(createdDateTime, "%w %b %e %H:%M:%S %%s %Y")) , Poco::Timezone::name())); }; void ShowFileCharacteristics(ScopedLogMessage& msg, const Poco::File& file) { ShowBoolWithException(msg, std::string(" isDevice() "), file.isDevice()); ShowBoolWithException(msg, std::string(" isDirectory() "), file.isDirectory()); ShowBoolWithException(msg, std::string(" isFile() "), file.isFile()); ShowBoolWithException(msg, std::string(" isHidden() "), file.isHidden()); ShowBoolWithException(msg, std::string(" isLink() "), file.isLink()); ShowBoolWithException(msg, std::string(" canExecute() "), file.canExecute()); ShowBoolWithException(msg, std::string(" canRead() "), file.canRead()); ShowBoolWithException(msg, std::string(" canWrite() "), file.canWrite()); ShowSizeWithException(msg, std::string(" getSize() "), file.getSize()); ShowDateWithException(msg, std::string(" created() "), file.created()); ShowDateWithException(msg, std::string(" getLastModified() "), file.getLastModified()); }; int main(int /*argc*/, char** /*argv*/) { PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION); ScopedLogMessage msg("FileTest ", "start", "end"); Poco::File dir(Poco::Path("testDir1/testDir2/testDir3", Poco::Path::PATH_UNIX)); dir.createDirectories(); if(dir.exists()) { msg.Message(Poco::format(" %s created", dir.path())); ShowFileCharacteristics(msg, dir); } msg.Message(""); Poco::File file(Poco::Path(Poco::Path(dir.path()), "testFile.txt")); file.createFile(); if(file.exists()) { msg.Message(Poco::format(" %s created", file.path())); ShowFileCharacteristics(msg, file); msg.Message(""); msg.Message(Poco::format(" change %s characteristics", file.path())); file.setExecutable(true); file.setReadOnly(true); file.setWriteable(false); ShowFileCharacteristics(msg, file); msg.Message(""); try { file.renameTo(Poco::replace(file.path(), "testFile", "renamedFile")); msg.Message(Poco::format(" %s renamed", file.path())); } catch(const Poco::Exception& exp) { msg.Message(Poco::format(" EXCEPTION [%s]", exp.displayText())); } } msg.Message(""); #if defined(POCO_OS_FAMILY_WINDOWS) file.setWriteable(true); #endif try { Poco::File topDir(Poco::Path("testDir1", Poco::Path::PATH_UNIX)); topDir.remove(true); msg.Message(Poco::format(" %s %s", topDir.path(), std::string(topDir.exists() ? "exists":"removed"))); } catch(const Poco::Exception& exp) { msg.Message(Poco::format(" EXCEPTION [%s]", exp.displayText())); } return 0; } |
Results of execution
・On Linux
[0] FileTest start [0] testDir1/testDir2/testDir3 created [0] isDevice() : false [0] isDirectory() : true [0] isFile() : false [0] isHidden() : false [0] isLink() : false [0] canExecute() : true [0] canRead() : true [0] canWrite() : true [0] getSize() : 4096 [0] created() : Wed May 19 19:38:19 JST 2010 [0] getLastModified() : Wed May 19 19:38:19 JST 2010 [0] [0] testDir1/testDir2/testDir3/testFile.txt created [0] isDevice() : false [0] isDirectory() : false [0] isFile() : true [0] isHidden() : false [0] isLink() : false [0] canExecute() : false [0] canRead() : true [0] canWrite() : true [0] getSize() : 0 [0] created() : Wed May 19 19:38:19 JST 2010 [0] getLastModified() : Wed May 19 19:38:19 JST 2010 [0] [0] change testDir1/testDir2/testDir3/testFile.txt characteristics [0] isDevice() : false [0] isDirectory() : false [0] isFile() : true [0] isHidden() : false [0] isLink() : false [0] canExecute() : true [0] canRead() : true [0] canWrite() : false [0] getSize() : 0 [0] created() : Wed May 19 19:38:19 JST 2010 [0] getLastModified() : Wed May 19 19:38:19 JST 2010 [0] [0] testDir1/testDir2/testDir3/renamedFile.txt renamed [0] [0] testDir1 removed [0] FileTest end |
・On Macintosh OSX 10.6.3
[0] FileTest start [0] testDir1/testDir2/testDir3 created [0] isDevice() : false [0] isDirectory() : true [0] isFile() : false [0] isHidden() : false [0] isLink() : false [0] canExecute() : true [0] canRead() : true [0] canWrite() : true [0] getSize() : 68 [0] created() : Wed May 19 05:16:56 JST 2010 [0] getLastModified() : Wed May 19 05:16:56 JST 2010 [0] [0] testDir1/testDir2/testDir3/testFile.txt created [0] isDevice() : false [0] isDirectory() : false [0] isFile() : true [0] isHidden() : false [0] isLink() : false [0] canExecute() : false [0] canRead() : true [0] canWrite() : true [0] getSize() : 0 [0] created() : Wed May 19 05:16:56 JST 2010 [0] getLastModified() : Wed May 19 05:16:56 JST 2010 [0] [0] change testDir1/testDir2/testDir3/testFile.txt characteristics [0] isDevice() : false [0] isDirectory() : false [0] isFile() : true [0] isHidden() : false [0] isLink() : false [0] canExecute() : true [0] canRead() : true [0] canWrite() : false [0] getSize() : 0 [0] created() : Wed May 19 05:16:56 JST 2010 [0] getLastModified() : Wed May 19 05:16:56 JST 2010 [0] [0] testDir1/testDir2/testDir3/renamedFile.txt renamed [0] [0] testDir1 removed [0] FileTest end |
・On Windows XP sp3
[0] FileTest start [0] testDir1\testDir2\testDir3 created [0] isDevice() : EXCEPTION [Access to file denied] [0] isDirectory() : true [0] isFile() : false [0] isHidden() : false [0] isLink() : false [0] canExecute() : false [0] canRead() : true [0] canWrite() : true [0] getSize() : 0 [0] created() : Wed May 19 19:31:32 東京 (標準時) 2010 [0] getLastModified() : Wed May 19 19:31:32 東京 (標準時) 2010 [0] [0] testDir1\testDir2\testDir3\testFile.txt created [0] isDevice() : false [0] isDirectory() : false [0] isFile() : true [0] isHidden() : false [0] isLink() : false [0] canExecute() : false [0] canRead() : true [0] canWrite() : true [0] getSize() : 0 [0] created() : Wed May 19 19:31:32 東京 (標準時) 2010 [0] getLastModified() : Wed May 19 19:31:32 東京 (標準時) 2010 [0] [0] change testDir1\testDir2\testDir3\testFile.txt characteristics [0] isDevice() : false [0] isDirectory() : false [0] isFile() : true [0] isHidden() : false [0] isLink() : false [0] canExecute() : false [0] canRead() : true [0] canWrite() : false [0] getSize() : 0 [0] created() : Wed May 19 19:31:32 東京 (標準時) 2010 [0] getLastModified() : Wed May 19 19:31:32 東京 (標準時) 2010 [0] [0] testDir1\testDir2\testDir3\renamedFile.txt renamed [0] [0] testDir1 removed [0] FileTest end |
Downloads
・ここをクリックすると、makefile や VC++ プロジェクトなど一式がダウンロードできます。
(2013.05.31 updated)
・2010年5月19日からのダウンロード数:1219
Subversion
・フリーの Subversion ホスティングサービス Assemblaで、ソースコードを管理しています。
Reference
・POCO流ファイル処理あれこれ
・http://pocoproject.org にある Files のプレセンテーション。(PDF)
![]() |
Copyright © 2010 Round Square Inc. All rights reserved. |
---|
ブログにコメントがあったので来てみました。
いろいろ参考にさせていただきます