Commit 9c0b1662 authored by JINMEI Tatuya's avatar JINMEI Tatuya
Browse files

[mavericks] avoid using istream::putback when the param is not the latest char.

this has been a bug, but it's now critical as libc++ makes it errors
for ifstream.
parent 887d1e72
......@@ -275,11 +275,11 @@ skipChars(std::istream& in, const char* chars, int& line, int& pos) {
}
// skip on the input stream to one of the characters in chars
// if another character is found this function returns false
// if another character is found this function throws JSONError
// unless that character is specified in the optional may_skip
//
// the character found is left on the stream
void
// It returns the found character (as an int value).
int
skipTo(std::istream& in, const std::string& file, int& line,
int& pos, const char* chars, const char* may_skip="")
{
......@@ -298,13 +298,12 @@ skipTo(std::istream& in, const std::string& file, int& line,
if (in.peek() == '\n') {
pos = 1;
++line;
} else {
++pos;
}
in.ignore();
++pos;
}
in.putback(c);
--pos;
return;
return (c);
} else {
throwJSONError(std::string("'") + std::string(1, c) + "' read, one of \"" + chars + "\" expected", file, line, pos);
}
......@@ -466,10 +465,11 @@ fromStringstreamList(std::istream& in, const std::string& file, int& line,
if (in.peek() != ']') {
cur_list_element = Element::fromJSON(in, file, line, pos);
list->add(cur_list_element);
skipTo(in, file, line, pos, ",]", WHITESPACE);
c = skipTo(in, file, line, pos, ",]", WHITESPACE);
} else {
c = in.get();
++pos;
}
c = in.get();
pos++;
}
return (list);
}
......@@ -492,15 +492,11 @@ fromStringstreamMap(std::istream& in, const std::string& file, int& line,
skipTo(in, file, line, pos, ":", WHITESPACE);
// skip the :
in.ignore();
pos++;
ConstElementPtr value = Element::fromJSON(in, file, line, pos);
map->set(key, value);
skipTo(in, file, line, pos, ",}", WHITESPACE);
c = in.get();
pos++;
c = skipTo(in, file, line, pos, ",}", WHITESPACE);
}
}
return (map);
......@@ -596,6 +592,7 @@ Element::fromJSON(std::istream& in, const std::string& file, int& line,
case '+':
case '.':
in.putback(c);
--pos;
element = fromStringstreamNumber(in, pos);
el_read = true;
break;
......@@ -604,17 +601,20 @@ Element::fromJSON(std::istream& in, const std::string& file, int& line,
case 'f':
case 'F':
in.putback(c);
--pos;
element = fromStringstreamBool(in, file, line, pos);
el_read = true;
break;
case 'n':
case 'N':
in.putback(c);
--pos;
element = fromStringstreamNull(in, file, line, pos);
el_read = true;
break;
case '"':
in.putback('"');
--pos;
element = fromStringstreamString(in, file, line, pos);
el_read = true;
break;
......
......@@ -98,16 +98,24 @@ TEST(Element, from_and_to_json) {
sv.push_back("\"\xFF\"");
BOOST_FOREACH(const std::string& s, sv) {
// test << operator, which uses Element::str()
std::ostringstream stream;
el = Element::fromJSON(s);
stream << *el;
EXPECT_EQ(s, stream.str());
// test toWire(ostream), which should also be the same now
std::ostringstream wire_stream;
el->toWire(wire_stream);
EXPECT_EQ(s, wire_stream.str());
// Test two types of fromJSON(): with string and istream.
for (int i = 0; i < 2; ++i) {
// test << operator, which uses Element::str()
if (i == 0) {
el = Element::fromJSON(s);
} else {
std::istringstream iss(s);
el = Element::fromJSON(iss);
}
std::ostringstream stream;
stream << *el;
EXPECT_EQ(s, stream.str());
// test toWire(ostream), which should also be the same now
std::ostringstream wire_stream;
el->toWire(wire_stream);
EXPECT_EQ(s, wire_stream.str());
}
}
// some parse errors
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment