diff options
Diffstat (limited to 'src/item.cc')
-rw-r--r-- | src/item.cc | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/src/item.cc b/src/item.cc index 7621290a..27fc6ef3 100644 --- a/src/item.cc +++ b/src/item.cc @@ -359,6 +359,32 @@ bool item_t::valid() const return true; } +void print_item(std::ostream& out, + const item_t& item, + const string& prefix) +{ + std::size_t len = item.end_pos - item.beg_pos; + + ifstream in(item.pathname); + in.seekg(item.beg_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; + } +} + string item_context(const item_t& item, const string& desc) { std::size_t len = item.end_pos - item.beg_pos; @@ -375,14 +401,6 @@ string item_context(const item_t& item, const string& desc) return out.str(); } - ifstream in(item.pathname); - in.seekg(item.beg_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'; - out << desc << " from \"" << item.pathname.string() << "\""; if (item.beg_line != item.end_line) @@ -391,16 +409,8 @@ string item_context(const item_t& item, const string& desc) else out << ", line " << item.beg_line << ":\n"; - 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 << "> " << p; - } + print_item(out, item, "> "); + return out.str(); } |