[CRT]
[reactos.git] / reactos / lib / sdk / crt / string / itow.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/sdk/crt/itow.c
5 * PURPOSE: converts a integer to wchar_t
6 * PROGRAMER:
7 * UPDATE HISTORY:
8 */
9
10 #include <precomp.h>
11
12 /*
13 * @implemented
14 */
15 wchar_t *
16 _i64tow(__int64 value, wchar_t *string, int radix)
17 {
18 ULONGLONG val;
19 int negative;
20 WCHAR buffer[65];
21 PWCHAR pos;
22 WCHAR digit;
23
24 if (value < 0 && radix == 10) {
25 negative = 1;
26 val = -value;
27 } else {
28 negative = 0;
29 val = value;
30 } /* if */
31
32 pos = &buffer[64];
33 *pos = '\0';
34
35 do {
36 digit = (WCHAR)(val % radix);
37 val = val / radix;
38 if (digit < 10) {
39 *--pos = '0' + digit;
40 } else {
41 *--pos = 'a' + digit - 10;
42 } /* if */
43 } while (val != 0L);
44
45 if (negative) {
46 *--pos = '-';
47 } /* if */
48
49 if (string != NULL) {
50 memcpy(string, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
51 } /* if */
52 return string;
53 }
54
55 /*
56 * @implemented
57 */
58 int
59 _i64tow_s(__int64 value, wchar_t *str, size_t size, int radix)
60 {
61 unsigned __int64 val;
62 unsigned int digit;
63 int is_negative;
64 wchar_t buffer[65], *pos;
65 size_t len;
66
67 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
68 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
69 {
70 if (str && size)
71 str[0] = '\0';
72
73 #ifndef _LIBCNT_
74 *_errno() = EINVAL;
75 #endif
76 return EINVAL;
77 }
78
79 if (value < 0 && radix == 10)
80 {
81 is_negative = 1;
82 val = -value;
83 }
84 else
85 {
86 is_negative = 0;
87 val = value;
88 }
89
90 pos = buffer + 64;
91 *pos = '\0';
92
93 do
94 {
95 digit = val % radix;
96 val /= radix;
97
98 if (digit < 10)
99 *--pos = '0' + digit;
100 else
101 *--pos = 'a' + digit - 10;
102 }
103 while (val != 0);
104
105 if (is_negative)
106 *--pos = '-';
107
108 len = buffer + 65 - pos;
109 if (len > size)
110 {
111 size_t i;
112 wchar_t *p = str;
113
114 /* Copy the temporary buffer backwards up to the available number of
115 * characters. Don't copy the negative sign if present. */
116
117 if (is_negative)
118 {
119 p++;
120 size--;
121 }
122
123 for (pos = buffer + 63, i = 0; i < size; i++)
124 *p++ = *pos--;
125
126 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
127 str[0] = '\0';
128 return ERANGE;
129 }
130
131 memcpy(str, pos, len * sizeof(wchar_t));
132 return 0;
133 }
134
135 /*
136 * @implemented
137 */
138 wchar_t *
139 _ui64tow(unsigned __int64 value, wchar_t *string, int radix)
140 {
141 WCHAR buffer[65];
142 PWCHAR pos;
143 WCHAR digit;
144
145 pos = &buffer[64];
146 *pos = '\0';
147
148 do {
149 digit = (WCHAR)(value % radix);
150 value = value / radix;
151 if (digit < 10) {
152 *--pos = '0' + digit;
153 } else {
154 *--pos = 'a' + digit - 10;
155 } /* if */
156 } while (value != 0L);
157
158 if (string != NULL) {
159 memcpy(string, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
160 } /* if */
161 return string;
162 }
163
164 /*
165 * @implemented
166 */
167 int
168 _ui64tow_s( unsigned __int64 value, wchar_t *str,
169 size_t size, int radix )
170 {
171 wchar_t buffer[65], *pos;
172 int digit;
173
174 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
175 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
176 #ifndef _LIBCNT_
177 *_errno() = EINVAL;
178 #endif
179 return EINVAL;
180 }
181
182 pos = &buffer[64];
183 *pos = '\0';
184
185 do {
186 digit = value % radix;
187 value = value / radix;
188 if (digit < 10)
189 *--pos = '0' + digit;
190 else
191 *--pos = 'a' + digit - 10;
192 } while (value != 0);
193
194 if((size_t)(buffer-pos+65) > size) {
195 MSVCRT_INVALID_PMT("str[size] is too small", EINVAL);
196 return EINVAL;
197 }
198
199 memcpy(str, pos, buffer-pos+65);
200 return 0;
201 }
202
203 /*
204 * @implemented
205 */
206 wchar_t *
207 _itow(int value, wchar_t *string, int radix)
208 {
209 return _ltow(value, string, radix);
210 }
211
212 /*
213 * @implemented
214 */
215 int
216 _itow_s(int value, wchar_t *str, size_t size, int radix)
217 {
218 return _ltow_s(value, str, size, radix);
219 }
220
221 /*
222 * @implemented
223 */
224 wchar_t *
225 _ltow(long value, wchar_t *string, int radix)
226 {
227 unsigned long val;
228 int negative;
229 WCHAR buffer[33];
230 PWCHAR pos;
231 WCHAR digit;
232
233 if (value < 0 && radix == 10) {
234 negative = 1;
235 val = -value;
236 } else {
237 negative = 0;
238 val = value;
239 } /* if */
240
241 pos = &buffer[32];
242 *pos = '\0';
243
244 do {
245 digit = (WCHAR)(val % radix);
246 val = val / radix;
247 if (digit < 10) {
248 *--pos = '0' + digit;
249 } else {
250 *--pos = 'a' + digit - 10;
251 } /* if */
252 } while (val != 0L);
253
254 if (negative) {
255 *--pos = '-';
256 } /* if */
257
258 if (string != NULL) {
259 memcpy(string, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
260 } /* if */
261 return string;
262 }
263
264 /*
265 * @implemented
266 */
267 int
268 _ltow_s(long value, wchar_t *str, size_t size, int radix)
269 {
270 unsigned long val;
271 unsigned int digit;
272 int is_negative;
273 wchar_t buffer[33], *pos;
274 size_t len;
275
276 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
277 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
278 {
279 if (str && size)
280 str[0] = '\0';
281
282 #ifndef _LIBCNT_
283 *_errno() = EINVAL;
284 #endif
285 return EINVAL;
286 }
287
288 if (value < 0 && radix == 10)
289 {
290 is_negative = 1;
291 val = -value;
292 }
293 else
294 {
295 is_negative = 0;
296 val = value;
297 }
298
299 pos = buffer + 32;
300 *pos = '\0';
301
302 do
303 {
304 digit = val % radix;
305 val /= radix;
306
307 if (digit < 10)
308 *--pos = '0' + digit;
309 else
310 *--pos = 'a' + digit - 10;
311 }
312 while (val != 0);
313
314 if (is_negative)
315 *--pos = '-';
316
317 len = buffer + 33 - pos;
318 if (len > size)
319 {
320 size_t i;
321 wchar_t *p = str;
322
323 /* Copy the temporary buffer backwards up to the available number of
324 * characters. Don't copy the negative sign if present. */
325
326 if (is_negative)
327 {
328 p++;
329 size--;
330 }
331
332 for (pos = buffer + 31, i = 0; i < size; i++)
333 *p++ = *pos--;
334
335 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
336 str[0] = '\0';
337 return ERANGE;
338 }
339
340 memcpy(str, pos, len * sizeof(wchar_t));
341 return 0;
342 }
343
344 /*
345 * @implemented
346 */
347 wchar_t *
348 _ultow(unsigned long value, wchar_t *string, int radix)
349 {
350 WCHAR buffer[33];
351 PWCHAR pos;
352 WCHAR digit;
353
354 pos = &buffer[32];
355 *pos = '\0';
356
357 do {
358 digit = (WCHAR)(value % radix);
359 value = value / radix;
360 if (digit < 10) {
361 *--pos = '0' + digit;
362 } else {
363 *--pos = 'a' + digit - 10;
364 } /* if */
365 } while (value != 0L);
366
367 if (string != NULL) {
368 memcpy(string, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
369 } /* if */
370 return string;
371 }