Modify makefile to create libXMLTree.a
[SXSI/XMLTree.git] / XMLTree.cpp
index e23a06a..119048d 100644 (file)
@@ -1,8 +1,11 @@
 #include "basics.h"\r
-//#include <cstring>\r
-#include <stack>\r
 #include "XMLTree.h"\r
 #include "timings.h"\r
+#include <errno.h>\r
+using std::cout;\r
+using std::endl;\r
+using std::min;\r
+using std::string;\r
 \r
 // functions to convert tag positions to the corresponding tree node and viceversa. \r
 // These are implemented in order to be able to change the tree and Tags representations, \r
 // Current implementation corresponds to balanced-parentheses representation for\r
 // the tree, and storing 2 tags per tree node (opening and closing tags).\r
 \r
-// tag position -> tree node\r
-static treeNode tagpos2node(int t) \r
- {\r
-    return (treeNode) t;\r
- }\r
 \r
 static int bits8 (int t ) {\r
   int r = bits(t);\r
@@ -26,36 +24,6 @@ static int bits8 (int t ) {
     return r;\r
 }\r
 \r
-// tree node -> tag position\r
-static int node2tagpos(treeNode x) \r
-{\r
-  return (int)x;\r
-}\r
-\r
-static int fast_find_close(bp *b,int s)\r
-{\r
-  return fwd_excess(b,s,-1);\r
-}\r
-\r
-static int fast_inspect(bp* Par,treeNode i)\r
-{\r
-  int j,l;\r
-  j = i >> logD;\r
-  l = i & (D-1);\r
-  return (Par->B[j] >> (D-1-l)) & 1;\r
-}\r
-\r
-static treeNode fast_first_child(bp *Par, treeNode x)\r
-{\r
-  x = x+1;\r
-  return (fast_inspect(Par,x) == OP) ? x : NULLT;\r
-}\r
-\r
-static treeNode fast_next_sibling(bp* Par,treeNode x)\r
-{\r
-  x = fwd_excess(Par,x,0);\r
-  return (fast_inspect(Par,x) == OP) ? x : NULLT;\r
-}\r
 \r
 \r
 static treeNode fast_sibling(bp* Par,treeNode x,TagType tag){\r
@@ -67,9 +35,7 @@ static treeNode fast_sibling(bp* Par,treeNode x,TagType tag){
 \r
 }\r
 \r
-static bool fast_isleaf(bp* Par,treeNode x){\r
-  return (fast_inspect(Par,x+1) == CP ? true : false);\r
-}\r
+\r
 \r
 \r
 inline uint get_field_no_power(uint *A, uint len, uint index) {\r
@@ -95,18 +61,16 @@ static uint fast_get_field(uint* A,int len, int idx)
 \r
 }\r
 \r
-inline bool fast_is_ancestor(bp * Par,treeNode x,treeNode y){\r
-  if (x > y) \r
-    return false;\r
-  else\r
-    return (x==0) || (y <= fast_find_close(Par,x));\r
-}\r
+\r
 \r
 \r
 XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdMap * const tim, \r
                  uint *empty_texts_bmp, TagType *tags,\r
-                 TextCollection * const TC, bool dis_tc)\r
+                 TextCollection * const TC, bool dis_tc,\r
+                 TextCollectionBuilder::index_type_t _index_type )\r
  {\r
+   buffer = 0;\r
+   print_stack = 0;\r
     // creates the data structure for the tree topology\r
     Par = (bp *)umalloc(sizeof(bp));\r
     STARTTIMER();\r
@@ -155,8 +119,7 @@ XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdM
 \r
     \r
     disable_tc = dis_tc;\r
-    stream = NULL;\r
-    stream_fd = 0;\r
+    text_index_type = _index_type;    \r
     std::cerr << "Number of distinct tags " << TagName->size() << "\n";\r
     //std::cerr.flush();\r
  }\r
@@ -184,11 +147,6 @@ XMLTree::~XMLTree()
 \r
     delete EBVector;\r
     EBVector = NULL;\r
-    if (stream != NULL){\r
-      fclose(stream);\r
-      stream = NULL;\r
-      stream_fd = 0;\r
-    };\r
 \r
  }\r
 \r
