/[dtapublic]/projs/trunk/shared_source/c_datd/esrg_win_int64.c
ViewVC logotype

Diff of /projs/trunk/shared_source/c_datd/esrg_win_int64.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.70  
changed lines
  Added in v.71

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25