simulavr  1.1.0
string2_template.h
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
5  * Copyright (C) 2009 Joel Sherrill
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  ****************************************************************************
22  *
23  * $Id$
24  */
25 
26 /*
27  * This file is designed to be included multiple times to instantiate
28  * it and should NOT be protected against multiple inclusions.
29  */
30 
31 #ifndef STRING_TO_TYPE
32  #error "STRING_TO_TYPE not defined"
33 #endif
34 
35 #ifndef STRING_TO_NAME
36  #error "STRING_TO_NAME not defined"
37 #endif
38 
39 #ifndef STRING_TO_METHOD
40  #error "STRING_TO_METHOD not defined"
41 #endif
42 
43 #ifndef STRING_TO_MAX
44  #error "STRING_TO_MAX not defined"
45 #endif
46 
48  const char *s,
49  STRING_TO_TYPE *n,
50  char **endptr,
51  int base
52 )
53 {
54  unsigned long long result;
55  char *end;
56 
57  if ( !n )
58  return false;
59 
60  errno = 0;
61  *n = 0;
62 
63  result = STRING_TO_METHOD( s, &end, base );
64 
65  if ( endptr )
66  *endptr = end;
67 
68  /* nothing was converted */
69  if ( end == s )
70  return false;
71 
72  /* there was a conversion error */
73  if ( (result == 0) && errno )
74  return false;
75 
76  /* there was an overflow */
77  if ( (result == LONG_MAX) && (errno == ERANGE))
78  return false;
79 
80  /* does not fit into target type */
81  if ( result > STRING_TO_MAX )
82  return false;
83 
84 #ifdef STRING_TO_MIN
85  /* there was an underflow */
86  if ( (result == STRING_TO_MIN) && (errno == ERANGE))
87  return false;
88 #endif
89 
90  *n = (STRING_TO_TYPE) result;
91  return true;
92 }
93 
#define STRING_TO_METHOD
Definition: string2.cpp:73
bool STRING_TO_NAME(const char *s, STRING_TO_TYPE *n, char **endptr, int base)
#define STRING_TO_TYPE
Definition: string2.cpp:71
#define STRING_TO_MAX
Definition: string2.cpp:74