OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [c/] [libc/] [stdlib/] [current/] [src/] [strtoul.cxx] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//===========================================================================
2
//
3
//      strtoul.cxx
4
//
5
//      ISO standard string to long int conversion function defined in
6
//      section 7.10.1.6 of the standard
7
//
8
//===========================================================================
9
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
10
// -------------------------------------------                              
11
// This file is part of eCos, the Embedded Configurable Operating System.   
12
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
13
//
14
// eCos is free software; you can redistribute it and/or modify it under    
15
// the terms of the GNU General Public License as published by the Free     
16
// Software Foundation; either version 2 or (at your option) any later      
17
// version.                                                                 
18
//
19
// eCos is distributed in the hope that it will be useful, but WITHOUT      
20
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
21
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
22
// for more details.                                                        
23
//
24
// You should have received a copy of the GNU General Public License        
25
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
26
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
27
//
28
// As a special exception, if other files instantiate templates or use      
29
// macros or inline functions from this file, or you compile this file      
30
// and link it with other works to produce a work based on this file,       
31
// this file does not by itself cause the resulting work to be covered by   
32
// the GNU General Public License. However the source code for this file    
33
// must still be made available in accordance with section (3) of the GNU   
34
// General Public License v2.                                               
35
//
36
// This exception does not invalidate any other reasons why a work based    
37
// on this file might be covered by the GNU General Public License.         
38
// -------------------------------------------                              
39
// ####ECOSGPLCOPYRIGHTEND####                                              
40
//===========================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):    jlarmour
44
// Contributors: 
45
// Date:         2000-04-30
46
// Purpose:     
47
// Description: 
48
// Usage:       
49
//
50
//####DESCRIPTIONEND####
51
//
52
//===========================================================================
53
//
54
// This code is based on original code with the following copyright:
55
//
56
/*
57
 * Copyright (c) 1990 Regents of the University of California.
58
 * All rights reserved.
59
 *
60
 * Redistribution and use in source and binary forms, with or without
61
 * modification, are permitted provided that the following conditions
62
 * are met:
63
 * 1. Redistributions of source code must retain the above copyright
64
 *    notice, this list of conditions and the following disclaimer.
65
 * 2. Redistributions in binary form must reproduce the above copyright
66
 *    notice, this list of conditions and the following disclaimer in the
67
 *    documentation and/or other materials provided with the distribution.
68
 * 3. Neither the name of the University nor the names of its contributors
69
 *    may be used to endorse or promote products derived from this software
70
 *    without specific prior written permission.
71
 *
72
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
73
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
75
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
76
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
77
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
78
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
79
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
80
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
81
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
82
 * SUCH DAMAGE.
83
 */
84
 
85
 
86
// CONFIGURATION
87
 
88
#include <pkgconf/libc_stdlib.h>   // Configuration header
89
 
90
// INCLUDES
91
 
92
#include <cyg/infra/cyg_type.h>     // Common type definitions and support
93
#include <cyg/infra/cyg_trac.h>     // Tracing support
94
#include <cyg/infra/cyg_ass.h>      // Assertion support
95
#include <limits.h>                 // Definition of ULONG_MAX
96
#include <ctype.h>                  // Definition of many ctype functions
97
#include <errno.h>                  // Error code definitions
98
#include <stdlib.h>                 // Header for all stdlib functions
99
                                    // (like this one)
100
 
101
 
102
// FUNCTIONS
103
 
104
//
105
// Convert a string to an unsigned long integer.
106
//
107
// Ignores `locale' stuff.  Assumes that the upper and lower case
108
// alphabets and digits are each contiguous.
109
//
110
 
111
unsigned long
112
strtoul( const char *nptr, char **endptr, int base )
113
{
114
    const char *s = nptr;
115
    unsigned long acc;
116
    int c;
117
    unsigned long cutoff;
118
    int neg = 0, any, cutlim;
119
 
120
    CYG_REPORT_FUNCNAMETYPE( "strtoul", "returning long %d" );
121
    CYG_REPORT_FUNCARG3( "nptr=%08x, endptr=%08x, base=%d",
122
                         nptr, endptr, base );
123
    CYG_CHECK_DATA_PTR( nptr, "nptr is not a valid pointer!" );
124
 
125
    if (endptr != NULL)
126
        CYG_CHECK_DATA_PTR( endptr, "endptr is not a valid pointer!" );
127
    //
128
    // See strtol for comments as to the logic used.
129
    //
130
    do {
131
        c = *s++;
132
    } while (isspace(c));
133
    if (c == '-') {
134
        neg = 1;
135
        c = *s++;
136
    } else if (c == '+')
137
        c = *s++;
138
    if ((base == 0 || base == 16) &&
139
        c == '0' && (*s == 'x' || *s == 'X')) {
140
        c = s[1];
141
        s += 2;
142
        base = 16;
143
    }
144
    if (base == 0)
145
        base = c == '0' ? 8 : 10;
146
    cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
147
    cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
148
    for (acc = 0, any = 0;; c = *s++) {
149
        if (isdigit(c))
150
            c -= '0';
151
        else if (isalpha(c))
152
            c -= isupper(c) ? 'A' - 10 : 'a' - 10;
153
        else
154
            break;
155
        if (c >= base)
156
            break;
157
        if ((any < 0) || (acc > cutoff) || ((acc == cutoff) && (c > cutlim)))
158
            any = -1;
159
        else {
160
            any = 1;
161
            acc *= base;
162
            acc += c;
163
        }
164
    }
165
    if (any < 0) {
166
        acc = ULONG_MAX;
167
        errno = ERANGE;
168
    } else if (neg)
169
        acc = -acc;
170
    if (endptr != 0)
171
        *endptr = (char *) (any ? s - 1 : nptr);
172
 
173
    CYG_REPORT_RETVAL( acc );
174
 
175
    return acc;
176
} // strtoul()
177
 
178
// EOF strtoul.cxx

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.