int STRING_TOKENIZER_get_next_token()

in src/string_tokenizer.c [69:187]


int STRING_TOKENIZER_get_next_token(STRING_TOKENIZER_HANDLE tokenizer, STRING_HANDLE output, const char* delimiters)
{
    int result;
    /* Codes_SRS_STRING_04_004: [STRING_TOKENIZER_get_next_token shall return a nonzero value if any of the 3 parameters is NULL] */
    if (tokenizer == NULL || output == NULL || delimiters == NULL)
    {
        result = MU_FAILURE;
    }
    else
    {
        STRING_TOKEN* token = (STRING_TOKEN*)tokenizer;
        /* Codes_SRS_STRING_04_011: [Each subsequent call to STRING_TOKENIZER_get_next_token starts searching from the saved position on t and behaves as described above.] */
        size_t remainingInputStringSize = token->sizeOfinputString - (token->currentPos - token->inputString);
        size_t delimitterSize = strlen(delimiters);

        /* First Check if we reached the end of the string*/
        /* Codes_SRS_STRING_TOKENIZER_04_014: [STRING_TOKENIZER_get_next_token shall return nonzero value if t contains an empty string.] */
        if (remainingInputStringSize == 0)
        {
            result = MU_FAILURE;
        }
        else if (delimitterSize == 0)
        {
            LogError("Empty delimiters parameter.");
            result = MU_FAILURE;
        }
        else
        {
            size_t i;
            /* Codes_SRS_STRING_04_005: [STRING_TOKENIZER_get_next_token searches the string inside STRING_TOKENIZER_HANDLE for the first character that is NOT contained in the current delimiter] */
            for (i = 0; i < remainingInputStringSize; i++)
            {
                size_t j;

                bool foundDelimitter = false;
                for (j = 0; j < delimitterSize; j++)
                {
                    if (token->currentPos[i] == delimiters[j])
                    {
                        foundDelimitter = true;
                        break;
                    }
                }

                /* Codes_SRS_STRING_04_007: [If such a character is found, STRING_TOKENIZER_get_next_token consider it as the start of a token.] */
                if (!foundDelimitter)
                {
                    break;
                }
            }

            /* Codes_SRS_STRING_04_006: [If no such character is found, then STRING_TOKENIZER_get_next_token shall return a nonzero Value (You've reach the end of the string or the string consists with only delimiters).] */
            //At this point update Current Pos to the character of the last token found or end of String.
            token->currentPos += i;

            //Update the remainingInputStringSize
            remainingInputStringSize -= i;

            /* Codes_SRS_STRING_04_006: [If no such character is found, then STRING_TOKENIZER_get_next_token shall return a nonzero Value (You've reach the end of the string or the string consists with only delimiters).] */
            if (remainingInputStringSize == 0)
            {
                result = MU_FAILURE;
            }
            else
            {
                bool foundDelimitter = false;
                const char* endOfTokenPosition=NULL;
                size_t amountOfCharactersToCopy;
                size_t j;
                //At this point the Current Pos is pointing to a character that is point to a nonDelimiter. So, now search for a Delimiter, till the end of the String.
                /*Codes_SRS_STRING_04_008: [STRING_TOKENIZER_get_next_token than searches from the start of a token for a character that is contained in the delimiters string.] */
                /* Codes_SRS_STRING_04_009: [If no such character is found, STRING_TOKENIZER_get_next_token extends the current token to the end of the string inside t, copies the token to output and returns 0.] */
                /* Codes_SRS_STRING_04_010: [If such a character is found, STRING_TOKENIZER_get_next_token consider it the end of the token and copy it's content to output, updates the current position inside t to the next character and returns 0.] */
                for (j = 0; j < delimitterSize; j++)
                {
                    if ((endOfTokenPosition = strchr(token->currentPos, delimiters[j])) != NULL)
                    {
                        foundDelimitter = true;
                        break;
                    }
                }

                //If token not found, than update the EndOfToken to the end of the inputString;
                if (endOfTokenPosition == NULL)
                {
                    amountOfCharactersToCopy = remainingInputStringSize;
                }
                else
                {
                    amountOfCharactersToCopy = endOfTokenPosition - token->currentPos;
                }

                //copy here the string to output.
                if (STRING_copy_n(output, token->currentPos, amountOfCharactersToCopy) != 0)
                {
                    LogError("Problem copying token to output String.");
                    result = MU_FAILURE;
                }
                else
                {
                    //Update the Current position.
                    //Check if end of String reached so, currentPos points to the end of String.
                    if (foundDelimitter)
                    {
                        token->currentPos += amountOfCharactersToCopy + 1;
                    }
                    else
                    {
                        token->currentPos += amountOfCharactersToCopy;
                    }

                    result = 0; //Result will be on the output.
                }
            }
        }
    }

    return result;
}