new shiny project architecture
This commit is contained in:
parent
8c1cadbb75
commit
74351cc3de
99 changed files with 505 additions and 13 deletions
3
libftasm/.gitmodules
vendored
Normal file
3
libftasm/.gitmodules
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[submodule "ft_printf"]
|
||||
path = ft_printf
|
||||
url = http://github.com/jzck/ft_printf
|
||||
|
|
@ -2,7 +2,7 @@ NAME = libft.a
|
|||
CC = gcc
|
||||
AR = ar -rc
|
||||
|
||||
D_SRC = .
|
||||
D_SRC = src
|
||||
D_OBJ = obj
|
||||
|
||||
O_FLAGS =
|
||||
|
|
@ -11,17 +11,18 @@ DEBUG =
|
|||
MKDIR = mkdir -p
|
||||
RM = /bin/rm -rf
|
||||
|
||||
F_SRC := $(shell ls -1 $(D_SRC) | grep "\.c$$")
|
||||
F_OBJ := $(F_SRC:.c=.o)
|
||||
F_OBJ := $(addprefix $(D_OBJ)/, $(F_OBJ))
|
||||
D_INC = includes
|
||||
|
||||
F_SRC := $(shell find $(D_SRC) -type f -regex ".*\.c$$")
|
||||
F_OBJ := $(addprefix $(D_OBJ)/, $(notdir $(F_SRC:.c=.o)))
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(D_OBJ)/%.o: $(D_SRC)/%.c
|
||||
$(D_OBJ)/%.o: $(D_SRC)/*/%.c
|
||||
@$(MKDIR) $(D_OBJ)
|
||||
@$(CC) $(W_FLAGS) -c $< -o $@ $(DEBUG)
|
||||
@$(CC) -I$(D_INC) $(W_FLAGS) -c $< -o $@ $(DEBUG)
|
||||
@echo "(libft) Compiling "$<"..."
|
||||
|
||||
$(NAME): $(F_OBJ)
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
jhalford
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
#include "libft.h"
|
||||
|
||||
int ft_diff(void *a, void *b)
|
||||
{
|
||||
return (*(int *)a - *(int *)b);
|
||||
}
|
||||
BIN
libftasm/ft_printf.pdf
Normal file
BIN
libftasm/ft_printf.pdf
Normal file
Binary file not shown.
57
libftasm/includes/libftprintf.h
Normal file
57
libftasm/includes/libftprintf.h
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#ifndef LIBFTPRINTF_H
|
||||
# define LIBFTPRINTF_H
|
||||
# include "libft.h"
|
||||
# include <stdarg.h>
|
||||
# define ALL_FLAGS "#0- +"
|
||||
# define ALL_CONVERSIONS "sSpdDioOuUxXcC"
|
||||
|
||||
typedef struct s_fmt
|
||||
{
|
||||
char flags[6];
|
||||
int width;
|
||||
int precision;
|
||||
char modifier[3];
|
||||
char conversion;
|
||||
int valid;
|
||||
} t_fmt;
|
||||
|
||||
typedef char *(*t_converter)(t_fmt *fmt, va_list ap);
|
||||
|
||||
typedef struct s_conv
|
||||
{
|
||||
char id;
|
||||
char base[20];
|
||||
t_converter converter;
|
||||
char allowed_flags[6];
|
||||
} t_conv;
|
||||
|
||||
extern t_conv g_convs[];
|
||||
|
||||
t_fmt *ft_fmt_init(void);
|
||||
void ft_fmt_print(t_fmt *fmt);
|
||||
|
||||
int ft_printf(const char *format, ...);
|
||||
t_fmt *ft_parse(char **format);
|
||||
char *ft_transform(t_fmt *fmt, va_list ap);
|
||||
|
||||
void ft_fmt_error_conv(char conv);
|
||||
void ft_fmt_error_mod_conv(char *mod, char conv);
|
||||
void ft_fmt_error_flag_conv(char flag, char conv);
|
||||
void ft_fmt_error_flag_flag(char flag1, char flag2);
|
||||
|
||||
void ft_fmt_simplify(t_fmt *fmt);
|
||||
int ft_fmt_validate_conversion(t_fmt *fmt);
|
||||
void ft_fmt_validate_flags(t_fmt *fmt);
|
||||
void ft_fmt_validate_mod(t_fmt *fmt);
|
||||
|
||||
char *ft_signed_conversion(t_fmt *fmt, va_list ap);
|
||||
char *ft_unsigned_conversion(t_fmt *fmt, va_list ap);
|
||||
char *ft_str_conversion(t_fmt *fmt, va_list ap);
|
||||
char *ft_char_conversion(t_fmt *fmt, va_list ap);
|
||||
|
||||
void ft_pad_left(char *str, t_fmt *fmt);
|
||||
void ft_pad_right(char *str, t_fmt *fmt);
|
||||
void ft_pad_sharp_o(char *str);
|
||||
void ft_pad_sharp_x(char *str, t_fmt *fmt);
|
||||
void ft_pad_sharp(char *str, t_fmt *fmt);
|
||||
#endif
|
||||
55
libftasm/src/ft_printf/ft_conversion.c
Normal file
55
libftasm/src/ft_printf/ft_conversion.c
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#include "libftprintf.h"
|
||||
|
||||
char *ft_signed_conversion(t_fmt *fmt, va_list ap)
|
||||
{
|
||||
char base10[] = "0123456789";
|
||||
long long arg = va_arg(ap, int);
|
||||
|
||||
(void)fmt;
|
||||
return (ft_lltoa_base(arg, base10, fmt->flags));
|
||||
}
|
||||
|
||||
char *ft_unsigned_conversion(t_fmt *fmt, va_list ap)
|
||||
{
|
||||
unsigned int uiarg;
|
||||
unsigned long long ullarg;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (fmt->conversion != g_convs[i].id)
|
||||
i++;
|
||||
if (!*fmt->modifier
|
||||
|| ft_strcmp(fmt->modifier, "hh") == 0
|
||||
|| ft_strcmp(fmt->modifier, "h") == 0
|
||||
|| ft_strcmp(fmt->modifier, "z") == 0)
|
||||
{
|
||||
uiarg = va_arg(ap, int);
|
||||
return (ft_uitoa_base(uiarg, g_convs[i].base));
|
||||
}
|
||||
ullarg = va_arg(ap, long long);
|
||||
return (ft_ulltoa_base(ullarg, g_convs[i].base));
|
||||
}
|
||||
|
||||
char *ft_char_conversion(t_fmt *fmt, va_list ap)
|
||||
{
|
||||
char *ret;
|
||||
|
||||
(void)fmt;
|
||||
ret = (char *)malloc(sizeof(char) + 1);
|
||||
/* if (ft_strcmp(fmt->modifier, "l") == 0) */
|
||||
/* va_arg(ap, wint_t); */
|
||||
ret[0] = (char)va_arg(ap, int);
|
||||
ret[1] = '\0';
|
||||
return (ret);
|
||||
}
|
||||
|
||||
char *ft_str_conversion(t_fmt *fmt, va_list ap)
|
||||
{
|
||||
char *ret;
|
||||
|
||||
(void)fmt;
|
||||
/* if (ft_strcmp(fmt->modifier, "l") == 0) */
|
||||
/* va_arg(ap, wchar_t *); */
|
||||
ret = ft_strdup(va_arg(ap, char *));
|
||||
return (ret);
|
||||
}
|
||||
95
libftasm/src/ft_printf/ft_parse.c
Normal file
95
libftasm/src/ft_printf/ft_parse.c
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
#include "libftprintf.h"
|
||||
|
||||
void ft_parse_flags(t_fmt *fmt, char **format)
|
||||
{
|
||||
int i;
|
||||
char *str;
|
||||
|
||||
|
||||
i = 0;
|
||||
str = *format;
|
||||
while (str[i])
|
||||
{
|
||||
if (ft_strchr(ALL_FLAGS, (int)str[i]))
|
||||
{
|
||||
if (!ft_strchr(fmt->flags, (int)str[i]))
|
||||
ft_strncat(fmt->flags, str + i, 1);
|
||||
}
|
||||
else
|
||||
break ;
|
||||
i++;
|
||||
}
|
||||
*format += i;
|
||||
/* printf("\nparse_flags: %s\n", *format); */
|
||||
/* fflush(stdout); */
|
||||
}
|
||||
|
||||
void ft_parse_nums(t_fmt *fmt, char **format)
|
||||
{
|
||||
int i;
|
||||
char buf[10];
|
||||
char *str;
|
||||
|
||||
i = 0;
|
||||
str = *format;
|
||||
ft_strcpy(buf, "0");
|
||||
while (ft_isdigit((int)(str[i])))
|
||||
ft_strncat(buf, str + i++, 1);
|
||||
fmt->width = ft_atoi(buf);
|
||||
ft_strcpy(buf, "0");
|
||||
if (str[i] == '.')
|
||||
{
|
||||
i++;
|
||||
while (ft_isdigit(str[i]))
|
||||
ft_strncat(buf, str + i++, 1);
|
||||
}
|
||||
fmt->precision = ft_atoi(buf);
|
||||
*format += i;
|
||||
/* printf("\nparse_nums: %s\n", *format); */
|
||||
/* fflush(stdout); */
|
||||
}
|
||||
|
||||
void ft_parse_modifiers(t_fmt *fmt, char **format)
|
||||
{
|
||||
char *str;
|
||||
|
||||
str = *format;
|
||||
if (str[0] == 'h' && str[1] == 'h')
|
||||
ft_strcpy(fmt->modifier, "hh");
|
||||
else if (str[0] == 'h' && str[1] != 'h')
|
||||
ft_strcpy(fmt->modifier, "h");
|
||||
else if (str[0] == 'l' && str[1] == 'l')
|
||||
ft_strcpy(fmt->modifier, "ll");
|
||||
else if (str[0] == 'l' && str[1] != 'l')
|
||||
ft_strcpy(fmt->modifier, "l");
|
||||
else if (str[0] == 'j')
|
||||
ft_strcpy(fmt->modifier, "j");
|
||||
else if (str[0] == 'z')
|
||||
ft_strcpy(fmt->modifier, "z");
|
||||
else
|
||||
ft_strcpy(fmt->modifier, "");
|
||||
*format += ft_strlen(fmt->modifier);
|
||||
/* printf("\nparse_mods: %s\n", *format); */
|
||||
/* fflush(stdout); */
|
||||
}
|
||||
|
||||
|
||||
t_fmt *ft_parse(char **format)
|
||||
{
|
||||
t_fmt *fmt;
|
||||
|
||||
fmt = ft_fmt_init();
|
||||
ft_parse_flags(fmt, format);
|
||||
ft_parse_nums(fmt, format);
|
||||
ft_parse_modifiers(fmt, format);
|
||||
fmt->conversion = **format;
|
||||
(*format)++;
|
||||
|
||||
ft_fmt_validate_mod(fmt);
|
||||
ft_fmt_validate_flags(fmt);
|
||||
ft_fmt_simplify(fmt);
|
||||
fmt->valid = ft_fmt_validate_conversion(fmt) ? 0 : 1;
|
||||
|
||||
ft_fmt_print(fmt);
|
||||
return (fmt);
|
||||
}
|
||||
42
libftasm/src/ft_printf/ft_printf.c
Normal file
42
libftasm/src/ft_printf/ft_printf.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#include "libftprintf.h"
|
||||
|
||||
t_conv g_convs[] =
|
||||
{
|
||||
{'d', "0123456789", ft_signed_conversion, "0- +"},
|
||||
{'i', "0123456789", ft_signed_conversion, "0- +"},
|
||||
{'u', "0123456789", ft_unsigned_conversion, "#0-"},
|
||||
{'o', "01234567", ft_unsigned_conversion, "#0-"},
|
||||
{'x', "0123456789abcdef", ft_unsigned_conversion, "#0-"},
|
||||
{'X', "0123456789ABCDEF", ft_unsigned_conversion, "#0-"},
|
||||
{'s', "", ft_str_conversion, "-"},
|
||||
{'c', "", ft_char_conversion, "-"},
|
||||
};
|
||||
|
||||
int ft_printf(const char *format, ...)
|
||||
{
|
||||
va_list ap1;
|
||||
char *str;
|
||||
char final[1000];
|
||||
t_fmt *fmt;
|
||||
|
||||
va_start(ap1, format);
|
||||
str = ft_strdup(format);
|
||||
ft_putendl(format);
|
||||
while (*str)
|
||||
{
|
||||
if (*str == '%')
|
||||
{
|
||||
str++;
|
||||
if (!(fmt = ft_parse(&str)))
|
||||
return (1);
|
||||
if (!fmt->valid)
|
||||
ft_strncat(final, &fmt->conversion, 1);
|
||||
else
|
||||
ft_strcat(final, ft_transform(fmt, ap1));
|
||||
}
|
||||
else
|
||||
ft_strncat(final, str++, 1);
|
||||
}
|
||||
ft_putstr(final);
|
||||
return (0);
|
||||
}
|
||||
26
libftasm/src/ft_printf/ft_tranform.c
Normal file
26
libftasm/src/ft_printf/ft_tranform.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#include "libftprintf.h"
|
||||
|
||||
char *ft_transform(t_fmt *fmt, va_list ap)
|
||||
{
|
||||
char *buf;
|
||||
char *ret;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (fmt->conversion != g_convs[i].id)
|
||||
i++;
|
||||
ret = (g_convs[i].converter)(fmt, ap);
|
||||
if (fmt->width > (int)ft_strlen(ret))
|
||||
{
|
||||
buf = ret;
|
||||
ret = (char *)malloc(sizeof(char) * (fmt->width + 5));
|
||||
ft_strcpy(ret, buf);
|
||||
}
|
||||
/* printf("before padding: '%s'\n", ret); */
|
||||
/* fflush(stdout); */
|
||||
if (ft_strchr(fmt->flags, '-'))
|
||||
ft_pad_right(ret, fmt);
|
||||
else
|
||||
ft_pad_left(ret, fmt);
|
||||
return (ret);
|
||||
}
|
||||
33
libftasm/src/ft_printf/lib_fmt.c
Normal file
33
libftasm/src/ft_printf/lib_fmt.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include "libftprintf.h"
|
||||
|
||||
t_fmt *ft_fmt_init(void)
|
||||
{
|
||||
t_fmt *fmt;
|
||||
|
||||
fmt = (t_fmt *)malloc(sizeof(t_fmt) + 1);
|
||||
ft_bzero(fmt->flags, 6);
|
||||
/* fmt->flags.hash = 0; */
|
||||
/* fmt->flags.zero = 0; */
|
||||
/* fmt->flags.minus = 0; */
|
||||
/* fmt->flags.space = 0; */
|
||||
/* fmt->flags.plus = 0; */
|
||||
ft_bzero(fmt->modifier, 3);
|
||||
fmt->conversion = '\0';
|
||||
fmt->width = 0;
|
||||
fmt->precision = 0;
|
||||
fmt->valid = 0;
|
||||
return (fmt);
|
||||
}
|
||||
|
||||
void ft_fmt_print(t_fmt *fmt)
|
||||
{
|
||||
printf("\n---\n");
|
||||
printf("valid: %i\n", fmt->valid);
|
||||
printf("flags: %s\n", fmt->flags);
|
||||
printf("width: %i\n", fmt->width);
|
||||
printf("prec.: %i\n", fmt->precision);
|
||||
printf("modif: %s\n", fmt->modifier);
|
||||
printf("conv.: %c\n", fmt->conversion);
|
||||
printf("---\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
35
libftasm/src/ft_printf/lib_fmt_error.c
Normal file
35
libftasm/src/ft_printf/lib_fmt_error.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include "libftprintf.h"
|
||||
|
||||
void ft_fmt_error_conv(char conv)
|
||||
{
|
||||
ft_putstr("Warning: invalid or unsupported conversion specifier '");
|
||||
ft_putchar(conv);
|
||||
ft_putendl("'");
|
||||
}
|
||||
|
||||
void ft_fmt_error_mod_conv(char *mod, char conv)
|
||||
{
|
||||
ft_putstr("warning: length modifier '");
|
||||
ft_putstr(mod);
|
||||
ft_putstr("' results in undefined behaviour or no effect with '");
|
||||
ft_putchar(conv);
|
||||
ft_putendl("' conversion specifier");
|
||||
}
|
||||
|
||||
void ft_fmt_error_flag_conv(char flag, char conv)
|
||||
{
|
||||
ft_putstr("warning: flag '");
|
||||
ft_putchar(flag);
|
||||
ft_putstr("' results in undefined behaviour with '");
|
||||
ft_putchar(conv);
|
||||
ft_putendl("' conversion specifier");
|
||||
}
|
||||
|
||||
void ft_fmt_error_flag_flag(char flag1, char flag2)
|
||||
{
|
||||
ft_putstr("warning: flag '");
|
||||
ft_putchar(flag1);
|
||||
ft_putstr("' is ignored when flag '");
|
||||
ft_putchar(flag2);
|
||||
ft_putendl("' is present");
|
||||
}
|
||||
71
libftasm/src/ft_printf/lib_fmt_validate.c
Normal file
71
libftasm/src/ft_printf/lib_fmt_validate.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#include "libftprintf.h"
|
||||
|
||||
void ft_fmt_simplify(t_fmt *fmt)
|
||||
{
|
||||
char hashtag;
|
||||
|
||||
hashtag = '#';
|
||||
if (fmt->conversion == 'p')
|
||||
{
|
||||
fmt->conversion = 'x';
|
||||
if (!ft_strchr(fmt->flags, '#'))
|
||||
ft_strncat(fmt->flags, &hashtag, 1);
|
||||
}
|
||||
if (ft_strchr("DOUCS", fmt->conversion))
|
||||
{
|
||||
fmt->conversion += 32;
|
||||
ft_strcpy(fmt->modifier, "l");
|
||||
}
|
||||
}
|
||||
int ft_fmt_validate_conversion(t_fmt *fmt)
|
||||
{
|
||||
if (!ft_strchr(ALL_CONVERSIONS, fmt->conversion))
|
||||
{
|
||||
if (fmt->conversion != '%')
|
||||
ft_fmt_error_conv(fmt->conversion);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void ft_fmt_validate_flags(t_fmt *fmt)
|
||||
{
|
||||
char *flag_ptr;
|
||||
char flag;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (fmt->conversion != g_convs[i].id)
|
||||
i++;
|
||||
flag_ptr = fmt->flags;
|
||||
while (*flag_ptr)
|
||||
{
|
||||
flag = *flag_ptr;
|
||||
if (!ft_strchr(g_convs[i].allowed_flags, flag))
|
||||
{
|
||||
ft_fmt_error_flag_conv(flag, fmt->conversion);
|
||||
if (flag == '#')
|
||||
*flag_ptr = '.';
|
||||
}
|
||||
flag_ptr++;
|
||||
}
|
||||
if (ft_strchr(fmt->flags, '+') && (flag_ptr = ft_strchr(fmt->flags, ' ')))
|
||||
{
|
||||
ft_fmt_error_flag_flag(' ', '+');
|
||||
*flag_ptr = '.';
|
||||
}
|
||||
if (ft_strchr(fmt->flags, '-') && (flag_ptr = ft_strchr(fmt->flags, '0')))
|
||||
{
|
||||
ft_fmt_error_flag_flag('0', '-');
|
||||
*flag_ptr = '.';
|
||||
}
|
||||
if (fmt->precision && (flag_ptr = ft_strchr(fmt->flags, '0')))
|
||||
*flag_ptr = '.';
|
||||
}
|
||||
|
||||
void ft_fmt_validate_mod(t_fmt *fmt)
|
||||
{
|
||||
if (fmt->conversion == 's' || fmt->conversion == 'c')
|
||||
if (fmt->modifier[0] && ft_strcmp(fmt->modifier, "l"))
|
||||
ft_fmt_error_mod_conv(fmt->modifier, fmt->conversion);
|
||||
}
|
||||
39
libftasm/src/ft_printf/lib_pad.c
Normal file
39
libftasm/src/ft_printf/lib_pad.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include "libftprintf.h"
|
||||
|
||||
void ft_pad_right(char *str, t_fmt *fmt)
|
||||
{
|
||||
ft_pad_sharp(str, fmt);
|
||||
while ((int)ft_strlen(str) < fmt->width)
|
||||
ft_strcat(str, " ");
|
||||
}
|
||||
|
||||
void ft_pad_left(char *str, t_fmt *fmt)
|
||||
{
|
||||
char buf[100];
|
||||
char sign;
|
||||
|
||||
sign = '\0';
|
||||
if (str[0] == '-' || str[0] == '+' || str[0] == ' ')
|
||||
{
|
||||
sign = str[0];
|
||||
str++;
|
||||
}
|
||||
if (ft_strchr(fmt->flags, '0'))
|
||||
{
|
||||
while ((int)ft_strlen(str) < fmt->width - (sign ? 1 : 0))
|
||||
{
|
||||
ft_strcpy(buf, "0");
|
||||
ft_strcat(buf, str);
|
||||
ft_strcpy(str, buf);
|
||||
}
|
||||
}
|
||||
if (sign)
|
||||
str--;
|
||||
ft_pad_sharp(str, fmt);
|
||||
while ((int)ft_strlen(str) < fmt->width)
|
||||
{
|
||||
ft_strcpy(buf, " ");
|
||||
ft_strcat(buf, str);
|
||||
ft_strcpy(str, buf);
|
||||
}
|
||||
}
|
||||
42
libftasm/src/ft_printf/lib_pad_sharp.c
Normal file
42
libftasm/src/ft_printf/lib_pad_sharp.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#include "libftprintf.h"
|
||||
|
||||
void ft_pad_sharp_o(char *str)
|
||||
{
|
||||
char buf[100];
|
||||
|
||||
if (str[0] != '0')
|
||||
{
|
||||
ft_strcpy(buf, "0");
|
||||
ft_strcat(buf, str);
|
||||
ft_strcpy(str, buf);
|
||||
}
|
||||
}
|
||||
|
||||
void ft_pad_sharp_x(char *str, t_fmt *fmt)
|
||||
{
|
||||
char buf[100];
|
||||
|
||||
if (str[1] == '0')
|
||||
str[1] = fmt->conversion;
|
||||
else
|
||||
{
|
||||
ft_strcpy(buf, "0");
|
||||
if (str[0] == '0')
|
||||
str[0] = fmt->conversion;
|
||||
else
|
||||
ft_strcat(buf, &fmt->conversion);
|
||||
ft_strcat(buf, str);
|
||||
ft_strcpy(str, buf);
|
||||
}
|
||||
}
|
||||
|
||||
void ft_pad_sharp(char *str, t_fmt *fmt)
|
||||
{
|
||||
if (ft_strchr(fmt->flags, '#'))
|
||||
{
|
||||
if (fmt->conversion == 'x' || fmt->conversion == 'X')
|
||||
ft_pad_sharp_x(str, fmt);
|
||||
if (fmt->conversion == 'o')
|
||||
ft_pad_sharp_o(str);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue