mirror of
https://github.com/genodelabs/genode.git
synced 2025-06-19 15:43:56 +00:00
gems: move reusable code to include/gems
This patch makes various utilities that were originally developed for the backdrop application publicly available.
This commit is contained in:
55
repos/gems/src/lib/file/file.cc
Normal file
55
repos/gems/src/lib/file/file.cc
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* \brief Utility for loading a file
|
||||
* \author Norman Feske
|
||||
* \date 2014-08-14
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2014 Genode Labs GmbH
|
||||
*
|
||||
* This file is part of the Genode OS framework, which is distributed
|
||||
* under the terms of the GNU General Public License version 2.
|
||||
*/
|
||||
|
||||
/* Genode includes */
|
||||
#include <base/printf.h>
|
||||
|
||||
/* gems includes */
|
||||
#include <gems/file.h>
|
||||
|
||||
/* libc includes */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
static Genode::size_t file_size(char const *name)
|
||||
{
|
||||
struct stat s;
|
||||
s.st_size = 0;
|
||||
stat(name, &s);
|
||||
return s.st_size;
|
||||
}
|
||||
|
||||
|
||||
File::File(char const *name, Genode::Allocator &alloc)
|
||||
:
|
||||
_alloc(alloc),
|
||||
_file_size(file_size(name)),
|
||||
_data(alloc.alloc(_file_size))
|
||||
{
|
||||
int const fd = open(name, O_RDONLY);
|
||||
if (read(fd, _data, _file_size) < 0) {
|
||||
PERR("reading from file \"%s\" failed (error %d)", name, errno);
|
||||
throw Reading_failed();
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
|
||||
File::~File()
|
||||
{
|
||||
_alloc.free(_data, _file_size);
|
||||
}
|
Reference in New Issue
Block a user