// File: /workspace/github_cpp_makefile_star2_O2/smallmuou[P]tinyhtml/tinyxml.cpp
// Function: TiXmlDocument::LoadFile(_IO_FILE*, TiXmlEncoding)
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;
	}

	// 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;
	}

	// Process the buffer in place to normalize new lines. (See comment above.)
	// Copies from the 'p' to 'q' pointer, where p can advance faster if
	// a newline-carriage return is hit.
	//
	// Wikipedia:
	// Systems based on ASCII or a compatible character set use either LF  (Line feed, '\n', 0x0A, 10 in decimal) or 
	// CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, 0x0D 0x0A)...
	//		* LF:    Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.), BeOS, Amiga, RISC OS, and others
    //		* CR+LF: DEC RT-11 and most other early non-Unix, non-IBM OSes, CP/M, MP/M, DOS, OS/2, Microsoft Windows, Symbian OS
    //		* CR:    Commodore 8-bit machines, Apple II family, Mac OS up to version 9 and OS-9

	const char* p = buf;	// the read head
	char* q = buf;			// the write head
	const char CR = 0x0d;
	const char LF = 0x0a;

	buf[length] = 0;
	while( *p ) {
		assert( p < (buf+length) );
		assert( q <= (buf+length) );
		assert( q <= p );

		if ( *p == CR ) {
			*q++ = LF;
			p++;
			if ( *p == LF ) {		// check for CR+LF (and skip LF)
				p++;
			}
		}
		else {
			*q++ = *p++;
		}
	}
	assert( q <= (buf+length) );
	*q = 0;

	Parse( buf, 0, encoding );

	delete [] buf;
	return !Error();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %rbx
testq %rsi, %rsi
je 0x48ec
movl %edx, %ebp
movq %rsi, %r15
movq %rbx, %rdi
callq 0x359a
orq $-0x1, 0x8(%rbx)
pushq $0x2
popq %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x3170
movq %r15, %rdi
callq 0x3030
movq %rax, %r12
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3170
testq %r12, %r12
jle 0x48dd
leaq 0x1(%r12), %rdi
callq 0x3040
movq %rax, %r14
movb $0x0, (%rax)
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x3210
cmpq $0x1, %rax
jne 0x48e4
movq %r14, %rax
addq %r12, %rax
movb $0x0, (%r14,%r12)
movq %r14, %rdx
movq %r14, %rcx
movb (%rdx), %sil
testb %sil, %sil
je 0x490d
cmpq %rax, %rdx
jae 0x4979
cmpq %rax, %rcx
ja 0x493b
cmpq %rdx, %rcx
ja 0x495a
cmpb $0xd, %sil
jne 0x48cc
movb $0xa, (%rcx)
cmpb $0xa, 0x1(%rdx)
je 0x48d4
incq %rdx
jmp 0x48d8
incq %rdx
movb %sil, (%rcx)
jmp 0x48d8
addq $0x2, %rdx
incq %rcx
jmp 0x4895
xorl %r14d, %r14d
pushq $0xc
jmp 0x48f1
movq %r14, %rdi
callq 0x31d0
xorl %r14d, %r14d
pushq $0x2
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6760
movl %r14d, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpq %rax, %rcx
ja 0x4998
movb $0x0, (%rcx)
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl %ebp, %ecx
callq *0x18(%rax)
movq %r14, %rdi
callq 0x31d0
movb 0x50(%rbx), %r14b
xorb $0x1, %r14b
jmp 0x4901
leaq 0x49ba(%rip), %rdi      # 0x92fc
leaq 0x46ee(%rip), %rsi      # 0x9037
leaq 0x4978(%rip), %rcx      # 0x92c8
movl $0x424, %edx            # imm = 0x424
callq 0x30e0
leaq 0x49ad(%rip), %rdi      # 0x930e
leaq 0x46cf(%rip), %rsi      # 0x9037
leaq 0x4959(%rip), %rcx      # 0x92c8
movl $0x425, %edx            # imm = 0x425
callq 0x30e0
leaq 0x4937(%rip), %rdi      # 0x92b7
leaq 0x46b0(%rip), %rsi      # 0x9037
leaq 0x493a(%rip), %rcx      # 0x92c8
movl $0x423, %edx            # imm = 0x423
callq 0x30e0
leaq 0x495d(%rip), %rdi      # 0x92fc
leaq 0x4691(%rip), %rsi      # 0x9037
leaq 0x491b(%rip), %rcx      # 0x92c8
movl $0x432, %edx            # imm = 0x432
callq 0x30e0
nop