Simple data structures / objects in plain C Snapshot
strtoint.h
Go to the documentation of this file.
00001 #ifndef __STRTOINT__H_
00002 #define __STRTOINT__H_
00003 
00004 
00005 #include <errno.h>
00006 #include <limits.h>
00007 #include <stdlib.h>
00008 #include <cutils/base.h>
00009 
00010 /**
00011  * @defgroup strtoint
00012  * @brief an attempt to normalize the wild strtol/stroll/strtoul/strtoull set of functions for the stdint.h world.
00013  *
00014  * @{
00015  */
00016 
00017 M_INLINE int16_t strto_int16_t (const char *nptr, char **endptr, int base )
00018 {
00019   long rval = strtol( nptr, endptr, base );
00020   if (rval > SHRT_MAX) {
00021     errno = ERANGE;
00022   }
00023   return (int16_t) rval;
00024 }
00025 
00026 M_INLINE uint16_t strto_uint16_t (const char *nptr, char **endptr, int base )
00027 {
00028   long rval = strtoul( nptr, endptr, base );
00029   if (rval > USHRT_MAX) {
00030     errno = ERANGE;
00031   }
00032   return (uint16_t) rval;
00033 }
00034 
00035 
00036 M_INLINE int32_t strto_int32_t (const char *nptr, char **endptr, int base )
00037 {
00038   long rval = strtol( nptr, endptr, base );
00039   if (rval > INT_MAX) {
00040     errno = ERANGE;
00041   }
00042   return (int32_t) rval;
00043 }
00044 
00045 M_INLINE uint32_t strto_uint32_t (const char *nptr, char **endptr, int base )
00046 {
00047   uint32_t rval = strtol( nptr, endptr, base );
00048   if (rval > UINT_MAX) {
00049     errno = ERANGE;
00050   }
00051   return (int32_t) rval;
00052 }
00053 
00054 #define strto_int64_t   strtoll
00055 #define strto_uint64_t  strtoull
00056 
00057 /**
00058  * @}
00059  */
00060 
00061 #endif
00062 
00063