// File: /workspace/github_cpp_makefile_star2_O3/PrarabdhJoshi[P]DatabaseImplementation/Schema.cc
// Function: Schema::Schema(char const*, char const*)
Schema :: Schema (const char *fName, const char *relName) {

	FILE *foo = fopen (fName, "r");
	
	// this is enough space to hold any tokens
	char space[200];

	fscanf (foo, "%s", space);
	int totscans = 1;

	// see if the file starts with the correct keyword
	if (strcmp (space, "BEGIN")) {
		cout << "Unfortunately, this does not seem to be a schema file.\n";
		exit (1);
	}	
		
	while (1) {

		// check to see if this is the one we want
		fscanf (foo, "%s", space);
		totscans++;
		if (strcmp (space, relName)) {

			// it is not, so suck up everything to past the BEGIN
			while (1) {

				// suck up another token
				if (fscanf (foo, "%s", space) == EOF) {
					cerr << "Could not find the schema for the specified relation.\n";
					exit (1);
				}

				totscans++;
				if (!strcmp (space, "BEGIN")) {
					break;
				}
			}

		// otherwise, got the correct file!!
		} else {
			break;
		}
	}

	// suck in the file name
	fscanf (foo, "%s", space);
	totscans++;
	fileName = strdup (space);

	// count the number of attributes specified
	numAtts = 0;
	while (1) {
		fscanf (foo, "%s", space);
		if (!strcmp (space, "END")) {
			break;		
		} else {
			fscanf (foo, "%s", space);
			numAtts++;
		}
	}

	// now actually load up the schema
	fclose (foo);
	foo = fopen (fName, "r");

	// go past any un-needed info
	for (int i = 0; i < totscans; i++) {
		fscanf (foo, "%s", space);
	}

	// and load up the schema
	myAtts = new Attribute[numAtts];
	for (int i = 0; i < numAtts; i++ ) {

		// read in the attribute name
		fscanf (foo, "%s", space);	
		myAtts[i].name = strdup (space);

		// read in the attribute type
		fscanf (foo, "%s", space);
		if (!strcmp (space, "Int")) {
			myAtts[i].myType = Int;
		} else if (!strcmp (space, "Double")) {
			myAtts[i].myType = Double;
		} else if (!strcmp (space, "String")) {
			myAtts[i].myType = String;
		} else {
			cout << "Bad attribute type for " << myAtts[i].name << "\n";
			exit (1);
		}
	}

	fclose (foo);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x480d(%rip), %rsi      # 0x8ab2
movq %r14, %rdi
callq 0x21d0
movq %rax, %r15
leaq 0x3fb7(%rip), %rsi      # 0x826e
leaq 0x20(%rsp), %r13
movq %rax, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2350
movl $0x49474542, %eax       # imm = 0x49474542
xorl (%r13), %eax
movzwl 0x4(%r13), %ecx
xorl $0x4e, %ecx
orl %eax, %ecx
jne 0x45a7
movq %r14, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
leaq 0x3f7b(%rip), %rsi      # 0x826e
leaq 0x20(%rsp), %r13
movq %r15, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2350
movq %r13, %rdi
movq %r12, %rsi
callq 0x2230
testl %eax, %eax
je 0x4390
movq %r12, 0x18(%rsp)
movl $0x2, %ecx
leaq 0x3f49(%rip), %r13      # 0x826e
leaq 0x20(%rsp), %rbp
movl $0x49474542, %r14d      # imm = 0x49474542
movl %ecx, %r12d
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2350
cmpl $-0x1, %eax
je 0x4556
leal 0x1(%r12), %ebx
movl 0x20(%rsp), %eax
xorl %r14d, %eax
movzwl 0x24(%rsp), %ecx
xorl $0x4e, %ecx
orl %eax, %ecx
movl %ebx, %ecx
jne 0x4330
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2350
addl $0x2, %r12d
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
callq 0x2230
movl %r12d, %ecx
testl %eax, %eax
jne 0x4330
jmp 0x4395
movl $0x1, %ebx
leaq 0x3ed2(%rip), %r12      # 0x826e
leaq 0x20(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2350
movq %r13, %rdi
callq 0x2340
movq 0x8(%rsp), %rbp
movq %rax, 0x10(%rbp)
movl $0x0, (%rbp)
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2350
cmpl $0x444e45, (%r13)       # imm = 0x444E45
je 0x441c
leaq 0x3e84(%rip), %r12      # 0x826e
leaq 0x20(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2350
incl (%rbp)
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2350
cmpl $0x444e45, 0x20(%rsp)   # imm = 0x444E45
jne 0x43ef
movq %r15, %rdi
callq 0x2180
leaq 0x4687(%rip), %rsi      # 0x8ab2
movq 0x10(%rsp), %rdi
callq 0x21d0
movq %rax, %r14
cmpl $-0x1, %ebx
jl 0x4460
addl $0x2, %ebx
leaq 0x3e27(%rip), %r15      # 0x826e
leaq 0x20(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2350
decl %ebx
jne 0x444c
movslq (%rbp), %rbx
movq %rbx, %rax
shlq $0x4, %rax
testq %rbx, %rbx
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, 0x8(%rbp)
testq %rbx, %rbx
jle 0x453c
leaq 0x3ddc(%rip), %r15      # 0x826e
leaq 0x20(%rsp), %r12
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2350
movq %r12, %rdi
callq 0x2340
movq 0x8(%rbp), %rcx
movq %rax, (%rcx,%rbx)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2350
cmpl $0x746e49, 0x20(%rsp)   # imm = 0x746E49
movl $0x0, %eax
je 0x451b
movl 0x20(%rsp), %eax
movl $0x62756f44, %ecx       # imm = 0x62756F44
xorl %ecx, %eax
movl 0x23(%rsp), %ecx
movl $0x656c62, %edx         # imm = 0x656C62
xorl %edx, %ecx
orl %eax, %ecx
je 0x4516
movl 0x20(%rsp), %ecx
movl $0x69727453, %eax       # imm = 0x69727453
xorl %eax, %ecx
movl 0x23(%rsp), %edx
movl $0x676e69, %eax         # imm = 0x676E69
xorl %eax, %edx
movl $0x2, %eax
orl %ecx, %edx
je 0x451b
jmp 0x4573
movl $0x1, %eax
movq 0x8(%rsp), %rbp
movq 0x8(%rbp), %rcx
movl %eax, 0x8(%rcx,%rbx)
incq %r13
movslq (%rbp), %rax
addq $0x10, %rbx
cmpq %rax, %r13
jl 0x449c
movq %r14, %rdi
callq 0x2180
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7a93(%rip), %rdi      # 0xbff0
leaq 0x3d4b(%rip), %rsi      # 0x82af
callq 0x2170
movl $0x1, %edi
callq 0x21f0
movq 0x7a46(%rip), %rdi      # 0xbfc0
leaq 0x3d65(%rip), %rsi      # 0x82e6
callq 0x2170
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx,%rbx), %rsi
movq %rax, %rdi
callq 0x2170
leaq 0x45c3(%rip), %rsi      # 0x8b65
movq %rax, %rdi
jmp 0x4564
movq 0x7a12(%rip), %rdi      # 0xbfc0
leaq 0x3cc2(%rip), %rsi      # 0x8277
jmp 0x4564
nop