add FileReader class

This commit is contained in:
Josh Holtrop 2016-06-17 18:58:25 -04:00
parent e976d8df52
commit 7a1894bb53
2 changed files with 87 additions and 0 deletions

74
src/FileReader.cc Normal file
View File

@ -0,0 +1,74 @@
#include "FileReader.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef _WIN32
#define JES_O_BINARY O_BINARY
#else
#define JES_O_BINARY 0
#endif
/**
* Load a file into memory.
*
* @param[in] fname File name.
* @param[out] buf Pointer to memory buffer containing file. Must be freed by
* caller with delete[].
* @param[out] size Set to length of the file. If the return value is true
* and *size is 0, then no buffer was allocated and *buf should not be used.
*
* @retval false The file failed to load. In this case the output parameter
* values *buf and *size should not be used.
* @retval true The file was loaded successfully.
*/
bool FileReader::load(const char * fname, uint8_t ** buf, size_t * size)
{
struct stat st;
if (stat(fname, &st) != 0)
{
return false;
}
if (st.st_size < 0)
{
return false;
}
*size = st.st_size;
if (st.st_size == 0)
{
return true;
}
int fd = open(fname, O_RDONLY | JES_O_BINARY);
if (fd < 0)
{
return false;
}
*buf = new uint8_t[st.st_size];
off_t n_bytes_read = 0u;
for (;;)
{
off_t rd_size = read(fd, &(*buf)[n_bytes_read], st.st_size - n_bytes_read);
if (rd_size <= 0)
break;
n_bytes_read += rd_size;
if (n_bytes_read >= st.st_size)
break;
}
if (n_bytes_read != st.st_size)
{
delete[] *buf;
*buf = NULL;
close(fd);
return false;
}
close(fd);
return true;
}

13
src/FileReader.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef JES_FILEREADER_H
#define JES_FILEREADER_H
#include <stdint.h>
#include <stdlib.h>
class FileReader
{
public:
static bool load(const char * fname, uint8_t ** buf, size_t * size);
};
#endif