Fix the printing some more
[SXSI/XMLTree.git] / XMLTree.cpp
index e23a06a..db9814c 100644 (file)
@@ -3,7 +3,7 @@
 #include <stack>\r
 #include "XMLTree.h"\r
 #include "timings.h"\r
-\r
+#include <errno.h>\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
 // without affecting the code so much.\r
@@ -53,7 +53,7 @@ static treeNode fast_first_child(bp *Par, treeNode x)
 \r
 static treeNode fast_next_sibling(bp* Par,treeNode x)\r
 {\r
-  x = fwd_excess(Par,x,0);\r
+  x = fast_find_close(Par,x)+1;\r
   return (fast_inspect(Par,x) == OP) ? x : NULLT;\r
 }\r
 \r
@@ -107,6 +107,7 @@ XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdM
                  uint *empty_texts_bmp, TagType *tags,\r
                  TextCollection * const TC, bool dis_tc)\r
  {\r
+   buffer = 0;\r
     // creates the data structure for the tree topology\r
     Par = (bp *)umalloc(sizeof(bp));\r
     STARTTIMER();\r
@@ -248,6 +249,7 @@ void XMLTree::Save(int fd)
 // 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
@@ -941,11 +943,18 @@ 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
+  if (stream == 0){\r
+    perror(NULL);\r
+    return;\r
+  };\r
 \r
+  if (buffer == 0)\r
+    buffer = new string();\r
 \r
   FILE* fp = stream;\r
   treeNode fin = fast_find_close(Par,x);\r
@@ -954,8 +963,8 @@ void XMLTree::Print(int fd,treeNode x, bool no_text){
   uchar * tagstr;\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
@@ -967,59 +976,66 @@ void XMLTree::Print(int fd,treeNode x, bool no_text){
    uchar * current_text=NULL;\r
    if (first_idx != NULLT)\r
    current_text = GetText(MyText(first_idx));\r
-   int read = 0;\r
-\r
- std::stack<uchar*> st;\r
+   size_t read = 0;\r
+   std::vector<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
+\r
        if (no_text)\r
-        fputs("<$/>",fp);\r
+        myfputs("<$/>",fp);\r
        else{\r
-       read = fprintf(fp,"%s",(const char*) current_text);\r
-       current_text += (read + 1);\r
-       }\r
+        read = myfprintf((const char*) current_text, fp);\r
+        current_text += (read + 1);\r
+       };\r
        n+=2; // skip closing $\r
        tag = Tag(n);\r
       \r
      }\r
      else {\r
-\r
-       fputc('<',fp);\r
+       myfputc('<',fp);\r
        tagstr = (uchar*) GetTagNameByRef(tag);\r
-       fputs((const char*) tagstr ,fp);\r
+       myfputs((const char*) tagstr ,fp);\r
        n++;\r
        if (fast_inspect(Par,n)) {\r
-        st.push(tagstr);\r
+        st.push_back(tagstr);\r
         tag = Tag(n);\r
         if (tag == ATTRIBUTE_TAG_ID){\r
           n++;\r
+          if (no_text) myfputs("><@@>",fp);\r
           while (fast_inspect(Par,n)){\r
-            fputc(' ',fp);\r
-            fputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);\r
-            fputs("=\"",fp);\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
+            if (no_text) {\r
+              myfputc('<',fp);\r
+              myfputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);\r
+              myfputc('>',fp);\r
+              myfputs("<$@/></",fp);\r
+              myfputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);\r
+              myfputc('>',fp);\r
+              n+= 4;\r
+            }\r
+            else {\r
+              myfputc(' ',fp);\r
+              myfputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);\r
+              n++;\r
+              myfputs("=\"",fp);\r
+              read = myfprintf((const char*) current_text,fp);\r
+              current_text += (read + 1);\r
+              myfputc('"',fp);\r
+              n+=3;\r
             }\r
-            n+=3; //close @$ @@                             \r
           };\r
-          fputc('>',fp);\r
+          if (no_text) \r
+            myfputs("</@@>",fp);\r
+          else myfputc('>',fp);\r
           n++;\r
           tag=Tag(n);\r
         }\r
         else {\r
-          fputc('>',fp);\r
+          myfputc('>',fp);\r
         };\r
        }\r
        else {// <foo /> tag\r
-        fputs("/>",fp);\r
+        myfputs("/>",fp);\r
         n++;\r
         tag=Tag(n);     \r
        };     \r
@@ -1027,15 +1043,16 @@ void XMLTree::Print(int fd,treeNode x, bool no_text){
    }\r
    else\r
      do {\r
-       fputs("</",fp);\r
-       fputs((const char*)st.top(),fp);\r
-       fputc('>', fp);\r
-       st.pop();\r
+       myfputs("</",fp);\r
+       myfputs((const char*)st.back(),fp);\r
+       myfputc('>', fp);\r
+       st.pop_back();\r
        n++;\r
      }while (!fast_inspect(Par,n) && !st.empty());\r
    tag=Tag(n);\r
  };\r
- fputc('\n',fp);\r
- fflush(fp);\r
+ myfputc('\n',fp);\r
+ mybufferflush(fp);\r
+ //fflush(fp);\r
  fclose(fp);\r
 }\r