/*!
 * Tokenizer.cpp
 *
 * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 * 
 * @Author:  NADAL Jean-Baptiste
 * @Date: 04/05/2015
 *
 */

/*-------------------------------- INCLUDES ---------------------------------*/

#include "Tokenizer.h"

const std::string Tokenizer::DELIMITERS(" \t\n\r");


/*!  ----------------------------------------------------------------------------
 *  @fn Tokenizer
 *
 *  @brief Constructor of the Tokenizer.
 */
Tokenizer::Tokenizer (const std::string& aString) :
	m_offset (0),
	m_string (aString), 
	m_delimiters (DELIMITERS) {}


/*!  ----------------------------------------------------------------------------
 *  @fn Tokenizer
 *
 *  @brief Constructor of the Tokenizer.
 */
Tokenizer::Tokenizer (const std::string& aString, const std::string& aDelimiters) :
    m_offset (0), 
    m_string (aString), 
    m_delimiters (aDelimiters) {}


/*!  ----------------------------------------------------------------------------
 *  @fn NextToken
 *
 *  @brief Return true if we found another token.
 */
bool Tokenizer::NextToken (void) 
{
    return NextToken (m_delimiters);
}


/*!  ----------------------------------------------------------------------------
 *  @fn NextToken
 *
 *  @brief Return true if we found another token.
 */
bool Tokenizer::NextToken (const std::string& delimiters) 
{
	size_t i = m_string.find_first_not_of (delimiters, m_offset);
	
	if (std::string::npos == i) {

		m_offset = m_string.length ();
		return false;
	}

    size_t j = m_string.find_first_of (delimiters, i);

    if (std::string::npos == j) {

		m_token = m_string.substr (i);
		m_offset = m_string.length ();
		return true;
	}

    m_token = m_string.substr (i, j - i);
    m_offset = j;

    return true;
}


/*!  ----------------------------------------------------------------------------
 *  @fn GetToken
 *
 *  @brief Return the Current Token.
 */
const std::string Tokenizer::GetToken (void) const
{
	return m_token;
}


/*!  ----------------------------------------------------------------------------
 *  @fn Reset
 *
 *  @brief Return the Current Token navigation.
 */
void Tokenizer::Reset (void)
{
	m_offset = 0;
}
