This commit is contained in:
张壹 2024-12-13 10:11:47 +08:00
parent 864ba45b2f
commit b13872843b
3 changed files with 335 additions and 21 deletions

View File

@ -32,21 +32,35 @@ using namespace gctl;
int main(int argc, char const *argv[]) try
{
text_content tc;
tc.load_text("tmp/sample", ".txt", HasColumnName);
tc.info();
/*
text_content tc("tmp/topo", ".txt", HasColumnName);
array<double> x, y, z;
tc.get_column(0, x);
tc.get_column(1, y);
tc.get_column(2, z);
tc.get_column("elevation", 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');
display_vector(tc.get_tags(), std::clog, '\n');
display_vector(tc.get_annotoations(), std::clog, '\n');
display_vector(tc.get_head_records(), std::clog, '\n');
tc.set_delimeter(',');
tc.save_text("out", ".csv");
_1s_vector s = tc.get_tags();
s.push_back("Elev = 1000");
tc.set_tags(s);
tc.save_csv("out");
double c = 4.25242153654;
tc.cell(0, 0, c, 12);
std::clog << std::setprecision(12) << tc.cell<double>(0, 0) << "\n";
tc.info();
*/
return 0;
}
catch(std::exception &e)

View File

@ -81,6 +81,8 @@ void gctl::text_content::load_text(std::string filename, std::string file_exten,
std::getline(infile, tmp_line);
if (!tmp_line.empty()) heads_.push_back(tmp_line);
}
// 校正头信息行数
head_num_ = heads_.size();
while (std::getline(infile, tmp_line))
{
@ -142,6 +144,20 @@ void gctl::text_content::load_text(std::string filename, std::string file_exten,
table_[i].push_back(empty_cell);
}
}
for (size_t i = col_names_.size(); i < col_num_; i++)
{
col_names_.push_back("col-" + std::to_string(i + 1));
}
file_ = filename + file_exten;
return;
}
void gctl::text_content::load_csv(std::string filename, text_head_type_e t)
{
set_delimeter(',');
load_text(filename, ".csv", t);
return;
}
@ -165,15 +181,12 @@ 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 << col_names_[0];
for (size_t j = 1; j < col_names_.size(); j++)
{
outfile << deli_sym_ << col_names_[j];
}
outfile << std::endl;
outfile << deli_sym_ << col_names_[j];
}
outfile << std::endl;
for (int i = 0; i < row_num_; i++)
{
@ -189,28 +202,104 @@ void gctl::text_content::save_text(std::string filename, std::string file_exten)
return;
}
void gctl::text_content::save_csv(std::string filename)
{
set_delimeter(',');
save_text(filename, ".csv");
return;
}
void gctl::text_content::init_table(int row, int col)
{
row_num_ = row;
col_num_ = col;
lines_.resize(row_num_);
table_.resize(row_num_);
for (size_t i = 0; i < row_num_; i++)
{
lines_[i] = "";
table_[i].resize(col_num_);
for (size_t j = 0; j < col_num_; j++)
{
table_[i][j].str_ = "";
}
}
col_names_.resize(col_num_);
for (size_t i = 0; i < col_num_; i++)
{
col_names_[i] = "col-" + std::to_string(i + 1);
}
return;
}
void gctl::text_content::set_column_name(const std::vector<std::string> &names)
void gctl::text_content::init_table(int row, int col, const std::vector<std::string> &names)
{
init_table(row, col);
set_column_names(names);
return;
}
void gctl::text_content::set_head_records(const std::vector<std::string> &heads)
{
heads_ = heads;
head_num_ = heads_.size();
return;
}
void gctl::text_content::set_annotoations(const std::vector<std::string> &att)
{
annotates_ = att;
return;
}
void gctl::text_content::set_tags(const std::vector<std::string> &tags)
{
tags_ = tags;
return;
}
void gctl::text_content::set_column_names(const std::vector<std::string> &names)
{
if (col_num_ != col_names_.size())
{
throw std::runtime_error("[gctl::text_content::set_column_names] Invalid name size.");
}
col_names_ = names;
return;
}
void gctl::text_content::cell(int r, int c, double d, int p)
{
std::stringstream ss;
if (p != 6) ss.precision(p);
ss << d;
ss >> table_[r][c].str_;
return;
}
void gctl::text_content::info()
{
std::clog << "File: " << file_ << "\n------------\n";
std::clog << "Head(s): " << head_num_ << "\n";
std::clog << "Annotation(s): " << annotates_.size() << "\n";
std::clog << "Tag(s): " << tags_.size() << "\n";
std::clog << "------------\nColumns:\n============\n";
if (col_names_.empty())
{
for (size_t i = 0; i < col_num_; i++)
{
std::clog << table_[0][i].str_ << " -> " << table_.back()[i].str_ << "\n";
}
}
else
{
for (size_t i = 0; i < col_num_; i++)
{
std::clog << col_names_[i] << ": " << table_[0][i].str_ << " -> " << table_.back()[i].str_ << "\n";
}
}
return;
}

View File

