/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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.
 ******************************************************/

#include "str.h"

//替换str中所有lod_value为new_value,返回被替换的old_value的个数
int gctl::replace_all(std::string& new_str, const std::string& old_str,const std::string& old_value,const std::string& new_value)
{
	int count = 0;
	new_str = old_str;
	for(std::string::size_type pos(0);pos!=std::string::npos;pos+=new_value.length())
	{
		if((pos=new_str.find(old_value,pos))!=std::string::npos)
		{
			new_str.replace(pos,old_value.length(),new_value);
			count++;
		}
		else break;
	}
	return count;
}

//在输入字符串末尾添加一段字符串，如果输入字符串的尾端与待添加的字符串一致则不添加并返回原字符串
std::string gctl::patch_string(std::string in_str, std::string patch_str)
{
	int inlen = in_str.length();
	int exlen = patch_str.length();

	std::string out_str = in_str;

	if (exlen > inlen)
	{
		out_str += patch_str;
		return out_str;
	}

	if (exlen == inlen && in_str != patch_str)
	{
		out_str += patch_str;
		return out_str;
	}
	
    if (in_str.substr(inlen - exlen, inlen) != patch_str)
    {
        out_str += patch_str;
		return out_str;
    }

	return out_str;
}

//转换string对象为stringstream对象
void gctl::str2ss(std::string in_str, std::stringstream &out_ss, std::string delimiter)
{
	if (delimiter != "")
	{
		std::string replace_str;
		replace_all(replace_str, in_str, delimiter, " ");
		out_ss.clear();
		out_ss.str(replace_str);
	}
	else
	{
		out_ss.clear();
		out_ss.str(in_str);
	}
	return;
}

/**
 * @brief      转换string字符串为double类型的数值
 * 
 * 这个函数的主要作用是检查输入字符串是否为nan或者inf等表示无效值的符号。有的编译器
 * 可以在>>输入符中完成此检测，但为了函数功能的稳定，所以在此处自定了这个函数。
 *
 * @param[in]  instr  输入字符串
 *
 * @return     返回的double类型的数值
 */
double gctl::str2double(std::string instr)
{
	if (instr == "NAN" || instr == "nan" || instr == "NaN")
		return NAN;
	else if (instr == "INF" || instr == "inf" || instr == "Inf")
		return INFINITY;
	else
	{
		auto e(instr.find_first_of("dD"));
		if (e != std::string::npos) instr[e] = 'e';

		return atof(instr.c_str());
	}
}

/**
 * @brief      转换double类型数值为string类型字符串
 *
 * @param[in]  in_d  输入数值
 *
 * @return     输出字符串
 */
std::string gctl::double2str(double in_d)
{
	std::string tmp_str;
	std::stringstream tmp_ss;

	tmp_ss.clear();
	if (std::isnan(in_d))
		tmp_ss.str("nan");
	else if (std::isinf(in_d))
		tmp_ss.str("inf");
	else tmp_ss << in_d;

	tmp_ss >> tmp_str;
	return tmp_str;
}

//返回指定长度的随机字符串
void gctl::random_char(unsigned int length, char* out)
{
	char str[76] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*+_-=?";
	int i,lstr;
	char ss[2] = {0};
	lstr = strlen(str);//计算字符串长度
	srand((unsigned int)time((time_t *)NULL));//使用系统时间来初始化随机数发生器
	//按指定大小返回相应的字符串
	for(i = 1; i <= length; i++)
	{
		snprintf(ss, 2, "%c",str[(rand()%lstr)]);//rand()%lstr 可随机返回0-61之间的整数, str[0-61]可随机得到其中的字符
		strcat(out,ss);//将随机生成的字符串连接到指定数组后面
	}
	return;
}

void gctl::random_str(unsigned int length, std::string &out_str)
{
	char *out_char = new char [length];
	random_char(length, out_char);
	out_str  = out_char;
	delete[] out_char;
	return;
}

void gctl::parse_string_with_quotes(std::string in_str, std::vector<std::string> &str_vec)
{
	std::vector<std::string> tmp_units;
    parse_string_to_vector(in_str, ' ', tmp_units);

    // Combine strings into one if they are enclosed by quotes
    std::string tmp_str = "";
    int i = 0;
    while (i < tmp_units.size())
    {
        if (tmp_units[i].front() == '"' && tmp_units[i].back() == '"')
        {
            str_vec.push_back(tmp_units[i].substr(1, tmp_units[i].length() - 1));
            i++;
        }
        else if (tmp_units[i].front() == '"')
        {
            tmp_str = tmp_units[i].substr(1, tmp_units[i].length());
            i++;

            while (tmp_units[i].back() != '"')
            {
                tmp_str += " " + tmp_units[i];
                i++;
            }

            tmp_str += " " + tmp_units[i].substr(0, tmp_units[i].length() - 1);
            i++;

            str_vec.push_back(tmp_str);
            tmp_str = "";
        }
        else
        {
            str_vec.push_back(tmp_units[i]);
            i++;
        }
    }
	return;
}