This commit is contained in:
张壹 2024-12-12 23:23:55 +08:00
parent 93604a89ae
commit 864ba45b2f
4 changed files with 170 additions and 47 deletions

View File

@ -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)

View File

@ -32,36 +32,21 @@ using namespace gctl;
int main(int argc, char const *argv[]) try
{
/*
text_content tc("tmp/topo");
text_content tc("tmp/topo", ".txt", HasColumnName);
array<double> x(tc.lines_.size());
array<double> y(tc.lines_.size());
array<double> z(tc.lines_.size());
for (size_t i = 0; i < tc.lines_.size(); i++)
{
parse_string_to_value(tc.lines_[i], ' ', true, x[i], y[i], z[i]);
}
*/
text_content tc("tmp/topo", ".txt", true);
array<double> x(tc.table_.size());
array<double> y(tc.table_.size());
array<double> z(tc.table_.size());
for (size_t i = 0; i < tc.table_.size(); i++)
{
x[i] = tc.table_[i][0].value<double>();
y[i] = tc.table_[i][1].value<double>();
z[i] = tc.table_[i][2].value<double>();
}
array<double> x, y, z;
tc.get_column(0, x);
tc.get_column(1, y);
tc.get_column(2, z);
std::clog << std::setprecision(11) << z.front() << "\n";
std::clog << x.back() << "," << y.back() << "," << z.back() << "\n";
display_vector(tc.tags_, std::clog, '\n');
display_vector(tc.annotates_, std::clog, '\n');
display_vector(tc.heads_, std::clog, '\n');
tc.set_delimeter(',');
tc.save_text("out", ".csv");
return 0;
}
catch(std::exception &e)

View File

@ -43,7 +43,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, text_head_type_e t)
{
// 设置基础参数
att_sym_ = '#';
@ -53,16 +53,7 @@ gctl::text_content::text_content(std::string filename, std::string file_exten)
row_num_ = 0;
col_num_ = 0;
// 载入文本内容
load_text(filename, file_exten);
}
void gctl::text_content::set(char deli_sym, int h_num, char att_sym, char tag_sym)
{
att_sym_ = att_sym;
tag_sym_ = tag_sym;
deli_sym_ = deli_sym;
head_num_ = h_num;
return;
load_text(filename, file_exten, t);
}
void gctl::text_content::clear()
@ -71,11 +62,12 @@ void gctl::text_content::clear()
destroy_vector(annotates_);
destroy_vector(tags_);
destroy_vector(lines_);
destroy_vector(col_names_);
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, text_head_type_e t)
{
std::ifstream infile;
open_infile(infile, filename, file_exten);
@ -114,6 +106,12 @@ void gctl::text_content::load_text(std::string filename, std::string file_exten)
}
infile.close();
if (t == HasColumnName)
{
parse_string_to_vector(lines_[0], deli_sym_, col_names_);
lines_.erase(lines_.begin());
}
row_num_ = lines_.size();
table_.resize(row_num_);
@ -167,6 +165,16 @@ void gctl::text_content::save_text(std::string filename, std::string file_exten)
outfile << "# " << annotates_[i] << std::endl;
}
if (!col_names_.empty())
{
outfile << col_names_[0];
for (size_t j = 1; j < col_names_.size(); j++)
{
outfile << deli_sym_ << col_names_[j];
}
outfile << std::endl;
}
for (int i = 0; i < row_num_; i++)
{
outfile << table_[i][0].str_;
@ -199,4 +207,10 @@ void gctl::text_content::init_table(int row, int col)
}
}
return;
}
void gctl::text_content::set_column_name(const std::vector<std::string> &names)
{
col_names_ = names;
return;
}

View File

