#include "common.hpp"
#include <sys/stat.h>
#include <fcntl.h>


#ifndef SIZE_MAX
    #define SIZE_MAX ((size_t)-1)
#endif


config_t config = {
    { "ao_len",            0,  0.0 },
    { "ao_lin",            0,  0.0 },
    { "ao_os",             0,  0.5 },
    { "ao_fac",            0,  0.5 },
    { "alight_samples",    3,  3.0 },
    { "supersample",       2,  2.0 }
};


char* file_read(const char* fn, size_t& size) {
    int fd = open(fn, O_RDONLY);
    if (fd == -1) {
        LOG_ERRNO("open(%s)", fn);
        return NULL;
    }
    struct stat ss;
    if (fstat(fd, &ss) == -1) {
        LOG_ERRNO("fstat(%s)", fn);
        close(fd);
        return NULL;
    }
    if (!ss.st_size || (size_t)ss.st_size > SIZE_MAX) {
        close(fd);
        return NULL;
    }
    size = (size_t)ss.st_size;
    char* buf = (char*)malloc(size + 1);
    if (read(fd, buf, size) != (ssize_t)size) {
        free(buf);
        close(fd);
        return NULL;
    }
    buf[size] = '\0';
    close(fd);
    return buf;
}