Feat: Better IO functions & Refractor of "colors.h" to "stdio.h"
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
#ifndef TYPES_H
|
||||
#define TYPES_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
static char* __itoa(int value, char* buffer, int base) {
|
||||
char* ptr = buffer, *ptr1 = buffer, tmp_char;
|
||||
int tmp_value;
|
||||
|
||||
if (value < 0 && base == 10) {
|
||||
value = -value;
|
||||
*ptr++ = '-';
|
||||
ptr1++;
|
||||
}
|
||||
|
||||
do {
|
||||
tmp_value = value;
|
||||
value /= base;
|
||||
*ptr++ = "0123456789ABCDEF"[tmp_value - value * base];
|
||||
} while (value);
|
||||
|
||||
*ptr-- = '\0';
|
||||
|
||||
while (ptr1 < ptr) {
|
||||
tmp_char = *ptr;
|
||||
*ptr-- = *ptr1;
|
||||
*ptr1++ = tmp_char;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static char* __utoa(unsigned int value, char* buffer, int base) {
|
||||
char* ptr = buffer, *ptr1 = buffer, tmp_char;
|
||||
unsigned int tmp_value;
|
||||
|
||||
do {
|
||||
tmp_value = value;
|
||||
value /= base;
|
||||
*ptr++ = "0123456789ABCDEF"[tmp_value - value * base];
|
||||
} while (value);
|
||||
|
||||
*ptr-- = '\0';
|
||||
|
||||
while (ptr1 < ptr) {
|
||||
tmp_char = *ptr;
|
||||
*ptr-- = *ptr1;
|
||||
*ptr1++ = tmp_char;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static char* __ftoa(double value, char* buffer) {
|
||||
int int_part = (int)value;
|
||||
double frac_part = value - (double)int_part;
|
||||
char* ptr = __itoa(int_part, buffer, 10);
|
||||
|
||||
*ptr++ = '.';
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
frac_part *= 10.0;
|
||||
int digit = (int)frac_part;
|
||||
*ptr++ = '0' + digit;
|
||||
frac_part -= (double)digit;
|
||||
}
|
||||
|
||||
*ptr = '\0';
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static char* itoa(int value, char* buffer) {
|
||||
return __itoa(value, buffer, 10);
|
||||
}
|
||||
|
||||
static char* utoa(unsigned int value, char* buffer) {
|
||||
return __utoa(value, buffer, 10);
|
||||
}
|
||||
|
||||
static char* otoa(unsigned int value, char* buffer) {
|
||||
return __utoa(value, buffer, 8);
|
||||
}
|
||||
|
||||
static char* xtoa(unsigned int value, char* buffer) {
|
||||
return __utoa(value, buffer, 16);
|
||||
}
|
||||
|
||||
static char* ftoa(double value, char* buffer) {
|
||||
return __ftoa(value, buffer);
|
||||
}
|
||||
|
||||
#endif // TYPES_H
|
||||
Reference in New Issue
Block a user