add FileReader class
This commit is contained in:
parent
975c393b1d
commit
08979cef45
@ -1,5 +1,5 @@
|
||||
#ifndef JES_FILEREADER_H
|
||||
#define JES_FILEREADER_H
|
||||
#ifndef JES_FILELOADER_H
|
||||
#define JES_FILELOADER_H
|
||||
|
||||
#include "jes/Ref.h"
|
||||
#include "jes/Text.h"
|
||||
@ -40,7 +40,6 @@ namespace jes
|
||||
typedef std::vector<LineIndexPair> LineIndexPairVector;
|
||||
typedef Ref<LineIndexPairVector> LineIndexPairVectorRef;
|
||||
void load_buf(size_t size);
|
||||
int m_fd;
|
||||
uint8_t * m_buf;
|
||||
int m_line_endings;
|
||||
LineIndexPairVectorRef m_lines;
|
||||
|
16
src/lib/include/jes/FileReader.h
Normal file
16
src/lib/include/jes/FileReader.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef JES_FILEREADER_H
|
||||
#define JES_FILEREADER_H
|
||||
|
||||
#include "jes/Ref.h"
|
||||
#include <stdint.h>
|
||||
|
||||
namespace jes
|
||||
{
|
||||
class FileReader
|
||||
{
|
||||
public:
|
||||
static bool load(const char * fname, uint8_t ** buf, size_t * size);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -1,5 +1,6 @@
|
||||
#include "jes.h"
|
||||
#include "jes/FileLoader.h"
|
||||
#include "jes/FileReader.h"
|
||||
#include "jes/Text.h"
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
@ -11,7 +12,6 @@ namespace jes
|
||||
{
|
||||
FileLoader::FileLoader()
|
||||
{
|
||||
m_fd = 0;
|
||||
m_buf = NULL;
|
||||
m_line_endings = LINE_ENDING_COUNT;
|
||||
m_lines = NULL;
|
||||
@ -28,53 +28,15 @@ namespace jes
|
||||
|
||||
bool FileLoader::load(const char * fname)
|
||||
{
|
||||
struct stat st;
|
||||
if (stat(fname, &st) != 0)
|
||||
size_t size;
|
||||
|
||||
if (!FileReader::load(fname, &m_buf, &size))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (st.st_size < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
load_buf(size);
|
||||
|
||||
if (st.st_size == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
m_fd = open(fname, O_RDONLY | JES_O_BINARY);
|
||||
if (m_fd < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_buf = new uint8_t[st.st_size];
|
||||
|
||||
off_t n_bytes_read = 0u;
|
||||
for (;;)
|
||||
{
|
||||
off_t rd_size = read(m_fd, &m_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[] m_buf;
|
||||
m_buf = NULL;
|
||||
close(m_fd);
|
||||
m_fd = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
close(m_fd);
|
||||
m_fd = 0;
|
||||
|
||||
load_buf(st.st_size);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
59
src/lib/src/FileReader.cc
Normal file
59
src/lib/src/FileReader.cc
Normal file
@ -0,0 +1,59 @@
|
||||
#include "jes.h"
|
||||
#include "jes/FileReader.h"
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace jes
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user