Fix the printing some more
[SXSI/XMLTree.git] / XMLTree.cpp
index 9be500e..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
@@ -944,11 +944,15 @@ bool XMLTree::IsOpen(treeNode x) { return fast_inspect(Par,x); }
 //WARNING this uses directly the underlying implementation for plain text\r
 \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
@@ -959,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
@@ -972,18 +976,18 @@ 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
+   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
-       //       myfputs((const char*) (GetText(MyTextUnsafe(n))),fp);\r
+\r
        if (no_text)\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
@@ -1002,11 +1006,10 @@ void XMLTree::Print(int fd,treeNode x, bool no_text){
           while (fast_inspect(Par,n)){\r
             if (no_text) {\r
               myfputc('<',fp);\r
-              const uchar * tmp = &(GetTagNameByRef(Tag(n))[3]);\r
-              myfputs((const char*) tmp,fp);\r
+              myfputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);\r
               myfputc('>',fp);\r
               myfputs("<$@/></",fp);\r
-              myfputs((const char*) tmp,fp);\r
+              myfputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);\r
               myfputc('>',fp);\r
               n+= 4;\r
             }\r
@@ -1015,9 +1018,8 @@ void XMLTree::Print(int fd,treeNode x, bool no_text){
               myfputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);\r
               n++;\r
               myfputs("=\"",fp);\r
-              read = fprintf(fp,"%s",(const char*) current_text);\r
+              read = myfprintf((const char*) current_text,fp);\r
               current_text += (read + 1);\r
-              //myfputs((const char*) GetText(MyTextUnsafe(n)),fp);\r
               myfputc('"',fp);\r
               n+=3;\r
             }\r
@@ -1049,8 +1051,8 @@ void XMLTree::Print(int fd,treeNode x, bool no_text){
      }while (!fast_inspect(Par,n) && !st.empty());\r
    tag=Tag(n);\r
  };\r
//myfputc('\n',fp);\r
+ myfputc('\n',fp);\r
  mybufferflush(fp);\r
- fflush(fp);\r
//fflush(fp);\r
  fclose(fp);\r
 }\r