|
commit 01ea77ed88269930688f41449168103dbbbc1985
Author: Albrecht Schlosser <albrechts.fltk@online.de>
AuthorDate: Fri Oct 1 16:51:36 2021 +0200
Commit: Albrecht Schlosser <albrechts.fltk@online.de>
CommitDate: Fri Oct 1 19:03:02 2021 +0200
Refactor class Fl_Image_Reader to FLTK coding style
... and separate constructors for memory reader with and w/o length
argument for better documentation.
src/Fl_Image_Reader.cxx | 104 +++++++++++++++++++++++++++---------------------
src/Fl_Image_Reader.h | 51 +++++++++++-------------
2 files changed, 83 insertions(+), 72 deletions(-)
diff --git src/Fl_Image_Reader.cxx src/Fl_Image_Reader.cxx
index b9a2425..e15d343 100644
--- src/Fl_Image_Reader.cxx
+++ src/Fl_Image_Reader.cxx
@@ -41,68 +41,82 @@
int Fl_Image_Reader::open(const char *filename) {
if (!filename)
return -1;
- pName = fl_strdup(filename);
- if ( (pFile = fl_fopen(filename, "rb")) == NULL ) {
+ name_ = fl_strdup(filename);
+ if ((file_ = fl_fopen(filename, "rb")) == NULL) {
return -1;
}
- pIsFile = 1;
+ is_file_ = 1;
return 0;
}
-// Initialize the reader for memory access, name is copied and stored
-int Fl_Image_Reader::open(const char *imagename, const unsigned char *data, const long datasize) {
+// Initialize the reader for memory access, name is copied and stored.
+int Fl_Image_Reader::open(const char *imagename, const unsigned char *data, const size_t datasize) {
if (imagename)
- pName = fl_strdup(imagename);
+ name_ = fl_strdup(imagename);
if (data) {
- pStart = pData = data;
- pIsData = 1;
- if (datasize > 0) {
- pEnd = pStart + datasize;
- }
+ start_ = data_ = data;
+ is_data_ = 1;
+ end_ = start_ + datasize;
+ return 0;
+ }
+ return -1;
+}
+
+
+// Initialize the reader for memory access, name is copied and stored.
+// Deprecated, DO NOT USE! Buffer overruns will not be checked!
+// Please use instead:
+// Fl_Image_Reader::open(const char *imagename, const unsigned char *data, const size_t datasize)
+
+int Fl_Image_Reader::open(const char *imagename, const unsigned char *data) {
+ if (imagename)
+ name_ = fl_strdup(imagename);
+ if (data) {
+ start_ = data_ = data;
+ is_data_ = 1;
return 0;
- } else {
- return -1;
}
+ return -1;
}
// Close and destroy the reader
Fl_Image_Reader::~Fl_Image_Reader() {
- if (pIsFile && pFile) {
- fclose(pFile);
+ if (is_file_ && file_) {
+ fclose(file_);
}
- if (pName)
- ::free(pName);
+ if (name_)
+ free(name_);
}
// Read a single byte from memory or a file
uchar Fl_Image_Reader::read_byte() {
if (error()) // don't read after read error or EOF
return 0;
- if (pIsFile) {
- int ret = getc(pFile);
+ if (is_file_) {
+ int ret = getc(file_);
if (ret < 0) {
- if (feof(pFile))
- pError = 1;
- else if (ferror(pFile))
- pError = 2;
+ if (feof(file_))
+ error_ = 1;
+ else if (ferror(file_))
+ error_ = 2;
else
- pError = 3; // unknown error
+ error_ = 3; // unknown error
return 0;
}
return ret;
- } else if (pIsData) {
- if (pData < pEnd)
- return *pData++;
- pError = 1; // EOF
+ } else if (is_data_) {
+ if (data_ < end_)
+ return *data_++;
+ error_ = 1; // EOF
return 0;
}
- pError = 3; // undefined mode
+ error_ = 3; // undefined mode
return 0;
}
// Read a 16-bit unsigned integer, LSB-first
unsigned short Fl_Image_Reader::read_word() {
- unsigned char b0, b1; // Bytes from file or memory
+ unsigned char b0, b1; // Bytes from file or memory
b0 = read_byte();
b1 = read_byte();
if (error())
@@ -112,7 +126,7 @@ unsigned short Fl_Image_Reader::read_word() {
// Read a 32-bit unsigned integer, LSB-first
unsigned int Fl_Image_Reader::read_dword() {
- unsigned char b0, b1, b2, b3; // Bytes from file or memory
+ unsigned char b0, b1, b2, b3; // Bytes from file or memory
b0 = read_byte();
b1 = read_byte();
b2 = read_byte();
@@ -125,24 +139,24 @@ unsigned int Fl_Image_Reader::read_dword() {
// Move the current read position to a byte offset from the beginning
// of the file or the original start address in memory.
// This method clears the error flag if the position is valid.
-// If reading from memory and (pStart + n) overflows, then the result is undefined.
+// If reading from memory and (start_ + n) overflows, then the result is undefined.
void Fl_Image_Reader::seek(unsigned int n) {
- pError = 0;
- if (pIsFile) {
- int ret = fseek(pFile, n , SEEK_SET);
+ error_ = 0;
+ if (is_file_) {
+ int ret = fseek(file_, n, SEEK_SET);
if (ret < 0)
- pError = 2; // read / position error
+ error_ = 2; // read / position error
return;
- } else if (pIsData) {
- if (pStart + n <= pEnd)
- pData = pStart + n;
+ } else if (is_data_) {
+ if (start_ + n <= end_)
+ data_ = start_ + n;
else
- pError = 2; // read / position error
+ error_ = 2; // read / position error
return;
}
// unknown mode (not initialized ?)
- pError = 3;
+ error_ = 3;
}
// Get the current read position as a byte offset from the
@@ -152,10 +166,10 @@ void Fl_Image_Reader::seek(unsigned int n) {
// return -1 or any error code from ftell().
long Fl_Image_Reader::tell() const {
- if (pIsFile) {
- return ftell(pFile);
- } else if (pIsData) {
- return long(pData - pStart);
+ if (is_file_) {
+ return ftell(file_);
+ } else if (is_data_) {
+ return long(data_ - start_);
}
return 0;
}
diff --git src/Fl_Image_Reader.h src/Fl_Image_Reader.h
index e9b2937..ef32792 100644
--- src/Fl_Image_Reader.h
+++ src/Fl_Image_Reader.h
@@ -29,25 +29,27 @@
#include <stdio.h>
-class Fl_Image_Reader
-{
+class Fl_Image_Reader {
public:
// Create the reader.
- Fl_Image_Reader() :
- pIsFile(0), pIsData(0),
- pFile(0L), pData(0L),
- pStart(0L),
- pEnd((const unsigned char *)(-1L)),
- pName(0L),
- pError(0)
- {}
+ Fl_Image_Reader()
+ : is_file_(0)
+ , is_data_(0)
+ , file_(0L)
+ , data_(0L)
+ , start_(0L)
+ , end_((const unsigned char *)(-1L))
+ , name_(0L)
+ , error_(0) {}
// Initialize the reader to access the file system, filename is copied
// and stored.
int open(const char *filename);
// Initialize the reader for memory access, name is copied and stored
- int open(const char *imagename, const unsigned char *data, const long datasize = -1);
+ int open(const char *imagename, const unsigned char *data, const size_t datasize);
+ // Deprecated, DO NOT USE!
+ int open(const char *imagename, const unsigned char *data);
// Close and destroy the reader
~Fl_Image_Reader();
@@ -62,9 +64,7 @@ public:
unsigned int read_dword();
// Read a 32-bit signed integer, LSB-first
- int read_long() {
- return (int)read_dword();
- }
+ int read_long() { return (int)read_dword(); }
// Move the current read position to a byte offset from the beginning
// of the file or the original start address in memory
@@ -75,36 +75,33 @@ public:
long tell() const;
// Get the current EOF or error status of the file or data block
- int error() const {
- return pError;
- }
+ int error() const { return error_; }
// return the name or filename for this reader
- const char *name() const { return pName; }
+ const char *name() const { return name_; }
// skip a given number of bytes
void skip(unsigned int n) { seek(tell() + n); }
private:
-
// open() sets this if we read from a file
- char pIsFile;
+ char is_file_;
// open() sets this if we read from memory
- char pIsData;
+ char is_data_;
// a pointer to the opened file
- FILE *pFile;
+ FILE *file_;
// a pointer to the current byte in memory
- const unsigned char *pData;
+ const unsigned char *data_;
// a pointer to the start of the image data
- const unsigned char *pStart;
+ const unsigned char *start_;
// a pointer to the end of image data if reading from memory, otherwise undefined
// note: currently (const unsigned char *)(-1L) if end of memory is not available
// ... which means "unlimited"
- const unsigned char *pEnd;
+ const unsigned char *end_;
// a copy of the name associated with this reader
- char *pName;
+ char *name_;
// a flag to store EOF or error status
- int pError;
+ int error_;
};
#endif // FL_IMAGE_READER_H
[ Direct Link to Message ] | |