From 864ba45b2f4cb55f7b1aac2d2b13d564f1db60b1 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Thu, 12 Dec 2024 23:23:55 +0800 Subject: [PATCH] tmp --- example/CMakeLists.txt | 2 +- example/text_io_ex.cpp | 33 +++------- lib/io/text_io2.cpp | 38 +++++++---- lib/io/text_io2.h | 144 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 170 insertions(+), 47 deletions(-) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 6b39b6d..a32fda5 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -26,7 +26,7 @@ add_example(kde_ex OFF) add_example(meshio_ex OFF) add_example(autodiff_ex OFF) add_example(multinary_ex OFF) -add_example(text_io_ex OFF) +add_example(text_io_ex ON) add_example(getoption_ex OFF) add_example(process_ex OFF) 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 41d6783..663a1de 100644 --- a/example/text_io_ex.cpp +++ b/example/text_io_ex.cpp @@ -32,36 +32,21 @@ using namespace gctl; int main(int argc, char const *argv[]) try { -/* - text_content tc("tmp/topo"); + text_content tc("tmp/topo", ".txt", HasColumnName); - array x(tc.lines_.size()); - array y(tc.lines_.size()); - array z(tc.lines_.size()); - - for (size_t i = 0; i < tc.lines_.size(); i++) - { - parse_string_to_value(tc.lines_[i], ' ', true, x[i], y[i], z[i]); - } -*/ - - text_content tc("tmp/topo", ".txt", true); - - array x(tc.table_.size()); - array y(tc.table_.size()); - array z(tc.table_.size()); - - for (size_t i = 0; i < tc.table_.size(); i++) - { - x[i] = tc.table_[i][0].value(); - y[i] = tc.table_[i][1].value(); - z[i] = tc.table_[i][2].value(); - } + array x, y, z; + tc.get_column(0, x); + tc.get_column(1, y); + tc.get_column(2, z); + std::clog << std::setprecision(11) << z.front() << "\n"; std::clog << x.back() << "," << y.back() << "," << z.back() << "\n"; display_vector(tc.tags_, std::clog, '\n'); display_vector(tc.annotates_, std::clog, '\n'); display_vector(tc.heads_, std::clog, '\n'); + + tc.set_delimeter(','); + tc.save_text("out", ".csv"); return 0; } catch(std::exception &e) diff --git a/lib/io/text_io2.cpp b/lib/io/text_io2.cpp index cf441f2..71aab05 100644 --- a/lib/io/text_io2.cpp +++ b/lib/io/text_io2.cpp @@ -43,7 +43,7 @@ gctl::text_content::~text_content() clear(); } -gctl::text_content::text_content(std::string filename, std::string file_exten) +gctl::text_content::text_content(std::string filename, std::string file_exten, text_head_type_e t) { // 设置基础参数 att_sym_ = '#'; @@ -53,16 +53,7 @@ gctl::text_content::text_content(std::string filename, std::string file_exten) row_num_ = 0; col_num_ = 0; // 载入文本内容 - load_text(filename, file_exten); -} - -void gctl::text_content::set(char deli_sym, int h_num, char att_sym, char tag_sym) -{ - att_sym_ = att_sym; - tag_sym_ = tag_sym; - deli_sym_ = deli_sym; - head_num_ = h_num; - return; + load_text(filename, file_exten, t); } void gctl::text_content::clear() @@ -71,11 +62,12 @@ void gctl::text_content::clear() destroy_vector(annotates_); destroy_vector(tags_); destroy_vector(lines_); + destroy_vector(col_names_); destroy_vector(table_); return; } -void gctl::text_content::load_text(std::string filename, std::string file_exten) +void gctl::text_content::load_text(std::string filename, std::string file_exten, text_head_type_e t) { std::ifstream infile; open_infile(infile, filename, file_exten); @@ -114,6 +106,12 @@ void gctl::text_content::load_text(std::string filename, std::string file_exten) } infile.close(); + if (t == HasColumnName) + { + parse_string_to_vector(lines_[0], deli_sym_, col_names_); + lines_.erase(lines_.begin()); + } + row_num_ = lines_.size(); table_.resize(row_num_); @@ -167,6 +165,16 @@ void gctl::text_content::save_text(std::string filename, std::string file_exten) outfile << "# " << annotates_[i] << std::endl; } + if (!col_names_.empty()) + { + outfile << col_names_[0]; + for (size_t j = 1; j < col_names_.size(); j++) + { + outfile << deli_sym_ << col_names_[j]; + } + outfile << std::endl; + } + for (int i = 0; i < row_num_; i++) { outfile << table_[i][0].str_; @@ -199,4 +207,10 @@ void gctl::text_content::init_table(int row, int col) } } return; +} + +void gctl::text_content::set_column_name(const std::vector &names) +{ + col_names_ = names; + return; } \ No newline at end of file diff --git a/lib/io/text_io2.h b/lib/io/text_io2.h index 6480aa8..6b6d887 100644 --- a/lib/io/text_io2.h +++ b/lib/io/text_io2.h @@ -51,6 +51,12 @@ namespace gctl } }; + enum text_head_type_e + { + HasColumnName, + NoColumnName, + }; + struct text_content { // 头信息行数 表格行数 表格列数 @@ -60,6 +66,7 @@ namespace gctl // 头信息行 注释行 标记行 std::vector heads_, annotates_, tags_; // 内容行与表格 + std::vector col_names_; std::vector lines_; std::vector > table_; @@ -81,17 +88,35 @@ namespace gctl * @param filename 文件名 * @param file_exten 文件扩展名 */ - text_content(std::string filename, std::string file_exten = ".txt"); + text_content(std::string filename, std::string file_exten = ".txt", text_head_type_e t = NoColumnName); /** - * @brief 设置文件对象 + * @brief 设置列分隔符 * - * @param att_sym 注释行起始符 - * @param tag_sym 标记行起始符 - * @param deli 分割符 - * @param h_num 头信息行数 + * @param deli_sym 分隔符 */ - void set(char deli_sym = ' ', int h_num = 0, char att_sym = '#', char tag_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 清理字符串向量对象 @@ -105,7 +130,7 @@ namespace gctl * @param filename 文件名 * @param file_exten 文件扩展名 */ - void load_text(std::string filename, std::string file_exten = ".txt"); + void load_text(std::string filename, std::string file_exten = ".txt", text_head_type_e t = NoColumnName); /** * @brief 将内容写入文件文件 @@ -115,20 +140,119 @@ namespace gctl */ void save_text(std::string filename, std::string file_exten = ".txt"); + /** + * @brief 初始化表格 + * + * @param row 行数 + * @param col 列数 + */ void init_table(int row, int col); + /** + * @brief 设置列数据名称 + * + * @param names 列名称 + */ + void set_column_name(const std::vector &names); + + /** + * @brief 填充列 + * + * @tparam T 数据类型 + * @param idx 列索引 + * @param data 列数据 + */ template void fill_column(int idx, const array &data); + + /** + * @brief 填充行 + * + * @tparam T 数据类型 + * @param idx 行索引 + * @param data 行数据 + */ + template void fill_row(int idx, const array &data); + + /** + * @brief 获取列数据 + * + * @tparam T 数据类型 + * @param idx 列索引 + * @param data 列数据 + */ + template void get_column(int idx, array &data); + + /** + * @brief 获取行数据 + * + * @tparam T 数据类型 + * @param idx 行索引 + * @param data 行数据 + */ + template void get_row(int idx, array &data); }; template - void text_content::fill_column(int idx, const array &data) + void text_content::fill_column(int idx, const array &data) { - for (size_t i = 0; i < row_num_; i++) + if (idx >= col_num_) + { + throw std::runtime_error("[gctl::text_content::fill_column] Invalid column index."); + } + + for (size_t i = 0; i < std::min(row_num_, data.size()); i++) { table_[i][idx].value(data[i]); } return; } + + template + void text_content::fill_row(int idx, const array &data) + { + if (idx >= row_num_) + { + throw std::runtime_error("[gctl::text_content::fill_row] Invalid row index."); + } + + for (size_t i = 0; i < std::min(col_num_, data.size()); i++) + { + table_[idx][i].value(data[i]); + } + return; + } + + template + void text_content::get_column(int idx, array &data) + { + if (idx >= col_num_) + { + throw std::runtime_error("[gctl::text_content::get_column] Invalid column index."); + } + + data.resize(row_num_); + for (size_t i = 0; i < row_num_; i++) + { + data[i] = table_[i][idx].value(); + } + return; + } + + template + void text_content::get_row(int idx, array &data) + { + if (idx >= row_num_) + { + throw std::runtime_error("[gctl::text_content::get_column] Invalid row index."); + } + + data.resize(col_num_); + for (size_t i = 0; i < col_num_; i++) + { + data[i] = table_[idx][i].value(); + } + return; + } } #endif //_GCTL_TEXT_IO2_H \ No newline at end of file