@ -51,6 +51,12 @@ namespace gctl
}
};
enum text_head_type_e
{
HasColumnName,
NoColumnName,
};
struct text_content
{
// 头信息行数 表格行数 表格列数
@ -60,6 +66,7 @@ namespace gctl
// 头信息行 注释行 标记行
std::vector<std::string> heads_, annotates_, tags_;
// 内容行与表格
std::vector<std::string> col_names_;
std::vector<std::string> lines_;
std::vector<std::vector<cell_content> > table_;
@ -81,17 +88,35 @@ namespace gctl
* @param filename
* @param file_exten
*/
text_content(std::string filename, std::string file_exten = ".txt");
text_content(std::string filename, std::string file_exten = ".txt", text_head_type_e t = NoColumnName);
/**
* @brief
* @brief
*
* @param att_sym
* @param tag_sym
* @param deli
* @param h_num
* @param deli_sym
*/
void set(char deli_sym = ' ', int h_num = 0, char att_sym = '#', char tag_sym = '!');
void set_delimeter(char deli_sym){deli_sym_ = deli_sym;}
/**
* @brief
*
* @param num
*/
void set_head_number(char num){head_num_ = num;}
/**
* @brief
*
* @param att_sym
*/
void set_annotation_symbol(char att_sym){att_sym_ = att_sym;}
/**
* @brief
*
* @param tag_sym
*/
void set_tag_symbol(char tag_sym){tag_sym_ = tag_sym;}
/**
* @brief
@ -105,7 +130,7 @@ namespace gctl
* @param filename
* @param file_exten
*/
void load_text(std::string filename, std::string file_exten = ".txt");
void load_text(std::string filename, std::string file_exten = ".txt", text_head_type_e t = NoColumnName);
/**
* @brief
@ -115,20 +140,119 @@ namespace gctl
*/
void save_text(std::string filename, std::string file_exten = ".txt");
/**
* @brief
*
* @param row
* @param col
*/
void init_table(int row, int col);
/**
* @brief
*
* @param names
*/
void set_column_name(const std::vector<std::string> &names);
/**
* @brief
*
* @tparam T
* @param idx
* @param data
*/
template <typename T> void fill_column(int idx, const array<T> &data);
/**
* @brief
*
* @tparam T
* @param idx
* @param data
*/
template <typename T> void fill_row(int idx, const array<T> &data);
/**
* @brief
*
* @tparam T
* @param idx
* @param data
*/
template <typename T> void get_column(int idx, array<T> &data);
/**
* @brief
*
* @tparam T
* @param idx
* @param data
*/
template <typename T> void get_row(int idx, array<T> &data);
};
template <typename T>
void text_content::fill_column<T>(int idx, const array<T> &data)
void text_content::fill_column(int idx, const array<T> &data)
{
for (size_t i = 0; i < row_num_; i++)
if (idx >= col_num_)
{
throw std::runtime_error("[gctl::text_content::fill_column] Invalid column index.");
}
for (size_t i = 0; i < std::min(row_num_, data.size()); i++)
{
table_[i][idx].value(data[i]);
}
return;
}
template <typename T>
void text_content::fill_row(int idx, const array<T> &data)
{
if (idx >= row_num_)
{
throw std::runtime_error("[gctl::text_content::fill_row] Invalid row index.");
}
for (size_t i = 0; i < std::min(col_num_, data.size()); i++)
{
table_[idx][i].value(data[i]);
}
return;
}
template <typename T>
void text_content::get_column(int idx, array<T> &data)
{
if (idx >= col_num_)
{
throw std::runtime_error("[gctl::text_content::get_column] Invalid column index.");
}
data.resize(row_num_);
for (size_t i = 0; i < row_num_; i++)
{
data[i] = table_[i][idx].value<T>();
}
return;
}
template <typename T>
void text_content::get_row(int idx, array<T> &data)
{
if (idx >= row_num_)
{
throw std::runtime_error("[gctl::text_content::get_column] Invalid row index.");
}
data.resize(col_num_);
for (size_t i = 0; i < col_num_; i++)
{
data[i] = table_[idx][i].value<T>();
}
return;
}
}
#endif //_GCTL_TEXT_IO2_H