This commit is contained in:
张壹 2024-12-31 09:29:54 +08:00
parent 33e37c6b3e
commit 2e086c4b00
4 changed files with 156 additions and 109 deletions

View File

@ -32,25 +32,25 @@ using namespace gctl;
int main(int argc, char const *argv[]) try
{
/*
dsv_io tc;
tc.set_delimeter('|');
tc.load_text("tmp/world_data", ".txt", BothHead);
tc.info();
tc.info(BothHead);
//_1s_vector name = tc.get_row_names();
//display_vector(name);
_1s_array name;
tc.get_column("Name_s", name);
name.show(std::cout, '|');
//_1s_array name;
//tc.get_column("Name_s", name);
//name.show(std::cout, '|');
tc.get_row("AUS", name);
name.show(std::cout, ',');
//tc.get_row("AUS", name);
//name.show(std::cout, ',');
tc.save_csv("out");
*/
/*
geodsv_io tc;
tc.load_text("tmp/topo", ".txt", ColumnHead);
tc.cell(0, 1, std::string("x (m)"));
@ -78,7 +78,7 @@ int main(int argc, char const *argv[]) try
std::clog << std::setprecision(12) << tc.cell<double>(2, 1) << "\n";
tc.info();
*/
return 0;
}
catch(std::exception &e)

View File

@ -74,6 +74,46 @@ void gctl::dsv_io::clear()
return;
}
void gctl::dsv_io::get_row_names(std::vector<std::string> &names)
{
names.resize(row_num_);
for (size_t i = 1; i < row_num_; i++)
{
names[i] = table_[i][0].str_;
}
return;
}
void gctl::dsv_io::get_column_names(std::vector<std::string> &names)
{
names.resize(col_num_);
for (size_t i = 1; i < col_num_; i++)
{
names[i] = table_[0][i].str_;
}
return;
}
void gctl::dsv_io::set_row_names(const std::vector<std::string> &names)
{
for (size_t i = 1; i <= std::min(row_num_, (int) names.size()); i++)
{
table_[i][0].str_ = names[i];
}
table_[0][0].str_ = "row_name";
return;
}
void gctl::dsv_io::set_column_names(const std::vector<std::string> &names)
{
for (size_t i = 1; i <= std::min(col_num_, (int) names.size()); i++)
{
table_[0][i].str_ = names[i];
}
return;
}
void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table_headtype_e t)
{
std::ifstream infile;
@ -145,7 +185,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table
}
std::vector<table_cell> empty_line;
if (t == NoHead)
if (t == NoHead) // 无表头 需要补齐空白的列头和行头
{
row_num_ = table_.size();
col_num_ = table_[0].size();
@ -159,7 +199,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table
}
}
if (t == ColumnHead)
if (t == ColumnHead) // 有列头 需要补齐空白的行头
{
row_num_ = table_.size() - 1;
col_num_ = table_[0].size();
@ -170,7 +210,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table
}
}
if (t == RowHead)
if (t == RowHead) // 有行头 需要补齐空白的列头
{
row_num_ = table_.size();
col_num_ = table_[0].size() - 1;
@ -179,7 +219,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table
table_[0].resize(col_num_ + 1);
}
if (t == BothHead)
if (t == BothHead) // 有行头和列头
{
row_num_ = table_.size() - 1;
col_num_ = table_[0].size() - 1;
@ -216,6 +256,7 @@ void gctl::dsv_io::save_text(std::string filename, std::string file_exten)
outfile << "# " << annotates_[i] << std::endl;
}
// 单独处理第一行 即列头
bool line_st = false;
if (table_[0][0].out_ok_ && table_[0][0].str_ != "")
{
@ -235,10 +276,12 @@ void gctl::dsv_io::save_text(std::string filename, std::string file_exten)
if (line_st) outfile << std::endl;
// 处理余下的行
for (int i = 1; i <= row_num_; i++)
{
line_st = false;
// 单独处理第一列 即行头
if (table_[i][0].out_ok_ && table_[i][0].str_ != "")
{
outfile << table_[i][0].str_;
@ -273,6 +316,7 @@ void gctl::dsv_io::init_table(int row, int col, table_headtype_e t)
row_num_ = row;
col_num_ = col;
// 初始的列头和行头均为空白
table_.resize(row_num_ + 1);
for (size_t i = 0; i < row_num_ + 1; i++)
{
@ -281,53 +325,17 @@ void gctl::dsv_io::init_table(int row, int col, table_headtype_e t)
return;
}
void gctl::dsv_io::set_head_records(const std::vector<std::string> &heads)
{
heads_ = heads;
head_num_ = heads_.size();
return;
}
void gctl::dsv_io::set_annotoations(const std::vector<std::string> &att)
{
annotates_ = att;
return;
}
void gctl::dsv_io::set_tags(const std::vector<std::string> &tags)
{
tags_ = tags;
return;
}
void gctl::dsv_io::set_row_names(const std::vector<std::string> &names)
{
for (size_t i = 1; i <= std::min(row_num_, (int) names.size()); i++)
{
table_[i][0].str_ = names[i];
}
table_[0][0].str_ = "row_name";
return;
}
void gctl::dsv_io::set_column_names(const std::vector<std::string> &names)
{
for (size_t i = 1; i <= std::min(col_num_, (int) names.size()); i++)
{
table_[0][i].str_ = names[i];
}
return;
}
void gctl::dsv_io::info()
void gctl::dsv_io::info(table_headtype_e t)
{
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";
std::clog << "------------\n";
if (t == ColumnHead || t == BothHead)
{
std::clog << "Columns:\n";
for (size_t i = 1; i <= col_num_; i++)
{
if (table_[0][i].str_ != "")
@ -343,11 +351,33 @@ void gctl::dsv_io::info()
std::clog << std::endl;
}
std::clog << "============\n";
}
if (t == RowHead || t == BothHead)
{
std::clog << "Rows:\n";
for (size_t i = 1; i <= row_num_; i++)
{
if (table_[i][0].str_ != "")
{
std::clog << table_[i][0].str_ << ": " << table_[i][1].str_ << " -> " << table_[i][col_num_].str_;
}
else
{
std::clog << "R" + std::to_string(i) << ": " << table_[i][1].str_ << " -> " << table_[i][col_num_].str_;
}
if (!table_[i][0].out_ok_) std::clog << " (No output)";
std::clog << std::endl;
}
std::clog << "============\n";
}
return;
}
int gctl::dsv_io::name_index(std::string name, bool iter_row)
{
// 拾取行号或列号 格式为R<id>和C<id>
std::smatch ret;
std::regex patr("R(\\d*)"), patc("C(\\d*)");
if (regex_search(name, ret, patr))

View File

@ -134,32 +134,10 @@ namespace gctl
dsv_io(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead);
/**
* @brief
* @brief
*
* @param deli_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;}
void clear();
/**
* @brief
@ -203,26 +181,68 @@ namespace gctl
*/
const std::vector<std::string>& get_tags(){return tags_;}
/**
* @brief
*
* @param names
*/
void get_row_names(std::vector<std::string> &names);
/**
* @brief
*
* @param names
*/
void get_column_names(std::vector<std::string> &names);
/**
* @brief
*
* @param deli_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
*
* @param heads
*/
void set_head_records(const std::vector<std::string> &heads);
void set_head_records(const std::vector<std::string> &heads){heads_ = heads; head_num_ = heads_.size();}
/**
* @brief
*
* @param att
*/
void set_annotoations(const std::vector<std::string> &att);
void set_annotoations(const std::vector<std::string> &att){annotates_ = att;}
/**
* @brief
*
* @param tags
*/
void set_tags(const std::vector<std::string> &tags);
void set_tags(const std::vector<std::string> &tags){tags_ = tags;}
/**
* @brief
@ -238,12 +258,6 @@ namespace gctl
*/
void set_column_names(const std::vector<std::string> &names);
/**
* @brief
*
*/
void clear();
/**
* @brief
*
@ -286,15 +300,16 @@ namespace gctl
/**
* @brief
*
* @param t
*/
void info();
void info(table_headtype_e t = ColumnHead);
/**
* @brief name的行或列的索引
* @brief name和R<id>C<id>
*
* @param name R<id>C<id>
* @param iter_row
* @return 1
* @param name R<id>C<id>
* @param iter_row name参数为R<id>C<id>
* @return 1 -1
*/
int name_index(std::string name, bool iter_row = false);

View File

@ -207,11 +207,13 @@ void quit(const std::vector<std::string> &cmd_units)
void info(const std::vector<std::string> &cmd_units)
{
if (cmd_units.size() == 1) // cmd_units[0] == info
// info [column|row|both]
if (cmd_units.size() > 1)
{
tc.info();
return;
if (cmd_units[1] == "row") tc.info(RowHead);
if (cmd_units[1] == "both") tc.info(BothHead);
}
else tc.info();
return;
}