gctl/lib/utility/stream.h
2024-12-15 19:15:12 +08:00

202 lines
6.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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_STREAM_H
#define _GCTL_STREAM_H
// library's head files
#include "stream_t.h"
// system's head files
#include "cstring"
#include "iostream"
#include "fstream"
#include "iomanip"
#include "ctime"
#include "stdio.h"
#ifdef _WINDOWS
#include "windows.h"
#else
#include "sys/ioctl.h"
#endif
namespace gctl
{
/**
* @brief 在终端显示一个简易的GCTL图标
*/
void display_logo(std::ostream &sout = std::cout);
/**
* @brief 替换字符串中的指定子串
*
* @param new_str 替换后的字符串
* @param old_str 需要操作的字符串
* @param old_value 需要被替换的字串
* @param new_value 替换的字串
*
* @return 替换的次数
*/
int replace_all(std::string& new_str, const std::string& old_str, const std::string& old_value,
const std::string& new_value);
/**
* @brief 在输入字符串末尾添加一段字符串,如果输入字符串的尾端与待添加的字符串一致则不添加并返回原字符串
*
* @param in_str 输入字符串
* @param patch_str 待匹配的字符串
* @return 输出字符串
*/
std::string patch_string(std::string in_str, std::string patch_str);
/**
* @brief 转换string对象为stringstream对象
*
* @param[in] in_str 输入的string字符串
* @param out_ss 引用返回的stringstreams对象
* @param[in] delimiter 分割string对象时的分隔符。如果不为空
* 函数会将delimiter转换为空格
*/
void str2ss(std::string in_str, std::stringstream &out_ss, std::string delimiter = "");
/**
* @brief 转换string字符串为double类型的数值
*
* 这个函数的主要作用是检查输入字符串是否为nan或者inf等表示无效值的符号。有的编译器
* 可以在>>输入符中完成此检测,但为了函数功能的稳定,所以在此处自定了这个函数。这个函数
* 还可以将fortran输出文件中以D标注的科学数字转换为浮点类型
*
* @param[in] instr 输入字符串
*
* @return 返回的double类型的数值
*/
double str2double(std::string instr);
/**
* @brief 转换double类型数值为string类型字符串 标准库中有to_string函数以后就不用这个了。
*
* @param[in] in_d 输入数值
*
* @return 输出字符串
*/
std::string double2str(double in_d);
/**
* @brief 返回一定长度的随机字符串
*
* @param[in] length 字符串的长度
* @param out 输出字符串
*/
void random_char(unsigned int length, char* out);
/**
* @brief 返回一定长度的随机字符串
*
* @param[in] length 字符串的长度
* @param out_str 输出字符串
*/
void random_str(unsigned int lenght, std::string &out_str);
/**
* @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 返回当前终端窗口的字符宽度
*
* @return 宽度
*/
int terminal_width();
/**
* @brief 返回当前终端窗口的字符高度
*
* @return 高度
*/
int terminal_height();
/**
* @brief 解析字符串并返回以空格分割的字符串向量
*
* @note 双引号括起来的部分将解析为一个字符串
*
* @param in_str 输入字符串
* @param str_vec 输出字符串向量
*/
void parse_string_with_quotes(std::string in_str, std::vector<std::string> &str_vec);
/**
* @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_STREAM_H