]> git.tdb.fi Git - libs/core.git/blobdiff - examples/ls.cpp
Add move semantics to Variant
[libs/core.git] / examples / ls.cpp
index fd4d71f382bd6212c8e8d9ec0adb3c74636be12d..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):
@@ -31,34 +32,34 @@ Ls::Ls(int argc, char **argv):
 {
        GetOpt getopt;
        getopt.add_option('l', "long", long_format, GetOpt::NO_ARG);
+       getopt.add_argument("file", args, GetOpt::OPTIONAL_ARG);
        getopt(argc, argv);
 
-       args = getopt.get_args();
        if(args.empty())
                args.push_back(".");
 }
 
 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(col_width[j], row[j].size());
+                       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)
                {