gctl/lib/io/text_io2.cpp

176 lines
5.2 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 tcription of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#include "text_io2.h"
gctl::text_content::text_content()
{
// 设置基础参数
att_sym_ = '#';
2024-11-21 12:43:10 +08:00
tag_sym_ = '!';
2024-09-10 15:45:07 +08:00
deli_sym_ = ' ';
head_num_ = 0;
}
gctl::text_content::~text_content()
{
clear();
}
2024-11-04 16:03:39 +08:00
gctl::text_content::text_content(std::string filename, std::string file_exten, bool unpack)
2024-09-10 15:45:07 +08:00
{
// 设置基础参数
att_sym_ = '#';
2024-11-21 12:43:10 +08:00
tag_sym_ = '!';
2024-09-10 15:45:07 +08:00
deli_sym_ = ' ';
head_num_ = 0;
// 载入文本内容
2024-11-04 16:03:39 +08:00
load_text(filename, file_exten, unpack);
2024-09-10 15:45:07 +08:00
}
void gctl::text_content::set(char deli_sym, int h_num, char att_sym, char tag_sym)
{
att_sym_ = att_sym;
tag_sym_ = tag_sym;
deli_sym_ = deli_sym;
head_num_ = h_num;
return;
}
void gctl::text_content::clear()
{
2024-11-04 16:03:39 +08:00
destroy_vector(heads_);
destroy_vector(annotates_);
destroy_vector(tags_);
destroy_vector(lines_);
destroy_vector(table_);
2024-09-10 15:45:07 +08:00
return;
}
2024-11-04 16:03:39 +08:00
void gctl::text_content::load_text(std::string filename, std::string file_exten, bool unpack)
2024-09-10 15:45:07 +08:00
{
std::ifstream infile;
open_infile(infile, filename, file_exten);
clear();
std::string tmp_line;
std::stringstream tmp_ss;
2024-11-21 12:43:10 +08:00
for (int i = 0; i < head_num_; i++) // 跳过前n行 包括空行 但不会保存空行
2024-09-10 15:45:07 +08:00
{
std::getline(infile, tmp_line);
2024-11-04 16:03:39 +08:00
if (!tmp_line.empty()) heads_.push_back(tmp_line);
2024-09-10 15:45:07 +08:00
}
while (std::getline(infile, tmp_line))
{
2024-11-21 12:43:10 +08:00
if (tmp_line.empty()) continue; // 跳过空行
else if (tmp_line[0] == att_sym_) // 注释行或者标记行 # #!
2024-09-10 15:45:07 +08:00
{
2024-11-21 12:43:10 +08:00
if (tmp_line[1] == tag_sym_) // #!
{
tmp_line = tmp_line.substr(2); // 去掉前两个字符
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
tags_.push_back(tmp_line);
continue;
}
// #
tmp_line = tmp_line.substr(1); // 去掉第一个字符
2024-09-10 15:45:07 +08:00
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
annotates_.push_back(tmp_line);
}
2024-11-21 12:43:10 +08:00
else lines_.push_back(tmp_line);
2024-09-10 15:45:07 +08:00
}
infile.close();
2024-11-04 16:03:39 +08:00
if (unpack)
{
table_.resize(lines_.size());
std::vector<std::string> tmp_cols;
for (size_t i = 0; i < lines_.size(); i++)
{
tmp_cols.clear();
parse_string_to_vector(lines_[i], deli_sym_, tmp_cols);
table_[i].resize(tmp_cols.size());
for (size_t j = 0; j < tmp_cols.size(); j++)
{
table_[i][j].str_ = tmp_cols[j];
}
}
}
2024-09-10 15:45:07 +08:00
return;
}
2024-11-04 16:03:39 +08:00
void gctl::text_content::save_text(std::string filename, std::string file_exten, bool packed)
2024-09-10 15:45:07 +08:00
{
std::ofstream outfile;
open_outfile(outfile, filename, file_exten);
for (int i = 0; i < heads_.size(); i++)
{
outfile << heads_[i] << std::endl;
}
for (int i = 0; i < tags_.size(); i++)
{
2024-11-21 12:43:10 +08:00
outfile << "#! " << tags_[i] << std::endl;
2024-09-10 15:45:07 +08:00
}
for (int i = 0; i < annotates_.size(); i++)
{
outfile << "# " << annotates_[i] << std::endl;
}
2024-11-04 16:03:39 +08:00
if (packed)
2024-09-10 15:45:07 +08:00
{
2024-11-04 16:03:39 +08:00
for (int i = 0; i < lines_.size(); i++)
{
outfile << lines_[i] << std::endl;
}
}
else
{
for (int i = 0; i < table_.size(); i++)
{
outfile << table_[i][0].str_;
for (size_t j = 1; j < table_[i].size(); j++)
{
outfile << deli_sym_ << table_[i][j].str_;
}
outfile << std::endl;
}
2024-09-10 15:45:07 +08:00
}
outfile.close();
return;
}