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 connection_string_parser.c Source File

connection_string_parser.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 #include "azure_c_shared_utility/connection_string_parser.h"
00005 #include "azure_c_shared_utility/map.h"
00006 #include "azure_c_shared_utility/strings.h"
00007 #include "azure_c_shared_utility/string_tokenizer.h"
00008 #include <stdbool.h>
00009 #include "azure_c_shared_utility/xlogging.h"
00010 
00011 /* 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.] */
00012 MAP_HANDLE connectionstringparser_parse(STRING_HANDLE connection_string)
00013 {
00014     MAP_HANDLE result;
00015 
00016     if (connection_string == NULL)
00017     {
00018         /* Codes_SRS_CONNECTIONSTRINGPARSER_01_002: [If connection_string is NULL then connectionstringparser_parse shall fail and return NULL.] */
00019         result = NULL;
00020         LogError("NULL connection string passed to tokenizer.\r\n");
00021     }
00022     else
00023     {
00024         /* 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.] */
00025         /* Codes_SRS_CONNECTIONSTRINGPARSER_01_004: [connectionstringparser_parse shall start scanning at the beginning of the connection string.] */
00026         STRING_TOKENIZER_HANDLE tokenizer = STRING_TOKENIZER_create(connection_string);
00027         if (tokenizer == NULL)
00028         {
00029             /* Codes_SRS_CONNECTIONSTRINGPARSER_01_015: [If STRING_TOKENIZER_create fails, connectionstringparser_parse shall fail and return NULL.] */
00030             result = NULL;
00031             LogError("Error creating STRING tokenizer.\r\n");
00032         }
00033         else
00034         {
00035             /* Codes_SRS_CONNECTIONSTRINGPARSER_01_016: [2 STRINGs shall be allocated in order to hold the to be parsed key and value tokens.] */
00036             STRING_HANDLE token_key_string = STRING_new();
00037             if (token_key_string == NULL)
00038             {
00039                 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_017: [If allocating the STRINGs fails connectionstringparser_parse shall fail and return NULL.] */
00040                 result = NULL;
00041                 LogError("Error creating key token STRING.\r\n");
00042             }
00043             else
00044             {
00045                 STRING_HANDLE token_value_string = STRING_new();
00046                 if (token_value_string == NULL)
00047                 {
00048                     /* Codes_SRS_CONNECTIONSTRINGPARSER_01_017: [If allocating the STRINGs fails connectionstringparser_parse shall fail and return NULL.] */
00049                     result = NULL;
00050                     LogError("Error creating value token STRING.\r\n");
00051                 }
00052                 else
00053                 {
00054                     result = Map_Create(NULL);
00055                     if (result == NULL)
00056                     {
00057                         /* Codes_SRS_CONNECTIONSTRINGPARSER_01_018: [If creating the result map fails, then connectionstringparser_parse shall return NULL.] */
00058                         LogError("Error creating Map\r\n");
00059                     }
00060                     else
00061                     {
00062                         /* Codes_SRS_CONNECTIONSTRINGPARSER_01_005: [The following actions shall be repeated until parsing is complete:] */
00063                         /* 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.] */
00064                         /* Codes_SRS_CONNECTIONSTRINGPARSER_01_007: [If STRING_TOKENIZER_get_next_token fails, parsing shall be considered complete.] */
00065                         while (STRING_TOKENIZER_get_next_token(tokenizer, token_key_string, "=") == 0)
00066                         {
00067                             bool is_error = false;
00068 
00069                             /* 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.] */
00070                             if (STRING_TOKENIZER_get_next_token(tokenizer, token_value_string, ";") != 0)
00071                             {
00072                                 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_009: [If STRING_TOKENIZER_get_next_token fails, connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
00073                                 is_error = true;
00074                                 LogError("Error reading value token from the connection string.\r\n");
00075                             }
00076                             else
00077                             {
00078                                 /* 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.] */
00079                                 const char* token = STRING_c_str(token_key_string);
00080                                 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_013: [If STRING_c_str fails then connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
00081                                 if ((token == NULL) ||
00082                                     /* Codes_SRS_CONNECTIONSTRINGPARSER_01_019: [If the key length is zero then connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
00083                                     (strlen(token) == 0))
00084                                 {
00085                                     is_error = true;
00086                                     LogError("The key token is NULL or empty.\r\n");
00087                                 }
00088                                 else
00089                                 {
00090                                     /* 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.] */
00091                                     const char* value = STRING_c_str(token_value_string);
00092                                     if (value == NULL)
00093                                     {
00094                                         /* Codes_SRS_CONNECTIONSTRINGPARSER_01_013: [If STRING_c_str fails then connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
00095                                         is_error = true;
00096                                         LogError("Could not get C string for value token.\r\n");
00097                                     }
00098                                     else
00099                                     {
00100                                         /* Codes_SRS_CONNECTIONSTRINGPARSER_01_010: [The key and value shall be added to the result map by using Map_Add.] */
00101                                         if (Map_Add(result, token, value) != 0)
00102                                         {
00103                                             /* Codes_SRS_CONNECTIONSTRINGPARSER_01_012: [If Map_Add fails connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
00104                                             is_error = true;
00105                                             LogError("Could not add the key/value pair to the result map.\r\n");
00106                                         }
00107                                     }
00108                                 }
00109                             }
00110 
00111                             if (is_error)
00112                             {
00113                                 LogError("Error parsing connection string.\r\n");
00114                                 Map_Destroy(result);
00115                                 result = NULL;
00116                                 break;
00117                             }
00118                         }
00119                     }
00120 
00121                     STRING_delete(token_value_string);
00122                 }
00123 
00124                 STRING_delete(token_key_string);
00125             }
00126 
00127             /* 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.] */
00128             STRING_TOKENIZER_destroy(tokenizer);
00129         }
00130     }
00131 
00132     return result;
00133 }