From 10c884b155d647a4e3f6daf4f179424c97ceaecf Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Fri, 14 Feb 2025 09:45:57 +0800 Subject: [PATCH] tmp --- lib/io/dsv_io.cpp | 109 ++++++++++++++++++++++--------------- lib/io/dsv_io.h | 9 ++- tool/dsviewer/dsviewer.cpp | 13 +++++ tool/dsviewer/dsviewer.h | 4 +- 4 files changed, 89 insertions(+), 46 deletions(-) diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index a04ec52..b79c1dd 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -427,96 +427,117 @@ gctl::dsv_io gctl::dsv_io::export_table(bool ignore_disabled) return out_table; } -void gctl::dsv_io::info(int t) +void gctl::dsv_io::info(int t, std::ostream &os) { if (t & HeadInfo) { - std::clog << "Head(s): " << head_num_ << "\n"; + os << "Head(s): " << head_num_ << "\n"; for (size_t i = 0; i < heads_.size(); i++) { - std::clog << heads_[i] << "\n"; + os << heads_[i] << "\n"; } - std::clog << "------------\n"; + os << "------------\n"; } if (t & AttInfo) { - std::clog << "Annotation(s): " << annotates_.size() << "\n"; + os << "Annotation(s): " << annotates_.size() << "\n"; for (size_t i = 0; i < annotates_.size(); i++) { - std::clog << annotates_[i] << "\n"; + os << annotates_[i] << "\n"; } - std::clog << "------------\n"; + os << "------------\n"; } if (t & TagInfo) { - std::clog << "Tag(s): " << tags_.size() << "\n"; + os << "Tag(s): " << tags_.size() << "\n"; for (size_t i = 0; i < tags_.size(); i++) { - std::clog << tags_[i] << "\n"; + os << tags_[i] << "\n"; } - std::clog << "------------\n"; + os << "------------\n"; } if (t & ColInfo) { - std::clog << "Columns:\n"; + os << "Columns:\n"; for (size_t i = 1; i <= col_num_; i++) { if (table_[0][i].str_ != "") { - std::clog << table_[0][i].str_ << " | "; - if (table_[0][i].out_ok_) std::clog << "Enabled | "; - else std::clog << "Disabled | "; - if (table_[1][i].type_ == String) std::clog << "String | "; - if (table_[1][i].type_ == Int) std::clog << "Int | "; - if (table_[1][i].type_ == Float) std::clog << "Float | "; - std::clog << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; + os << table_[0][i].str_ << " | "; + if (table_[0][i].out_ok_) os << "Enabled | "; + else os << "Disabled | "; + if (table_[1][i].type_ == String) os << "String | "; + if (table_[1][i].type_ == Int) os << "Int | "; + if (table_[1][i].type_ == Float) os << "Float | "; + os << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; } else { - std::clog << "C" + std::to_string(i) << " | "; - if (table_[0][i].out_ok_) std::clog << "Enabled | "; - else std::clog << "Disabled | "; - if (table_[1][i].type_ == String) std::clog << "String | "; - if (table_[1][i].type_ == Int) std::clog << "Int | "; - if (table_[1][i].type_ == Float) std::clog << "Float | "; - std::clog << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; + os << "C" + std::to_string(i) << " | "; + if (table_[0][i].out_ok_) os << "Enabled | "; + else os << "Disabled | "; + if (table_[1][i].type_ == String) os << "String | "; + if (table_[1][i].type_ == Int) os << "Int | "; + if (table_[1][i].type_ == Float) os << "Float | "; + os << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; } - std::clog << std::endl; + os << std::endl; } - std::clog << "------------\n"; + os << "------------\n"; } if (t & RowInfo) { - std::clog << "Rows:\n"; + os << "Rows:\n"; for (size_t i = 1; i <= row_num_; i++) { if (table_[i][0].str_ != "") { - std::clog << table_[i][0].str_ << " | "; - if (table_[i][0].out_ok_) std::clog << "Enabled | "; - else std::clog << "Disabled | "; - if (table_[i][1].type_ == String) std::clog << "String | "; - if (table_[i][1].type_ == Int) std::clog << "Int | "; - if (table_[i][1].type_ == Float) std::clog << "Float | "; - std::clog << table_[i][1].str_ << " -> " << table_[i][col_num_].str_; + os << table_[i][0].str_ << " | "; + if (table_[i][0].out_ok_) os << "Enabled | "; + else os << "Disabled | "; + if (table_[i][1].type_ == String) os << "String | "; + if (table_[i][1].type_ == Int) os << "Int | "; + if (table_[i][1].type_ == Float) os << "Float | "; + os << table_[i][1].str_ << " -> " << table_[i][col_num_].str_; } else { - std::clog << "R" + std::to_string(i) << " | "; - if (table_[i][0].out_ok_) std::clog << "Enabled | "; - else std::clog << "Disabled | "; - if (table_[i][1].type_ == String) std::clog << "String | "; - if (table_[i][1].type_ == Int) std::clog << "Int | "; - if (table_[i][1].type_ == Float) std::clog << "Float | "; - std::clog << table_[i][1].str_ << " -> " << table_[i][col_num_].str_; + os << "R" + std::to_string(i) << " | "; + if (table_[i][0].out_ok_) os << "Enabled | "; + else os << "Disabled | "; + if (table_[i][1].type_ == String) os << "String | "; + if (table_[i][1].type_ == Int) os << "Int | "; + if (table_[i][1].type_ == Float) os << "Float | "; + os << table_[i][1].str_ << " -> " << table_[i][col_num_].str_; + } + os << std::endl; + } + os << "------------\n"; + } + return; +} + +void gctl::dsv_io::display(std::ostream &os) +{ + for (int i = 0; i <= row_num_; i++) + { + for (size_t j = 0; j <= col_num_; j++) + { + if (table_[i][j].out_ok_ && table_[i][j].str_!= "") + { + os << table_[i][j].str_; + for (size_t k = j + 1; k <= col_num_; k++) + { + if (table_[i][k].out_ok_) os << deli_sym_ << table_[i][k].str_; + } + os << std::endl; + break; } - std::clog << std::endl; } - std::clog << "------------\n"; } return; } diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 669f011..f60db1a 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -435,7 +435,14 @@ namespace gctl * * @param t 显示表格信息的类型 */ - void info(int t); + void info(int t, std::ostream &os = std::cout); + + /** + * @brief 显示表格(不会显示失效的行和列) + * + * @param os 输出流 + */ + void display(std::ostream &os = std::cout); /** * @brief 返回名称为name和R和C的行或列的索引 diff --git a/tool/dsviewer/dsviewer.cpp b/tool/dsviewer/dsviewer.cpp index 449e685..165ae32 100644 --- a/tool/dsviewer/dsviewer.cpp +++ b/tool/dsviewer/dsviewer.cpp @@ -242,6 +242,9 @@ void head(const std::vector &cmd_units) if (cmd_units.size() > 1) h = atoi(cmd_units[1].c_str()); if (h <= 0) throw std::runtime_error("head: invalid number."); + _1s_vector cnames = tc.column_names(); + display_vector(cnames, std::cout, tc.delimeter()); + _1s_array line; for (size_t i = 1; i <= h; i++) { @@ -258,6 +261,9 @@ void tail(const std::vector &cmd_units) if (cmd_units.size() > 1) h = atoi(cmd_units[1].c_str()); if (h <= 0) throw std::runtime_error("tail: invalid number."); + _1s_vector cnames = tc.column_names(); + display_vector(cnames, std::cout, tc.delimeter()); + _1s_array line; for (size_t i = tc.row_number() - h + 1; i <= tc.row_number(); i++) { @@ -521,6 +527,13 @@ void insert_data(const std::vector &cmd_units) return; } +void display_table(const std::vector &cmd_units) +{ + // view + tc.display(); + return; +} + int main(int argc, char *argv[]) { if (argc >= 2) diff --git a/tool/dsviewer/dsviewer.h b/tool/dsviewer/dsviewer.h index 41a564c..036ddfc 100644 --- a/tool/dsviewer/dsviewer.h +++ b/tool/dsviewer/dsviewer.h @@ -59,10 +59,12 @@ void math_func(const std::vector &cmd_units); void rand_data(const std::vector &cmd_units); void filt_data(const std::vector &cmd_units); void insert_data(const std::vector &cmd_units); +void display_table(const std::vector &cmd_units); -#define CMD_NUM 16 +#define CMD_NUM 17 const cmd_pair commands[CMD_NUM] = { {"quit", quit, "Quit the program."}, + {"view", display_table, "Display valid cells."}, {"info", info, "Show the table information."}, {"head", head, "Show head line(s) of the table."}, {"tail", tail, "Show tail line(s) of the table."},