@@ -237,23 +195,39 @@ void XMLTree::Save(int fd)
     \r
     // stores the texts   \r
     if (!disable_tc) {\r
-      Text->Save(fp);\r
-    };\r
 \r
+      ufwrite(&text_index_type, sizeof(TextCollectionBuilder::index_type_t), 1, fp);\r
+     \r
+      const char * pref;\r
+      switch (text_index_type){\r
+      case TextCollectionBuilder::index_type_default:\r
+       pref = "default_";\r
+       break;\r
+      case TextCollectionBuilder::index_type_swcsa:\r
+       pref = "swcsa_";\r
+       break;\r
+      case TextCollectionBuilder::index_type_rlcsa:\r
+       pref = "rlcsa_";\r
+       break;\r
+      };\r
+      \r
+      Text->Save(fp, pref);\r
+          \r
 \r
+    }\r
  }\r
 \r
-\r
 // Load: loads XML tree data structure from file. Returns\r
 // a pointer to the loaded data structure\r
 XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor) \r
  {\r
+\r
     FILE *fp;\r
     char buffer[1024];\r
     XMLTree *XML_Tree;\r
     int i;\r
 \r
-\r
+    buffer[1023] = '\0';\r
 \r
     fp = fdopen(fd, "r");\r
 \r
@@ -267,23 +241,21 @@ XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor)
     PRINTTIME("Loading parenthesis struct", Loading);\r
     STARTTIMER();\r
 \r
-    XML_Tree->TagName = new vector<string>();\r
-    XML_Tree->tIdMap = new std::unordered_map<string,int>();\r
-    \r
-    string s;\r
+    XML_Tree->TagName = new std::vector<std::string>();\r
+    XML_Tree->tIdMap = new std::unordered_map<std::string,int>();\r
+    std::string s;\r
     int ntags;\r
     \r
     // Load the tag names\r
     ufread(&ntags, sizeof(int), 1, fp);\r
 \r
     for (i=0; i<ntags;i++) {\r
-      char * r = fgets(buffer,1023,fp);\r
-       if (r==NULL)\r
+       if (fgets(buffer,1022,fp) != buffer)\r
         throw "Cannot read tag list";\r
-       s = (const char*) buffer;\r
+       s = buffer;\r
        // remove the trailing \n\r
        s.erase(s.size()-1);       \r
-       XML_Tree->TagName->push_back(s);\r
+       XML_Tree->TagName->push_back(s);       \r
        XML_Tree->tIdMap->insert(std::make_pair(s,i));\r
        \r
     };\r
@@ -318,44 +290,57 @@ XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor)
     \r
     ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);\r
     if (load_tc) {\r
-    XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
-    //XML_Tree->EBVector = static_bitsequence_sdarray::load(fp);\r
+      XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
 \r
-    STOPTIMER(Loading);\r
-    PRINTTIME("Loading text bitvector struct", Loading);\r
-    STARTTIMER();\r
-\r
-    // Not used  \r
-    // loads the texts\r
-    if (!XML_Tree->disable_tc){\r
-      XML_Tree->Text = TextCollection::Load(fp,sample_factor);\r
-    }\r
-    else XML_Tree->Text = NULL;\r
-    STOPTIMER(Loading);\r
-    PRINTTIME("Loading TextCollection", Loading);\r
-    STARTTIMER(); \r
+      STOPTIMER(Loading);\r
+      PRINTTIME("Loading text bitvector struct", Loading);\r
+      STARTTIMER();\r
+      \r
+      // Not used  \r
+      // loads the texts\r
+      if (!XML_Tree->disable_tc){\r
+       ufread(&(XML_Tree->text_index_type),\r
+              sizeof(TextCollectionBuilder::index_type_t), 1, fp);\r
+       const char * pref;\r
+       switch (!XML_Tree->text_index_type){\r
+       case TextCollectionBuilder::index_type_default:\r
+         pref = "default_";\r
+         break;\r
+       case TextCollectionBuilder::index_type_swcsa:\r
+         pref = "swcsa_";\r
+         break;\r
+       case TextCollectionBuilder::index_type_rlcsa:\r
+         pref = "rlcsa_";\r
+         break;\r
+       };      \r
+        XML_Tree->Text = TextCollection::Load(fp, pref, TextCollection::index_mode_default, sample_factor);\r
+\r
+      }\r
+      else XML_Tree->Text = NULL;\r
+      STOPTIMER(Loading);\r
+      PRINTTIME("Loading TextCollection", Loading);\r
+      STARTTIMER(); \r
     }\r
     else {\r
       XML_Tree->EBVector = NULL;\r
       XML_Tree->Text = NULL;\r
       XML_Tree->disable_tc = true;\r
     };\r
