This commit is contained in:
张壹 2024-12-17 09:59:06 +08:00
parent cb803c2c89
commit 4959733702
3 changed files with 199 additions and 100 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);
@ -49,8 +49,8 @@ int main(int argc, char const *argv[]) try
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);
@ -69,11 +69,11 @@ int main(int argc, char const *argv[]) try
tc.save_csv("out"); tc.save_csv("out");
double c = 4.25242153654; double c = 4.25242153654;
tc.cell(0, 0, c, 12); tc.cell(1, 0, c, 12);
std::clog << std::setprecision(12) << tc.cell<double>(0, 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

@ -140,7 +140,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table
} }
// 补齐可能的空格 // 补齐可能的空格
cell_content empty_cell; table_cell empty_cell;
empty_cell.str_ = ""; empty_cell.str_ = "";
for (size_t i = 0; i < row_num_; i++) for (size_t i = 0; i < row_num_; i++)
{ {
@ -294,13 +294,44 @@ void gctl::dsv_io::info()
return; return;
} }
gctl::geodsv_io::geodsv_io() : dsv_io::dsv_io(){} int gctl::dsv_io::name_index(std::string name, bool iter_row)
gctl::geodsv_io::~geodsv_io()
{ {
dsv_io::~dsv_io(); if (iter_row)
{
if (thead_ != RowHead && thead_ != BothHead)
{
throw std::runtime_error("[gctl::dsv_io] The table is initialized with no row names.");
}
for (size_t i = 0; i < row_num_; 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 0;
}
else
{
if (thead_ != ColumnHead && thead_ != BothHead)
{
throw std::runtime_error("[gctl::dsv_io] The table is initialized with no column names.");
}
for (size_t i = 0; i < col_num_; 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 0;
}
} }
gctl::geodsv_io::geodsv_io(){}
gctl::geodsv_io::~geodsv_io(){}
gctl::geodsv_io::geodsv_io(std::string filename, std::string file_exten, table_headtype_e t) gctl::geodsv_io::geodsv_io(std::string filename, std::string file_exten, table_headtype_e t)
{ {
file_ = ""; file_ = "";
@ -320,7 +351,7 @@ void gctl::geodsv_io::fill_column_point2dc(int xid, int yid, const array<point2d
{ {
if (xid >= col_num_ || yid >= col_num_ || xid == yid) if (xid >= col_num_ || yid >= col_num_ || xid == yid)
{ {
throw std::runtime_error("[gctl::geodsv_io::fill_column_point2dc] Invalid column index."); throw std::runtime_error("[gctl::geodsv_io] Invalid column index.");
} }
std::stringstream ss; std::stringstream ss;
@ -340,64 +371,97 @@ void gctl::geodsv_io::fill_column_point2dc(int xid, int yid, const array<point2d
return; return;
} }
void gctl::geodsv_io::fill_column_point2dc(std::string xname, std::string yname, const array<point2dc> &data, int p)
{
fill_column_point2dc(name_index(xname, false), name_index(yname, false), data, p);
return;
}
void gctl::geodsv_io::fill_column_point3dc(int xid, int yid, int zid, const array<point3dc> &data, int p) void gctl::geodsv_io::fill_column_point3dc(int xid, int yid, int zid, const array<point3dc> &data, int p)
{ {
if (xid >= col_num_ || yid >= col_num_ || zid >= col_num_ || xid == yid || yid == zid || xid == zid) if (xid >= col_num_ || yid >= col_num_ || zid >= col_num_ || xid == yid || yid == zid || xid == zid)
{ {
throw std::runtime_error("[gctl::geodsv_io::fill_column_point3dc] Invalid column index."); throw std::runtime_error("[gctl::geodsv_io] Invalid column index.");
} }
int st = 0;
if (thead_ == ColumnHead || thead_ == BothHead) st = 1;
std::stringstream ss; std::stringstream ss;
std::string s; std::string s;
for (size_t i = 0; i < std::min(row_num_, (int) data.size()); i++) for (size_t i = st; i < std::min(row_num_, (int) data.size()); i++)
{ {
ss.clear(); ss.clear();
ss << data[i].x; ss << data[i - st].x;
ss >> s; ss >> s;
table_[i][xid].str_ = s; table_[i][xid].str_ = s;
ss.clear(); ss.clear();
ss << data[i].y; ss << data[i - st].y;
ss >> s; ss >> s;
table_[i][yid].str_ = s; table_[i][yid].str_ = s;
ss.clear(); ss.clear();
ss << data[i].z; ss << data[i - st].z;
ss >> s; ss >> s;
table_[i][zid].str_ = s; table_[i][zid].str_ = s;
} }
return; return;
} }
void gctl::geodsv_io::fill_column_point3dc(std::string xname, std::string yname, std::string zname, const array<point3dc> &data, int p)
{
fill_column_point3dc(name_index(xname, false), name_index(yname, false), name_index(zname, false), data, p);
return;
}
void gctl::geodsv_io::get_column_point2dc(int xid, int yid, array<point2dc> &data) void gctl::geodsv_io::get_column_point2dc(int xid, int yid, array<point2dc> &data)
{ {
if (xid >= col_num_ || yid >= col_num_ || xid == yid) if (xid >= col_num_ || yid >= col_num_ || xid == yid)
{ {
throw std::runtime_error("[gctl::geodsv_io::fill_column_point2dc] Invalid column index."); throw std::runtime_error("[gctl::geodsv_io] Invalid column index.");
} }
data.resize(row_num_); int st = 0;
for (size_t i = 0; i < std::min(row_num_, (int) data.size()); i++) if (thead_ == ColumnHead || thead_ == BothHead) st = 1;
data.resize(row_num_ - st);
for (size_t i = st; i < row_num_; i++)
{ {
data[i].x = table_[i][xid].value<double>(); data[i - st].x = table_[i][xid].value<double>();
data[i].y = table_[i][yid].value<double>(); data[i - st].y = table_[i][yid].value<double>();
} }
return; return;
} }
void gctl::geodsv_io::get_column_point2dc(std::string xname, std::string yname, array<point2dc> &data)
{
get_column_point2dc(name_index(xname, false), name_index(yname, false), data);
return;
}
void gctl::geodsv_io::get_column_point3dc(int xid, int yid, int zid, array<point3dc> &data) void gctl::geodsv_io::get_column_point3dc(int xid, int yid, int zid, array<point3dc> &data)
{ {
if (xid >= col_num_ || yid >= col_num_ || zid >= col_num_ || xid == yid || yid == zid || xid == zid) if (xid >= col_num_ || yid >= col_num_ || zid >= col_num_ || xid == yid || yid == zid || xid == zid)
{ {
throw std::runtime_error("[gctl::geodsv_io::fill_column_point3dc] Invalid column index."); throw std::runtime_error("[gctl::geodsv_io] Invalid column index.");
} }
data.resize(row_num_); int st = 0;
for (size_t i = 0; i < std::min(row_num_, (int) data.size()); i++) if (thead_ == ColumnHead || thead_ == BothHead) st = 1;
data.resize(row_num_ - st);
for (size_t i = st; i < row_num_; i++)
{ {
data[i].x = table_[i][xid].value<double>(); data[i - st].x = table_[i][xid].value<double>();
data[i].y = table_[i][yid].value<double>(); data[i - st].y = table_[i][yid].value<double>();
data[i].z = table_[i][zid].value<double>(); data[i - st].z = table_[i][zid].value<double>();
} }
return; return;
}
void gctl::geodsv_io::get_column_point3dc(std::string xname, std::string yname, std::string zname, array<point3dc> &data)
{
get_column_point3dc(name_index(xname, false), name_index(yname, false), name_index(zname, false), data);
return;
} }

View File

@ -25,8 +25,8 @@
* Also add information on how to contact you by electronic and paper mail. * Also add information on how to contact you by electronic and paper mail.
******************************************************/ ******************************************************/
#ifndef _GCTL_TEXT_IO2_H #ifndef _GCTL_DSV_IO_H
#define _GCTL_TEXT_IO2_H #define _GCTL_DSV_IO_H
#include "../core.h" #include "../core.h"
#include "../utility.h" #include "../utility.h"
@ -34,7 +34,7 @@
namespace gctl namespace gctl
{ {
struct cell_content struct table_cell
{ {
std::string str_; std::string str_;
@ -78,7 +78,7 @@ namespace gctl
}; };
/** /**
* @brief * @brief DSV文本读写
* *
* *
* 1. '#' * 1. '#'
@ -100,7 +100,7 @@ namespace gctl
// 头信息行 注释行 标记行 // 头信息行 注释行 标记行
std::vector<std::string> heads_, annotates_, tags_; std::vector<std::string> heads_, annotates_, tags_;
// 内容表格 // 内容表格
std::vector<std::vector<cell_content> > table_; std::vector<std::vector<table_cell> > table_;
public: public:
/** /**
@ -264,6 +264,15 @@ namespace gctl
*/ */
void info(); void info();
/**
* @brief name的行或列的索引
*
* @param name
* @param iter_row
* @return
*/
int name_index(std::string name, bool iter_row = false);
/** /**
* @brief * @brief
* *
@ -362,7 +371,7 @@ namespace gctl
{ {
if (idx >= col_num_) if (idx >= col_num_)
{ {
throw std::runtime_error("[gctl::dsv_io::fill_column] Invalid column index."); throw std::runtime_error("[gctl::dsv_io] Invalid column index.");
} }
int st = 0; int st = 0;
@ -378,21 +387,7 @@ namespace gctl
template <typename T> template <typename T>
void dsv_io::fill_column(std::string name, const array<T> &data, int p) void dsv_io::fill_column(std::string name, const array<T> &data, int p)
{ {
if (thead_ != ColumnHead && thead_ != BothHead) fill_column(name_index(name, false), data, p);
{
throw std::runtime_error("[gctl::dsv_io::fill_column] The table is initialized with no column names.");
}
for (size_t i = 0; i < col_num_; i++)
{
if (table_[0][i].str_ == name)
{
fill_column(i, data, p);
return;
}
}
throw std::runtime_error("[gctl::dsv_io::fill_column] No column found by the input name.");
return; return;
} }
@ -401,7 +396,7 @@ namespace gctl
{ {
if (idx >= row_num_) if (idx >= row_num_)
{ {
throw std::runtime_error("[gctl::dsv_io::fill_row] Invalid row index."); throw std::runtime_error("[gctl::dsv_io] Invalid row index.");
} }
int st = 0; int st = 0;
@ -417,22 +412,7 @@ namespace gctl
template <typename T> template <typename T>
void dsv_io::fill_row(std::string name, const array<T> &data, int p) void dsv_io::fill_row(std::string name, const array<T> &data, int p)
{ {
if (thead_ != RowHead && thead_ != BothHead) fill_row(name_index(name, true), data, p);
{
throw std::runtime_error("[gctl::dsv_io::fill_row] The table is initialized with no row names.");
}
for (size_t i = 0; i < row_num_; i++)
{
if (table_[i][0].str_ == name)
{
fill_row(i, data, p);
return;
}
}
throw std::runtime_error("[gctl::dsv_io::fill_row] No row found by the input name.");
return;
} }
template <typename T> template <typename T>
@ -440,7 +420,7 @@ namespace gctl
{ {
if (idx >= col_num_) if (idx >= col_num_)
{ {
throw std::runtime_error("[gctl::dsv_io::get_column] Invalid column index."); throw std::runtime_error("[gctl::dsv_io] Invalid column index.");
} }
int st = 0; int st = 0;
@ -457,22 +437,7 @@ namespace gctl
template <typename T> template <typename T>
void dsv_io::get_column(std::string name, array<T> &data) void dsv_io::get_column(std::string name, array<T> &data)
{ {
if (thead_ != ColumnHead && thead_ != BothHead) get_column(name_index(name, false), data);
{
throw std::runtime_error("[gctl::dsv_io::get_column] The table is initialized with no column names.");
}
for (size_t i = 0; i < col_num_; i++)
{
if (table_[0][i].str_ == name)
{
get_column(i, data);
return;
}
}
throw std::runtime_error("[gctl::dsv_io::get_column] No column found by the input name.");
return;
} }
template <typename T> template <typename T>
@ -480,7 +445,7 @@ namespace gctl
{ {
if (idx >= row_num_) if (idx >= row_num_)
{ {
throw std::runtime_error("[gctl::dsv_io::get_row] Invalid row index."); throw std::runtime_error("[gctl::dsv_io] Invalid row index.");
} }
int st = 0; int st = 0;
@ -497,28 +462,26 @@ namespace gctl
template <typename T> template <typename T>
void dsv_io::get_row(std::string name, array<T> &data) void dsv_io::get_row(std::string name, array<T> &data)
{ {
if (thead_ != RowHead && thead_ != BothHead) get_row(name_index(name, true), data);
{
throw std::runtime_error("[gctl::dsv_io::get_row] The table is initialized with no row names.");
}
for (size_t i = 0; i < row_num_; i++)
{
if (table_[i][0].str_ == name)
{
get_row(i, data);
return;
}
}
throw std::runtime_error("[gctl::dsv_io::get_row] No row found by the input name.");
return;
} }
/**
* @brief DSV文件读写类
*
*/
class geodsv_io : public dsv_io class geodsv_io : public dsv_io
{ {
public: public:
/**
* @brief Construct a new geodsv_io object
*
*/
geodsv_io(); geodsv_io();
/**
* @brief Destroy the geodsv_io object
*
*/
~geodsv_io(); ~geodsv_io();
/** /**
@ -529,14 +492,86 @@ namespace gctl
*/ */
geodsv_io(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead); geodsv_io(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead);
/**
* @brief
*
* @param xid x坐标列索引
* @param yid y坐标列索引
* @param data
* @param p
*/
void fill_column_point2dc(int xid, int yid, const array<point2dc> &data, int p = 6); void fill_column_point2dc(int xid, int yid, const array<point2dc> &data, int p = 6);
/**
* @brief
*
* @param xname x坐标列名称
* @param yname y坐标列名称
* @param data
* @param p
*/
void fill_column_point2dc(std::string xname, std::string yname, const array<point2dc> &data, int p = 6);
/**
* @brief
*
* @param xid x坐标列索引
* @param yid y坐标列索引
* @param zid z坐标列索引
* @param data
* @param p
*/
void fill_column_point3dc(int xid, int yid, int zid, const array<point3dc> &data, int p = 6); void fill_column_point3dc(int xid, int yid, int zid, const array<point3dc> &data, int p = 6);
/**
* @brief
*
* @param xname x坐标列名称
* @param yname y坐标列名称
* @param zname z坐标列名称
* @param data
* @param p
*/
void fill_column_point3dc(std::string xname, std::string yname, std::string zname, const array<point3dc> &data, int p = 6);
/**
* @brief
*
* @param xid x坐标列索引
* @param yid y坐标列索引
* @param data
*/
void get_column_point2dc(int xid, int yid, array<point2dc> &data); void get_column_point2dc(int xid, int yid, array<point2dc> &data);
/**
* @brief
*
* @param xname x坐标列名称
* @param yname y坐标列名称
* @param data
*/
void get_column_point2dc(std::string xname, std::string yname, array<point2dc> &data);
/**
* @brief
*
* @param xid x坐标列索引
* @param yid y坐标列索引
* @param zid z坐标列索引
* @param data
*/
void get_column_point3dc(int xid, int yid, int zid, array<point3dc> &data); void get_column_point3dc(int xid, int yid, int zid, array<point3dc> &data);
/**
* @brief
*
* @param xname x坐标列名称
* @param yname y坐标列名称
* @param zname z坐标列名称
* @param data
*/
void get_column_point3dc(std::string xname, std::string yname, std::string zname, array<point3dc> &data);
}; };
} }
#endif //_GCTL_TEXT_IO2_H #endif //_GCTL_DSV_IO_H