mirror of
https://github.com/qpdf/qpdf.git
synced 2024-12-22 19:08:59 +00:00
Add new private header file InputSource_private.hh
Move qpdf private inline methods to private header file.
This commit is contained in:
parent
54cf0e519c
commit
2c7c121a97
@ -78,7 +78,7 @@ class QPDF_DLL_CLASS InputSource
|
||||
// semantically equivalent to seek(-1, SEEK_CUR) but is much more efficient.
|
||||
virtual void unreadCh(char ch) = 0;
|
||||
|
||||
// The following methods are for use by QPDFTokenizer
|
||||
// The following methods are for internal use by qpdf only.
|
||||
inline qpdf_offset_t fastTell();
|
||||
inline bool fastRead(char&);
|
||||
inline void fastUnread(bool);
|
||||
@ -111,57 +111,4 @@ class QPDF_DLL_CLASS InputSource
|
||||
qpdf_offset_t buf_start = 0;
|
||||
};
|
||||
|
||||
inline void
|
||||
InputSource::loadBuffer()
|
||||
{
|
||||
this->buf_idx = 0;
|
||||
this->buf_len = qpdf_offset_t(read(this->buffer, this->buf_size));
|
||||
// NB read sets last_offset
|
||||
this->buf_start = this->last_offset;
|
||||
}
|
||||
|
||||
inline qpdf_offset_t
|
||||
InputSource::fastTell()
|
||||
{
|
||||
if (this->buf_len == 0) {
|
||||
loadBuffer();
|
||||
} else {
|
||||
auto curr = tell();
|
||||
if (curr < this->buf_start || curr >= (this->buf_start + this->buf_len)) {
|
||||
loadBuffer();
|
||||
} else {
|
||||
this->last_offset = curr;
|
||||
this->buf_idx = curr - this->buf_start;
|
||||
}
|
||||
}
|
||||
return this->last_offset;
|
||||
}
|
||||
|
||||
inline bool
|
||||
InputSource::fastRead(char& ch)
|
||||
{
|
||||
// Before calling fastRead, fastTell must be called to prepare the buffer. Once reading is
|
||||
// complete, fastUnread must be called to set the correct file position.
|
||||
if (this->buf_idx < this->buf_len) {
|
||||
ch = this->buffer[this->buf_idx];
|
||||
++(this->buf_idx);
|
||||
++(this->last_offset);
|
||||
return true;
|
||||
|
||||
} else if (this->buf_len == 0) {
|
||||
return false;
|
||||
} else {
|
||||
seek(this->buf_start + this->buf_len, SEEK_SET);
|
||||
fastTell();
|
||||
return fastRead(ch);
|
||||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
InputSource::fastUnread(bool back)
|
||||
{
|
||||
this->last_offset -= back ? 1 : 0;
|
||||
seek(this->last_offset, SEEK_SET);
|
||||
}
|
||||
|
||||
#endif // QPDF_INPUTSOURCE_HH
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include <qpdf/InputSource.hh>
|
||||
#include <qpdf/InputSource_private.hh>
|
||||
|
||||
#include <qpdf/QIntC.hh>
|
||||
#include <qpdf/QTC.hh>
|
||||
|
@ -3,6 +3,7 @@
|
||||
// DO NOT USE ctype -- it is locale dependent for some things, and it's not worth the risk of
|
||||
// including it in case it may accidentally be used.
|
||||
|
||||
#include <qpdf/InputSource_private.hh>
|
||||
#include <qpdf/QIntC.hh>
|
||||
#include <qpdf/QPDFExc.hh>
|
||||
#include <qpdf/QPDFObjectHandle.hh>
|
||||
|
59
libqpdf/qpdf/InputSource_private.hh
Normal file
59
libqpdf/qpdf/InputSource_private.hh
Normal file
@ -0,0 +1,59 @@
|
||||
#ifndef QPDF_INPUTSOURCE_PRIVATE_HH
|
||||
#define QPDF_INPUTSOURCE_PRIVATE_HH
|
||||
|
||||
#include <qpdf/InputSource.hh>
|
||||
|
||||
inline void
|
||||
InputSource::loadBuffer()
|
||||
{
|
||||
buf_idx = 0;
|
||||
buf_len = qpdf_offset_t(read(buffer, buf_size));
|
||||
// NB read sets last_offset
|
||||
buf_start = last_offset;
|
||||
}
|
||||
|
||||
inline qpdf_offset_t
|
||||
InputSource::fastTell()
|
||||
{
|
||||
if (buf_len == 0) {
|
||||
loadBuffer();
|
||||
} else {
|
||||
auto curr = tell();
|
||||
if (curr < buf_start || curr >= (buf_start + buf_len)) {
|
||||
loadBuffer();
|
||||
} else {
|
||||
last_offset = curr;
|
||||
buf_idx = curr - buf_start;
|
||||
}
|
||||
}
|
||||
return last_offset;
|
||||
}
|
||||
|
||||
inline bool
|
||||
InputSource::fastRead(char& ch)
|
||||
{
|
||||
// Before calling fastRead, fastTell must be called to prepare the buffer. Once reading is
|
||||
// complete, fastUnread must be called to set the correct file position.
|
||||
if (buf_idx < buf_len) {
|
||||
ch = buffer[buf_idx];
|
||||
++(buf_idx);
|
||||
++(last_offset);
|
||||
return true;
|
||||
|
||||
} else if (buf_len == 0) {
|
||||
return false;
|
||||
} else {
|
||||
seek(buf_start + buf_len, SEEK_SET);
|
||||
fastTell();
|
||||
return fastRead(ch);
|
||||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
InputSource::fastUnread(bool back)
|
||||
{
|
||||
last_offset -= back ? 1 : 0;
|
||||
seek(last_offset, SEEK_SET);
|
||||
}
|
||||
|
||||
#endif // QPDF_INPUTSOURCE_PRIVATE_HH
|
Loading…
Reference in New Issue
Block a user