| The Gaudi Framework v28r2p1 (f1a77ff4)

- 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 <stdlib.h>
4 #include <limits.h>
5 #include "GaudiKernel/xtoa.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 char *p; /* pointer to traverse string */
41 char *firstdig; /* pointer to first digit */
42 char temp; /* temp char */
43 unsigned digval; /* value of digit */
44
45 p = buf;
46
47 if (is_neg) {
48 /* negative, so output '-' and negate */
49 *p++ = '-';
50 val = (unsigned long)(-(long)val);
51 }
52
53 firstdig = p; /* save pointer to first digit */
54
55 do {
56 digval = (unsigned) (val % radix);
57 val /= radix; /* get next digit */
58
59 /* convert to ascii and store */
60 if (digval > 9)
61 *p++ = (char) (digval - 10 + 'a'); /* a letter */
62 else
63 *p++ = (char) (digval + '0'); /* a digit */
64 } while (val > 0);
65
66 /* We now have the digit of the number in the buffer, but in reverse
67 order. Thus we reverse them now. */
68
69 *p-- = '\0'; /* terminate string; p points to last digit */
70
71 do {
72 temp = *p;
73 *p = *firstdig;
74 *firstdig = temp; /* swap *p and *firstdig */
75 --p;
76 ++firstdig; /* advance to next two digits */
77 } while (firstdig < p); /* repeat until halfway */
78 }
79
80 /* Actual functions just call conversion helper with neg flag set correctly,
81 and return pointer to buffer. */
82
83 extern "C" char * __cdecl _itoa (int val,char *buf,int radix) {
84 if (radix == 10 && val < 0)
85 xtoa((unsigned long)val, buf, radix, 1);
86 else
87 xtoa((unsigned long)(unsigned int)val, buf, radix, 0);
88 return buf;
89 }
90
91 extern "C" char * __cdecl _ltoa (long val,char *buf,int radix) {
92 xtoa((unsigned long)val, buf, radix, (radix == 10 && val < 0));
93 return buf;
94 }
95
96 extern "C" char * __cdecl _ultoa (unsigned long val,char *buf,int radix) {
97 xtoa(val, buf, radix, 0);
98 return buf;
99 }
100
101 #if 0 // ndef _NO_INT64
102
103 static void __stdcall x64toa ( /* stdcall is faster and smaller... Might as well use it for the helper. */
104 unsigned __int64 val,
105 char *buf,
106 unsigned radix,
107 int is_neg
108 )
109 {
110 char *p; /* pointer to traverse string */
111 char *firstdig; /* pointer to first digit */
112 char temp; /* temp char */
113 unsigned digval; /* value of digit */
114
115 p = buf;
116
117 if ( is_neg ) {
118 *p++ = '-'; /* negative, so output '-' and negate */
119 }
120
121 firstdig = p; /* save pointer to first digit */
122
123 do {
124 digval = (unsigned) (val % radix);
125 val /= radix; /* get next digit */
126
127 /* convert to ascii and store */
128 if (digval > 9)
129 *p++ = (char) (digval - 10 + 'a'); /* a letter */
130 else
131 *p++ = (char) (digval + '0'); /* a digit */
132 } while (val > 0);
133
134 /* We now have the digit of the number in the buffer, but in reverse
135 order. Thus we reverse them now. */
136
137 *p-- = '\0'; /* terminate string; p points to last digit */
138
139 do {
140 temp = *p;
141 *p = *firstdig;
142 *firstdig = temp; /* swap *p and *firstdig */
143 --p;
144 ++firstdig; /* advance to next two digits */
145 } while (firstdig < p); /* repeat until halfway */
146 }
147
148 /* Actual functions just call conversion helper with neg flag set correctly,
149 and return pointer to buffer. */
150
151 extern "C" char * __cdecl _i64toa (__int64 val,char *buf,int radix ) {
152 x64toa((unsigned __int64)val, buf, radix, (radix == 10 && val < 0));
153 return buf;
154 }
155
156 extern "C" char * __cdecl i64toa (__int64 val,char *buf,int radix ) {
157 x64toa((unsigned __int64)val, buf, radix, (radix == 10 && val < 0));
158 return buf;
159 }
160
161 extern "C" char * __cdecl _ui64toa (unsigned __int64 val,char *buf,int radix) {
162 x64toa(val, buf, radix, 0);
163 return buf;
164 }
165
166 extern "C" char * __cdecl ui64toa (unsigned __int64 val,char *buf,int radix) {
167 x64toa(val, buf, radix, 0);
168 return buf;
169 }
170
171 #endif /* _NO_INT64 */
172
173 #endif /* WIN32 */
__stdcall
#define __stdcall
Definition: xtoa.h:3
xtoa.h
_ltoa
char *__cdecl _ltoa(long val, char *buf, int radix)
Definition: xtoa.cpp:91
_itoa
char *__cdecl _itoa(int val, char *buf, int radix)
Definition: xtoa.cpp:83
_ultoa
char *__cdecl _ultoa(unsigned long val, char *buf, int radix)
Definition: xtoa.cpp:96
__cdecl
#define __cdecl
Definition: xtoa.h:2