gem-graph-client/include/base.h

79 lines
1.8 KiB
C
Raw Normal View History

/*
* Gem-graph OpenGL experiments
*
* Desc: Base header
*
* Copyright (C) 2023 Arthur Menges <arthur.menges@a-lec.org>
* Copyright (C) 2023 Adrien Bourmault <neox@a-lec.org>
*
* This file is part of Gem-graph.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
enum
{
X_AXIS,
Y_AXIS,
Z_AXIS,
N_AXIS
};
/*
* Read a file from filename into a provided buffer
*
* @param filename, file name
* contents, target ptr
*
* @return void
*/
static inline char *read_file(char *filename)
{
2023-02-23 21:54:22 +01:00
int fd;
int filesize;
char *contents;
2023-02-23 21:54:22 +01:00
fd = open(filename, O_RDONLY);
if(fd < 0) {
printf("Couldn't read file: %s\n",filename);
return NULL;
}
filesize = lseek(fd, 0, SEEK_END) + 1 ;
contents = malloc(filesize * sizeof(char));
2023-02-23 21:54:22 +01:00
if (contents == NULL) {
perror("Not enough memory to allocate file");
return NULL;
}
lseek(fd, 0, SEEK_SET);
read(fd,contents,filesize);
contents[filesize-1] = '\0';
close(fd);
return contents;
}