This commit is contained in:
张壹 2024-12-28 13:47:25 +08:00
parent ff2732a9be
commit 676c665619
5 changed files with 93 additions and 35 deletions

View File

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

View File

@ -293,17 +293,17 @@ void gctl::dsv_io::info()
for (size_t i = 0; i < col_num_; i++) for (size_t i = 0; i < col_num_; i++)
{ {
// skip disabled columns
if (!bool_table_[0][i]) continue;
if (thead_ == ColumnHead || thead_ == BothHead) if (thead_ == ColumnHead || thead_ == BothHead)
{ {
std::clog << table_[0][i].str_ << ": " << table_[1][i].str_ << " -> " << table_[row_num_ - 1][i].str_ << "\n"; std::clog << table_[0][i].str_ << ": " << table_[1][i].str_ << " -> " << table_[row_num_ - 1][i].str_;
} }
else else
{ {
std::clog << "Col-" << std::to_string(i) << ": " << table_[0][i].str_ << " -> " << table_[row_num_ - 1][i].str_ << "\n"; std::clog << "Col-" << std::to_string(i) << ": " << table_[0][i].str_ << " -> " << table_[row_num_ - 1][i].str_;
} }
if (!bool_table_[0][i]) std::clog << " (No output)";
std::clog << std::endl;
} }
std::clog << "============\n"; std::clog << "============\n";
return; return;
@ -323,8 +323,7 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row)
if (table_[i][0].str_ == name) return i; if (table_[i][0].str_ == name) return i;
} }
throw gctl::runtime_error("[gctl::dsv_io] No row found by the input name: " + name); return -1;
return 0;
} }
else else
{ {
@ -338,12 +337,11 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row)
if (table_[0][i].str_ == name) return i; if (table_[0][i].str_ == name) return i;
} }
throw gctl::runtime_error("[gctl::dsv_io] No column found by the input name: " + name); return -1;
return 0;
} }
} }
void gctl::dsv_io::disable_column(int idx) void gctl::dsv_io::column_output(int idx, switch_type_e s)
{ {
if (idx >= col_num_) if (idx >= col_num_)
{ {
@ -352,18 +350,19 @@ void gctl::dsv_io::disable_column(int idx)
for (size_t i = 0; i < row_num_; i++) for (size_t i = 0; i < row_num_; i++)
{ {
bool_table_[i][idx] = false; if (s == Enable) bool_table_[i][idx] = true;
else bool_table_[i][idx] = false;
} }
return; return;
} }
void gctl::dsv_io::disable_column(std::string name) void gctl::dsv_io::column_output(std::string name, switch_type_e s)
{ {
disable_column(name_index(name)); column_output(name_index(name), s);
return; return;
} }
void gctl::dsv_io::disable_row(int idx) void gctl::dsv_io::row_output(int idx, switch_type_e s)
{ {
if (idx >= row_num_) if (idx >= row_num_)
{ {
@ -372,14 +371,15 @@ void gctl::dsv_io::disable_row(int idx)
for (size_t i = 0; i < col_num_; i++) for (size_t i = 0; i < col_num_; i++)
{ {
bool_table_[idx][i] = false; if (s == Enable) bool_table_[idx][i] = true;
else bool_table_[idx][i] = false;
} }
return; return;
} }
void gctl::dsv_io::disable_row(std::string name) void gctl::dsv_io::row_output(std::string name, switch_type_e s)
{ {
disable_row(name_index(name)); row_output(name_index(name), s);
return; return;
} }

View File

@ -276,32 +276,36 @@ namespace gctl
int name_index(std::string name, bool iter_row = false); int name_index(std::string name, bool iter_row = false);
/** /**
* @brief * @brief 使
* *
* @param idx * @param idx
* @param s
*/ */
void disable_column(int idx); void column_output(int idx, switch_type_e s = Disable);
/** /**
* @brief * @brief 使
* *
* @param name * @param name
* @param s
*/ */
void disable_column(std::string name); void column_output(std::string name, switch_type_e s = Disable);
/** /**
* @brief * @brief 使
* *
* @param idx * @param idx
* @param s
*/ */
void disable_row(int idx); void row_output(int idx, switch_type_e s = Disable);
/** /**
* @brief * @brief 使
* *
* @param name * @param name
* @param s
*/ */
void disable_row(std::string name); void row_output(std::string name, switch_type_e s = Disable);
/** /**
* @brief * @brief
@ -460,7 +464,7 @@ namespace gctl
template <typename T> template <typename T>
void dsv_io::fill_column(int idx, const array<T> &data, int p) void dsv_io::fill_column(int idx, const array<T> &data, int p)
{ {
if (idx >= col_num_) if (idx >= col_num_ || idx < 0)
{ {
throw std::runtime_error("[gctl::dsv_io] Invalid column index."); throw std::runtime_error("[gctl::dsv_io] Invalid column index.");
} }
@ -485,7 +489,7 @@ namespace gctl
template <typename T> template <typename T>
void dsv_io::fill_row(int idx, const array<T> &data, int p) void dsv_io::fill_row(int idx, const array<T> &data, int p)
{ {
if (idx >= row_num_) if (idx >= row_num_ || idx < 0)
{ {
throw std::runtime_error("[gctl::dsv_io] Invalid row index."); throw std::runtime_error("[gctl::dsv_io] Invalid row index.");
} }
@ -510,7 +514,7 @@ namespace gctl
template <typename T> template <typename T>
void dsv_io::get_column(int idx, array<T> &data) void dsv_io::get_column(int idx, array<T> &data)
{ {
if (idx >= col_num_) if (idx >= col_num_ || idx < 0)
{ {
throw std::runtime_error("[gctl::dsv_io] Invalid column index."); throw std::runtime_error("[gctl::dsv_io] Invalid column index.");
} }
@ -536,7 +540,7 @@ namespace gctl
template <typename T> template <typename T>
void dsv_io::get_row(int idx, array<T> &data) void dsv_io::get_row(int idx, array<T> &data)
{ {
if (idx >= row_num_) if (idx >= row_num_ || idx < 0)
{ {
throw std::runtime_error("[gctl::dsv_io] Invalid row index."); throw std::runtime_error("[gctl::dsv_io] Invalid row index.");
} }

View File

@ -215,6 +215,56 @@ void info(const std::vector<std::string> &cmd_units)
return; return;
} }
void set_enable(const std::vector<std::string> &cmd_units)
{
// enable column|row <column> <column>...
if (cmd_units.size() < 3) throw std::runtime_error("enable: insufficient parameters.");
if (cmd_units[1] == "column")
{
for (size_t i = 2; i < cmd_units.size(); i++)
{
if (tc.name_index(cmd_units[i]) < 0) tc.column_output(atoi(cmd_units[i].c_str()), Enable);
else tc.column_output(cmd_units[i], Enable);
}
}
else if (cmd_units[1] == "row")
{
for (size_t i = 2; i < cmd_units.size(); i++)
{
if (tc.name_index(cmd_units[i], true) < 0) tc.row_output(atoi(cmd_units[i].c_str()), Enable);
else tc.row_output(cmd_units[i], Enable);
}
}
else throw std::runtime_error("enable: invalid parameters.");
return;
}
void set_disable(const std::vector<std::string> &cmd_units)
{
// disable column|row <column> <column>...
if (cmd_units.size() < 3) throw std::runtime_error("disable: insufficient parameters.");
if (cmd_units[1] == "column")
{
for (size_t i = 2; i < cmd_units.size(); i++)
{
if (tc.name_index(cmd_units[i]) < 0) tc.column_output(atoi(cmd_units[i].c_str()), Disable);
else tc.column_output(cmd_units[i], Enable);
}
}
else if (cmd_units[1] == "row")
{
for (size_t i = 2; i < cmd_units.size(); i++)
{
if (tc.name_index(cmd_units[i], true) < 0) tc.row_output(atoi(cmd_units[i].c_str()), Disable);
else tc.row_output(cmd_units[i], Enable);
}
}
else throw std::runtime_error("disable: invalid parameters.");
return;
}
void load_file(const std::vector<std::string> &cmd_units) void load_file(const std::vector<std::string> &cmd_units)
{ {
// load <file> [nohead|hashead] [<delimeter>] [tag_sym] [att_sym] [head_num] // load <file> [nohead|hashead] [<delimeter>] [tag_sym] [att_sym] [head_num]

View File

@ -48,14 +48,18 @@ void info(const std::vector<std::string> &cmd_units);
void load_file(const std::vector<std::string> &cmd_units); void load_file(const std::vector<std::string> &cmd_units);
void save_file(const std::vector<std::string> &cmd_units); void save_file(const std::vector<std::string> &cmd_units);
void statistic(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);
#define CMD_NUM 6 #define CMD_NUM 8
const cmd_pair commands[CMD_NUM] = { const cmd_pair commands[CMD_NUM] = {
{"quit", quit, "Quit the program."}, {"quit", quit, "Quit the program."},
{"info", info, "Show the table information."}, {"info", info, "Show the table information."},
{"open", load_file, "Open a dsv/csv file."}, {"open", load_file, "Open a dsv/csv file."},
{"save", save_file, "Save the table to a file."}, {"save", save_file, "Save the table to a file."},
{"stats", statistic, "Calculate statistics of the selected columns."}, {"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."},
{"null", nullptr, "null"} {"null", nullptr, "null"}
}; };