From 93604a89ae51fea967d9f017e001058a6ffc05f8 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Thu, 12 Dec 2024 16:55:06 +0800 Subject: [PATCH] tmp --- lib/io/text_io2.cpp | 88 +++++++++++++++++++++++++++++---------------- lib/io/text_io2.h | 31 ++++++++++------ 2 files changed, 78 insertions(+), 41 deletions(-) diff --git a/lib/io/text_io2.cpp b/lib/io/text_io2.cpp index 722e1bb..cf441f2 100644 --- a/lib/io/text_io2.cpp +++ b/lib/io/text_io2.cpp @@ -34,6 +34,8 @@ gctl::text_content::text_content() tag_sym_ = '!'; deli_sym_ = ' '; head_num_ = 0; + row_num_ = 0; + col_num_ = 0; } gctl::text_content::~text_content() @@ -41,15 +43,17 @@ gctl::text_content::~text_content() clear(); } -gctl::text_content::text_content(std::string filename, std::string file_exten, bool unpack) +gctl::text_content::text_content(std::string filename, std::string file_exten) { // 设置基础参数 att_sym_ = '#'; tag_sym_ = '!'; deli_sym_ = ' '; head_num_ = 0; + row_num_ = 0; + col_num_ = 0; // 载入文本内容 - load_text(filename, file_exten, unpack); + load_text(filename, file_exten); } void gctl::text_content::set(char deli_sym, int h_num, char att_sym, char tag_sym) @@ -71,7 +75,7 @@ void gctl::text_content::clear() return; } -void gctl::text_content::load_text(std::string filename, std::string file_exten, bool unpack) +void gctl::text_content::load_text(std::string filename, std::string file_exten) { std::ifstream infile; open_infile(infile, filename, file_exten); @@ -108,30 +112,42 @@ void gctl::text_content::load_text(std::string filename, std::string file_exten, } else lines_.push_back(tmp_line); } - infile.close(); - if (unpack) + row_num_ = lines_.size(); + table_.resize(row_num_); + + int cn; + std::vector tmp_cols; + for (size_t i = 0; i < row_num_; i++) { - table_.resize(lines_.size()); + tmp_cols.clear(); + parse_string_to_vector(lines_[i], deli_sym_, tmp_cols); + + cn = tmp_cols.size(); + col_num_ = std::max(cn, col_num_); - std::vector tmp_cols; - for (size_t i = 0; i < lines_.size(); i++) + table_[i].resize(tmp_cols.size()); + for (size_t j = 0; j < tmp_cols.size(); j++) { - tmp_cols.clear(); - parse_string_to_vector(lines_[i], deli_sym_, tmp_cols); + table_[i][j].str_ = tmp_cols[j]; + } + } - table_[i].resize(tmp_cols.size()); - for (size_t j = 0; j < tmp_cols.size(); j++) - { - table_[i][j].str_ = tmp_cols[j]; - } + cell_content empty_cell; + empty_cell.str_ = ""; + for (size_t i = 0; i < row_num_; i++) + { + cn = table_[i].size(); + for (size_t j = cn; j < col_num_; j++) + { + table_[i].push_back(empty_cell); } } return; } -void gctl::text_content::save_text(std::string filename, std::string file_exten, bool packed) +void gctl::text_content::save_text(std::string filename, std::string file_exten) { std::ofstream outfile; open_outfile(outfile, filename, file_exten); @@ -151,26 +167,36 @@ void gctl::text_content::save_text(std::string filename, std::string file_exten, outfile << "# " << annotates_[i] << std::endl; } - if (packed) + for (int i = 0; i < row_num_; i++) { - for (int i = 0; i < lines_.size(); i++) + outfile << table_[i][0].str_; + for (size_t j = 1; j < col_num_; j++) { - outfile << lines_[i] << std::endl; - } - } - else - { - for (int i = 0; i < table_.size(); i++) - { - outfile << table_[i][0].str_; - for (size_t j = 1; j < table_[i].size(); j++) - { - outfile << deli_sym_ << table_[i][j].str_; - } - outfile << std::endl; + outfile << deli_sym_ << table_[i][j].str_; } + outfile << std::endl; } outfile.close(); return; +} + +void gctl::text_content::init_table(int row, int col) +{ + row_num_ = row; + col_num_ = col; + + lines_.resize(row_num_); + table_.resize(row_num_); + for (size_t i = 0; i < row_num_; i++) + { + lines_[i] = ""; + + table_[i].resize(col_num_); + for (size_t j = 0; j < col_num_; j++) + { + table_[i][j].str_ = ""; + } + } + return; } \ No newline at end of file diff --git a/lib/io/text_io2.h b/lib/io/text_io2.h index e5c2a73..6480aa8 100644 --- a/lib/io/text_io2.h +++ b/lib/io/text_io2.h @@ -53,8 +53,8 @@ namespace gctl struct text_content { - // 头信息行数 - int head_num_; + // 头信息行数 表格行数 表格列数 + int head_num_, row_num_, col_num_; // 注释行起始符 标记行起始符 分割符 char att_sym_, tag_sym_, deli_sym_; // 头信息行 注释行 标记行 @@ -80,10 +80,9 @@ namespace gctl * * @param filename 文件名 * @param file_exten 文件扩展名 - * @param unpack 将文件行内容按分隔符拆散,保存至table_ */ - text_content(std::string filename, std::string file_exten = ".txt", bool unpack = false); - + text_content(std::string filename, std::string file_exten = ".txt"); + /** * @brief 设置文件对象 * @@ -105,19 +104,31 @@ namespace gctl * * @param filename 文件名 * @param file_exten 文件扩展名 - * @param unpack 将文件行内容按分隔符拆散,保存至table_ */ - void load_text(std::string filename, std::string file_exten = ".txt", bool unpack = false); + void load_text(std::string filename, std::string file_exten = ".txt"); /** * @brief 将内容写入文件文件 * * @param filename 文件名 * @param file_exten 文件扩展名 - * @param packed 为真使用lines_写入文件,为假则使用tabl_写入文件 */ - void save_text(std::string filename, std::string file_exten = ".txt", bool packed = true); - }; + void save_text(std::string filename, std::string file_exten = ".txt"); + + void init_table(int row, int col); + + template void fill_column(int idx, const array &data); + }; + + template + void text_content::fill_column(int idx, const array &data) + { + for (size_t i = 0; i < row_num_; i++) + { + table_[i][idx].value(data[i]); + } + return; + } } #endif //_GCTL_TEXT_IO2_H \ No newline at end of file