tmp
This commit is contained in:
parent
93604a89ae
commit
864ba45b2f
@ -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)
|
@ -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)
|
||||
|
@ -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;
|
||||
}
|
@ -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
|
Loading…
Reference in New Issue
Block a user