From ff2732a9beb7d0051d709a3c77c678995f27d5aa Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Sat, 28 Dec 2024 09:42:02 +0800 Subject: [PATCH 01/14] tmp --- example/text_io_ex.cpp | 2 ++ lib/io/dsv_io.cpp | 71 ++++++++++++++++++++++++++++++++++++++++-- lib/io/dsv_io.h | 29 +++++++++++++++++ 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/example/text_io_ex.cpp b/example/text_io_ex.cpp index b2fbbb5..d564b94 100644 --- a/example/text_io_ex.cpp +++ b/example/text_io_ex.cpp @@ -66,6 +66,8 @@ 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)"); + _1s_vector s = tc.get_tags(); s.push_back("Elev = 1000"); tc.set_tags(s); diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index 4817d90..3b5f0f4 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -74,6 +74,7 @@ void gctl::dsv_io::clear() destroy_vector(annotates_); destroy_vector(tags_); destroy_vector(table_); + destroy_vector(bool_table_); return; } @@ -148,6 +149,16 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table } } + bool_table_.resize(row_num_); + for (size_t i = 0; i < row_num_; i++) + { + bool_table_[i].resize(col_num_); + for (size_t j = 0; j < col_num_; j++) + { + bool_table_[i][j] = true; + } + } + thead_ = t; file_ = filename + file_exten; destroy_vector(lines); @@ -181,12 +192,25 @@ void gctl::dsv_io::save_text(std::string filename, std::string file_exten) outfile << "# " << annotates_[i] << std::endl; } + bool line_st; for (int i = 0; i < row_num_; i++) { - outfile << table_[i][0].str_; + line_st = false; + + if (bool_table_[i][0]) + { + outfile << table_[i][0].str_; + line_st = true; + } + for (size_t j = 1; j < col_num_; j++) { - outfile << deli_sym_ << table_[i][j].str_; + if (line_st && bool_table_[i][j]) outfile << deli_sym_ << table_[i][j].str_; // line started + else if (bool_table_[i][j]) // line not started + { + outfile << table_[i][j].str_; + line_st = true; // start line + } } outfile << std::endl; } @@ -269,6 +293,9 @@ 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"; @@ -316,6 +343,46 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row) } } +void gctl::dsv_io::disable_column(int idx) +{ + if (idx >= col_num_) + { + throw std::runtime_error("[gctl::dsv_io] Invalid column index."); + } + + for (size_t i = 0; i < row_num_; i++) + { + bool_table_[i][idx] = false; + } + return; +} + +void gctl::dsv_io::disable_column(std::string name) +{ + disable_column(name_index(name)); + return; +} + +void gctl::dsv_io::disable_row(int idx) +{ + if (idx >= row_num_) + { + throw std::runtime_error("[gctl::dsv_io] Invalid row index."); + } + + for (size_t i = 0; i < col_num_; i++) + { + bool_table_[idx][i] = false; + } + return; +} + +void gctl::dsv_io::disable_row(std::string name) +{ + disable_row(name_index(name)); + return; +} + gctl::geodsv_io::geodsv_io(){} gctl::geodsv_io::~geodsv_io(){} diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 85692c5..2016c57 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -102,6 +102,7 @@ namespace gctl std::vector heads_, annotates_, tags_; // 内容表格 std::vector > table_; + std::vector > bool_table_; public: /** @@ -274,6 +275,34 @@ namespace gctl */ int name_index(std::string name, bool iter_row = false); + /** + * @brief 屏蔽列 + * + * @param idx 列索引 + */ + void disable_column(int idx); + + /** + * @brief 屏蔽列 + * + * @param name 列名称 + */ + void disable_column(std::string name); + + /** + * @brief 屏蔽行 + * + * @param idx 行索引 + */ + void disable_row(int idx); + + /** + * @brief 屏蔽行 + * + * @param name 行名称 + */ + void disable_row(std::string name); + /** * @brief 填充表格 * From 676c665619c1cf07a1417389bf9be4664f6a5979 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Sat, 28 Dec 2024 13:47:25 +0800 Subject: [PATCH 02/14] 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"} }; From bb6f97b6aa204ab309ac8f22c6266af25f63f758 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Sat, 28 Dec 2024 16:58:54 +0800 Subject: [PATCH 03/14] tmp --- lib/io/dsv_io.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index 18466d1..cbdb8b7 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -313,11 +313,6 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row) { if (iter_row) { - if (thead_ != RowHead && thead_ != BothHead) - { - throw std::runtime_error("[gctl::dsv_io] The table is initialized with no row names."); - } - for (size_t i = 0; i < row_num_; i++) { if (table_[i][0].str_ == name) return i; @@ -327,11 +322,6 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row) } else { - if (thead_ != ColumnHead && thead_ != BothHead) - { - throw std::runtime_error("[gctl::dsv_io] The table is initialized with no column names."); - } - for (size_t i = 0; i < col_num_; i++) { if (table_[0][i].str_ == name) return i; From 5c726cf83abb9331824f8bfcd172fed50a58e29f Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Sat, 28 Dec 2024 21:02:10 +0800 Subject: [PATCH 04/14] tmp --- example/text_io_ex.cpp | 6 +++--- lib/io/dsv_io.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/example/text_io_ex.cpp b/example/text_io_ex.cpp index 8dbadda..f0c0cfc 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); @@ -49,8 +49,8 @@ int main(int argc, char const *argv[]) try 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)")); @@ -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 cbdb8b7..8098fdb 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -299,7 +299,7 @@ void gctl::dsv_io::info() } else { - std::clog << "Col-" << std::to_string(i) << ": " << table_[0][i].str_ << " -> " << table_[row_num_ - 1][i].str_; + std::clog << table_[0][i].str_ << " -> " << table_[row_num_ - 1][i].str_; } if (!bool_table_[0][i]) std::clog << " (No output)"; From 33e37c6b3e2ae32fd12c64a57cdae2d1abfb0d6f Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Mon, 30 Dec 2024 22:09:39 +0800 Subject: [PATCH 05/14] tmp --- example/text_io_ex.cpp | 14 +-- lib/io/dsv_io.cpp | 258 ++++++++++++++++++++++++----------------- lib/io/dsv_io.h | 159 +++++++++++++------------ 3 files changed, 236 insertions(+), 195 deletions(-) diff --git a/example/text_io_ex.cpp b/example/text_io_ex.cpp index f0c0cfc..ec9e3ab 100644 --- a/example/text_io_ex.cpp +++ b/example/text_io_ex.cpp @@ -53,12 +53,12 @@ int main(int argc, char const *argv[]) try geodsv_io tc; tc.load_text("tmp/topo", ".txt", ColumnHead); - tc.cell(0, 0, std::string("x (m)")); - tc.cell(0, 1, std::string("y (m)")); - tc.cell(0, 2, std::string("elevation (m)")); + tc.cell(0, 1, std::string("x (m)")); + tc.cell(0, 2, std::string("y (m)")); + tc.cell(0, 3, std::string("elevation (m)")); array topo; - tc.get_column_point3dc(0, 1, 2, topo); + tc.get_column_point3dc(1, 2, 3, topo); std::clog << std::setprecision(11) << topo.front().z << "\n"; std::clog << topo.back().x << "," << topo.back().y << "," << topo.back().z << "\n"; @@ -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.column_output("elevation (m)", Disable); + tc.column_output("C3", Disable); _1s_vector s = tc.get_tags(); s.push_back("Elev = 1000"); @@ -74,8 +74,8 @@ int main(int argc, char const *argv[]) try tc.save_csv("out"); double c = 4.25242153654; - tc.cell(1, 0, c, 12); - std::clog << std::setprecision(12) << tc.cell(1, 0) << "\n"; + tc.cell(2, 1, c, 12); + std::clog << std::setprecision(12) << tc.cell(2, 1) << "\n"; tc.info(); diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index 8098fdb..d078254 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -36,7 +36,6 @@ gctl::dsv_io::dsv_io() head_num_ = 0; row_num_ = 0; col_num_ = 0; - thead_ = ColumnHead; } gctl::dsv_io::~dsv_io() @@ -53,7 +52,6 @@ gctl::dsv_io::dsv_io(std::string filename, std::string file_exten, table_headtyp head_num_ = 0; row_num_ = 0; col_num_ = 0; - thead_ = ColumnHead; if (file_exten == ".csv") load_csv(filename, t); else load_text(filename, file_exten, t); @@ -68,13 +66,11 @@ void gctl::dsv_io::clear() head_num_ = 0; row_num_ = 0; col_num_ = 0; - thead_ = ColumnHead; destroy_vector(heads_); destroy_vector(annotates_); destroy_vector(tags_); destroy_vector(table_); - destroy_vector(bool_table_); return; } @@ -83,6 +79,8 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table std::ifstream infile; open_infile(infile, filename, file_exten); + file_ = filename + file_exten; + int h = 0; std::string tmp_line; std::vector lines; @@ -115,20 +113,18 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table } infile.close(); - // 首先初始化行数 - row_num_ = lines.size(); - table_.resize(row_num_); + table_.resize(lines.size()); - int cn; + int cn, cn_max = 0; std::vector tmp_cols; - for (size_t i = 0; i < row_num_; i++) + for (size_t i = 0; i < lines.size(); i++) { tmp_cols.clear(); parse_string_to_vector(lines[i], deli_sym_, tmp_cols); // 动态调整列数 cn = tmp_cols.size(); - col_num_ = std::max(cn, col_num_); + cn_max = std::max(cn, cn_max); table_[i].resize(tmp_cols.size()); for (size_t j = 0; j < tmp_cols.size(); j++) @@ -139,28 +135,56 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table // 补齐可能的空格 table_cell empty_cell; - empty_cell.str_ = ""; - for (size_t i = 0; i < row_num_; i++) + for (size_t i = 0; i < table_.size(); i++) { cn = table_[i].size(); - for (size_t j = cn; j < col_num_; j++) + for (size_t j = cn; j < cn_max; j++) { table_[i].push_back(empty_cell); } } - bool_table_.resize(row_num_); - for (size_t i = 0; i < row_num_; i++) + std::vector empty_line; + if (t == NoHead) { - bool_table_[i].resize(col_num_); - for (size_t j = 0; j < col_num_; j++) + row_num_ = table_.size(); + col_num_ = table_[0].size(); + + table_.emplace(table_.begin(), empty_line); + table_[0].resize(col_num_); + + for (size_t i = 0; i < table_.size(); i++) { - bool_table_[i][j] = true; + table_[i].emplace(table_[i].begin(), empty_cell); } } - thead_ = t; - file_ = filename + file_exten; + if (t == ColumnHead) + { + row_num_ = table_.size() - 1; + col_num_ = table_[0].size(); + + for (size_t i = 0; i < table_.size(); i++) + { + table_[i].emplace(table_[i].begin(), empty_cell); + } + } + + if (t == RowHead) + { + row_num_ = table_.size(); + col_num_ = table_[0].size() - 1; + + table_.emplace(table_.begin(), empty_line); + table_[0].resize(col_num_ + 1); + } + + if (t == BothHead) + { + row_num_ = table_.size() - 1; + col_num_ = table_[0].size() - 1; + } + destroy_vector(lines); return; } @@ -192,21 +216,39 @@ void gctl::dsv_io::save_text(std::string filename, std::string file_exten) outfile << "# " << annotates_[i] << std::endl; } - bool line_st; - for (int i = 0; i < row_num_; i++) + bool line_st = false; + if (table_[0][0].out_ok_ && table_[0][0].str_ != "") + { + outfile << table_[0][0].str_; + line_st = true; + } + + for (int j = 1; j <= col_num_; j++) + { + if (line_st && table_[0][j].out_ok_ && table_[0][j].str_ != "") outfile << deli_sym_ << table_[0][j].str_; // line started + else if (table_[0][j].out_ok_ && table_[0][j].str_ != "") // line not started + { + outfile << table_[0][j].str_; + line_st = true; // start line + } + } + + if (line_st) outfile << std::endl; + + for (int i = 1; i <= row_num_; i++) { line_st = false; - if (bool_table_[i][0]) + if (table_[i][0].out_ok_ && table_[i][0].str_ != "") { outfile << table_[i][0].str_; line_st = true; } - for (size_t j = 1; j < col_num_; j++) + for (int j = 1; j <= col_num_; j++) { - if (line_st && bool_table_[i][j]) outfile << deli_sym_ << table_[i][j].str_; // line started - else if (bool_table_[i][j]) // line not started + if (line_st && table_[i][j].out_ok_) outfile << deli_sym_ << table_[i][j].str_; // line started + else if (table_[i][j].out_ok_) // line not started { outfile << table_[i][j].str_; line_st = true; // start line @@ -214,6 +256,7 @@ void gctl::dsv_io::save_text(std::string filename, std::string file_exten) } outfile << std::endl; } + outfile.close(); return; } @@ -229,37 +272,11 @@ void gctl::dsv_io::init_table(int row, int col, table_headtype_e t) { row_num_ = row; col_num_ = col; - thead_ = t; - table_.resize(row_num_); - for (size_t i = 0; i < row_num_; i++) + table_.resize(row_num_ + 1); + for (size_t i = 0; i < row_num_ + 1; i++) { - table_[i].resize(col_num_); - for (size_t j = 0; j < col_num_; j++) - { - table_[i][j].str_ = ""; - } - } - - if (t == RowHead || t == BothHead) - { - for (size_t i = 0; i < row_num_; i++) - { - table_[i][0].str_ = "row-" + std::to_string(i); - } - } - - if (t == ColumnHead || t == BothHead) - { - for (size_t i = 0; i < col_num_; i++) - { - table_[0][i].str_ = "col-" + std::to_string(i); - } - } - - if (t == BothHead) - { - table_[0][0].str_ = "row-idx"; + table_[i].resize(col_num_ + 1); } return; } @@ -283,6 +300,26 @@ void gctl::dsv_io::set_tags(const std::vector &tags) return; } +void gctl::dsv_io::set_row_names(const std::vector &names) +{ + for (size_t i = 1; i <= std::min(row_num_, (int) names.size()); i++) + { + table_[i][0].str_ = names[i]; + } + + table_[0][0].str_ = "row_name"; + return; +} + +void gctl::dsv_io::set_column_names(const std::vector &names) +{ + for (size_t i = 1; i <= std::min(col_num_, (int) names.size()); i++) + { + table_[0][i].str_ = names[i]; + } + return; +} + void gctl::dsv_io::info() { std::clog << "File: " << file_ << "\n------------\n"; @@ -291,18 +328,18 @@ void gctl::dsv_io::info() std::clog << "Tag(s): " << tags_.size() << "\n"; std::clog << "------------\nColumns:\n"; - for (size_t i = 0; i < col_num_; i++) + for (size_t i = 1; i <= col_num_; i++) { - if (thead_ == ColumnHead || thead_ == BothHead) + if (table_[0][i].str_ != "") { - std::clog << table_[0][i].str_ << ": " << table_[1][i].str_ << " -> " << table_[row_num_ - 1][i].str_; + std::clog << table_[0][i].str_ << ": " << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; } else { - std::clog << table_[0][i].str_ << " -> " << table_[row_num_ - 1][i].str_; + std::clog << "C" + std::to_string(i) << ": " << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; } - if (!bool_table_[0][i]) std::clog << " (No output)"; + if (!table_[0][i].out_ok_) std::clog << " (No output)"; std::clog << std::endl; } std::clog << "============\n"; @@ -311,9 +348,25 @@ void gctl::dsv_io::info() int gctl::dsv_io::name_index(std::string name, bool iter_row) { + std::smatch ret; + std::regex patr("R(\\d*)"), patc("C(\\d*)"); + if (regex_search(name, ret, patr)) + { + int r = atoi(std::string(ret[1]).c_str()); + if (r >= 1 && r <= row_num_) return r; + else return -1; + } + + if (regex_search(name, ret, patc)) + { + int c = atoi(std::string(ret[1]).c_str()); + if (c >= 1 && c <= col_num_) return c; + else return -1; + } + if (iter_row) { - for (size_t i = 0; i < row_num_; i++) + for (size_t i = 1; i <= row_num_; i++) { if (table_[i][0].str_ == name) return i; } @@ -322,7 +375,7 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row) } else { - for (size_t i = 0; i < col_num_; i++) + for (size_t i = 1; i <= col_num_; i++) { if (table_[0][i].str_ == name) return i; } @@ -333,15 +386,15 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row) void gctl::dsv_io::column_output(int idx, switch_type_e s) { - if (idx >= col_num_) + if (idx > col_num_ || idx <= 0) { throw std::runtime_error("[gctl::dsv_io] Invalid column index."); } - for (size_t i = 0; i < row_num_; i++) + for (size_t i = 0; i <= row_num_; i++) { - if (s == Enable) bool_table_[i][idx] = true; - else bool_table_[i][idx] = false; + if (s == Enable) table_[i][idx].out_ok_ = true; + else table_[i][idx].out_ok_ = false; } return; } @@ -354,15 +407,15 @@ void gctl::dsv_io::column_output(std::string name, switch_type_e s) void gctl::dsv_io::row_output(int idx, switch_type_e s) { - if (idx >= row_num_) + if (idx > row_num_ || idx <= 0) { throw std::runtime_error("[gctl::dsv_io] Invalid row index."); } - for (size_t i = 0; i < col_num_; i++) + for (size_t i = 0; i <= col_num_; i++) { - if (s == Enable) bool_table_[idx][i] = true; - else bool_table_[idx][i] = false; + if (s == Enable) table_[idx][i].out_ok_ = true; + else table_[idx][i].out_ok_ = false; } return; } @@ -386,7 +439,6 @@ gctl::geodsv_io::geodsv_io(std::string filename, std::string file_exten, table_h head_num_ = 0; row_num_ = 0; col_num_ = 0; - thead_ = ColumnHead; if (file_exten == ".csv") load_csv(filename, t); else load_text(filename, file_exten, t); @@ -394,27 +446,24 @@ gctl::geodsv_io::geodsv_io(std::string filename, std::string file_exten, table_h void gctl::geodsv_io::fill_column_point2dc(int xid, int yid, const array &data, int p) { - if (xid >= col_num_ || yid >= col_num_ || xid == yid) + if (xid > col_num_ || yid > col_num_ || xid == yid || xid <= 0 || yid <= 0) { throw std::runtime_error("[gctl::geodsv_io] Invalid column index."); } - int st = 0; - if (thead_ == ColumnHead || thead_ == BothHead) st = 1; - std::stringstream ss; std::string s; - for (size_t i = 0; i < std::min(row_num_ - st, (int) data.size()); i++) + for (size_t i = 1; i <= std::min(row_num_, (int) data.size()); i++) { ss.clear(); - ss << data[i].x; + ss << data[i - 1].x; ss >> s; - table_[i + st][xid].str_ = s; + table_[i][xid].str_ = s; ss.clear(); - ss << data[i].y; + ss << data[i - 1].y; ss >> s; - table_[i + st][yid].str_ = s; + table_[i][yid].str_ = s; } return; } @@ -427,32 +476,30 @@ void gctl::geodsv_io::fill_column_point2dc(std::string xname, std::string yname, void gctl::geodsv_io::fill_column_point3dc(int xid, int yid, int zid, const array &data, int p) { - if (xid >= col_num_ || yid >= col_num_ || zid >= col_num_ || xid == yid || yid == zid || xid == zid) + if (xid > col_num_ || yid > col_num_ || zid > col_num_ || xid == yid || yid == zid || xid == zid + || xid <= 0 || yid <= 0 || zid <= 0) { throw std::runtime_error("[gctl::geodsv_io] Invalid column index."); } - int st = 0; - if (thead_ == ColumnHead || thead_ == BothHead) st = 1; - std::stringstream ss; std::string s; - for (size_t i = 0; i < std::min(row_num_ - st, (int) data.size()); i++) + for (size_t i = 1; i < std::min(row_num_, (int) data.size()); i++) { ss.clear(); - ss << data[i].x; + ss << data[i - 1].x; ss >> s; - table_[i + st][xid].str_ = s; + table_[i][xid].str_ = s; ss.clear(); - ss << data[i].y; + ss << data[i - 1].y; ss >> s; - table_[i + st][yid].str_ = s; + table_[i][yid].str_ = s; ss.clear(); - ss << data[i].z; + ss << data[i - 1].z; ss >> s; - table_[i + st][zid].str_ = s; + table_[i][zid].str_ = s; } return; } @@ -465,19 +512,16 @@ void gctl::geodsv_io::fill_column_point3dc(std::string xname, std::string yname, void gctl::geodsv_io::get_column_point2dc(int xid, int yid, array &data) { - if (xid >= col_num_ || yid >= col_num_ || xid == yid) + if (xid > col_num_ || yid > col_num_ || xid == yid || xid <= 0 || yid <= 0) { throw std::runtime_error("[gctl::geodsv_io] Invalid column index."); } - int st = 0; - if (thead_ == ColumnHead || thead_ == BothHead) st = 1; - - data.resize(row_num_ - st); - for (size_t i = st; i < row_num_; i++) + data.resize(row_num_); + for (size_t i = 1; i <= row_num_; i++) { - data[i - st].x = table_[i][xid].value(); - data[i - st].y = table_[i][yid].value(); + data[i - 1].x = table_[i][xid].value(); + data[i - 1].y = table_[i][yid].value(); } return; } @@ -490,20 +534,18 @@ void gctl::geodsv_io::get_column_point2dc(std::string xname, std::string yname, void gctl::geodsv_io::get_column_point3dc(int xid, int yid, int zid, array &data) { - if (xid >= col_num_ || yid >= col_num_ || zid >= col_num_ || xid == yid || yid == zid || xid == zid) + if (xid > col_num_ || yid > col_num_ || zid > col_num_ || xid == yid || yid == zid || xid == zid + || xid <= 0 || yid <= 0 || zid <= 0) { throw std::runtime_error("[gctl::geodsv_io] Invalid column index."); } - int st = 0; - if (thead_ == ColumnHead || thead_ == BothHead) st = 1; - - data.resize(row_num_ - st); - for (size_t i = st; i < row_num_; i++) + data.resize(row_num_); + for (size_t i = 1; i <= row_num_; i++) { - data[i - st].x = table_[i][xid].value(); - data[i - st].y = table_[i][yid].value(); - data[i - st].z = table_[i][zid].value(); + data[i - 1].x = table_[i][xid].value(); + data[i - 1].y = table_[i][yid].value(); + data[i - 1].z = table_[i][zid].value(); } return; } diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 702b293..323d023 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -31,12 +31,20 @@ #include "../core.h" #include "../utility.h" #include "../geometry.h" +#include "regex.h" namespace gctl { struct table_cell { - std::string str_; + std::string str_; // 单元格的内容 统一保存为字符串 + bool out_ok_; // 是否可输出到文件 + + table_cell() + { + str_ = ""; + out_ok_ = true; + } template T value() { @@ -86,23 +94,22 @@ namespace gctl * 2. 以'#!'开始的行均为标记行,标识符可由用户指定; * 3. 文本开始可以包含n行头信息; * 4. 数据体为一个row*col大小的表格; - * 5. 数据体可以包含一列行名称与列名称。 - * + * 5. 数据表格文件可以额外包含一列行名称与列名称; + * 6. 如论文件内是否包含行头或列头,均可使用内置的行名称R或列名称C指定相应的行或列 */ class dsv_io { protected: + // 文件名 std::string file_; - table_headtype_e thead_; - // 头信息行数 表格行数 表格列数 + // 头信息行数 表格行数(不包括表头) 表格列数(不包括表头) int head_num_, row_num_, col_num_; // 注释行起始符 标记行起始符 分割符 char att_sym_, tag_sym_, deli_sym_; // 头信息行 注释行 标记行 std::vector heads_, annotates_, tags_; - // 内容表格 + // 内容表格(大小为row_num_+1 * col_num_+1) std::vector > table_; - std::vector > bool_table_; public: /** @@ -122,6 +129,7 @@ namespace gctl * * @param filename 文件名 * @param file_exten 文件扩展名 + * @param t 表格是否有行和列名称 */ dsv_io(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead); @@ -161,14 +169,14 @@ namespace gctl int head_number(){return head_num_;} /** - * @brief 返回行数 + * @brief 返回数据行数 * * @return 行数 */ int row_number(){return row_num_;} /** - * @brief 返回列数 + * @brief 返回数据列数 * * @return 列数 */ @@ -216,6 +224,20 @@ namespace gctl */ void set_tags(const std::vector &tags); + /** + * @brief 设置行名称 + * + * @param names 名称 + */ + void set_row_names(const std::vector &names); + + /** + * @brief 设置列名称 + * + * @param names 名称 + */ + void set_column_names(const std::vector &names); + /** * @brief 清理字符串向量对象 * @@ -227,6 +249,7 @@ namespace gctl * * @param filename 文件名 * @param file_exten 文件扩展名 + * @param t 表格是否有行和列名称 */ void load_text(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead); @@ -255,8 +278,8 @@ namespace gctl /** * @brief 初始化表格 * - * @param row 行数 - * @param col 列数 + * @param row 数据行数 + * @param col 数据列数 */ void init_table(int row, int col, table_headtype_e t = ColumnHead); @@ -269,16 +292,16 @@ namespace gctl /** * @brief 返回名称为name的行或列的索引 * - * @param name 名称 + * @param name 名称 可以是具体的名称,或者R和C这种内置名称 * @param iter_row 搜索行名称(默认为搜索列名称) - * @return 索引 + * @return 索引 返回的索引从1开始 */ int name_index(std::string name, bool iter_row = false); /** * @brief 设置列输出。你仍然可以使用这些数据,它们只是不会被输出 * - * @param idx 列索引 + * @param idx 列索引 从1开始 * @param s 设置输出类型 */ void column_output(int idx, switch_type_e s = Disable); @@ -294,7 +317,7 @@ namespace gctl /** * @brief 设置行输出。你仍然可以使用这些数据,它们只是不会被输出 * - * @param idx 行索引 + * @param idx 行索引 从1开始 * @param s 设置输出类型 */ void row_output(int idx, switch_type_e s = Disable); @@ -311,7 +334,7 @@ namespace gctl * @brief 填充表格 * * @tparam T 数据类型 - * @param data 矩阵数据 + * @param data 矩阵数据 大小与表格一致 * @param p 浮点类数据保存时的有效数字位数 */ template void fill_table(const matrix &data, int p = 6); @@ -328,7 +351,7 @@ namespace gctl * @brief 填充列 * * @tparam T 数据类型 - * @param idx 列索引 + * @param idx 列索引 从1开始 * @param data 列数据 * @param p 浮点类数据保存时的有效数字位数 */ @@ -348,7 +371,7 @@ namespace gctl * @brief 填充行 * * @tparam T 数据类型 - * @param idx 行索引 + * @param idx 行索引 从1开始 * @param data 行数据 * @param p 浮点类数据保存时的有效数字位数 */ @@ -368,7 +391,7 @@ namespace gctl * @brief 获取列数据 * * @tparam T 数据类型 - * @param idx 列索引 + * @param idx 列索引 从1开始 * @param data 列数据 */ template void get_column(int idx, array &data); @@ -386,7 +409,7 @@ namespace gctl * @brief 获取行数据 * * @tparam T 数据类型 - * @param idx 行索引 + * @param idx 行索引 从1开始 * @param data 行数据 */ template void get_row(int idx, array &data); @@ -404,8 +427,8 @@ namespace gctl * @brief 获取表格单元数据 * * @tparam T 数据类型 - * @param r 行号 - * @param c 列号 + * @param r 行号 从0开始(可以操作行或列名称) + * @param c 列号 从0开始(可以操作行或列名称) * @return T 单元数据 */ template T cell(int r, int c){return table_[r][c].value();} @@ -414,8 +437,8 @@ namespace gctl * @brief 填充表格单元数据 * * @tparam T 数据类型 - * @param r 行号 - * @param c 列号 + * @param r 行号 从0开始(可以操作行或列名称) + * @param c 列号 从0开始(可以操作行或列名称) * @param d 数据 * @param p 浮点类数据保存时的有效数字位数 */ @@ -425,17 +448,11 @@ namespace gctl template void dsv_io::fill_table(const matrix &data, int p) { - int st = 0; - if (thead_ == ColumnHead || thead_ == BothHead) st = 1; - - int ft = 0; - if (thead_ == RowHead || thead_ == BothHead) ft = 1; - - for (size_t i = 0; i < std::min(row_num_ - st, (int) data.row_size()); i++) + for (size_t i = 1; i <= std::min(row_num_, (int) data.row_size()); i++) { - for (size_t j = 0; j < std::min(col_num_ - ft, (int) data.col_size()); j++) + for (size_t j = 1; j <= std::min(col_num_, (int) data.col_size()); j++) { - table_[i + st][j + ft].value(data[i][j], p); + table_[i][j].value(data[i - 1][j - 1], p); } } return; @@ -444,18 +461,12 @@ namespace gctl template void dsv_io::get_table(matrix &data) { - int st = 0; - if (thead_ == ColumnHead || thead_ == BothHead) st = 1; - - int ft = 0; - if (thead_ == RowHead || thead_ == BothHead) ft = 1; - - data.resize(row_num_ - st, col_num_ - ft); - for (size_t i = st; i < row_num_; i++) + data.resize(row_num_, col_num_); + for (size_t i = 1; i <= row_num_; i++) { - for (size_t j = ft; j < col_num_; j++) + for (size_t j = 1; j <= col_num_; j++) { - data[i - st][j - ft] = table_[i][j].value(); + data[i -1][j - 1] = table_[i][j].value(); } } return; @@ -464,17 +475,14 @@ namespace gctl template void dsv_io::fill_column(int idx, const array &data, int p) { - if (idx >= col_num_ || idx < 0) + if (idx > col_num_ || idx <= 0) { throw std::runtime_error("[gctl::dsv_io] Invalid column index."); } - - int st = 0; - if (thead_ == ColumnHead || thead_ == BothHead) st = 1; - - for (size_t i = 0; i < std::min(row_num_ - st, (int) data.size()); i++) + + for (size_t i = 1; i <= std::min(row_num_, (int) data.size()); i++) { - table_[i + st][idx].value(data[i], p); + table_[i][idx].value(data[i - 1], p); } return; } @@ -489,17 +497,14 @@ namespace gctl template void dsv_io::fill_row(int idx, const array &data, int p) { - if (idx >= row_num_ || idx < 0) + if (idx > row_num_ || idx <= 0) { throw std::runtime_error("[gctl::dsv_io] Invalid row index."); } - int st = 0; - if (thead_ == RowHead || thead_ == BothHead) st = 1; - - for (size_t i = 0; i < std::min(col_num_ - st, (int) data.size()); i++) + for (size_t i = 1; i <= std::min(col_num_, (int) data.size()); i++) { - table_[idx][i + st].value(data[i], p); + table_[idx][i].value(data[i - 1], p); } return; } @@ -514,18 +519,15 @@ namespace gctl template void dsv_io::get_column(int idx, array &data) { - if (idx >= col_num_ || idx < 0) + if (idx > col_num_ || idx <= 0) { throw std::runtime_error("[gctl::dsv_io] Invalid column index."); } - int st = 0; - if (thead_ == ColumnHead || thead_ == BothHead) st = 1; - - data.resize(row_num_ - st); - for (size_t i = st; i < row_num_; i++) + data.resize(row_num_); + for (size_t i = 1; i <= row_num_; i++) { - data[i - st] = table_[i][idx].value(); + data[i - 1] = table_[i][idx].value(); } return; } @@ -540,18 +542,15 @@ namespace gctl template void dsv_io::get_row(int idx, array &data) { - if (idx >= row_num_ || idx < 0) + if (idx > row_num_ || idx <= 0) { throw std::runtime_error("[gctl::dsv_io] Invalid row index."); } - - int st = 0; - if (thead_ == RowHead || thead_ == BothHead) st = 1; - data.resize(col_num_ - st); - for (size_t i = st; i < col_num_; i++) + data.resize(col_num_); + for (size_t i = 1; i <= col_num_; i++) { - data[i - st] = table_[idx][i].value(); + data[i - 1] = table_[idx][i].value(); } return; } @@ -593,8 +592,8 @@ namespace gctl /** * @brief 填充二维坐标列 * - * @param xid x坐标列索引 - * @param yid y坐标列索引 + * @param xid x坐标列索引 从1开始 + * @param yid y坐标列索引 从1开始 * @param data 返回的二维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ @@ -613,9 +612,9 @@ namespace gctl /** * @brief 填充三维坐标列 * - * @param xid x坐标列索引 - * @param yid y坐标列索引 - * @param zid z坐标列索引 + * @param xid x坐标列索引 从1开始 + * @param yid y坐标列索引 从1开始 + * @param zid z坐标列索引 从1开始 * @param data 返回的三维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ @@ -635,8 +634,8 @@ namespace gctl /** * @brief 读取二维坐标列 * - * @param xid x坐标列索引 - * @param yid y坐标列索引 + * @param xid x坐标列索引 从1开始 + * @param yid y坐标列索引 从1开始 * @param data 返回的二维坐标数据 */ void get_column_point2dc(int xid, int yid, array &data); @@ -653,9 +652,9 @@ namespace gctl /** * @brief 读取三维坐标列 * - * @param xid x坐标列索引 - * @param yid y坐标列索引 - * @param zid z坐标列索引 + * @param xid x坐标列索引 从1开始 + * @param yid y坐标列索引 从1开始 + * @param zid z坐标列索引 从1开始 * @param data 返回的三维坐标数据 */ void get_column_point3dc(int xid, int yid, int zid, array &data); From 2e086c4b002b055d6599f775761e73b305c19ba0 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Tue, 31 Dec 2024 09:29:54 +0800 Subject: [PATCH 06/14] tmp --- example/text_io_ex.cpp | 18 ++--- lib/io/dsv_io.cpp | 144 ++++++++++++++++++++++--------------- lib/io/dsv_io.h | 95 +++++++++++++----------- tool/dsviewer/dsviewer.cpp | 8 ++- 4 files changed, 156 insertions(+), 109 deletions(-) diff --git a/example/text_io_ex.cpp b/example/text_io_ex.cpp index ec9e3ab..6169cf9 100644 --- a/example/text_io_ex.cpp +++ b/example/text_io_ex.cpp @@ -32,25 +32,25 @@ 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); - tc.info(); + tc.info(BothHead); //_1s_vector name = tc.get_row_names(); //display_vector(name); - _1s_array name; - tc.get_column("Name_s", name); - name.show(std::cout, '|'); + //_1s_array name; + //tc.get_column("Name_s", name); + //name.show(std::cout, '|'); - tc.get_row("AUS", name); - 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, 1, std::string("x (m)")); @@ -78,7 +78,7 @@ int main(int argc, char const *argv[]) try std::clog << std::setprecision(12) << tc.cell(2, 1) << "\n"; tc.info(); - +*/ return 0; } catch(std::exception &e) diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index d078254..44bae89 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -74,6 +74,46 @@ void gctl::dsv_io::clear() return; } +void gctl::dsv_io::get_row_names(std::vector &names) +{ + names.resize(row_num_); + for (size_t i = 1; i < row_num_; i++) + { + names[i] = table_[i][0].str_; + } + return; +} + +void gctl::dsv_io::get_column_names(std::vector &names) +{ + names.resize(col_num_); + for (size_t i = 1; i < col_num_; i++) + { + names[i] = table_[0][i].str_; + } + return; +} + +void gctl::dsv_io::set_row_names(const std::vector &names) +{ + for (size_t i = 1; i <= std::min(row_num_, (int) names.size()); i++) + { + table_[i][0].str_ = names[i]; + } + + table_[0][0].str_ = "row_name"; + return; +} + +void gctl::dsv_io::set_column_names(const std::vector &names) +{ + for (size_t i = 1; i <= std::min(col_num_, (int) names.size()); i++) + { + table_[0][i].str_ = names[i]; + } + return; +} + void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table_headtype_e t) { std::ifstream infile; @@ -145,7 +185,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table } std::vector empty_line; - if (t == NoHead) + if (t == NoHead) // 无表头 需要补齐空白的列头和行头 { row_num_ = table_.size(); col_num_ = table_[0].size(); @@ -159,7 +199,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table } } - if (t == ColumnHead) + if (t == ColumnHead) // 有列头 需要补齐空白的行头 { row_num_ = table_.size() - 1; col_num_ = table_[0].size(); @@ -170,7 +210,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table } } - if (t == RowHead) + if (t == RowHead) // 有行头 需要补齐空白的列头 { row_num_ = table_.size(); col_num_ = table_[0].size() - 1; @@ -179,7 +219,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table table_[0].resize(col_num_ + 1); } - if (t == BothHead) + if (t == BothHead) // 有行头和列头 { row_num_ = table_.size() - 1; col_num_ = table_[0].size() - 1; @@ -216,6 +256,7 @@ void gctl::dsv_io::save_text(std::string filename, std::string file_exten) outfile << "# " << annotates_[i] << std::endl; } + // 单独处理第一行 即列头 bool line_st = false; if (table_[0][0].out_ok_ && table_[0][0].str_ != "") { @@ -235,10 +276,12 @@ void gctl::dsv_io::save_text(std::string filename, std::string file_exten) if (line_st) outfile << std::endl; + // 处理余下的行 for (int i = 1; i <= row_num_; i++) { line_st = false; + // 单独处理第一列 即行头 if (table_[i][0].out_ok_ && table_[i][0].str_ != "") { outfile << table_[i][0].str_; @@ -272,7 +315,8 @@ void gctl::dsv_io::init_table(int row, int col, table_headtype_e t) { row_num_ = row; col_num_ = col; - + + // 初始的列头和行头均为空白 table_.resize(row_num_ + 1); for (size_t i = 0; i < row_num_ + 1; i++) { @@ -281,73 +325,59 @@ void gctl::dsv_io::init_table(int row, int col, table_headtype_e t) return; } -void gctl::dsv_io::set_head_records(const std::vector &heads) -{ - heads_ = heads; - head_num_ = heads_.size(); - return; -} - -void gctl::dsv_io::set_annotoations(const std::vector &att) -{ - annotates_ = att; - return; -} - -void gctl::dsv_io::set_tags(const std::vector &tags) -{ - tags_ = tags; - return; -} - -void gctl::dsv_io::set_row_names(const std::vector &names) -{ - for (size_t i = 1; i <= std::min(row_num_, (int) names.size()); i++) - { - table_[i][0].str_ = names[i]; - } - - table_[0][0].str_ = "row_name"; - return; -} - -void gctl::dsv_io::set_column_names(const std::vector &names) -{ - for (size_t i = 1; i <= std::min(col_num_, (int) names.size()); i++) - { - table_[0][i].str_ = names[i]; - } - return; -} - -void gctl::dsv_io::info() +void gctl::dsv_io::info(table_headtype_e t) { std::clog << "File: " << file_ << "\n------------\n"; std::clog << "Head(s): " << head_num_ << "\n"; std::clog << "Annotation(s): " << annotates_.size() << "\n"; std::clog << "Tag(s): " << tags_.size() << "\n"; - std::clog << "------------\nColumns:\n"; + std::clog << "------------\n"; - for (size_t i = 1; i <= col_num_; i++) + if (t == ColumnHead || t == BothHead) { - if (table_[0][i].str_ != "") + std::clog << "Columns:\n"; + for (size_t i = 1; i <= col_num_; i++) { - std::clog << table_[0][i].str_ << ": " << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; - } - else - { - std::clog << "C" + std::to_string(i) << ": " << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; - } + if (table_[0][i].str_ != "") + { + std::clog << table_[0][i].str_ << ": " << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; + } + else + { + std::clog << "C" + std::to_string(i) << ": " << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; + } - if (!table_[0][i].out_ok_) std::clog << " (No output)"; - std::clog << std::endl; + if (!table_[0][i].out_ok_) std::clog << " (No output)"; + std::clog << std::endl; + } + std::clog << "============\n"; + } + + if (t == RowHead || t == BothHead) + { + std::clog << "Rows:\n"; + for (size_t i = 1; i <= row_num_; i++) + { + if (table_[i][0].str_ != "") + { + std::clog << table_[i][0].str_ << ": " << table_[i][1].str_ << " -> " << table_[i][col_num_].str_; + } + else + { + std::clog << "R" + std::to_string(i) << ": " << table_[i][1].str_ << " -> " << table_[i][col_num_].str_; + } + + if (!table_[i][0].out_ok_) std::clog << " (No output)"; + std::clog << std::endl; + } + std::clog << "============\n"; } - std::clog << "============\n"; return; } int gctl::dsv_io::name_index(std::string name, bool iter_row) { + // 拾取行号或列号 格式为R和C std::smatch ret; std::regex patr("R(\\d*)"), patc("C(\\d*)"); if (regex_search(name, ret, patr)) diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 323d023..940c26c 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -134,32 +134,10 @@ namespace gctl dsv_io(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead); /** - * @brief 设置列分隔符 - * - * @param deli_sym 分隔符 - */ - void set_delimeter(char deli_sym){deli_sym_ = deli_sym;} - - /** - * @brief 设置头信息行数 - * - * @param num 行数 - */ - void set_head_number(char num){head_num_ = num;} - - /** - * @brief 设置注释行符号 - * - * @param att_sym 注释符号 - */ - void set_annotation_symbol(char att_sym){att_sym_ = att_sym;} - - /** - * @brief 设置标记行符号 - * - * @param tag_sym 标记符号 - */ - void set_tag_symbol(char tag_sym){tag_sym_ = tag_sym;} + * @brief 清理字符串向量对象 + * + */ + void clear(); /** * @brief 返回头信息行数 @@ -203,26 +181,68 @@ namespace gctl */ const std::vector& get_tags(){return tags_;} + /** + * @brief 获取行名称 + * + * @param names 名称 + */ + void get_row_names(std::vector &names); + + /** + * @brief 获取列名称 + * + * @param names 名称 + */ + void get_column_names(std::vector &names); + + /** + * @brief 设置列分隔符 + * + * @param deli_sym 分隔符 + */ + void set_delimeter(char deli_sym){deli_sym_ = deli_sym;} + + /** + * @brief 设置头信息行数 + * + * @param num 行数 + */ + void set_head_number(char num){head_num_ = num;} + + /** + * @brief 设置注释行符号 + * + * @param att_sym 注释符号 + */ + void set_annotation_symbol(char att_sym){att_sym_ = att_sym;} + + /** + * @brief 设置标记行符号 + * + * @param tag_sym 标记符号 + */ + void set_tag_symbol(char tag_sym){tag_sym_ = tag_sym;} + /** * @brief 设置头信息 * * @param heads 头信息 */ - void set_head_records(const std::vector &heads); + void set_head_records(const std::vector &heads){heads_ = heads; head_num_ = heads_.size();} /** * @brief 设置注释 * * @param att 注释 */ - void set_annotoations(const std::vector &att); + void set_annotoations(const std::vector &att){annotates_ = att;} /** * @brief 设置标记 * * @param tags 标记 */ - void set_tags(const std::vector &tags); + void set_tags(const std::vector &tags){tags_ = tags;} /** * @brief 设置行名称 @@ -238,12 +258,6 @@ namespace gctl */ void set_column_names(const std::vector &names); - /** - * @brief 清理字符串向量对象 - * - */ - void clear(); - /** * @brief 读入文本文件 * @@ -286,15 +300,16 @@ namespace gctl /** * @brief 返回表格信息 * + * @param t 显示表格信息的类型 */ - void info(); + void info(table_headtype_e t = ColumnHead); /** - * @brief 返回名称为name的行或列的索引 + * @brief 返回名称为name和R和C的行或列的索引 * - * @param name 名称 可以是具体的名称,或者R和C这种内置名称 - * @param iter_row 搜索行名称(默认为搜索列名称) - * @return 索引 返回的索引从1开始 + * @param name 名称 可以是具体的名称(如有),或者是R和C + * @param iter_row 搜索行名称(默认为搜索列名称),如果name参数为R和C则此参数无效 + * @return 索引 返回的索引(大于等于1 小于等于行数或列数)失败则返回-1 */ int name_index(std::string name, bool iter_row = false); diff --git a/tool/dsviewer/dsviewer.cpp b/tool/dsviewer/dsviewer.cpp index 1edfb7c..77df11d 100644 --- a/tool/dsviewer/dsviewer.cpp +++ b/tool/dsviewer/dsviewer.cpp @@ -207,11 +207,13 @@ void quit(const std::vector &cmd_units) void info(const std::vector &cmd_units) { - if (cmd_units.size() == 1) // cmd_units[0] == info + // info [column|row|both] + if (cmd_units.size() > 1) { - tc.info(); - return; + if (cmd_units[1] == "row") tc.info(RowHead); + if (cmd_units[1] == "both") tc.info(BothHead); } + else tc.info(); return; } From f2345de489de9ac47bc18c524b58e28e288d6809 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Tue, 31 Dec 2024 09:36:25 +0800 Subject: [PATCH 07/14] edit ignore list --- .gitignore | 3 ++- installer | 54 ------------------------------------------------------ 2 files changed, 2 insertions(+), 55 deletions(-) delete mode 100755 installer diff --git a/.gitignore b/.gitignore index b046f86..7064089 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ build/ .vscode/ tmp/ doc/html -doc/man \ No newline at end of file +doc/man +installer \ No newline at end of file diff --git a/installer b/installer deleted file mode 100755 index fd9d6e5..0000000 --- a/installer +++ /dev/null @@ -1,54 +0,0 @@ -#!/bin/bash - -if [[ $# == 0 || ${1} == "help" ]]; then - echo "Compiles executables/libraries and maintains installed files. Two tools 'Cmake' and 'stow' are empolyed here. For more information, see https://cmake.org and https://www.gnu.org/software/stow/." - echo "" - echo "School of Earth Sciences, Zhejiang University" - echo "Yi Zhang (yizhang-geo@zju.edu.cn)" - echo "" - echo "Usage: ./config.sh [option] [Cmake options]" - echo "" - echo "Options:" - echo "(1) configure: Configure Cmake project(s). This option could take extra Cmake options as in