Mark Radbourne / Mbed 2 deprecated iothub_client_sample_amqp

Dependencies:   EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed

Fork of iothub_client_sample_amqp by Azure IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers string_tokenizer.c Source File

string_tokenizer.c

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 //
00005 // PUT NO INCLUDES BEFORE HERE
00006 //
00007 #ifdef _CRTDBG_MAP_ALLOC
00008 #include <crtdbg.h>
00009 #endif
00010 #include "azure_c_shared_utility/gballoc.h"
00011 
00012 //
00013 // PUT NO CLIENT LIBRARY INCLUDES BEFORE HERE
00014 //
00015 
00016 #include <stdbool.h>
00017 #include "azure_c_shared_utility/string_tokenizer.h"
00018 #include "azure_c_shared_utility/xlogging.h"
00019 #include "azure_c_shared_utility/crt_abstractions.h"
00020 
00021 typedef struct STRING_TOKEN_TAG
00022 {
00023     const char* inputString;
00024     const char* currentPos;
00025     size_t sizeOfinputString;
00026 } STRING_TOKEN;
00027 
00028 STRING_TOKENIZER_HANDLE STRING_TOKENIZER_create(STRING_HANDLE handle)
00029 {
00030     STRING_TOKENIZER_HANDLE result;
00031 
00032     /* Codes_SRS_STRING_04_001: [STRING_TOKENIZER_create shall return an NULL STRING_TOKENIZER_HANDLE if parameter handle is NULL] */
00033     if (handle == NULL)
00034     {
00035         LogError("Invalid Argument. Handle cannot be NULL.");
00036         result = NULL;
00037     }
00038     else
00039     {
00040         /* Codes_SRS_STRING_04_002: [STRING_TOKENIZER_create shall allocate a new STRING_TOKENIZER_HANDLE having the content of the STRING_HANDLE copied and current position pointing at the beginning of the string] */
00041         result = STRING_TOKENIZER_create_from_char(STRING_c_str(handle));
00042     }
00043 
00044     return result;
00045 }
00046 
00047 extern STRING_TOKENIZER_HANDLE STRING_TOKENIZER_create_from_char(const char* input)
00048 {
00049     STRING_TOKEN *result;
00050     char* inputStringToMalloc;
00051 
00052     /* Codes_SRS_STRING_07_001: [STRING_TOKENIZER_create shall return an NULL STRING_TOKENIZER_HANDLE if parameter input is NULL] */
00053     if (input == NULL)
00054     {
00055         LogError("Invalid Argument. Handle cannot be NULL.");
00056         result = NULL;
00057     }
00058     /* Codes_SRS_STRING_07_002: [STRING_TOKENIZER_create shall allocate a new STRING_TOKENIZER_HANDLE having the content of the STRING_HANDLE copied and current position pointing at the beginning of the string] */
00059     else if ((result = (STRING_TOKEN*)malloc(sizeof(STRING_TOKEN))) == NULL)
00060     {
00061         LogError("Memory Allocation failed. Cannot allocate STRING_TOKENIZER.");
00062     }
00063     else if ((mallocAndStrcpy_s(&inputStringToMalloc, input)) != 0)
00064     {
00065         LogError("Memory Allocation Failed. Cannot allocate and copy string Content.");
00066         free(result);
00067         result = NULL;
00068     }
00069     else
00070     {
00071         result->inputString = inputStringToMalloc;
00072         result->currentPos = result->inputString; //Current Pos will point to the initial position of Token.
00073         result->sizeOfinputString = strlen(result->inputString); //Calculate Size of Current String
00074     }
00075     return (STRING_TOKENIZER_HANDLE)result;
00076 }
00077 
00078 int STRING_TOKENIZER_get_next_token(STRING_TOKENIZER_HANDLE tokenizer, STRING_HANDLE output, const char* delimiters)
00079 {
00080     int result;
00081     /* Codes_SRS_STRING_04_004: [STRING_TOKENIZER_get_next_token shall return a nonzero value if any of the 3 parameters is NULL] */
00082     if (tokenizer == NULL || output == NULL || delimiters == NULL)
00083     {
00084         result = __LINE__;
00085     }
00086     else 
00087     {
00088         STRING_TOKEN* token = (STRING_TOKEN*)tokenizer;
00089         /* 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.] */
00090         size_t remainingInputStringSize = token->sizeOfinputString - (token->currentPos - token->inputString);
00091         size_t delimitterSize = strlen(delimiters);
00092 
00093         /* First Check if we reached the end of the string*/
00094         /* Codes_SRS_STRING_TOKENIZER_04_014: [STRING_TOKENIZER_get_next_token shall return nonzero value if t contains an empty string.] */
00095         if (remainingInputStringSize == 0)
00096         {
00097             result = __LINE__;
00098         }
00099         else if (delimitterSize == 0)
00100         {
00101             LogError("Empty delimiters parameter.");
00102             result = __LINE__;
00103         }
00104         else
00105         {
00106             size_t i;
00107             /* 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] */
00108             for (i = 0; i < remainingInputStringSize; i++)
00109             {
00110                 size_t j;
00111 
00112                 bool foundDelimitter = false;
00113                 for (j = 0; j < delimitterSize; j++)
00114                 {
00115                     if (token->currentPos[i] == delimiters[j])
00116                     {
00117                         foundDelimitter = true;
00118                         break;
00119                     }
00120                 }
00121 
00122                 /* Codes_SRS_STRING_04_007: [If such a character is found, STRING_TOKENIZER_get_next_token consider it as the start of a token.] */
00123                 if (!foundDelimitter)
00124                 {
00125                     break;
00126                 }
00127             }
00128 
00129             /* 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).] */
00130             //At this point update Current Pos to the character of the last token found or end of String.
00131             token->currentPos += i; 
00132             
00133             //Update the remainingInputStringSize
00134             remainingInputStringSize -= i;
00135             
00136             /* 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).] */
00137             if (remainingInputStringSize == 0)
00138             {
00139                 result = __LINE__;
00140             }
00141             else
00142             {
00143                 bool foundDelimitter = false;
00144                 char* endOfTokenPosition=NULL;
00145                 size_t amountOfCharactersToCopy;
00146                 size_t j;
00147                 //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. 
00148                 /*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.] */
00149                 /* 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.] */
00150                 /* 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.] */
00151                 for (j = 0; j < delimitterSize; j++)
00152                 {
00153                     if ((endOfTokenPosition = strchr(token->currentPos, delimiters[j])) != NULL)
00154                     {
00155                         foundDelimitter = true;
00156                         break;
00157                     }
00158                 }
00159 
00160                 //If token not found, than update the EndOfToken to the end of the inputString;
00161                 if (endOfTokenPosition == NULL)
00162                 {
00163                     amountOfCharactersToCopy = remainingInputStringSize;
00164                 }
00165                 else
00166                 {
00167                     amountOfCharactersToCopy = endOfTokenPosition - token->currentPos;
00168                 }
00169                 
00170                 //copy here the string to output. 
00171                 if (STRING_copy_n(output, token->currentPos, amountOfCharactersToCopy) != 0)
00172                 {
00173                     LogError("Problem copying token to output String.");
00174                     result = __LINE__;
00175                 }
00176                 else
00177                 {
00178                     //Update the Current position.
00179                     //Check if end of String reached so, currentPos points to the end of String.
00180                     if (foundDelimitter)
00181                     {
00182                         token->currentPos += amountOfCharactersToCopy + 1;
00183                     }
00184                     else
00185                     {
00186                         token->currentPos += amountOfCharactersToCopy;
00187                     }
00188                     
00189                     result = 0; //Result will be on the output. 
00190                 }
00191             }
00192         }
00193     }
00194 
00195     return result;
00196 }
00197 
00198 
00199 /* Codes_SRS_STRING_TOKENIZER_04_012: [STRING_TOKENIZER_destroy shall free the memory allocated by the STRING_TOKENIZER_create ] */
00200 void STRING_TOKENIZER_destroy(STRING_TOKENIZER_HANDLE t)
00201 {
00202     /* Codes_SRS_STRING_TOKENIZER_04_013: [When the t argument is NULL, then STRING_TOKENIZER_destroy shall not attempt to free] */
00203     if (t != NULL)
00204     {
00205         STRING_TOKEN* value = (STRING_TOKEN*)t;
00206         free((char*)value->inputString);
00207         value->inputString = NULL;
00208         free(value);
00209     }
00210 }
00211