]> git.tdb.fi Git - libs/core.git/commitdiff
Update example programs
authorMikko Rasa <tdb@tdb.fi>
Sun, 29 Aug 2021 23:49:36 +0000 (02:49 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 30 Aug 2021 00:25:18 +0000 (03:25 +0300)
examples/ls.cpp
examples/syncdir.cpp

index 3e873180d57bf04b72e53b40281bf9088693a2c2..42667d5e07acd998038ee4e1a584f606aa6987b7 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/algorithm.h>
 #include <msp/core/application.h>
 #include <msp/core/getopt.h>
 #include <msp/fs/dir.h>
@@ -23,7 +24,7 @@ public:
        virtual int main();
 private:
        Row create_row(const string &, const FS::Stat &);
-       void print_rows(const list<Row> &);
+       void print_rows(const vector<Row> &);
 };
 
 Ls::Ls(int argc, char **argv):
@@ -40,25 +41,25 @@ Ls::Ls(int argc, char **argv):
 
 int Ls::main()
 {
-       list<Row> file_rows;
+       vector<Row> file_rows;
        bool first = true;
-       for(vector<string>::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<Row> rows;
-                       list<string> files = FS::list_files(path);
-                       files.sort();
-                       for(list<string>::iterator j=files.begin(); j!=files.end(); ++j)
+                       vector<Row> rows;
+                       vector<string> 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<Row> &rows)
+void Ls::print_rows(const vector<Row> &rows)
 {
        vector<unsigned> col_width;
 
-       for(list<Row>::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<row.size(); ++j)
                        col_width[j] = max<unsigned>(col_width[j], row[j].size());
        }
 
-       for(list<Row>::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<row.size(); ++j)
                {
index 7d4789aea944604c58f3f5cd21386cd71686fee8..1c8cb263ca18b0e0aebfbb853b7fb2b347414240 100644 (file)
@@ -54,10 +54,9 @@ void SyncDir::sync_directory(const FS::Path &src, const FS::Path &dest)
                FS::mkdir(dest, 0755);
        }
 
-       list<string> src_files = FS::list_files(src);
-       for(list<string>::const_iterator i=src_files.begin(); i!=src_files.end(); ++i)
+       vector<string> 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<string> dest_files = FS::list_files(dest);
-       for(list<string>::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)