/******************************************************** * ██████╗ ██████╗████████╗██╗ * ██╔════╝ ██╔════╝╚══██╔══╝██║ * ██║ ███╗██║ ██║ ██║ * ██║ ██║██║ ██║ ██║ * ╚██████╔╝╚██████╗ ██║ ███████╗ * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ * Geophysical Computational Tools & Library (GCTL) * * Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn) * * GCTL is distributed under a dual licensing scheme. You can redistribute * it and/or modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation, either version 2 * of the License, or (at your option) any later version. You should have * received a copy of the GNU Lesser General Public License along with this * program. If not, see . * * If the terms and conditions of the LGPL v.2. would prevent you from using * the GCTL, please consider the option to obtain a commercial license for a * fee. These licenses are offered by the GCTL's original author. As a rule, * licenses are provided "as-is", unlimited in time for a one time fee. Please * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget * to include some description of your company and the realm of its activities. * Also add information on how to contact you by electronic and paper mail. ******************************************************/ #ifndef _GCTL_DSV_IO_H #define _GCTL_DSV_IO_H #include "../gctl_config.h" #include "../core.h" #include "../utility.h" #include "../geometry.h" #include "regex.h" #ifdef GCTL_EXPRTK #include "exprtk.hpp" #endif // GCTL_EXPRTK namespace gctl { enum cell_type_e { String, Int, Float, }; struct table_cell { std::string str_; // 单元格的内容 统一保存为字符串 cell_type_e type_; // 类型字符串 bool out_ok_; // 是否可输出到文件 table_cell() { str_ = ""; type_ = Float; out_ok_ = true; } template T value() { // 如果输出也是字符串 就直接赋值即可(避免l_str中含有空格会出现bug) if constexpr (std::is_same::value) return str_; else { T out; str2type(str_, out); return out; } } template void value(const T &in, int p = 6) { // 单元被赋值时使用的类型 const std::type_info &tinfo = typeid(T); std::smatch ret; std::regex pat("basic_string"); std::string t_str = tinfo.name(); if (regex_search(t_str, ret, pat)) type_ = String; else if (t_str == "i") type_ = Int; else if (t_str == "f") type_ = Float; else if (t_str == "d") type_ = Float; // 对于double类型 可以设置转换的有效数字位数(精度) if constexpr (std::is_same::value) { std::stringstream ss; ss.precision(p); ss << in; ss >> str_; } else if constexpr (std::is_same::value) str_ = in; else str_ = std::to_string(in); return; } }; /** * @brief 表格的头信息类型 * */ enum table_headtype_e { NoHead, // 没有表头 BothHead, // 同时有行与列表头 ColumnHead, // 只有列表头 RowHead, // 只有行表头 }; /** * @brief DSV文本读写类型可以读写并处理一定格式保存的文本数据,具体的格式如下: * 1. 所有以注释符(默认为#号)开始的行均会保存至注释变量内,可由用户提取; * 2. 所有以标记符(默认为#!号)开始的行均会保存至标记变量内,可由用户提取; * 3. 文件内可以包含n(默认为0)行不以注释或标记符开始头信息行,保存为头信息变量内,可由用户提取; * 4. 注释、标记和头信息可出现在文本数据的任意行,且只有在头信息读入结束后才会开始读入数据; * 5. 读入的表格保存为一个二维字符串数组,可由用户提取。浮点类型在读入/保存时可设置有效数字位数; * 6. 若制定读入的数据表格存在行或列表头,则读入表格的第一列或第一行将被初始化为对应的行名称或列名称; * 7. 表格的行与列数据可通过数字索引访问(从1开始计数),也可由具体的行或列名称指定; * 8. 表格可识别内置的行与列名称(格式为为R和C),但不能用于输出文件。 */ class dsv_io { protected: // 文件名 std::string file_; // 头信息行数 表格行数(不包括表头) 表格列数(不包括表头) int head_num_, row_num_, col_num_; // 注释行起始符 标记行起始符 分割符 char att_sym_, tag_sym_, deli_sym_; // 头信息行 注释行 标记行 std::vector heads_, annotates_, tags_; // 内容表格(大小为row_num_+1 * col_num_+1) std::vector > table_; public: /** * @brief Construct a new text content object * */ dsv_io(); /** * @brief Destroy the text content object * */ ~dsv_io(); /** * @brief Construct a new text descriptor object and load text file * * @param filename 文件名 * @param file_exten 文件扩展名 * @param t 表格是否有行和列名称 */ dsv_io(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead); /** * @brief 清理字符串向量对象 * */ void clear(); /** * @brief 返回头信息行数 * * @return 行数 */ int head_number(){return head_num_;} /** * @brief 返回数据行数 * * @return 行数 */ int row_number(){return row_num_;} /** * @brief 返回数据列数 * * @return 列数 */ int col_number(){return col_num_;} /** * @brief 返回头信息 * * @return 头信息 */ const std::vector& get_head_records(){return heads_;} /** * @brief 返回注释行 * * @return 注释行 */ const std::vector& get_annotoations(){return annotates_;} /** * @brief 返回标记行 * * @return 标记 */ const std::vector& get_tags(){return tags_;} /** * @brief 获取行名称 * * @param names 名称 */ void get_row_names(std::vector &names); /** * @brief 获取列名称 * * @param names 名称 */ void get_column_names(std::vector &names); /** * @brief 设置列分隔符 * * @param deli_sym 分隔符 */ void set_delimeter(char deli_sym){deli_sym_ = deli_sym;} /** * @brief 设置头信息行数 * * @param num 行数 */ void set_head_number(char num){head_num_ = num;} /** * @brief 设置注释行符号 * * @param att_sym 注释符号 */ void set_annotation_symbol(char att_sym){att_sym_ = att_sym;} /** * @brief 设置标记行符号 * * @param tag_sym 标记符号 */ void set_tag_symbol(char tag_sym){tag_sym_ = tag_sym;} /** * @brief 设置头信息 * * @param heads 头信息 */ void set_head_records(const std::vector &heads){heads_ = heads; head_num_ = heads_.size();} /** * @brief 设置注释 * * @param att 注释 */ void set_annotoations(const std::vector &att){annotates_ = att;} /** * @brief 设置标记 * * @param tags 标记 */ void set_tags(const std::vector &tags){tags_ = tags;} /** * @brief 设置行名称 * * @param names 名称数组 * @param idx 索引数组(若为空则依次设置行名称) * @param corner_name 表格左上角位置名称(默认为RowNames) */ void set_row_names(const std::vector &names, const std::vector &idx = {}, std::string corner_name = "RowNames"); /** * @brief 设置列名称 * * @param names 名称数组 * @param idx 索引数组(若为空则依次设置行名称) */ void set_column_names(const std::vector &names, const std::vector &idx = {}); /** * @brief 设置行类型 * * @param t 类型名称 String|Float|Int * @param idx 行索引 */ void set_row_type(cell_type_e t, int idx); /** * @brief 设置行类型 * * @param t 类型名称 String|Float|Int * @param name 行名称 */ void set_row_type(cell_type_e t, std::string name); /** * @brief 设置列类型 * * @param t 类型名称 String|Float|Int * @param idx 列索引 */ void set_column_type(cell_type_e t, int idx); /** * @brief 设置列类型 * * @param t 类型名称 String|Float|Int * @param name 列名称 */ void set_column_type(cell_type_e t, std::string name); /** * @brief 读入文本文件 * * @param filename 文件名 * @param file_exten 文件扩展名 * @param t 表格是否有行和列名称 */ void load_text(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead); /** * @brief 读入CSV文件 * * @param filename 文件名 */ void load_csv(std::string filename, table_headtype_e t = ColumnHead); /** * @brief 将内容写入文件 * * @param filename 文件名 * @param file_exten 文件扩展名 */ void save_text(std::string filename, std::string file_exten = ".txt"); /** * @brief 将内容写入CSV文件 * * @param filename 文件名(无后缀) */ void save_csv(std::string filename); /** * @brief 初始化表格 * * @param row 数据行数 * @param col 数据列数 */ void init_table(int row, int col); /** * @brief 返回表格信息 * * @param t 显示表格信息的类型 */ void info(table_headtype_e t = ColumnHead); /** * @brief 返回名称为name和R和C的行或列的索引 * * @param name 名称 可以是具体的名称(如有),或者是R和C * @param iter_row 搜索行名称(默认为搜索列名称),如果name参数为R和C则此参数无效 * @return 索引 返回的索引(大于等于1 小于等于行数或列数)失败则返回-1 */ int name_index(std::string name, bool iter_row = false); /** * @brief 设置列输出。你仍然可以使用这些数据,它们只是不会被输出 * * @param idx 列索引 从1开始 * @param s 设置输出类型 */ void column_output(int idx, switch_type_e s = Disable); /** * @brief 设置列输出。你仍然可以使用这些数据,它们只是不会被输出 * * @param name 列名称 * @param s 设置输出类型 */ void column_output(std::string name, switch_type_e s = Disable); /** * @brief 设置行输出。你仍然可以使用这些数据,它们只是不会被输出 * * @param idx 行索引 从1开始 * @param s 设置输出类型 */ void row_output(int idx, switch_type_e s = Disable); /** * @brief 设置行输出。你仍然可以使用这些数据,它们只是不会被输出 * * @param name 行名称 * @param s 设置输出类型 */ void row_output(std::string name, switch_type_e s = Disable); /** * @brief 在索引为idx的位置插入一个空白列,剩余列后移一位。如果idx大于列数则在表尾添加一列。 * * @param idx 列索引 * @param name 设置列名称 * * @return 索引 返回的索引(大于等于1 小于等于行数或列数)失败则返回-1 */ int add_column(std::string name = "", int idx = 9999); /** * @brief 在名称为id_name的列的位置插入一个空白列,剩余列后移一位。 * * @param id_name 索引列名称 * @param name 设置列名称 * * @return 索引 返回的索引(大于等于1 小于等于行数或列数)失败则返回-1 */ int add_column(std::string name, std::string id_name); /** * @brief 在索引为idx的位置插入一个空白行,剩余行后移一位。如果idx大于等于行数则在表尾添加一行。 * * @param idx 行索引 * @param name 设置行名称 * * @return 索引 返回的索引(大于等于1 小于等于行数或列数)失败则返回-1 */ int add_row(std::string name = "", int idx = 9999); /** * @brief 在名称为id_name的行的位置插入一个空白行,剩余行后移一位。 * * @param id_name 索引行名称 * @param name 设置行名称 * * @return 索引 返回的索引(大于等于1 小于等于行数或列数)失败则返回-1 */ int add_row(std::string name, std::string id_name); /** * @brief 按行过滤并返回符合条件的列数据 * * @note 过滤后的表格第一列尾用于匹配正则表达式的列,剩余列尾为筛选后符合条件的列数据。 * * @param cnd_str 正则表达式 * @param cnd_col 用于匹配正则表达式的列名称 * @param out_col 输出的列索引列表(列表为空时则会输出所有列),正则表达式为真时即筛选这些行与列上对应的数据 * @param out_table 输出的表格 */ void filt_column(std::string cnd_str, std::string cnd_col, const std::vector &out_col, dsv_io &out_table); /** * @brief row operate function pointer * */ typedef bool (*rowbool_func_t)(const std::vector &table_row); /** * @brief 按行过滤并返回符合条件的列数据 * * @param func 处理行类容的布尔函数 * @param out_col 输出的列索引列表(列表为空时则会输出所有列),正则表达式为真时即筛选这些行与列上对应的数据 * @param out_table 输出的表格 */ void filt_column(rowbool_func_t func, const std::vector &out_col, dsv_io &out_table); #ifdef GCTL_EXPRTK /** * @brief 使用现有的表格数据计算新的列数据。使用时需要提供计算表达式,例如:C3 := C1 + C2 * 表示将C1与C2列数据的和保存到C3列,其中C表示计算所使用数据的索引,你也可以使用具体的列名称。 * * @note 只有单元格类型为float和Int类型的列数据才能用于计算。计算由exprtk库完成,支持的表达式见其说明文档。 * * @param expr_str 计算表达式 如C3 := C1 + C2 * @param col_list 列索引列表 如C3 C1 C2 (注意保存计算结果的列要放在开头) * @param p 浮点类数据保存时的有效数字位数 */ void cal_column(std::string expr_str, const std::vector &col_list, int p = 6); /** * @brief 按行过滤并返回符合条件的列数据 * * @note 只有单元格类型为float和Int类型的列数据才能用于计算。计算由exprtk库完成,支持的表达式见其说明文档。 * 因为没有使用strtk库的相关内容,所以并不支持对字符串与数字类型的混合条件判断。基于字符串的内容提取请使用其他函数。 * * @param cnd_str 条件表达式 * @param cnd_col 用于条件表达式的列索引列表 * @param out_col 输出的列索引列表(列表为空时则会输出所有列),即条件判断为真时即筛选这些行与列上对应的数据 * @param out_table 输出的表格 */ void filt_column(std::string cnd_str, const std::vector &cnd_col, const std::vector &out_col, dsv_io &out_table); #endif // GCTL_EXPRTK /** * @brief 初始化表格 * * @param data 向量表格 每行元素个数必须相等(不包含行与列的名称) */ template void init_table(const std::vector > &data, int p = 6); /** * @brief 填充表格 * * @tparam T 数据类型 * @param data 矩阵数据 大小与表格一致 * @param p 浮点类数据保存时的有效数字位数 */ template void init_table(const matrix &data, int p = 6); /** * @brief 获取表格 * * @tparam T 数据类型 * @param data 矩阵数据 */ template void get_table(matrix &data); /** * @brief 填充列 * * @tparam T 数据类型 * @param idx 列索引 从1开始 * @param data 列数据 * @param p 浮点类数据保存时的有效数字位数 */ template void fill_column(const array &data, int idx, int p = 6); /** * @brief 填充列 * * @tparam T 数据类型 * @param name 列名称 * @param data 列数据 * @param p 浮点类数据保存时的有效数字位数 */ template void fill_column(const array &data, std::string name, int p = 6); /** * @brief 填充行 * * @tparam T 数据类型 * @param idx 行索引 从1开始 * @param data 行数据 * @param p 浮点类数据保存时的有效数字位数 */ template void fill_row(const array &data, int idx, int p = 6); /** * @brief 填充行 * * @tparam T 数据类型 * @param name 行名称 * @param data 行数据 * @param p 浮点类数据保存时的有效数字位数 */ template void fill_row(const array &data, std::string name, int p = 6); /** * @brief 获取列数据 * * @tparam T 数据类型 * @param idx 列索引 从1开始 * @param data 列数据 */ template void get_column(array &data, int idx); /** * @brief 获取列数据 * * @tparam T 数据类型 * @param name 列名称 * @param data 列数据 */ template void get_column(array &data, std::string name); /** * @brief 获取行数据 * * @tparam T 数据类型 * @param idx 行索引 从1开始 * @param data 行数据 */ template void get_row(array &data, int idx); /** * @brief 获取行数据 * * @tparam T 数据类型 * @param name 行名称 * @param data 行数据 */ template void get_row(array &data, std::string name); /** * @brief 获取表格单元数据 * * @tparam T 数据类型 * @param r 行号 从0开始(可以操作行或列名称) * @param c 列号 从0开始(可以操作行或列名称) * @return T 单元数据 */ template T cell(int r, int c){return table_[r][c].value();} /** * @brief 填充表格单元数据 * * @tparam T 数据类型 * @param r 行号 从0开始(可以操作行或列名称) * @param c 列号 从0开始(可以操作行或列名称) * @param d 数据 * @param p 浮点类数据保存时的有效数字位数 */ template void cell(T d, int r, int c, int p = 6){table_[r][c].value(d, p); return;} }; template void dsv_io::init_table(const std::vector > &data, int p) { if (!table_.empty()) clear(); row_num_ = data.size(); col_num_ = data[0].size(); // 初始的列头和行头均为空白 table_.resize(row_num_ + 1); for (size_t i = 0; i < row_num_ + 1; i++) { table_[i].resize(col_num_ + 1); } for (size_t i = 1; i <= row_num_; i++) { for (size_t j = 1; j <= col_num_; j++) { table_[i][j].value(data[i - 1][j - 1], p); } } return; } template void dsv_io::init_table(const matrix &data, int p) { if (!table_.empty()) clear(); row_num_ = data.row_size(); col_num_ = data.col_size(); // 初始的列头和行头均为空白 table_.resize(row_num_ + 1); for (size_t i = 0; i < row_num_ + 1; i++) { table_[i].resize(col_num_ + 1); } for (size_t i = 1; i <= row_num_; i++) { for (size_t j = 1; j <= col_num_; j++) { table_[i][j].value(data[i - 1][j - 1], p); } } return; } template void dsv_io::get_table(matrix &data) { data.resize(row_num_, col_num_); for (size_t i = 1; i <= row_num_; i++) { for (size_t j = 1; j <= col_num_; j++) { data[i -1][j - 1] = table_[i][j].value(); } } return; } template void dsv_io::fill_column(const array &data, int idx, int p) { if (idx > col_num_ || idx <= 0) { throw std::runtime_error("[gctl::dsv_io] Invalid column index."); } for (size_t i = 1; i <= std::min(row_num_, (int) data.size()); i++) { table_[i][idx].value(data[i - 1], p); } return; } template void dsv_io::fill_column(const array &data, std::string name, int p) { fill_column(data, name_index(name, false), p); return; } template void dsv_io::fill_row(const array &data, int idx, int p) { if (idx > row_num_ || idx <= 0) { throw std::runtime_error("[gctl::dsv_io] Invalid row index."); } for (size_t i = 1; i <= std::min(col_num_, (int) data.size()); i++) { table_[idx][i].value(data[i - 1], p); } return; } template void dsv_io::fill_row(const array &data, std::string name, int p) { fill_row(data, name_index(name, true), p); return; } template void dsv_io::get_column(array &data, int idx) { if (idx > col_num_ || idx <= 0) { throw std::runtime_error("[gctl::dsv_io] Invalid column index."); } data.resize(row_num_); for (size_t i = 1; i <= row_num_; i++) { data[i - 1] = table_[i][idx].value(); } return; } template void dsv_io::get_column(array &data, std::string name) { get_column(data, name_index(name, false)); return; } template void dsv_io::get_row(array &data, int idx) { if (idx > row_num_ || idx <= 0) { throw std::runtime_error("[gctl::dsv_io] Invalid row index."); } data.resize(col_num_); for (size_t i = 1; i <= col_num_; i++) { data[i - 1] = table_[idx][i].value(); } return; } template void dsv_io::get_row(array &data, std::string name) { get_row(data, name_index(name, true)); return; } /** * @brief 地理数据类型DSV文件读写类 * */ class geodsv_io : public dsv_io { public: /** * @brief Construct a new geodsv_io object * */ geodsv_io(); /** * @brief Destroy the geodsv_io object * */ ~geodsv_io(); /** * @brief Construct a new text descriptor object and load text file * * @param filename 文件名 * @param file_exten 文件扩展名 */ geodsv_io(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead); /** * @brief 填充二维坐标列 * * @param xid x坐标列索引 从1开始 * @param yid y坐标列索引 从1开始 * @param data 返回的二维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ void fill_column_point2dc(const array &data, int xid, int yid, int p = 6); /** * @brief 填充二维坐标列 * * @param xname x坐标列名称 * @param yname y坐标列名称 * @param data 返回的二维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ void fill_column_point2dc(const array &data, std::string xname, std::string yname, int p = 6); /** * @brief 填充二维坐标列 * * @param rid rad坐标列索引 从1开始 * @param cid arc坐标列索引 从1开始 * @param data 返回的二维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ void fill_column_point2dp(const array &data, int rid, int cid, int p = 6); /** * @brief 填充二维坐标列 * * @param rname rad坐标列名称 * @param cname arc坐标列名称 * @param data 返回的二维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ void fill_column_point2dp(const array &data, std::string rname, std::string cname, int p = 6); /** * @brief 填充三维坐标列 * * @param xid x坐标列索引 从1开始 * @param yid y坐标列索引 从1开始 * @param zid z坐标列索引 从1开始 * @param data 返回的三维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ void fill_column_point3dc(const array &data, int xid, int yid, int zid, int p = 6); /** * @brief 填充三维坐标列 * * @param xname x坐标列名称 * @param yname y坐标列名称 * @param zname z坐标列名称 * @param data 返回的三维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ void fill_column_point3dc(const array &data, std::string xname, std::string yname, std::string zname, int p = 6); /** * @brief 填充三维坐标列 * * @param rid rad坐标列索引 从1开始 * @param pid phi坐标(经度)列索引 从1开始 * @param tid theta坐标(纬度)列索引 从1开始 * @param data 返回的三维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ void fill_column_point3ds(const array &data, int rid, int pid, int tid, int p = 6); /** * @brief 填充三维坐标列 * * @param rname rad坐标列名称 从1开始 * @param pname phi坐标(经度)列名称 从1开始 * @param tname theta坐标(纬度)列名称 从1开始 * @param data 返回的三维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ void fill_column_point3ds(const array &data, std::string rname, std::string pname, std::string tname, int p = 6); /** * @brief 读取二维坐标列 * * @param xid x坐标列索引 从1开始 * @param yid y坐标列索引 从1开始 * @param data 返回的二维坐标数据 */ void get_column_point2dc(array &data, int xid, int yid); /** * @brief 读取二维坐标列 * * @param xname x坐标列名称 * @param yname y坐标列名称 * @param data 返回的二维坐标数据 */ void get_column_point2dc(array &data, std::string xname, std::string yname); /** * @brief 读取二维坐标列 * * @param rid rad坐标列索引 从1开始 * @param cid arc坐标列索引 从1开始 * @param data 返回的二维坐标数据 */ void get_column_point2dp(array &data, int rid, int cid); /** * @brief 读取二维坐标列 * * @param rname rad坐标列名称 * @param cname arc坐标列名称 * @param data 返回的二维坐标数据 */ void get_column_point2dp(array &data, std::string rname, std::string cname); /** * @brief 读取三维坐标列 * * @param xid x坐标列索引 从1开始 * @param yid y坐标列索引 从1开始 * @param zid z坐标列索引 从1开始 * @param data 返回的三维坐标数据 */ void get_column_point3dc(array &data, int xid, int yid, int zid); /** * @brief 读取三维坐标列 * * @param xname x坐标列名称 * @param yname y坐标列名称 * @param zname z坐标列名称 * @param data 返回的三维坐标数据 */ void get_column_point3dc(array &data, std::string xname, std::string yname, std::string zname); /** * @brief 读取三维坐标列 * * @param rid rad坐标列索引 从1开始 * @param pid phi坐标(经度)列索引 从1开始 * @param tid theta坐标(纬度)列索引 从1开始 * @param data 返回的三维坐标数据 */ void get_column_point3ds(array &data, int rid, int pid, int tid); /** * @brief 读取三维坐标列 * * @param rname rad坐标列名称 从1开始 * @param pname phi坐标(经度)列名称 从1开始 * @param tname theta坐标(纬度)列名称 从1开始 * @param data 返回的三维坐标数据 */ void get_column_point3ds(array &data, std::string rname, std::string pname, std::string tname); }; } #endif //_GCTL_DSV_IO_H