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, size *file_size, size *capacity) {
    if (!filename || !data || !file_size || !capacity) {
    LOG_ERROR("invalid arguments.");
    
    Str code = StrInit();
    ReadCompleteFile(input_filename, &code.data, &code.length, &code.capacity);
    
    EnumEntries entries = VecInit();
    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,
    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

ReadFmt

ReadFmt Description Read formatted input from the standard input stream (stdin). This is a convenience macro calling FReadFmt with stdin.

Read More

StrReadFmt

StrReadFmt Description Parse input string according to format string with rust-style placeholders, extracting values into provided arguments. This is a macro wrapper around StrReadFmtInternal.

Read More

FReadFmt

FReadFmt Description Read formatted data from a file stream. This is a macro wrapper around FReadFmtInternal.

Read More