//
// Simple string parser.
//
// Copyright (c) 2003 by Octane Project Group
// All Rights Reserved
//
// http://octane.sourceforge.net/
//

/// @file   stringparser.hpp
/// @brief  Simple string parser.
/// @author Jibz
/// @date   2003.08.01

#ifndef OCTANE_STRINGPARSER_HPP_INCLUDED
#define OCTANE_STRINGPARSER_HPP_INCLUDED

#include <string>

/// Simple string parser.
///
/// Breaks up a string into space-delimited tokens.
///
/// @if developer
/// The class operates on a copy of the string, to avoid any
/// synchronization issues. This should not be a problem for
/// performance if the string implementation is reference
/// counted.
/// @endif
///
/// @ingroup Utility
///
class stringparser {

public:
    /// Constructor.
    /// @param _s - the string to parse.
    stringparser(const std::string &_s)	: s(_s), idx(0) { ; }

    /// Get next token from string.
    /// @return the next token.
    /// @todo should be able to handle quoted tokens.
    std::string get_token()
    {
	if (idx == std::string::npos) return "";

	std::string::size_type p = s.find_first_not_of(' ', idx);

	if (p == std::string::npos) return "";

	idx = s.find_first_of(' ', p);

	if (idx == std::string::npos) return s.substr(p);

	return s.substr(p, idx - p);
    }

private:
    const std::string s;
    std::string::size_type idx;
};

#endif // OCTANE_STRINGPARSER_HPP_INCLUDED
