ReadCompleteFile

Table of Contents

ReadCompleteFile

Description

Read complete contents of file at once. Pointer returned is malloc’d and hence must be freed after use. The returned pointer can also be reused by providing pointer to it in data parameter. realloc is called on *data in order to expand it’s size. If *capacity exceeds the size of file to be loaded, then no reallocation is performed. This means the provided buffer will automatically be expanded if required. The returned buffer is null-terminated just-in-case. The implementation and API is designed in such a way that it can be used with containers like Vec and Str.

Parameters

NameDirectionDescription
filenameinName/path of file to be read.
datain,outMemory buffer where loaded file will be stored.
file_sizeoutComplete size of file in bytes will be stored here.
capacityin,outHints towards current capacity of data buffer. New capacity of data buffer is automatically stored here if realloc is performed.

Success

true

Failure

false

Usage example (Cross-references)

    #include <Misra/Types.h>
    
    bool ReadCompleteFile(const char *filename, char **data, u64 *file_size, u64 *capacity) {
    if (!filename || !data || !file_size || !capacity) {
    LOG_ERROR("invalid arguments.");
    Str config = StrInit();
    Scope(&config, StrDeinit, {
    if (!ReadCompleteFile(config_path, &config.data, &config.length, &config.capacity)) {
    LOG_FATAL("Failed to read config file.");
    }
    Str file_contents = StrInit();
    Scope(&file_contents, StrDeinit, {
    if (!ReadCompleteFile(file_path.data, &file_contents.data, &file_contents.length, &file_contents.capacity)) {
    LOG_ERROR("Failed to read \"{}\" source file.", file_path.data);
    continue;
    
    Str code = StrInit();
    ReadCompleteFile(input_filename, &code.data, &code.length, &code.capacity);
    
    EnumEntries entries = VecInit();
    int main(void) {
    Str file = StrInit();
    if (ReadCompleteFile("Bin/Demangler/CppNameManglingGrammar", &file.data, &file.length, &file.capacity)) {
    Strs lines = StrSplit(&file, "\n");

Share :

Related Posts

MAX2

MAX2 Description Returns the larger of two values x and y.

Read More

IN_RANGE

IN_RANGE Description Checks if the value x is within the inclusive range [lo, hi].

Read More

MIN2

MIN2 Description Returns the smaller of two values x and y.

Read More