User:A2569875-bot/Code/Util.cpp

#include <iostream>
#include <string>
#include <vector>

#include "CreateCasRedirect.h"

std::vector<std::string> split(const std::string &s, char delim) {
	std::stringstream ss(s);
	std::string item;
	std::vector<std::string> elems;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
		// elems.push_back(std::move(item)); // if C++11 (based on comment from @mchiasson)
	}
	return elems;
}

int string_to_int(const std::string &s) {
	std::stringstream ss(s);
	int tmp = -1;
	ss >> tmp;
	return tmp;
}

std::ostream& operator <<(std::ostream& out, System::String^ str) {
	using namespace System::Runtime::InteropServices;
	const char* chars =
		(const char*)(Marshal::StringToHGlobalAnsi(str)).ToPointer();
	out << chars;
	Marshal::FreeHGlobal(System::IntPtr((void*)chars));
	return out;
}

void MarshalString(System::String ^ s, std::string& os) {
	using namespace System::Runtime::InteropServices;
	const char* chars =
		(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
	os = chars;
	Marshal::FreeHGlobal(System::IntPtr((void*)chars));
}

std::vector<std::string> toVector(cli::array<System::String^>^ input_arr) {
	std::vector<std::string> temp_vector;
	for each (System::String^ var in input_arr)
	{
		std::string temp_sring;
		MarshalString(var, temp_sring);
		temp_vector.push_back(temp_sring);
	}
	return temp_vector;
}