2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-01-08 17:24:06 +00:00

Add qpdf private API methods InputSource::read_line

This commit is contained in:
m-holger 2024-11-02 13:01:53 +00:00
parent fa2d331215
commit 64a7b9f34a
2 changed files with 23 additions and 7 deletions

View File

@ -81,6 +81,8 @@ class QPDF_DLL_CLASS InputSource
// The following methods are for internal use by qpdf only. // The following methods are for internal use by qpdf only.
inline size_t read(std::string& str, size_t count, qpdf_offset_t at = -1); inline size_t read(std::string& str, size_t count, qpdf_offset_t at = -1);
inline std::string read(size_t count, qpdf_offset_t at = -1); inline std::string read(size_t count, qpdf_offset_t at = -1);
size_t read_line(std::string& str, size_t count, qpdf_offset_t at = -1);
std::string read_line(size_t count, qpdf_offset_t at = -1);
inline qpdf_offset_t fastTell(); inline qpdf_offset_t fastTell();
inline bool fastRead(char&); inline bool fastRead(char&);
inline void fastUnread(bool); inline void fastUnread(bool);

View File

@ -19,27 +19,41 @@ InputSource::getLastOffset() const
return this->last_offset; return this->last_offset;
} }
std::string size_t
InputSource::readLine(size_t max_line_length) InputSource::read_line(std::string& str, size_t count, qpdf_offset_t at)
{ {
// Return at most max_line_length characters from the next line. Lines are terminated by one or // Return at most max_line_length characters from the next line. Lines are terminated by one or
// more \r or \n characters. Consume the trailing newline characters but don't return them. // more \r or \n characters. Consume the trailing newline characters but don't return them.
// After this is called, the file will be positioned after a line terminator or at the end of // After this is called, the file will be positioned after a line terminator or at the end of
// the file, and last_offset will point to position the file had when this method was called. // the file, and last_offset will point to position the file had when this method was called.
auto result = read(max_line_length); read(str, count, at);
auto eol = result.find_first_of("\n\r"sv); auto eol = str.find_first_of("\n\r"sv);
if (eol != std::string::npos) { if (eol != std::string::npos) {
auto next_line = result.find_first_not_of("\n\r"sv, eol); auto next_line = str.find_first_not_of("\n\r"sv, eol);
result.resize(eol); str.resize(eol);
if (eol != std::string::npos) { if (eol != std::string::npos) {
seek(last_offset + static_cast<qpdf_offset_t>(next_line), SEEK_SET); seek(last_offset + static_cast<qpdf_offset_t>(next_line), SEEK_SET);
return result; return eol;
} }
} }
// We did not necessarily find the end of the trailing newline sequence. // We did not necessarily find the end of the trailing newline sequence.
seek(last_offset, SEEK_SET); seek(last_offset, SEEK_SET);
findAndSkipNextEOL(); findAndSkipNextEOL();
return eol;
}
std::string
InputSource::readLine(size_t max_line_length)
{
return read_line(max_line_length);
}
inline std::string
InputSource::read_line(size_t count, qpdf_offset_t at)
{
std::string result(count, '\0');
read_line(result, count, at);
return result; return result;
} }