From 2e086c4b002b055d6599f775761e73b305c19ba0 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Tue, 31 Dec 2024 09:29:54 +0800 Subject: [PATCH] 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; }