-\r
-    XML_Tree->stream = NULL;\r
-    XML_Tree->stream_fd = 0;\r
     \r
+\r
     return XML_Tree;\r
  }\r
 \r
 \r
 \r
 // SubtreeSize(x): the number of nodes (and attributes) in the subtree of node x.\r
-int XMLTree::SubtreeSize(treeNode x) \r
+/*int XMLTree::SubtreeSize(treeNode x) \r
  {\r
     return subtree_size(Par, x);\r
  }\r
-\r
+*/\r
 // SubtreeTags(x,tag): the number of occurrences of tag within the subtree of node x.\r
+/*\r
 int XMLTree::SubtreeTags(treeNode x, TagType tag) \r
  {\r
     if (x == Root())\r
@@ -364,8 +349,9 @@ int XMLTree::SubtreeTags(treeNode x, TagType tag)
 \r
     int s = x + 2*subtree_size(Par, x) - 1;\r
  \r
-    return Tags->rank(tag, s) - Tags->rank(tag, node2tagpos(x)-1);\r
+    return (Tags->rank(tag, s) - Tags->rank(tag, node2tagpos(x)-1))+1;\r
  }\r
+*/\r
 int XMLTree::SubtreeElements(treeNode x) \r
  {\r
     \r
@@ -409,11 +395,11 @@ bool XMLTree::IsChild(treeNode x, treeNode y)
  }\r
 \r
 // IsFirstChild(x): returns whether node x is the first child of its parent.\r
-bool XMLTree::IsFirstChild(treeNode x)\r
+/*bool XMLTree::IsFirstChild(treeNode x)\r
  {\r
     return ((x != NULLT)&&(x==Root() || prev_sibling(Par,x) == (treeNode)-1));\r
  }\r
-\r
+*/\r
 \r
 // NumChildren(x): number of children of node x. Constant time with the data structure\r
 // of Sadakane.\r
@@ -478,14 +464,14 @@ range XMLTree::DocIds(treeNode x)
  }\r
 \r
 // Parent(x): returns the parent node of node x.\r
-\r
+/*\r
 treeNode XMLTree::Parent(treeNode x) \r
  {\r
     if (x == Root())\r
       return NULLT;\r
     else\r
       return  parent(Par, x);;\r
- }\r
+ }*/\r
 \r
 // Child(x,i): returns the i-th child of node x, assuming it exists.\r
 treeNode XMLTree::Child(treeNode x, int i) \r
@@ -495,13 +481,14 @@ treeNode XMLTree::Child(treeNode x, int i)
 }\r
 \r
 // FirstChild(x): returns the first child of node x, assuming it exists. Very fast in BP.\r
-\r
+/*\r
 treeNode XMLTree::FirstChild(treeNode x) \r
  {\r
    NULLT_IF(x==NULLT);\r
    return fast_first_child(Par, x);\r
  }\r
-\r
+*/\r
+/*\r
 treeNode XMLTree::FirstElement(treeNode x) \r
  {\r
    NULLT_IF(x==NULLT);\r
@@ -524,7 +511,7 @@ treeNode XMLTree::FirstElement(treeNode x)
      return x;\r
    }\r
  }\r
-\r
+*//*\r
 treeNode XMLTree::NextElement(treeNode x) \r
 {\r
   NULLT_IF(x==NULLT);\r
@@ -535,50 +522,35 @@ treeNode XMLTree::NextElement(treeNode x)
      return (fast_inspect(Par,x)==OP)? x : NULLT;\r
   }\r
   else return x;  \r
-}\r
-value XMLTree::CamlFirstElement(value x)\r
-{\r
-  return Val_int(FirstElement(Int_val(x)));\r
-}\r
-value XMLTree::CamlNextElement(value x)\r
-{\r
-  return Val_int(NextElement(Int_val(x)));\r
-}\r
-\r
-extern "C" value caml_cpp_fast_first_element(value xmltree, value node){\r
-  return XMLTREE(xmltree)->CamlFirstElement(node);\r
-}\r
-\r
-extern "C" value caml_cpp_fast_next_element(value xmltree, value node){\r
-  return XMLTREE(xmltree)->CamlNextElement(node);\r
-}\r
+  }*/\r
 \r
 // LastChild(x): returns the last child of node x.\r