@ -57,8 +57,20 @@ namespace gctl
NoColumnName,
};
struct text_content
/**
* @brief
*
*
* 1. '#'
* 2. '#!'
* 3. n行头信息
* 4. row*col大小的表格
*
*/
class text_content
{
private:
std::string file_;
// 头信息行数 表格行数 表格列数
int head_num_, row_num_, col_num_;
// 注释行起始符 标记行起始符 分割符
@ -70,6 +82,7 @@ namespace gctl
std::vector<std::string> lines_;
std::vector<std::vector<cell_content> > table_;
public:
/**
* @brief Construct a new text content object
*
@ -118,6 +131,92 @@ namespace gctl
*/
void set_tag_symbol(char tag_sym){tag_sym_ = tag_sym;}
/**
* @brief
*
* @return
*/
int head_number(){return head_num_;}
/**
* @brief
*
* @return
*/
int row_number(){return row_num_;}
/**
* @brief
*
* @return
*/
int col_number(){return col_num_;}
/**
* @brief
*
* @note load_text函数才会使用lines
*
* @return
*/
const std::vector<std::string>& get_lines(){return lines_;}
/**
* @brief
*
* @return
*/
const std::vector<std::string>& get_head_records(){return heads_;}
/**
* @brief
*
* @return
*/
const std::vector<std::string>& get_annotoations(){return annotates_;}
/**
* @brief
*
* @return
*/
const std::vector<std::string>& get_tags(){return tags_;}
/**
* @brief
*
* @return
*/
const std::vector<std::string>& get_column_names(){return col_names_;}
/**
* @brief
*
* @param heads
*/
void set_head_records(const std::vector<std::string> &heads);
/**
* @brief
*
* @param att
*/
void set_annotoations(const std::vector<std::string> &att);
/**
* @brief
*
* @param tags
*/
void set_tags(const std::vector<std::string> &tags);
/**
* @brief
*
* @param names
*/
void set_column_names(const std::vector<std::string> &names);
/**
* @brief
*
@ -133,13 +232,27 @@ namespace gctl
void load_text(std::string filename, std::string file_exten = ".txt", text_head_type_e t = NoColumnName);
/**
* @brief
* @brief CSV文件
*
* @param filename
*/
void load_csv(std::string filename, text_head_type_e t = HasColumnName);
/**
* @brief
*
* @param filename
* @param file_exten
*/
void save_text(std::string filename, std::string file_exten = ".txt");
/**
* @brief CSV文件
*
* @param filename
*/
void save_csv(std::string filename);
/**
* @brief
*
@ -149,11 +262,13 @@ namespace gctl
void init_table(int row, int col);
/**
* @brief
* @brief
*
* @param row
* @param col
* @param names
*/
void set_column_name(const std::vector<std::string> &names);
void init_table(int row, int col, const std::vector<std::string> &names);
/**
* @brief
@ -164,6 +279,15 @@ namespace gctl
*/
template <typename T> void fill_column(int idx, const array<T> &data);
/**
* @brief
*
* @tparam T
* @param name
* @param data
*/
template <typename T> void fill_column(std::string name, const array<T> &data);
/**
* @brief
*
@ -182,6 +306,15 @@ namespace gctl
*/
template <typename T> void get_column(int idx, array<T> &data);
/**
* @brief
*
* @tparam T
* @param name
* @param data
*/
template <typename T> void get_column(std::string name, array<T> &data);
/**
* @brief
*
@ -190,6 +323,42 @@ namespace gctl
* @param data
*/
template <typename T> void get_row(int idx, array<T> &data);
/**
* @brief
*
* @tparam T
* @param r
* @param c
* @return T
*/
template <typename T> T cell(int r, int c){return table_[r][c].value<T>();}
/**
* @brief
*
* @tparam T
* @param r
* @param c
* @param d
*/
template <typename T> void cell(int r, int c, T d){table_[r][c].value(d); return;}
/**
* @brief double类型专用
*
* @param r
* @param c
* @param d
* @param p
*/
void cell(int r, int c, double d, int p);
/**
* @brief
*
*/
void info();
};
template <typename T>
@ -207,6 +376,27 @@ namespace gctl
return;
}
template <typename T>
void text_content::fill_column(std::string name, const array<T> &data)
{
if (col_names_.empty())
{
throw std::runtime_error("[gctl::text_content::fill_column] No column names instored.");
}
for (size_t i = 0; i < col_names_.size(); i++)
{
if (col_names_[i] == name)
{
fill_column(i, data);
return;
}
}
throw std::runtime_error("[gctl::text_content::fill_column] No column found by the input name.");
return;
}
template <typename T>
void text_content::fill_row(int idx, const array<T> &data)
{
@ -238,6 +428,27 @@ namespace gctl
return;
}
template <typename T>
void text_content::get_column(std::string name, array<T> &data)
{
if (col_names_.empty())
{
throw std::runtime_error("[gctl::text_content::get_column] No column names instored.");
}
for (size_t i = 0; i < col_names_.size(); i++)
{
if (col_names_[i] == name)
{
get_column(i, data);
return;
}
}
throw std::runtime_error("[gctl::text_content::get_column] No column found by the input name.");
return;
}
template <typename T>
void text_content::get_row(int idx, array<T> &data)
{