Files
42_INT_12_webserv/srcs/cgi-bin/cgi_utils.cpp
2022-08-15 16:18:47 +02:00

107 lines
2.0 KiB
C++

#include "cgi_utils.hpp"
std::string trim(std::string str, char del)
{
size_t pos;
// delete leadings del
pos = str.find_first_not_of(del);
if (pos == NPOS)
pos = str.size();
str = str.substr(pos);
// delete trailing del
pos = str.find_last_not_of(del);
if (pos != NPOS)
str = str.substr(0, pos + 1);
return str;
}
std::vector<std::string>
split(const std::string & input, std::string delim, char ctrim)
{
std::vector<std::string> split_str;
std::string tmp;
size_t start = 0;
size_t end = 0;
size_t len = 0;
while (end != NPOS)
{
end = input.find(delim, start);
len = end - start;
if (end == NPOS)
len = end;
tmp = input.substr(start, len);
if (ctrim != '\0')
tmp = trim(tmp, ctrim);
if (tmp.size() != 0)
split_str.push_back( tmp );
start = end + delim.size();
}
return split_str;
}
std::string itos(int n)
{
std::stringstream strs;
strs << n;
return ( strs.str() );
}
std::string fill_tag(std::string content, std::string tag)
{
std::string ret;
ret = "<" + tag + ">" + content + "</" + tag + ">";
return ret;
}
std::string fill_env(std::string env, std::string tag)
{
std::string ret;
char * ret_env;
ret = "<" + tag + ">";
ret_env = getenv(env.c_str());
if (ret_env != NULL)
ret += ret_env;
else
ret += env + " not foud";
ret += "</" + tag + ">";
return ret;
}
std::string
parse_form_infos()
{
std::string ret;
std::cin >> ret;
return ret;
}
std::string
fill_form(std::string form, std::string tag_key, std::string tag_val)
{
std::vector<std::string> split_str;
std::vector<std::string> sub_split_str;
std::vector<std::string>::const_iterator it;
std::string ret;
split_str = split(form, "&");
for (it = split_str.begin(); it != split_str.end(); ++it)
{
sub_split_str = split(*it, "=");
ret = "<" + tag_key + ">" + sub_split_str[0] + ": </" + tag_key + ">";
ret = "<" + tag_val + ">" + sub_split_str[1] + "</" + tag_val + ">";
}
return ret;
}