Get-Next-Line

This function is now part of Libft

Get-Next-Line

The Get Next Line project aims to create a function in C that reads and returns a line from a given file descriptor. This function is designed to handle various edge cases and should work efficiently even with varying buffer sizes. This project not only provides a useful function but also introduces the concept of static variables in C programming.

Features


File Handling

The fcntl.h and unistd.h libraries in C provides advanced file control operations.

open()

read()

write()

close()


Usage Example:

void	display(char *filename)
{
	int		fd;
	char	character;

	fd = open(filename, O_RDONLY);
	if (fd < 0)
		return (-1);
	while (read(fd, &character, 1))
		write(1, &character, 1);
	close(fd);
}


Static Variables

A static variable in C is a variable that retains its value between multiple function calls. It is initialized only once and exists for the lifetime of the program, but its scope is limited to the block in which it is defined.

void count_calls() {
    static int count = 0; // Static variable
    count++;
    printf("Function called %d times\n", count);
}

int main() {
    for (int i = 0; i < 5; i++) {
        count_calls();
    }
    return 0;
}


Implementation

Get_next_line_utils.c

ft_isnewline

The ft_isnewline function checks if there is a newline character (\n) in the given buffer and returns its position if found. If the buffer is NULL or no newline character is present, it returns -1.

int	ft_isnewline(char *buffer)
{
	int	i;

	if (!buffer)
		return (-1);
	i = 0;
	while (buffer[i])
	{
		if (buffer[i] == '\n')
			return (i);
		i++;
	}
	return (-1);
}

Parameters:

Functionality:


ft_strlen

The ft_strlen function calculates the length of a given string by counting the number of characters until the null terminator (\0). If the string is NULL, it returns 0.

int	ft_strlen(char *str)
{
	int	i;

	i = 0;
	if (!str)
		return (0);
	while (str[i])
		i++;
	return (i);
}

Parameters:

Functionality:


ft_strncat

The ft_strncat function concatenates a specified number of characters from the buffer string to the end of the saved string. It dynamically allocates memory for the concatenated result and updates the saved pointer to point to the new string.

int	ft_strncat(char **saved, char *buffer, int size)
{
	int		i;
	int		j;
	char	*str;

	i = 0;
	if (!(*saved))
		str = malloc((size + 1) * sizeof(char));
	else
		str = malloc(((ft_strlen(*saved) + size) + 1) * sizeof(char));
	if (!str)
		return (0);
	while (*saved && (*saved)[i])
	{
		str[i] = (*saved)[i];
		i++;
	}
	j = 0;
	while (j < size)
		str[i++] = buffer[j++];
	str[i] = '\0';
	if (*saved)
		free(*saved);
	*saved = NULL;
	*saved = str;
	return (1);
}

Parameters:

Functionality:


ft_strtrim_jump

The ft_strtrim_jump function extracts a substring from the beginning of saved up to and including the first newline character (\n). It then updates saved to contain only the remaining part of the original string after the newline character. The extracted substring is returned, while the original string is modified.

char	*ft_strtrim_jump(char **saved)
{
	char	*strcut;
	char	*strsaved;
	int		i;
	int		j;

	i = -1;
	strcut = malloc((ft_isnewline(*saved) + 2) * sizeof(char));
	if (!strcut)
		return (NULL);
	while ((*saved)[++i] != '\n')
		strcut[i] = (*saved)[i];
	strcut[i++] = '\n';
	strcut[i] = '\0';
	strsaved = NULL;
	if ((*saved)[i])
	{
		j = 0;
		strsaved = malloc((ft_strlen(&(*saved)[i]) + 1) * sizeof(char));
		while ((*saved)[i])
			strsaved[j++] = (*saved)[i++];
		strsaved[j] = '\0';
	}
	free(*saved);
	*saved = strsaved;
	return (strcut);
}

Parameters:

Functionality:


Get_next_line.c

get_last_line

The get_last_line function extracts the entire string stored in saved, returning it as a new string. After extracting the string, it frees the memory associated with saved and sets saved to NULL.

char	*get_last_line(char **saved)
{
	char	*res;
	int		i;

	if (!(*saved) || !(*saved)[0])
		return (NULL);
	res = malloc(sizeof(char) * (ft_strlen(*saved) + 1));
	if (!res)
		return (NULL);
	i = 0;
	while ((*saved)[i] != '\0')
	{
		res[i] = (*saved)[i];
		i++;
	}
	res[i] = '\0';
	free(*saved);
	*saved = NULL;
	return (res);
}

Parameters:

Functionality:


ft_readfd

The ft_readfd function reads data from a file descriptor fd into the saved string until a newline character (\n) is found or the end of the file is reached. The function returns a string up to the newline character or the remaining content of the file if no newline is found.

char	*ft_readfd(char **saved, int fd)
{
	int		readed;
	char	*buffer;

	while (ft_isnewline((*saved)) == -1)
	{
		buffer = malloc(BUFFER_SIZE * sizeof(char));
		if (!buffer)
			return (NULL);
		readed = read(fd, buffer, BUFFER_SIZE);
		if (readed > 0)
			readed = ft_strncat(saved, buffer, readed);
		free(buffer);
		if (readed == 0)
			return (get_last_line(saved));
		else if (readed == -1)
		{
			if ((*saved))
			{
				free((*saved));
				(*saved) = NULL;
			}
			return (NULL);
		}
	}
	return (ft_strtrim_jump(saved));
}

Parameters:

Functionality:


get_next_line

The get_next_line function reads and returns the next line from a file descriptor fd, including the newline character (\n) if present. It handles multiple file descriptors simultaneously by using a static array to keep track of the saved state for each file descriptor.

char	*get_next_line(int fd)
{
	static char	*saved[1024];

	if (fd < 0 || fd >= 1024 || BUFFER_SIZE <= 0)
		return (NULL);
	if (saved[fd] && ft_isnewline(saved[fd]) > -1)
		return (ft_strtrim_jump(&saved[fd]));
	else
		return (ft_readfd(&saved[fd], fd));
	return (NULL);
}

Parameters:

Functionality:

Theme  Moonwalk