-treeNode XMLTree::LastChild(treeNode x)\r
+   /*treeNode XMLTree::LastChild(treeNode x)\r
  {\r
    NULLT_IF(x == NULLT || fast_isleaf(Par,x));\r
    return find_open(Par, fast_find_close(Par, x)-1);\r
  }\r
-\r
+   */\r
 // NextSibling(x): returns the next sibling of node x, assuming it exists.\r
-treeNode XMLTree::NextSibling(treeNode x) \r
+/*treeNode XMLTree::NextSibling(treeNode x) \r
  {\r
    NULLT_IF(x==NULLT || x == Root() );\r
    x = fast_find_close(Par,x)+1;\r
    return (fast_inspect(Par,x) == CP ? NULLT : x);\r
  }\r
-\r
+*/\r
 \r
 // PrevSibling(x): returns the previous sibling of node x, assuming it exists.\r
-treeNode XMLTree::PrevSibling(treeNode x) \r
+/*treeNode XMLTree::PrevSibling(treeNode x) \r
  {\r
    NULLT_IF(x==NULLT);\r
    return prev_sibling(Par, x);\r
  }\r
-\r
+*/\r
 // TaggedChild(x,tag): returns the first child of node x tagged tag, or NULLT if there is none.\r
 // Because of the balanced-parentheses representation of the tree, this operation is not supported\r
 // efficiently, just iterating among the children of node x until finding the desired child.\r
+/*\r
 treeNode XMLTree::TaggedChild(treeNode x, TagType tag) \r
  {\r
    \r
@@ -605,7 +577,7 @@ treeNode XMLTree::TaggedFollowingSibling(treeNode x, TagType tag)
   }\r
   return NULLT; // no such sibling was found   \r
 }\r
-\r
+*/\r
 treeNode XMLTree::SelectChild(treeNode x, TagIdSet *tags)\r
 {\r
   \r
@@ -640,6 +612,7 @@ treeNode XMLTree::SelectFollowingSibling(treeNode x, TagIdSet *tags)
 \r
 // TaggedDescendant(x,tag): returns the first node tagged tag with larger preorder than x and within\r
 // the subtree of x. Returns NULLT if there is none.\r
+/*\r
 treeNode XMLTree::TaggedDescendant(treeNode x, TagType tag) \r
  {\r
    //NULLT_IF(x==NULLT || fast_isleaf(Par,x));\r
@@ -651,8 +624,8 @@ treeNode XMLTree::TaggedDescendant(treeNode x, TagType tag)
    \r
    return (fast_is_ancestor(Par,x,y) ? y : NULLT);\r
  }\r
-\r
-\r
+*/\r
+/*\r
 treeNode XMLTree::SelectDescendant(treeNode x, TagIdSet *tags)\r
  {\r
    NULLT_IF (x ==NULLT || fast_isleaf(Par,x));\r
@@ -670,7 +643,7 @@ treeNode XMLTree::SelectDescendant(treeNode x, TagIdSet *tags)
    return min;\r
  }\r
 \r
-\r
+*/\r
 \r
 // TaggedPrec(x,tag): returns the first node tagged tag with smaller preorder than x and not an\r
 // ancestor of x. Returns NULLT if there is none.\r
@@ -704,36 +677,20 @@ treeNode XMLTree::TaggedFollowing(treeNode x, TagType tag)
 \r
 // TaggedFollBelow(x,tag,root): returns the first node tagged tag with larger preorder than x \r
 // and not in the subtree of x. Returns NULLT if there is none.\r
+/*\r
 treeNode XMLTree::TaggedFollowingBelow(treeNode x, TagType tag, treeNode ancestor)\r
 {\r
   // NULLT_IF (x == NULLT || x == Root() || x == ancestor); \r
 \r
   //Special optimisation, test for the following sibling first\r
   treeNode close = fast_find_close(Par, x);\r
-  /*\r
-   treeNode ns = close+1;\r
-  if (fast_inspect(Par,ns) == OP) {\r
-    TagType tagns = Tag(ns);\r
-    //    cout << GetTagNameByRef(tagns) << endl;\r
-    //cout.flush();\r
-    if (tagns == PCDATA_TAG_ID){\r
-      close = ns+1;\r
-      ns = ns+2;\r
-      if (fast_inspect(Par,ns) != OP)\r
-       goto after;\r
-      tagns = Tag(ns);      \r
-    };\r
-    if (tagns == tag)\r
-      return ns;\r
-  };\r
- after:\r
-  */\r
   treeNode s = tagpos2node(Tags->select_next(tag, close));\r
   \r
   if (ancestor == Root() || s==NULLT || s < fast_find_close(Par,ancestor)) return s;\r
   else return NULLT;\r
 } \r
