/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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_ = '#';
	tag_sym_ = '>';
	deli_sym_ = ' ';
	head_num_ = 0;
}

gctl::text_content::~text_content()
{
    clear();
}

gctl::text_content::text_content(std::string filename, std::string file_exten)
{
    // 设置基础参数
    att_sym_ = '#';
	tag_sym_ = '>';
	deli_sym_ = ' ';
	head_num_ = 0;
    // 载入文本内容
    load_text(filename, file_exten);
}

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()
{
	heads_.clear();
	annotates_.clear();
	tags_.clear();
    contents_.clear();
	return;
}

void gctl::text_content::load_text(std::string filename, std::string file_exten)
{
    std::ifstream infile;
    open_infile(infile, filename, file_exten);

    clear();

    std::string tmp_line;
    std::stringstream tmp_ss;
    for (int i = 0; i < head_num_; i++)
    {
        std::getline(infile, tmp_line);
        heads_.push_back(tmp_line);
    }

    while (std::getline(infile, tmp_line))
    {
        if (tmp_line[0] == att_sym_)
        {
            tmp_line = tmp_line.substr(1);
            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);
        }
        else if (tmp_line[0] == tag_sym_)
        {
            tmp_line = tmp_line.substr(1);
            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);
        }
        else contents_.push_back(tmp_line);
    }

    infile.close();
    return;
}

void gctl::text_content::save_text(std::string filename, std::string file_exten)
{
    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++)
    {
        outfile << "> " << tags_[i] << std::endl;
    }

    for (int i = 0; i < annotates_.size(); i++)
    {
        outfile << "# " << annotates_[i] << std::endl;
    }

    for (int i = 0; i < contents_.size(); i++)
    {
        outfile << contents_[i] << std::endl;
    }

    outfile.close();
    return;
}