From: Mikko Rasa Date: Sun, 29 Aug 2021 23:49:36 +0000 (+0300) Subject: Update example programs X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=1fa7166286abd6a31203b85fce11e810136ffd96;p=libs%2Fcore.git Update example programs --- diff --git a/examples/ls.cpp b/examples/ls.cpp index 3e87318..42667d5 100644 --- a/examples/ls.cpp +++ b/examples/ls.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -23,7 +24,7 @@ public: virtual int main(); private: Row create_row(const string &, const FS::Stat &); - void print_rows(const list &); + void print_rows(const vector &); }; Ls::Ls(int argc, char **argv): @@ -40,25 +41,25 @@ Ls::Ls(int argc, char **argv): int Ls::main() { - list file_rows; + vector file_rows; bool first = true; - for(vector::const_iterator i=args.begin(); i!=args.end(); ++i) + for(const string &a: args) { - FS::Path path = *i; + FS::Path path = a; FS::Stat stat = FS::lstat(path); if(stat.is_directory()) { - list rows; - list files = FS::list_files(path); - files.sort(); - for(list::iterator j=files.begin(); j!=files.end(); ++j) + vector rows; + vector files = FS::list_files(path); + sort(files); + for(const string &fn: files) { if(long_format) - rows.push_back(create_row(*j, FS::lstat(path / *j))); + rows.push_back(create_row(fn, FS::lstat(path/fn))); else { Row r; - r.push_back(*j); + r.push_back(fn); rows.push_back(r); } } @@ -66,17 +67,17 @@ int Ls::main() if(!first) IO::print("\n"); if(args.size()>1) - IO::print("%s:\n", *i); + IO::print("%s:\n", a); print_rows(rows); first = false; } else if(long_format) - file_rows.push_back(create_row(*i, stat)); + file_rows.push_back(create_row(a, stat)); else { Row r; - r.push_back(*i); + r.push_back(a); file_rows.push_back(r); } } @@ -112,22 +113,20 @@ Ls::Row Ls::create_row(const string &name, const FS::Stat &st) return result; } -void Ls::print_rows(const list &rows) +void Ls::print_rows(const vector &rows) { vector col_width; - for(list::const_iterator i=rows.begin(); i!=rows.end(); ++i) + for(const Row &row: rows) { - const Row &row = *i; if(row.size()>col_width.size()) col_width.resize(row.size(), 0); for(unsigned j=0; j(col_width[j], row[j].size()); } - for(list::const_iterator i=rows.begin(); i!=rows.end(); ++i) + for(const Row &row: rows) { - const Row &row = *i; string line; for(unsigned j=0; j src_files = FS::list_files(src); - for(list::const_iterator i=src_files.begin(); i!=src_files.end(); ++i) + vector src_files = FS::list_files(src); + for(const string &fn: src_files) { - const string &fn = *i; FS::Stat ss = FS::stat(src/fn); if(ss.is_directory()) sync_directory(src/fn, dest/fn); @@ -69,19 +68,15 @@ void SyncDir::sync_directory(const FS::Path &src, const FS::Path &dest) } } - list dest_files = FS::list_files(dest); - for(list::const_iterator i=dest_files.begin(); i!=dest_files.end(); ++i) - { - if(find(src_files.begin(), src_files.end(), *i)==src_files.end()) + for(const string &fn: FS::list_files(dest)) + if(find(src_files.begin(), src_files.end(), fn)==src_files.end()) { - const string &fn = *i; IO::print("Removing obsolete %s\n", dest/fn); if(FS::is_dir(dest/fn)) FS::rmpath(dest/fn); else FS::unlink(dest/fn); } - } } void SyncDir::copy_file(const FS::Path &src, const FS::Path &dest)