/*
 *
 *    stype.cc
 *
 *    C++ source for string processing functions.
 *
 *    =========================================================================
 *
 *    Copyright (C) 1999-2003 by Matt Roberts, KK5JY.
 *
 *    This library is free software; 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.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    =========================================================================
 *
 */

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

/*
 *
 *   my::split (const std::string &)
 *
 */

std::list<std::string> my::split (const std::string &s) {
	std::string::const_iterator token_begin, token_end;
	std::string the_str = my::strip(s);
	std::list<std::string> result;

	// start by clearing the list
	result.clear();

	// if there is no data in the string, just return
	if (the_str.empty())
		return result;

	// loop through each character
	for (
			token_begin = the_str.begin();
			token_begin != the_str.end();
			++token_begin
		) {

		// if we fell off the end, just quit now
		if (token_end == the_str.end())
			return result;

		// if we have whitespace, keep going
		if (isspace(*token_begin)) {
			continue;
		} else {
			// here we have reached the start of a token
			for (
					token_end = token_begin;
					(token_end != the_str.end()) && (!isspace(*token_end));
					++token_end
				);

			// add the token to the list
			result.push_back(std::string(token_begin, token_end));

			// adjust to the new position in the string
			token_begin = token_end;
		}
	}

	return result;
}


/*
 *
 *   my::split (const std::string &, std::string::value_type delim)
 *
 */

std::list<std::string> my::split (const std::string &the_str, std::string::value_type delim, bool do_strip) {
	std::string::const_iterator token_begin, token_end;
	std::list<std::string> result;

	// start by clearing the list
	result.clear();

	// if there is no data in the string, just return
	if (the_str.empty())
		return result;

	// start at the beginning of the string
	token_begin = the_str.begin();

	// loop through each character
	for (
			token_end = the_str.begin();
			token_end != the_str.end();
			++token_end
		) {

		// if we fell off the end, just quit now
		if (token_end == the_str.end())
			return result;

		if (*token_end == delim) {
			// add the token to the list
			if (do_strip)  // strip, if requested
				result.push_back(my::strip(std::string(token_begin, token_end)));
			else
				result.push_back(std::string(token_begin, token_end));

			// adjust to the new position in the string
			token_begin = token_end + 1;
		}
	}

	// if we have one last token pending, add it
	if (token_begin != the_str.end()) {
		if (do_strip)  // strip, if requested
			result.push_back(my::strip(std::string(token_begin, token_end)));
		else
			result.push_back(std::string(token_begin, token_end));
	}

	// return the list
	return result;
}

/* End of file: stype.cc */
