From 51bca90178d02ec9dc58dadcda70c78163ed73c3 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Wed, 5 Feb 2025 14:25:59 +0800 Subject: [PATCH] add functions --- lib/io/dsv_io.cpp | 32 +++++++++++++++++++++++++++++++- lib/io/dsv_io.h | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index 91db8a1..e52e934 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -142,7 +142,7 @@ void gctl::dsv_io::row_type(table_cell_type t, int idx) { if (idx > row_num_ || idx <= 0) { - throw std::runtime_error("[gctl::dsv_io] Invalid column index."); + throw std::runtime_error("[gctl::dsv_io] Invalid row index."); } for (size_t i = 0; i <= col_num_; i++) @@ -156,6 +156,21 @@ void gctl::dsv_io::row_type(table_cell_type t, std::string name) row_type(t, name_index(name, true)); } +gctl::table_cell_type gctl::dsv_io::row_type(int idx) +{ + if (idx > row_num_ || idx <= 0) + { + throw std::runtime_error("[gctl::dsv_io] Invalid row index."); + } + + return table_[idx][0].type_; +} + +gctl::table_cell_type gctl::dsv_io::row_type(std::string name) +{ + return row_type(name_index(name, true)); +} + void gctl::dsv_io::column_type(table_cell_type t, int idx) { if (idx > col_num_ || idx <= 0) @@ -175,6 +190,21 @@ void gctl::dsv_io::column_type(table_cell_type t, std::string name) column_type(t, name_index(name, false)); } +gctl::table_cell_type gctl::dsv_io::column_type(int idx) +{ + if (idx > col_num_ || idx <= 0) + { + throw std::runtime_error("[gctl::dsv_io] Invalid column index."); + } + + return table_[0][idx].type_; +} + +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) { std::ifstream infile; diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 0b0a1b7..10e1c9d 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -164,8 +164,7 @@ namespace gctl dsv_io(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead); /** - * @brief 清理字符串向量对象 - * + * @brief 清理(还原)表格 */ void clear(); @@ -195,7 +194,7 @@ namespace gctl * * @param num 行数 */ - void head_number(char num){head_num_ = num;} + void head_number(int num){head_num_ = num;} /** * @brief 返回头信息 @@ -328,6 +327,22 @@ namespace gctl */ void row_type(table_cell_type t, std::string name); + /** + * @brief 获取行类型 + * + * @param idx 行索引 + * @return 行类型 + */ + table_cell_type row_type(int idx); + + /** + * @brief 获取行类型 + * + * @param name 行名称 + * @return 行类型 + */ + table_cell_type row_type(std::string name); + /** * @brief 设置列类型 * @@ -344,6 +359,22 @@ namespace gctl */ void column_type(table_cell_type t, std::string name); + /** + * @brief 获取列类型 + * + * @param idx 列索引 + * @return 列类型 + */ + table_cell_type column_type(int idx); + + /** + * @brief 获取列类型 + * + * @param name 列名称 + * @return 列类型 + */ + table_cell_type column_type(std::string name); + /** * @brief 读入文本文件 *