From 857c4856742bc3f5b327098b3f82e4cb58644d2b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 31 Jul 2011 18:34:38 +0300 Subject: [PATCH] Add an ls example to demonstrate some of the fs components --- Build | 9 +++ examples/ls.cpp | 146 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 examples/ls.cpp diff --git a/Build b/Build index 318f341..fc7b33a 100644 --- a/Build +++ b/Build @@ -93,6 +93,15 @@ package "mspcore" }; }; + program "ls" + { + source "examples/ls.cpp"; + build_info + { + library "mspcore"; + }; + }; + tarball "@src" { source "License.txt"; diff --git a/examples/ls.cpp b/examples/ls.cpp new file mode 100644 index 0000000..fd4d71f --- /dev/null +++ b/examples/ls.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace Msp; + +class Ls: public RegisteredApplication +{ +private: + typedef vector Row; + + bool long_format; + vector args; + +public: + Ls(int, char **); + + virtual int main(); +private: + Row create_row(const string &, const FS::Stat &); + void print_rows(const list &); +}; + +Ls::Ls(int argc, char **argv): + long_format(false) +{ + GetOpt getopt; + getopt.add_option('l', "long", long_format, GetOpt::NO_ARG); + getopt(argc, argv); + + args = getopt.get_args(); + if(args.empty()) + args.push_back("."); +} + +int Ls::main() +{ + list file_rows; + bool first = true; + for(vector::const_iterator i=args.begin(); i!=args.end(); ++i) + { + FS::Path path = *i; + 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) + { + if(long_format) + rows.push_back(create_row(*j, FS::lstat(path / *j))); + else + { + Row r; + r.push_back(*j); + rows.push_back(r); + } + } + + if(!first) + IO::print("\n"); + if(args.size()>1) + IO::print("%s:\n", *i); + print_rows(rows); + + first = false; + } + else if(long_format) + file_rows.push_back(create_row(*i, stat)); + else + { + Row r; + r.push_back(*i); + file_rows.push_back(r); + } + } + + if(!file_rows.empty()) + { + if(!first) + IO::print("\n"); + print_rows(file_rows); + } + + return 0; +} + +Ls::Row Ls::create_row(const string &name, const FS::Stat &st) +{ + Row result; + if(st.is_regular()) + result.push_back("-"); + else if(st.is_directory()) + result.push_back("D"); + else if(st.is_symlink()) + result.push_back("L"); + else + result.push_back("?"); + + result.push_back(st.get_owner()); + result.push_back(st.get_group()); + result.push_back(format("%d", st.get_size())); + result.push_back(Time::DateTime(st.get_modify_time()).format("%Y-%m-%d %H:%M:%S")); + result.push_back(name); + + return result; +} + +void Ls::print_rows(const list &rows) +{ + vector col_width; + + for(list::const_iterator i=rows.begin(); i!=rows.end(); ++i) + { + const Row &row = *i; + if(row.size()>col_width.size()) + col_width.resize(row.size(), 0); + for(unsigned j=0; j::const_iterator i=rows.begin(); i!=rows.end(); ++i) + { + const Row &row = *i; + string line; + for(unsigned j=0; j0) + line += ' '; + unsigned padding = col_width[j]-row[j].size(); + if(j==3) + line += string(padding, ' '); + line += row[j]; + if(j!=3) + line += string(padding, ' '); + } + line += '\n'; + IO::print(line); + } +} -- 2.45.2