Ejemplo n.º 1
0
void *file_utils::read_file_to_heap(const char *pPath, size_t &data_size)
{
    data_size = 0;

    FILE *pFile = vogl_fopen(pPath, "rb");
    if (!pFile)
        return NULL;

    vogl_fseek(pFile, 0, SEEK_END);
    uint64_t file_size = vogl_ftell(pFile);
    vogl_fseek(pFile, 0, SEEK_SET);

    if (file_size > VOGL_MAX_POSSIBLE_HEAP_BLOCK_SIZE)
    {
        vogl_fclose(pFile);
        return NULL;
    }

    data_size = static_cast<size_t>(file_size);
    VOGL_ASSERT(data_size == file_size);

    void *p = vogl_malloc(data_size);
    if (!p)
    {
        vogl_fclose(pFile);
        data_size = 0;
        return NULL;
    }

    bool success = (vogl_fread(p, 1, data_size, pFile) == data_size);

    vogl_fclose(pFile);

    if (!success)
    {
        vogl_free(p);
        data_size = 0;
        return NULL;
    }

    return p;
}
Ejemplo n.º 2
0
    bool data_stream::write_file_data(const char *pFilename)
    {
        uint64_t file_size;
        if (!file_utils::get_file_size(pFilename, file_size))
            return false;

        if (!file_size)
            return true;

        FILE *pFile = vogl_fopen(pFilename, "rb");
        if (!pFile)
            return false;

        uint8_vec buf(64 * 1024);

        uint64_t bytes_remaining = file_size;
        while (bytes_remaining)
        {
            uint n = static_cast<uint>(math::minimum<uint64_t>(buf.size(), bytes_remaining));

            if (vogl_fread(buf.get_ptr(), 1, n, pFile) != n)
            {
                vogl_fclose(pFile);
                return false;
            }

            if (write(buf.get_ptr(), n) != n)
            {
                vogl_fclose(pFile);
                return false;
            }

            bytes_remaining -= n;
        }

        vogl_fclose(pFile);
        return true;
    }