Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
connection_string_parser.c@16:18e7ebd42bb2, 2016-12-14 (annotated)
- Committer:
- Azure.IoT.Build
- Date:
- Wed Dec 14 16:00:59 2016 -0800
- Revision:
- 16:18e7ebd42bb2
- Child:
- 18:6d8a413a4d9a
1.1.2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT.Build | 16:18e7ebd42bb2 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT.Build | 16:18e7ebd42bb2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT.Build | 16:18e7ebd42bb2 | 3 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 4 | #include "azure_c_shared_utility/connection_string_parser.h" |
Azure.IoT.Build | 16:18e7ebd42bb2 | 5 | #include "azure_c_shared_utility/map.h" |
Azure.IoT.Build | 16:18e7ebd42bb2 | 6 | #include "azure_c_shared_utility/strings.h" |
Azure.IoT.Build | 16:18e7ebd42bb2 | 7 | #include "azure_c_shared_utility/string_tokenizer.h" |
Azure.IoT.Build | 16:18e7ebd42bb2 | 8 | #include <stdbool.h> |
Azure.IoT.Build | 16:18e7ebd42bb2 | 9 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT.Build | 16:18e7ebd42bb2 | 10 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 11 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_001: [connectionstringparser_parse shall parse all key value pairs from the connection_string passed in as argument and return a new map that holds the key/value pairs.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 12 | MAP_HANDLE connectionstringparser_parse(STRING_HANDLE connection_string) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 13 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 14 | MAP_HANDLE result; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 15 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 16 | if (connection_string == NULL) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 17 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 18 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_002: [If connection_string is NULL then connectionstringparser_parse shall fail and return NULL.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 19 | result = NULL; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 20 | LogError("NULL connection string passed to tokenizer.\r\n"); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 21 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 22 | else |
Azure.IoT.Build | 16:18e7ebd42bb2 | 23 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 24 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_003: [connectionstringparser_parse shall create a STRING tokenizer to be used for parsing the connection string, by calling STRING_TOKENIZER_create.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 25 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_004: [connectionstringparser_parse shall start scanning at the beginning of the connection string.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 26 | STRING_TOKENIZER_HANDLE tokenizer = STRING_TOKENIZER_create(connection_string); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 27 | if (tokenizer == NULL) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 28 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 29 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_015: [If STRING_TOKENIZER_create fails, connectionstringparser_parse shall fail and return NULL.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 30 | result = NULL; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 31 | LogError("Error creating STRING tokenizer.\r\n"); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 32 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 33 | else |
Azure.IoT.Build | 16:18e7ebd42bb2 | 34 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 35 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_016: [2 STRINGs shall be allocated in order to hold the to be parsed key and value tokens.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 36 | STRING_HANDLE token_key_string = STRING_new(); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 37 | if (token_key_string == NULL) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 38 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 39 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_017: [If allocating the STRINGs fails connectionstringparser_parse shall fail and return NULL.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 40 | result = NULL; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 41 | LogError("Error creating key token STRING.\r\n"); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 42 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 43 | else |
Azure.IoT.Build | 16:18e7ebd42bb2 | 44 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 45 | STRING_HANDLE token_value_string = STRING_new(); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 46 | if (token_value_string == NULL) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 47 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 48 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_017: [If allocating the STRINGs fails connectionstringparser_parse shall fail and return NULL.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 49 | result = NULL; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 50 | LogError("Error creating value token STRING.\r\n"); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 51 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 52 | else |
Azure.IoT.Build | 16:18e7ebd42bb2 | 53 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 54 | result = Map_Create(NULL); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 55 | if (result == NULL) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 56 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 57 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_018: [If creating the result map fails, then connectionstringparser_parse shall return NULL.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 58 | LogError("Error creating Map\r\n"); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 59 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 60 | else |
Azure.IoT.Build | 16:18e7ebd42bb2 | 61 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 62 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_005: [The following actions shall be repeated until parsing is complete:] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 63 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_006: [connectionstringparser_parse shall find a token (the key of the key/value pair) delimited by the `=` character, by calling STRING_TOKENIZER_get_next_token.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 64 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_007: [If STRING_TOKENIZER_get_next_token fails, parsing shall be considered complete.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 65 | while (STRING_TOKENIZER_get_next_token(tokenizer, token_key_string, "=") == 0) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 66 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 67 | bool is_error = false; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 68 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 69 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_008: [connectionstringparser_parse shall find a token (the value of the key/value pair) delimited by the `;` character, by calling STRING_TOKENIZER_get_next_token.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 70 | if (STRING_TOKENIZER_get_next_token(tokenizer, token_value_string, ";") != 0) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 71 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 72 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_009: [If STRING_TOKENIZER_get_next_token fails, connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 73 | is_error = true; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 74 | LogError("Error reading value token from the connection string.\r\n"); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 75 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 76 | else |
Azure.IoT.Build | 16:18e7ebd42bb2 | 77 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 78 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_011: [The C strings for the key and value shall be extracted from the previously parsed STRINGs by using STRING_c_str.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 79 | const char* token = STRING_c_str(token_key_string); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 80 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_013: [If STRING_c_str fails then connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 81 | if ((token == NULL) || |
Azure.IoT.Build | 16:18e7ebd42bb2 | 82 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_019: [If the key length is zero then connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 83 | (strlen(token) == 0)) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 84 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 85 | is_error = true; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 86 | LogError("The key token is NULL or empty.\r\n"); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 87 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 88 | else |
Azure.IoT.Build | 16:18e7ebd42bb2 | 89 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 90 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_011: [The C strings for the key and value shall be extracted from the previously parsed STRINGs by using STRING_c_str.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 91 | const char* value = STRING_c_str(token_value_string); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 92 | if (value == NULL) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 93 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 94 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_013: [If STRING_c_str fails then connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 95 | is_error = true; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 96 | LogError("Could not get C string for value token.\r\n"); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 97 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 98 | else |
Azure.IoT.Build | 16:18e7ebd42bb2 | 99 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 100 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_010: [The key and value shall be added to the result map by using Map_Add.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 101 | if (Map_Add(result, token, value) != 0) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 102 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 103 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_012: [If Map_Add fails connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 104 | is_error = true; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 105 | LogError("Could not add the key/value pair to the result map.\r\n"); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 106 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 107 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 108 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 109 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 110 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 111 | if (is_error) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 112 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 113 | LogError("Error parsing connection string.\r\n"); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 114 | Map_Destroy(result); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 115 | result = NULL; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 116 | break; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 117 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 118 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 119 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 120 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 121 | STRING_delete(token_value_string); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 122 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 123 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 124 | STRING_delete(token_key_string); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 125 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 126 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 127 | /* Codes_SRS_CONNECTIONSTRINGPARSER_01_014: [After the parsing is complete the previously allocated STRINGs and STRING tokenizer shall be freed by calling STRING_TOKENIZER_destroy.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 128 | STRING_TOKENIZER_destroy(tokenizer); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 129 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 130 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 131 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 132 | return result; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 133 | } |