From ab425430a31e363120a8847bfed6f23811adeeee Mon Sep 17 00:00:00 2001 From: bernie Date: Tue, 15 Mar 2005 00:06:30 +0000 Subject: [PATCH] Simpler, smaller, faster. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@393 38d2e660-2303-0410-9eaa-f027e97ec537 --- mware/strtol10.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ mware/strtol10.h | 47 +++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100755 mware/strtol10.c create mode 100755 mware/strtol10.h diff --git a/mware/strtol10.c b/mware/strtol10.c new file mode 100755 index 00000000..13777223 --- /dev/null +++ b/mware/strtol10.c @@ -0,0 +1,82 @@ +/*! + * \file + * + * + * \brief Poor man's hex arrays (implementation). + * + * \version $Id$ + * \author Bernardo Innocenti + */ + +/*#* + *#* $Log$ + *#* Revision 1.1 2005/03/15 00:06:30 bernie + *#* Simpler, smaller, faster. + *#* + *#*/ + +#include "strtol10.h" + +/*! + * Convert a formatted base-10 ASCII number to unsigned long binary representation. + * + * Unlike the standard strtoul(), this function has an interface + * that makes it better suited for protocol parsers. It's also + * much simpler and smaller than a full featured strtoul(). + * + * \param first Pointer to first byte of input range (STL-style). + * \param last Pointer to end of input range (STL-style). + * Pass NULL to parse up to the first \0. + * \param val Pointer to converted value. + * + * \return true for success, false for failure. + * + * \see strtol10() + */ +bool strtoul10(const char *first, const char *last, unsigned long *val) +{ + // Check for no input + if (*first == '\0') + return false; + + *val = 0; + for(/*nop*/; first != last && *first != '\0'; ++first) + { + if ((*first < '0') || (*first > '9')) + return false; + + *val = (*val * 10L) + (*first - '0'); + } + + return true; +} + + +/*! + * Convert a formatted base-10 ASCII number to signed long binary representation. + * + * \see strtoul10() + */ +bool strtol10(const char *first, const char *last, long *val) +{ + bool negative = false; + + if (*first == '+') + ++first; /* skip unary plus sign */ + else if (*first == '-') + { + negative = true; + ++first; + } + + bool result = strtoul10(first, last, (unsigned long *)val); + + if (negative) + *val = - *val; + + return result; +} + diff --git a/mware/strtol10.h b/mware/strtol10.h new file mode 100755 index 00000000..e41a98e8 --- /dev/null +++ b/mware/strtol10.h @@ -0,0 +1,47 @@ +/*! + * \file + * + * + * \brief Poor man's hex arrays (implementation). + * + * \version $Id$ + * \author Bernardo Innocenti + */ + +/*#* + *#* $Log$ + *#* Revision 1.1 2005/03/15 00:06:30 bernie + *#* Simpler, smaller, faster. + *#* + *#*/ + +#ifndef MWARE_STRTOL10_H +#define MWARE_STRTOL10_H + +#include /* bool */ + +bool strtoul10(const char *first, const char *last, unsigned long *val); +bool strtol10(const char *first, const char *last, long *val); + +/*! + * Replacement for standard library function atol(). + */ +INLINE long atol(const char *str) +{ + long val; + strtol10(str, NULL, &val); + return val; +} + +/*! + * Replacement for standard library function atoi(). + */ +INLINE int atoi(const char *str) +{ + return (int)atol(str); +} + +#endif /* MWARE_STRTOL10_H */ -- 2.25.1