_itow, _ltow, _ultow added to crtdll.
[reactos.git] / reactos / lib / crtdll / stdlib / itow.c
1 /* $Id: itow.c,v 1.1 2000/12/22 23:17:03 ea Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/crtdll/stdlib/itow.c
6 * PURPOSE: converts a integer to wchar_t
7 * PROGRAMER:
8 * UPDATE HISTORY:
9 * 1995: Created
10 * 1998: Added ltoa Boudewijn Dekker
11 * 2000: derived from ./itoa.c by ea
12 */
13 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
14 #include <crtdll/errno.h>
15 #include <crtdll/stdlib.h>
16 #include <crtdll/internal/file.h>
17
18 wchar_t *
19 _itow (int value, wchar_t *string, int radix)
20 {
21 wchar_t tmp [33];
22 wchar_t * tp = tmp;
23 int i;
24 unsigned int v;
25 int sign;
26 wchar_t * sp;
27
28 if (radix > 36 || radix <= 1)
29 {
30 __set_errno(EDOM);
31 return 0;
32 }
33
34 sign = ((radix == 10) && (value < 0));
35 if (sign)
36 {
37 v = -value;
38 }
39 else
40 {
41 v = (unsigned) value;
42 }
43 while (v || tp == tmp)
44 {
45 i = v % radix;
46 v = v / radix;
47 if (i < 10)
48 {
49 *tp++ = i+ (wchar_t) '0';
50 }
51 else
52 {
53 *tp++ = i + (wchar_t) 'a' - 10;
54 }
55 }
56
57 if (string == 0)
58 {
59 string = (wchar_t *) malloc((tp-tmp) + (sign + 1) * sizeof(wchar_t));
60 }
61 sp = string;
62
63 if (sign)
64 {
65 *sp++ = (wchar_t) '-';
66 }
67 while (tp > tmp)
68 {
69 *sp++ = *--tp;
70 }
71 *sp = (wchar_t) 0;
72 return string;
73 }
74
75
76 wchar_t *
77 _ltow (long value, wchar_t *string, int radix)
78 {
79 wchar_t tmp [33];
80 wchar_t * tp = tmp;
81 long int i;
82 unsigned long int v;
83 int sign;
84 wchar_t * sp;
85
86 if (radix > 36 || radix <= 1)
87 {
88 __set_errno(EDOM);
89 return 0;
90 }
91
92 sign = ((radix == 10) && (value < 0));
93 if (sign)
94 {
95 v = -value;
96 }
97 else
98 {
99 v = (unsigned long) value;
100 }
101 while (v || tp == tmp)
102 {
103 i = v % radix;
104 v = v / radix;
105 if (i < 10)
106 {
107 *tp++ = i + (wchar_t) '0';
108 }
109 else
110 {
111 *tp++ = i + (wchar_t) 'a' - 10;
112 }
113 }
114
115 if (string == 0)
116 {
117 string = (wchar_t *) malloc((tp - tmp) + (sign + 1) * sizeof(wchar_t));
118 }
119 sp = string;
120
121 if (sign)
122 {
123 *sp++ = (wchar_t) '-';
124 }
125 while (tp > tmp)
126 {
127 *sp++ = *--tp;
128 }
129 *sp = (wchar_t) 0;
130 return string;
131 }
132
133 wchar_t *
134 _ultow (unsigned long value, wchar_t *string, int radix)
135 {
136 wchar_t tmp [33];
137 wchar_t * tp = tmp;
138 long int i;
139 unsigned long int v = value;
140 wchar_t * sp;
141
142 if (radix > 36 || radix <= 1)
143 {
144 __set_errno(EDOM);
145 return 0;
146 }
147 while (v || tp == tmp)
148 {
149 i = v % radix;
150 v = v / radix;
151 if (i < 10)
152 {
153 *tp++ = i + (wchar_t) '0';
154 }
155 else
156 {
157 *tp++ = i + (wchar_t) 'a' - 10;
158 }
159 }
160
161 if (string == 0)
162 {
163 string = (wchar_t *) malloc((tp - tmp) + sizeof(wchar_t));
164 }
165 sp = string;
166
167
168 while (tp > tmp)
169 {
170 *sp++ = *--tp;
171 }
172 *sp = (wchar_t) 0;
173 return string;
174 }
175
176
177 /* EOF */