-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibft.h
394 lines (333 loc) · 12.4 KB
/
libft.h
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariahi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/08 10:49:10 by ariahi #+# #+# */
/* Updated: 2023/01/17 14:10:23 by ariahi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <string.h>
# include <stdlib.h>
# include <stdio.h>
# include <unistd.h>
# include <fcntl.h>
/*
* A struct that represents a node in a linked list.
* A NULL pointer is considered as an empty linked list.
*/
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
typedef int (*t_cmpfn)(void *, void *);
typedef void (*t_delfn)(void *);
/* Counts the number of nodes in the linked list and returns it. */
int ft_lstsize(t_list *lst);
/*
* Creates a new `t_list` node, and returns a pointer to it
* that can be freed using `free`.
* The `next` member is set to NULL.
* Returns NULL on allocation failure.
*/
t_list *ft_lstnew(void *content);
/* Returns the last node of the linked list. */
t_list *ft_lstlast(t_list *lst);
/* function li tatsorti lik wa5a hia matatsori lk walo hhh*/
t_list *lst_put_orderly(t_list **lst, char *d_name);
/*
* Adds the node `new` at the beginning of the linked list `*lst`.
* If the linked list is empty, `*lst` is set to `new`.
*/
void ft_lstadd_front(t_list **lst, t_list *new);
/*
* Adds the node `new` at the end of the linked list `*lst`.
* If the linked list is empty, `*lst` is set to `new`.
*/
void ft_lstadd_back(t_list **lst, t_list *new);
/* Frees the node’s content using `del` then frees `lst` using `free()`. */
void ft_lstdelone(t_list *lst, t_delfn fn);
/*
* Frees the linked list's nodes and their contents using `ft_lstdelone()`
* and sets `*lst` to NULL.
*/
void ft_lstclear(t_list **lst, t_delfn fn);
/*
* Calls `f` for each node in the linked list.
* `f` receives a pointer to the content of the currently processed node.
*/
void ft_lstiter(t_list *lst, void (*f)(void*));
/* Removes the provided node from the list `*lst` but does not free it. */
void ft_lstremove(t_list **lst, t_list *to_rm);
/*
* Calculates the length of the string `s`,
* excluding the terminating NUL-byte.
*/
size_t ft_strlen(const char *s);
/*
* Copies up to `size - 1` characters
* from the NUL-terminated string `src` to `dst`,
* NUL-terminating the result.
* Returns the length of `src`.
*/
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize);
/*
* Returns the number of bytes in the initial segment of `s`
* which are not in the string `reject`.
* The NUL-byte is not considered in the matching process.
*/
size_t ft_strspn(const char *s, const char *charset);
/*
* Compares at most `n` bytes from the two strings `s1` and `s2`.
* It returns an integer less than, equal to, or greater than zero
* if `s1` is found, respectively, to be less than, to match,
* or be greater than s2.
* If `n` is zero, the return value is zero.
*/
size_t ft_strcspn(const char *s, const char *charset);
/*
* Appends the string `src` to the end of `dst`, NUL-terminating the result.
* Retruns the initial length of `dst` plus the length of `src`.
* If `size` characters are traversed without finding a NUL-byte,
* the length of `dst` is considered to be `size`,
* and the result will not be NUL-terminated.
*/
int ft_strlcat(char *dst, const char *src, int dstsize);
/*
* Returns a pointer to the first occurrence of the character `c`
* in the string `s` or NULL otherwise.
* The terminating NUL-byte is considered part of the string.
*/
char *ft_strchr(const char *s, int c);
/*
* Returns the number of bytes in the initial segment of `s`
* which consist only of bytes from `accept`.
* The NUL-byte is not considered in the matching process.
*/
char *ft_strrchr(const char *s, int c);
/*
* Finds the first occurrence of the string `needle` in the string `haystack`.
* Return a pointer to the beginning of the located substring,
* or NULL if the substring is not found.
*/
char *ft_strnstr(const char *haystack, const char *needle, size_t len);
/* Same as `ft_strlen()` except that it counts at most `n` characters. */
int ft_strnlen(const char *str, int n);
/*
* Compares bytes from the two strings `s1` and `s2`.
* It returns an integer less than, equal to, or greater than zero
* if `s1` is found, respectively, to be less than, to match,
* or be greater than s2.
*/
int ft_strcmp(char *s1, char *s2);
/*
* Compares at most `n` bytes from the two strings `s1` and `s2`.
* It returns an integer less than, equal to, or greater than zero
* if `s1` is found, respectively, to be less than, to match,
* or be greater than s2.
* If `n` is zero, the return value is zero.
*/
int ft_strncmp(const char *s1, const char *s2, size_t n);
/*
* Checks for an alphabetic character.
* It is equivalent to `(ft_isupper(c) || ft_islower(c))`.
*/
int ft_isalpha(int c);
/* Checks for a digit character (0 through 9).*/
int ft_isdigit(int c);
/*
* Checks for an alphanumeric character.
* It is equivalent to `(ft_isalpha(c) || ft_isdigit(c))`.
*/
int ft_isalnum(int c);
/*
* Checks whether `c` is a 7-bit unsigned char value
* that fits into the ASCII character set.
*/
int ft_isascii(int c);
/* Checks for any printable character including space. */
int ft_isprint(int c);
/* Returns the uppercase equivalent of `c` or `c` itself if there's none. */
int ft_toupper(int c);
/* Returns the lowercase equivalent of `c` or `c` itself if there's none. */
int ft_tolower(int c);
/*
* Converts the initial part of the string `str` to an int.
* The string may begin with an arbitrary amount of white space
* followed by a single optional '+' or '-' sign.
* The remainder of the string is converted to an int in the obvious manner,
* stopping at the first character which is not a valid digit.
* If an overflow occurs, INT_MAX is returned and errno is set to ERANGE.
* If an undeflow occurs, INT_MIN is returned and errno is set to ERANGE.
*/
int ft_atoi(const char *str);
/*
* Returns an integer less than, equal to, or greater than zero
* if the first `n` bytes of `s1` are found, respectively,
* to be less than, to match, or be greater than the first `n` bytes of `s2`.
* The memory areas are interpreted as unsigned char.
* If `n` is zero, the return value is zero.
*/
int ft_memcmp(const void *s1, const void *s2, size_t n);
/*
* Allocates memory for an array of `count` elements of `size` bytes each
* and returns a pointer to the allocated memory.
* The memory is set to zero.
* If `count` or `size` is 0,
* then ft_calloc() returns either NULL,
* or a unique pointer value that can later be successfully passed to free()
* Integer overflow is not handled.
*/
void *ft_memset(void *b, int c, size_t len);
/*
* Copies `n` bytes from memory area `src` to memory area `dst`.
* The memory areas must not overlap.
* Returns `dst`.
*/
void *ft_memcpy(void *dst, const void *src, size_t n);
/*
* Copies `n` bytes from memory area `src` to memory area `dst`.
* The memory areas may overlap.
* Returns `dst`.
*/
void *ft_memmove(void *dst, const void *src, size_t len);
/*
* Scans the initial `n` bytes of the memory area
* pointed to by `s` for the first instance of `c`.
* Both `c` and the bytes of the memory area pointed to by `s`
* are interpreted as unsigned char.
* Returns a pointer to the matching byte or NULL otherwise.
*/
void *ft_memchr(const void *s, int c, size_t n);
/*
* Copies `n` bytes from memory area `src` to memory area `dst`.
* The memory areas must not overlap.
* Returns `dst`.
*/
void ft_bzero(void *s, size_t n);
/*
* Erases the data in the `n` bytes of the memory
* starting at the location pointed to by `s`,
* by writing zeros to that area.
*/
void *ft_calloc(size_t count, size_t size);
void lst_put_orderly_sorted_2(t_list **lst, t_list *new);
void lst_put_orderly_sorted(t_list **lst, t_list *new, t_cmpfn cmpfn);
/*
* Returns a pointer to a new string which is a duplicate of the string `s`,
* or NULL on allocation failure.
*/
char *ft_strdup(const char *s1);
/*
* Returns a pointer to a new string which is a substring of `s`
* that starts at `start` and has a maximum length of `len`,
* or NULL on allocation failure.
* If `start` exceeds the length of `s`
* then start is considered to be ft_strlen(s).
*/
char *ft_substr(const char *s, unsigned int start, size_t len);
/*
* Returns a pointer to a new string that is the result of concatenating
* `s1` and `s2`, Or NULL on allocation failure.
*/
char *ft_strjoin(const char *s1, const char *s2);
/*
* Removes all leading and trailing occurrences
* of characters in the string `set` from the string `s`,
* and returns a pointer to a new string which contains the result,
* or NULL on allocation failure.
* The NUL-byte is not considered in the matching process.
*/
char *ft_strtrim(char const *s1, char const *set);
/*
* Splits `s` into an array of substrings.
* The substrings are delimited by at least one occurence
* of one of the characters in the `sep` set.
* Returns a pointer to the array of substrings, or NULL on allocation failure.
*/
char **ft_split(char const *s, char c);
/*
* Converts an int to its string representation.
* Returns NULL on allocation failure.
*/
char *ft_itoa(int n);
/*
* Returns a pointer to a new string populated with the results of calling `f`
* on every character in `s`, or NULL on allocation failure.
* `f` receives the index of the processed character
* and the character itself.
*/
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
/*
* Locates, in the string referenced by `*str`,
* the first occurrence of any character in the string `sep`
* (or the terminating '\0' character) and replaces it with a '\0'.
* The location of the next character after the delimiter character
* (or NULL, if the end of the string was reached) is stored in `*str`.
* The original value of `*str` is returned.
* If `*str` is initially NULL, NULL is returned.
*/
char *ft_strsep(char **str, const char *sep);
/*
* Calls `f` on each character in `s`.
* `f` receives the index of the processed character
* and a pointer to it.
*/
void ft_striteri(char *s, void (*f)(unsigned int, char *));
/*
* Outputs the character `c` to the file descriptor `fd`.
* Returns the number of bytes written or -1 on failure.
*/
void ft_putchar_fd(char c, int fd);
/*
* Outputs the string `s` to the file descriptor `fd`.
* Returns the number of bytes written or -1 on failure.
*/
void ft_putstr_fd(char *s, int fd);
/*
* Outputs the string `s` to the file descriptor `fd`, followed by a newline.
* Returns the number of bytes written or -1 on failure.
*/
void ft_putendl_fd(char *s, int fd);
/*
* Outputs the string representation of an int to the file descriptor `fd`.
* Returns the number of bytes written or -1 on failure.
*/
void ft_putnbr_fd(int n, int fd);
/*
* Converts the initial part of the string `str` to a long long.
* The string may begin with an arbitrary amount of white space
* followed by a single optional '+' or '-' sign.
* The remainder of the string is converted to a long long
* in the obvious manner,
* stopping at the first character which is not a valid digit.
* If an overflow occurs, LONG_LONG_MAX is returned and errno is set to ERANGE.
* If an underflow occurs, LONG_LONG_MIN is returned and errno is set to ERANGE.
*/
long long ft_atoll(const char *str);
/*It free 2D char table
*It take table to be free and return void
*/
void ft_free(char **s);
/*It calc the lenght of 2D char table
*It take table to be calc and return lenght of it
*/
int ft_2d_len(char **av);
/*
* Returns a line read from the file descriptor `fd`.
* Returns NULL if there are no more characters to read,
* or if an error occured.
*/
char *get_next_line(int fd);
/*
* This function mimics the real `printf` but is limited:
* - Supports these specifiers: 'cspdiuxX%'
* - Supports these flags: '-0#+ '
*/
int ft_printf(const char *str, ...);
#endif