-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibft.h
More file actions
584 lines (523 loc) · 16.1 KB
/
libft.h
File metadata and controls
584 lines (523 loc) · 16.1 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eduaserr < eduaserr@student.42malaga.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/12 14:40:57 by eduaserr #+# #+# */
/* Updated: 2025/07/09 17:25:08 by eduaserr ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <unistd.h>
# include <stdlib.h>
// INCLUDE FOR FT_PRINTF
# include <stdarg.h>
// INCLUDE FOR GET_NEXT_LINE
# include <fcntl.h>
// struct in ft_flood_fill.c
typedef struct s_data
{
int width;
int length;
int wall;
int floor;
int coin;
int exit;
int player;
} t_data;
// struct in bonus functions
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
/**
* @brief Function tests for any character for which isalpha(3) or
isdigit(3) is true.
*
* @param a
* @return (int) Returns zero if the character tests false and returns
non-zero if tests true.
*/
int ft_isalnum(int a);
/**
* @brief Tests for any character for which isspace is true.
*
* @param a
* @return (int) Returns zero if the character tests false and returns
non-zero if tests true.
*/
int ft_isspace(int a);
/**
* @brief Remove all double spaces in str. Doesn't add aditionals spaces
* at the start, neither at the end.
*
* @param str
* @return (char *) Realloc the same str. Received ass char **.
*/
char *ft_rm_db_spaces(char **str);
/**
* @brief Tests for any character for which isupper or islower is true.
*
* @param a
* @return (int) Returns zero if the character tests false and
returns non-zero if tests true.
*/
int ft_isalpha(int a);
/**
* @brief Function tests for an ASCII character, which is any character
between 0 and octal 0177 inclusive.
*
* @param a
* @return (int)
*/
int ft_isascii(int a);
/**
* @brief Tests for a decimal digit character.
*
* @param a
* @return (int) Return zero if the character tests false and
return non-zero if tests true.
*/
int ft_isdigit(int a);
/**
* @brief Tests for any printing character printable.
*
* @param a
* @return (int) Returns zero if the character tests false and
returns non-zero if tests true.
*/
int ft_isprint(int a);
/**
* @brief Lower case to upper case letter conversion.
* @param c
* @return (int) Returns the corresponding upper-case letter if there is one;
otherwise, is returned unchanged.
*/
int ft_toupper(int c);
/**
* @brief Upper case to lower case letter conversion.
*
* @param c
* @return (int) Returns the corresponding lower-case letter if there is one;
otherwise, is returned unchanged.
*/
int ft_tolower(int c);
/**
* @brief Writes 'len' bytes of value 'c' (converted to an unsigned char) to
the string 'b'.
*
* @param b
* @param c
* @param len
* @return (void*) Returns its first argument.
*/
void *ft_memset(void *b, int c, size_t len);
/**
* @brief Writes 'n' zeroed bytes to string 's'. If 'n' is zero, does nothing.
Does not return a value.
*
* @param s
* @param n
*/
void ft_bzero(void *s, size_t n);
/**
* @brief The calloc() function contigously allocates enough space for count
objects that are size bytes of memory each and returns a pointer to the
allocated memory. Filled with bytes of value zero.
*
* @param count
* @param size
* @return (void*)Returns a pointer to allocated memory. If there is an error,
they return a NULL pointer and set errno to ENOMEM.
*/
void *ft_calloc(size_t count, size_t size);
/**
* @brief Locates the first occurence of 'c' (convered to an unsigned char)
in string 's'. Returns a pointer to the byte located, or NULL
if no such byte exists within n bytes.
*
* @param s
* @param c
* @param n
*/
void *ft_memchr(const void *s, int c, size_t n);
/**
* @brief Copies 'n' bytes from memory area 'src' to memory area 'dst'. If dst
and src overlap, behavior is undefined.
*
* @param dst
* @param src
* @param n
* @return (void*) Returns the original value of 'dst'. If both param overlap,
behavior is undefined. Should use memove(3) instead.
*/
void *ft_memcpy(void *dst, const void *src, size_t n);
/**
* @brief Copies 'len' bytes from string 'src' to string 'dst'.
The two strings may overlap; always done in a non-destructive manner.
*
* @param dst
* @param src
* @param len
* @return (void*) Returns the original value of 'dst'.
*/
void *ft_memmove(void *dst, const void *src, size_t len);
/**
* @brief Compares byte string 's1' against byte string 's2'.
Both strings are assumed to be 'n' bytes long.
*
* @param s1
* @param s2
* @param n
* @return (int) Returns zero if the two strings are identical, otherwise
returns the difference between the first two differing bytes
(treated as unsigned char values).
*/
int ft_memcmp(const void *s1, const void *s2, size_t n);
/**
* @brief Locates the first occurence of 'c' in the string pointed to by 's'.
Null character terminated is considered to be part of the string;
therefor if 'c' is '\0', the function locate the terminating '\0'.
*
* @param s
* @param c
* @return (int) Return int to the located character, or -1 if the
character does not appear in the string.
*/
int ft_istrchr(char *s, int c);
/**
* @brief Locates the last occurence of 'c' in the string pointed to by 's'.
Null character terminated is considered to be part of the string;
therefor if 'c' is '\0', the function locate the terminating '\0'.
*
* @param s
* @param c
* @return (int) Return int to the located character, or -1 if the
character does not appear in the string.
*/
int ft_istrrchr(char *s, int c);
/**
* @brief Locates the first occurence of 'c' (converted to a char) in the string
pointed to by 's'. Null character terminated is considered to be part of the
string; therefor if 'c' is '\0', the function locate the terminating '\0'.
*
* @param s
* @param c
* @return (char*) Return a pointer to the located character, or NULL if the
character does not appear in the string.
*/
char *ft_strchr(const char *s, int c);
/**
* @brief Locates the last occurence of 'c' in the 's'.
*
* @param s
* @param c
* @return (char*) Returns a pointer to the located character, or NULL
if the character does not appear in the string.
*/
char *ft_strrchr(const char *s, int c);
int ft_strcmp(const char *s1, const char *s2);
/**
* @brief Lexicographically compare the null-terminated strings 's1' and 's2'.
Not more than 'n' characters. Designed for comparing strings rather than
binary data, characters that appear after a '\0' character are not compared.
*
* @param s1
* @param s2
* @param n
* @return (int) Returns an integer greater than, equal to, or less than 0.
Is done using unsigned characters.
*/
int ft_strncmp(const char *s1, const char *s2, size_t n);
/**
* @brief Locates the first occurence of the null-terminated string 'needle' in
string 'haystack', where not more than 'len' characters are searched.
Characters after '\0' are not searched.
*
* @param haystack
* @param needle
* @param len
* @return (char*) Needle is an empty string, 'haystack' is returned; if 'needle'
occurs nowhere in 'haystack', NULL is returned; otherwise a pointer to the
first character of the first occurence of 'needle' is returned.
*/
char *ft_strnstr(const char *haystack, const char *needle, size_t len);
/**
* @brief Allocates sufficient memory for a copy of the string 's1', does the
copy, and returns a pointer to it. The pointer may subsequently be used as
an argument to free(3).
*
* @param s1
* @return (char*) Returns a pointer. If memory is unavailable, (NULL)
is returned and errno is set to ENOMEM.
*/
char *ft_strdup(const char *s1);
/**
* @brief Returns a substring in the string 's'. The substring starts from index
'start' and has a maximum length 'len'.
*
* @param s
* @param start
* @param len
* @return (char*) Returns the substring. NULL if malloc fails.
*/
char *ft_substr(char const *s, ssize_t start, ssize_t len);
/**
* @brief return the new string, formed by the concatenation of 's1' and 's2'.
*
* @param s1
* @param s2
* @return (char*) Returns the new string. NULL if malloc fails.
*/
char *ft_strjoin(char const *s1, char const *s2);
/**
* @brief Remove all characters from the string 'set' at the beginning and at
the end of 's1', until find a character not belonging to 'set'.
*
* @param s1
* @param set
* @return (char*) Return trimmed string. NULL if malloc fails.
*/
char *ft_strtrim(char const *s1, char const *set);
/**
* @brief For each 's' string`s character. Function 'f' giving as parameters the
index of each character inside 's' and the character. genders a new string
with the result of successive use of 'f'.
*
* @param s
* @param f
* @return (char*) The new string after correctly use of 'f' on each character.
NULL if malloc fails.
*/
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
/**
* @brief Apply the function 'f' to each characters of the string 's', passing
its index as a first parameter. Each character is transmitted by address to
'f' so it can be modified if necessary.
*
* @param s
* @param f
*/
void ft_striteri(char *s, void (*f)(unsigned int, char*));
/**
* @brief Converts the initial portion of the string pointed to by 'str' to
int representation.
*
* @param str
* @return (int)
*/
int ft_atoi(const char *str);
/**
* @brief Concatenate strings. Function take the full size of the
destination buffer and guarantee NUL-termination if there is room.
Appends string 'src' to the end of 'dst'. It will append at most
'dstsize' - strlen(dst) - 1 characters. It will then NUL-terminate, unless
'dstsize' is 0 or the original dst string was longer than 'dstsize'.
If the'src' and 'dst' strings overlap, the behavior is undefined.
*
* @param dest
* @param src
* @param dstsize
* @return (size_t) Return the total length of the string it tried to create.
Means initial length of 'dst' plus the length of 'src'. If the return value
is >= dstsize, the output string has been truncated.
*/
size_t ft_strlcat(char *dest, const char *src, size_t dstsize);
/**
* @brief Copy strings, and guarantee NUL-termination.
*
* @param dest
* @param src
* @return (char *) Return the string it created.
*/
char *ft_strcpy(char *dest, char *src);
/**
* @brief Copy strings. Function take the full size of the
destination buffer and guarantee NUL-termination if there is room.
Copies up to 'dstsize' - 1 characters from the string 'src' to 'dst',
NUL-terminating the result if 'dstsize' is not 0.
*
* @param dest
* @param src
* @param dstsize
* @return (size_t) Return the total length of the string it tried to create.
(length of src).
*/
size_t ft_strlcpy(char *dest, const char *src, size_t dstsize);
/**
* @brief Computes the length of the string. Function attempts to compute the
length of 's', but never scans beyond the first maxlen bytes of 's'.
*
* @param s
* @return (size_t) Returns the number of characters that precede the
terminating NULL character.
*/
ssize_t ft_strlen(const char *s);
/**
* @brief Using malloc, generates a string that represents the value received
* as argument. Negative numbers must be taken into account.
* @param n
* @return Returns the string that equals to such number or NULL if malloc fails.
* @warning This function uses malloc.
*/
char *ft_itoa(int n);
/**
* @brief Returns an array of strings obtained by splitting 's' with the
character 'c', used as delimiter.
*
* @param s
* @param c
* @return (char**) Returns an array of strings resulting from the splitting of
's'. NULL if malloc fails.
*/
char **ft_split(char const *s, char c);
/**
* @brief Sends character 'c' to the file descriptor fd specified.
* @param c
* @param fd
* @return
* @warning This function uses write.
*/
void ft_putchar_fd(char c, int fd);
/**
* @brief Sends the string 's' to the file descriptor fd specified.
* @param s
* @param fd
* @return
* @warning This function uses write.
*/
void ft_putstr_fd(char *s, int fd);
/**
* @brief Sends the string 's' to the file descriptor fd specified followed
by a newline.
* @param s
* @param fd
* @return
* @warning This function uses write.
*/
void ft_putendl_fd(char *s, int fd);
/**
* @brief Sends the int 'n' to the file descriptor fd specified.
* @param n
* @param fd
* @return
* @warning This function uses write.
*/
void ft_putnbr_fd(int n, int fd);
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *new);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **lst, t_list *new);
void ft_lstdelone(t_list *lst, void (*del)(void *));
void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
/**
* @brief recursive function for search an specific char in a 2D array matrix.
* @param data structure
* @param map 2D array matrix
* @param search char to find
* @param y int
* @param x int
* @return true (1) if the char is in the matrix. False (0) if not.
* @warning This function is recursive, that means it will look around his
* first appearance. then it continoues where is true and follow this path.
*/
//int ft_flood_fill(t_data *data, char **map, int y, int x, char search);
/**
* @brief print array.
* @param str
*/
void ft_printlines(char *str);
/**
* @brief prints every array in a 2D matrix.
* @param file_matrix the file matrix.
*/
void ft_printmatrix(char **file_matrix);
/**
* @brief duplicate with malloc any 2D array.
* @param arr
* @return
* @warning must allocate type * (size + 1) into parenthesis.
*/
char **ft_arrdup(char **arr);
/**
* @brief frees any 2D array matrix allocated.
* @param matrix 2D matrix array.
*/
void ft_freematrix(char ***matrix);
/**
* @brief Extracts a complete word (without spaces) from a specified
* position (i) in the string to the next space or end of string.
* Useful for parsing commands word by word.
* @param str full str containing text
* @param i index where the word start.
* @return (word) new malloc str separated by spaces.
*/
char *get_word(char *str, int i);
/* FT_PRINTF FOLDER */
# define INT_BASE "0123456789"
# define HEX_UPP_BASE "0123456789ABCDEF"
# define HEX_LOW_BASE "0123456789abcdef"
/**
* @brief Prints character and increments count.
*
* @param c
* @param count count bytes printed by printf function.
*/
void ft_putchar_pf(char c, int *count);
/**
* @brief Prints numbers in decimal base. It puts too numbers in
* hexadecimal base.
*
* @param nbr
* @param count count bytes printed by printf function.
*/
void ft_putnbr_pf(long long nbr, char *base, int *count);
/**
* @brief Prints a string.
*
* @param str
* @param count count bytes printed by printf function.
*/
void ft_putstr_pf(char *str, int *count);
/**
* @brief Prints a pointer in hexadecimal base, adding "0x" before.
*
* @param nbr
* @param count count bytes printed by printf function.
*/
void ft_putptr_pf(unsigned long long nbr, int *count);
/**
* @brief Chooses what type of variable is going to print.
*
* @param lst
* @param str
* @param count count bytes printed by printf function.
* @return (int)
*/
int ft_fillformat_pf(va_list lst, char str, int *count);
/**
* @brief Recreates printf function.
*
* @param str
* @param ...
* @return (int)
*/
int ft_printf(char const *str, ...);
/* GET_NEXT_LINE FOLDER */
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif
char *ft_free_str(char **str);
char *ft_substr_gnl(char *str, ssize_t start, ssize_t len);
char *ft_strjoin_gnl(char *s1, char *s2);
char *get_next_line(int fd);
/* MINISHELL FOLDER */
char *get_in_quotes(char *str, int start, int end);
#endif