#include <iostream>
#include <string>
#include <my++/stype.h>

//#include "split.h"

using namespace std;
using namespace my;

// utility function to display raw data
const std::string dump_data (const std::string &s) {
	std::string result;
	std::string::const_iterator i;
	char cbuf[10];

	for (i = s.begin(); i != s.end(); ++i) {
		switch (*i) {
			case '\r':
			case '\n':
			case '\t':
			case ' ':
				snprintf(cbuf, sizeof(cbuf), "[0x%X]", (int)(*i));
				result += cbuf;
				break;
				result += *i;
				break;
			default:
				if (isprint(*i))
					result += *i;
		}
	}

	return result;
}

// overloaded version of above, but for string lists
const std::string dump_data (const std::list<std::string> &sl) {
	std::string result;
	char cbuf[80];
	snprintf(cbuf, sizeof(cbuf), "String List (%d elements):\n", sl.size());
	result = cbuf;

	for (std::list<std::string>::const_iterator li = sl.begin();
			li != sl.end();
			++li ) {
		result += "   + \'" + (*li) + "\'\n";
	}

	return result;
}


int main (int, char**) {
	std::string s("          TEST MESSAGE          ");
	std::cout << "ltrim: \"" << dump_data(ltrim(s)) << '\"' << endl;
	std::cout << "rtrim: \"" << dump_data(rtrim(s)) << '\"' << endl;
	std::cout << "both : \"" << dump_data(ltrim(rtrim(s))) << '\"' << endl;
	std::cout << "both : \"" << dump_data(rtrim(ltrim(s))) << '\"' << endl;
	s = "\t\n\rTEST MESSAGE\r\n\t";
	std::cout << "ltrim: \"" << dump_data(ltrim(s)) << '\"' << endl;
	std::cout << "rtrim: \"" << dump_data(rtrim(s)) << '\"' << endl;
	std::cout << "both : \"" << dump_data(ltrim(rtrim(s))) << '\"' << endl;
	std::cout << "both : \"" << dump_data(rtrim(ltrim(s))) << '\"' << endl;
	s = "          TEST MESSAGE          ";
	std::cout << "split(\"" << s << "\"): " << dump_data(split(s));
	s = ltrim(rtrim(s));
	std::cout << "split(\"" << s << "\"): " << dump_data(split(s));
	s = " This, is a , test,message   ,,,";
	std::cout << "split(\"" << s << "\", \",\" strip=true): " << dump_data(split(s, ',', true));
	s = "root:x:0:0:root:/root:/bin/bash";
	std::cout << "split(\"" << s << "\", \":\"): " << dump_data(split(s, ':'));
	return 0;
}

