// File: roberty65[P]bServer/common/src/ConfigProperty.cpp
// Function: ConfigProperty::parse(char const*)
int ConfigProperty::parse(const char *file)
{
	FILE *fp = fopen(file, "r");
	if (fp == NULL) return -1;
	
	int lineno = 0;
	char buf[(CP_NAME_MAX + CP_VALUE_MAX) * 2];

	while (fgets(buf, sizeof buf, fp) != NULL) {
		++lineno;
		if (*buf == '#') continue;

		int slen = strlen(buf);	
		while (slen > 0 && (buf[slen - 1] == '\n' || buf[slen - 1] == '\r'))
			--slen;
		buf[slen] = 0;

		char *endptr = buf + slen;
		if ((endptr = strchr(buf, '#')) != NULL) {
			*endptr = 0;
		}

#define SKIP_SPACE(p)	do { while (isspace(*(p))) (p)++; } while (0)
#define SKIP_CHARS(p)	do { while (*(p) && !isspace(*(p))) (p)++; } while (0)

		char *nptr = buf;
		SKIP_SPACE(nptr);
		if (*nptr == 0) continue; // empty-line

		char *vptr = strchr(nptr, '=');
		if (vptr == NULL) {
			fprintf(stderr, "No '=' found at line: %d\n", lineno);
			errno = EINVAL;
			fclose(fp);
			return -1;
		}
		else {
			*vptr++ = 0;
		}

		SKIP_SPACE(nptr); endptr = nptr; SKIP_CHARS(endptr); *endptr = 0;
		SKIP_SPACE(vptr); endptr = vptr; SKIP_CHARS(endptr); *endptr = 0;
#undef SKIP_SPACE
#undef SKIP_CHARS
		properties.insert(std::make_pair(std::string(nptr), std::string(vptr)));
	}

	fclose(fp);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb50, %rsp            # imm = 0xB50
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb08(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x5205(%rip), %rsi      # 0x12eee
callq 0x34a0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xdd05
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe1d0
movl $0x0, -0x24(%rbp)
leaq -0xa30(%rbp), %rdi
movq -0x20(%rbp), %rdx
movl $0xa00, %esi            # imm = 0xA00
callq 0x3610
cmpq $0x0, %rax
je 0xe1c0
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movsbl -0xa30(%rbp), %eax
cmpl $0x23, %eax
jne 0xdd42
jmp 0xdd0c
leaq -0xa30(%rbp), %rdi
callq 0x3170
movl %eax, -0xa34(%rbp)
xorl %eax, %eax
cmpl $0x0, -0xa34(%rbp)
movb %al, -0xb09(%rbp)
jle 0xddb0
movl -0xa34(%rbp), %eax
subl $0x1, %eax
cltq
movsbl -0xa30(%rbp,%rax), %ecx
movb $0x1, %al
cmpl $0xa, %ecx
movb %al, -0xb0a(%rbp)
je 0xdda4
movl -0xa34(%rbp), %eax
subl $0x1, %eax
cltq
movsbl -0xa30(%rbp,%rax), %eax
cmpl $0xd, %eax
sete %al
movb %al, -0xb0a(%rbp)
movb -0xb0a(%rbp), %al
movb %al, -0xb09(%rbp)
movb -0xb09(%rbp), %al
testb $0x1, %al
jne 0xddbc
jmp 0xddcd
movl -0xa34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xa34(%rbp)
jmp 0xdd54
movslq -0xa34(%rbp), %rax
movb $0x0, -0xa30(%rbp,%rax)
leaq -0xa30(%rbp), %rax
movslq -0xa34(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xa40(%rbp)
leaq -0xa30(%rbp), %rdi
movl $0x23, %esi
callq 0x3120
movq %rax, -0xa40(%rbp)
cmpq $0x0, %rax
je 0xde1c
movq -0xa40(%rbp), %rax
movb $0x0, (%rax)
leaq -0xa30(%rbp), %rax
movq %rax, -0xa48(%rbp)
jmp 0xde2c
movq -0xa48(%rbp), %rax
movsbl (%rax), %edi
callq 0x3070
cmpl $0x0, %eax
je 0xde54
movq -0xa48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa48(%rbp)
jmp 0xde2c
jmp 0xde56
movq -0xa48(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0xde6a
jmp 0xdd0c
movq -0xa48(%rbp), %rdi
movl $0x3d, %esi
callq 0x3120
movq %rax, -0xa50(%rbp)
cmpq $0x0, -0xa50(%rbp)
jne 0xdec7
movq 0xc145(%rip), %rax      # 0x19fd8
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0x5678(%rip), %rsi      # 0x13518
movb $0x0, %al
callq 0x35a0
callq 0x3060
movl $0x16, (%rax)
movq -0x20(%rbp), %rdi
callq 0x33b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe1d0
movq -0xa50(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0xa50(%rbp)
movb $0x0, (%rax)
jmp 0xdee1
jmp 0xdee3
movq -0xa48(%rbp), %rax
movsbl (%rax), %edi
callq 0x3070
cmpl $0x0, %eax
je 0xdf0b
movq -0xa48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa48(%rbp)
jmp 0xdee3
jmp 0xdf0d
movq -0xa48(%rbp), %rax
movq %rax, -0xa40(%rbp)
jmp 0xdf1d
movq -0xa40(%rbp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, -0xb0b(%rbp)
je 0xdf4e
movq -0xa40(%rbp), %rax
movsbl (%rax), %edi
callq 0x3070
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0xb0b(%rbp)
movb -0xb0b(%rbp), %al
testb $0x1, %al
jne 0xdf5a
jmp 0xdf6e
movq -0xa40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa40(%rbp)
jmp 0xdf1d
jmp 0xdf70
movq -0xa40(%rbp), %rax
movb $0x0, (%rax)
jmp 0xdf7c
movq -0xa50(%rbp), %rax
movsbl (%rax), %edi
callq 0x3070
cmpl $0x0, %eax
je 0xdfa4
movq -0xa50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa50(%rbp)
jmp 0xdf7c
jmp 0xdfa6
movq -0xa50(%rbp), %rax
movq %rax, -0xa40(%rbp)
jmp 0xdfb6
movq -0xa40(%rbp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, -0xb0c(%rbp)
je 0xdfe7
movq -0xa40(%rbp), %rax
movsbl (%rax), %edi
callq 0x3070
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0xb0c(%rbp)
movb -0xb0c(%rbp), %al
testb $0x1, %al
jne 0xdff3
jmp 0xe007
movq -0xa40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa40(%rbp)
jmp 0xdfb6
jmp 0xe009
movq -0xb08(%rbp), %rax
movq -0xa40(%rbp), %rcx
movb $0x0, (%rcx)
movq %rax, -0xb28(%rbp)
movq -0xa48(%rbp), %rax
movq %rax, -0xb20(%rbp)
leaq -0xab1(%rbp), %rdi
movq %rdi, -0xb18(%rbp)
callq 0x36a0
movq -0xb20(%rbp), %rsi
movq -0xb18(%rbp), %rdx
leaq -0xab0(%rbp), %rdi
callq 0x4a20
jmp 0xe05e
movq -0xa50(%rbp), %rax
movq %rax, -0xb38(%rbp)
leaq -0xae9(%rbp), %rdi
movq %rdi, -0xb30(%rbp)
callq 0x36a0
movq -0xb38(%rbp), %rsi
movq -0xb30(%rbp), %rdx
leaq -0xae8(%rbp), %rdi
callq 0x4a20
jmp 0xe09b
leaq -0xa90(%rbp), %rdi
leaq -0xab0(%rbp), %rsi
leaq -0xae8(%rbp), %rdx
callq 0xea60
jmp 0xe0b7
movq -0xb28(%rbp), %rdi
leaq -0xa90(%rbp), %rsi
callq 0xea20
movb %dl, -0xb41(%rbp)
movq %rax, -0xb40(%rbp)
jmp 0xe0d9
movb -0xb41(%rbp), %al
movq -0xb40(%rbp), %rcx
movq %rcx, -0xb00(%rbp)
movb %al, -0xaf8(%rbp)
leaq -0xa90(%rbp), %rdi
callq 0xeaa0
leaq -0xae8(%rbp), %rdi
callq 0x3220
leaq -0xae9(%rbp), %rdi
callq 0x3420
leaq -0xab0(%rbp), %rdi
callq 0x3220
leaq -0xab1(%rbp), %rdi
callq 0x3420
jmp 0xdd0c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xac0(%rbp)
movl %eax, -0xac4(%rbp)
jmp 0xe1b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xac0(%rbp)
movl %eax, -0xac4(%rbp)
jmp 0xe19a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xac0(%rbp)
movl %eax, -0xac4(%rbp)
jmp 0xe18e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xac0(%rbp)
movl %eax, -0xac4(%rbp)
leaq -0xa90(%rbp), %rdi
callq 0xeaa0
leaq -0xae8(%rbp), %rdi
callq 0x3220
leaq -0xae9(%rbp), %rdi
callq 0x3420
leaq -0xab0(%rbp), %rdi
callq 0x3220
leaq -0xab1(%rbp), %rdi
callq 0x3420
jmp 0xe1dc
movq -0x20(%rbp), %rdi
callq 0x33b0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb50, %rsp            # imm = 0xB50
popq %rbp
retq
movq -0xac0(%rbp), %rdi
callq 0x3690
nopl (%rax,%rax)