Corewar documentation 1.0.0
Loading...
Searching...
No Matches
my.h
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2023
3** Libmy
4** File description:
5** The header file of the libmy
6*/
13#include <stdlib.h>
14#include <stddef.h>
15#include <stdarg.h>
16#include <unistd.h>
17#include <stdio.h>
18#include <stdint.h>
19#include <stdbool.h>
20#include <ctype.h>
21#include <math.h>
22#include <limits.h>
23#include <errno.h>
24#include <string.h>
25#include <fcntl.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <time.h>
30#include <dirent.h>
31#include <signal.h>
32#include <glob.h>
33
34#ifndef MY_H_
35 #define MY_H_
36
37// MACROS :
38
39 // Constants :
40
41 #define MAX_INT 2147483647
42 #define MIN_INT -2147483648
43 #define MAX_LONG 9223372036854775807
44 #define MIN_LONG -9223372036854775808
45 #define MAX_SHORT 32767
46 #define MIN_SHORT -32768
47 #define MAX_SHORT_SHORT 127
48 #define MIN_SHORT_SHORT -128
49 #define MAX_SIZE_T 18446744073709551615
50
51 // Basics macros :
52
53 #define ABS(value) ((value < 0) ? - value : value)
54 #define MAX(a, b) ((a > b) ? a : b)
55 #define MIN(a, b) ((a < b) ? a : b)
56 #define STR_CONTAINS(str, find) (my_strstr(str, find) != NULL) ? 1 : 0
57 #define NB number_settings_t
58
59 // str_to_word_array macros :
60
61 #define STR2ARRAY(str) my_str_to_word_array(str)
62 #define STR2ARRAY_SEP(str, sep) my_str_to_word_array_select(str, sep)
63 #define STR2ARRAY_STR(str, sep) my_str_to_word_array_string(str, sep)
64
65 // Free macros :
66
67 #define FREE(ptr) ptr = my_free_ptr(ptr)
68 #define FREE_WORD_ARRAY(ptr) my_free_array((void **)ptr)
69
70
71
72// TYPEDEFS :
73
74/* A reproduction of the boolean */
75typedef enum my_bool {
76 FALSE = 0, /* FALSE */
77 TRUE = 1 /* TRUE */
78} my_bool_t;
79
80extern int my_errno; /* The error number */
81
82/* The information of the my_params_to_array */
83struct info_param {
84 int length; /*The length of the string */
85 char *str; /* The string */
86 char *copy; /* The copy of the string */
87 char **word_array; /* The array of words */
88};
89
90/* The settings of the my_super_number */
91typedef struct number_settings_s {
92 my_bool_t multiple_signe; /* Allow multiples signes in the string */
93 my_bool_t letter_before; /* Allow letters before the number */
94 my_bool_t letter_after; /* Allow letters after the number */
95 my_bool_t overflow; /* Allow overflow in the number */
97
98 #define NB number_settings_t
99
100
101
102// FUNCTIONS PROTOTYPES :
103
104 // Write functions :
105
112void my_putchar(char c);
113
121void my_isneg(int nb);
122
129void my_putnbr(int nb);
130
138int my_putstr(char const *str);
139
147void my_showstr(char const *str);
148
156void my_showmem(char const *str, int size);
157
166void my_print_combn(int n);
167
175void my_putnbr_base(int nbr, char const *base);
176
183void my_show_word_array(char *const *tab);
184
192void my_print_params(int argc, char **argv);
193
200int my_putstr_error(char const *str);
201
210int my_putstr_sized(char const *str, int size);
211
220int my_putstr_fd(char const *str, int fd);
221
230int my_putstr_fd_free(char *str, int fd);
231
240int my_printf(char const *format, ...);
241
250int my_fprintf(int fd, char const *format, ...);
251
252
253
254 // Char functions :
255
262int my_char_is_alpha(char const c);
263
270int my_char_is_num(char const c);
271
278int my_char_is_printable(char const c);
279
287int my_char_is(char c, const char *char_list);
288
297char *my_add_chr(char *str, char chr);
298
305char my_get_last_char(char *str);
306
307
308
309 // String functions :
310
318int my_str_isalpha(char const *str);
319
326int my_str_isnum(char const *str);
327
334int my_str_islower(char const *str);
335
342int my_str_isupper(char const *str);
343
350int my_str_isprintable(char const *str);
351
360int my_str_is(char *str, const char *char_list);
361
371int my_str_contains(char *str, char *char_list);
372
379int my_strlen(char const *str);
380
390char *my_strcpy(char *dest, char const *src);
391
402char *my_strncpy(char *dest, char const *src, int n);
403
411char *my_revstr(char *str);
412
422char *my_strstr(char *str, char const *to_find);
423
432int my_strcmp(char const *s1, char const *s2);
433
443int my_strncmp(char const *s1, char const *s2, int n);
444
451char *my_strupcase(char *str);
452
459char *my_strlowcase(char *str);
460
468char *my_strcapitalize(char *str);
469
478char *my_strcat(char *dest, char const *src);
479
489char *my_strncat(char *dest, char const *src, int nb);
490
497char *my_strdup(const char *src);
498
507char *my_strndup(const char *src, int n);
508
515char **my_strdup_word_array(char **array);
516
523const char *my_strerror(int error);
524
532int my_count_letter(char const *str, char c);
533
542int my_get_char_index(char *str, char c, int occurrence);
543
551void my_replace_char(char *str, char to_replace, char replace_by);
552
553
554
555 // Int functions :
556
563int my_nbrlen(int nb);
564
572void my_swap(int *a, int *b);
573
581void my_sort_int_array(int *tab, int size);
582
592char *my_convert_base(char const *nbr,
593 char const *base_from, char const *base_to);
594
604char *my_convert_base_unsigned(char const *nbr,
605 char const *base_from, char const *base_to);
606
616char *my_convert_base_size_t(char const *nbr,
617 char const *base_from, char const *base_to);
618
627void my_round_float_str(char *float_nb, char last_char, int i, int enable);
628
629
630
631 // Array functions :
632
639int my_array_len(void **array);
640
647char **my_str_to_word_array(char const *str);
648
657char **my_str_to_word_array_select(char const *str, char const *separator);
658
667char **my_str_to_word_array_string(char const *str, char const *separator);
668
677char **my_super_array(char *str, char *sep);
678
685void my_free_array(void **array);
686
693void *my_free_ptr(void *ptr);
694
695
696
697 // Get number functions :
698
707int my_getnbr(char const *str);
708
717int my_find_nbr(char const *str);
718
728float my_getnbr_float(char const *str);
729
740int my_getnbr_base(char const *str, char const *base);
741
750int my_strict_getnbr(char const *str);
751
760float my_strict_getnbr_float(char const *str);
761
770int my_strict_find_nbr(char const *str);
771
782int my_super_number(char *number, number_settings_t settings);
783
784
785
786 // Calculs functions :
787
795int my_compute_power_rec(int nb, int power);
796
805size_t my_compute_power_rec_size_t(int nb, int p);
806
813int my_compute_square_root(int nb);
814
822int my_compute_factorial_rec(int nb);
823
830int my_is_prime(int nb);
831
839int my_find_prime_sup(int nb);
840
848int my_find_prime_inf(int nb);
849
850
851
852 // Params functions :
853
861struct info_param *my_params_to_array(int ac, char **av);
862
869void my_show_param_array(struct info_param const *par);
870
877void my_rev_params(int argc, char **argv);
878
886void my_sort_params(int argc, char **argv);
887
895char *my_concat_params(int argc, char **argv);
896
897
898
899 // Str nbr functions :
900
907char *my_str_nbr(int nb);
908
915char *my_str_nbr_short(short int nb);
916
923char *my_str_nbr_short_short(signed char nb);
924
933char *my_str_nbr_base_unsigned(unsigned int nbr, char const *base);
934
943char *my_str_nbr_base_unsigned_short(unsigned short nbr, char const *base);
944
953char *my_str_nbr_base_unsigned_short_short(unsigned char nbr,
954 char const *base);
955
964char *my_str_nbr_base_unsigned_size_t(size_t nbr, char const *base);
965
974char *my_str_nbr_base_unsigned_long(unsigned long nbr, char const *base);
975
984char *my_str_nbr_base_long_long_int(long long int nbr, char const *base);
985
992char *my_str_nbr_unsigned(unsigned int nb);
993
1000char *my_str_nbr_unsigned_long(unsigned long int nb);
1001
1008char *my_str_nbr_long_long(long long nb);
1009
1016char *my_str_nbr_size_t(size_t nb);
1017
1018#endif /* MY_H_ */
char * my_str_nbr_base_unsigned_long(unsigned long nbr, char const *base)
Returns the string conversion of an unsigned long number (nbr) in specific base (base)
Definition my_str_nbr_base_unsigned_long.c:30
char * my_str_nbr(int nb)
Returns the string conversion of a number (nb)
Definition my_str_nbr.c:31
void my_sort_params(int argc, char **argv)
Sorts the parameters (argc and argv) in ascending order.
Definition my_sort_params.c:52
void my_show_word_array(char *const *tab)
Prints all word in an array (tab)
Definition my_show_word_array.c:15
void my_showstr(char const *str)
Prints a string (str) but replaces non-printable characters with their hexadecimal number.
Definition my_showstr.c:22
int my_putstr_fd_free(char *str, int fd)
Writes a string (str) in a file descriptor (fd), free it and returns the length of the printed string...
Definition my_putstr_fd_free.c:16
int my_str_islower(char const *str)
Checks if a string (str) is lowercase.
Definition my_str_islower.c:16
void my_show_param_array(struct info_param const *par)
Prints the information of a parameter array (par)
Definition my_show_param_array.c:16
int my_compute_power_rec(int nb, int power)
Returns the power (p) of the number (nb)
Definition my_compute_power_rec.c:15
int my_strcmp(char const *s1, char const *s2)
Compares two strings (s1) and (s2)
Definition my_strcmp.c:16
char * my_strndup(const char *src, int n)
Duplicates a string (src) with a defined size (n) and returns the new string.
Definition my_strndup.c:15
void my_putnbr_base(int nbr, char const *base)
Prints a number (nb) in the requested base (base) in stdout.
Definition my_putnbr_base.c:28
int my_find_prime_inf(int nb)
Returns the previous prime number before a number (nb)
Definition my_find_prime_inf.c:15
char * my_strcpy(char *dest, char const *src)
Copies a string (src) and paste it on another string (dest)
Definition my_strcpy.c:15
void my_putnbr(int nb)
Prints a number (nb) in stdout.
Definition my_putnbr.c:28
int my_strict_getnbr(char const *str)
Returns an int number starting from a string number (str)
Definition my_strict_getnbr.c:16
int my_compute_square_root(int nb)
Returns the square root of a number (nb)
Definition my_compute_square_root.c:15
void my_print_params(int argc, char **argv)
Prints all the parameters of the program.
Definition my_print_params.c:15
void my_sort_int_array(int *tab, int size)
Sorts an array (array) with a defined size (size) of integers in ascending order.
Definition my_sort_int_array.c:25
void my_swap(int *a, int *b)
Swaps two integers (a) and (b)
Definition my_swap.c:15
void my_replace_char(char *str, char to_replace, char replace_by)
Replace a char in a string by another char.
Definition my_replace_char.c:15
int my_putstr_fd(char const *str, int fd)
Writes a string (str) in a file descriptor (fd) and returns the length of the printed string.
Definition my_putstr_fd.c:16
int my_is_prime(int nb)
Returns 1 if the number (nb) is prime and 0 otherwise.
Definition my_is_prime.c:15
char * my_str_nbr_unsigned_long(unsigned long int nb)
Returns the string conversion of an unsigned long number (nb)
Definition my_str_nbr_unsigned_long.c:30
int my_strlen(char const *str)
Returns the length of a string (str)
Definition my_strlen.c:15
int my_char_is(char c, const char *char_list)
Checks if a char (c) is in a list of chars (char_list)
Definition my_char_is.c:16
char * my_strncpy(char *dest, char const *src, int n)
Copies a string (src) and paste it on another string (dest) with a defined size (n)
Definition my_strncpy.c:16
char * my_str_nbr_short_short(signed char nb)
Returns the string conversion of a short short number (nb)
Definition my_str_nbr_short_short.c:31
char * my_strupcase(char *str)
Replaces lowcase by upcase of a string (str)
Definition my_strupcase.c:15
int my_find_nbr(char const *str)
Find the first number (number) found in a string.
Definition my_find_nbr.c:15
int my_strncmp(char const *s1, char const *s2, int n)
Compares two strings (s1) and (s2) with a defined size (n)
Definition my_strncmp.c:16
int my_str_is(char *str, const char *char_list)
Checks if a string (str) is composed of characters in the characters list (char_list) or 0 if not.
Definition my_str_is.c:16
int my_array_len(void **array)
Returns the length of an array (array)
Definition my_array_len.c:15
int my_str_isprintable(char const *str)
Checks if a string (str) is printable.
Definition my_str_isprintable.c:15
void my_print_combn(int n)
Prints all the numbers composed by (n) different digits numbers All digits in the number are differen...
Definition my_print_combn.c:56
float my_getnbr_float(char const *str)
Returns a float number starting from a string number (str)
Definition my_getnbr_float.c:24
char ** my_str_to_word_array_select(char const *str, char const *separator)
Returns an array of words delimited by a list of separator (separator) from a string (str)
Definition my_str_to_word_array_select.c:76
char ** my_strdup_word_array(char **array)
Duplicates a word array (array) and returns the new array.
Definition my_strdup_word_array.c:15
char * my_strcapitalize(char *str)
Capitalizes the first letter of each word in a string (str) and lowercase the other letters.
Definition my_strcapitalize.c:15
int my_str_isnum(char const *str)
Checks if a string (str) is a number.
Definition my_str_isnum.c:15
void my_rev_params(int argc, char **argv)
Prints all the arguments (argc and argv) in reverse order.
Definition my_rev_params.c:15
char * my_strstr(char *str, char const *to_find)
Search a string (to_find) on another (str) and returns the address of the first occurrence.
Definition my_strstr.c:16
int my_get_char_index(char *str, char c, int occurrence)
Get the index of a char in a str according to its occurrence.
Definition my_get_char_index.c:15
int my_count_letter(char const *str, char c)
Counts the number of times a letter (c) is in a string (str)
Definition my_count_letter.c:15
int my_str_contains(char *str, char *char_list)
Checks if a string (str) contains a character in a list of characters (char_list)
Definition my_str_contains.c:25
size_t my_compute_power_rec_size_t(int nb, int p)
Returns the power (p) of the number (nb)
Definition my_compute_power_rec_size_t.c:15
char * my_add_chr(char *str, char chr)
Adds a char (chr) at the end of a string (str)
Definition my_add_chr.c:15
int my_str_isupper(char const *str)
Checks if a string (str) is uppercase.
Definition my_str_isupper.c:16
char * my_str_nbr_base_long_long_int(long long int nbr, char const *base)
Returns the string conversion of an long long number (nbr) in specific base (base)
Definition my_str_nbr_base_long_long_int.c:30
char * my_convert_base_unsigned(char const *nbr, char const *base_from, char const *base_to)
Returns the result of the conversion of a unsigned number (nbr) in a specific base (base_from) to ano...
Definition my_convert_base_unsigned.c:84
char * my_str_nbr_base_unsigned_short_short(unsigned char nbr, char const *base)
Returns the string conversion of an unsigned short short number (nbr) in specific base (base)
Definition my_str_nbr_base_unsigned_short_short.c:30
char * my_strncat(char *dest, char const *src, int nb)
Concatenates two strings (dest) and (src) with a defined size (nb)
Definition my_strncat.c:16
char * my_strlowcase(char *str)
Replaces upcase by lowcase of a string (str)
Definition my_strlowcase.c:15
char * my_str_nbr_base_unsigned(unsigned int nbr, char const *base)
Returns the string conversion of an unsigned number (nbr) in specific base (base)
Definition my_str_nbr_base_unsigned.c:30
int my_getnbr(char const *str)
Returns an int number starting from a string number (str)
Definition my_getnbr.c:24
int my_compute_factorial_rec(int nb)
Returns the factorial of a number (nb)
Definition my_compute_factorial_rec.c:15
int my_char_is_alpha(char const c)
Checks if a char (c) is a letter.
Definition my_char_is_alpha.c:15
int my_printf(char const *format,...)
Prints a string (format) with possible flags and format in stdout and returns the length the printed ...
Definition my_printf.c:87
int my_putstr_error(char const *str)
Prints a string (str) in stderr.
Definition my_putstr_error.c:15
char * my_str_nbr_size_t(size_t nb)
Returns the string conversion of an size_t number (nb)
Definition my_str_nbr_size_t.c:29
void my_isneg(int nb)
Prints 'P' if the number (n) is positive or 'N' if the number (n) is negative.
Definition my_isneg.c:16
void * my_free_ptr(void *ptr)
Frees a pointer (ptr)
Definition my_free_ptr.c:15
char * my_str_nbr_short(short int nb)
Returns the string conversion of a short number (nb)
Definition my_str_nbr_short.c:31
struct info_param * my_params_to_array(int ac, char **av)
Returns a info_params struct of the argc (ac) and the argv (av)
Definition my_params_to_array.c:15
char * my_concat_params(int argc, char **argv)
Returns a string with all the arguments (argc and argv) concatenated.
Definition my_concat_params.c:15
char * my_strcat(char *dest, char const *src)
Concatenates two strings (dest) and (src)
Definition my_strcat.c:16
char * my_convert_base(char const *nbr, char const *base_from, char const *base_to)
Returns the result of the conversion of a number (nbr) in a specific base (base_from) to another base...
Definition my_convert_base.c:52
char ** my_super_array(char *str, char *sep)
Returns an array of words delimited by a list of separator (separator) from a string (str)
Definition my_super_array.c:74
int my_putstr_sized(char const *str, int size)
Prints a string (str) in stdout with a defined size (size) and returns the length of the printed stri...
Definition my_putstr_sized.c:16
void my_showmem(char const *str, int size)
Prints a memory dump of a string (str) with a defined size.
Definition my_showmem.c:81
void my_free_array(void **array)
Frees an array (array)
Definition my_free_array.c:15
char ** my_str_to_word_array_string(char const *str, char const *separator)
Returns an array of words delimited by a separator (separator) from a string (str)
Definition my_str_to_word_array_string.c:74
char * my_strdup(const char *src)
Duplicates a string (src) and returns the new string.
Definition my_strdup.c:15
char * my_str_nbr_unsigned(unsigned int nb)
Returns the string conversion of an unsigned number (nb)
Definition my_str_nbr_unsigned.c:29
float my_strict_getnbr_float(char const *str)
Returns a float number starting from a string number (str)
Definition my_strict_getnbr_float.c:16
const char * my_strerror(int error)
Returns the error message of the error number (error)
Definition my_strerror.c:149
char my_get_last_char(char *str)
Returns the last char of a string (str)
Definition my_get_last_char.c:15
void my_putchar(char c)
Prints a char (c) in the stdout.
Definition my_putchar.c:15
char ** my_str_to_word_array(char const *str)
Returns an array of words from a string (str)
Definition my_str_to_word_array.c:68
int my_char_is_num(char const c)
Checks if a char (c) is a number.
Definition my_char_is_num.c:15
int my_putstr(char const *str)
Writes a string (str) in stdout and returns the length of the printed string.
Definition my_putstr.c:16
char * my_convert_base_size_t(char const *nbr, char const *base_from, char const *base_to)
Returns the result of the conversion of a size_t number (nbr) in a specific base (base_from) to anoth...
Definition my_convert_base_size_t.c:84
char * my_str_nbr_base_unsigned_short(unsigned short nbr, char const *base)
Returns the string conversion of an unsigned short number (nbr) in specific base (base)
Definition my_str_nbr_base_unsigned_short.c:30
int my_char_is_printable(char const c)
Checks if a char (c) is printable.
Definition my_char_is_printable.c:15
int my_fprintf(int fd, char const *format,...)
Writes a string (format) with possible flags and format in a file descriptor (fd) and returns the len...
Definition my_fprintf.c:87
char * my_str_nbr_base_unsigned_size_t(size_t nbr, char const *base)
Returns the string conversion of an size_t number (nbr) in specific base (base)
Definition my_str_nbr_base_unsigned_size_t.c:30
char * my_str_nbr_long_long(long long nb)
Returns the string conversion of an long long number (nb)
int my_getnbr_base(char const *str, char const *base)
Returns a number starting from a string number (str) in the requested base (base)
Definition my_getnbr_base.c:45
int my_str_isalpha(char const *str)
Checks if a string (str) is alphabetical.
Definition my_str_isalpha.c:15
int my_super_number(char *number, number_settings_t settings)
Returns an int number starting from a string number (number) and change my_errno if an error occurs d...
Definition my_super_number.c:45
char * my_revstr(char *str)
Reverses the characters in a string (str) and returns the string (str)
Definition my_revstr.c:15
void my_round_float_str(char *float_nb, char last_char, int i, int enable)
Modify a string (float) and round it.
Definition my_round_float_str.c:29
int my_strict_find_nbr(char const *str)
Find the first number (number) found in a string.
Definition my_strict_find_nbr.c:16
int my_nbrlen(int nb)
Returns the length of a number (nb)
Definition my_nbrlen.c:15
int my_find_prime_sup(int nb)
Returns the next prime number after a number (nb)
Definition my_find_prime_sup.c:15
Definition my.h:83
Definition my.h:91