当前位置: 首页>>代码示例>>C++>>正文


C++ TIXML_STRING::append方法代码示例

本文整理汇总了C++中TIXML_STRING::append方法的典型用法代码示例。如果您正苦于以下问题:C++ TIXML_STRING::append方法的具体用法?C++ TIXML_STRING::append怎么用?C++ TIXML_STRING::append使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TIXML_STRING的用法示例。


在下文中一共展示了TIXML_STRING::append方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: LoadFile

bool TiXmlDocument::LoadFile(FILE* file, TiXmlEncoding encoding) {
	if (!file) {
		SetError(TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN);
		return false;
	}

	// Delete the existing data:
	Clear();
	location.Clear();

	// Get the file size, so we can pre-allocate the string. HUGE speed impact.
	long length = 0;
	fseek(file, 0, SEEK_END);
	length = ftell(file);
	fseek(file, 0, SEEK_SET);

	// Strange case, but good to handle up front.
	if (length <= 0) {
		SetError(TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN);
		return false;
	}

	// If we have a file, assume it is all one big XML file, and read it in.
	// The document parser may decide the document ends sooner than the entire file, however.
	TIXML_STRING data;
	data.reserve(length);

	// Subtle bug here. TinyXml did use fgets. But from the XML spec:
	// 2.11 End-of-Line Handling
	// <snip>
	// <quote>
	// ...the XML processor MUST behave as if it normalized all line breaks in external
	// parsed entities (including the document entity) on input, before parsing, by translating
	// both the two-character sequence #xD #xA and any #xD that is not followed by #xA to
	// a single #xA character.
	// </quote>
	//
	// It is not clear fgets does that, and certainly isn't clear it works cross platform.
	// Generally, you expect fgets to translate from the convention of the OS to the c/unix
	// convention, and not work generally.

	/*
	while( fgets( buf, sizeof(buf), file ) )
	{
			data += buf;
	}
	 */

	char* buf = new char[ length + 1 ];
	buf[0] = 0;

	if (fread(buf, length, 1, file) != 1) {
		delete [] buf;
		SetError(TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN);
		return false;
	}

	const char* lastPos = buf;
	const char* p = buf;

	buf[length] = 0;
	while (*p) {
		assert(p < (buf + length));
		if (*p == 0xa) {
			// Newline character. No special rules for this. Append all the characters
			// since the last string, and include the newline.
			data.append(lastPos, (p - lastPos + 1)); // append, include the newline
			++p; // move past the newline
			lastPos = p; // and point to the new buffer (may be 0)
			assert(p <= (buf + length));
		} else if (*p == 0xd) {
			// Carriage return. Append what we have so far, then
			// handle moving forward in the buffer.
			if ((p - lastPos) > 0) {
				data.append(lastPos, p - lastPos); // do not add the CR
			}
			data += (char) 0xa; // a proper newline

			if (*(p + 1) == 0xa) {
				// Carriage return - new line sequence
				p += 2;
				lastPos = p;
				assert(p <= (buf + length));
			} else {
				// it was followed by something else...that is presumably characters again.
				++p;
				lastPos = p;
				assert(p <= (buf + length));
			}
		} else {
			++p;
		}
	}
	// Handle any left over characters.
	if (p - lastPos) {
		data.append(lastPos, p - lastPos);
	}
	delete [] buf;
	buf = 0;

//.........这里部分代码省略.........
开发者ID:9aa5,项目名称:crtmpserver,代码行数:101,代码来源:tinyxml.cpp

示例2: ReadFromMemory

bool TiXmlDocument::ReadFromMemory( const char* pBuf, size_t sz, TiXmlEncoding encoding)
{
    // Delete the existing data:
    Clear();
    location.Clear();

    // Get the file size, so we can pre-allocate the string. HUGE speed impact.
    long length = (long) sz;

    // Strange case, but good to handle up front.
    if ( length == 0 )
    {
        SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
        return false;
    }

    // If we have a file, assume it is all one big XML file, and read it in.
    // The document parser may decide the document ends sooner than the entire file, however.
    TIXML_STRING data;
    data.reserve( length );


    char* buf = new char[ length+1 ];
    memset(buf,0,length+1);

    memcpy(buf, pBuf, length);

    const char* lastPos = buf;
    const char* p = buf;

    buf[length] = 0;
    while( *p ) {
        assert( p < (buf+length) );
        if ( *p == 0xa ) {
            // Newline character. No special rules for this. Append all the characters
            // since the last string, and include the newline.
            data.append( lastPos, (p-lastPos+1) );  // append, include the newline
            ++p;                                    // move past the newline
            lastPos = p;                            // and point to the new buffer (may be 0)
            assert( p <= (buf+length) );
        }
        else if ( *p == 0xd ) {
            // Carriage return. Append what we have so far, then
            // handle moving forward in the buffer.
            if ( (p-lastPos) > 0 ) {
                data.append( lastPos, p-lastPos );  // do not add the CR
            }
            data += (char)0xa;                      // a proper newline

            if ( *(p+1) == 0xa ) {
                // Carriage return - new line sequence
                p += 2;
                lastPos = p;
                assert( p <= (buf+length) );
            }
            else {
                // it was followed by something else...that is presumably characters again.
                ++p;
                lastPos = p;
                assert( p <= (buf+length) );
            }
        }
        else {
            ++p;
        }
    }
    // Handle any left over characters.
    if ( p-lastPos ) {
        data.append( lastPos, p-lastPos );
    }
    delete [] buf;
    buf = 0;

    Parse( data.c_str(), 0, encoding );

    if (  Error() )
        return false;
    else
        return true;
}
开发者ID:HellicarAndLewis,项目名称:TheHelloWall,代码行数:80,代码来源:tinyxmlparser.cpp


注:本文中的TIXML_STRING::append方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。