]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[Core] Add switch_safe_atol() and switch_safe_atoll() functions. Add a unit-test. 1299/head
authorsuchi.sahoo <suchi.sahoo@five9.com>
Mon, 9 Aug 2021 18:28:56 +0000 (21:28 +0300)
committerAndrey Volk <andywolk@gmail.com>
Fri, 13 Aug 2021 17:05:29 +0000 (20:05 +0300)
src/include/switch_utils.h
tests/unit/switch_core.c

index 20655ecd4e374ab5096c760e8bded3ab74fe1e96..24c6151331f55a3b99c069e0b2c5fe841ee05c5f 100644 (file)
@@ -831,10 +831,10 @@ static inline char *switch_clean_name_string(char *s)
 
 
 /*!
-  \brief Turn a string into a number (default if NULL)
+  \brief Turn a string into an integer (default if NULL)
   \param nptr the string
   \param dft the default
-  \return the number version of the string or the default
+  \return the integer version of the string or the default
 */
 static inline int switch_safe_atoi(const char *nptr, int dft)
 {
@@ -842,6 +842,30 @@ static inline int switch_safe_atoi(const char *nptr, int dft)
 }
 
 
+/*!
+  \brief Turn a string into a long integer (default if NULL)
+  \param nptr the string
+  \param dft the default
+  \return the long integer version of the string or the default
+*/
+static inline long int switch_safe_atol(const char *nptr, long int dft)
+{
+       return nptr ? atol(nptr) : dft;
+}
+
+
+/*!
+  \brief Turn a string into a long long integer (default if NULL)
+  \param nptr the string
+  \param dft the default
+  \return the long long integer version of the string or the default
+*/
+static inline long long int switch_safe_atoll(const char *nptr, long long int dft)
+{
+       return nptr ? atoll(nptr) : dft;
+}
+
+
 /*!
   \brief Free a pointer and set it to NULL unless it already is NULL
   \param it the pointer
index bba7272cac13847ac1530a7403b44ffcd9fe590a..ed84e39c06079746a6a0ced1da2623fd611df93a 100644 (file)
@@ -223,6 +223,22 @@ FST_CORE_BEGIN("./conf")
 #endif
                }
                FST_TEST_END()
+
+               FST_TEST_BEGIN(test_switch_safe_atoXX)
+               {
+                       fst_check_int_equals(switch_safe_atoi("1", 0), 1);
+                       fst_check_int_equals(switch_safe_atoi("", 2), 0);
+                       fst_check_int_equals(switch_safe_atoi(0, 3), 3);
+
+                       fst_check_int_equals(switch_safe_atol("9275806", 0), 9275806);
+                       fst_check_int_equals(switch_safe_atol("", 2), 0);
+                       fst_check_int_equals(switch_safe_atol(0, 3), 3);
+
+                       fst_check_int_equals(switch_safe_atoll("9275806", 0), 9275806);
+                       fst_check_int_equals(switch_safe_atoll("", 2), 0);
+                       fst_check_int_equals(switch_safe_atoll(0, 3), 3);
+               }
+               FST_TEST_END()
        }
        FST_SUITE_END()
 }