xmlwrite.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 898 行 · 第 1/2 页

CPP
898
字号
}



/* 
 * ~XMLWriterTag
 * -------------
 * Destructor.
 *
 * input:
 * void
 *
 * output:
 * N/A
 *
 */
XMLWriterTag::~XMLWriterTag(void)
{
    // Go through the list of children and delete each of them.  This will then be
    // repeated by the children's children and we will recursively delete the tree.
    LISTPOSITION tagPosition = m_tags.GetHeadPosition();
    while (tagPosition != NULL)
    {
	delete (XMLWriterTag *)(m_tags.GetNext(tagPosition));
    }

    // Delete the attributes...
    LISTPOSITION attrPosition = m_attributes.GetHeadPosition();
    while (attrPosition != NULL)
    {
	delete (XMLWriterAttribute *)(m_attributes.GetNext(attrPosition));
    }   

    // Delete the strings...
    if (m_name != NULL) delete [] m_name;
    if (m_comment != NULL) delete [] m_comment;
    m_name = NULL;
    m_comment = NULL;
}






/*
 * GetLength
 * ---------
 * Returns the length required to print this and all it's attributes.
 *
 * input:
 * void
 *
 * output:
 * INT32		    - The length required.
 *
 */
INT32
XMLWriterTag::GetLength(void) const
{
    INT32 length = 0;

    // Get the combined attributes lengths...
    LISTPOSITION position = m_attributes.GetHeadPosition();
    while (position != NULL)
    {
	length += ((XMLWriterAttribute *)(m_attributes.GetNext(position)))->GetLength();
    }

    // Get the combined lengths of the child tags...
    position = m_tags.GetHeadPosition();
    while (position != NULL)
    {
	length += ((XMLWriterTag *)(m_tags.GetNext(position)))->GetLength();
    }

    // Tack on the length required in the different cases...
    length += 2*m_depth + ((m_name != NULL)? 2*(strlen(m_name)) : 0) + 20 + ((m_comment != NULL)? strlen(m_comment) : 0);   // 20 is arbitrary, based on all the stuff needed for brackets and slashes.
    return length;
}




/*
 * Write
 * -----
 * Write out this tag to the given file object.
 *
 * input:
 * IHXBuffer *buffer		- Pointer to buffer to use.
 * INT32& loc			- Pointer to a location in the buffer where to print to.
 *
 * output:
 * void
 *
 */
