Skip to content

Commit

Permalink
More robust PDF stream reading
Browse files Browse the repository at this point in the history
  • Loading branch information
hrobeers committed Aug 14, 2020
1 parent c5b84e6 commit 7cf0ae6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
25 changes: 24 additions & 1 deletion include/hrlib/io/vertexio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace hrlib
}

template<int Dim>
std::istream& read_next_vertex(std::istream& str, vertex<Dim>& v)
std::istream& read_next_vertex_line(std::istream& str, vertex<Dim>& v)
{
while (str.peek()!=EOF) {
// find next line containing floats
Expand Down Expand Up @@ -83,6 +83,29 @@ namespace hrlib
}
return str;
}

template<int Dim>
std::istream& read_next_vertex(std::istream& str, vertex<Dim>& v)
{
char c;
int i=0;

while (i<Dim) {
bool prefix=true;
std::stringstream fstr;
while (str.get(c)) {
if (is_float_char(c)) {
prefix = false;
fstr << c;
}
else if (!prefix)
break;
}
fstr >> v[i++];
}

return str;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/foillogic/foilio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ std::array<std::vector<vertex<2>>,2> foillogic::parse_profile(std::istream &stre
double direction=0;
vertex<2> prev;
vertex<2> point;
while(utf8::read_next_vertex<2>(stream, point)) {
while(utf8::read_next_vertex_line<2>(stream, point)) {
if (point[0]<-1 || point[0]>1 ||
point[1]<-1 || point[1]>1)
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/hrlib/io/pdfio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ std::istream& hrlib::pdf::read_next_binary(std::istream &stream, std::vector<cha
{ getline_safe(stream,line); }

// Read the binary data
if (length<32) { // TODO better length decoding (this case is to allow length references)
//if (length<32) { // TODO better length decoding (this case is to allow length references)
bin.clear();
size_t pos, slast = 0;
while ((pos=line.find("endstream"))==std::string::npos)
Expand All @@ -147,11 +147,13 @@ std::istream& hrlib::pdf::read_next_binary(std::istream &stream, std::vector<cha
bin.resize(bin.size()-(slast-pos));
while (bin.back()=='\n' || bin.back()=='\r')
bin.pop_back();
/*
}
else {
bin.resize(length);
stream.read(bin.data(), length);
}
*/

// Decompress if compressed
if (compressed)
Expand Down
4 changes: 2 additions & 2 deletions src/version_autogen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define MINOR_VERSION 2
#define REVISION 0

#define BUILD_NUMBER 619
#define COMMIT_HASH "326f37de443f1a67233bc7625e6a4e3c67b1d341"
#define BUILD_NUMBER 620
#define COMMIT_HASH "c5b84e6e67254c217c719ae4d2a1f9d0bfa9030b"

#endif // VERSION_AUTOGEN_H
2 changes: 1 addition & 1 deletion tests/unittests/hrlib-tests/curvefittests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void CurveFitTests::testVertexReading()

std::vector<vertex<2>> curve;
vertex<2> point;
while(utf8::read_next_vertex<2>(ifs, point))
while(utf8::read_next_vertex_line<2>(ifs, point))
curve.push_back(point);

if (std::all_of(curve.cbegin(), curve.cend(), [curve](const vertex<2> &p){return p[1] == curve[0][1];}))
Expand Down

0 comments on commit 7cf0ae6

Please sign in to comment.