tmp
This commit is contained in:
parent
33e37c6b3e
commit
2e086c4b00
@ -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)
|
||||
|
@ -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_;
|
||||
@ -272,7 +315,8 @@ 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,73 +325,59 @@ 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";
|
||||
|
||||
for (size_t i = 1; i <= col_num_; i++)
|
||||
if (t == ColumnHead || t == BothHead)
|
||||
{
|
||||
if (table_[0][i].str_ != "")
|
||||
std::clog << "Columns:\n";
|
||||
for (size_t i = 1; i <= col_num_; i++)
|
||||
{
|
||||
std::clog << table_[0][i].str_ << ": " << table_[1][i].str_ << " -> " << table_[row_num_][i].str_;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::clog << "C" + std::to_string(i) << ": " << table_[1][i].str_ << " -> " << table_[row_num_][i].str_;
|
||||
}
|
||||
if (table_[0][i].str_ != "")
|
||||
{
|
||||
std::clog << table_[0][i].str_ << ": " << table_[1][i].str_ << " -> " << table_[row_num_][i].str_;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::clog << "C" + std::to_string(i) << ": " << table_[1][i].str_ << " -> " << table_[row_num_][i].str_;
|
||||
}
|
||||
|
||||
if (!table_[0][i].out_ok_) std::clog << " (No output)";
|
||||
std::clog << std::endl;
|
||||
if (!table_[0][i].out_ok_) std::clog << " (No output)";
|
||||
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";
|
||||
}
|
||||
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))
|
||||
|
@ -134,32 +134,10 @@ namespace gctl
|
||||
dsv_io(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead);
|
||||
|
||||
/**
|
||||
* @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 清理字符串向量对象
|
||||
*
|
||||
*/
|
||||
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);
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user