Half implemented printf() [DOESN'T COMPILE]
Idk what to write, a friend wants to play minecraft so... here is the code I wrote before him calling me.
This commit is contained in:
parent
54a0729e77
commit
cac8d0a033
@ -1,3 +1,3 @@
|
||||
stdio.h(4): Warning! W138: No newline at end of file
|
||||
stdio.h(5): Warning! W138: No newline at end of file
|
||||
main.c(8): Warning! W138: No newline at end of file
|
||||
main.c(4): Warning! W303: Parameter 'bootDrive' has been defined, but not referenced
|
||||
|
@ -8,5 +8,7 @@ typedef signed long int int32_t;
|
||||
typedef unsigned long int uint32_t;
|
||||
typedef signed long int int64_t;
|
||||
typedef unsigned long int uint64_t;
|
||||
typedef uint8_t bool;
|
||||
|
||||
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
@ -13,3 +13,154 @@ void puts(const char *str)
|
||||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
#define PRINTF_STATE_NORMAL 0
|
||||
#define PRINTF_STATE_LENGHT 1
|
||||
#define PRINTF_STATE_LENGHT_SHORT 2
|
||||
#define PRINTF_STATE_LENGHT_LONG 1
|
||||
#define PRINTF_STATE_SPEC 4
|
||||
#define PRINTF_LENGTH_DEFAULT 0
|
||||
#define PRINTF_LENGTH_SHORT_SHORT 1
|
||||
#define PRINTF_LENGTH_SHORT 2
|
||||
#define PRINTF_LENGTH_LONG 3
|
||||
#define PRINTF_LENGTH_LONG_LONG 4
|
||||
int* printf_number(int *argp, int length, bool sign, int radix)
|
||||
const char g_HexChars[] = '0123456789abcdef'
|
||||
|
||||
void _cdecl printf(const char *fmt, ...)
|
||||
{
|
||||
int *argp = (int*)&fmt;
|
||||
int state = PRINTF_STATE_NORMAL;
|
||||
int length = PRINTF_LENGTH_DEFAULT;
|
||||
int radix = 10;
|
||||
bool sign = false;
|
||||
argp++;
|
||||
while (*fmt)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case PRINTF_STATE_NORMAL:
|
||||
switch (*fmt)
|
||||
{
|
||||
case '%':
|
||||
state = PRINTF_STATE_LENGHT;
|
||||
break;
|
||||
default:
|
||||
putc(*fmt);
|
||||
break;
|
||||
}
|
||||
|
||||
case PRINTF_STATE_LENGHT:
|
||||
switch (*fmt)
|
||||
{
|
||||
case 'h':
|
||||
length = PRINTF_LENGTH_SHORT;
|
||||
state = PRINTF_STATE_LENGHT_SHORT;
|
||||
break;
|
||||
case 'l':
|
||||
length = PRINTF_LENGTH_LONG;
|
||||
state = PRINTF_STATE_LENGHT_LONG;
|
||||
break;
|
||||
|
||||
default:
|
||||
goto PRINTF_STATE_SPEC;
|
||||
}
|
||||
break;
|
||||
|
||||
case PRINTF_LENGTH_SHORT:
|
||||
if (*fmt == 'h')
|
||||
{
|
||||
length = PRINTF_LENGTH_SHORT_SHORT;
|
||||
state = PRINTF_STATE_SPEC;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto PRINTF_STATE_SPEC;
|
||||
}
|
||||
break;
|
||||
|
||||
case PRINTF_STATE_SPEC:
|
||||
switch (*fmt)
|
||||
{
|
||||
case 'c':
|
||||
putc((char)*argp);
|
||||
argp++;
|
||||
break;
|
||||
case 's':
|
||||
puts(*(char **)argp);
|
||||
argp++;
|
||||
break;
|
||||
case '%':
|
||||
putc('%');
|
||||
break;
|
||||
case 'd':
|
||||
case 'i':
|
||||
radix = 10;
|
||||
sign = true;
|
||||
argp = printf_number(argp, length, sign, radix);
|
||||
break;
|
||||
case 'u':
|
||||
radix = 10;
|
||||
sign = false;
|
||||
argp = printf_number(argp, length, sign, radix);
|
||||
break;
|
||||
case 'x':
|
||||
case 'X':
|
||||
case 'p':
|
||||
radix = 16;
|
||||
sign = false;
|
||||
argp = printf_number(argp, length, sign, radix);
|
||||
break;
|
||||
case 'o':
|
||||
radix = 8;
|
||||
sign = false;
|
||||
argp = printf_number(argp, length, sign, radix);
|
||||
break;
|
||||
//Se è messo un carattere non valido questo codice fa in modo che li ignora.
|
||||
default:
|
||||
break;
|
||||
}
|
||||
//Porta le variabili allo stato di default / normale.
|
||||
state = PRINTF_STATE_NORMAL;
|
||||
length = PRINTF_LENGTH_DEFAULT;
|
||||
radix = 10;
|
||||
sign = false;
|
||||
break;
|
||||
}
|
||||
fmt++;
|
||||
}
|
||||
}
|
||||
|
||||
int* printf_number(int* argp, int length, bool sign, int radix)
|
||||
{
|
||||
char buffer[32];
|
||||
unsigned long long number;
|
||||
int number_sign = 1;
|
||||
int pos = 0;
|
||||
|
||||
switch (length)
|
||||
{
|
||||
case PRINTF_LENGTH_SHORT_SHORT:
|
||||
case PRINTF_LENGTH_SHORT:
|
||||
case PRINTF_LENGTH_LONG:
|
||||
case PRINTF_LENGTH_LONG_LONG:
|
||||
case PRINTF_LENGTH_DEFAULT:
|
||||
if (sign)
|
||||
{
|
||||
int n = *argp;
|
||||
if (n < 0)
|
||||
{
|
||||
n = -n;
|
||||
number_sign = -1;
|
||||
}
|
||||
number = n;
|
||||
}
|
||||
else
|
||||
{
|
||||
number = (*unsigned int*)argp;
|
||||
|
||||
}
|
||||
argp++;
|
||||
break;
|
||||
}
|
||||
}
|
@ -1,2 +1,35 @@
|
||||
stdio.h(4): Warning! W138: No newline at end of file
|
||||
stdio.h(5): Warning! W138: No newline at end of file
|
||||
x86.h(4): Warning! W138: No newline at end of file
|
||||
stdio.c(31): Error! E1024: Declared symbol 'printf' is not in parameter list
|
||||
stdio.c(31): Error! E1076: Missing semicolon at end of declaration
|
||||
stdio.c(32): Error! E1034: Symbol 'argp' already defined
|
||||
stdio.c(32): Note! I2002: 'argp' defined in: stdio.c(28)
|
||||
stdio.c(32): Error! E1011: Symbol 'fmt' has not been declared
|
||||
stdio.c(34): Error! E1034: Symbol 'length' already defined
|
||||
stdio.c(34): Note! I2002: 'length' defined in: stdio.c(28)
|
||||
stdio.c(35): Error! E1034: Symbol 'radix' already defined
|
||||
stdio.c(35): Note! I2002: 'radix' defined in: stdio.c(28)
|
||||
stdio.c(36): Error! E1034: Symbol 'sign' already defined
|
||||
stdio.c(36): Note! I2002: 'sign' defined in: stdio.c(28)
|
||||
stdio.c(38): Error! E1029: Expression must be 'pointer to ...'
|
||||
stdio.c(43): Error! E1029: Expression must be 'pointer to ...'
|
||||
stdio.c(49): Error! E1029: Expression must be 'pointer to ...'
|
||||
stdio.c(49): Error! E1151: Parameter count does not agree with previous definition
|
||||
stdio.c(49): Note! I2002: 'putc' defined in: stdio.c(4)
|
||||
stdio.c(54): Error! E1029: Expression must be 'pointer to ...'
|
||||
stdio.c(66): Error! E1038: Expecting label for goto statement
|
||||
stdio.c(71): Error! E1029: Expression must be 'pointer to ...'
|
||||
stdio.c(78): Error! E1038: Expecting label for goto statement
|
||||
stdio.c(83): Error! E1029: Expression must be 'pointer to ...'
|
||||
stdio.c(28): Warning! W303: Parameter 'sign' has been defined, but not referenced
|
||||
stdio.c(28): Warning! W303: Parameter 'length' has been defined, but not referenced
|
||||
stdio.c(28): Warning! W303: Parameter 'radix' has been defined, but not referenced
|
||||
stdio.c(28): Warning! W303: Parameter 'argp' has been defined, but not referenced
|
||||
stdio.c(132): Warning! W107: Missing return value for function 'printf_number'
|
||||
stdio.c(135): Error! E1034: Symbol 'printf_number' already defined
|
||||
stdio.c(137): Warning! W138: No newline at end of file
|
||||
stdio.c(134): Warning! W303: Parameter 'sign' has been defined, but not referenced
|
||||
stdio.c(134): Warning! W303: Parameter 'length' has been defined, but not referenced
|
||||
stdio.c(134): Warning! W303: Parameter 'radix' has been defined, but not referenced
|
||||
stdio.c(134): Warning! W303: Parameter 'argp' has been defined, but not referenced
|
||||
stdio.c(137): Warning! W107: Missing return value for function 'printf_number'
|
||||
|
@ -2,3 +2,4 @@
|
||||
|
||||
void putc(char c);
|
||||
void puts(const char* str);
|
||||
void _cdecl printf(const char *fmt, ...);
|
Loading…
x
Reference in New Issue
Block a user