void
XMLWriterTag::Write(IHXBuffer *buffer, INT32& loc)
{
    char temp[MAX_WRITER_BUFFER]; /* Flawfinder: ignore */
    char tabs[MAX_WRITER_BUFFER]; /* Flawfinder: ignore */
    temp[0] = tabs[0] = '\0';
    
    // See the depth of this tag and create a string composed of depth number of tabs...
    for (INT32 i = 0; i < m_depth; i++)
	SafeStrCat(tabs,  "\t", MAX_WRITER_BUFFER);

    // Switch on the type of tag...
    switch (m_type)
    {
    case XMLPlainTag:
	{
	    // If the name field is empty, return...
	    if (m_name == NULL) return;

	    // If there are attributes....
	    if (m_attributes.GetCount() > 0)
	    {
		// Open the open tag...
		SafeSprintf(temp, MAX_WRITER_BUFFER, "%s<%s%s", tabs, m_name, WRITER_EOL);
		SafeStrCpy((char *)(buffer->GetBuffer()) + loc,  temp, buffer->GetSize()-loc);
		loc += strlen(temp);

		// Write the attributes...
		WriteAttributes(buffer, loc);

		// Close the open tag...
		SafeSprintf(temp, MAX_WRITER_BUFFER, ">%s", WRITER_EOL);
		SafeStrCpy((char *)(buffer->GetBuffer()) + loc,  temp, buffer->GetSize()-loc);
		loc += strlen(temp);

		// Write any other tags...
		WriteTags(buffer, loc);
		
		// Write the closing tag...
		SafeSprintf(temp, MAX_WRITER_BUFFER, "%s</%s>%s", tabs, m_name, WRITER_EOL);
		SafeStrCpy((char *)(buffer->GetBuffer()) + loc,  temp, buffer->GetSize()-loc);
		loc += strlen(temp);
	    }

	    // When there are no attributes, just open and close the tag...
	    else
	    {
		// Open the tag...
		SafeSprintf(temp, MAX_WRITER_BUFFER, "%s<%s>%s", tabs, m_name, WRITER_EOL);
		SafeStrCpy((char *)(buffer->GetBuffer()) + loc,  temp, buffer->GetSize()-loc);
		loc += strlen(temp);

		// Write the other tags...
		WriteTags(buffer, loc);

		// Close the tag...
		SafeSprintf(temp, MAX_WRITER_BUFFER, "%s</%s>%s", tabs, m_name, WRITER_EOL);
		SafeStrCpy((char *)(buffer->GetBuffer()) + loc,  temp, buffer->GetSize()-loc);
		loc += strlen(temp);
	    }

	}
	break;
    case XMLCommentTag:
	{
	    // If the comment field is empty, return...
	    if (m_comment == NULL) return;

	    // Otherwise, format it and write it out...
	    SafeSprintf(temp, MAX_WRITER_BUFFER, "%s<-- %s -->%s", tabs, m_comment, WRITER_EOL);
	    SafeStrCpy((char *)(buffer->GetBuffer()) + loc,  temp, buffer->GetSize()-loc);
	    loc += strlen(temp);
	}
	break;
    case XMLDirectiveTag:
	{
	}
	break;
    case XMLProcInstTag:
	{
	    // If there are no attributes, then don't print out...
	    if (m_attributes.IsEmpty()) return;
	    if (m_name == NULL) return;

	    // Otherwise, format the beginning...
	    SafeSprintf(temp, MAX_WRITER_BUFFER, "%s<?%s ", tabs, m_name);
	    SafeStrCpy((char *)(buffer->GetBuffer()) + loc,  temp, buffer->GetSize()-loc);
	    loc += strlen(temp);

	    // Write out the attributes...
	    WriteAttributes(buffer, loc);

	    // Format the ending...
	    SafeSprintf(temp, buffer->GetSize()-loc, "?>%s", WRITER_EOL);
	    SafeStrCpy((char *)(buffer->GetBuffer()) + loc,  temp, buffer->GetSize()-loc);
	    loc += strlen(temp);	
	}
	break;
    }
}






/* 
 * SetProcessingInstruction
 * ------------------------
 * Make this tag a processing instruction.
 *
 * input:
 * const char *instruction	    - Name of the instruction to use.
 *
 * output:
 * void
 *
 */
void 
XMLWriterTag::SetProcessingInstruction(const char *instruction)
{
    m_type = XMLProcInstTag;
    if (m_name != NULL) delete [] m_name;
    m_name = NULL;
    if (instruction != NULL)
    {
	m_name = new char[strlen(instruction) + 1];
	strcpy(m_name, instruction); /* Flawfinder: ignore */
    }
}






/*
 * WriteAttributes
 * ---------------
 * Write out the attributes for this tag.
 *
 * input:
 * IHXBuffer *buffer		- Buffer to write to.
 * INT32& loc			- Location to where we start writing.
 *
 * output:
 * void
 *
 */
void 
XMLWriterTag::WriteAttributes(IHXBuffer *buffer, INT32& loc)
{
    // If there are no attributes, return...
    if (m_attributes.IsEmpty()) return;

    // Write out the attributes to the buffer...
    LISTPOSITION position = m_attributes.GetHeadPosition();
    while (position != NULL)
    {
	((XMLWriterAttribute *)(m_attributes.GetNext(position)))->Write(buffer, loc);
	if (position != NULL) *(((char *)(buffer->GetBuffer())) + loc++) = '\n';
    }
}





