Introduction de src/util/io.c où est déplacée: 'char *read_file(char *filename)' < OK

This commit is contained in:
Jean Sirmai 2024-08-24 15:01:32 +02:00
parent 07bafc912a
commit 961e470c2c
Signed by: jean
GPG Key ID: FB3115C340E057E3
5 changed files with 90 additions and 41 deletions

View File

@ -76,43 +76,3 @@ struct arrow_t { uint load; uint site; uint x; uint y; uint z; };
#define SOUTH 4 // + z blue
#define NORTH 5 // - z yellow
/******************************************************************************/
/* U T I L I T I E S */
/******************************************************************************/
/*
* char *read_file(char *filename)
* reads a file from filename into a provided buffer
*
* @param filename, file name
* contents, target ptr
*
* @return void
*/
static inline char *read_file(char *filename)
{
int fd;
int filesize;
char *contents;
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 = g_malloc(filesize * sizeof(char));
assert (contents);
lseek(fd, 0, SEEK_SET);
read(fd,contents,filesize);
contents[filesize-1] = '\0';
close(fd);
return contents;
}

View File

@ -78,3 +78,5 @@ void View(pile *);
//------------------------------------------------------------------------------
void util_pile_test(); // def: scr/util/tests
char *read_file(char *filename);

View File

@ -28,6 +28,8 @@
#include "../../include/graphics.h"
#include "../../include/parse.h"
#include "../../include/util.h"
#define TEST 0

85
src/util/io.c Normal file
View File

@ -0,0 +1,85 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Gem-graph client *
* *
* Base header *
* *
* Copyright © 2021 Libre en Communs <contact@a-lec.org> *
* Copyright © 2021 Adrien Bourmault <neox@a-lec.org> *
* Copyright © 2021 Jean Sirmai <jean@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/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h> // Defines NULL.
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
//#include <glib-2.0/glib.h>
//#include <gtk-4.0/gtk/gtk.h>
#include "../../include/util.h"
/*
* char *read_file(char *filename)
* reads a file from filename into a provided buffer
*
* @param filename, file name
* contents, target ptr
*
* @return void
*/
char *read_file(char *filename)
{
int fd;
int filesize;
char *contents;
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 = g_malloc(filesize * sizeof(char));
assert (contents);
lseek(fd, 0, SEEK_SET);
read(fd,contents,filesize);
contents[filesize-1] = '\0';
close(fd);
return contents;
}

View File

@ -28,7 +28,7 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "../../../include/base.h"
#include "../../../include/util.h"
#include "../../../include/signal.h"
#include "../../../include/widget.h"