/* Codes_SRS_CONNECTIONSTRINGPARSER_21_022: [connectionstringparser_splitHostName_from_char shall split the provided hostName in name and suffix.]*/
int connectionstringparser_splitHostName_from_char(const char* hostName, STRING_HANDLE nameString, STRING_HANDLE suffixString)
{
    int result;
    const char* runHostName = hostName;

    if ((hostName == NULL) || ((*hostName) == '\0') || ((*hostName) == '.') || (nameString == NULL) || (suffixString == NULL))
    {
        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_026: [If the hostName is NULL, connectionstringparser_splitHostName_from_char shall return __FAILURE__.]*/
        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_027: [If the hostName is an empty string, connectionstringparser_splitHostName_from_char shall return __FAILURE__.]*/
        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_028: [If the nameString is NULL, connectionstringparser_splitHostName_from_char shall return __FAILURE__.]*/
        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_029: [If the suffixString is NULL, connectionstringparser_splitHostName_from_char shall return __FAILURE__.]*/
        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_030: [If the hostName is not a valid host name, connectionstringparser_splitHostName_from_char shall return __FAILURE__.]*/
        result = __FAILURE__;
    }
    else
    {
        while ((*runHostName) != '\0')
        {
            if ((*runHostName) == '.')
            {
                /* Codes_SRS_CONNECTIONSTRINGPARSER_21_023: [connectionstringparser_splitHostName_from_char shall copy all characters, from the beginning of the hostName to the first `.` to the nameString.]*/
                /* Codes_SRS_CONNECTIONSTRINGPARSER_21_024: [connectionstringparser_splitHostName_from_char shall copy all characters, from the first `.` to the end of the hostName, to the suffixString.]*/
                runHostName++;
                break;
            }
            runHostName++;
        }
     
        if ((*runHostName) == '\0')
        {
            /* Codes_SRS_CONNECTIONSTRINGPARSER_21_030: [If the hostName is not a valid host name, connectionstringparser_splitHostName_from_char shall return __FAILURE__.]*/
            result = __FAILURE__;
        }
        else
        {
            /* Codes_SRS_CONNECTIONSTRINGPARSER_21_023: [connectionstringparser_splitHostName_from_char shall copy all characters, from the beginning of the hostName to the first `.` to the nameString.]*/
            if (STRING_copy_n(nameString, hostName, runHostName - hostName - 1) != 0)
            {
                /* Codes_SRS_CONNECTIONSTRINGPARSER_21_031: [If connectionstringparser_splitHostName_from_char get error copying the name to the nameString, it shall return __FAILURE__.]*/
                result = __FAILURE__;
            }
            /* Codes_SRS_CONNECTIONSTRINGPARSER_21_024: [connectionstringparser_splitHostName_from_char shall copy all characters, from the first `.` to the end of the hostName, to the suffixString.]*/
            else if (STRING_copy(suffixString, runHostName) != 0)
            {
                /* Codes_SRS_CONNECTIONSTRINGPARSER_21_032: [If connectionstringparser_splitHostName_from_char get error copying the suffix to the suffixString, it shall return __FAILURE__.]*/
                result = __FAILURE__;
            }
            else
            {
                /* Codes_SRS_CONNECTIONSTRINGPARSER_21_025: [If connectionstringparser_splitHostName_from_char get success splitting the hostName, it shall return 0.]*/
                result = 0;
            }
        }
    }

    return result;
}
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 = __LINE__;
    }
    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 = __LINE__;
        }
        else if (delimitterSize == 0)
        {
            LogError("Empty delimiters parameter.\r\n");
            result = __LINE__;
        }
        else
        {
            size_t i;
            size_t j;
            /* 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++)
            {
                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 = __LINE__;
            }
            else
            {
                bool foundDelimitter = false;
                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.\r\n");
                    result = __LINE__;
                }
                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;
}