mirror of
https://github.com/qpdf/qpdf.git
synced 2025-01-03 07:12:28 +00:00
QUtil::read_lines_from_file: optional EOL preservation
This commit is contained in:
parent
9a398504ca
commit
211a7f57be
@ -1,3 +1,10 @@
|
|||||||
|
2020-01-13 Jay Berkenbilt <ejb@ql.org>
|
||||||
|
|
||||||
|
* QUtil::read_lines_from_file: add new versions that use FILE*,
|
||||||
|
use FILE* instead if std::ifstream internally to support correct
|
||||||
|
handling of Unicode filenames in Windows, and add the option to
|
||||||
|
preserve line endings.
|
||||||
|
|
||||||
2019-11-17 Jay Berkenbilt <ejb@ql.org>
|
2019-11-17 Jay Berkenbilt <ejb@ql.org>
|
||||||
|
|
||||||
* 9.1.0: release
|
* 9.1.0: release
|
||||||
|
@ -1115,6 +1115,12 @@ QUtil::read_lines_from_file(std::function<bool(char&)> next_char,
|
|||||||
buf->reserve(buf->capacity() * 2);
|
buf->reserve(buf->capacity() * 2);
|
||||||
}
|
}
|
||||||
if (c == '\n')
|
if (c == '\n')
|
||||||
|
{
|
||||||
|
if (preserve_eol)
|
||||||
|
{
|
||||||
|
buf->append(1, c);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
// Remove any carriage return that preceded the
|
// Remove any carriage return that preceded the
|
||||||
// newline and discard the newline
|
// newline and discard the newline
|
||||||
@ -1122,6 +1128,7 @@ QUtil::read_lines_from_file(std::function<bool(char&)> next_char,
|
|||||||
{
|
{
|
||||||
buf->erase(buf->length() - 1);
|
buf->erase(buf->length() - 1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
buf = 0;
|
buf = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -418,6 +418,31 @@ void read_from_file_test()
|
|||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test with EOL preservation
|
||||||
|
std::list<std::string> lines2 =
|
||||||
|
QUtil::read_lines_from_file("other-file", true);
|
||||||
|
auto line = lines2.begin();
|
||||||
|
assert(37 == (*line).length());
|
||||||
|
assert('.' == (*line).at(35));
|
||||||
|
assert('\n' == (*line).at(36));
|
||||||
|
++line;
|
||||||
|
assert(24 == (*line).length());
|
||||||
|
assert('.' == (*line).at(21));
|
||||||
|
assert('\r' == (*line).at(22));
|
||||||
|
assert('\n' == (*line).at(23));
|
||||||
|
++line;
|
||||||
|
assert(24591 == (*line).length());
|
||||||
|
assert('.' == (*line).at(24589));
|
||||||
|
assert('\n' == (*line).at(24590));
|
||||||
|
// Test the other versions and make sure we get the same results
|
||||||
|
{
|
||||||
|
std::ifstream infs("other-file", std::ios_base::binary);
|
||||||
|
assert(QUtil::read_lines_from_file(infs, true) == lines2);
|
||||||
|
FILE* fp = QUtil::safe_fopen("other-file", "rb");
|
||||||
|
assert(QUtil::read_lines_from_file(fp, true) == lines2);
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
PointerHolder<char> buf;
|
PointerHolder<char> buf;
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
QUtil::read_file_into_memory("other-file", buf, size);
|
QUtil::read_file_into_memory("other-file", buf, size);
|
||||||
|
Loading…
Reference in New Issue
Block a user