Skip to content

Commit

Permalink
Fix lib
Browse files Browse the repository at this point in the history
  • Loading branch information
IchiiDev committed Nov 7, 2022
1 parent 2daa93a commit f13487d
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*.out
*.o
project
compile_flags.txt
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ NAME = project

LDFLAGS = -L./lib/ -lmy

CPPFLAGS = -I./include/
CPPFLAGS = -I./includes/

all: $(NAME)

Expand Down
58 changes: 58 additions & 0 deletions includes/internal/flags.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
** EPITECH PROJECT, 2022
** my_printf
** File description:
** flags
*/

#ifndef FLAGS_H_
#define FLAGS_H_

/**
* @brief Parsed conversion specifier.
*/
typedef struct s_flags {
int flags;
int converter;
int width;
int precision;
} t_flags;

#define F_ALT (1 << 0) // #
#define F_ZEROS (1 << 1) // 0
#define F_BlANKS (1 << 2) // -
#define F_POS (1 << 3) // ' '
#define F_SIGN (1 << 4) // +

#define L_CHAR (1 << 5) // hh
#define L_SHORT (1 << 6) // h
#define L_LONGLONG (1 << 7) // ll
#define L_LONG (1 << 8) // l
#define L_LONG_DOUBLE (1 << 9) // L
#define L_INTMAX (1 << 10) // j
#define L_SIZE (1 << 11) // z
#define L_PTRDIFF (1 << 12) // t

#define C_INT (1 << 0) // d
#define C_INT_ALT (1 << 1) // i
#define C_OCTAL (1 << 2) // o
#define C_UINT (1 << 3) // u
#define C_HEX (1 << 4) // x
#define C_HEX_UPPER (1 << 5) // X
#define C_D_EXP (1 << 6) // e
#define C_D_EXP_UPPER (1 << 7) // E
#define C_D_DEC (1 << 8) // f
#define C_D_DEC_UPPER (1 << 9) // F
#define C_D_AUTO (1 << 10) // g
#define C_D_AUTO_UPPER (1 << 11) // G
#define C_D_HEX (1 << 12) // a
#define C_D_HEX_UPPER (1 << 13) // A
#define C_BIN (1 << 14) // b
#define C_CHAR (1 << 15) // c
#define C_STR (1 << 16) // s
#define C_STR_OCT (1 << 17) // S
#define C_PTR (1 << 18) // p
#define C_COUNT (1 << 19) // n
#define C_PERCENT (1 << 20) // %

#endif /* !FLAGS_H_ */
62 changes: 62 additions & 0 deletions includes/internal/int.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
** EPITECH PROJECT, 2022
** my_printf
** File description:
** int
*/

#ifndef INT_H_
#define INT_H_

#include <stdint.h>
#include "my_string.h"
#include "internal/flags.h"

typedef enum sign_kind_e {
SIGN_DEFAULT,
SIGN_SPACE,
SIGN_ALWAYS,
} sign_kind;

typedef enum alt_kind_s {
ALT_NONE,
ALT_OCT,
ALT_HEX,
ALT_HEX_UPPER,
ALT_BIN,
} alt_kind;

/**
* @brief Format used to print a number.
*
* @param base Base to format the string in (2 <= base <= 16)
* @param uppercase `1` if letters should be uppercase.
* @param sign Sign display format.
*/
typedef struct int_fmt_s {
int base;
int uppercase;
sign_kind sign;
alt_kind alt;
} int_fmt;

/**
* @brief Push a number formatted with a specific base into a string.
*/
void push_base(
intmax_t value, int_fmt fmt, t_flags flags, l_string *string);

/**
* @brief Push an unsigned number formatted with a specific base into a
* string.
*/
void push_ubase(
uintmax_t value, int_fmt fmt, t_flags flags, l_string *string);

int get_base(t_flags flags);
int get_uppercase(t_flags flags);
sign_kind get_sign_kind(t_flags flags);
alt_kind get_alt_kind(t_flags flags);
int is_signed(t_flags flags);

#endif /* !INT_H_ */
69 changes: 69 additions & 0 deletions includes/internal/parse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
** EPITECH PROJECT, 2022
** my_printf
** File description:
** Header file for private functions used for the my_printf implementation.
*/

#ifndef UTIL_H_
#define UTIL_H_

#include <stdarg.h>
#include <stdint.h>

#include "../internal/flags.h"

#define IS_NBR(c) (c >= '0' && c <= '9') ? 1 : 0
#define IS_PRINTABLE(c) (c >= 32 && c < 127) ? 1 : 0

/**
* @brief Parse a single flag.
*
* @param str Pointer to the start of the flag (after `%`).
* @return t_flags Parsed flag (or NULL if error)
*/
t_flags parse_flag(char **str, va_list *list);

/**
* @brief This function will return the first number in a string
*
* @return int The parsed number returned by the function
*/
int parse_numbers(char const * str);

/**
* @brief Checks if the `source` string starts with the given `str`.
*
* @param source String to search in.
* @param str String to search for.
* @return int boolean
*/
int p_starts_with(const char *source, const char *str);

/**
* @brief Checks if the `source` string starts with one of the given
* `elems`.
*
* @param source String to search in.
* @param elems List of strings to search for.
* @param size Size of `elems`.
* @return int Index of matched element, `-1` if no match.
*/
int p_contains(char *source, char **elems, int size);

/**
* @brief Get the length of a string.
*
* @param str The string.
* @return int Length of the string.
*/
int p_str_len(const char *str);

