Browse Source

faux: Conversions for l and ul

Serj Kalichev 4 years ago
parent
commit
95a9520044
5 changed files with 67 additions and 40 deletions
  1. 6 6
      faux/Makefile.am
  2. 3 0
      faux/conv/Makefile.am
  3. 58 26
      faux/conv/conv.c
  4. 0 4
      faux/conv/module.am
  5. 0 4
      faux/conv/private.h

+ 6 - 6
faux/Makefile.am

@@ -6,20 +6,21 @@ nobase_include_HEADERS += \
 	faux/types.h \
 	faux/ctype.h \
 	faux/str.h \
-	faux/sysdb.h
+	faux/sysdb.h \
+	faux/conv.h
 
 #	faux/argv.h \
 #	faux/list.h \
 #	faux/dump.h \
 #	faux/system.h \
 #	faux/ini.h \
-#	faux/log.h \
-#	faux/conv.h
+#	faux/log.h
 
 EXTRA_DIST += \
 	faux/ctype/Makefile.am \
 	faux/str/Makefile.am \
-	faux/sysdb/Makefile.am
+	faux/sysdb/Makefile.am \
+	faux/conv/Makefile.am
 
 #	faux/argv/module.am \
 #	faux/list/module.am \
@@ -27,12 +28,12 @@ EXTRA_DIST += \
 #	faux/system/module.am \
 #	faux/ini/module.am \
 #	faux/log/module.am \
-#	faux/conv/module.am \
 #	faux/README
 
 include $(top_srcdir)/faux/ctype/Makefile.am
 include $(top_srcdir)/faux/str/Makefile.am
 include $(top_srcdir)/faux/sysdb/Makefile.am
+include $(top_srcdir)/faux/conv/Makefile.am
 
 #include $(top_srcdir)/faux/argv/module.am
 #include $(top_srcdir)/faux/list/module.am
@@ -40,4 +41,3 @@ include $(top_srcdir)/faux/sysdb/Makefile.am
 #include $(top_srcdir)/faux/system/module.am
 #include $(top_srcdir)/faux/ini/module.am
 #include $(top_srcdir)/faux/log/module.am
-#include $(top_srcdir)/faux/conv/module.am

+ 3 - 0
faux/conv/Makefile.am

@@ -0,0 +1,3 @@
+libfaux_la_SOURCES += \
+	faux/conv/conv.c
+

+ 58 - 26
faux/conv/conv.c

@@ -1,43 +1,75 @@
-/*
- * conv.c
+/** @file conv.c
+ * @brief Functions to convert from string to integer.
  */
-#include "private.h"
 
 #include <stdlib.h>
 #include <errno.h>
 #include <limits.h>
 
-/*--------------------------------------------------------- */
-int lub_conv_atol(const char *str, long int *val, int base)
-{
+
+/** @brief Converts string to long int
+ *
+ * Converts string to long int and check for overflow and valid
+ * input values. Function indicates error by return value. It
+ * returns the convertion result by second argument.
+ *
+ * @param [in] str Input string to convert.
+ * @param [out] val Pointer to result value.
+ * @param [in] base Base to convert.
+ * @return 0 - success, < 0 - error
+ */
+int faux_conv_atol(const char *str, long int *val, int base) {
+
 	char *endptr;
-	long int tmp;
+	long int res;
 
-	errno = 0;
-	tmp = strtol(str, &endptr, base);
-	if ((errno != 0) || (endptr == str))
+	res = strtol(str, &endptr, base);
+	// Check fof overflow
+	if (((LONG_MIN == res) || (LONG_MAX == res)) && (ERANGE == errno))
 		return -1;
-	*val = tmp;
+	// No valid digits at all
+	if ((0 == res) && ((endptr == str) || (errno != 0)))
+		return -1;
+	*val = res;
+
 	return 0;
 }
 
-/*--------------------------------------------------------- */
-int lub_conv_atoul(const char *str, unsigned long int *val, int base)
-{
-	long int tmp;
-	if (lub_conv_atol(str, &tmp, base) < 0)
+
+/** @brief Converts string to unsigned long int
+ *
+ * Converts string to unsigned long int and check for overflow and valid
+ * input values. Function indicates error by return value. It
+ * returns the convertion result by second argument.
+ *
+ * @param [in] str Input string to convert.
+ * @param [out] val Pointer to result value.
+ * @param [in] base Base to convert.
+ * @return 0 - success, < 0 - error
+ */
+int faux_conv_atoul(const char *str, unsigned long int *val, int base) {
+
+	char *endptr;
+	unsigned long int res;
+
+	res = strtoul(str, &endptr, base);
+	// Check fof overflow
+	if ((ULONG_MAX == res) && (ERANGE == errno))
 		return -1;
-	if ((tmp < 0) || (tmp > LONG_MAX)) /* Overflow */
+	// No valid digits at all
+	if ((0 == res) && ((endptr == str) || (errno != 0)))
 		return -1;
-	*val = tmp;
+	*val = res;
+
 	return 0;
 }
 
+
 /*--------------------------------------------------------- */
-int lub_conv_atos(const char *str, short *val, int base)
+int faux_conv_atos(const char *str, short *val, int base)
 {
 	long int tmp;
-	if (lub_conv_atol(str, &tmp, base) < 0)
+	if (faux_conv_atol(str, &tmp, base) < 0)
 		return -1;
 	if ((tmp < SHRT_MIN) || (tmp > SHRT_MAX)) /* Overflow */
 		return -1;
@@ -46,10 +78,10 @@ int lub_conv_atos(const char *str, short *val, int base)
 }
 
 /*--------------------------------------------------------- */
-int lub_conv_atous(const char *str, unsigned short *val, int base)
+int faux_conv_atous(const char *str, unsigned short *val, int base)
 {
 	unsigned long int tmp;
-	if (lub_conv_atoul(str, &tmp, base) < 0)
+	if (faux_conv_atoul(str, &tmp, base) < 0)
 		return -1;
 	if (tmp > USHRT_MAX) /* Overflow */
 		return -1;
@@ -58,10 +90,10 @@ int lub_conv_atous(const char *str, unsigned short *val, int base)
 }
 
 /*--------------------------------------------------------- */
-int lub_conv_atoi(const char *str, int *val, int base)
+int faux_conv_atoi(const char *str, int *val, int base)
 {
 	long int tmp;
-	if (lub_conv_atol(str, &tmp, base) < 0)
+	if (faux_conv_atol(str, &tmp, base) < 0)
 		return -1;
 	if ((tmp < INT_MIN) || (tmp > INT_MAX)) /* Overflow */
 		return -1;
@@ -70,10 +102,10 @@ int lub_conv_atoi(const char *str, int *val, int base)
 }
 
 /*--------------------------------------------------------- */
-int lub_conv_atoui(const char *str, unsigned int *val, int base)
+int faux_conv_atoui(const char *str, unsigned int *val, int base)
 {
 	unsigned long int tmp;
-	if (lub_conv_atoul(str, &tmp, base) < 0)
+	if (faux_conv_atoul(str, &tmp, base) < 0)
 		return -1;
 	if (tmp > UINT_MAX) /* Overflow */
 		return -1;

+ 0 - 4
faux/conv/module.am

@@ -1,4 +0,0 @@
-liblub_la_SOURCES += \
-	lub/conv/conv.c \
-	lub/conv/private.h
-

+ 0 - 4
faux/conv/private.h

@@ -1,4 +0,0 @@
-/*
- * private.h
- */
-#include "lub/conv.h"