Poco 模拟tree命令
tree.cpp
// tree.cpp // After //poco/1.4/Foundation/samples/dir/src/dir.cpp // #include "Poco/DirectoryIterator.h" #include "Poco/File.h" #include "Poco/Path.h" #include "Poco/DateTimeFormatter.h" #include "Poco/DateTimeFormat.h" #include "Poco/Exception.h" #include <iostream> using Poco::DirectoryIterator; using Poco::File; using Poco::Path; using Poco::DateTimeFormatter; using Poco::DateTimeFormat; int recuDir(std::string dir, std::string preSpc) { try { DirectoryIterator it(dir); DirectoryIterator end; preSpc += " "; std::cout << preSpc << '|' << "- " << '.' << std::endl; std::cout << preSpc << '|' << "- " << ".." << std::endl; while (it != end) { Path p(it->path()); std::cout << preSpc << '|' << "- " << (it->isDirectory() ? 'd' : '-') << (it->canRead() ? 'r' : '-') << (it->canWrite() ? 'w' : '-') << ' ' << DateTimeFormatter::format(it->getLastModified(), DateTimeFormat::SORTABLE_FORMAT) << ' ' << p.getFileName() << std::endl; if (it->isDirectory()) { std::cout << preSpc + " \\" << std::endl; if (recuDir(dir + Path::separator() + p.getFileName(), preSpc) == 1) return 1; } ++it; } } catch (Poco::Exception& exc) { std::cerr << exc.displayText() << std::endl; return 1; } return 0; } int main(int argc, char** argv) { std::string dir; if (argc > 1) dir = argv[1]; else dir = Path::current(); std::cout << dir << std::endl; recuDir(dir, ""); return 0; }
编译:
[root@slayer src]# g++ tree.cpp -lPocoFoundation
结果:
[root@slayer src]# ./a.out /tmp/src/ |- . |- .. |- -rw 2012-12-09 17:46:11 tree.cpp |- -rw 2012-12-09 17:34:43 dir.cpp |- -rw 2012-12-09 17:34:43 .tree.cpp.swp |- drw 2012-12-09 17:34:43 testDir \ |- . |- .. |- -rw 2012-12-09 17:48:58 a.out [root@slayer src]# tree . |-- a.out |-- dir.cpp |-- testDir `-- tree.cpp 1 directory, 3 files