1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #include <stdlib.h>
typedef struct s_flag
{
char flags;
int width;
int precision;
int lenght;
char specifer;
va_list *list;
} t_flag;
t_flag *init();
extern struct {
void (*fct_p)(t_flag*);
char opt;
} op_print[];
typedef int EPITECH_BOOL;
#define EPITECH_FALSE 0
#define EPITECH_TRUE 1
int OperateurTernaire_int(EPITECH_BOOL bCondition, int valeurSiOui, int valeurSiNon)
{
if(bCondition)
return valeurSiOui;
else
return valeurSiNon;
}
EPITECH_BOOL IsFlag(char c)
{
if(c == '-' || c == '+' || c == ' ' || c == '#' || c == '0')
return EPITECH_TRUE;
else
return EPITECH_FALSE;
}
void NextChar(char **ppCurrentChar)
{
(*ppCurrentChar)++;
}
int ParseInt(char **ppCurrentChar)
{
int value = 0;
while (**ppCurrentChar >= '0' && **ppCurrentChar <= '9')
{
value *= 10;
value += (**ppCurrentChar - '0');
NextChar(ppCurrentChar);
}
return value;
}
void FindAndCallPrintFunction(char currentChar, t_flag *pStruct)
{
int i;
i = 0;
while (op_print[i].opt)
{
if (currentChar == op_print[i].opt)
{
op_print[i].fct_p(pStruct);
}
i++;
}
}
void ParseStruct(char **ppCurrentChar, t_flag *pStruct)
{
int width;
if (IsFlag(**ppCurrentChar))
{
pStruct->flags = **ppCurrentChar; /*Est-ce normal qu'on ne puisse stocker qu'un seul flag dans un champ appelé "flags" ?*/
NextChar(ppCurrentChar);
}
else
pStruct->flags = '~';
width = ParseInt(ppCurrentChar);
pStruct->width = OperateurTernaire_int(width!=0, width, -1);
if (**ppCurrentChar == '.')
{
NextChar(ppCurrentChar);
if (**ppCurrentChar == '*')
pStruct->precision = -2;
else
{
int const precision = ParseInt(ppCurrentChar);
pStruct->precision = OperateurTernaire_int(precision!=0, precision, -1);
}
}
}
void my_printopt(char **ppCurrentChar, va_list *ap)
{
t_flag *pStruct;
NextChar(ppCurrentChar); /*sauter le pourcent*/
pStruct = init();
if(pStruct != NULL)
{
ParseStruct(ppCurrentChar, pStruct);
pStruct->list = ap;
FindAndCallPrintFunction(**ppCurrentChar, pStruct);
free(pStruct);
}
} |
Partager