~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to strings/str2int.c

  • Committer: Brian Aker
  • Date: 2008-07-14 22:18:37 UTC
  • mfrom: (77.1.96 codestyle)
  • Revision ID: brian@tangent.org-20080714221837-oceoshx7fjkla9u3
Merge from Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
  The result is a pointer to the first character after the number;
23
23
  trailing spaces will NOT be skipped.
24
24
 
25
 
  If an error is detected, the result will be (char *)0, the value put
 
25
  If an error is detected, the result will be NullS, the value put
26
26
  in val will be 0, and errno will be set to
27
27
        EDOM    if there are no digits
28
28
        ERANGE  if the result would overflow or otherwise fail to lie
37
37
  call has no problems.
38
38
*/
39
39
 
 
40
#include <my_global.h>
40
41
#include "m_string.h"
41
42
#include "m_ctype.h"
 
43
#include "my_sys.h"                     /* defines errno */
42
44
#include <errno.h>
43
45
 
44
46
#define char_val(X) (X >= '0' && X <= '9' ? X-'0' :\
63
65
  *val = 0;
64
66
 
65
67
  /*  Check that the radix is in the range 2..36  */
 
68
 
 
69
#ifndef DBUG_OFF
66
70
  if (radix < 2 || radix > 36) {
67
71
    errno=EDOM;
68
 
    return (char *)0;
 
72
    return NullS;
69
73
  }
 
74
#endif
70
75
 
71
76
  /*  The basic problem is: how do we handle the conversion of
72
77
      a number without resorting to machine-specific code to
115
120
 
116
121
  if (start == src) {
117
122
    errno=EDOM;
118
 
    return (char *)0;
 
123
    return NullS;
119
124
  }
120
125
 
121
126
  /*  The invariant we want to maintain is that src is just
135
140
  {
136
141
    if ((long) -(d=digits[n]) < limit) {
137
142
      errno=ERANGE;
138
 
      return (char *)0;
 
143
      return NullS;
139
144
    }
140
145
    limit = (limit+d)/radix, sofar += d*scale; scale *= radix;
141
146
  }
144
149
    if ((long) -(d=digits[n]) < limit)          /* get last digit */
145
150
    {
146
151
      errno=ERANGE;
147
 
      return (char *)0;
 
152
      return NullS;
148
153
    }
149
154
    sofar+=d*scale;
150
155
  }
161
166
    if (sofar < -LONG_MAX || (sofar= -sofar) > upper)
162
167
    {
163
168
      errno=ERANGE;
164
 
      return (char *)0;
 
169
      return NullS;
165
170
    }
166
171
  }
167
172
  else if (sofar < lower)
168
173
  {
169
174
    errno=ERANGE;
170
 
    return (char *)0;
 
175
    return NullS;
171
176
  }
172
177
  *val = sofar;
173
178
  errno=0;                      /* indicate that all went well */