source: box/trunk/lib/common/ConversionString.cpp @ 1826

Revision 1826, 2.9 KB checked in by chris, 5 years ago (diff)

Fix compiler warning. (merges [1820], [1821])

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    ConversionString.cpp
5//              Purpose: Conversions to and from strings
6//              Created: 9/4/04
7//
8// --------------------------------------------------------------------------
9
10#include "Box.h"
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <limits.h>
15#include <errno.h>
16
17#include "Conversion.h"
18#include "autogen_ConversionException.h"
19
20#include "MemLeakFindOn.h"
21
22// --------------------------------------------------------------------------
23//
24// Function
25//              Name:    BoxConvert::_ConvertStringToInt(const char *, int)
26//              Purpose: Convert from string to integer, with range checking.
27//                               Always does signed -- no point in unsigned as C++ type checking
28//                               isn't up to handling it properly.
29//                               If a null pointer is passed in, then returns 0.
30//              Created: 9/4/04
31//
32// --------------------------------------------------------------------------
33int32_t BoxConvert::_ConvertStringToInt(const char *pString, int Size)
34{
35        // Handle null strings gracefully.
36        if(pString == 0)
37        {
38                return 0;
39        }
40
41        // Check for initial validity
42        if(*pString == '\0')
43        {
44                THROW_EXCEPTION(ConversionException, CannotConvertEmptyStringToInt)
45        }
46       
47        // Convert.
48        char *numEnd = 0;
49        errno = 0;                      // Some platforms don't reset it.
50        long r = ::strtol(pString, &numEnd, 0);
51
52        // Check that all the characters were used
53        if(*numEnd != '\0')
54        {
55                THROW_EXCEPTION(ConversionException, BadStringRepresentationOfInt)
56        }
57       
58        // Error check
59        if(r == 0 && errno == EINVAL)
60        {
61                THROW_EXCEPTION(ConversionException, BadStringRepresentationOfInt)
62        }
63
64        // Range check from strtol
65        if((r == LONG_MIN || r == LONG_MAX) && errno == ERANGE)
66        {
67                THROW_EXCEPTION(ConversionException, IntOverflowInConvertFromString)
68        }
69       
70        // Check range for size of integer
71        switch(Size)
72        {
73        case 32:
74                {
75                        // No extra checking needed if long is an int32
76                        if(sizeof(long) > sizeof(int32_t))
77                        {
78                                if(r <= (0 - 0x7fffffffL) || r > 0x7fffffffL)
79                                {
80                                        THROW_EXCEPTION(ConversionException, IntOverflowInConvertFromString)
81                                }
82                        }
83                        break;
84                }
85
86        case 16:
87                {
88                        if(r <= (0 - 0x7fff) || r > 0x7fff)
89                        {
90                                THROW_EXCEPTION(ConversionException, IntOverflowInConvertFromString)
91                        }
92                        break;
93                }
94       
95        case 8:
96                {
97                        if(r <= (0 - 0x7f) || r > 0x7f)
98                        {
99                                THROW_EXCEPTION(ConversionException, IntOverflowInConvertFromString)
100                        }
101                        break;
102                }
103               
104        default:
105                {
106                        THROW_EXCEPTION(ConversionException, BadIntSize)
107                        break;
108                }
109        }
110       
111        // Return number
112        return r;
113}
114
115// --------------------------------------------------------------------------
116//
117// Function
118//              Name:    BoxConvert::_ConvertIntToString(std::string &, int32_t)
119//              Purpose: Convert signed interger to a string
120//              Created: 9/4/04
121//
122// --------------------------------------------------------------------------
123void BoxConvert::_ConvertIntToString(std::string &rTo, int32_t From)
124{
125        char text[64];  // size more than enough
126        ::sprintf(text, "%d", (int)From);
127        rTo = text;
128}
129
Note: See TracBrowser for help on using the repository browser.