Browse Source

added one more optional createFromString method that lets you specify the source file name for better error reporting

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/parkinglot@707 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen 15 years ago
parent
commit
f55eb12345
3 changed files with 27 additions and 13 deletions
  1. 25 12
      src/lib/cc/cpp/data.cc
  2. 1 0
      src/lib/cc/cpp/data.h
  3. 1 1
      src/lib/cc/cpp/data_unittests.cc

+ 25 - 12
src/lib/cc/cpp/data.cc

@@ -316,17 +316,23 @@ from_stringstream_map(std::istream &in, const std::string& file, int& line, int&
     std::string cur_map_key;
     ElementPtr cur_map_element;
     skip_chars(in, " \t\n", line, pos);
-    while (c != EOF && c != '}') {
-        p.first = str_from_stringstream(in, file, line, pos);
-        skip_to(in, file, line, pos, ":", " \t\n");
-        // skip the :
-        in.get();
-        pos++;
-        p.second = Element::createFromString(in, file, line, pos);
-        m.insert(p);
-        skip_to(in, file, line, pos, ",}", " \t\n");
+    c = in.peek();
+    if (c == '}') {
+        // empty map, skip closing curly
         c = in.get();
-        pos++;
+    } else {
+        while (c != EOF && c != '}') {
+            p.first = str_from_stringstream(in, file, line, pos);
+            skip_to(in, file, line, pos, ":", " \t\n");
+            // skip the :
+            in.get();
+            pos++;
+            p.second = Element::createFromString(in, file, line, pos);
+            m.insert(p);
+            skip_to(in, file, line, pos, ",}", " \t\n");
+            c = in.get();
+            pos++;
+        }
     }
     return Element::create(m);
 }
@@ -335,7 +341,14 @@ ElementPtr
 Element::createFromString(std::istream &in) throw(ParseError)
 {
     int line = 1, pos = 1;
-    return createFromString(in, "<unknown>", line, pos);
+    return createFromString(in, "<istream>", line, pos);
+}
+
+ElementPtr
+Element::createFromString(std::istream &in, const std::string& file_name) throw(ParseError)
+{
+    int line = 1, pos = 1;
+    return createFromString(in, file_name, line, pos);
 }
 
 ElementPtr
@@ -403,7 +416,7 @@ Element::createFromString(const std::string &in)
 {
     std::stringstream ss;
     ss << in;
-    return createFromString(ss);
+    return createFromString(ss, "<string>");
 }
 
 //

+ 1 - 0
src/lib/cc/cpp/data.h

@@ -284,6 +284,7 @@ public:
     /// \return An ElementPtr that contains the element(s) specified
     /// in the given input stream.
     static ElementPtr createFromString(std::istream& in) throw(ParseError);
+    static ElementPtr createFromString(std::istream& in, const std::string& file_name) throw(ParseError);
     /// Creates an Element from the given input stream, where we keep
     /// track of the location in the stream for error reporting.
     ///

+ 1 - 1
src/lib/cc/cpp/data_unittests.cc

@@ -75,7 +75,7 @@ TEST(Element, from_and_to_str) {
         Element::createFromString("{1}");
     } catch (isc::data::ParseError pe) {
         std::string s = std::string(pe.what());
-        EXPECT_EQ(s, "String expected in <unknown>:1:3");
+        EXPECT_EQ(s, "String expected in <string>:1:3");
     }
     
     sv.clear();