Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenitoa.c
Go to the documentation of this file.
00001 /* taken from wine ntdll and msvcrt string.c */ 00002 00003 #include <precomp.h> 00004 00005 /* 00006 * @implemented 00007 */ 00008 char * 00009 _i64toa(__int64 value, char *string, int radix) 00010 { 00011 ULONGLONG val; 00012 int negative; 00013 char buffer[65]; 00014 char *pos; 00015 int digit; 00016 00017 if (value < 0 && radix == 10) { 00018 negative = 1; 00019 val = -value; 00020 } else { 00021 negative = 0; 00022 val = value; 00023 } /* if */ 00024 00025 pos = &buffer[64]; 00026 *pos = '\0'; 00027 00028 do { 00029 digit = val % radix; 00030 val = val / radix; 00031 if (digit < 10) { 00032 *--pos = '0' + digit; 00033 } else { 00034 *--pos = 'a' + digit - 10; 00035 } /* if */ 00036 } while (val != 0L); 00037 00038 if (negative) { 00039 *--pos = '-'; 00040 } /* if */ 00041 00042 memcpy(string, pos, &buffer[64] - pos + 1); 00043 return string; 00044 } 00045 00046 /* 00047 * @implemented 00048 */ 00049 int CDECL _i64toa_s(__int64 value, char *str, size_t size, int radix) 00050 { 00051 unsigned __int64 val; 00052 unsigned int digit; 00053 int is_negative; 00054 char buffer[65], *pos; 00055 size_t len; 00056 00057 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) || 00058 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36)) 00059 { 00060 if (str && size) 00061 str[0] = '\0'; 00062 #ifndef _LIBCNT_ 00063 *_errno() = EINVAL; 00064 #endif 00065 return EINVAL; 00066 } 00067 00068 if (value < 0 && radix == 10) 00069 { 00070 is_negative = 1; 00071 val = -value; 00072 } 00073 else 00074 { 00075 is_negative = 0; 00076 val = value; 00077 } 00078 00079 pos = buffer + 64; 00080 *pos = '\0'; 00081 00082 do 00083 { 00084 digit = val % radix; 00085 val /= radix; 00086 00087 if (digit < 10) 00088 *--pos = '0' + digit; 00089 else 00090 *--pos = 'a' + digit - 10; 00091 } 00092 while (val != 0); 00093 00094 if (is_negative) 00095 *--pos = '-'; 00096 00097 len = buffer + 65 - pos; 00098 if (len > size) 00099 { 00100 size_t i; 00101 char *p = str; 00102 00103 /* Copy the temporary buffer backwards up to the available number of 00104 * characters. Don't copy the negative sign if present. */ 00105 00106 if (is_negative) 00107 { 00108 p++; 00109 size--; 00110 } 00111 00112 for (pos = buffer + 63, i = 0; i < size; i++) 00113 *p++ = *pos--; 00114 00115 str[0] = '\0'; 00116 MSVCRT_INVALID_PMT("str[size] is too small"); 00117 #ifndef _LIBCNT_ 00118 *_errno() = ERANGE; 00119 #endif 00120 return ERANGE; 00121 } 00122 00123 memcpy(str, pos, len); 00124 return 0; 00125 } 00126 00127 /* 00128 * @implemented 00129 */ 00130 char * 00131 _ui64toa(unsigned __int64 value, char *string, int radix) 00132 { 00133 char buffer[65]; 00134 char *pos; 00135 int digit; 00136 00137 pos = &buffer[64]; 00138 *pos = '\0'; 00139 00140 do { 00141 digit = value % radix; 00142 value = value / radix; 00143 if (digit < 10) { 00144 *--pos = '0' + digit; 00145 } else { 00146 *--pos = 'a' + digit - 10; 00147 } /* if */ 00148 } while (value != 0L); 00149 00150 memcpy(string, pos, &buffer[64] - pos + 1); 00151 return string; 00152 } 00153 00154 /* 00155 * @implemented 00156 */ 00157 int CDECL _ui64toa_s(unsigned __int64 value, char *str, 00158 size_t size, int radix) 00159 { 00160 char buffer[65], *pos; 00161 int digit; 00162 00163 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) || 00164 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) { 00165 #ifndef _LIBCNT_ 00166 *_errno() = EINVAL; 00167 #endif 00168 return EINVAL; 00169 } 00170 00171 pos = buffer+64; 00172 *pos = '\0'; 00173 00174 do { 00175 digit = value%radix; 00176 value /= radix; 00177 00178 if(digit < 10) 00179 *--pos = '0'+digit; 00180 else 00181 *--pos = 'a'+digit-10; 00182 }while(value != 0); 00183 00184 if((unsigned)(buffer-pos+65) > size) { 00185 MSVCRT_INVALID_PMT("str[size] is too small"); 00186 #ifndef _LIBCNT_ 00187 *_errno() = EINVAL; 00188 #endif 00189 return EINVAL; 00190 } 00191 00192 memcpy(str, pos, buffer-pos+65); 00193 return 0; 00194 } 00195 00196 /* 00197 * @implemented 00198 */ 00199 int CDECL _itoa_s(int value, char *str, size_t size, int radix) 00200 { 00201 return _ltoa_s(value, str, size, radix); 00202 } 00203 00204 /* 00205 * @implemented 00206 */ 00207 char * 00208 _itoa(int value, char *string, int radix) 00209 { 00210 return _ltoa(value, string, radix); 00211 } 00212 00213 /* 00214 * @implemented 00215 */ 00216 char * 00217 _ltoa(long value, char *string, int radix) 00218 { 00219 unsigned long val; 00220 int negative; 00221 char buffer[33]; 00222 char *pos; 00223 int digit; 00224 00225 if (value < 0 && radix == 10) { 00226 negative = 1; 00227 val = -value; 00228 } else { 00229 negative = 0; 00230 val = value; 00231 } /* if */ 00232 00233 pos = &buffer[32]; 00234 *pos = '\0'; 00235 00236 do { 00237 digit = val % radix; 00238 val = val / radix; 00239 if (digit < 10) { 00240 *--pos = '0' + digit; 00241 } else { 00242 *--pos = 'a' + digit - 10; 00243 } /* if */ 00244 } while (val != 0L); 00245 00246 if (negative) { 00247 *--pos = '-'; 00248 } /* if */ 00249 00250 memcpy(string, pos, &buffer[32] - pos + 1); 00251 return string; 00252 } 00253 00254 /* 00255 * @implemented 00256 */ 00257 int CDECL _ltoa_s(long value, char *str, size_t size, int radix) 00258 { 00259 unsigned long val; 00260 unsigned int digit; 00261 int is_negative; 00262 char buffer[33], *pos; 00263 size_t len; 00264 00265 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) || 00266 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36)) 00267 { 00268 if (str && size) 00269 str[0] = '\0'; 00270 00271 #ifndef _LIBCNT_ 00272 *_errno() = EINVAL; 00273 #endif 00274 return EINVAL; 00275 } 00276 00277 if (value < 0 && radix == 10) 00278 { 00279 is_negative = 1; 00280 val = -value; 00281 } 00282 else 00283 { 00284 is_negative = 0; 00285 val = value; 00286 } 00287 00288 pos = buffer + 32; 00289 *pos = '\0'; 00290 00291 do 00292 { 00293 digit = val % radix; 00294 val /= radix; 00295 00296 if (digit < 10) 00297 *--pos = '0' + digit; 00298 else 00299 *--pos = 'a' + digit - 10; 00300 } 00301 while (val != 0); 00302 00303 if (is_negative) 00304 *--pos = '-'; 00305 00306 len = buffer + 33 - pos; 00307 if (len > size) 00308 { 00309 size_t i; 00310 char *p = str; 00311 00312 /* Copy the temporary buffer backwards up to the available number of 00313 * characters. Don't copy the negative sign if present. */ 00314 00315 if (is_negative) 00316 { 00317 p++; 00318 size--; 00319 } 00320 00321 for (pos = buffer + 31, i = 0; i < size; i++) 00322 *p++ = *pos--; 00323 00324 str[0] = '\0'; 00325 MSVCRT_INVALID_PMT("str[size] is too small"); 00326 #ifndef _LIBCNT_ 00327 *_errno() = EINVAL; 00328 #endif 00329 return ERANGE; 00330 } 00331 00332 memcpy(str, pos, len); 00333 return 0; 00334 } 00335 00336 /* 00337 * @implemented 00338 */ 00339 char * 00340 _ultoa(unsigned long value, char *string, int radix) 00341 { 00342 char buffer[33]; 00343 char *pos; 00344 int digit; 00345 00346 pos = &buffer[32]; 00347 *pos = '\0'; 00348 00349 do { 00350 digit = value % radix; 00351 value = value / radix; 00352 if (digit < 10) { 00353 *--pos = '0' + digit; 00354 } else { 00355 *--pos = 'a' + digit - 10; 00356 } /* if */ 00357 } while (value != 0L); 00358 00359 memcpy(string, pos, &buffer[32] - pos + 1); 00360 00361 return string; 00362 } Generated on Sun May 27 2012 04:36:39 for ReactOS by
1.7.6.1
|