Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Fri Jan 13 18:41:15 2017 -0800
Revision:
18:6d8a413a4d9a
Parent:
7:1af47e3a19b6
Child:
19:2e0811512ceb
1.1.4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:fa2de1b79154 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:fa2de1b79154 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:fa2de1b79154 3
Azure.IoT Build 0:fa2de1b79154 4 //
Azure.IoT Build 0:fa2de1b79154 5 // PUT NO INCLUDES BEFORE HERE
Azure.IoT Build 0:fa2de1b79154 6 //
Azure.IoT Build 0:fa2de1b79154 7 #ifdef _CRTDBG_MAP_ALLOC
Azure.IoT Build 0:fa2de1b79154 8 #include <crtdbg.h>
Azure.IoT Build 0:fa2de1b79154 9 #endif
Azure.IoT Build 0:fa2de1b79154 10 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:fa2de1b79154 11
Azure.IoT Build 0:fa2de1b79154 12 //
Azure.IoT Build 0:fa2de1b79154 13 // PUT NO CLIENT LIBRARY INCLUDES BEFORE HERE
Azure.IoT Build 0:fa2de1b79154 14 //
Azure.IoT Build 0:fa2de1b79154 15
AzureIoTClient 18:6d8a413a4d9a 16 #include <stdlib.h>
Azure.IoT Build 0:fa2de1b79154 17 #include <stdbool.h>
Azure.IoT Build 0:fa2de1b79154 18 #include "azure_c_shared_utility/string_tokenizer.h"
Azure.IoT Build 6:c55b013dfc2a 19 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 20 #include "azure_c_shared_utility/crt_abstractions.h"
Azure.IoT Build 0:fa2de1b79154 21
Azure.IoT Build 0:fa2de1b79154 22 typedef struct STRING_TOKEN_TAG
Azure.IoT Build 0:fa2de1b79154 23 {
Azure.IoT Build 0:fa2de1b79154 24 const char* inputString;
Azure.IoT Build 0:fa2de1b79154 25 const char* currentPos;
Azure.IoT Build 0:fa2de1b79154 26 size_t sizeOfinputString;
Azure.IoT Build 0:fa2de1b79154 27 } STRING_TOKEN;
Azure.IoT Build 0:fa2de1b79154 28
Azure.IoT Build 0:fa2de1b79154 29 STRING_TOKENIZER_HANDLE STRING_TOKENIZER_create(STRING_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 30 {
Azure.IoT Build 0:fa2de1b79154 31 STRING_TOKENIZER_HANDLE result;
Azure.IoT Build 0:fa2de1b79154 32
Azure.IoT Build 0:fa2de1b79154 33 /* Codes_SRS_STRING_04_001: [STRING_TOKENIZER_create shall return an NULL STRING_TOKENIZER_HANDLE if parameter handle is NULL] */
Azure.IoT Build 0:fa2de1b79154 34 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 35 {
AzureIoTClient 1:9190c0f4d23a 36 LogError("Invalid Argument. Handle cannot be NULL.");
Azure.IoT Build 0:fa2de1b79154 37 result = NULL;
Azure.IoT Build 0:fa2de1b79154 38 }
Azure.IoT Build 0:fa2de1b79154 39 else
Azure.IoT Build 0:fa2de1b79154 40 {
Azure.IoT Build 0:fa2de1b79154 41 /* 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] */
Azure.IoT Build 0:fa2de1b79154 42 result = STRING_TOKENIZER_create_from_char(STRING_c_str(handle));
Azure.IoT Build 0:fa2de1b79154 43 }
Azure.IoT Build 0:fa2de1b79154 44
Azure.IoT Build 0:fa2de1b79154 45 return result;
Azure.IoT Build 0:fa2de1b79154 46 }
Azure.IoT Build 0:fa2de1b79154 47
Azure.IoT Build 0:fa2de1b79154 48 extern STRING_TOKENIZER_HANDLE STRING_TOKENIZER_create_from_char(const char* input)
Azure.IoT Build 0:fa2de1b79154 49 {
Azure.IoT Build 0:fa2de1b79154 50 STRING_TOKEN *result;
Azure.IoT Build 0:fa2de1b79154 51 char* inputStringToMalloc;
Azure.IoT Build 0:fa2de1b79154 52
Azure.IoT Build 0:fa2de1b79154 53 /* Codes_SRS_STRING_07_001: [STRING_TOKENIZER_create shall return an NULL STRING_TOKENIZER_HANDLE if parameter input is NULL] */
Azure.IoT Build 0:fa2de1b79154 54 if (input == NULL)
Azure.IoT Build 0:fa2de1b79154 55 {
AzureIoTClient 1:9190c0f4d23a 56 LogError("Invalid Argument. Handle cannot be NULL.");
Azure.IoT Build 0:fa2de1b79154 57 result = NULL;
Azure.IoT Build 0:fa2de1b79154 58 }
Azure.IoT Build 0:fa2de1b79154 59 /* 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] */
Azure.IoT Build 0:fa2de1b79154 60 else if ((result = (STRING_TOKEN*)malloc(sizeof(STRING_TOKEN))) == NULL)
Azure.IoT Build 0:fa2de1b79154 61 {
AzureIoTClient 1:9190c0f4d23a 62 LogError("Memory Allocation failed. Cannot allocate STRING_TOKENIZER.");
Azure.IoT Build 0:fa2de1b79154 63 }
Azure.IoT Build 0:fa2de1b79154 64 else if ((mallocAndStrcpy_s(&inputStringToMalloc, input)) != 0)
Azure.IoT Build 0:fa2de1b79154 65 {
AzureIoTClient 1:9190c0f4d23a 66 LogError("Memory Allocation Failed. Cannot allocate and copy string Content.");
Azure.IoT Build 0:fa2de1b79154 67 free(result);
Azure.IoT Build 0:fa2de1b79154 68 result = NULL;
Azure.IoT Build 0:fa2de1b79154 69 }
Azure.IoT Build 0:fa2de1b79154 70 else
Azure.IoT Build 0:fa2de1b79154 71 {
Azure.IoT Build 0:fa2de1b79154 72 result->inputString = inputStringToMalloc;
Azure.IoT Build 0:fa2de1b79154 73 result->currentPos = result->inputString; //Current Pos will point to the initial position of Token.
Azure.IoT Build 0:fa2de1b79154 74 result->sizeOfinputString = strlen(result->inputString); //Calculate Size of Current String
Azure.IoT Build 0:fa2de1b79154 75 }
Azure.IoT Build 0:fa2de1b79154 76 return (STRING_TOKENIZER_HANDLE)result;
Azure.IoT Build 0:fa2de1b79154 77 }
Azure.IoT Build 0:fa2de1b79154 78
Azure.IoT Build 0:fa2de1b79154 79 int STRING_TOKENIZER_get_next_token(STRING_TOKENIZER_HANDLE tokenizer, STRING_HANDLE output, const char* delimiters)
Azure.IoT Build 0:fa2de1b79154 80 {
Azure.IoT Build 0:fa2de1b79154 81 int result;
Azure.IoT Build 0:fa2de1b79154 82 /* Codes_SRS_STRING_04_004: [STRING_TOKENIZER_get_next_token shall return a nonzero value if any of the 3 parameters is NULL] */
Azure.IoT Build 0:fa2de1b79154 83 if (tokenizer == NULL || output == NULL || delimiters == NULL)
Azure.IoT Build 0:fa2de1b79154 84 {
Azure.IoT Build 0:fa2de1b79154 85 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 86 }
Azure.IoT Build 0:fa2de1b79154 87 else
Azure.IoT Build 0:fa2de1b79154 88 {
Azure.IoT Build 0:fa2de1b79154 89 STRING_TOKEN* token = (STRING_TOKEN*)tokenizer;
Azure.IoT Build 0:fa2de1b79154 90 /* 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.] */
Azure.IoT Build 0:fa2de1b79154 91 size_t remainingInputStringSize = token->sizeOfinputString - (token->currentPos - token->inputString);
Azure.IoT Build 0:fa2de1b79154 92 size_t delimitterSize = strlen(delimiters);
Azure.IoT Build 0:fa2de1b79154 93
Azure.IoT Build 0:fa2de1b79154 94 /* First Check if we reached the end of the string*/
Azure.IoT Build 0:fa2de1b79154 95 /* Codes_SRS_STRING_TOKENIZER_04_014: [STRING_TOKENIZER_get_next_token shall return nonzero value if t contains an empty string.] */
Azure.IoT Build 0:fa2de1b79154 96 if (remainingInputStringSize == 0)
Azure.IoT Build 0:fa2de1b79154 97 {
Azure.IoT Build 0:fa2de1b79154 98 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 99 }
Azure.IoT Build 0:fa2de1b79154 100 else if (delimitterSize == 0)
Azure.IoT Build 0:fa2de1b79154 101 {
AzureIoTClient 1:9190c0f4d23a 102 LogError("Empty delimiters parameter.");
Azure.IoT Build 0:fa2de1b79154 103 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 104 }
Azure.IoT Build 0:fa2de1b79154 105 else
Azure.IoT Build 0:fa2de1b79154 106 {
Azure.IoT Build 0:fa2de1b79154 107 size_t i;
Azure.IoT Build 0:fa2de1b79154 108 /* 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] */
Azure.IoT Build 0:fa2de1b79154 109 for (i = 0; i < remainingInputStringSize; i++)
Azure.IoT Build 0:fa2de1b79154 110 {
AzureIoTClient 7:1af47e3a19b6 111 size_t j;
AzureIoTClient 7:1af47e3a19b6 112
Azure.IoT Build 0:fa2de1b79154 113 bool foundDelimitter = false;
Azure.IoT Build 0:fa2de1b79154 114 for (j = 0; j < delimitterSize; j++)
Azure.IoT Build 0:fa2de1b79154 115 {
Azure.IoT Build 0:fa2de1b79154 116 if (token->currentPos[i] == delimiters[j])
Azure.IoT Build 0:fa2de1b79154 117 {
Azure.IoT Build 0:fa2de1b79154 118 foundDelimitter = true;
Azure.IoT Build 0:fa2de1b79154 119 break;
Azure.IoT Build 0:fa2de1b79154 120 }
Azure.IoT Build 0:fa2de1b79154 121 }
Azure.IoT Build 0:fa2de1b79154 122
Azure.IoT Build 0:fa2de1b79154 123 /* Codes_SRS_STRING_04_007: [If such a character is found, STRING_TOKENIZER_get_next_token consider it as the start of a token.] */
Azure.IoT Build 0:fa2de1b79154 124 if (!foundDelimitter)
Azure.IoT Build 0:fa2de1b79154 125 {
Azure.IoT Build 0:fa2de1b79154 126 break;
Azure.IoT Build 0:fa2de1b79154 127 }
Azure.IoT Build 0:fa2de1b79154 128 }
Azure.IoT Build 0:fa2de1b79154 129
Azure.IoT Build 0:fa2de1b79154 130 /* 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).] */
Azure.IoT Build 0:fa2de1b79154 131 //At this point update Current Pos to the character of the last token found or end of String.
Azure.IoT Build 0:fa2de1b79154 132 token->currentPos += i;
Azure.IoT Build 0:fa2de1b79154 133
Azure.IoT Build 0:fa2de1b79154 134 //Update the remainingInputStringSize
Azure.IoT Build 0:fa2de1b79154 135 remainingInputStringSize -= i;
Azure.IoT Build 0:fa2de1b79154 136
Azure.IoT Build 0:fa2de1b79154 137 /* 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).] */
Azure.IoT Build 0:fa2de1b79154 138 if (remainingInputStringSize == 0)
Azure.IoT Build 0:fa2de1b79154 139 {
Azure.IoT Build 0:fa2de1b79154 140 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 141 }
Azure.IoT Build 0:fa2de1b79154 142 else
Azure.IoT Build 0:fa2de1b79154 143 {
Azure.IoT Build 0:fa2de1b79154 144 bool foundDelimitter = false;
Azure.IoT Build 0:fa2de1b79154 145 char* endOfTokenPosition=NULL;
Azure.IoT Build 0:fa2de1b79154 146 size_t amountOfCharactersToCopy;
Azure.IoT Build 0:fa2de1b79154 147 size_t j;
Azure.IoT Build 0:fa2de1b79154 148 //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.
Azure.IoT Build 0:fa2de1b79154 149 /*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.] */
Azure.IoT Build 0:fa2de1b79154 150 /* 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.] */
Azure.IoT Build 0:fa2de1b79154 151 /* 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.] */
Azure.IoT Build 0:fa2de1b79154 152 for (j = 0; j < delimitterSize; j++)
Azure.IoT Build 0:fa2de1b79154 153 {
Azure.IoT Build 0:fa2de1b79154 154 if ((endOfTokenPosition = strchr(token->currentPos, delimiters[j])) != NULL)
Azure.IoT Build 0:fa2de1b79154 155 {
Azure.IoT Build 0:fa2de1b79154 156 foundDelimitter = true;
Azure.IoT Build 0:fa2de1b79154 157 break;
Azure.IoT Build 0:fa2de1b79154 158 }
Azure.IoT Build 0:fa2de1b79154 159 }
Azure.IoT Build 0:fa2de1b79154 160
Azure.IoT Build 0:fa2de1b79154 161 //If token not found, than update the EndOfToken to the end of the inputString;
Azure.IoT Build 0:fa2de1b79154 162 if (endOfTokenPosition == NULL)
Azure.IoT Build 0:fa2de1b79154 163 {
Azure.IoT Build 0:fa2de1b79154 164 amountOfCharactersToCopy = remainingInputStringSize;
Azure.IoT Build 0:fa2de1b79154 165 }
Azure.IoT Build 0:fa2de1b79154 166 else
Azure.IoT Build 0:fa2de1b79154 167 {
Azure.IoT Build 0:fa2de1b79154 168 amountOfCharactersToCopy = endOfTokenPosition - token->currentPos;
Azure.IoT Build 0:fa2de1b79154 169 }
Azure.IoT Build 0:fa2de1b79154 170
Azure.IoT Build 0:fa2de1b79154 171 //copy here the string to output.
Azure.IoT Build 0:fa2de1b79154 172 if (STRING_copy_n(output, token->currentPos, amountOfCharactersToCopy) != 0)
Azure.IoT Build 0:fa2de1b79154 173 {
AzureIoTClient 1:9190c0f4d23a 174 LogError("Problem copying token to output String.");
Azure.IoT Build 0:fa2de1b79154 175 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 176 }
Azure.IoT Build 0:fa2de1b79154 177 else
Azure.IoT Build 0:fa2de1b79154 178 {
Azure.IoT Build 0:fa2de1b79154 179 //Update the Current position.
Azure.IoT Build 0:fa2de1b79154 180 //Check if end of String reached so, currentPos points to the end of String.
Azure.IoT Build 0:fa2de1b79154 181 if (foundDelimitter)
Azure.IoT Build 0:fa2de1b79154 182 {
Azure.IoT Build 0:fa2de1b79154 183 token->currentPos += amountOfCharactersToCopy + 1;
Azure.IoT Build 0:fa2de1b79154 184 }
Azure.IoT Build 0:fa2de1b79154 185 else
Azure.IoT Build 0:fa2de1b79154 186 {
Azure.IoT Build 0:fa2de1b79154 187 token->currentPos += amountOfCharactersToCopy;
Azure.IoT Build 0:fa2de1b79154 188 }
Azure.IoT Build 0:fa2de1b79154 189
Azure.IoT Build 0:fa2de1b79154 190 result = 0; //Result will be on the output.
Azure.IoT Build 0:fa2de1b79154 191 }
Azure.IoT Build 0:fa2de1b79154 192 }
Azure.IoT Build 0:fa2de1b79154 193 }
Azure.IoT Build 0:fa2de1b79154 194 }
Azure.IoT Build 0:fa2de1b79154 195
Azure.IoT Build 0:fa2de1b79154 196 return result;
Azure.IoT Build 0:fa2de1b79154 197 }
Azure.IoT Build 0:fa2de1b79154 198
Azure.IoT Build 0:fa2de1b79154 199
Azure.IoT Build 0:fa2de1b79154 200 /* Codes_SRS_STRING_TOKENIZER_04_012: [STRING_TOKENIZER_destroy shall free the memory allocated by the STRING_TOKENIZER_create ] */
Azure.IoT Build 0:fa2de1b79154 201 void STRING_TOKENIZER_destroy(STRING_TOKENIZER_HANDLE t)
Azure.IoT Build 0:fa2de1b79154 202 {
Azure.IoT Build 0:fa2de1b79154 203 /* Codes_SRS_STRING_TOKENIZER_04_013: [When the t argument is NULL, then STRING_TOKENIZER_destroy shall not attempt to free] */
Azure.IoT Build 0:fa2de1b79154 204 if (t != NULL)
Azure.IoT Build 0:fa2de1b79154 205 {
Azure.IoT Build 0:fa2de1b79154 206 STRING_TOKEN* value = (STRING_TOKEN*)t;
Azure.IoT Build 0:fa2de1b79154 207 free((char*)value->inputString);
Azure.IoT Build 0:fa2de1b79154 208 value->inputString = NULL;
Azure.IoT Build 0:fa2de1b79154 209 free(value);
Azure.IoT Build 0:fa2de1b79154 210 }
Azure.IoT Build 0:fa2de1b79154 211 }
Azure.IoT Build 0:fa2de1b79154 212