diff options
-rw-r--r-- | cpukit/ChangeLog | 17 | ||||
-rw-r--r-- | cpukit/Makefile.am | 3 | ||||
-rw-r--r-- | cpukit/libmisc/Makefile.am | 31 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringto.h | 176 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringto_template.h | 107 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtodouble.c | 21 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtofloat.c | 20 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtoint.c | 20 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtolong.c | 21 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtolonglong.c | 21 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtounsignedchar.c | 20 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtounsignedint.c | 20 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtounsignedlong.c | 20 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtounsignedlonglong.c | 20 | ||||
-rw-r--r-- | cpukit/preinstall.am | 4 | ||||
-rw-r--r-- | cpukit/wrapup/Makefile.am | 1 |
16 files changed, 506 insertions, 16 deletions
diff --git a/cpukit/ChangeLog b/cpukit/ChangeLog index c69db9df1f..caee9546da 100644 --- a/cpukit/ChangeLog +++ b/cpukit/ChangeLog @@ -1,3 +1,20 @@ +2009-07-22 Joel Sherrill <joel.sherrill@oarcorp.com> + + * Makefile.am, preinstall.am, libmisc/Makefile.am, wrapup/Makefile.am: + Add the stringto family of string to number converters. These are + error checking wrappers for the strtoXXX methods and do their best to + return false if the conversion failed. The error checking required + for this is tedious and error prone. Hence better to have in a family + of helper routines. + * libmisc/stringto/stringto.h, libmisc/stringto/stringto_template.h, + libmisc/stringto/stringtodouble.c, libmisc/stringto/stringtofloat.c, + libmisc/stringto/stringtoint.c, libmisc/stringto/stringtolong.c, + libmisc/stringto/stringtolonglong.c, + libmisc/stringto/stringtounsignedchar.c, + libmisc/stringto/stringtounsignedint.c, + libmisc/stringto/stringtounsignedlong.c, + libmisc/stringto/stringtounsignedlonglong.c: New files. + 2009-07-21 Joel Sherrill <joel.sherrill@OARcorp.com> * posix/include/rtems/posix/key.h, posix/src/keycreate.c, diff --git a/cpukit/Makefile.am b/cpukit/Makefile.am index 2eb472fdcb..846f824260 100644 --- a/cpukit/Makefile.am +++ b/cpukit/Makefile.am @@ -132,6 +132,9 @@ if LIBSHELL include_rtems_HEADERS += libmisc/shell/shell.h libmisc/shell/shellconfig.h endif +## stringto +include_rtems_HEADERS += libmisc/stringto/stringto.h + ## i2c include_rtems_HEADERS += libi2c/libi2c.h diff --git a/cpukit/libmisc/Makefile.am b/cpukit/libmisc/Makefile.am index 85f7e79e7f..a84699f5bd 100644 --- a/cpukit/libmisc/Makefile.am +++ b/cpukit/libmisc/Makefile.am @@ -87,8 +87,8 @@ libshell_a_SOURCES = shell/cat_file.c shell/cmds.c shell/internal.h \ shell/fts.c shell/print_heapinfo.c shell/main_wkspaceinfo.c \ shell/shell_script.c shell/login_prompt.c shell/login_check.c \ shell/fdisk.c shell/main_rtc.c \ - shell/dd-args.c shell/main_dd.c shell/dd-conv.c shell/dd-conv_tab.c shell/dd-misc.c \ - shell/dd-position.c \ + shell/dd-args.c shell/main_dd.c shell/dd-conv.c shell/dd-conv_tab.c \ + shell/dd-misc.c shell/dd-position.c \ shell/main_hexdump.c shell/hexdump-conv.c shell/hexdump-display.c \ shell/hexdump-odsyntax.c shell/hexdump-parse.c shell/hexsyntax.c \ shell/main_time.c @@ -126,26 +126,25 @@ libuntar_a_SOURCES = untar/untar.c untar/untar.h EXTRA_DIST += untar/README +## stringto +noinst_LIBRARIES += libstringto.a +libstringto_a_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/stringto +libstringto_a_SOURCES = stringto/stringtodouble.c stringto/stringtofloat.c \ + stringto/stringtoint.c stringto/stringtolong.c stringto/stringtolonglong.c \ + stringto/stringtounsignedchar.c stringto/stringtounsignedint.c \ + stringto/stringtounsignedlong.c stringto/stringtounsignedlonglong.c + +EXTRA_DIST += stringto/stringto_template.h + ## fsmount noinst_LIBRARIES += libfsmount.a libfsmount_a_SOURCES = fsmount/fsmount.c fsmount/fsmount.h -EXTRA_DIST += fsmount/README - ## uuid noinst_LIBRARIES += libuuid.a -libuuid_a_SOURCES = uuid/clear.c \ - uuid/compare.c \ - uuid/copy.c \ - uuid/gen_uuid.c \ - uuid/isnull.c \ - uuid/pack.c \ - uuid/parse.c \ - uuid/unpack.c \ - uuid/unparse.c \ - uuid/uuid_time.c \ - uuid/uuidd.h \ - uuid/uuidP.h +libuuid_a_SOURCES = uuid/clear.c uuid/compare.c uuid/copy.c uuid/gen_uuid.c \ + uuid/isnull.c uuid/pack.c uuid/parse.c uuid/unpack.c uuid/unparse.c \ + uuid/uuid_time.c uuid/uuidd.h uuid/uuidP.h ## --- include $(srcdir)/preinstall.am diff --git a/cpukit/libmisc/stringto/stringto.h b/cpukit/libmisc/stringto/stringto.h new file mode 100644 index 0000000000..2e7fe151c6 --- /dev/null +++ b/cpukit/libmisc/stringto/stringto.h @@ -0,0 +1,176 @@ +/* + * 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$ + */ + +#ifndef __STRING_TO_A_TYPE_h__ +#define __STRING_TO_A_TYPE_h__ + +/** + * @brief Convert String to Unsigned Character (with validation) + * + * This method converts a string to an unsigned character with + * range 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 + * @param[in] base is the expected base of the number + * + * @return This method returns true on successful conversion and *n is + * filled in. + */ +bool rtems_string_to_unsigned_char( + const char *s, + unsigned char *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Int (with validation) + * + * This method converts a string to an int with range 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 + * @param[in] base is the expected base of the number + * + * @return This method returns true on successful conversion and *n is + * filled in. + */ +bool rtems_string_to_int( + const char *s, + int *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Long (with validation) + * + * This method converts a string to a long with + * range 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 + * @param[in] base is the expected base of the number + * + * @return This method returns true on successful conversion and *n is + * filled in. + */ +bool rtems_string_to_long( + const char *s, + long *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Unsigned Long (with validation) + * + * This method converts a string to an unsigned long with + * range 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 + * @param[in] base is the expected base of the number + * + * @return This method returns true on successful conversion and *n is + * filled in. + */ +bool rtems_string_to_unsigned_long( + const char *s, + unsigned long *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Long Long (with validation) + * + * This method converts a string to a long long with + * range 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 + * @param[in] base is the expected base of the number + * + * @return This method returns true on successful conversion and *n is + * filled in. + */ +bool rtems_string_to_long_long( + const char *s, + long long *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Unsigned Long Long (with validation) + * + * This method converts a string to an unsigned character with + * range 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 + * @param[in] base is the expected base of the number + * + * @return This method returns true on successful conversion and *n is + * filled in. + */ +bool rtems_string_to_unsigned_long_long( + const char *s, + unsigned long long *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Float (with validation) + * + * This method converts a string to a float with range 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 true on successful conversion and *n is + * filled in. + */ +bool rtems_string_to_float( + const char *s, + float *n, + char **endptr +); + +/** + * @brief Convert String to Double (with validation) + * + * This method converts a string to a double with range 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 true on successful conversion and *n is + * filled in. + */ +bool rtems_string_to_double( + const char *s, + double *n, + char **endptr +); + +#endif diff --git a/cpukit/libmisc/stringto/stringto_template.h b/cpukit/libmisc/stringto/stringto_template.h new file mode 100644 index 0000000000..b1890e81d0 --- /dev/null +++ b/cpukit/libmisc/stringto/stringto_template.h @@ -0,0 +1,107 @@ +/* + * 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$ + */ + +#include <errno.h> +#include <stdlib.h> +#include <limits.h> +#include <stdbool.h> + +#include <math.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_FLOAT) && !defined(STRING_TO_INTEGER) + #error "Neither STRING_TO_FLOAT nor STRING_TO_INTEGER defined" +#endif + +#if defined(STRING_TO_FLOAT) && defined(STRING_TO_INTEGER) + #error "Both STRING_TO_FLOAT nor STRING_TO_INTEGER defined" +#endif + +#ifndef STRING_TO_TYPE + #error "STRING_TO_TYPE not defined" +#endif + +#ifndef STRING_TO_NAME + #error "STRING_TO_NAME not defined" +#endif + +#ifndef STRING_TO_METHOD + #error "STRING_TO_METHOD not defined" +#endif + +#ifndef STRING_TO_MAX + #error "STRING_TO_MAX not defined" +#endif + +#undef ZERO +#ifdef STRING_TO_FLOAT + #define ZERO 0.0 +#elif defined(STRING_TO_INTEGER) + #define ZERO 0 +#endif + +bool STRING_TO_NAME ( + const char *s, + STRING_TO_TYPE *n, + char **endptr + #if defined(STRING_TO_INTEGER) + , + int base + #endif +) +{ + STRING_TO_TYPE result; + char *end; + + if ( !n ) + return false; + + errno = 0; + *n = 0; + + #ifdef STRING_TO_FLOAT + result = STRING_TO_METHOD( s, &end ); + #elif defined(STRING_TO_INTEGER) + result = STRING_TO_METHOD( s, &end, base ); + #endif + + /* If the user wants the end pointer back, then return it. */ + if ( endptr ) + *endptr = end; + + /* nothing was converted */ + if ( end == s ) + return false; + + /* there was a conversion error */ + if ( (result == ZERO) && errno ) + return false; + + #ifdef STRING_TO_MAX + /* there was an overflow */ + if ( (result == STRING_TO_MAX) && (errno == ERANGE)) + return false; + #endif + + #ifdef STRING_TO_MIN + /* there was an underflow */ + if ( (result == STRING_TO_MIN) && (errno == ERANGE)) + return false; + #endif + + *n = result; + return true; +} + diff --git a/cpukit/libmisc/stringto/stringtodouble.c b/cpukit/libmisc/stringto/stringtodouble.c new file mode 100644 index 0000000000..080b53c60d --- /dev/null +++ b/cpukit/libmisc/stringto/stringtodouble.c @@ -0,0 +1,21 @@ +/* + * 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 strtod (double) + */ +#define STRING_TO_FLOAT +#define STRING_TO_TYPE double +#define STRING_TO_NAME rtems_string_to_double +#define STRING_TO_METHOD strtod +#define STRING_TO_MAX HUGE_VAL +#include "stringto_template.h" + diff --git a/cpukit/libmisc/stringto/stringtofloat.c b/cpukit/libmisc/stringto/stringtofloat.c new file mode 100644 index 0000000000..2de118278f --- /dev/null +++ b/cpukit/libmisc/stringto/stringtofloat.c @@ -0,0 +1,20 @@ +/* + * 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 strtof (float) + */ +#define STRING_TO_FLOAT +#define STRING_TO_TYPE float +#define STRING_TO_NAME rtems_string_to_float +#define STRING_TO_METHOD strtof +#define STRING_TO_MAX HUGE_VALF +#include "stringto_template.h" diff --git a/cpukit/libmisc/stringto/stringtoint.c b/cpukit/libmisc/stringto/stringtoint.c new file mode 100644 index 0000000000..0e87a3357b --- /dev/null +++ b/cpukit/libmisc/stringto/stringtoint.c @@ -0,0 +1,20 @@ +/* + * 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 strtol (int) + */ +#define STRING_TO_INTEGER +#define STRING_TO_TYPE int +#define STRING_TO_NAME rtems_string_to_int +#define STRING_TO_METHOD strtol +#define STRING_TO_MAX LONG_MAX +#include "stringto_template.h" diff --git a/cpukit/libmisc/stringto/stringtolong.c b/cpukit/libmisc/stringto/stringtolong.c new file mode 100644 index 0000000000..fd8234e431 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtolong.c @@ -0,0 +1,21 @@ +/* + * 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 strtol (long) + */ +#define STRING_TO_INTEGER +#define STRING_TO_TYPE long int +#define STRING_TO_NAME rtems_string_to_long +#define STRING_TO_METHOD strtol +#define STRING_TO_MIN LONG_MIN +#define STRING_TO_MAX LONG_MAX +#include "stringto_template.h" diff --git a/cpukit/libmisc/stringto/stringtolonglong.c b/cpukit/libmisc/stringto/stringtolonglong.c new file mode 100644 index 0000000000..390612e211 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtolonglong.c @@ -0,0 +1,21 @@ +/* + * 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 strtoll (long long) + */ +#define STRING_TO_INTEGER +#define STRING_TO_TYPE long long +#define STRING_TO_NAME rtems_string_to_long_long +#define STRING_TO_METHOD strtoll +#define STRING_TO_MIN LONG_LONG_MIN +#define STRING_TO_MAX LONG_LONG_MAX +#include "stringto_template.h" diff --git a/cpukit/libmisc/stringto/stringtounsignedchar.c b/cpukit/libmisc/stringto/stringtounsignedchar.c new file mode 100644 index 0000000000..b9a3e983bd --- /dev/null +++ b/cpukit/libmisc/stringto/stringtounsignedchar.c @@ -0,0 +1,20 @@ +/* + * 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 strtol (unsigned char) + */ +#define STRING_TO_INTEGER +#define STRING_TO_TYPE unsigned char +#define STRING_TO_NAME rtems_string_to_unsigned_char +#define STRING_TO_METHOD strtoul +#define STRING_TO_MAX ULONG_MAX +#include "stringto_template.h" diff --git a/cpukit/libmisc/stringto/stringtounsignedint.c b/cpukit/libmisc/stringto/stringtounsignedint.c new file mode 100644 index 0000000000..82f45abe5d --- /dev/null +++ b/cpukit/libmisc/stringto/stringtounsignedint.c @@ -0,0 +1,20 @@ +/* + * 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 (unsigned int) + */ +#define STRING_TO_INTEGER +#define STRING_TO_TYPE unsigned int +#define STRING_TO_NAME rtems_string_to_unsigned_int +#define STRING_TO_METHOD strtoul +#define STRING_TO_MAX ULONG_MAX +#include "stringto_template.h" diff --git a/cpukit/libmisc/stringto/stringtounsignedlong.c b/cpukit/libmisc/stringto/stringtounsignedlong.c new file mode 100644 index 0000000000..0a66e88ee4 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtounsignedlong.c @@ -0,0 +1,20 @@ +/* + * 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 (unsigned long) + */ +#define STRING_TO_INTEGER +#define STRING_TO_TYPE unsigned long int +#define STRING_TO_NAME rtems_string_to_unsigned_long +#define STRING_TO_METHOD strtoul +#define STRING_TO_MAX ULONG_MAX +#include "stringto_template.h" diff --git a/cpukit/libmisc/stringto/stringtounsignedlonglong.c b/cpukit/libmisc/stringto/stringtounsignedlonglong.c new file mode 100644 index 0000000000..6f79c3d874 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtounsignedlonglong.c @@ -0,0 +1,20 @@ +/* + * 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 strtoull (unsigned long long) + */ +#define STRING_TO_INTEGER +#define STRING_TO_TYPE unsigned long long +#define STRING_TO_NAME rtems_string_to_unsigned_long_long +#define STRING_TO_METHOD strtoull +#define STRING_TO_MAX ULONG_LONG_MAX +#include "stringto_template.h" diff --git a/cpukit/preinstall.am b/cpukit/preinstall.am index 95c02f1731..d1e93699ec 100644 --- a/cpukit/preinstall.am +++ b/cpukit/preinstall.am @@ -251,6 +251,10 @@ $(PROJECT_INCLUDE)/rtems/shellconfig.h: libmisc/shell/shellconfig.h $(PROJECT_IN $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/shellconfig.h PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/shellconfig.h endif +$(PROJECT_INCLUDE)/rtems/stringto.h: libmisc/stringto/stringto.h $(PROJECT_INCLUDE)/rtems/$(dirstamp) + $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/stringto.h +PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/stringto.h + $(PROJECT_INCLUDE)/rtems/libi2c.h: libi2c/libi2c.h $(PROJECT_INCLUDE)/rtems/$(dirstamp) $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/libi2c.h PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/libi2c.h diff --git a/cpukit/wrapup/Makefile.am b/cpukit/wrapup/Makefile.am index 9bd6f32fd7..674a7a5b88 100644 --- a/cpukit/wrapup/Makefile.am +++ b/cpukit/wrapup/Makefile.am @@ -59,6 +59,7 @@ TMP_LIBS += ../libmisc/libdummy.a TMP_LIBS += ../libmisc/libmw-fb.a TMP_LIBS += ../libmisc/libcapture.a TMP_LIBS += ../libmisc/libfsmount.a +TMP_LIBS += ../libmisc/libstringto.a TMP_LIBS += ../libmisc/libuuid.a TMP_LIBS += ../libi2c/libi2c.a |