Sha256: afc7fcb63eccb2e6d1ac5e995068100597a8776b05f98925b377ba91dc7f3dbe

Contents?: true

Size: 1.32 KB

Versions: 19

Compression:

Stored size: 1.32 KB

Contents

#include <fcntl.h>

static pm_string_init_result_t
tebako_string_file_init(pm_string_t *string, const char *filepath) {

    // Open the file for reading
    int fd = open(filepath, O_RDONLY);
    if (fd == -1) {
        return PM_STRING_INIT_ERROR_GENERIC;
    }

    // Stat the file to get the file size
    struct stat sb;
    if (fstat(fd, &sb) == -1) {
        close(fd);
        return PM_STRING_INIT_ERROR_GENERIC;
    }

    // Ensure it is a file and not a directory
    if (S_ISDIR(sb.st_mode)) {
        close(fd);
        return PM_STRING_INIT_ERROR_DIRECTORY;
    }

    // Check the size to see if it's empty
    size_t size = (size_t) sb.st_size;
    if (size == 0) {
        close(fd);
        const uint8_t source[] = "";
        *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
        return PM_STRING_INIT_SUCCESS;
    }

    size_t length = (size_t) size;
    uint8_t *source = xmalloc(length);
    if (source == NULL) {
        close(fd);
        return PM_STRING_INIT_ERROR_GENERIC;
    }

    long bytes_read = (long) read(fd, source, length);
    close(fd);

    if (bytes_read == -1) {
        xfree(source);
        return PM_STRING_INIT_ERROR_GENERIC;
    }

    *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
    return PM_STRING_INIT_SUCCESS;
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
tebako-0.12.16 include/tebako/tebako-prism.h
tebako-0.12.15 include/tebako/tebako-prism.h
tebako-0.12.15.rc1 include/tebako/tebako-prism.h
tebako-0.12.14 include/tebako/tebako-prism.h
tebako-0.12.13 include/tebako/tebako-prism.h
tebako-0.12.12 include/tebako/tebako-prism.h
tebako-0.12.11 include/tebako/tebako-prism.h
tebako-0.12.10 include/tebako/tebako-prism.h
tebako-0.12.9 include/tebako/tebako-prism.h
tebako-0.12.8 include/tebako/tebako-prism.h
tebako-0.12.7 include/tebako/tebako-prism.h
tebako-0.12.6 include/tebako/tebako-prism.h
tebako-0.12.5 include/tebako/tebako-prism.h
tebako-0.12.4 include/tebako/tebako-prism.h
tebako-0.12.3 include/tebako/tebako-prism.h
tebako-0.12.2 include/tebako/tebako-prism.h
tebako-0.12.2.rc1 include/tebako/tebako-prism.h
tebako-0.12.1 include/tebako/tebako-prism.h
tebako-0.12.0 include/tebako/tebako-prism.h