47 lines
		
	
	
		
			990 B
		
	
	
	
		
			C
		
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			990 B
		
	
	
	
		
			C
		
	
	
| #include "helper.h"
 | |
| /**
 | |
| * C++ version 0.4 char* style "itoa":
 | |
| * Written by Lukás Chmela
 | |
| * Released under GPLv3.
 | |
| */
 | |
| char* itoa(int value, char* result, int base) {
 | |
| 	// check that the base if valid
 | |
| if (base < 2 || base > 36) { *result = '\0'; return result; }
 | |
| 
 | |
| char* ptr = result, *ptr1 = result, tmp_char;
 | |
| int tmp_value;
 | |
| 
 | |
| do {
 | |
| 	tmp_value = value;
 | |
| 	value /= base;
 | |
| 	*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
 | |
| 	} while ( value );
 | |
| 
 | |
| 	// Apply negative sign
 | |
| 	if (tmp_value < 0) *ptr++ = '-';
 | |
| 	*ptr-- = '\0';
 | |
| 	while(ptr1 < ptr) {
 | |
| 		tmp_char = *ptr;
 | |
| 		*ptr--= *ptr1;
 | |
| 		*ptr1++ = tmp_char;
 | |
| 	}
 | |
| 	return result;
 | |
| }
 | |
| 
 | |
| #if __APPLE__ | _WIN32 | _WIN64
 | |
| char * strndup (const char *s, size_t n) {
 | |
|     char *result;
 | |
|     size_t len = strlen (s);
 | |
| 
 | |
|     if (n < len)
 | |
|         len = n;
 | |
| 
 | |
|     result = (char *) malloc (len + 1);
 | |
|     if (!result)
 | |
|         return 0;
 | |
| 
 | |
|     result[len] = '\0';
 | |
|     return (char *) memcpy (result, s, len);
 | |
| }
 | |
| #endif
 |