Mostly minor updates to the source tree for portcls.
[reactos.git] / reactos / lib / string / itoa.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <windows.h>
4
5 /*
6 * @implemented
7 * copy _i64toa from wine cvs 2006 month 05 day 21
8 */
9 char *
10 _i64toa(__int64 value, char *string, int radix)
11 {
12 ULONGLONG val;
13 int negative;
14 char buffer[65];
15 char *pos;
16 int digit;
17
18 if (value < 0 && radix == 10) {
19 negative = 1;
20 val = -value;
21 } else {
22 negative = 0;
23 val = value;
24 } /* if */
25
26 pos = &buffer[64];
27 *pos = '\0';
28
29 do {
30 digit = val % radix;
31 val = val / radix;
32 if (digit < 10) {
33 *--pos = '0' + digit;
34 } else {
35 *--pos = 'a' + digit - 10;
36 } /* if */
37 } while (val != 0L);
38
39 if (negative) {
40 *--pos = '-';
41 } /* if */
42
43 memcpy(string, pos, &buffer[64] - pos + 1);
44 return string;
45 }
46
47
48 /*
49 * @implemented
50 * copy _i64toa from wine cvs 2006 month 05 day 21
51 */
52 char *
53 _ui64toa(unsigned __int64 value, char *string, int radix)
54 {
55 char buffer[65];
56 char *pos;
57 int digit;
58
59 pos = &buffer[64];
60 *pos = '\0';
61
62 do {
63 digit = value % radix;
64 value = value / radix;
65 if (digit < 10) {
66 *--pos = '0' + digit;
67 } else {
68 *--pos = 'a' + digit - 10;
69 } /* if */
70 } while (value != 0L);
71
72 memcpy(string, pos, &buffer[64] - pos + 1);
73 return string;
74 }
75
76
77 /*
78 * @implemented
79 */
80 char *
81 _itoa(int value, char *string, int radix)
82 {
83 return _ltoa(value, string, radix);
84 }
85
86
87 /*
88 * @implemented
89 */
90 char *
91 _ltoa(long value, char *string, int radix)
92 {
93 unsigned long val;
94 int negative;
95 char buffer[33];
96 char *pos;
97 int digit;
98
99 if (value < 0 && radix == 10) {
100 negative = 1;
101 val = -value;
102 } else {
103 negative = 0;
104 val = value;
105 } /* if */
106
107 pos = &buffer[32];
108 *pos = '\0';
109
110 do {
111 digit = val % radix;
112 val = val / radix;
113 if (digit < 10) {
114 *--pos = '0' + digit;
115 } else {
116 *--pos = 'a' + digit - 10;
117 } /* if */
118 } while (val != 0L);
119
120 if (negative) {
121 *--pos = '-';
122 } /* if */
123
124 memcpy(string, pos, &buffer[32] - pos + 1);
125 return string;
126 }
127
128
129 /*
130 * @implemented
131 * copy it from wine 0.9.0 with small modifcations do check for NULL
132 */
133 char *
134 _ultoa(unsigned long value, char *string, int radix)
135 {
136 char buffer[33];
137 char *pos;
138 int digit;
139
140 pos = &buffer[32];
141 *pos = '\0';
142
143 if (string == NULL)
144 {
145 return NULL;
146 }
147
148 do {
149 digit = 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 memcpy(string, pos, &buffer[32] - pos + 1);
159
160 return string;
161 }