1 |
//$Header$ |
2 |
//------------------------------------------------------------------------------------------------- |
3 |
//This file is part of "David T. Ashley's Shared Source Code", a set of shared components |
4 |
//integrated into many of David T. Ashley's projects. |
5 |
//------------------------------------------------------------------------------------------------- |
6 |
//This source code and any program in which it is compiled/used is provided under the MIT License, |
7 |
//reproduced below. |
8 |
//------------------------------------------------------------------------------------------------- |
9 |
//Permission is hereby granted, free of charge, to any person obtaining a copy of |
10 |
//this software and associated documentation files(the "Software"), to deal in the |
11 |
//Software without restriction, including without limitation the rights to use, |
12 |
//copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the |
13 |
//Software, and to permit persons to whom the Software is furnished to do so, |
14 |
//subject to the following conditions : |
15 |
// |
16 |
//The above copyright notice and this permission notice shall be included in all |
17 |
//copies or substantial portions of the Software. |
18 |
// |
19 |
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 |
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 |
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
22 |
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 |
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 |
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25 |
//SOFTWARE. |
26 |
//------------------------------------------------------------------------------------------------- |
27 |
#define MODULE_ESRG_WIN_INT64 |
28 |
|
29 |
#include <assert.h> |
30 |
#include <string.h> |
31 |
|
32 |
#include "charfunc.h" |
33 |
#include "esrg_win_int64.h" |
34 |
|
35 |
|
36 |
void ESRG_WIN_INT64_StringPlainToUi |
37 |
( |
38 |
const char *s, |
39 |
ESRG_WIN_INT64_UI64 *rv, |
40 |
int *err |
41 |
) |
42 |
{ |
43 |
int cval; |
44 |
ESRG_WIN_INT64_UI64 mul_10_max = 1844674407370955161; |
45 |
//The maximum value that can be successfully |
46 |
//multiplied by 10 and still stay within 64 bits. |
47 |
ESRG_WIN_INT64_UI64 result; |
48 |
|
49 |
assert(s != NULL); |
50 |
assert(rv != NULL); |
51 |
assert(err != NULL); |
52 |
|
53 |
//By default, no error on return. |
54 |
*err = 0; |
55 |
|
56 |
//Cover the case of an illegal first character or |
57 |
//a zero length string. |
58 |
cval = CHARFUNC_digit_to_val(*s); |
59 |
if (cval == -1) |
60 |
goto err_return; |
61 |
|
62 |
//Cover the case of canonical zero. |
63 |
if (cval == 0) |
64 |
{ |
65 |
if (s[1] != 0) |
66 |
{ |
67 |
//There is a '0', but not a null char trailing. |
68 |
goto err_return; |
69 |
} |
70 |
else |
71 |
{ |
72 |
//Canonical zero. |
73 |
*rv = 0; |
74 |
return; |
75 |
} |
76 |
} |
77 |
|
78 |
//If we're here, we have a digit which is not a zero, |
79 |
//and presumably more to follow. Iterate through, applying |
80 |
//the standard formula. |
81 |
// |
82 |
//Overflow can occur under two circumstances. Either we |
83 |
//multiply by 10 and overflow that way, or else we add a |
84 |
//digit and overflow that way. |
85 |
// |
86 |
//The multiply by 10 overflow can be prevented by |
87 |
//knowing the maximum constant that can be multiplied by |
88 |
//10 without overflow (we know this at compile-time). |
89 |
//The add overflow can be detected by the observation that |
90 |
//if the result is less than the digit we added, we |
91 |
//overflowed. |
92 |
result = 0; |
93 |
do |
94 |
{ |
95 |
if (result > mul_10_max) //Can't mul--already too large. |
96 |
goto err_return; |
97 |
|
98 |
result *= 10; |
99 |
|
100 |
result += cval; |
101 |
|
102 |
if (result < cval) //Overflow. |
103 |
goto err_return; |
104 |
|
105 |
s++; |
106 |
if (*s) |
107 |
{ |
108 |
cval = CHARFUNC_digit_to_val(*s); |
109 |
} |
110 |
else |
111 |
{ |
112 |
cval = 0; |
113 |
} |
114 |
|
115 |
if (cval == -1) |
116 |
goto err_return; |
117 |
|
118 |
} while (*s); |
119 |
|
120 |
|
121 |
*rv = result; |
122 |
return; |
123 |
|
124 |
//--------- |
125 |
err_return: |
126 |
*rv = 0; |
127 |
*err = 1; |
128 |
return; |
129 |
} |
130 |
|
131 |
|
132 |
const char *ESRG_WIN_INT64_cvcinfo(void) |
133 |
{ |
134 |
return("$Header$"); |
135 |
} |
136 |
|
137 |
|
138 |
const char *ESRG_WIN_INT64_hvcinfo(void) |
139 |
{ |
140 |
return(ESRG_WIN_INT64_H_VERSION); |
141 |
} |
142 |
|
143 |
//End of esrg_win_int64.c. |