2009-02-17 09:45:57 +08:00
|
|
|
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
|
2008-05-02 22:19:38 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
This is an example illustrating the use of the dir_nav component from the dlib C++ Library.
|
|
|
|
It prints a listing of all directories and files in the users
|
|
|
|
current working directory or the directory specified on the command line.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include "dlib/dir_nav.h"
|
2008-09-07 10:54:28 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dlib;
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
string loc;
|
|
|
|
if (argc == 2)
|
|
|
|
loc = argv[1];
|
|
|
|
else
|
|
|
|
loc = "."; // if no argument is given then use the current working dir.
|
|
|
|
|
|
|
|
directory test(loc);
|
|
|
|
|
2008-09-07 10:54:28 +08:00
|
|
|
std::vector<directory> dirs;
|
|
|
|
std::vector<file> files;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
cout << "directory: " << test.name() << endl;
|
|
|
|
cout << "full path: " << test.full_name() << endl;
|
|
|
|
cout << "is root: " << ((test.is_root())?"yes":"no") << endl;
|
|
|
|
|
|
|
|
// get all directories and files in test
|
|
|
|
test.get_dirs(dirs);
|
|
|
|
test.get_files(files);
|
|
|
|
|
2009-11-29 23:52:54 +08:00
|
|
|
// sort the files and directories
|
2008-09-07 10:54:28 +08:00
|
|
|
sort(files.begin(), files.end());
|
|
|
|
sort(dirs.begin(), dirs.end());
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
cout << "\n\n\n";
|
|
|
|
|
|
|
|
// print all the subdirectories
|
2008-09-07 10:54:28 +08:00
|
|
|
for (unsigned long i = 0; i < dirs.size(); ++i)
|
|
|
|
cout << " <DIR> " << dirs[i].name() << "\n";
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
// print all the subfiles
|
2008-09-07 10:54:28 +08:00
|
|
|
for (unsigned long i = 0; i < files.size(); ++i)
|
|
|
|
cout << setw(13) << files[i].size() << " " << files[i].name() << "\n";
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
|
2008-09-07 10:54:28 +08:00
|
|
|
cout << "\n\nnumber of dirs: " << dirs.size() << endl;
|
|
|
|
cout << "number of files: " << files.size() << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
}
|
2008-09-07 10:54:28 +08:00
|
|
|
catch (file::file_not_found& e)
|
2008-05-02 22:19:38 +08:00
|
|
|
{
|
2009-11-29 23:52:54 +08:00
|
|
|
cout << "file not found or accessible: " << e.info << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
}
|
2008-09-07 10:54:28 +08:00
|
|
|
catch (directory::dir_not_found& e)
|
2008-05-02 22:19:38 +08:00
|
|
|
{
|
2009-11-29 23:52:54 +08:00
|
|
|
cout << "dir not found or accessible: " << e.info << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
}
|
2008-09-07 10:54:28 +08:00
|
|
|
catch (directory::listing_error& e)
|
2008-05-02 22:19:38 +08:00
|
|
|
{
|
|
|
|
cout << "listing error: " << e.info << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|