update dsv_io

This commit is contained in:
张壹 2025-02-13 10:10:19 +08:00
parent 2802086a76
commit fe46cc7aa0
5 changed files with 40 additions and 24 deletions

View File

@ -36,15 +36,16 @@ int main(int argc, char const *argv[]) try
tc.delimeter('|');
tc.head_number(1);
tc.load_text("tmp/world_data", ".txt", ColHead|RowHead);
tc.info(AttInfo|HeadInfo|TagInfo|ColInfo);
tc.info(AttInfo|HeadInfo|TagInfo);
tc.filter("America", "Continent_s", ColHead);
tc.save_text("out");
//tc.filter("America", "BLZ", RowHead);
//tc.save_text("out");
dsv_io tc2 = tc.export_table();
tc2.head_records(tc.head_records());
//tc2.head_records(tc.head_records());
tc2.delimeter('|');
tc2.save_text("out2");
tc2.save_text("out");
/*
geodsv_io tc;

View File

@ -530,7 +530,7 @@ namespace gctl
*
* @param cnd_str
* @param cnd_tar
* @param thead RowHead表示按列过滤ColHead时表示按行过滤
* @param thead RowHead表示名称为行头则按列过滤ColHead时表示名称为列头则按行过滤
*/
void filter(std::string cnd_str, std::string cnd_tar, table_headtype_e thead = RowHead);
@ -569,8 +569,8 @@ namespace gctl
* 使strtk库的相关内容使
*
* @param cnd_str
* @param cnd_tars
* @param thead RowHead表示按列过滤ColHead时表示按行过滤
* @param cnd_tars
* @param thead RowHead表示名称为行头则按列过滤ColHead时表示名称为列头则按行过滤
*/
void filter(std::string cnd_str, const std::vector<std::string> &cnd_tars, table_headtype_e thead = RowHead);

View File

@ -93,7 +93,7 @@ char *command_generator(const char *text, int state)
* contents of rl_line_buffer in case we want to do some simple
* parsing. Return the array of matches, or NULL if there aren't any.
*/
char **gridmanager_completion(const char *text, int start, int end)
char **dsviewer_completion(const char *text, int start, int end)
{
char **matches = nullptr;
@ -108,10 +108,10 @@ char **gridmanager_completion(const char *text, int start, int end)
void initialize_readline(void)
{
/* Allow conditional parsing of the ~/.inputrc file. */
rl_readline_name = "gridmanager";
rl_readline_name = "dsviewer";
/* Tell the completer that we want a crack first. */
rl_attempted_completion_function = gridmanager_completion;
rl_attempted_completion_function = dsviewer_completion;
}
} // End C section
@ -126,7 +126,7 @@ void display_cmds()
std::clog << "\nEnter \"<command>?\" to see detailed instructions.\n";
std::clog << "\nFile:\n";
std::clog << "\nCmdFile:\n";
std::clog << "Each line of the input file(s) will be parsed as a command.\nEmpty lines and lines start with '#' will be skipped.\n";
std::clog << "\nHereDoc:\n";
@ -450,28 +450,31 @@ void rand_data(const std::vector<std::string> &cmd_units)
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);
if (cmd_units.size() >= 5) tc.fill_column(rd_data, cmd_units[4], 12);
else
{
std::string col_name = "RdData";
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 row|col <expression> <cdn1>,<cdn2>,<cdn3>...
if (cmd_units.size() < 4) throw std::runtime_error("filter: insufficient parameters.");
// filter row|col regex|math <expression> <name1>,<name2>,<name3>...
if (cmd_units.size() < 5) throw std::runtime_error("filter: insufficient parameters.");
std::vector<std::string> tar_names;
parse_string_to_vector(cmd_units[3], ',', tar_names);
parse_string_to_vector(cmd_units[4], ',', tar_names);
table_headtype_e thead;
if (cmd_units[1] == "row") thead = RowHead;
else if (cmd_units[1] == "col") thead = ColHead;
else throw std::runtime_error("filter: invalid parameters.");
if (tar_names.size() == 1) tc.filter(cmd_units[2], tar_names[0], thead);
else tc.filter(cmd_units[2], tar_names, thead);
if (cmd_units[2] == "regex") tc.filter(cmd_units[3], tar_names[0], thead);
else if (cmd_units[2] == "math") tc.filter(cmd_units[3], tar_names, thead);
else throw std::runtime_error("filter: invalid parameters.");
return;
}
@ -505,7 +508,7 @@ int main(int argc, char *argv[])
display_logo();
std::clog << "dsviewer - read, manipulate and write dsv/csv files.\n";
std::clog << "Usage: dsviewer [<file1> <file2>...]\n";
std::clog << "Usage: dsviewer [<cmdfile1> <cmdfile2>...]\n";
std::clog << "Enter '?' to see all available commands.\n";
std::string cmd_str;

View File

@ -72,7 +72,7 @@ const cmd_pair commands[CMD_NUM] = {
{"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."},
{"filter", filt_data, "Filter row and column data using regex and mathematic expressions."},
{"null", nullptr, "null"}
};

View File

@ -23,4 +23,16 @@ Show statistics of a data column or columns. If the input file has no column nam
Enable table outputs. If the input file has no row or column names, use inbuild names `R<id>` and `C<id>` to select the rows and columns.
#### disable column|row \<name1\> \<name2\> ...
Disable table outputs. If the input file has no row or column names, use inbuild names `R<id>` and `C<id>` to select the rows and columns.
Disable table outputs. If the input file has no row or column names, use inbuild names `R<id>` and `C<id>` to select the rows and columns.
#### title row|col \<t1\>,\<t2\>,\<t3\>,... [\<id1\>,\<id2\>,\<id3\>,...]
Set row and column titles. The titles will be assigned sequentially if no index is specified.
#### math \<func\> \<col1\>,\<col2\>,\<col3\>...
Preform mathematic operations of column data. The user needs to write a mathematical expression using the column names as variables. Then provide the column names as the arguments, in which the resultant column's name must be placed at the beginning. For example, to calculate the sum of column `C1` and `C2`, and store the result in column `C3`, the command is: `math C3=C1+C2 C3,C1,C2`.
#### random normal|uniform \<p1\> \<p2\> [\<colname\>]
Generate random data using normal or uniform distribution. The first parameter is the mean or the lower bound, and the second parameter is the standard deviation or the upper bound. If no column name is provided, a new column will be created (titled "RdData").
#### filter row|col regex|math \<expression\> \<name1\>,\<name2\>,\<name3\>...
Filter row and column data using regex and mathematic expressions. The first parameter is the expression, and the following parameters are the column names. For example, to filter rows whose column `C1` is greater than 0, the command is: `filter col math C1>0 C1`. Regex expression can be used to filter rows whose column `C1` contains the string "abc", the command is: `filter col regex abc C1`.