/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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_FILE_IO_H
#define _GCTL_FILE_IO_H

#include "../core/str.h"

namespace gctl
{
    /**
	 * @brief      打开输入文件
	 *
	 * @param      infile     ifstream对象
	 * @param[in]  filename   文件名
	 * @param[in]  extension  文件扩展名
	 * @param[in]  mode       标准读入模式
	 */
	void open_infile(std::ifstream &infile, std::string filename, std::string extension = "", 
		std::ios_base::openmode mode = std::ios_base::in);

	/**
	 * @brief      打开输入文件同时检查后缀名是否符合指定要求
	 *
	 * @param      infile         输入文件流
	 * @param[in]  filename       输入文件名
	 * @param[in]  exten_pattern  待匹配的后缀名（多个后缀名使用逗号分隔）
	 * @param[in]  mode           输入文件的打开模式
	 */
	void open_matched_infile(std::ifstream &infile, std::string filename, std::string exten_pattern, 
		std::ios_base::openmode mode = std::ios_base::in);

	/**
	 * @brief      打开输出文件
	 *
	 * @param      outfile    ofstream对象
	 * @param[in]  filename   文件名
	 * @param[in]  extension  文件扩展名
	 * @param[in]  mode       标准输出模式
	 */
	void open_outfile(std::ofstream &outfile, std::string filename, std::string extension = "", 
		std::ios_base::openmode mode = std::ios_base::out);

	/**
	 * @brief      打开输出文件同时检查后缀名是否符合指定要求
	 *
	 * @param      outfile        输出文件流
	 * @param[in]  filename       输出文件名
	 * @param[in]  exten_pattern  待匹配的后缀名（多个后缀名使用逗号分隔）
	 * @param[in]  mode           输出文件的打开模式
	 */
	void open_matched_outfile(std::ofstream &outfile, std::string filename, std::string exten_pattern, 
		std::ios_base::openmode mode = std::ios_base::out);

	/**
	 * @brief 解析文件路径
	 * 
	 * @param in_str 输入字符串
	 * @param naked_name 不含后缀的文件名
	 * @param exten_name 文件后缀名
	 */
	void parse_filename(std::string filename, std::string &naked_name, std::string &exten_name);
};

#endif // _GCTL_FILE_IO_H