Fix bug in xml_tree::print (which would pop the printing stack one time
[SXSI/XMLTree.git] / xml-tree.cpp
index 6d33878..df7efe2 100644 (file)
@@ -13,6 +13,10 @@ extern "C" {
 
 using namespace SXSI;
 
+const xml_tree::node_t xml_tree::NIL;
+const xml_tree::node_t xml_tree::ROOT;
+
+
 const xml_tree::tag_t xml_tree::NIL_TAG_ID;
 const char* xml_tree::NIL_TAG = "<!NIL!>";
 const xml_tree::tag_t xml_tree::DOCUMENT_OPEN_TAG_ID;
@@ -26,6 +30,7 @@ const char* xml_tree::ATTRIBUTE_DATA_OPEN_TAG = "<@$>";
 const xml_tree::tag_t xml_tree::CLOSE_TAG_ID;
 const char* xml_tree::CLOSE_TAG = "</>";
 
+
 static int bits8 (int t ) {
   int r = bits(t);
   if (r <= 8)
@@ -78,6 +83,7 @@ xml_tree::xml_tree(std::vector<int32_t> *tags,
 
   size_t npar = parbitmap->size();
   parbitmap->pack();
+
   par = bp_construct(npar,
                      parbitmap->get_vector_ptr(),
                      OPT_DEGREE);
@@ -92,7 +98,8 @@ xml_tree::xml_tree(std::vector<int32_t> *tags,
   uint32_t max_tag = tag_names->size() - 1;
   static_bitsequence_builder *bmb = new static_bitsequence_builder_sdarray();
   alphabet_mapper *am = new alphabet_mapper_none();
-  this->tags = new static_sequence_bs((uint32_t *) &tags[0], npar, am, bmb);
+
+  this->tags = new static_sequence_bs((uint32_t*)&((*tags)[0]), npar, am, bmb);
   bits_per_tag = bits8(max_tag);
   tag_seq_len = npar;
   tag_seq = new uint32_t[uint_len(bits_per_tag, tag_seq_len)];
@@ -110,7 +117,8 @@ xml_tree::xml_tree(std::vector<int32_t> *tags,
     text_positions = new static_bitsequence_rrr02(textbm,
                                                   npar,
                                                   32);
-    delete [] textbm;
+    //delete [] textbm;
+    delete textbitmap;
 
     this->text_index_type = idx_type;
     text_collection = tc_builder->InitTextCollection();
@@ -177,15 +185,18 @@ xml_tree::select_descendant(xml_tree::node_t x,
   return min;
 }
 
-
 xml_tree::node_t
 xml_tree::select_sibling(xml_tree::node_t x,
                          std::unordered_set<tag_t> *tags) const
 {
   xml_tree::node_t sibling = next_sibling(x);
-  while(!is_nil(sibling) && tags->find(tag(sibling)) == tags->end())
+  xml_tree::tag_t t;
+  while(!is_nil(sibling)) {
+    t = tag(sibling);
+    if (tags->find(t) != tags->end()) return sibling;
     sibling = next_sibling(sibling);
-  return (sibling);
+  };
+  return sibling;
 }
 
 xml_tree::node_t
@@ -225,10 +236,11 @@ void xml_tree::save(int fd, char* s)
   ufwrite(tag_seq, sizeof(uint), uint_len(bits_per_tag, tag_seq_len), fp);
 
   bool disable_tc = text_collection == 0 || text_positions == 0;
+
   ufwrite(&disable_tc, sizeof(bool),1,fp);
 
-  text_positions->save(fp);
   if (!disable_tc) {
+    text_positions->save(fp);
     ufwrite(&text_index_type,
             sizeof(TextCollectionBuilder::index_type_t),
             1, fp);
@@ -253,8 +265,7 @@ void xml_tree::save(int fd, char* s)
   fclose(fp);
 
 }
-//static xml_tree* load(char*, bool, int);
-//  void print(int, node_t, bool no_text=false);
+
 xml_tree* xml_tree::load(int fd, char* name, bool load_tc, int sf)
 {
   FILE *fp;
@@ -263,7 +274,6 @@ xml_tree* xml_tree::load(int fd, char* name, bool load_tc, int sf)
   int i;
   buffer[1023] = '\0';
   fp = fdopen(fd, "r");
-
   xml_tree *tree = new xml_tree();
 
   tree->par = loadTree(fp); //TODO use new api
@@ -398,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());
@@ -410,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);
@@ -481,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<std::string>();
     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);
@@ -567,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);
@@ -576,5 +591,5 @@ void xml_tree::print(int fd, xml_tree::node_t x, bool no_text)
       text_collection->DeleteText(orig_text - 1);
     else
       text_collection->DeleteText(orig_text);
-  
+
 }