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