Lexical Analyzer v0.1.0
 
Loading...
Searching...
No Matches

File chunk handling utilities for lexical analysis. More...

Classes

struct  fchnk_t
 A structure that represents a chunk of data from a file. More...
 

Functions

fchnk_tfchnk_ctor ()
 Constructs a new file chunk object.
 
void fchnk_dtor (fchnk_t *chnk)
 Destructor for the file chunk object.
 
fchnk_tfchnk_ptor (char *const buff, const size_t chksz)
 Initializes and returns a file chunk object with provided buffer and size.
 
bool fwrite_fchnk (const char *fname, const fchnk_t *chnk)
 Writes the contents of a file chunk to a specified file.
 
fchnk_tget_fchnk (const char *fname)
 Reads the contents of a binary file into a dynamically allocated buffer and stores it in a new fchnk_t structure.
 

Detailed Description

File chunk handling utilities for lexical analysis.

Provides structures and functions for:

Note
All functions maintain binary-safe operations
See also
Tokenization For token processing pipeline

Function Documentation

◆ fchnk_ctor()

fchnk_t * fchnk_ctor ( )

Constructs a new file chunk object.

This function dynamically allocates memory for a new fchnk_t structure, initializing it to hold a chunk of data from a file. The buff pointer in the newly allocated fchnk_t structure is not initialized (it may be NULL initially). The caller should ensure to allocate memory for buff and set chksz appropriately after the constructor is called.

Returns
A pointer to a newly allocated fchnk_t structure. If the memory allocation fails, it will return NULL.

◆ fchnk_dtor()

void fchnk_dtor ( fchnk_t * chnk)

Destructor for the file chunk object.

This function frees the memory allocated for the buff buffer and the fchnk_t structure itself. It is important to call this function when the file chunk object is no longer needed to avoid memory leaks.

Parameters
chnkPointer to the fchnk_t structure to be destroyed.
Note
After calling this function, the pointer chnk should not be used as it will be freed.

◆ fchnk_ptor()

fchnk_t * fchnk_ptor ( char *const buff,
const size_t chksz )

Initializes and returns a file chunk object with provided buffer and size.

This function allocates memory for a new fchnk_t structure and assigns the provided character buffer and its corresponding size to the structure.

Parameters
buffPointer to a character buffer containing the file chunk data.
chkszThe size of the buffer, representing the number of characters stored in it.
Returns
A pointer to the initialized fchnk_t structure, or NULL if allocation fails.
Note
The function does not copy the buffer contents, it only assigns the pointer. The caller is responsible for managing the memory for buff.
Warning
Transfers buffer ownership

◆ fwrite_fchnk()

bool fwrite_fchnk ( const char * fname,
const fchnk_t * chnk )

Writes the contents of a file chunk to a specified file.

Opens a file in write mode and writes the buffer stored in the given fchnk_t structure to that file. The write operation attempts to write exactly chksz characters.

Parameters
fnameThe name of the file to write to.
chnkPointer to the file chunk structure containing the data and its size.
Returns
true if the entire buffer was successfully written to the file, false otherwise.
Note
If the file cannot be opened or writing fails, appropriate errors are printed using perror().

◆ get_fchnk()

fchnk_t * get_fchnk ( const char * fname)

Reads the contents of a binary file into a dynamically allocated buffer and stores it in a new fchnk_t structure.

This function opens the specified file in binary read mode, reads its entire content into a dynamically allocated buffer, and initializes a new fchnk_t object with the buffer and its size. The caller is responsible for managing and freeing the memory of the returned object.

Parameters
fnameThe path to the file to be read.
Returns
A pointer to an fchnk_t structure containing the file data and size, or NULL if an error occurs (e.g., file open failure, memory allocation failure).
Return values
NULLon read failure
Note
The returned buffer is null-terminated, but the file is read in binary mode, so the null terminator is not counted in chksz.
Ensure fchnk_ctor() properly initializes the fchnk_t object.
See also
fchnk_ctor()