summaryrefslogtreecommitdiff
path: root/src/error.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-02-26 04:41:38 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-02-26 04:41:38 -0400
commitfbb734689b8989bb8bc5df8217b1469e084ca64b (patch)
tree2384d5cad88c82c50c885d24efa2e1129815b2b2 /src/error.cc
parent513e2b59eb71de874cf685f887d74fc3b7792d8f (diff)
downloadfork-ledger-fbb734689b8989bb8bc5df8217b1469e084ca64b.tar.gz
fork-ledger-fbb734689b8989bb8bc5df8217b1469e084ca64b.tar.bz2
fork-ledger-fbb734689b8989bb8bc5df8217b1469e084ca64b.zip
Added a new source_context function
Diffstat (limited to 'src/error.cc')
-rw-r--r--src/error.cc36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/error.cc b/src/error.cc
index ffcd2941..5a45318f 100644
--- a/src/error.cc
+++ b/src/error.cc
@@ -77,4 +77,40 @@ string line_context(const string& line,
return buf.str();
}
+string source_context(const path& file,
+ std::size_t pos,
+ std::size_t end_pos,
+ const string& prefix)
+{
+ std::size_t len = end_pos - pos;
+ if (! len)
+ return _("<no source context>");
+
+ assert(len > 0);
+ assert(len < 2048);
+
+ std::ostringstream out;
+
+ ifstream in(file);
+ in.seekg(pos, std::ios::beg);
+
+ scoped_array<char> buf(new char[len + 1]);
+ in.read(buf.get(), len);
+ assert(static_cast<std::size_t>(in.gcount()) == len);
+ buf[len] = '\0';
+
+ bool first = true;
+ for (char * p = std::strtok(buf.get(), "\n");
+ p;
+ p = std::strtok(NULL, "\n")) {
+ if (first)
+ first = false;
+ else
+ out << '\n';
+ out << prefix << p;
+ }
+
+ return out.str();
+}
+
} // namespace ledger