summaryrefslogtreecommitdiffstats
path: root/cpukit/libmisc/stringto
diff options
context:
space:
mode:
authorJoel Sherrill <joel.sherrill@OARcorp.com>2009-07-23 14:32:34 +0000
committerJoel Sherrill <joel.sherrill@OARcorp.com>2009-07-23 14:32:34 +0000
commit48751ab095bbbb23489c2e3243f57571cbfa9153 (patch)
tree7a15647c01c3acdb2d1c61142bb13616ad03a2aa /cpukit/libmisc/stringto
parent2009-07-23 Santosh G Vattam <vattam.santosh@gmail.com> (diff)
downloadrtems-48751ab095bbbb23489c2e3243f57571cbfa9153.tar.bz2
2009-07-23 Joel Sherrill <joel.sherrill@oarcorp.com>
* libmisc/Makefile.am, libmisc/shell/main_chmod.c, libmisc/shell/main_mdump.c, libmisc/shell/main_medit.c, libmisc/shell/main_mfill.c, libmisc/shell/main_mmove.c, libmisc/shell/main_msdosfmt.c, libmisc/shell/main_mwdump.c, libmisc/shell/main_sleep.c, libmisc/shell/main_umask.c, libmisc/shell/shell_script.c, libmisc/stringto/stringto.h, libmisc/stringto/stringto_template.h: Convert return type from bool to rtems_status_code and add rtems_string_to_pointer. Perform associated clean up and changes for return type change. * libmisc/stringto/stringtopointer.c: New file.
Diffstat (limited to 'cpukit/libmisc/stringto')
-rw-r--r--cpukit/libmisc/stringto/stringto.h78
-rw-r--r--cpukit/libmisc/stringto/stringto_template.h41
-rw-r--r--cpukit/libmisc/stringto/stringtopointer.c25
3 files changed, 109 insertions, 35 deletions
diff --git a/cpukit/libmisc/stringto/stringto.h b/cpukit/libmisc/stringto/stringto.h
index 2e7fe151c6..e0ba24416c 100644
--- a/cpukit/libmisc/stringto/stringto.h
+++ b/cpukit/libmisc/stringto/stringto.h
@@ -12,6 +12,28 @@
#ifndef __STRING_TO_A_TYPE_h__
#define __STRING_TO_A_TYPE_h__
+#include <rtems.h>
+
+/**
+ * @brief Convert String to Pointer (with validation)
+ *
+ * This method converts a string to a pointer (void *) with
+ * basic numeric validation.
+ *
+ * @param[in] s is the string to convert
+ * @param[in] n points to the variable to place the converted output in
+ * @param[in] endptr is used to keep track of the position in the string
+ *
+ * @return This method returns RTEMS_SUCCESSFUL on successful conversion
+ * and *n is filled in. Otherwise, the status indicates the
+ * source of the error.
+ */
+rtems_status_code rtems_string_to_pointer(
+ const char *s,
+ void **n,
+ char **endptr
+);
+
/**
* @brief Convert String to Unsigned Character (with validation)
*
@@ -23,10 +45,11 @@
* @param[in] endptr is used to keep track of the position in the string
* @param[in] base is the expected base of the number
*
- * @return This method returns true on successful conversion and *n is
- * filled in.
+ * @return This method returns RTEMS_SUCCESSFUL on successful conversion
+ * and *n is filled in. Otherwise, the status indicates the
+ * source of the error.
*/
-bool rtems_string_to_unsigned_char(
+rtems_status_code rtems_string_to_unsigned_char(
const char *s,
unsigned char *n,
char **endptr,
@@ -43,10 +66,11 @@ bool rtems_string_to_unsigned_char(
* @param[in] endptr is used to keep track of the position in the string
* @param[in] base is the expected base of the number
*
- * @return This method returns true on successful conversion and *n is
- * filled in.
+ * @return This method returns RTEMS_SUCCESSFUL on successful conversion
+ * and *n is filled in. Otherwise, the status indicates the
+ * source of the error.
*/
-bool rtems_string_to_int(
+rtems_status_code rtems_string_to_int(
const char *s,
int *n,
char **endptr,
@@ -64,10 +88,11 @@ bool rtems_string_to_int(
* @param[in] endptr is used to keep track of the position in the string
* @param[in] base is the expected base of the number
*
- * @return This method returns true on successful conversion and *n is
- * filled in.
+ * @return This method returns RTEMS_SUCCESSFUL on successful conversion
+ * and *n is filled in. Otherwise, the status indicates the
+ * source of the error.
*/
-bool rtems_string_to_long(
+rtems_status_code rtems_string_to_long(
const char *s,
long *n,
char **endptr,
@@ -85,10 +110,11 @@ bool rtems_string_to_long(
* @param[in] endptr is used to keep track of the position in the string
* @param[in] base is the expected base of the number
*
- * @return This method returns true on successful conversion and *n is
- * filled in.
+ * @return This method returns RTEMS_SUCCESSFUL on successful conversion
+ * and *n is filled in. Otherwise, the status indicates the
+ * source of the error.
*/
-bool rtems_string_to_unsigned_long(
+rtems_status_code rtems_string_to_unsigned_long(
const char *s,
unsigned long *n,
char **endptr,
@@ -106,10 +132,11 @@ bool rtems_string_to_unsigned_long(
* @param[in] endptr is used to keep track of the position in the string
* @param[in] base is the expected base of the number
*
- * @return This method returns true on successful conversion and *n is
- * filled in.
+ * @return This method returns RTEMS_SUCCESSFUL on successful conversion
+ * and *n is filled in. Otherwise, the status indicates the
+ * source of the error.
*/
-bool rtems_string_to_long_long(
+rtems_status_code rtems_string_to_long_long(
const char *s,
long long *n,
char **endptr,
@@ -127,10 +154,11 @@ bool rtems_string_to_long_long(
* @param[in] endptr is used to keep track of the position in the string
* @param[in] base is the expected base of the number
*
- * @return This method returns true on successful conversion and *n is
- * filled in.
+ * @return This method returns RTEMS_SUCCESSFUL on successful conversion
+ * and *n is filled in. Otherwise, the status indicates the
+ * source of the error.
*/
-bool rtems_string_to_unsigned_long_long(
+rtems_status_code rtems_string_to_unsigned_long_long(
const char *s,
unsigned long long *n,
char **endptr,
@@ -146,10 +174,11 @@ bool rtems_string_to_unsigned_long_long(
* @param[in] n points to the variable to place the converted output in
* @param[in] endptr is used to keep track of the position in the string
*
- * @return This method returns true on successful conversion and *n is
- * filled in.
+ * @return This method returns RTEMS_SUCCESSFUL on successful conversion
+ * and *n is filled in. Otherwise, the status indicates the
+ * source of the error.
*/
-bool rtems_string_to_float(
+rtems_status_code rtems_string_to_float(
const char *s,
float *n,
char **endptr
@@ -164,10 +193,11 @@ bool rtems_string_to_float(
* @param[in] n points to the variable to place the converted output in
* @param[in] endptr is used to keep track of the position in the string
*
- * @return This method returns true on successful conversion and *n is
- * filled in.
+ * @return This method returns RTEMS_SUCCESSFUL on successful conversion
+ * and *n is filled in. Otherwise, the status indicates the
+ * source of the error.
*/
-bool rtems_string_to_double(
+rtems_status_code rtems_string_to_double(
const char *s,
double *n,
char **endptr
diff --git a/cpukit/libmisc/stringto/stringto_template.h b/cpukit/libmisc/stringto/stringto_template.h
index 501f4393f8..863c98ff81 100644
--- a/cpukit/libmisc/stringto/stringto_template.h
+++ b/cpukit/libmisc/stringto/stringto_template.h
@@ -9,18 +9,31 @@
* $Id$
*/
+#include <rtems/stringto.h>
+
#include <errno.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
-#include <math.h>
+/*
+ * If we are doing floating point conversion, then we need math.h
+ */
+#if defined(STRING_TO_FLOAT)
+ #include <math.h>
+#endif
+
+#include <rtems.h>
/*
* This file is designed to be included multiple times to instantiate
* it and should NOT be protected against multiple inclusions.
*/
+#if defined(STRING_TO_POINTER)
+ #define STRING_TO_INTEGER
+#endif
+
#if !defined(STRING_TO_FLOAT) && !defined(STRING_TO_INTEGER)
#error "Neither STRING_TO_FLOAT nor STRING_TO_INTEGER defined"
#endif
@@ -52,27 +65,33 @@
#define ZERO 0
#endif
-bool STRING_TO_NAME (
+#if !defined(STRING_TO_INPUT_TYPE)
+ #define STRING_TO_INPUT_TYPE STRING_TO_TYPE
+#endif
+
+rtems_status_code STRING_TO_NAME (
const char *s,
STRING_TO_TYPE *n,
char **endptr
- #if defined(STRING_TO_INTEGER)
+ #if defined(STRING_TO_INTEGER) && !defined(STRING_TO_POINTER)
,
int base
#endif
)
{
- STRING_TO_TYPE result;
- char *end;
+ STRING_TO_INPUT_TYPE result;
+ char *end;
if ( !n )
- return false;
+ return RTEMS_INVALID_ADDRESS;
errno = 0;
*n = 0;
#ifdef STRING_TO_FLOAT
result = STRING_TO_METHOD( s, &end );
+ #elif defined(STRING_TO_POINTER)
+ result = STRING_TO_METHOD( s, &end, 16 );
#elif defined(STRING_TO_INTEGER)
result = STRING_TO_METHOD( s, &end, base );
#endif
@@ -83,25 +102,25 @@ bool STRING_TO_NAME (
/* nothing was converted */
if ( end == s )
- return false;
+ return RTEMS_NOT_DEFINED;
/* there was a conversion error */
if ( (result == ZERO) && errno )
- return false;
+ return RTEMS_INVALID_NUMBER;
#ifdef STRING_TO_MAX
/* there was an overflow */
if ( (result == STRING_TO_MAX) && (errno == ERANGE))
- return false;
+ return RTEMS_INVALID_NUMBER;
#endif
#ifdef STRING_TO_MIN
/* there was an underflow */
if ( (result == STRING_TO_MIN) && (errno == ERANGE))
- return false;
+ return RTEMS_INVALID_NUMBER;
#endif
*n = (STRING_TO_TYPE) result;
- return true;
+ return RTEMS_SUCCESSFUL;
}
diff --git a/cpukit/libmisc/stringto/stringtopointer.c b/cpukit/libmisc/stringto/stringtopointer.c
new file mode 100644
index 0000000000..ca1e480d2d
--- /dev/null
+++ b/cpukit/libmisc/stringto/stringtopointer.c
@@ -0,0 +1,25 @@
+/*
+ * COPYRIGHT (c) 2009.
+ * On-Line Applications Research Corporation (OAR).
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+/*
+ * Instantiate an error checking wrapper for strtoul which is
+ * used to input a (void *)
+ *
+ * NOTE: This is only an appropriate implementation when unsigned long
+ * can represent a void *
+ */
+#define STRING_TO_POINTER
+#define STRING_TO_TYPE void *
+#define STRING_TO_INPUT_TYPE unsigned long
+#define STRING_TO_NAME rtems_string_to_pointer
+#define STRING_TO_METHOD strtoul
+#define STRING_TO_MAX ULONG_MAX
+#include "stringto_template.h"