{"id":26383038,"url":"https://github.com/pmarkaide/42_get_next_line","last_synced_at":"2025-03-17T06:19:54.826Z","repository":{"id":212110569,"uuid":"730214399","full_name":"pmarkaide/42_get_next_line","owner":"pmarkaide","description":"Implementation of the get_next_line function, returning the next line from a file in each call","archived":false,"fork":false,"pushed_at":"2024-01-11T10:29:21.000Z","size":20,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-01-11T13:56:43.968Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pmarkaide.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-12-11T12:54:04.000Z","updated_at":"2024-01-11T07:38:58.000Z","dependencies_parsed_at":"2024-01-11T11:57:36.703Z","dependency_job_id":"e90e9abf-0f6c-4f34-bab0-d865eba62864","html_url":"https://github.com/pmarkaide/42_get_next_line","commit_stats":null,"previous_names":["pmarkaide/42_get_next_line"],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmarkaide%2F42_get_next_line","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmarkaide%2F42_get_next_line/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmarkaide%2F42_get_next_line/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmarkaide%2F42_get_next_line/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pmarkaide","download_url":"https://codeload.github.com/pmarkaide/42_get_next_line/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243982256,"owners_count":20378607,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-17T06:19:54.225Z","updated_at":"2025-03-17T06:19:54.818Z","avatar_url":"https://github.com/pmarkaide.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n# Custom get_next_line Implementation\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/pmarkaide/meta/raw/master/get_next_line.png\" alt=\"get_next_line Logo\" width=\"600\"/\u003e\n\u003c/p\u003e\n\nThe `get_next_line` function reads a line from a file descriptor, handling dynamic memory allocation and providing a convenient way to process input line by line. The implementation focuses on low-level C programming concepts, ensuring readability and maintainability while avoiding the use of most built-in functions.\n\nIn developing this project, I learned about how to use **static variables** and improved memory handling using `malloc`. Defining the string as `static` at runtime ensures that we can make multiple calls to the `get_next_line` function, and we don't lose the information about where on the file we are positioned. Additionally, we have to dynamically update the memory as we append new characters to the previously existing string.\n\n## Functions\n* `get_next_line`: Main entry point orchestrating line reading, dynamic memory management, and processing.\n* `load_buffer`: Reads and accumulates data from a file descriptor, dynamically allocating memory.\n* `parse_line`: Extracts a line from the remainder, dynamically allocating memory for the line.\n* `update_remainder`: Updates the remainder after parsing a line, excluding processed data.\n\nHelper Functions:\n* `ft_free`: Properly deallocates memory.\n* `ft_strlenc`: Finds the length of a string until a specified character.\n* `ft_strchr`: Finds the first occurrence of a character in a string.\n* `ft_memmove`: Moves memory from one location to another.\n\n## Implementation\nWhen `get_next_line` is called, it initiates the line-reading process by calling the `load_buffer` function. This function reads data from the specified file descriptor (`fd`) into a buffer, dynamically allocating memory as needed. The reading process continues until a newline character is encountered or the end of the file is reached. Subsequently, the `parse_line` function is invoked to extract a line from the accumulated data in the buffer. This involves determining the length of the line and dynamically allocating memory for the line itself. After processing the line, the `update_remainder` function is called to update the remainder of the data in the buffer, excluding the already processed line. This process ensures that subsequent calls to `get_next_line` resume reading from the point after the last newline character. The extracted line is returned to the caller, providing a convenient mechanism for reading lines from a file descriptor in a memory-efficient manner while managing dynamic memory allocation and updates to the data buffer.\n\n## How to Use\nInclude the `get_next_line.h` header file in your project. Call the get_next_line function with the desired file descriptor to read lines.\n\n```c\n#include \"get_next_line.h\"\n\nint main() {\n    int fd = open(\"example.txt\", O_RDONLY);\n    char *line;\n\n    while ((line = get_next_line(fd)) != NULL) {\n        // Process the line as needed\n\t\t// For example, printing it\n        printf(\"Line: %s\\n\", line);\n        free(line);\n    }\n\n    close(fd);\n    return 0;\n}\n```\n\n## Contributing\nFeel free to open an issue if you see any error. Your feedback and improvements are highly appreciated.\n\n## License\nThis project is licensed under the [MIT License](https://raw.githubusercontent.com/pmarkaide/42_get_next_line/master/LICENSE)..","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpmarkaide%2F42_get_next_line","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpmarkaide%2F42_get_next_line","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpmarkaide%2F42_get_next_line/lists"}