| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

xtoa.cpp
Go to the documentation of this file.
1 // $Header: /tmp/svngaudi/tmp.jEpFh25751/Gaudi/GaudiKernel/src/Lib/xtoa.cpp,v 1.3 2003/11/27 10:20:59 mato Exp $
2 #if !defined( _WIN32 )
3 #include "GaudiKernel/xtoa.h"
4 #include <limits.h>
5 #include <stdlib.h>
6
7 /***
8 *xtoa.c - convert integers/longs to ASCII string
9 *
10 * Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
11 *
12 *Purpose:
13 * The module has code to convert integers/longs to ASCII strings. See
14 *
15 *******************************************************************************/
16
17 /***
18 *char *_itoa, *_ltoa, *_ultoa(val, buf, radix) - convert binary int to ASCII
19 * string
20 *
21 *Purpose:
22 * Converts an int to a character string.
23 *
24 *Entry:
25 * val - number to be converted (int, long or unsigned long)
26 * int radix - base to convert into
27 * char *buf - ptr to buffer to place result
28 *
29 *Exit:
30 * fills in space pointed to by buf with string result
31 * returns a pointer to this buffer
32 *
33 *Exceptions:
34 *
35 *******************************************************************************/
36
37 /* helper routine that does the main job. */
38
39 static void __cdecl xtoa( unsigned long val, char* buf, unsigned radix, int is_neg )
40 {
41 char* p; /* pointer to traverse string */
42 char* firstdig; /* pointer to first digit */
43 char temp; /* temp char */
44 unsigned digval; /* value of digit */
45
46 p = buf;
47
48 if ( is_neg ) {
49 /* negative, so output '-' and negate */
50 *p++ = '-';
51 val = (unsigned long)( -(long)val );
52 }
53
54 firstdig = p; /* save pointer to first digit */
55
56 do {
57 digval = (unsigned)( val % radix );
58 val /= radix; /* get next digit */
59
60 /* convert to ascii and store */
61 if ( digval > 9 )
62 *p++ = (char)( digval - 10 + 'a' ); /* a letter */
63 else
64 *p++ = (char)( digval + '0' ); /* a digit */
65 } while ( val > 0 );
66
67 /* We now have the digit of the number in the buffer, but in reverse
68 order. Thus we reverse them now. */
69
70 *p-- = '\0'; /* terminate string; p points to last digit */
71
72 do {
73 temp = *p;
74 *p = *firstdig;
75 *firstdig = temp; /* swap *p and *firstdig */
76 --p;
77 ++firstdig; /* advance to next two digits */
78 } while ( firstdig < p ); /* repeat until halfway */
79 }
80
81 /* Actual functions just call conversion helper with neg flag set correctly,
82 and return pointer to buffer. */
83
84 extern "C" char* __cdecl _itoa( int val, char* buf, int radix )
85 {
86 if ( radix == 10 && val < 0 )
87 xtoa( (unsigned long)val, buf, radix, 1 );
88 else
89 xtoa( (unsigned long)(unsigned int)val, buf, radix, 0 );
90 return buf;
91 }
92
93 extern "C" char* __cdecl _ltoa( long val, char* buf, int radix )
94 {
95 xtoa( (unsigned long)val, buf, radix, ( radix == 10 && val < 0 ) );
96 return buf;
97 }
98
99 extern "C" char* __cdecl _ultoa( unsigned long val, char* buf, int radix )
100 {
101 xtoa( val, buf, radix, 0 );
102 return buf;
103 }
104
105 #if 0 // ndef _NO_INT64
106
107 static void __stdcall x64toa ( /* stdcall is faster and smaller... Might as well use it for the helper. */
108 unsigned __int64 val,
109 char *buf,
110 unsigned radix,
111 int is_neg
112 )
113 {
114 char *p; /* pointer to traverse string */
115 char *firstdig; /* pointer to first digit */
116 char temp; /* temp char */
117 unsigned digval; /* value of digit */
118
119 p = buf;
120
121 if ( is_neg ) {
122 *p++ = '-'; /* negative, so output '-' and negate */
123 }
124
125 firstdig = p; /* save pointer to first digit */
126
127 do {
128 digval = (unsigned) (val % radix);
129 val /= radix; /* get next digit */
130
131 /* convert to ascii and store */
132 if (digval > 9)
133 *p++ = (char) (digval - 10 + 'a'); /* a letter */
134 else
135 *p++ = (char) (digval + '0'); /* a digit */
136 } while (val > 0);
137
138 /* We now have the digit of the number in the buffer, but in reverse
139 order. Thus we reverse them now. */
140
141 *p-- = '\0'; /* terminate string; p points to last digit */
142
143 do {
144 temp = *p;
145 *p = *firstdig;
146 *firstdig = temp; /* swap *p and *firstdig */
147 --p;
148 ++firstdig; /* advance to next two digits */
149 } while (firstdig < p); /* repeat until halfway */
150 }
151
152 /* Actual functions just call conversion helper with neg flag set correctly,
153 and return pointer to buffer. */
154
155 extern "C" char * __cdecl _i64toa (__int64 val,char *buf,int radix ) {
156 x64toa((unsigned __int64)val, buf, radix, (radix == 10 && val < 0));
157 return buf;
158 }
159
160 extern "C" char * __cdecl i64toa (__int64 val,char *buf,int radix ) {
161 x64toa((unsigned __int64)val, buf, radix, (radix == 10 && val < 0));
162 return buf;
163 }
164
165 extern "C" char * __cdecl _ui64toa (unsigned __int64 val,char *buf,int radix) {
166 x64toa(val, buf, radix, 0);
167 return buf;
168 }
169
170 extern "C" char * __cdecl ui64toa (unsigned __int64 val,char *buf,int radix) {
171 x64toa(val, buf, radix, 0);
172 return buf;
173 }
174
175 #endif /* _NO_INT64 */
176
177 #endif /* WIN32 */
__stdcall
#define __stdcall
Definition: xtoa.h:3
xtoa.h
_ltoa
char *__cdecl _ltoa(long val, char *buf, int radix)
Definition: xtoa.cpp:93
_itoa
char *__cdecl _itoa(int val, char *buf, int radix)
Definition: xtoa.cpp:84
_ultoa
char *__cdecl _ultoa(unsigned long val, char *buf, int radix)
Definition: xtoa.cpp:99
__cdecl
#define __cdecl
Definition: xtoa.h:2