+#include <msp/core/algorithm.h>
#include <msp/core/application.h>
#include <msp/core/getopt.h>
#include <msp/fs/dir.h>
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):
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);
}
}
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);
}
}
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)
{
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);
}
}
- 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)