/*
 * WriteTags
 * ---------
 * Write out the tags for this tag.
 *
 * input:
 * IHXBuffer *buffer		- Buffer to write to.
 * INT32& loc			- Location to where we start writing.
 *
 * output:
 * void
 *
 */
void 
XMLWriterTag::WriteTags(IHXBuffer *buffer, INT32& loc)
{
    // If there are no attributes, return...
    if (m_tags.IsEmpty()) return;

    // Get the size and call write on each...
    LISTPOSITION position = m_tags.GetHeadPosition();
    while (position != NULL)
    {
	((XMLWriterTag *)(m_tags.GetNext(position)))->Write(buffer, loc);
    }
}







/*
 * CreateTag
 * ---------
 * Create a tag with the given name, adding it to the child list and return it.
 *
 * input:
 * const char *name	    - Name of the tag.
 *
 * output:
 * XMLWriterTag *	    - Pointer to the newly created tag.
 *
 */
XMLWriterTag *
XMLWriterTag::CreateTag(const char *name)
{
    XMLWriterTag *newTag = new XMLWriterTag;
    HX_ASSERT(newTag != NULL);
    newTag->SetName(name);
    newTag->m_depth = m_depth + 1;
    m_tags.AddTail(newTag);
    return newTag;
}




/*
 * AddAttribute
 * ------------
 * Add an attribute to the tag and then return a pointer to that attribute.
 *
 * input:
 * const char *name	    - Name of the attribute.
 * const char *value	    - Value of the attribute.
 *
 * output:
 * XMLWriterAttribute *	    - Attribute that was added.
 *
 */
XMLWriterAttribute *
XMLWriterTag::AddAttribute(const char *name, const char *value)
{
    // Create an attribute...
    XMLWriterAttribute *newAttribute = new XMLWriterAttribute;
    HX_ASSERT(newAttribute != NULL);

    // Set the name and value...
    newAttribute->SetName(name);
    newAttribute->SetValue(value);
    newAttribute->m_depth = m_depth;

    // Add the attribute...
    m_attributes.AddTail(newAttribute);
    return newAttribute;
}







/* 
 * CreateAttribute
 * ---------------
 * Creates an attribute, adding it to the list of attributes, then returning the pointer to it.
 *
 * input:
 * void
 *
 * output:
 * XMLWriterAttribute *		    - The pointer to the newly created attribute.
 *
 */
XMLWriterAttribute *
XMLWriterTag::CreateAttribute(void)
{
    // Create the attribute...
    XMLWriterAttribute *newAttribute = new XMLWriterAttribute;
    HX_ASSERT(newAttribute != NULL);

    // Add the attribute to the list of attributes...
    m_attributes.AddTail(newAttribute);
    newAttribute->m_depth = m_depth;
    return newAttribute;
}







/*
 * SetComment
 * ----------
 * Set the comment and set the type as a comment.
 *
 * input:
 * const char *comment		- Comment to set.
 *
 * output:
 * void
 *
 */
void
XMLWriterTag::SetComment(const char *comment)
{
    if (m_comment != NULL) delete [] m_comment;
    m_comment = NULL;
    if (comment != NULL)
    {
	m_comment = new char[strlen(comment) + 1];
	strcpy(m_comment, comment); /* Flawfinder: ignore */
    }
    m_type = XMLCommentTag;
}






/* 
 * SetName
 * -------
 * Set the name of this tag.
 *
 * input:
 * const char *name	- Name to set for this tag.
 *
 * output:
 * void
 *
 */
void
XMLWriterTag::SetName(const char *name)
{
    if (m_name != NULL) delete [] m_name;
    m_name = NULL;
    if (name != NULL)
    {
	m_name = new char[strlen(name) + 1];
	strcpy(m_name, name); /* Flawfinder: ignore */
    }
}




⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?