X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=XMLTree.cpp;h=a696d7f62507ce8a10b1ea256b3c0902ebc3d168;hb=2220c6ca1fb8ebefd48d6f5d9a6be3afc7325da5;hp=3c51b2ffb92d1c3808237738382193f918152d2c;hpb=9b3825663e88360c57dfe66cee5330f8f80fb452;p=SXSI%2FXMLTree.git diff --git a/XMLTree.cpp b/XMLTree.cpp index 3c51b2f..a696d7f 100644 --- a/XMLTree.cpp +++ b/XMLTree.cpp @@ -1,6 +1,7 @@ #include "XMLTree.h" #include -// functions to convert tag positions to the corresponding tree node and viceversa. + + // functions to convert tag positions to the corresponding tree node and viceversa. // These are implemented in order to be able to change the tree and Tags representations, // without affecting the code so much. // Current implementation corresponds to balanced-parentheses representation for @@ -16,6 +17,24 @@ inline int node2tagpos(treeNode x) { return (int)x; } +// to prevent suprious "unused result" warnings + +inline void ufread(void *ptr, size_t size, size_t nmemb, FILE *stream){ + size_t res; + res = fread(ptr,size,nmemb,stream); + if (res < nmemb) + throw "ufread I/O error"; + + return; +} +inline void ufwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){ + size_t res; + res = fwrite(ptr,size,nmemb,stream); + if (res < nmemb) + throw "ufwrite I/O error"; + return; +} + // Save: saves XML tree data structure to file. void XMLTree::Save(unsigned char *filename) { @@ -35,14 +54,16 @@ void XMLTree::Save(unsigned char *filename) saveTree(Par, fp); // stores the table with tag names - fwrite(&ntagnames, sizeof(int), 1, fp); + ufwrite(&ntagnames, sizeof(int), 1, fp); for (i=0; isave(fp); @@ -50,7 +71,8 @@ void XMLTree::Save(unsigned char *filename) Tags->save(fp); // stores the texts - Text->Save(fp); + if (!disable_tc) + Text->Save(fp); fclose(fp); @@ -82,32 +104,47 @@ XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text) loadTree(XML_Tree->Par, fp); // stores the table with tag names - fread(&XML_Tree->ntagnames, sizeof(int), 1, fp); - + ufread(&XML_Tree->ntagnames, sizeof(int), 1, fp); XML_Tree->TagName = (unsigned char **)malloc(XML_Tree->ntagnames*sizeof(unsigned char *)); for (i=0; intagnames;i++) { - int k = feof(fp); - fscanf(fp, "%s\n",filenameaux); - XML_Tree->TagName[i] = (unsigned char *)malloc(sizeof(unsigned char)*(strlen((const char *)filenameaux)+1)); - strcpy((char *)XML_Tree->TagName[i], (const char *)filenameaux); + + // Kim is it needed ? + int k = feof(fp); + // fscanf chokes ont "\n" which is the case for the root element + char * r = fgets(filenameaux,1023,fp); + // int r = fscanf(fp, "<%s>\n",filenameaux); + if (r==NULL) + throw "Cannot read tag list"; + + + int len = strlen((const char*)filenameaux); + XML_Tree->TagName[i] = (unsigned char *)calloc(len,sizeof(char)); + + //XML_Tree->TagName[i] = (unsigned char *)malloc(sizeof(unsigned char)*(strlen((const char *)filenameaux)+1)); + //the - 1 removes the trailing \n + strncpy((char *)XML_Tree->TagName[i], (const char *)filenameaux,len - 1); } // loads the flags - fread(&(XML_Tree->indexing_empty_texts), sizeof(bool), 1, fp); - fread(&(XML_Tree->initialized), sizeof(bool), 1, fp); - fread(&(XML_Tree->finished), sizeof(bool), 1, fp); - + ufread(&(XML_Tree->indexing_empty_texts), sizeof(bool), 1, fp); + ufread(&(XML_Tree->initialized), sizeof(bool), 1, fp); + ufread(&(XML_Tree->finished), sizeof(bool), 1, fp); + ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp); if (!(XML_Tree->indexing_empty_texts)) XML_Tree->EBVector = static_bitsequence_rrr02::load(fp); // loads the tags XML_Tree->Tags = static_sequence::load(fp); - // loads the texts - XML_Tree->Text->Load(fp,sample_rate_text); + // loads the texts + if (!XML_Tree->disable_tc){ + XML_Tree->Text = TextCollection::InitTextCollection(sample_rate_text); + XML_Tree->Text->Load(fp,sample_rate_text); + } + else + XML_Tree->Text = NULL; fclose(fp); - return XML_Tree; } @@ -147,7 +184,7 @@ XMLTree::~XMLTree() treeNode XMLTree::Root() { if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); + fprintf(stderr, "Root() : Error: data structure has not been constructed properly\n"); exit(1); } return root_node(Par); @@ -311,8 +348,10 @@ treeNode XMLTree::Parent(treeNode x) fprintf(stderr, "Error: data structure has not been constructed properly\n"); exit(1); } - - return parent(Par, x); + if (x == Root()) + return NULLT; + else + return parent(Par, x); } // Child(x,i): returns the i-th child of node x, assuming it exists. @@ -568,14 +607,16 @@ treeNode XMLTree::ParentNode(DocID d) // OpenDocument(empty_texts): it starts the construction of the data structure for // the XML document. Parameter empty_texts indicates whether we index empty texts // in document or not. Returns a non-zero value upon success, NULLT in case of error. -int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text) +int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text,bool dtc) { initialized = true; finished = false; + found_attributes = false; npar = 0; parArraySize = 1; - ntagnames = 0; - + ntagnames = 2; + disable_tc = dtc; + indexing_empty_texts = empty_texts; par_aux = (pb *)malloc(sizeof(pb)*parArraySize); @@ -590,7 +631,28 @@ int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text) return NULLT; } - TagName = NULL; + TagName = (unsigned char **) malloc(2*sizeof(unsigned char*)); + if (!TagName){ + fprintf(stderr, "Error: not enough memory\n"); + return NULLT; + } + + TagName[0] = (unsigned char *) malloc(4*sizeof(unsigned char)); + strcpy((char *) TagName[0], "<@>"); + + if (!TagName[0]){ + fprintf(stderr, "Error: not enough memory\n"); + return NULLT; + } + + TagName[1] = (unsigned char *) malloc(4*sizeof(unsigned char)); + if (!TagName[1]){ + fprintf(stderr, "Error: not enough memory\n"); + return NULLT; + } + + strcpy((char *) TagName[1], "<$>"); + if (!indexing_empty_texts) { empty_texts_aux = (unsigned int *)malloc(sizeof(unsigned int)); @@ -631,17 +693,29 @@ int XMLTree::CloseDocument() static_permutation_builder * pmb = new static_permutation_builder_mrrr(PERM_SAMPLE, bmb); static_sequence_builder * ssb = new static_sequence_builder_gmr_chunk(bmb, pmb); - Tags = new static_sequence_gmr((uint *) tags_aux, (uint) npar-1,2*ntagnames, bmb, ssb); + + // If we found an attribute then "<@>" is present in the tree + // if we didn't then it is not. "<$>" is never present in the tree + int ntagsize = found_attributes ? 2*ntagnames-1 : 2*ntagnames - 2; + + Tags = new static_sequence_gmr((uint *) tags_aux, (uint) npar-1,ntagsize, bmb, ssb); delete bmb; delete pmb; delete ssb; // makes the text collection static - Text->MakeStatic(); + if (!disable_tc) + Text->MakeStatic(); // creates the data structure marking the non-empty texts (just in the case it is necessary) - if (!indexing_empty_texts) + if (!indexing_empty_texts) { EBVector = new static_bitsequence_rrr02((uint *)empty_texts_aux,(ulong)npar,(uint)32); + free (empty_texts_aux); + empty_texts_aux = NULL; + } + + free(tags_aux); + tags_aux = NULL; finished = true; @@ -677,8 +751,13 @@ int XMLTree::NewOpenTag(unsigned char *tagname) // transforms the tagname into a tag identifier. If the tag is new, we insert // it in the table. for (i=0; i") was called + if (i==0) + found_attributes=true; + if (i==ntagnames) { // the tag is a new one, then we insert it TagName = (unsigned char **)realloc(TagName, sizeof(char *)*(ntagnames+1)); @@ -775,6 +854,11 @@ int XMLTree::NewText(unsigned char *s) return NULLT; } + if (disable_tc) { + XMLTree::NewEmptyText(); + return 1; + }; + if (!indexing_empty_texts) { empty_texts_aux = (unsigned int *)realloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb)))); if (!empty_texts_aux) { @@ -844,4 +928,19 @@ unsigned char *XMLTree::GetTagName(TagType tagid) } - +TagType XMLTree::RegisterTag(unsigned char *tagname) +{ + if (!finished) + return NULLT; + + + TagType id = XMLTree::GetTagId(tagname); + if (id == NULLT){ + id = ntagnames; + ntagnames = ntagnames + 1; + TagName = (unsigned char **) realloc(TagName,ntagnames*(sizeof(unsigned char*))); + strcpy((char*)TagName[id], (const char *)tagname); + }; + + return id; +}