Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Revision:
18:6d8a413a4d9a
Parent:
16:18e7ebd42bb2
Child:
21:b92006c5b9ff
--- a/connection_string_parser.c	Sun Jan 08 11:12:54 2017 -0800
+++ b/connection_string_parser.c	Fri Jan 13 18:41:15 2017 -0800
@@ -8,6 +8,28 @@
 #include <stdbool.h>
 #include "azure_c_shared_utility/xlogging.h"
 
+
+MAP_HANDLE connectionstringparser_parse_from_char(const char* connection_string)
+{
+    MAP_HANDLE result;
+    STRING_HANDLE connString = NULL;
+
+    /* Codes_SRS_CONNECTIONSTRINGPARSER_21_020: [connectionstringparser_parse_from_char shall create a STRING_HANDLE from the connection_string passed in as argument and parse it using the connectionstringparser_parse.]*/
+    if ((connString = STRING_construct(connection_string)) == NULL)
+    {
+        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_021: [If connectionstringparser_parse_from_char get error creating a STRING_HANDLE, it shall return NULL.]*/
+        LogError("Error constructing connection String");
+        result = NULL;
+    }
+    else
+    {
+        result = connectionstringparser_parse(connString);
+    }
+
+    return result;
+}
+
+
 /* 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.] */
 MAP_HANDLE connectionstringparser_parse(STRING_HANDLE connection_string)
 {
@@ -17,7 +39,7 @@
     {
         /* Codes_SRS_CONNECTIONSTRINGPARSER_01_002: [If connection_string is NULL then connectionstringparser_parse shall fail and return NULL.] */
         result = NULL;
-        LogError("NULL connection string passed to tokenizer.\r\n");
+        LogError("NULL connection string passed to tokenizer.");
     }
     else
     {
@@ -28,7 +50,7 @@
         {
             /* Codes_SRS_CONNECTIONSTRINGPARSER_01_015: [If STRING_TOKENIZER_create fails, connectionstringparser_parse shall fail and return NULL.] */
             result = NULL;
-            LogError("Error creating STRING tokenizer.\r\n");
+            LogError("Error creating STRING tokenizer.");
         }
         else
         {
@@ -38,7 +60,7 @@
             {
                 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_017: [If allocating the STRINGs fails connectionstringparser_parse shall fail and return NULL.] */
                 result = NULL;
-                LogError("Error creating key token STRING.\r\n");
+                LogError("Error creating key token STRING.");
             }
             else
             {
@@ -47,7 +69,7 @@
                 {
                     /* Codes_SRS_CONNECTIONSTRINGPARSER_01_017: [If allocating the STRINGs fails connectionstringparser_parse shall fail and return NULL.] */
                     result = NULL;
-                    LogError("Error creating value token STRING.\r\n");
+                    LogError("Error creating value token STRING.");
                 }
                 else
                 {
@@ -55,7 +77,7 @@
                     if (result == NULL)
                     {
                         /* Codes_SRS_CONNECTIONSTRINGPARSER_01_018: [If creating the result map fails, then connectionstringparser_parse shall return NULL.] */
-                        LogError("Error creating Map\r\n");
+                        LogError("Error creating Map.");
                     }
                     else
                     {
@@ -71,7 +93,7 @@
                             {
                                 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_009: [If STRING_TOKENIZER_get_next_token fails, connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
                                 is_error = true;
-                                LogError("Error reading value token from the connection string.\r\n");
+                                LogError("Error reading value token from the connection string.");
                             }
                             else
                             {
@@ -83,7 +105,7 @@
                                     (strlen(token) == 0))
                                 {
                                     is_error = true;
-                                    LogError("The key token is NULL or empty.\r\n");
+                                    LogError("The key token is NULL or empty.");
                                 }
                                 else
                                 {
@@ -93,7 +115,7 @@
                                     {
                                         /* Codes_SRS_CONNECTIONSTRINGPARSER_01_013: [If STRING_c_str fails then connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
                                         is_error = true;
-                                        LogError("Could not get C string for value token.\r\n");
+                                        LogError("Could not get C string for value token.");
                                     }
                                     else
                                     {
@@ -102,7 +124,7 @@
                                         {
                                             /* Codes_SRS_CONNECTIONSTRINGPARSER_01_012: [If Map_Add fails connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
                                             is_error = true;
-                                            LogError("Could not add the key/value pair to the result map.\r\n");
+                                            LogError("Could not add the key/value pair to the result map.");
                                         }
                                     }
                                 }
@@ -110,7 +132,7 @@
 
                             if (is_error)
                             {
-                                LogError("Error parsing connection string.\r\n");
+                                LogError("Error parsing connection string.");
                                 Map_Destroy(result);
                                 result = NULL;
                                 break;
@@ -131,3 +153,81 @@
 
     return result;
 }
+
+/* Codes_SRS_CONNECTIONSTRINGPARSER_21_022: [connectionstringparser_splitHostName_from_char shall split the provided hostName in name and suffix.]*/
+int connectionstringparser_splitHostName_from_char(const char* hostName, STRING_HANDLE nameString, STRING_HANDLE suffixString)
+{
+    int result;
+    const char* runHostName = hostName;
+
+    if ((hostName == NULL) || ((*hostName) == '\0') || ((*hostName) == '.') || (nameString == NULL) || (suffixString == NULL))
+    {
+        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_026: [If the hostName is NULL, connectionstringparser_splitHostName_from_char shall return __LINE__.]*/
+        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_027: [If the hostName is an empty string, connectionstringparser_splitHostName_from_char shall return __LINE__.]*/
+        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_028: [If the nameString is NULL, connectionstringparser_splitHostName_from_char shall return __LINE__.]*/
+        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_029: [If the suffixString is NULL, connectionstringparser_splitHostName_from_char shall return __LINE__.]*/
+        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_030: [If the hostName is not a valid host name, connectionstringparser_splitHostName_from_char shall return __LINE__.]*/
+        result = __LINE__;
+    }
+    else
+    {
+        while ((*runHostName) != '\0')
+        {
+            if ((*runHostName) == '.')
+            {
+                /* Codes_SRS_CONNECTIONSTRINGPARSER_21_023: [connectionstringparser_splitHostName_from_char shall copy all characters, from the beginning of the hostName to the first `.` to the nameString.]*/
+                /* Codes_SRS_CONNECTIONSTRINGPARSER_21_024: [connectionstringparser_splitHostName_from_char shall copy all characters, from the first `.` to the end of the hostName, to the suffixString.]*/
+                runHostName++;
+                break;
+            }
+            runHostName++;
+        }
+     
+        if ((*runHostName) == '\0')
+        {
+            /* Codes_SRS_CONNECTIONSTRINGPARSER_21_030: [If the hostName is not a valid host name, connectionstringparser_splitHostName_from_char shall return __LINE__.]*/
+            result = __LINE__;
+        }
+        else
+        {
+            /* Codes_SRS_CONNECTIONSTRINGPARSER_21_023: [connectionstringparser_splitHostName_from_char shall copy all characters, from the beginning of the hostName to the first `.` to the nameString.]*/
+            if (STRING_copy_n(nameString, hostName, runHostName - hostName - 1) != 0)
+            {
+                /* Codes_SRS_CONNECTIONSTRINGPARSER_21_031: [If connectionstringparser_splitHostName_from_char get error copying the name to the nameString, it shall return __LINE__.]*/
+                result = __LINE__;
+            }
+            /* Codes_SRS_CONNECTIONSTRINGPARSER_21_024: [connectionstringparser_splitHostName_from_char shall copy all characters, from the first `.` to the end of the hostName, to the suffixString.]*/
+            else if (STRING_copy(suffixString, runHostName) != 0)
+            {
+                /* Codes_SRS_CONNECTIONSTRINGPARSER_21_032: [If connectionstringparser_splitHostName_from_char get error copying the suffix to the suffixString, it shall return __LINE__.]*/
+                result = __LINE__;
+            }
+            else
+            {
+                /* Codes_SRS_CONNECTIONSTRINGPARSER_21_025: [If connectionstringparser_splitHostName_from_char get success splitting the hostName, it shall return 0.]*/
+                result = 0;
+            }
+        }
+    }
+
+    return result;
+}
+
+
+int connectionstringparser_splitHostName(STRING_HANDLE hostNameString, STRING_HANDLE nameString, STRING_HANDLE suffixString)
+{
+    int result;
+
+    if (hostNameString == NULL)
+    {
+        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_034: [If the hostNameString is NULL, connectionstringparser_splitHostName shall return __LINE__.]*/
+        result = __LINE__;
+    }
+    else
+    {
+        /* Codes_SRS_CONNECTIONSTRINGPARSER_21_033: [connectionstringparser_splitHostName shall convert the hostNameString to a connection_string passed in as argument, and call connectionstringparser_splitHostName_from_char.]*/
+        result = connectionstringparser_splitHostName_from_char(STRING_c_str(hostNameString), nameString, suffixString);
+    }
+
+    return result;
+}