From d701c6234bfdb685acc694174afef3ab7981e4de Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Mon, 4 Nov 2024 16:03:39 +0800 Subject: [PATCH] tmp --- example/text_io_ex.cpp | 25 ++++++++++++++---- lib/io/text_io2.cpp | 58 +++++++++++++++++++++++++++++++++--------- lib/io/text_io2.h | 36 +++++++++++++++++++++----- 3 files changed, 96 insertions(+), 23 deletions(-) diff --git a/example/text_io_ex.cpp b/example/text_io_ex.cpp index 31a597c..2142ef6 100644 --- a/example/text_io_ex.cpp +++ b/example/text_io_ex.cpp @@ -32,15 +32,30 @@ using namespace gctl; int main(int argc, char const *argv[]) try { +/* text_content tc("tmp/topo"); - array x(tc.contents_.size()); - array y(tc.contents_.size()); - array z(tc.contents_.size()); + array x(tc.lines_.size()); + array y(tc.lines_.size()); + array z(tc.lines_.size()); - for (size_t i = 0; i < tc.contents_.size(); i++) + for (size_t i = 0; i < tc.lines_.size(); i++) { - parse_string_to_value(tc.contents_[i], ' ', true, x[i], y[i], z[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(); } std::clog << x.back() << "," << y.back() << "," << z.back() << "\n"; diff --git a/lib/io/text_io2.cpp b/lib/io/text_io2.cpp index 1863617..e88d841 100644 --- a/lib/io/text_io2.cpp +++ b/lib/io/text_io2.cpp @@ -41,7 +41,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, bool unpack) { // 设置基础参数 att_sym_ = '#'; @@ -49,7 +49,7 @@ gctl::text_content::text_content(std::string filename, std::string file_exten) deli_sym_ = ' '; head_num_ = 0; // 载入文本内容 - load_text(filename, file_exten); + load_text(filename, file_exten, unpack); } void gctl::text_content::set(char deli_sym, int h_num, char att_sym, char tag_sym) @@ -63,14 +63,15 @@ void gctl::text_content::set(char deli_sym, int h_num, char att_sym, char tag_sy void gctl::text_content::clear() { - heads_.clear(); - annotates_.clear(); - tags_.clear(); - contents_.clear(); + destroy_vector(heads_); + destroy_vector(annotates_); + destroy_vector(tags_); + destroy_vector(lines_); + 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, bool unpack) { std::ifstream infile; open_infile(infile, filename, file_exten); @@ -82,7 +83,7 @@ void gctl::text_content::load_text(std::string filename, std::string file_exten) for (int i = 0; i < head_num_; i++) { std::getline(infile, tmp_line); - heads_.push_back(tmp_line); + if (!tmp_line.empty()) heads_.push_back(tmp_line); } while (std::getline(infile, tmp_line)) @@ -101,14 +102,32 @@ void gctl::text_content::load_text(std::string filename, std::string file_exten) tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1); tags_.push_back(tmp_line); } - else contents_.push_back(tmp_line); + else if (!tmp_line.empty()) lines_.push_back(tmp_line); } infile.close(); + + if (unpack) + { + table_.resize(lines_.size()); + + std::vector tmp_cols; + for (size_t i = 0; i < lines_.size(); i++) + { + tmp_cols.clear(); + parse_string_to_vector(lines_[i], deli_sym_, tmp_cols); + + table_[i].resize(tmp_cols.size()); + for (size_t j = 0; j < tmp_cols.size(); j++) + { + table_[i][j].str_ = tmp_cols[j]; + } + } + } return; } -void gctl::text_content::save_text(std::string filename, std::string file_exten) +void gctl::text_content::save_text(std::string filename, std::string file_exten, bool packed) { std::ofstream outfile; open_outfile(outfile, filename, file_exten); @@ -128,9 +147,24 @@ void gctl::text_content::save_text(std::string filename, std::string file_exten) outfile << "# " << annotates_[i] << std::endl; } - for (int i = 0; i < contents_.size(); i++) + if (packed) { - outfile << contents_[i] << std::endl; + for (int i = 0; i < lines_.size(); i++) + { + 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.close(); diff --git a/lib/io/text_io2.h b/lib/io/text_io2.h index c27eb94..05c11b3 100644 --- a/lib/io/text_io2.h +++ b/lib/io/text_io2.h @@ -33,14 +33,35 @@ namespace gctl { + struct cell_content + { + std::string str_; + + template T value() + { + T out; + str2type(str_, out); + return out; + } + + template void value(const T &in) + { + str_ = std::to_string(in); + return; + } + }; + struct text_content { // 头信息行数 int head_num_; // 注释行起始符 标记行起始符 分割符 char att_sym_, tag_sym_, deli_sym_; - // 注释行 标记行 - std::vector heads_, annotates_, tags_, contents_; + // 头信息行 注释行 标记行 + std::vector heads_, annotates_, tags_; + // 内容行与表格 + std::vector lines_; + std::vector > table_; /** * @brief Construct a new text content object @@ -59,8 +80,9 @@ namespace gctl * * @param filename 文件名 * @param file_exten 文件扩展名 + * @param unpack 将文件行内容按分隔符拆散,保存至table_ */ - text_content(std::string filename, std::string file_exten = ".txt"); + text_content(std::string filename, std::string file_exten = ".txt", bool unpack = false); /** * @brief 设置文件对象 @@ -83,16 +105,18 @@ namespace gctl * * @param filename 文件名 * @param file_exten 文件扩展名 + * @param unpack 将文件行内容按分隔符拆散,保存至table_ */ - void load_text(std::string filename, std::string file_exten = ".txt"); + void load_text(std::string filename, std::string file_exten = ".txt", bool unpack = false); /** - * @brief 写入文件文件 + * @brief 将内容写入文件文件 * * @param filename 文件名 * @param file_exten 文件扩展名 + * @param packed 为真使用lines_写入文件,为假则使用tabl_写入文件 */ - void save_text(std::string filename, std::string file_exten = ".txt"); + void save_text(std::string filename, std::string file_exten = ".txt", bool packed = true); }; }