-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
150 lines (136 loc) · 3.26 KB
/
get_next_line_utils.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anpayot <anpayot@student.42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 03:46:49 by anpayot #+# #+# */
/* Updated: 2024/12/30 16:05:50 by anpayot ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
/**
* @brief Calculate length of null-terminated string
*
* @param s String to measure
* @return size_t Number of characters before null terminator
*/
size_t ft_strlen(const char *s)
{
const char *ptr;
if (!s)
return (0);
ptr = s;
while (*ptr)
ptr++;
return (ptr - s);
}
/**
* @brief Locate first occurrence of character in string
*
* @param s String to search
* @param c Character to find (converted to unsigned char)
* @return char* Pointer to first occurrence, or NULL if not found
*/
char *ft_strchr(const char *s, int c)
{
if (!s)
return (NULL);
while (*s)
{
if (*s == (char)c)
return ((char *)s);
s++;
}
if ((char)c == '\0')
return ((char *)s);
return (NULL);
}
/**
* @brief Concatenate two strings into new allocation
*
* @param s1 First string (will be freed)
* @param s2 Second string
* @return char* New allocated string with s1+s2, or NULL if allocation fails
* @note Frees s1 before returning
*/
char *ft_strjoin(char *s1, const char *s2)
{
char *str;
char *ptr;
char *tmp;
if (!s2)
return (free_null(s1));
str = malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
if (!str)
return (free_null(s1));
ptr = str;
if (s1)
{
tmp = s1;
while (*tmp)
*ptr++ = *tmp++;
}
while (*s2)
*ptr++ = *s2++;
*ptr = '\0';
free(s1);
return (str);
}
/**
* @brief Free pointer and return NULL
*
* @param ptr Pointer to free
* @return char* Always returns NULL
* @note Safe to call with NULL pointer
*/
char *free_null(void *ptr)
{
if (ptr)
free(ptr);
return (NULL);
}
/*
#include <stdio.h>
#include <strings.h>
int main(void)
{
char *str1 = "Hello, ";
char *str2 = "World!";
char *joined_str;
char *found_char;
size_t len;
// Test ft_strlen
len = ft_strlen(str1);
printf("Length of '%s': %zu\n", str1, len);
// Test ft_strchr
found_char = ft_strchr(str1, 'e');
if (found_char)
printf("Char 'e' found in '%s' at pos: %ld\n", str1, found_char - str1);
else
printf("Character 'e' not found in '%s'\n", str1);
// Test ft_strjoin
joined_str = ft_strjoin(strdup(str1), str2);
if (joined_str)
{
printf("Joined string: '%s'\n", joined_str);
free(joined_str);
}
else
{
printf("Failed to join strings\n");
}
// Test free_null
char *ptr = malloc(10);
if (ptr)
{
ptr = free_null(ptr);
if (!ptr)
printf("Pointer successfully freed and set to NULL\n");
else
printf("Pointer not set to NULL\n");
}
return 0;
}
*/