gctl/lib/io/text_io2.h

258 lines
6.3 KiB
C
Raw Normal View History

2024-09-10 15:45:07 +08:00
/********************************************************
*
*
*
*
*
*
* 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 <http://www.gnu.org/licenses/>.
*
* 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_TEXT_IO2_H
#define _GCTL_TEXT_IO2_H
#include "../core.h"
#include "../utility.h"
namespace gctl
{
2024-11-04 16:03:39 +08:00
struct cell_content
{
std::string str_;
template <typename T> T value()
{
T out;
str2type(str_, out);
return out;
}
template <typename T> void value(const T &in)
{
str_ = std::to_string(in);
return;
}
};
2024-12-12 23:23:55 +08:00
enum text_head_type_e
{
HasColumnName,
NoColumnName,
};
2024-09-10 15:45:07 +08:00
struct text_content
{
2024-12-12 16:55:06 +08:00
// 头信息行数 表格行数 表格列数
int head_num_, row_num_, col_num_;
2024-09-10 15:45:07 +08:00
// 注释行起始符 标记行起始符 分割符
char att_sym_, tag_sym_, deli_sym_;
2024-11-04 16:03:39 +08:00
// 头信息行 注释行 标记行
std::vector<std::string> heads_, annotates_, tags_;
// 内容行与表格
2024-12-12 23:23:55 +08:00
std::vector<std::string> col_names_;
2024-11-04 16:03:39 +08:00
std::vector<std::string> lines_;
std::vector<std::vector<cell_content> > table_;
2024-09-10 15:45:07 +08:00
/**
* @brief Construct a new text content object
*
*/
text_content();
/**
* @brief Destroy the text content object
*
*/
~text_content();
/**
* @brief Construct a new text descriptor object and load text file
*
* @param filename
* @param file_exten
*/
2024-12-12 23:23:55 +08:00
text_content(std::string filename, std::string file_exten = ".txt", text_head_type_e t = NoColumnName);
/**
* @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;}
2024-12-12 16:55:06 +08:00
2024-09-10 15:45:07 +08:00
/**
2024-12-12 23:23:55 +08:00
* @brief
2024-09-10 15:45:07 +08:00
*
2024-12-12 23:23:55 +08:00
* @param att_sym
2024-09-10 15:45:07 +08:00
*/
2024-12-12 23:23:55 +08:00
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;}
2024-09-10 15:45:07 +08:00
/**
* @brief
*
*/
void clear();
/**
* @brief
*
* @param filename
* @param file_exten
*/
2024-12-12 23:23:55 +08:00
void load_text(std::string filename, std::string file_exten = ".txt", text_head_type_e t = NoColumnName);
2024-09-10 15:45:07 +08:00
/**
2024-11-04 16:03:39 +08:00
* @brief
2024-09-10 15:45:07 +08:00
*
* @param filename
* @param file_exten
*/
2024-12-12 16:55:06 +08:00
void save_text(std::string filename, std::string file_exten = ".txt");
2024-12-12 23:23:55 +08:00
/**
* @brief
*
* @param row
* @param col
*/
2024-12-12 16:55:06 +08:00
void init_table(int row, int col);
2024-12-12 23:23:55 +08:00
/**
* @brief
*
* @param names
*/
void set_column_name(const std::vector<std::string> &names);
/**
* @brief
*
* @tparam T
* @param idx
* @param data
*/
2024-12-12 16:55:06 +08:00
template <typename T> void fill_column(int idx, const array<T> &data);
2024-12-12 23:23:55 +08:00
/**
* @brief
*
* @tparam T
* @param idx
* @param data
*/
template <typename T> void fill_row(int idx, const array<T> &data);
/**
* @brief
*
* @tparam T
* @param idx
* @param data
*/
template <typename T> void get_column(int idx, array<T> &data);
/**
* @brief
*
* @tparam T
* @param idx
* @param data
*/
template <typename T> void get_row(int idx, array<T> &data);
2024-12-12 16:55:06 +08:00
};
template <typename T>
2024-12-12 23:23:55 +08:00
void text_content::fill_column(int idx, const array<T> &data)
2024-12-12 16:55:06 +08:00
{
2024-12-12 23:23:55 +08:00
if (idx >= col_num_)
{
throw std::runtime_error("[gctl::text_content::fill_column] Invalid column index.");
}
for (size_t i = 0; i < std::min(row_num_, data.size()); i++)
2024-12-12 16:55:06 +08:00
{
table_[i][idx].value(data[i]);
}
return;
}
2024-12-12 23:23:55 +08:00
template <typename T>
void text_content::fill_row(int idx, const array<T> &data)
{
if (idx >= row_num_)
{
throw std::runtime_error("[gctl::text_content::fill_row] Invalid row index.");
}
for (size_t i = 0; i < std::min(col_num_, data.size()); i++)
{
table_[idx][i].value(data[i]);
}
return;
}
template <typename T>
void text_content::get_column(int idx, array<T> &data)
{
if (idx >= col_num_)
{
throw std::runtime_error("[gctl::text_content::get_column] Invalid column index.");
}
data.resize(row_num_);
for (size_t i = 0; i < row_num_; i++)
{
data[i] = table_[i][idx].value<T>();
}
return;
}
template <typename T>
void text_content::get_row(int idx, array<T> &data)
{
if (idx >= row_num_)
{
throw std::runtime_error("[gctl::text_content::get_column] Invalid row index.");
}
data.resize(col_num_);
for (size_t i = 0; i < col_num_; i++)
{
data[i] = table_[idx][i].value<T>();
}
return;
}
2024-09-10 15:45:07 +08:00
}
#endif //_GCTL_TEXT_IO2_H