This commit is contained in:
张壹 2024-12-12 16:55:06 +08:00
parent a6ed09e850
commit 93604a89ae
2 changed files with 78 additions and 41 deletions

View File

@ -34,6 +34,8 @@ gctl::text_content::text_content()
tag_sym_ = '!';
deli_sym_ = ' ';
head_num_ = 0;
row_num_ = 0;
col_num_ = 0;
}
gctl::text_content::~text_content()
@ -41,15 +43,17 @@ gctl::text_content::~text_content()
clear();
}
gctl::text_content::text_content(std::string filename, std::string file_exten, bool unpack)
gctl::text_content::text_content(std::string filename, std::string file_exten)
{
// 设置基础参数
att_sym_ = '#';
tag_sym_ = '!';
deli_sym_ = ' ';
head_num_ = 0;
row_num_ = 0;
col_num_ = 0;
// 载入文本内容
load_text(filename, file_exten, unpack);
load_text(filename, file_exten);
}
void gctl::text_content::set(char deli_sym, int h_num, char att_sym, char tag_sym)
@ -71,7 +75,7 @@ void gctl::text_content::clear()
return;
}
void gctl::text_content::load_text(std::string filename, std::string file_exten, bool unpack)
void gctl::text_content::load_text(std::string filename, std::string file_exten)
{
std::ifstream infile;
open_infile(infile, filename, file_exten);
@ -108,30 +112,42 @@ void gctl::text_content::load_text(std::string filename, std::string file_exten,
}
else lines_.push_back(tmp_line);
}
infile.close();
if (unpack)
row_num_ = lines_.size();
table_.resize(row_num_);
int cn;
std::vector<std::string> tmp_cols;
for (size_t i = 0; i < row_num_; i++)
{
table_.resize(lines_.size());
tmp_cols.clear();
parse_string_to_vector(lines_[i], deli_sym_, tmp_cols);
std::vector<std::string> tmp_cols;
for (size_t i = 0; i < lines_.size(); i++)
cn = tmp_cols.size();
col_num_ = std::max(cn, col_num_);
table_[i].resize(tmp_cols.size());
for (size_t j = 0; j < tmp_cols.size(); j++)
{
tmp_cols.clear();
parse_string_to_vector(lines_[i], deli_sym_, tmp_cols);
table_[i][j].str_ = tmp_cols[j];
}
}
table_[i].resize(tmp_cols.size());
for (size_t j = 0; j < tmp_cols.size(); j++)
{
table_[i][j].str_ = tmp_cols[j];
}
cell_content empty_cell;
empty_cell.str_ = "";
for (size_t i = 0; i < row_num_; i++)
{
cn = table_[i].size();
for (size_t j = cn; j < col_num_; j++)
{
table_[i].push_back(empty_cell);
}
}
return;
}
void gctl::text_content::save_text(std::string filename, std::string file_exten, bool packed)
void gctl::text_content::save_text(std::string filename, std::string file_exten)
{
std::ofstream outfile;
open_outfile(outfile, filename, file_exten);
@ -151,26 +167,36 @@ void gctl::text_content::save_text(std::string filename, std::string file_exten,
outfile << "# " << annotates_[i] << std::endl;
}
if (packed)
for (int i = 0; i < row_num_; i++)
{
for (int i = 0; i < lines_.size(); i++)
outfile << table_[i][0].str_;
for (size_t j = 1; j < col_num_; j++)
{
outfile << lines_[i] << std::endl;
}
}
else
{
for (int i = 0; i < table_.size(); i++)
{
outfile << table_[i][0].str_;
for (size_t j = 1; j < table_[i].size(); j++)
{
outfile << deli_sym_ << table_[i][j].str_;
}
outfile << std::endl;
outfile << deli_sym_ << table_[i][j].str_;
}
outfile << std::endl;
}
outfile.close();
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_ = "";
}
}
return;
}

View File

@ -53,8 +53,8 @@ namespace gctl
struct text_content
{
// 头信息行数
int head_num_;
// 头信息行数 表格行数 表格列数
int head_num_, row_num_, col_num_;
// 注释行起始符 标记行起始符 分割符
char att_sym_, tag_sym_, deli_sym_;
// 头信息行 注释行 标记行
@ -80,9 +80,8 @@ namespace gctl
*
* @param filename
* @param file_exten
* @param unpack table_
*/
text_content(std::string filename, std::string file_exten = ".txt", bool unpack = false);
text_content(std::string filename, std::string file_exten = ".txt");
/**
* @brief
@ -105,19 +104,31 @@ namespace gctl
*
* @param filename
* @param file_exten
* @param unpack table_
*/
void load_text(std::string filename, std::string file_exten = ".txt", bool unpack = false);
void load_text(std::string filename, std::string file_exten = ".txt");
/**
* @brief
*
* @param filename
* @param file_exten
* @param packed 使lines_写入文件使tabl_写入文件
*/
void save_text(std::string filename, std::string file_exten = ".txt", bool packed = true);
void save_text(std::string filename, std::string file_exten = ".txt");
void init_table(int row, int col);
template <typename T> void fill_column(int idx, const array<T> &data);
};
template <typename T>
void text_content::fill_column<T>(int idx, const array<T> &data)
{
for (size_t i = 0; i < row_num_; i++)
{
table_[i][idx].value(data[i]);
}
return;
}
}
#endif //_GCTL_TEXT_IO2_H