source: rtems/cpukit/libmisc/stringto/stringto_template.h @ 73dfaf8

4.104.115
Last change on this file since 73dfaf8 was 35d09ba, checked in by Joel Sherrill <joel.sherrill@…>, on 07/22/09 at 15:17:37

2009-07-22 Joel Sherrill <joel.sherrill@…>

  • 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.h, libmisc/shell/shell_script.c, libmisc/stringto/stringto_template.h: Convert all shell code to use stringto.h mehods with better error checking.
  • libmisc/shell/str2int.c: Removed.
  • Property mode set to 100644
File size: 2.1 KB
Line 
1/*
2 *  COPYRIGHT (c) 2009.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.com/license/LICENSE.
8 *
9 *  $Id$
10 */
11
12#include <errno.h>
13#include <stdlib.h>
14#include <limits.h>
15#include <stdbool.h>
16
17#include <math.h>
18
19/*
20 * This file is designed to be included multiple times to instantiate
21 * it and should NOT be protected against multiple inclusions.
22 */
23
24#if !defined(STRING_TO_FLOAT) && !defined(STRING_TO_INTEGER)
25  #error "Neither STRING_TO_FLOAT nor STRING_TO_INTEGER defined"
26#endif
27
28#if defined(STRING_TO_FLOAT) && defined(STRING_TO_INTEGER)
29  #error "Both STRING_TO_FLOAT nor STRING_TO_INTEGER defined"
30#endif
31
32#ifndef STRING_TO_TYPE
33  #error "STRING_TO_TYPE not defined"
34#endif
35
36#ifndef STRING_TO_NAME
37  #error "STRING_TO_NAME not defined"
38#endif
39
40#ifndef STRING_TO_METHOD
41  #error "STRING_TO_METHOD not defined"
42#endif
43
44#ifndef STRING_TO_MAX
45  #error "STRING_TO_MAX not defined"
46#endif
47
48#undef ZERO
49#ifdef STRING_TO_FLOAT
50  #define ZERO 0.0
51#elif defined(STRING_TO_INTEGER)
52  #define ZERO 0
53#endif
54
55bool STRING_TO_NAME (
56  const char      *s,
57  STRING_TO_TYPE  *n,
58  char           **endptr
59  #if defined(STRING_TO_INTEGER)
60    ,
61    int              base
62  #endif
63)
64{
65  STRING_TO_TYPE  result;
66  char           *end;
67
68  if ( !n )
69    return false;
70
71  errno = 0;
72  *n    = 0;
73
74  #ifdef STRING_TO_FLOAT
75    result = STRING_TO_METHOD( s, &end );
76  #elif defined(STRING_TO_INTEGER)
77    result = STRING_TO_METHOD( s, &end, base );
78  #endif
79
80  /* If the user wants the end pointer back, then return it. */
81  if ( endptr )
82    *endptr = end;
83
84  /* nothing was converted */
85  if ( end == s )
86    return false;
87
88  /* there was a conversion error */
89  if ( (result == ZERO) && errno )
90    return false;
91
92  #ifdef STRING_TO_MAX
93    /* there was an overflow */
94    if ( (result == STRING_TO_MAX) && (errno == ERANGE))
95      return false;
96  #endif
97
98  #ifdef STRING_TO_MIN
99    /* there was an underflow */
100    if ( (result == STRING_TO_MIN) && (errno == ERANGE))
101      return false;
102  #endif
103
104  *n = (STRING_TO_TYPE) result;
105  return true;
106}
107
Note: See TracBrowser for help on using the repository browser.