This commit is contained in:
张壹 2025-02-11 22:20:51 +08:00
parent b184ac95fb
commit fc34ed3e2c
4 changed files with 101 additions and 15 deletions

View File

@ -29,7 +29,6 @@
gctl::dsv_io::dsv_io()
{
file_ = "";
att_sym_ = '#';
tag_sym_ = '!';
deli_sym_ = ' ';
@ -45,7 +44,6 @@ gctl::dsv_io::~dsv_io()
gctl::dsv_io::dsv_io(std::string filename, std::string file_exten, int t)
{
file_ = "";
att_sym_ = '#';
tag_sym_ = '!';
deli_sym_ = ' ';
@ -59,7 +57,6 @@ gctl::dsv_io::dsv_io(std::string filename, std::string file_exten, int t)
void gctl::dsv_io::clear()
{
file_ = "";
att_sym_ = '#';
tag_sym_ = '!';
deli_sym_ = ' ';
@ -210,8 +207,6 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, int t
std::ifstream infile;
open_infile(infile, filename, file_exten);
file_ = filename + file_exten;
int h = 0;
std::string tmp_line;
std::vector<std::string> lines;
@ -398,10 +393,9 @@ void gctl::dsv_io::init_table(int row, int col)
void gctl::dsv_io::info(int t)
{
std::clog << "File: " << file_ << "\n------------\n";
std::clog << "Head(s): " << head_num_ << "\n";
if (t & HeadInfo)
{
std::clog << "Head(s): " << head_num_ << "\n";
for (size_t i = 0; i < heads_.size(); i++)
{
std::clog << heads_[i] << "\n";
@ -409,9 +403,9 @@ void gctl::dsv_io::info(int t)
std::clog << "------------\n";
}
std::clog << "Annotation(s): " << annotates_.size() << "\n";
if (t & AttInfo)
{
std::clog << "Annotation(s): " << annotates_.size() << "\n";
for (size_t i = 0; i < annotates_.size(); i++)
{
std::clog << annotates_[i] << "\n";
@ -419,16 +413,15 @@ void gctl::dsv_io::info(int t)
std::clog << "------------\n";
}
std::clog << "Tag(s): " << tags_.size() << "\n";
if (t & TagInfo)
{
std::clog << "Tag(s): " << tags_.size() << "\n";
for (size_t i = 0; i < tags_.size(); i++)
{
std::clog << tags_[i] << "\n";
}
std::clog << "------------\n";
}
else std::clog << "------------\n";
if (t & ColInfo)
{
@ -969,7 +962,6 @@ gctl::geodsv_io::~geodsv_io(){}
gctl::geodsv_io::geodsv_io(std::string filename, std::string file_exten, int t)
{
file_ = "";
att_sym_ = '#';
tag_sym_ = '!';
deli_sym_ = ' ';

View File

@ -140,8 +140,6 @@ namespace gctl
class dsv_io
{
protected:
// 文件名
std::string file_;
// 头信息行数 表格行数(不包括表头) 表格列数(不包括表头)
int head_num_, row_num_, col_num_;
// 注释行起始符 标记行起始符 分割符

View File

@ -221,7 +221,7 @@ void info(const std::vector<std::string> &cmd_units)
std::regex pata("att"), patt("tag"), patc("col"), patr("row"), path("hdr");
// info [att|tag|hdr|col|row]
int info_code;
int info_code = 0;
if (cmd_units.size() > 1)
{
if (regex_search(cmd_units[1], ret, pata)) info_code = AttInfo;
@ -389,6 +389,94 @@ void statistic(const std::vector<std::string> &cmd_units)
return;
}
void set_titles(const std::vector<std::string> &cmd_units)
{
// title <row|col> <t1>,<t2>,<t3>,... [<id1>,<id2>,<id3>,...]
if (cmd_units.size() < 3) throw std::runtime_error("title: insufficient parameters.");
if (cmd_units[1] == "row")
{
std::vector<std::string> row_titles;
std::vector<int> row_ids;
parse_string_to_vector(cmd_units[2], ',', row_titles);
if (cmd_units.size() >= 4)
{
parse_string_to_vector(cmd_units[3], ',', row_ids);
if (row_titles.size() != row_ids.size()) throw std::runtime_error("title: invalid parameters.");
tc.row_names(row_titles, row_ids);
}
else tc.row_names(row_titles);
}
else if (cmd_units[1] == "col")
{
std::vector<std::string> col_titles;
std::vector<int> col_ids;
parse_string_to_vector(cmd_units[2], ',', col_titles);
if (cmd_units.size() >= 4)
{
parse_string_to_vector(cmd_units[3], ',', col_ids);
if (col_titles.size()!= col_ids.size()) throw std::runtime_error("title: invalid parameters.");
tc.column_names(col_titles, col_ids);
}
else tc.column_names(col_titles);
}
else throw std::runtime_error("title: invalid parameters.");
return;
}
void math_func(const std::vector<std::string> &cmd_units)
{
// math <func> <col1>,<col2>,<col3>...
if (cmd_units.size() < 3) throw std::runtime_error("math: insufficient parameters.");
std::vector<std::string> col_names;
parse_string_to_vector(cmd_units[2], ',', col_names);
tc.cal_column(cmd_units[1], col_names, 12);
return;
}
void rand_data(const std::vector<std::string> &cmd_units)
{
// random normal|uniform <p1> <p2> [<colname>]
if (cmd_units.size() < 4) throw std::runtime_error("random: insufficient parameters.");
random_type_e rd_type = RdNormal;
if (cmd_units[1] == "normal") rd_type = RdNormal;
else if (cmd_units[1] == "uniform") rd_type = RdUniform;
else throw std::runtime_error("random: invalid parameters.");
double p1 = atof(cmd_units[2].c_str());
double p2 = atof(cmd_units[3].c_str());
array<double> rd_data(tc.row_number());
rd_data.random_float(p1, p2, rd_type);
std::string col_name = "RdData";
if (cmd_units.size() >= 5) col_name = cmd_units[4];
tc.add_column(col_name);
tc.fill_column(rd_data, col_name, 12);
return;
}
void filt_data(const std::vector<std::string> &cmd_units)
{
// filter <out-table> <expression> <cdn1>,<cdn2>,<cdn3>... <out1>,<out2>,<out3>...
if (cmd_units.size() < 5) throw std::runtime_error("filter: insufficient parameters.");
std::vector<std::string> tar_names, out_names;
parse_string_to_vector(cmd_units[3], ',', tar_names);
parse_string_to_vector(cmd_units[4], ',', out_names);
dsv_io out_table;
if (tar_names.size() == 1) tc.filt_column(cmd_units[2], tar_names[0], out_names, out_table);
else tc.filt_column(cmd_units[2], tar_names, out_names, out_table);
std::string naked_name, exten_name;
parse_filename(cmd_units[1], naked_name, exten_name);
if (exten_name == ".csv") out_table.save_csv(cmd_units[1]);
else out_table.save_text(naked_name, exten_name);
return;
}
int main(int argc, char *argv[])
{
if (argc >= 2)

View File

@ -53,8 +53,12 @@ void save_file(const std::vector<std::string> &cmd_units);
void statistic(const std::vector<std::string> &cmd_units);
void set_enable(const std::vector<std::string> &cmd_units);
void set_disable(const std::vector<std::string> &cmd_units);
void set_titles(const std::vector<std::string> &cmd_units);
void math_func(const std::vector<std::string> &cmd_units);
void rand_data(const std::vector<std::string> &cmd_units);
void filt_data(const std::vector<std::string> &cmd_units);
#define CMD_NUM 10
#define CMD_NUM 14
const cmd_pair commands[CMD_NUM] = {
{"quit", quit, "Quit the program."},
{"info", info, "Show the table information."},
@ -65,6 +69,10 @@ const cmd_pair commands[CMD_NUM] = {
{"stats", statistic, "Calculate statistics of the selected columns."},
{"enable", set_enable, "Enable column/row outputs by name or index."},
{"disable", set_disable, "Disable column/row outputs by name or index."},
{"title", set_titles, "Set row and column titles."},
{"math", math_func, "Preform mathematic operations of column data."},
{"random", rand_data, "Generate random column data."},
{"filter", filt_data, "Filter column data by value."},
{"null", nullptr, "null"}
};