From 115c103a05a3143ccea9e6a725fc6ee3fa206061 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kim=20Nguy=E1=BB=85n?= Date: Thu, 12 Apr 2012 16:31:05 +0200 Subject: [PATCH] Fix bug in xml_tree::print (which would pop the printing stack one time too many). --- xml-tree.cpp | 14 +++++++++----- xml-tree.hpp | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/xml-tree.cpp b/xml-tree.cpp index 3e32ffd..df7efe2 100644 --- a/xml-tree.cpp +++ b/xml-tree.cpp @@ -238,8 +238,7 @@ void xml_tree::save(int fd, char* s) bool disable_tc = text_collection == 0 || text_positions == 0; ufwrite(&disable_tc, sizeof(bool),1,fp); - fprintf(stderr, "whoot\n"); - fflush(stderr); + if (!disable_tc) { text_positions->save(fp); ufwrite(&text_index_type, @@ -409,6 +408,7 @@ void xml_tree::uflush(int fd) if (size < BUFFER_SIZE) return; uflush_r(fd, size); } + void xml_tree::flush(int fd) { uflush_r(fd, print_buffer->size()); @@ -421,12 +421,14 @@ void xml_tree::uflush_r(int fd, size_t s) size_t written; while (1) { written = write(fd, print_buffer->data(), s); - if ((written < 0) && (errno == EAGAIN || errno == EINTR)) + if ((written < 0) && (errno == EAGAIN || errno == EINTR)){ continue; + }; break; }; print_buffer->clear(); } + void xml_tree::uput_str(std::string s, int fd) { print_buffer->append(s); @@ -492,14 +494,16 @@ size_t xml_tree::uprintf(const char*s, int fd) return i; } -void xml_tree::print(int fd, xml_tree::node_t x, bool no_text) +void xml_tree::print(xml_tree::node_t x, int fd, bool no_text) { + if (print_buffer == 0) { print_buffer = new std::string(BUFFER_SIZE, 0); print_buffer->clear(); print_stack = new std::vector(); print_stack->reserve(256); }; + xml_tree::node_t fin = bp_find_close(par, x); xml_tree::node_t n = x; xml_tree::tag_t label = tag(n); @@ -578,7 +582,7 @@ void xml_tree::print(int fd, xml_tree::node_t x, bool no_text) uputc('>', fd); print_stack->pop_back(); n++; - } while (!bp_inspect(par, n) || print_stack->empty()); + } while (!bp_inspect(par, n) && !print_stack->empty()); label = tag(n); }; uputc('\n', fd); diff --git a/xml-tree.hpp b/xml-tree.hpp index 950f0ef..debabe9 100644 --- a/xml-tree.hpp +++ b/xml-tree.hpp @@ -111,7 +111,7 @@ public: //I/O functions void save(int, char*); static xml_tree* load(int, char*, bool, int); - void print(int, node_t, bool no_text=false); + void print(node_t, int, bool no_text=false); void flush(int); private: -- 2.17.1