From e44f34958ee816e03119c626a1843c36392362d9 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Tue, 4 Feb 2025 11:54:29 +0800 Subject: [PATCH] tmp --- lib/io.h | 2 +- lib/io/dsv_io.h | 21 +++++++++++++ tool/dsviewer/dsviewer.cpp | 61 ++++++++++++++++++++++++++++++++------ tool/dsviewer/dsviewer.h | 6 +++- tool/dsviewer/dsviewer.md | 23 +++++++++----- 5 files changed, 95 insertions(+), 18 deletions(-) diff --git a/lib/io.h b/lib/io.h index ae60207..3e5b4c6 100644 --- a/lib/io.h +++ b/lib/io.h @@ -28,6 +28,7 @@ #ifndef _GCTL_IO_H #define _GCTL_IO_H +#include "io/netcdf_io.h" #include "io/mesh_io.h" #include "io/native_io.h" #include "io/text_io.h" @@ -37,6 +38,5 @@ #include "io/tetgen_io.h" #include "io/triangle_io.h" #include "io/off_io.h" -#include "io/netcdf_io.h" #endif // _GCTL_IO_H \ No newline at end of file diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 8ed3893..5a41fd9 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -228,6 +228,13 @@ namespace gctl */ void set_delimeter(char deli_sym){deli_sym_ = deli_sym;} + /** + * @brief 获取列分隔符 + * + * @return 分隔符 + */ + char get_delimeter(){return deli_sym_;} + /** * @brief 设置头信息行数 * @@ -235,6 +242,13 @@ namespace gctl */ void set_head_number(char num){head_num_ = num;} + /** + * @brief 获取头信息行数 + * + * @return 行数 + */ + int get_head_number(){return head_num_;} + /** * @brief 设置注释行符号 * @@ -242,6 +256,13 @@ namespace gctl */ void set_annotation_symbol(char att_sym){att_sym_ = att_sym;} + /** + * @brief 获取注释行符号 + * + * @return 注释符号 + */ + char get_annotation_symbol(){return att_sym_;} + /** * @brief 设置标记行符号 * diff --git a/tool/dsviewer/dsviewer.cpp b/tool/dsviewer/dsviewer.cpp index 0157c98..c7dd42e 100644 --- a/tool/dsviewer/dsviewer.cpp +++ b/tool/dsviewer/dsviewer.cpp @@ -125,6 +125,16 @@ void display_cmds() } std::clog << "\nEnter \"?\" to see detailed instructions.\n"; + + std::clog << "\nFile:\n"; + std::clog << "Each line of the input file(s) will be parsed as a command.\nAny line starts with '#' will be skipped.\n"; + + std::clog << "\nHereDoc:\n"; + std::clog << "You can use HereDoc to input commands. A simple example is:\n"; + std::clog << "dsviewer << EOF\n"; + std::clog << "open file1.csv\n"; + std::clog << "info\n"; + std::clog << "EOF\n"; return; } @@ -217,6 +227,38 @@ void info(const std::vector &cmd_units) return; } +void head(const std::vector &cmd_units) +{ + // head [number] + int h = 10; + if (cmd_units.size() > 1) h = atoi(cmd_units[1].c_str()); + if (h <= 0) throw std::runtime_error("head: invalid number."); + + _1s_array line; + for (size_t i = 1; i <= h; i++) + { + tc.get_row(line, i); + line.show(std::cout, tc.get_delimeter()); + } + return; +} + +void tail(const std::vector &cmd_units) +{ + // tail [number] + int h = 10; + if (cmd_units.size() > 1) h = atoi(cmd_units[1].c_str()); + if (h <= 0) throw std::runtime_error("tail: invalid number."); + + _1s_array line; + for (size_t i = tc.row_number() - h + 1; i <= tc.row_number(); i++) + { + tc.get_row(line, i); + line.show(std::cout, tc.get_delimeter()); + } + return; +} + void set_enable(const std::vector &cmd_units) { // enable column|row ... @@ -269,7 +311,7 @@ void set_disable(const std::vector &cmd_units) void load_file(const std::vector &cmd_units) { - // load [nohead|hashead] [] [tag_sym] [att_sym] [head_num] + // load [nohead|column|row|both] [head_num] [] [att_sym] [tag_sym] if (cmd_units.size() < 2) throw std::runtime_error("open: insufficient parameters."); gctl::array copy_str(5, "null"); @@ -284,14 +326,14 @@ void load_file(const std::vector &cmd_units) else if (copy_str[0] == "row") ht = RowHead; else if (copy_str[0] == "both") ht = BothHead; - if (copy_str[1] != "null") tc.set_delimeter(copy_str[1][0]); - if (copy_str[2] != "null") tc.set_tag_symbol(copy_str[2][0]); - if (copy_str[3] != "null") tc.set_annotation_symbol(copy_str[3][0]); - int hnum = 0; - if (copy_str[4] != "null") hnum = atoi(copy_str[4].c_str()); + if (copy_str[1] != "null") hnum = atoi(copy_str[1].c_str()); if (hnum != 0) tc.set_head_number(hnum); + if (copy_str[2] != "null") tc.set_delimeter(copy_str[2][0]); + if (copy_str[3] != "null") tc.set_annotation_symbol(copy_str[3][0]); + if (copy_str[4] != "null") tc.set_tag_symbol(copy_str[4][0]); + std::string naked_name, exten_name; parse_filename(cmd_units[1], naked_name, exten_name); @@ -302,7 +344,7 @@ void load_file(const std::vector &cmd_units) void save_file(const std::vector &cmd_units) { - // save [] [tag_sym] [att_sym] + // save [] [att_sym] [tag_sym] if (cmd_units.size() < 2) throw std::runtime_error("save: insufficient parameters."); gctl::array copy_str(3, "null"); @@ -312,8 +354,8 @@ void save_file(const std::vector &cmd_units) } if (copy_str[0] != "null") tc.set_delimeter(copy_str[0][0]); - if (copy_str[1] != "null") tc.set_tag_symbol(copy_str[1][0]); - if (copy_str[2] != "null") tc.set_annotation_symbol(copy_str[2][0]); + if (copy_str[1] != "null") tc.set_annotation_symbol(copy_str[1][0]); + if (copy_str[2] != "null") tc.set_tag_symbol(copy_str[2][0]); std::string naked_name, exten_name; parse_filename(cmd_units[1], naked_name, exten_name); @@ -369,6 +411,7 @@ int main(int argc, char *argv[]) display_logo(); std::clog << "dsviewer - read, manipulate and write dsv/csv files.\n"; + std::clog << "Usage: dsviewer [ ...]\n"; std::clog << "Enter '?' to see all available commands.\n"; std::string cmd_str; diff --git a/tool/dsviewer/dsviewer.h b/tool/dsviewer/dsviewer.h index 751ed58..0ebacd4 100644 --- a/tool/dsviewer/dsviewer.h +++ b/tool/dsviewer/dsviewer.h @@ -45,16 +45,20 @@ struct cmd_pair void quit(const std::vector &cmd_units); void info(const std::vector &cmd_units); +void head(const std::vector &cmd_units); +void tail(const std::vector &cmd_units); void load_file(const std::vector &cmd_units); void save_file(const std::vector &cmd_units); void statistic(const std::vector &cmd_units); void set_enable(const std::vector &cmd_units); void set_disable(const std::vector &cmd_units); -#define CMD_NUM 8 +#define CMD_NUM 10 const cmd_pair commands[CMD_NUM] = { {"quit", quit, "Quit the program."}, {"info", info, "Show the table information."}, + {"head", head, "Show head line(s) of the table."}, + {"tail", tail, "Show tail line(s) of the table."}, {"open", load_file, "Open a dsv/csv file."}, {"save", save_file, "Save the table to a file."}, {"stats", statistic, "Calculate statistics of the selected columns."}, diff --git a/tool/dsviewer/dsviewer.md b/tool/dsviewer/dsviewer.md index 7f25865..c902f4c 100644 --- a/tool/dsviewer/dsviewer.md +++ b/tool/dsviewer/dsviewer.md @@ -1,14 +1,23 @@ #### quit Does what it says. -#### info +#### info [column|row|both] Show the table information. -#### open \ [hashead|nohead] [\] [\] [\] -Open a dsv/csv file. The default setups are 'nohead', space for delimeter, '#' for annotations, and '!' for tags. +#### head +Show the first `n` (the default is 10) rows of the table. -#### save \ -Save table to a dsv/csv file +#### tail +Show the last `n` (the default is 10) rows of the table. -#### stats \ \... -Show statistics of a data column or columns. \ No newline at end of file +#### open \ [nohead|column|row|both] [\] [\] [\] [\] +Open a dsv/csv file. The default setups are 'nohead', space for delimeter, '#' for annotations, and '!' for tags. If the file name ends with '.csv', the default delimeter is ','. + +#### save \ [\] [\] [\] +Save table to a dsv/csv file. The default setups are space for delimeter, '#' for annotations, and '!' for tags. If the file name ends with '.csv', the default delimeter is ','. + +#### stats \ \ ... +Show statistics of a data column or columns. If the input file has no column names, use inbuild names `C` to select the columns. + +#### enable column|row \ \ ... +Enable table outputs. If the input file has no row or column names, use inbuild names `R` and `C` to select the rows and columns. \ No newline at end of file