diff --git a/example/text_io_ex.cpp b/example/text_io_ex.cpp index 81ce3ea..1e343de 100644 --- a/example/text_io_ex.cpp +++ b/example/text_io_ex.cpp @@ -54,6 +54,9 @@ int main(int argc, char const *argv[]) try geodsv_io tc; tc.load_text("tmp/topo", ".txt", ColumnHead); tc.set_column_names({"x (m)", "y (m)", "elev (m)"}); + //tc.set_column_type(Float, "x (m)"); + //tc.set_column_type(Float, "y (m)"); + tc.set_column_type(Float, "elev (m)"); array topo; tc.get_column_point3dc(topo, 1, 2, 3); diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index 23ac94e..b66c88f 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -114,6 +114,43 @@ void gctl::dsv_io::set_column_names(const std::vector &names) return; } +void gctl::dsv_io::set_row_type(cell_type_e t, int idx) +{ + if (idx > row_num_ || idx <= 0) + { + throw std::runtime_error("[gctl::dsv_io] Invalid column index."); + } + + for (size_t i = 0; i <= col_num_; i++) + { + table_[idx][i].type_ = t; + } +} + +void gctl::dsv_io::set_row_type(cell_type_e t, std::string name) +{ + set_row_type(t, name_index(name, true)); +} + +void gctl::dsv_io::set_column_type(cell_type_e t, int idx) +{ + if (idx > col_num_ || idx <= 0) + { + throw std::runtime_error("[gctl::dsv_io] Invalid column index."); + } + + for (size_t i = 0; i <= row_num_; i++) + { + table_[i][idx].type_ = t; + } + return; +} + +void gctl::dsv_io::set_column_type(cell_type_e t, std::string name) +{ + set_column_type(t, name_index(name, false)); +} + void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table_headtype_e t) { std::ifstream infile; @@ -169,7 +206,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table table_[i].resize(tmp_cols.size()); for (size_t j = 0; j < tmp_cols.size(); j++) { - table_[i][j].str_ = tmp_cols[j]; + table_[i][j].value(tmp_cols[j]); } } @@ -340,11 +377,19 @@ void gctl::dsv_io::info(table_headtype_e t) { if (table_[0][i].str_ != "") { - std::clog << table_[0][i].str_ << ": " << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; + std::clog << table_[0][i].str_ << " | "; + if (table_[1][i].type_ == String) std::clog << "String | "; + if (table_[1][i].type_ == Int) std::clog << "Int | "; + if (table_[1][i].type_ == Float) std::clog << "Float | "; + std::clog << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; } else { - std::clog << "C" + std::to_string(i) << ": " << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; + std::clog << "C" + std::to_string(i) << " | "; + if (table_[1][i].type_ == String) std::clog << "String | "; + if (table_[1][i].type_ == Int) std::clog << "Int | "; + if (table_[1][i].type_ == Float) std::clog << "Float | "; + std::clog << table_[1][i].str_ << " -> " << table_[row_num_][i].str_; } if (!table_[0][i].out_ok_) std::clog << " (No output)"; @@ -360,11 +405,19 @@ void gctl::dsv_io::info(table_headtype_e t) { if (table_[i][0].str_ != "") { - std::clog << table_[i][0].str_ << ": " << table_[i][1].str_ << " -> " << table_[i][col_num_].str_; + std::clog << table_[i][0].str_ << " | "; + if (table_[i][1].type_ == String) std::clog << "String | "; + if (table_[i][1].type_ == Int) std::clog << "Int | "; + if (table_[i][1].type_ == Float) std::clog << "Float | "; + std::clog << table_[i][1].str_ << " -> " << table_[i][col_num_].str_; } else { - std::clog << "R" + std::to_string(i) << ": " << table_[i][1].str_ << " -> " << table_[i][col_num_].str_; + std::clog << "R" + std::to_string(i) << " | "; + if (table_[i][1].type_ == String) std::clog << "String | "; + if (table_[i][1].type_ == Int) std::clog << "Int | "; + if (table_[i][1].type_ == Float) std::clog << "Float | "; + std::clog << table_[i][1].str_ << " -> " << table_[i][col_num_].str_; } if (!table_[i][0].out_ok_) std::clog << " (No output)"; diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 7c1c13e..93785c9 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -35,14 +35,23 @@ namespace gctl { + enum cell_type_e + { + String, + Int, + Float, + }; + struct table_cell { std::string str_; // 单元格的内容 统一保存为字符串 + cell_type_e type_; // 类型字符串 bool out_ok_; // 是否可输出到文件 table_cell() { str_ = ""; + type_ = String; out_ok_ = true; } @@ -60,6 +69,18 @@ namespace gctl template void value(const T &in, int p = 6) { + // 单元被赋值时使用的类型 + const std::type_info &tinfo = typeid(T); + + std::smatch ret; + std::regex pat("basic_string"); + + std::string t_str = tinfo.name(); + if (regex_search(t_str, ret, pat)) type_ = String; + else if (t_str == "i") type_ = Int; + else if (t_str == "f") type_ = Float; + else if (t_str == "d") type_ = Float; + // 对于double类型 可以设置转换的有效数字位数(精度) if constexpr (std::is_same::value) { @@ -258,6 +279,38 @@ namespace gctl */ void set_column_names(const std::vector &names); + /** + * @brief 设置行类型 + * + * @param t 类型名称 String|Float|Int + * @param idx 行索引 + */ + void set_row_type(cell_type_e t, int idx); + + /** + * @brief 设置行类型 + * + * @param t 类型名称 String|Float|Int + * @param name 行名称 + */ + void set_row_type(cell_type_e t, std::string name); + + /** + * @brief 设置列类型 + * + * @param t 类型名称 String|Float|Int + * @param idx 列索引 + */ + void set_column_type(cell_type_e t, int idx); + + /** + * @brief 设置列类型 + * + * @param t 类型名称 String|Float|Int + * @param name 列名称 + */ + void set_column_type(cell_type_e t, std::string name); + /** * @brief 读入文本文件 *