-\r
+*/\r
+ /*\r
 treeNode XMLTree::TaggedFollowingBefore(treeNode x, TagType tag, treeNode closing)\r
 {\r
 \r
@@ -744,7 +701,7 @@ treeNode XMLTree::TaggedFollowingBefore(treeNode x, TagType tag, treeNode closin
   \r
   return s;\r
 } \r
-\r
+ */\r
 /* Here we inline TaggedFoll to find the min globally, and only at the end\r
    we check if the min is below the context node */\r
 treeNode XMLTree::SelectFollowingBelow(treeNode x, TagIdSet *tags, treeNode ancestor)\r
@@ -763,13 +720,9 @@ treeNode XMLTree::SelectFollowingBelow(treeNode x, TagIdSet *tags, treeNode ance
   \r
 \r
    TagIdSet::const_iterator tagit;\r
-   for (tagit = tags->begin(); tagit != tags->end(); tagit++) {\r
+   for (tagit = tags->begin(); tagit != tags->end(); ++tagit) {\r
 \r
      aux = tagpos2node(Tags->select_next(*tagit, close));\r
-     \r
-     // The next sibling of x is guaranteed to be below ctx\r
-     // and is the node with lowest preorder which is after ctx.\r
-     // if we find it, we return early;         \r
      if (aux == NULLT) continue;\r
      if ((min == NULLT) || (aux < min)) min = aux;\r
    };\r
@@ -777,14 +730,43 @@ treeNode XMLTree::SelectFollowingBelow(treeNode x, TagIdSet *tags, treeNode ance
    // found the smallest node in preorder which is after x.\r
    // if ctx is the root node, just return what we found.\r
 \r
-   if (ancestor == Root()) return min;\r
-   // else check whether if is in below the ctx node\r
-\r
-   NULLT_IF (min == NULLT || min >= fast_find_close(Par, ancestor));\r
+   if (ancestor == Root() || min == NULLT || min < fast_find_close(Par, ancestor)) return min;\r
+   else return NULLT;\r
    \r
-   return min;\r
+ }\r
+/*\r
+treeNode XMLTree::SelectFollowingBefore(treeNode x, TagIdSet *tags, treeNode ancestor_closing)\r
+ {\r
+\r
+   NULLT_IF(x==NULLT || x==Root());\r
+\r
+   treeNode close = fast_find_close(Par,x);\r
+   treeNode ns = close+1;\r
+   if ( (fast_inspect(Par,ns) == OP) && (tags->find(Tag(ns)) != tags->end()))\r
+     return ns;\r
+\r
+   int i;\r
+   treeNode min = NULLT;\r
+   treeNode aux;\r
+  \r
+\r
+   TagIdSet::const_iterator tagit;\r
+   for (tagit = tags->begin(); tagit != tags->end(); ++tagit) {\r
+\r
+     aux = tagpos2node(Tags->select_next(*tagit, close));\r
+     if (aux == NULLT) continue;\r
+     if ((min == NULLT) || (aux < min)) min = aux;\r
+   };\r
+     \r
+   // found the smallest node in preorder which is after x.\r
+   // if ctx is the root node, just return what we found.\r
+\r
+   if (ancestor_closing == Root() || min == NULLT || min < ancestor_closing) return min;\r
+   else return NULLT;\r
    \r
  }\r
+*/\r
+/*\r
 treeNode XMLTree::SelectFollowingBefore(treeNode x, TagIdSet *tags, treeNode closing)\r
  {\r
 \r
@@ -816,7 +798,7 @@ treeNode XMLTree::SelectFollowingBefore(treeNode x, TagIdSet *tags, treeNode clo
    return min;\r
    \r
  }\r
-\r
+*/\r
 \r
 // TaggedAncestor(x, tag): returns the closest ancestor of x tagged tag. Return\r
 // NULLT is there is none.\r
@@ -844,11 +826,10 @@ DocID XMLTree::MyText(treeNode x)
   // seems faster than testing EBVector->access(x);\r
 \r
   if (tag == PCDATA_TAG_ID || tag == ATTRIBUTE_DATA_TAG_ID)\r
-    //if (EBVector->access(x))\r
-    return (DocID) (EBVector->rank1(x)-1); //-1 because document ids start from 0\r
-  else \r
+    return (DocID) (EBVector->rank1(x)-1);  \r
+  else   \r
     return (DocID) NULLT;\r
-  \r
+    \r
 }\r
 // MyText(x): returns the document identifier of the text below node x, \r
 // or NULLT if x is not a leaf node or the text is empty. Assumes Doc \r
@@ -856,6 +837,7 @@ DocID XMLTree::MyText(treeNode x)
 DocID XMLTree::MyTextUnsafe(treeNode x) \r
 {\r
   return (DocID) (EBVector->rank1(x)-1); //-1 because document ids start from 0\r
+  \r
 }\r
 // TextXMLId(d): returns the preorder of document with identifier d in the tree consisting of\r
 // all tree nodes and all text nodes. Assumes that the tree root has preorder 1.\r
@@ -941,101 +923,107 @@ bool XMLTree::IsOpen(treeNode x) { return fast_inspect(Par,x); }
 \r
 //WARNING this uses directly the underlying implementation for plain text\r
 \r
+\r
 void XMLTree::Print(int fd,treeNode x, bool no_text){\r
   \r
-  int newfd = dup(fd);\r
-  stream = fdopen(newfd,"wa");\r
-\r
+  if (buffer == 0) { \r
+    buffer = new string(BUFFER_ALLOC, 0);\r
+    buffer->clear();\r
+    print_stack = new std::vector<string *>();\r
+    print_stack->reserve(256);\r
+  };\r
 \r
-  FILE* fp = stream;\r
   treeNode fin = fast_find_close(Par,x);\r
   treeNode n = x;\r
   TagType tag = Tag(n);\r
-  uchar * tagstr;\r
+  \r
   range r = DocIds(x);\r
   treeNode first_idx;\r
-  treeNode first_text = (tag == PCDATA_TAG_ID ?  x : TaggedDescendant(x,PCDATA_TAG_ID));\r
-  treeNode first_att =  NULLT;//TaggedDesc(x,ATTRIBUTE_DATA_TAG_ID);\r
+  treeNode first_text = (tag == PCDATA_TAG_ID ?  x : ParentNode(r.min-1));\r
+  treeNode first_att =  NULLT;\r
   \r
-  if (first_att  == NULLT)\r
-  first_idx = first_text;\r
+  if (first_att  == NULLT) \r
+    first_idx = first_text;\r
   else if (first_text == NULLT)\r
-  first_idx = first_att;\r
+    first_idx = first_att;\r
   else\r
-   first_idx = min(first_att,first_text);\r
+    first_idx = min(first_att,first_text);\r
    \r
    uchar * current_text=NULL;\r
+\r
    if (first_idx != NULLT)\r
-   current_text = GetText(MyText(first_idx));\r
-   int read = 0;\r
-\r
- std::stack<uchar*> st;\r
- while (n <= fin){\r
-   if (fast_inspect(Par,n)){\r
-     if (tag == PCDATA_TAG_ID  ) {       \r
-       //       fputs((const char*) (GetText(MyTextUnsafe(n))),fp);\r
-       if (no_text)\r
-        fputs("<$/>",fp);\r
-       else{\r
-       read = fprintf(fp,"%s",(const char*) current_text);\r
-       current_text += (read + 1);\r
-       }\r
-       n+=2; // skip closing $\r
-       tag = Tag(n);\r
-      \r
-     }\r
-     else {\r
-\r
-       fputc('<',fp);\r
-       tagstr = (uchar*) GetTagNameByRef(tag);\r
-       fputs((const char*) tagstr ,fp);\r
-       n++;\r
-       if (fast_inspect(Par,n)) {\r
-        st.push(tagstr);\r
+     current_text = GetText(MyTextUnsafe(first_idx));\r
+  \r
+   size_t read = 0;\r
+\r
+   while (n <= fin){\r
+     if (fast_inspect(Par,n)){\r
+       if (tag == PCDATA_TAG_ID) {       \r
+        \r
+        if (no_text)\r
+          _dputs("<$/>", fd);\r
+        else {\r
+          read = _dprintf((const char*) current_text, fd);\r
+          current_text += (read + 1);\r
+        };\r
+        n+=2; // skip closing $\r
         tag = Tag(n);\r
-        if (tag == ATTRIBUTE_TAG_ID){\r
-          n++;\r
-          while (fast_inspect(Par,n)){\r
-            fputc(' ',fp);\r
-            fputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);\r
-            fputs("=\"",fp);\r
+        \r
+       } else {\r
+\r
+        _dputc('<',fd);         \r
+        _dput_str((*TagName)[tag], fd);\r
+        n++;\r
+        if (fast_inspect(Par,n)) {\r
+          print_stack->push_back(&((*TagName)[tag]));\r
+          tag = Tag(n);\r
+          if (tag == ATTRIBUTE_TAG_ID){\r
             n++;\r
-            if (no_text)\r
-              fputs("<$@/>",fp);\r
-            else{\r
-            read = fprintf(fp,"%s",(const char*) current_text);\r
-            current_text += (read + 1);\r
-            //fputs((const char*) GetText(MyTextUnsafe(n)),fp);\r
-            fputc('"',fp);\r
-            }\r
-            n+=3; //close @$ @@                             \r
-          };\r
-          fputc('>',fp);\r
+            if (no_text) _dputs("><@@>",fd);\r
+\r
+            while (fast_inspect(Par,n)){\r
+              if (no_text) {\r
+                _dputc('<', fd);\r
+                _dputs((const char*) &(GetTagNameByRef(Tag(n))[3]), fd);\r
+                _dputc('>', fd);\r
+                _dputs("<$@/></", fd);\r
+                _dputs((const char*) &(GetTagNameByRef(Tag(n))[3]), fd);\r
+                _dputc('>', fd);\r
+                n+= 4;\r
+              } else {\r
+                _dputc(' ', fd);\r
+                _dputs((const char*) &(GetTagNameByRef(Tag(n))[3]), fd);\r
+                n++;\r
+                _dputs("=\"", fd);\r
+                read = _dprintf((const char*) current_text, fd);\r
+                current_text += (read + 1);\r
+                _dputc('"', fd);\r
+                n+=3;\r
+              }\r
+            };\r
+            if (no_text) _dputs("</@@>", fd);\r
+            else _dputc('>', fd);\r
+            n++;\r
+            tag=Tag(n);\r
+          \r
+          } else \r
+            _dputc('>', fd);\r
+          \r
+        } else {// <foo /> tag\r
+          _dputs("/>", fd);\r
           n++;\r
-          tag=Tag(n);\r
-        }\r
-        else {\r
-          fputc('>',fp);\r
-        };\r
-       }\r
-       else {// <foo /> tag\r
-        fputs("/>",fp);\r
+          tag=Tag(n);   \r
+        };     \r
+       };\r
+     } else do {\r
+        _dputs("</", fd);\r
+        _dput_str(*(print_stack->back()), fd);\r
+        _dputc('>', fd);\r
+        print_stack->pop_back();\r
         n++;\r
-        tag=Tag(n);     \r
-       };     \r
-     };\r
-   }\r
-   else\r
-     do {\r
-       fputs("</",fp);\r
-       fputs((const char*)st.top(),fp);\r
-       fputc('>', fp);\r
-       st.pop();\r
-       n++;\r
-     }while (!fast_inspect(Par,n) && !st.empty());\r
-   tag=Tag(n);\r
- };\r
- fputc('\n',fp);\r
- fflush(fp);\r
- fclose(fp);\r
+       } while (!(fast_inspect(Par,n) || print_stack->empty()));\r
+     tag = Tag(n);\r
+   };\r
+   _dputc('\n', fd);\r
+   //_flush(fd);\r
 }\r