From b184ac95fbb95cb76ef5e5bd07680b31f3cac81a Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Mon, 10 Feb 2025 21:25:13 +0800 Subject: [PATCH] ymp --- example/CMakeLists.txt | 2 +- example/text_io_ex.cpp | 5 ++-- lib/io/dsv_io.cpp | 54 ++++++++++++++++++++++++++++---------- lib/io/dsv_io.h | 32 ++++++++++++++-------- out.txt | 53 +++++++++++++++++++++++++++++++++++++ tool/dsviewer/dsviewer.cpp | 22 +++++++++++----- tool/dsviewer/dsviewer.h | 1 + tool/dsviewer/dsviewer.md | 4 +-- 8 files changed, 136 insertions(+), 37 deletions(-) create mode 100644 out.txt diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 42d3f57..fcc8b3a 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) diff --git a/example/text_io_ex.cpp b/example/text_io_ex.cpp index f2fdbb2..930579e 100644 --- a/example/text_io_ex.cpp +++ b/example/text_io_ex.cpp @@ -34,8 +34,9 @@ int main(int argc, char const *argv[]) try dsv_io tc, tout; tc.delimeter('|'); - tc.load_text("tmp/world_data", ".txt", BothHead); - //tc.info(); + tc.head_number(1); + tc.load_text("tmp/world_data", ".txt", ColHead|RowHead); + tc.info(AttInfo|HeadInfo|TagInfo|ColInfo|RowInfo); //tc.set_column_type(Int, "IndepYear_n"); //tc.filt_column("IndepYear_n < 0", {"IndepYear_n"}, {"Name_s", "Population_n", "GNP_n"}, tout); diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index e52e934..af3ccee 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -43,7 +43,7 @@ gctl::dsv_io::~dsv_io() clear(); } -gctl::dsv_io::dsv_io(std::string filename, std::string file_exten, table_headtype_e t) +gctl::dsv_io::dsv_io(std::string filename, std::string file_exten, int t) { file_ = ""; att_sym_ = '#'; @@ -205,7 +205,7 @@ gctl::table_cell_type gctl::dsv_io::column_type(std::string name) return column_type(name_index(name, false)); } -void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table_headtype_e t) +void gctl::dsv_io::load_text(std::string filename, std::string file_exten, int t) { std::ifstream infile; open_infile(infile, filename, file_exten); @@ -290,7 +290,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table } } - if (t == ColumnHead) // 有列头 需要补齐空白的行头 + if (t == ColHead) // 有列头 需要补齐空白的行头 { row_num_ = table_.size() - 1; col_num_ = table_[0].size(); @@ -310,7 +310,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 & RowHead) && (t & ColHead)) // 有行头和列头 { row_num_ = table_.size() - 1; col_num_ = table_[0].size() - 1; @@ -320,7 +320,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table return; } -void gctl::dsv_io::load_csv(std::string filename, table_headtype_e t) +void gctl::dsv_io::load_csv(std::string filename, int t) { delimeter(','); load_text(filename, ".csv", t); @@ -396,15 +396,41 @@ void gctl::dsv_io::init_table(int row, int col) return; } -void gctl::dsv_io::info(table_headtype_e t) +void gctl::dsv_io::info(int 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 << "------------\n"; + if (t & HeadInfo) + { + for (size_t i = 0; i < heads_.size(); i++) + { + std::clog << heads_[i] << "\n"; + } + std::clog << "------------\n"; + } - if (t == ColumnHead || t == BothHead) + std::clog << "Annotation(s): " << annotates_.size() << "\n"; + if (t & AttInfo) + { + for (size_t i = 0; i < annotates_.size(); i++) + { + std::clog << annotates_[i] << "\n"; + } + std::clog << "------------\n"; + } + + std::clog << "Tag(s): " << tags_.size() << "\n"; + if (t & TagInfo) + { + for (size_t i = 0; i < tags_.size(); i++) + { + std::clog << tags_[i] << "\n"; + } + std::clog << "------------\n"; + } + else std::clog << "------------\n"; + + if (t & ColInfo) { std::clog << "Columns:\n"; for (size_t i = 1; i <= col_num_; i++) @@ -429,10 +455,10 @@ void gctl::dsv_io::info(table_headtype_e t) if (!table_[0][i].out_ok_) std::clog << " (No output)"; std::clog << std::endl; } - std::clog << "============\n"; + std::clog << "------------\n"; } - if (t == RowHead || t == BothHead) + if (t & RowInfo) { std::clog << "Rows:\n"; for (size_t i = 1; i <= row_num_; i++) @@ -457,7 +483,7 @@ void gctl::dsv_io::info(table_headtype_e t) if (!table_[i][0].out_ok_) std::clog << " (No output)"; std::clog << std::endl; } - std::clog << "============\n"; + std::clog << "------------\n"; } return; } @@ -941,7 +967,7 @@ gctl::geodsv_io::geodsv_io(){} gctl::geodsv_io::~geodsv_io(){} -gctl::geodsv_io::geodsv_io(std::string filename, std::string file_exten, table_headtype_e t) +gctl::geodsv_io::geodsv_io(std::string filename, std::string file_exten, int t) { file_ = ""; att_sym_ = '#'; diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 5d1369e..52cdd68 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -105,15 +105,25 @@ namespace gctl }; /** - * @brief 表格的头信息类型 - * + * @brief 表格的头名称类型 */ enum table_headtype_e { - NoHead, // 没有表头 - BothHead, // 同时有行与列表头 - ColumnHead, // 只有列表头 - RowHead, // 只有行表头 + NoHead = 1, // 0001 无表头 + ColHead = 2, // 0010 有列表头 + RowHead = 4, // 0100 有行表头 + }; + + /** + * @brief 表格的信息类型 + */ + enum table_infotype_e + { + TagInfo = 1, // 00001 + AttInfo = 2, // 00010 + HeadInfo = 4, // 00100 + RowInfo = 8, // 01000 + ColInfo = 16, // 10000 }; /** @@ -161,7 +171,7 @@ namespace gctl * @param file_exten 文件扩展名 * @param t 表格是否有行和列名称 */ - dsv_io(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead); + dsv_io(std::string filename, std::string file_exten = ".txt", int t = NoHead); /** * @brief 清理(还原)表格 @@ -382,14 +392,14 @@ namespace gctl * @param file_exten 文件扩展名 * @param t 表格是否有行和列名称 */ - void load_text(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead); + void load_text(std::string filename, std::string file_exten = ".txt", int t = NoHead); /** * @brief 读入CSV文件 * * @param filename 文件名 */ - void load_csv(std::string filename, table_headtype_e t = ColumnHead); + void load_csv(std::string filename, int t = ColHead); /** * @brief 将内容写入文件 @@ -419,7 +429,7 @@ namespace gctl * * @param t 显示表格信息的类型 */ - void info(table_headtype_e t = ColumnHead); + void info(int t); /** * @brief 返回名称为name和R和C的行或列的索引 @@ -861,7 +871,7 @@ namespace gctl * @param filename 文件名 * @param file_exten 文件扩展名 */ - geodsv_io(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead); + geodsv_io(std::string filename, std::string file_exten = ".txt", int t = NoHead); /** * @brief 填充二维坐标列 diff --git a/out.txt b/out.txt new file mode 100644 index 0000000..9b49ad3 --- /dev/null +++ b/out.txt @@ -0,0 +1,53 @@ +Code_s|Continent_s|Name_s|Population_n|GNP_n +ANT|North America|Netherlands Antilles|217000|1941 +AIA|North America|Anguilla|8000|63.2 +ATG|North America|Antigua and Barbuda|68000|612 +ARG|South America|Argentina|37032000|340238 +ABW|North America|Aruba|103000|828 +BHS|North America|Bahamas|307000|3527 +BRB|North America|Barbados|270000|2223 +BLZ|North America|Belize|241000|630 +BMU|North America|Bermuda|65000|2328 +BOL|South America|Bolivia|8329000|8571 +BRA|South America|Brazil|170115000|776739 +VGB|North America|Virgin Islands, British|21000|612 +CYM|North America|Cayman Islands|38000|1263 +CHL|South America|Chile|15211000|72949 +CRI|North America|Costa Rica|4023000|10226 +DMA|North America|Dominica|71000|256 +DOM|North America|Dominican Republic|8495000|15846 +ECU|South America|Ecuador|12646000|19770 +SLV|North America|El Salvador|6276000|11863 +FLK|South America|Falkland Islands|2000|0 +GRD|North America|Grenada|94000|318 +GRL|North America|Greenland|56000|0 +GLP|North America|Guadeloupe|456000|3501 +GTM|North America|Guatemala|11385000|19008 +GUY|South America|Guyana|861000|722 +HTI|North America|Haiti|8222000|3459 +HND|North America|Honduras|6485000|5333 +JAM|North America|Jamaica|2583000|6871 +CAN|North America|Canada|31147000|598862 +COL|South America|Colombia|42321000|102896 +CUB|North America|Cuba|11201000|17843 +MTQ|North America|Martinique|395000|2731 +MEX|North America|Mexico|98881000|414972 +MSR|North America|Montserrat|11000|109 +NIC|North America|Nicaragua|5074000|1988 +PAN|North America|Panama|2856000|9131 +PRY|South America|Paraguay|5496000|8444 +PER|South America|Peru|25662000|64140 +PRI|North America|Puerto Rico|3869000|34100 +GUF|South America|French Guiana|181000|681 +KNA|North America|Saint Kitts and Nevis|38000|299 +LCA|North America|Saint Lucia|154000|571 +VCT|North America|Saint Vincent and the Grenadines|114000|285 +SPM|North America|Saint Pierre and Miquelon|7000|0 +SUR|South America|Suriname|417000|870 +TTO|North America|Trinidad and Tobago|1295000|6232 +TCA|North America|Turks and Caicos Islands|17000|96 +URY|South America|Uruguay|3337000|20831 +VEN|South America|Venezuela|24170000|95023 +USA|North America|United States|278357000|8510700 +VIR|North America|Virgin Islands, U.S.|93000|0 +|Asia|China|14000000|1949 diff --git a/tool/dsviewer/dsviewer.cpp b/tool/dsviewer/dsviewer.cpp index 22769ed..8fa73dc 100644 --- a/tool/dsviewer/dsviewer.cpp +++ b/tool/dsviewer/dsviewer.cpp @@ -217,13 +217,21 @@ void quit(const std::vector &cmd_units) void info(const std::vector &cmd_units) { - // info [column|row|both] + std::smatch ret; + std::regex pata("att"), patt("tag"), patc("col"), patr("row"), path("hdr"); + + // info [att|tag|hdr|col|row] + int info_code; if (cmd_units.size() > 1) { - if (cmd_units[1] == "row") tc.info(RowHead); - if (cmd_units[1] == "both") tc.info(BothHead); + if (regex_search(cmd_units[1], ret, pata)) info_code = AttInfo; + if (regex_search(cmd_units[1], ret, patt)) info_code = info_code|TagInfo; + if (regex_search(cmd_units[1], ret, patc)) info_code = info_code|ColInfo; + if (regex_search(cmd_units[1], ret, patr)) info_code = info_code|RowInfo; + if (regex_search(cmd_units[1], ret, path)) info_code = info_code|HeadInfo; + tc.info(info_code); } - else tc.info(); + else tc.info(ColInfo); return; } @@ -320,11 +328,11 @@ void load_file(const std::vector &cmd_units) copy_str[i] = cmd_units[i + 2]; } - table_headtype_e ht = NoHead; + int ht = NoHead; if (copy_str[0] == "nohead") ht = NoHead; - else if (copy_str[0] == "column") ht = ColumnHead; + else if (copy_str[0] == "column") ht = ColHead; else if (copy_str[0] == "row") ht = RowHead; - else if (copy_str[0] == "both") ht = BothHead; + else if (copy_str[0] == "both") ht = RowHead|ColHead; int hnum = 0; if (copy_str[1] != "null") hnum = atoi(copy_str[1].c_str()); diff --git a/tool/dsviewer/dsviewer.h b/tool/dsviewer/dsviewer.h index 0ebacd4..dcb64ad 100644 --- a/tool/dsviewer/dsviewer.h +++ b/tool/dsviewer/dsviewer.h @@ -28,6 +28,7 @@ #ifndef GCTL_DSVIEWER_H #define GCTL_DSVIEWER_H +#include #include "../../lib/gctl_config.h" #include "../../lib/io.h" diff --git a/tool/dsviewer/dsviewer.md b/tool/dsviewer/dsviewer.md index 7c71793..2fafc9a 100644 --- a/tool/dsviewer/dsviewer.md +++ b/tool/dsviewer/dsviewer.md @@ -1,8 +1,8 @@ #### quit Does what it says. -#### info [column|row|both] -Show the table information. +#### info [att|tag|hdr|col|row] +Show the table information. 'att' shows the annotations, 'tag' shows the tag lines, 'hdr' shows the head lines, 'col' shows the columns' summary, and 'row' shows the rows' summary. Use '|' to separate multiple options. #### head Show the first `n` (the default is 10) rows of the table.