Skip to content

SamyBravy/my_lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MyLib: Modular C System Library

Language Build Type

Overview

MyLib is a comprehensive static library written in C. It consolidates essential system-level programming tools into a modular architecture, providing optimized implementations of standard utilities, formatted output, and buffered I/O.

This library was engineered to serve as a standalone foundation for C projects, replacing the standard libc in environments where external dependencies are restricted. It offers granular control over memory allocation, string manipulation, and file descriptor handling.

Features & Modules

The library is organized into three distinct, high-performance modules:

1. Formatted Output (ft_printf)

A complete re-implementation of the standard printf function, handling variadic arguments and buffer management without external libraries.

  • Prototype: int ft_printf(const char *format, ...);
  • Variadic Parsing: Utilizes stdarg.h to process variable argument lists.
  • Type Support:
    • %c (Char), %s (String), %p (Pointer address)
    • %d, %i (Signed Decimal)
    • %u (Unsigned Decimal)
    • %x, %X (Hexadecimal Lower/Upper)
  • Advanced Flag Management:
    • Precision/Width: . (Precision), [number] (Minimum width).
    • Padding/Alignment: - (Left align), 0 (Zero padding).
    • Prefixes: # (Hex prefix), + (Force sign), space (Space for positive).

2. Buffered I/O (get_next_line)

An advanced file reading utility designed to read content line-by-line from file descriptors, solving the problem of reading files of unknown size without high-level abstractions.

  • Prototype: char *get_next_line(int fd);
  • Multi-FD Support: Capable of managing multiple file descriptors simultaneously (e.g., reading from a network socket and a log file concurrently) without losing thread context.
  • Static Persistence: Utilizes static variables to maintain the read buffer state between function calls.
  • Memory Efficiency: Dynamic buffer allocation prevents stack overflow on large reads.

3. Standard Libc Implementation and custom utilities (Libft)

A collection of over 40 functions re-implementing ctype.h, string.h, and stdlib.h behaviors for granular control over memory.
This component also includes several custom utility functions, extending the core API beyond the standard libc equivalents.

  • Memory Manipulation: memset, memcpy, memmove (overlap safe), calloc (zero-initialized), bzero.
  • String Operations: strlen, strlcpy, strlcat, strnstr, strdup, strjoin, split.
  • Type Conversion: atoi, itoa.
  • Data Structures: A generic Linked List implementation (t_list) for managing dynamic collections of data (lstnew, lstadd, lstmap, lstiter).

Directory Structure

The project maintains a clean separation of concerns for modular integration:

my_lib/
├── Makefile
├── Libft/             # Standard utilities, Memory & Lists
│   ├── libft.h
│   └── ...
├── ft_printf/         # Formatted output logic
│   ├── ft_printf.h
│   └── ...
└── get_next_line/     # Buffered Reader (Bonus version)
    ├── get_next_line_bonus.h
    └── ...

Integration & Usage

1. Compilation

Run make at the root of the repository to compile all modules into a single static archive (my_lib.a or similar).

make

2. Including Headers

To use the library in your project, include the specific module headers relative to the library root to ensure namespace clarity:

/* Core Utilities */
#include "my_lib/Libft/libft.h"

/* Formatted Output */
#include "my_lib/ft_printf/ft_printf.h"

/* Buffered I/O Reader */
#include "my_lib/get_next_line/get_next_line_bonus.h"

3. Linking

Compile your project by linking the static library.

gcc main.c my_lib/my_lib.a -o my_program

Performance & Design

  • No Memory Leaks: Every allocation is tracked. The library is tested with Valgrind to ensure complete memory safety.
  • Modularity: Functions are separated into individual files to minimize binary size during linking.
  • Error Handling: Robust protection against NULL pointers and invalid file descriptors.

About

A C static library featuring a custom `printf` implementation, a multi-fd buffered line reader (`get_next_line`), and optimized libc memory/string utilities (`Libft`)

Topics

Resources

Stars

Watchers

Forks

Contributors