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_t * | fchnk_ctor () |
| Constructs a new file chunk object. | |
| void | fchnk_dtor (fchnk_t *chnk) |
| Destructor for the file chunk object. | |
| fchnk_t * | fchnk_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_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. | |
File chunk handling utilities for lexical analysis.
Provides structures and functions for:
| 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.
fchnk_t structure. If the memory allocation fails, it will return NULL. | 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.
| chnk | Pointer to the fchnk_t structure to be destroyed. |
chnk should not be used as it will be freed. | 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.
| buff | Pointer to a character buffer containing the file chunk data. |
| chksz | The size of the buffer, representing the number of characters stored in it. |
fchnk_t structure, or NULL if allocation fails.buff. | 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.
| fname | The name of the file to write to. |
| chnk | Pointer to the file chunk structure containing the data and its size. |
true if the entire buffer was successfully written to the file, false otherwise.perror(). | 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.
| fname | The path to the file to be read. |
fchnk_t structure containing the file data and size, or NULL if an error occurs (e.g., file open failure, memory allocation failure). | NULL | on read failure |
chksz. fchnk_ctor() properly initializes the fchnk_t object.