Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
AzureIoTClient
Date:
Sat Jan 28 09:35:22 2017 -0800
Revision:
19:2e0811512ceb
Parent:
18:6d8a413a4d9a
Child:
21:b92006c5b9ff
1.1.6

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