This commit is contained in:
张壹 2024-11-04 16:03:39 +08:00
parent a3f12cb964
commit d701c6234b
3 changed files with 96 additions and 23 deletions

View File

@ -32,15 +32,30 @@ using namespace gctl;
int main(int argc, char const *argv[]) try int main(int argc, char const *argv[]) try
{ {
/*
text_content tc("tmp/topo"); text_content tc("tmp/topo");
array<double> x(tc.contents_.size()); array<double> x(tc.lines_.size());
array<double> y(tc.contents_.size()); array<double> y(tc.lines_.size());
array<double> z(tc.contents_.size()); array<double> z(tc.lines_.size());
for (size_t i = 0; i < tc.contents_.size(); i++) for (size_t i = 0; i < tc.lines_.size(); i++)
{ {
parse_string_to_value(tc.contents_[i], ' ', true, x[i], y[i], z[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>();
} }
std::clog << x.back() << "," << y.back() << "," << z.back() << "\n"; std::clog << x.back() << "," << y.back() << "," << z.back() << "\n";

View File

@ -41,7 +41,7 @@ gctl::text_content::~text_content()
clear(); 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, bool unpack)
{ {
// 设置基础参数 // 设置基础参数
att_sym_ = '#'; att_sym_ = '#';
@ -49,7 +49,7 @@ gctl::text_content::text_content(std::string filename, std::string file_exten)
deli_sym_ = ' '; deli_sym_ = ' ';
head_num_ = 0; head_num_ = 0;
// 载入文本内容 // 载入文本内容
load_text(filename, file_exten); load_text(filename, file_exten, unpack);
} }
void gctl::text_content::set(char deli_sym, int h_num, char att_sym, char tag_sym) void gctl::text_content::set(char deli_sym, int h_num, char att_sym, char tag_sym)
@ -63,14 +63,15 @@ void gctl::text_content::set(char deli_sym, int h_num, char att_sym, char tag_sy
void gctl::text_content::clear() void gctl::text_content::clear()
{ {
heads_.clear(); destroy_vector(heads_);
annotates_.clear(); destroy_vector(annotates_);
tags_.clear(); destroy_vector(tags_);
contents_.clear(); destroy_vector(lines_);
destroy_vector(table_);
return; 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, bool unpack)
{ {
std::ifstream infile; std::ifstream infile;
open_infile(infile, filename, file_exten); open_infile(infile, filename, file_exten);
@ -82,7 +83,7 @@ void gctl::text_content::load_text(std::string filename, std::string file_exten)
for (int i = 0; i < head_num_; i++) for (int i = 0; i < head_num_; i++)
{ {
std::getline(infile, tmp_line); std::getline(infile, tmp_line);
heads_.push_back(tmp_line); if (!tmp_line.empty()) heads_.push_back(tmp_line);
} }
while (std::getline(infile, tmp_line)) while (std::getline(infile, tmp_line))
@ -101,14 +102,32 @@ void gctl::text_content::load_text(std::string filename, std::string file_exten)
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1); tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
tags_.push_back(tmp_line); tags_.push_back(tmp_line);
} }
else contents_.push_back(tmp_line); else if (!tmp_line.empty()) lines_.push_back(tmp_line);
} }
infile.close(); infile.close();
if (unpack)
{
table_.resize(lines_.size());
std::vector<std::string> tmp_cols;
for (size_t i = 0; i < lines_.size(); i++)
{
tmp_cols.clear();
parse_string_to_vector(lines_[i], deli_sym_, tmp_cols);
table_[i].resize(tmp_cols.size());
for (size_t j = 0; j < tmp_cols.size(); j++)
{
table_[i][j].str_ = tmp_cols[j];
}
}
}
return; return;
} }
void gctl::text_content::save_text(std::string filename, std::string file_exten) void gctl::text_content::save_text(std::string filename, std::string file_exten, bool packed)
{ {
std::ofstream outfile; std::ofstream outfile;
open_outfile(outfile, filename, file_exten); open_outfile(outfile, filename, file_exten);
@ -128,9 +147,24 @@ void gctl::text_content::save_text(std::string filename, std::string file_exten)
outfile << "# " << annotates_[i] << std::endl; outfile << "# " << annotates_[i] << std::endl;
} }
for (int i = 0; i < contents_.size(); i++) if (packed)
{ {
outfile << contents_[i] << std::endl; for (int i = 0; i < lines_.size(); i++)
{
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.close(); outfile.close();

View File

@ -33,14 +33,35 @@
namespace gctl namespace gctl
{ {
struct cell_content
{
std::string str_;
template <typename T> T value()
{
T out;
str2type(str_, out);
return out;
}
template <typename T> void value(const T &in)
{
str_ = std::to_string(in);
return;
}
};
struct text_content struct text_content
{ {
// 头信息行数 // 头信息行数
int head_num_; int head_num_;
// 注释行起始符 标记行起始符 分割符 // 注释行起始符 标记行起始符 分割符
char att_sym_, tag_sym_, deli_sym_; char att_sym_, tag_sym_, deli_sym_;
// 注释行 标记行 // 头信息行 注释行 标记行
std::vector<std::string> heads_, annotates_, tags_, contents_; std::vector<std::string> heads_, annotates_, tags_;
// 内容行与表格
std::vector<std::string> lines_;
std::vector<std::vector<cell_content> > table_;
/** /**
* @brief Construct a new text content object * @brief Construct a new text content object
@ -59,8 +80,9 @@ namespace gctl
* *
* @param filename * @param filename
* @param file_exten * @param file_exten
* @param unpack table_
*/ */
text_content(std::string filename, std::string file_exten = ".txt"); text_content(std::string filename, std::string file_exten = ".txt", bool unpack = false);
/** /**
* @brief * @brief
@ -83,16 +105,18 @@ namespace gctl
* *
* @param filename * @param filename
* @param file_exten * @param file_exten
* @param unpack table_
*/ */
void load_text(std::string filename, std::string file_exten = ".txt"); void load_text(std::string filename, std::string file_exten = ".txt", bool unpack = false);
/** /**
* @brief * @brief
* *
* @param filename * @param filename
* @param file_exten * @param file_exten
* @param packed 使lines_写入文件使tabl_写入文件
*/ */
void save_text(std::string filename, std::string file_exten = ".txt"); void save_text(std::string filename, std::string file_exten = ".txt", bool packed = true);
}; };
} }