c18b679665a70679e16fc686e412a48f1e320fc6
[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");
127 str[0] = '\0';
128 #ifndef _LIBCNT_
129 *_errno() = ERANGE;
130 #endif
131 return ERANGE;
132 }
133
134 memcpy(str, pos, len * sizeof(wchar_t));
135 return 0;
136 }
137
138 /*
139 * @implemented
140 */
141 wchar_t *
142 _ui64tow(unsigned __int64 value, wchar_t *string, int radix)
143 {
144 WCHAR buffer[65];
145 PWCHAR pos;
146 WCHAR digit;
147
148 pos = &buffer[64];
149 *pos = '\0';
150
151 do {
152 digit = (WCHAR)(value % radix);
153 value = value / radix;
154 if (digit < 10) {
155 *--pos = '0' + digit;
156 } else {
157 *--pos = 'a' + digit - 10;
158 } /* if */
159 } while (value != 0L);
160
161 if (string != NULL) {
162 memcpy(string, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
163 } /* if */
164 return string;
165 }
166
167 /*
168 * @implemented
169 */
170 int
171 _ui64tow_s( unsigned __int64 value, wchar_t *str,
172 size_t size, int radix )
173 {
174 wchar_t buffer[65], *pos;
175 int digit;
176
177 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
178 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
179 #ifndef _LIBCNT_
180 *_errno() = EINVAL;
181 #endif
182 return EINVAL;
183 }
184
185 pos = &buffer[64];
186 *pos = '\0';
187
188 do {
189 digit = value % radix;
190 value = value / radix;
191 if (digit < 10)
192 *--pos = '0' + digit;
193 else
194 *--pos = 'a' + digit - 10;
195 } while (value != 0);
196
197 if((size_t)(buffer-pos+65) > size) {
198 MSVCRT_INVALID_PMT("str[size] is too small");
199 #ifndef _LIBCNT_
200 *_errno() = EINVAL;
201 #endif
202 return EINVAL;
203 }
204
205 memcpy(str, pos, buffer-pos+65);
206 return 0;
207 }
208
209 /*
210 * @implemented
211 */
212 wchar_t *
213 _itow(int value, wchar_t *string, int radix)
214 {
215 return _ltow(value, string, radix);
216 }
217
218 /*
219 * @implemented
220 */
221 int
222 _itow_s(int value, wchar_t *str, size_t size, int radix)
223 {
224 return _ltow_s(value, str, size, radix);
225 }
226
227 /*
228 * @implemented
229 */
230 wchar_t *
231 _ltow(long value, wchar_t *string, int radix)
232 {
233 unsigned long val;
234 int negative;
235 WCHAR buffer[33];
236 PWCHAR pos;
237 WCHAR digit;
238
239 if (value < 0 && radix == 10) {
240 negative = 1;
241 val = -value;
242 } else {
243 negative = 0;
244 val = value;
245 } /* if */
246
247 pos = &buffer[32];
248 *pos = '\0';
249
250 do {
251 digit = (WCHAR)(val % radix);
252 val = val / radix;
253 if (digit < 10) {
254 *--pos = '0' + digit;
255 } else {
256 *--pos = 'a' + digit - 10;
257 } /* if */
258 } while (val != 0L);
259
260 if (negative) {
261 *--pos = '-';
262 } /* if */
263
264 if (string != NULL) {
265 memcpy(string, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
266 } /* if */
267 return string;
268 }
269
270 /*
271 * @implemented
272 */
273 int
274 _ltow_s(long value, wchar_t *str, size_t size, int radix)
275 {
276 unsigned long val;
277 unsigned int digit;
278 int is_negative;
279 wchar_t buffer[33], *pos;
280 size_t len;
281
282 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
283 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
284 {
285 if (str && size)
286 str[0] = '\0';
287
288 #ifndef _LIBCNT_
289 *_errno() = EINVAL;
290 #endif
291 return EINVAL;
292 }
293
294 if (value < 0 && radix == 10)
295 {
296 is_negative = 1;
297 val = -value;
298 }
299 else
300 {
301 is_negative = 0;
302 val = value;
303 }
304
305 pos = buffer + 32;
306 *pos = '\0';
307
308 do
309 {
310 digit = val % radix;
311 val /= radix;
312
313 if (digit < 10)
314 *--pos = '0' + digit;
315 else
316 *--pos = 'a' + digit - 10;
317 }
318 while (val != 0);
319
320 if (is_negative)
321 *--pos = '-';
322
323 len = buffer + 33 - pos;
324 if (len > size)
325 {
326 size_t i;
327 wchar_t *p = str;
328
329 /* Copy the temporary buffer backwards up to the available number of
330 * characters. Don't copy the negative sign if present. */
331
332 if (is_negative)
333 {
334 p++;
335 size--;
336 }
337
338 for (pos = buffer + 31, i = 0; i < size; i++)
339 *p++ = *pos--;
340
341 MSVCRT_INVALID_PMT("str[size] is too small");
342 str[0] = '\0';
343 #ifndef _LIBCNT_
344 *_errno() = ERANGE;
345 #endif
346 return ERANGE;
347 }
348
349 memcpy(str, pos, len * sizeof(wchar_t));
350 return 0;
351 }
352
353 /*
354 * @implemented
355 */
356 wchar_t *
357 _ultow(unsigned long value, wchar_t *string, int radix)
358 {
359 WCHAR buffer[33];
360 PWCHAR pos;
361 WCHAR digit;
362
363 pos = &buffer[32];
364 *pos = '\0';
365
366 do {
367 digit = (WCHAR)(value % radix);
368 value = value / radix;
369 if (digit < 10) {
370 *--pos = '0' + digit;
371 } else {
372 *--pos = 'a' + digit - 10;
373 } /* if */
374 } while (value != 0L);
375
376 if (string != NULL) {
377 memcpy(string, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
378 } /* if */
379 return string;
380 }