diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 71fe3e9..6b39b6d 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -29,4 +29,4 @@ add_example(multinary_ex OFF) add_example(text_io_ex OFF) add_example(getoption_ex OFF) add_example(process_ex OFF) -add_example(array_ex ON) \ No newline at end of file +add_example(array_ex OFF) \ No newline at end of file diff --git a/example/text_io_ex.cpp b/example/text_io_ex.cpp index f864ca0..124dd56 100644 --- a/example/text_io_ex.cpp +++ b/example/text_io_ex.cpp @@ -44,6 +44,13 @@ int main(int argc, char const *argv[]) try tc.filt_column("America", "Continent_s", {"Name_s", "Population_n", "GNP_n"}, tout); //tc.match_column("America", "Continent_s", {}, tout); + //tout.add_column("GNP_n2", "Population_n"); + //array GNP_n2(tout.row_number(), 1000.0); + //tout.fill_column(GNP_n2, "GNP_n2"); + + int lr_id = tout.add_row(); + tout.fill_row(array{"Asia", "China", "14000000", "1949"}, lr_id); + tout.set_delimeter('|'); tout.save_text("out"); diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index cc5a13a..dcb20ba 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -293,6 +293,29 @@ void gctl::dsv_io::save_text(std::string filename, std::string file_exten) outfile << "# " << annotates_[i] << std::endl; } + // 探测是否有行头 + bool col_st = 1; + for (int i = 0; i <= row_num_; i++) + { + if (table_[i][0].out_ok_ && table_[i][0].str_ != "") + { + col_st = 0; + break; + } + } + + for (int i = 0; i <= row_num_; i++) + { + // 单独处理第一列 即行头 + outfile << table_[i][col_st].str_; + for (int j = col_st + 1; j <= col_num_; j++) + { + if (table_[i][j].out_ok_) outfile << deli_sym_ << table_[i][j].str_; + } + outfile << std::endl; + } + +/* // 单独处理第一行 即列头 bool line_st = false; if (table_[0][0].out_ok_ && table_[0][0].str_ != "") @@ -336,7 +359,7 @@ void gctl::dsv_io::save_text(std::string filename, std::string file_exten) } outfile << std::endl; } - +*/ outfile.close(); return; } @@ -508,14 +531,16 @@ void gctl::dsv_io::row_output(int idx, switch_type_e s) void gctl::dsv_io::row_output(std::string name, switch_type_e s) { - row_output(name_index(name), s); + row_output(name_index(name, true), s); return; } -void gctl::dsv_io::add_column(int idx, std::string name) +int gctl::dsv_io::add_column(std::string name, int idx) { + if (idx <= 0) throw std::runtime_error("[gctl::dsv_io] Invalid column index."); + table_cell empty_cell; - if (idx <= 0) + if (idx > col_num_) { for (size_t i = 0; i < table_.size(); i++) { @@ -524,6 +549,7 @@ void gctl::dsv_io::add_column(int idx, std::string name) table_[0].back().str_ = name; col_num_++; + return col_num_; } else { @@ -534,25 +560,28 @@ void gctl::dsv_io::add_column(int idx, std::string name) table_[0][idx].str_ = name; col_num_++; + return idx; } - return; + return -1; } -void gctl::dsv_io::add_column(std::string id_name, std::string name) +int gctl::dsv_io::add_column(std::string name, std::string id_name) { - add_column(name_index(id_name), name); - return; + return add_column(name, name_index(id_name)); } -void gctl::dsv_io::add_row(int idx, std::string name) +int gctl::dsv_io::add_row(std::string name, int idx) { + if (idx <= 0) throw std::runtime_error("[gctl::dsv_io] Invalid row index."); + std::vector empty_line; - if (idx <= 0) + if (idx > row_num_) { table_.push_back(empty_line); table_.back().resize(col_num_ + 1); table_.back().front().str_ = name; row_num_++; + return row_num_; } else { @@ -560,14 +589,14 @@ void gctl::dsv_io::add_row(int idx, std::string name) table_[idx].resize(col_num_ + 1); table_[idx].front().str_ = name; row_num_++; + return idx; } - return; + return -1; } -void gctl::dsv_io::add_row(std::string id_name, std::string name) +int gctl::dsv_io::add_row(std::string name, std::string id_name) { - add_row(name_index(id_name), name); - return; + return add_row(name, name_index(id_name, true)); } void gctl::dsv_io::filt_column(std::string cnd_str, std::string cnd_col, diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 7fafe8a..ed9536e 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -404,40 +404,50 @@ namespace gctl void row_output(std::string name, switch_type_e s = Disable); /** - * @brief 在索引为idx的列之后插入一个空白列 + * @brief 在索引为idx的位置插入一个空白列,剩余列后移一位。如果idx大于列数则在表尾添加一列。 * - * @param idx 列索引 小于等于0时将在表尾添加一列 + * @param idx 列索引 * @param name 设置列名称 + * + * @return 索引 返回的索引(大于等于1 小于等于行数或列数)失败则返回-1 */ - void add_column(int idx, std::string name = ""); + int add_column(std::string name = "", int idx = 9999); /** - * @brief 在名称为id_name的列之后插入一个空白列 + * @brief 在名称为id_name的列的位置插入一个空白列,剩余列后移一位。 * * @param id_name 索引列名称 * @param name 设置列名称 - */ - void add_column(std::string id_name, std::string name = ""); - - /** - * @brief 在索引为idx的列插入一个空白行 * - * @param idx 行索引 小于等于0时将在表尾添加一行 - * @param name 设置行名称 + * @return 索引 返回的索引(大于等于1 小于等于行数或列数)失败则返回-1 */ - void add_row(int idx, std::string name = ""); + int add_column(std::string name, std::string id_name); /** - * @brief 在名称为id_name的列插入一个空白行 + * @brief 在索引为idx的位置插入一个空白行,剩余行后移一位。如果idx大于等于行数则在表尾添加一行。 + * + * @param idx 行索引 + * @param name 设置行名称 + * + * @return 索引 返回的索引(大于等于1 小于等于行数或列数)失败则返回-1 + */ + int add_row(std::string name = "", int idx = 9999); + + /** + * @brief 在名称为id_name的行的位置插入一个空白行,剩余行后移一位。 * * @param id_name 索引行名称 * @param name 设置行名称 + * + * @return 索引 返回的索引(大于等于1 小于等于行数或列数)失败则返回-1 */ - void add_row(std::string id_name, std::string name = ""); + int add_row(std::string name, std::string id_name); /** * @brief 按行过滤并返回符合条件的列数据 * + * @note 过滤后的表格第一列尾用于匹配正则表达式的列,剩余列尾为筛选后符合条件的列数据。 + * * @param cnd_str 正则表达式 * @param cnd_col 用于匹配正则表达式的列名称 * @param out_col 输出的列索引列表(列表为空时则会输出所有列),正则表达式为真时即筛选这些行与列上对应的数据