/** @brief Parse the string as a number.
*
* @param str The string to parse.
* @return int The parsed number (0 if invalid).
*/
int p_get_nbr(char const *str);

#endif /* !UTIL_H_ */
73 changes: 73 additions & 0 deletions includes/internal/print.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
** EPITECH PROJECT, 2022
** my_printf
** File description:
** print
*/

#ifndef PRINT_H_
#define PRINT_H_

#include <stdarg.h>
#include <stdint.h>

#include "my_string.h"
#include "internal/flags.h"

/**
* @brief Print a flag at the end of the given string.
*
* @param flag Flag to print.
* @param string String to push the flag to.
* @param list Variadic arguments list.
*/
void print_flag(t_flags flag, l_string *string, va_list *list);

/* Generic print functions */

typedef struct print_s {
int flag;
void (*ptr_fn)(t_flags, l_string *, va_list *);
} print_t;

void print_int(t_flags flag, l_string *string, va_list *list);

void print_double_exp(t_flags flag, l_string *string, va_list *list);

void print_double_dec(t_flags flag, l_string *string, va_list *list);

void print_double_auto(t_flags flag, l_string *string, va_list *list);

void print_double_hex(t_flags flag, l_string *string, va_list *list);

void print_char(t_flags flag, l_string *string, va_list *list);

void print_str(t_flags flag, l_string *string, va_list *list);

void print_str_oct(t_flags flag, l_string *string, va_list *list);

void print_ptr(t_flags flag, l_string *string, va_list *list);

void print_count(t_flags flag, l_string *string, va_list *list);

void print_percent(t_flags flag, l_string *string, va_list *list);

/**
* @brief Get a signed number from vargs according to the flags.
*
* @param flags Flags to use.
* @param list Varargs list.
* @return intmax_t The number.
*/
intmax_t length_mod_signed(t_flags flags, va_list *list);

/**
* @brief Get an unsigned number from vargs according to the flags.
*
* @param flags Flags to use.
* @param list Varargs list.
* @return uintmax_t The number.
*/
uintmax_t length_mod_unsigned(t_flags flags, va_list *list);

#endif /* !PRINT_H_ */
24 changes: 24 additions & 0 deletions includes/internal/str.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
** EPITECH PROJECT, 2022
** my_printf
** File description:
** str
*/

#ifndef STR_H_
#define STR_H_

#include "my_string.h"

/**
* @brief Add padding to the given string if its length is less than
* `size`.
*
* @param string String to add the padding to.
* @param size Target size of the string (after padding).
* @param pad_char Character to use for the padding.
* @param right `1` if the string is right-padded (left by default).
*/
void add_padding(l_string *string, int size, char pad_char, int right);

#endif /* !STR_H_ */
8 changes: 5 additions & 3 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ SRC_MY = ./my/my_compute_power_rec.c \
./my/my_getnbr.c

SRC_PRINTF = ./printf/src/my_printf.c \
./printf/src/int/get_flags.c \
./printf/src/int/get_flag.c \
./printf/src/int/print.c \
./printf/src/int/push.c \
./printf/src/parse/flags.c \
Expand All @@ -54,19 +54,21 @@ SRC_STRING = ./string/alloc.c \

HEADERS = ./includes/my.h \
./includes/my_printf.h \
./includes/string.h
./includes/my_string.h

OBJ_MY = $(SRC_MY:.c=.o)

OBJ_PRINTF = $(SRC_PRINTF:.c=.o)

OBJ_STRING = $(SRC_STRING:.c=.o)

CPPFLAGS = -I./includes

NAME = libmy.a

all: $(NAME)

$(NAME): $(OBJ)
$(NAME): $(OBJ_MY) $(OBJ_PRINTF) $(OBJ_STRING)
ar rc $(NAME) $(OBJ_MY) $(OBJ_PRINTF) $(OBJ_STRING)
mkdir -p ../includes
cp $(HEADERS) ../includes
Expand Down
2 changes: 1 addition & 1 deletion lib/printf/src/int/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <stdarg.h>
#include <stdint.h>

#include "lib/string.h"
#include "my_string.h"

#include "internal/flags.h"
#include "internal/print.h"
Expand Down
2 changes: 1 addition & 1 deletion lib/printf/src/int/push.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <stdint.h>

#include "internal/str.h"
#include "lib/string.h"
#include "my_string.h"
#include "internal/int.h"
#include "internal/flags.h"

Expand Down
2 changes: 1 addition & 1 deletion lib/printf/src/my_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <stdarg.h>
#include <unistd.h>

#include "lib/string.h"
#include "my_string.h"

#include "internal/flags.h"
#include "internal/parse.h"
Expand Down
4 changes: 2 additions & 2 deletions lib/printf/src/print/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#include <stdarg.h>

#include "lib/string.h"
#include "internal/print.h"
#include "my_string.h"
#include "internal/flags.h"
#include "internal/print.h"

void print_noop(t_flags flag, l_string *string, va_list *list);

Expand Down
2 changes: 1 addition & 1 deletion lib/printf/src/str/padding.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
** padding
*/

#include "lib/string.h"
#include "my_string.h"

#include "internal/str.h"

Expand Down
2 changes: 1 addition & 1 deletion lib/printf/src/str/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <stdlib.h>
#include <stdint.h>

#include "lib/string.h"
#include "my_string.h"

#include "internal/flags.h"
#include "internal/int.h"
Expand Down

0 comments on commit f13487d

Please sign in to comment.