add type to dsv_io

This commit is contained in:
张壹 2025-01-02 09:40:27 +08:00
parent 4dc7e5ebc2
commit 47ff15a9e2
3 changed files with 114 additions and 5 deletions

View File

@ -54,6 +54,9 @@ int main(int argc, char const *argv[]) try
geodsv_io tc; geodsv_io tc;
tc.load_text("tmp/topo", ".txt", ColumnHead); tc.load_text("tmp/topo", ".txt", ColumnHead);
tc.set_column_names({"x (m)", "y (m)", "elev (m)"}); 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<point3dc> topo; array<point3dc> topo;
tc.get_column_point3dc(topo, 1, 2, 3); tc.get_column_point3dc(topo, 1, 2, 3);

View File

@ -114,6 +114,43 @@ void gctl::dsv_io::set_column_names(const std::vector<std::string> &names)
return; 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) void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table_headtype_e t)
{ {
std::ifstream infile; 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()); table_[i].resize(tmp_cols.size());
for (size_t j = 0; j < tmp_cols.size(); j++) 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_ != "") 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 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)"; 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_ != "") 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 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)"; if (!table_[i][0].out_ok_) std::clog << " (No output)";

View File

@ -35,14 +35,23 @@
namespace gctl namespace gctl
{ {
enum cell_type_e
{
String,
Int,
Float,
};
struct table_cell struct table_cell
{ {
std::string str_; // 单元格的内容 统一保存为字符串 std::string str_; // 单元格的内容 统一保存为字符串
cell_type_e type_; // 类型字符串
bool out_ok_; // 是否可输出到文件 bool out_ok_; // 是否可输出到文件
table_cell() table_cell()
{ {
str_ = ""; str_ = "";
type_ = String;
out_ok_ = true; out_ok_ = true;
} }
@ -60,6 +69,18 @@ namespace gctl
template <typename T> void value(const T &in, int p = 6) template <typename T> 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类型 可以设置转换的有效数字位数(精度) // 对于double类型 可以设置转换的有效数字位数(精度)
if constexpr (std::is_same<T, double>::value) if constexpr (std::is_same<T, double>::value)
{ {
@ -258,6 +279,38 @@ namespace gctl
*/ */
void set_column_names(const std::vector<std::string> &names); void set_column_names(const std::vector<std::string> &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 * @brief
* *