Sha256: 53dd841da1064ccb2b0f349a62d234a4892574737e0a21cf9220b670cd17238b

Contents?: true

Size: 1.09 KB

Versions: 22

Compression:

Stored size: 1.09 KB

Contents

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *file2strl(
    const char *path,
    unsigned int *file_len_out
)
{
    FILE *file;

    if (!(file = fopen(path, "rb")))
    {
        fprintf(stderr, "Unable to open file %s\n", path);
        return NULL;
    }

    if (-1 == fseek(file, 0, SEEK_END))
    {
        fprintf(stderr, "Unable to seek file %s\n", path);
        return NULL;
    }

    int file_len;
    if (-1 == (file_len = ftell(file)))
    {
        fprintf(stderr, "Unable to ftell() file %s\n", path);
        return NULL;
    }

    if (-1 == fseek(file, 0, SEEK_SET))
    {
        fprintf(stderr, "Unable to seek file %s\n", path);
        return NULL;
    }

    char *contents;
    if (!(contents = malloc(file_len + 1)))
    {
        fprintf(stderr, "Memory error!\n");
        fclose(file);
        return NULL;
    }

    fread(contents, file_len, 1, file);
    fclose(file);

    contents[file_len] = '\0';

    if (file_len_out)
        *file_len_out = file_len + 1;

    return contents;
}

char *file2str(
    const char *path
)
{
    return file2strl(path,NULL);
}

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
mathematical-1.6.20 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.18 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.14 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.13 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.12 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.11 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.10 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.9 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.8 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.7 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.6 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.5 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.4 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.3 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.2 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.1 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.6.0 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.5.12 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.5.0 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c
mathematical-1.4.2 ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c