From 676c665619c1cf07a1417389bf9be4664f6a5979 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Sat, 28 Dec 2024 13:47:25 +0800 Subject: [PATCH] tmp --- example/text_io_ex.cpp | 10 ++++---- lib/io/dsv_io.cpp | 34 +++++++++++++------------- lib/io/dsv_io.h | 28 ++++++++++++--------- tool/dsviewer/dsviewer.cpp | 50 ++++++++++++++++++++++++++++++++++++++ tool/dsviewer/dsviewer.h | 6 ++++- 5 files changed, 93 insertions(+), 35 deletions(-) diff --git a/example/text_io_ex.cpp b/example/text_io_ex.cpp index d564b94..8dbadda 100644 --- a/example/text_io_ex.cpp +++ b/example/text_io_ex.cpp @@ -32,7 +32,7 @@ using namespace gctl; int main(int argc, char const *argv[]) try { -/* + dsv_io tc; tc.set_delimeter('|'); tc.load_text("tmp/world_data", ".txt", BothHead); @@ -43,14 +43,14 @@ int main(int argc, char const *argv[]) try _1s_array name; tc.get_column("Name_s", name); - name.show(std::cout, ','); + name.show(std::cout, '|'); tc.get_row("AUS", name); name.show(std::cout, ','); tc.save_csv("out"); -*/ +/* geodsv_io tc; tc.load_text("tmp/topo", ".txt", ColumnHead); tc.cell(0, 0, std::string("x (m)")); @@ -66,7 +66,7 @@ int main(int argc, char const *argv[]) try display_vector(tc.get_annotoations(), std::clog, '\n'); display_vector(tc.get_head_records(), std::clog, '\n'); - tc.disable_column("elevation (m)"); + tc.column_output("elevation (m)", Disable); _1s_vector s = tc.get_tags(); s.push_back("Elev = 1000"); @@ -78,7 +78,7 @@ int main(int argc, char const *argv[]) try std::clog << std::setprecision(12) << tc.cell(1, 0) << "\n"; tc.info(); - +*/ return 0; } catch(std::exception &e) diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index 3b5f0f4..18466d1 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -293,17 +293,17 @@ void gctl::dsv_io::info() for (size_t i = 0; i < col_num_; i++) { - // skip disabled columns - if (!bool_table_[0][i]) continue; - if (thead_ == ColumnHead || thead_ == BothHead) { - std::clog << table_[0][i].str_ << ": " << table_[1][i].str_ << " -> " << table_[row_num_ - 1][i].str_ << "\n"; + std::clog << table_[0][i].str_ << ": " << table_[1][i].str_ << " -> " << table_[row_num_ - 1][i].str_; } else { - std::clog << "Col-" << std::to_string(i) << ": " << table_[0][i].str_ << " -> " << table_[row_num_ - 1][i].str_ << "\n"; + std::clog << "Col-" << std::to_string(i) << ": " << table_[0][i].str_ << " -> " << table_[row_num_ - 1][i].str_; } + + if (!bool_table_[0][i]) std::clog << " (No output)"; + std::clog << std::endl; } std::clog << "============\n"; return; @@ -323,8 +323,7 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row) if (table_[i][0].str_ == name) return i; } - throw gctl::runtime_error("[gctl::dsv_io] No row found by the input name: " + name); - return 0; + return -1; } else { @@ -338,12 +337,11 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row) if (table_[0][i].str_ == name) return i; } - throw gctl::runtime_error("[gctl::dsv_io] No column found by the input name: " + name); - return 0; + return -1; } } -void gctl::dsv_io::disable_column(int idx) +void gctl::dsv_io::column_output(int idx, switch_type_e s) { if (idx >= col_num_) { @@ -352,18 +350,19 @@ void gctl::dsv_io::disable_column(int idx) for (size_t i = 0; i < row_num_; i++) { - bool_table_[i][idx] = false; + if (s == Enable) bool_table_[i][idx] = true; + else bool_table_[i][idx] = false; } return; } -void gctl::dsv_io::disable_column(std::string name) +void gctl::dsv_io::column_output(std::string name, switch_type_e s) { - disable_column(name_index(name)); + column_output(name_index(name), s); return; } -void gctl::dsv_io::disable_row(int idx) +void gctl::dsv_io::row_output(int idx, switch_type_e s) { if (idx >= row_num_) { @@ -372,14 +371,15 @@ void gctl::dsv_io::disable_row(int idx) for (size_t i = 0; i < col_num_; i++) { - bool_table_[idx][i] = false; + if (s == Enable) bool_table_[idx][i] = true; + else bool_table_[idx][i] = false; } return; } -void gctl::dsv_io::disable_row(std::string name) +void gctl::dsv_io::row_output(std::string name, switch_type_e s) { - disable_row(name_index(name)); + row_output(name_index(name), s); return; } diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 2016c57..702b293 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -276,32 +276,36 @@ namespace gctl int name_index(std::string name, bool iter_row = false); /** - * @brief 屏蔽列 + * @brief 设置列输出。你仍然可以使用这些数据,它们只是不会被输出 * * @param idx 列索引 + * @param s 设置输出类型 */ - void disable_column(int idx); + void column_output(int idx, switch_type_e s = Disable); /** - * @brief 屏蔽列 + * @brief 设置列输出。你仍然可以使用这些数据,它们只是不会被输出 * * @param name 列名称 + * @param s 设置输出类型 */ - void disable_column(std::string name); + void column_output(std::string name, switch_type_e s = Disable); /** - * @brief 屏蔽行 + * @brief 设置行输出。你仍然可以使用这些数据,它们只是不会被输出 * * @param idx 行索引 + * @param s 设置输出类型 */ - void disable_row(int idx); + void row_output(int idx, switch_type_e s = Disable); /** - * @brief 屏蔽行 + * @brief 设置行输出。你仍然可以使用这些数据,它们只是不会被输出 * * @param name 行名称 + * @param s 设置输出类型 */ - void disable_row(std::string name); + void row_output(std::string name, switch_type_e s = Disable); /** * @brief 填充表格 @@ -460,7 +464,7 @@ namespace gctl template void dsv_io::fill_column(int idx, const array &data, int p) { - if (idx >= col_num_) + if (idx >= col_num_ || idx < 0) { throw std::runtime_error("[gctl::dsv_io] Invalid column index."); } @@ -485,7 +489,7 @@ namespace gctl template void dsv_io::fill_row(int idx, const array &data, int p) { - if (idx >= row_num_) + if (idx >= row_num_ || idx < 0) { throw std::runtime_error("[gctl::dsv_io] Invalid row index."); } @@ -510,7 +514,7 @@ namespace gctl template void dsv_io::get_column(int idx, array &data) { - if (idx >= col_num_) + if (idx >= col_num_ || idx < 0) { throw std::runtime_error("[gctl::dsv_io] Invalid column index."); } @@ -536,7 +540,7 @@ namespace gctl template void dsv_io::get_row(int idx, array &data) { - if (idx >= row_num_) + if (idx >= row_num_ || idx < 0) { throw std::runtime_error("[gctl::dsv_io] Invalid row index."); } diff --git a/tool/dsviewer/dsviewer.cpp b/tool/dsviewer/dsviewer.cpp index f171c27..1edfb7c 100644 --- a/tool/dsviewer/dsviewer.cpp +++ b/tool/dsviewer/dsviewer.cpp @@ -215,6 +215,56 @@ void info(const std::vector &cmd_units) return; } +void set_enable(const std::vector &cmd_units) +{ + // enable column|row ... + if (cmd_units.size() < 3) throw std::runtime_error("enable: insufficient parameters."); + + if (cmd_units[1] == "column") + { + for (size_t i = 2; i < cmd_units.size(); i++) + { + if (tc.name_index(cmd_units[i]) < 0) tc.column_output(atoi(cmd_units[i].c_str()), Enable); + else tc.column_output(cmd_units[i], Enable); + } + } + else if (cmd_units[1] == "row") + { + for (size_t i = 2; i < cmd_units.size(); i++) + { + if (tc.name_index(cmd_units[i], true) < 0) tc.row_output(atoi(cmd_units[i].c_str()), Enable); + else tc.row_output(cmd_units[i], Enable); + } + } + else throw std::runtime_error("enable: invalid parameters."); + return; +} + +void set_disable(const std::vector &cmd_units) +{ + // disable column|row ... + if (cmd_units.size() < 3) throw std::runtime_error("disable: insufficient parameters."); + + if (cmd_units[1] == "column") + { + for (size_t i = 2; i < cmd_units.size(); i++) + { + if (tc.name_index(cmd_units[i]) < 0) tc.column_output(atoi(cmd_units[i].c_str()), Disable); + else tc.column_output(cmd_units[i], Enable); + } + } + else if (cmd_units[1] == "row") + { + for (size_t i = 2; i < cmd_units.size(); i++) + { + if (tc.name_index(cmd_units[i], true) < 0) tc.row_output(atoi(cmd_units[i].c_str()), Disable); + else tc.row_output(cmd_units[i], Enable); + } + } + else throw std::runtime_error("disable: invalid parameters."); + return; +} + void load_file(const std::vector &cmd_units) { // load [nohead|hashead] [] [tag_sym] [att_sym] [head_num] diff --git a/tool/dsviewer/dsviewer.h b/tool/dsviewer/dsviewer.h index 495e933..751ed58 100644 --- a/tool/dsviewer/dsviewer.h +++ b/tool/dsviewer/dsviewer.h @@ -48,14 +48,18 @@ void info(const std::vector &cmd_units); void load_file(const std::vector &cmd_units); void save_file(const std::vector &cmd_units); void statistic(const std::vector &cmd_units); +void set_enable(const std::vector &cmd_units); +void set_disable(const std::vector &cmd_units); -#define CMD_NUM 6 +#define CMD_NUM 8 const cmd_pair commands[CMD_NUM] = { {"quit", quit, "Quit the program."}, {"info", info, "Show the table information."}, {"open", load_file, "Open a dsv/csv file."}, {"save", save_file, "Save the table to a file."}, {"stats", statistic, "Calculate statistics of the selected columns."}, + {"enable", set_enable, "Enable column/row outputs by name or index."}, + {"disable", set_disable, "Disable column/row outputs by name or index."}, {"null", nullptr, "null"} };