// File: /workspace/github_cpp_makefile_star2_O2/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, 0x18(%rsp)
movq %rsi, %rax
movq %rdi, 0x8(%rsp)
leaq 0x3b2f(%rip), %rsi      # 0x7ab2
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x21a0
movq %rax, %r15
leaq 0x32d4(%rip), %rsi      # 0x726e
leaq 0x20(%rsp), %r14
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2310
leaq 0x32be(%rip), %rsi      # 0x7271
pushq $0x6
popq %rdx
movq %r14, %rdi
callq 0x2180
testl %eax, %eax
jne 0x4212
pushq $0x1
popq %rbx
leaq 0x329e(%rip), %r13      # 0x726e
leaq 0x20(%rsp), %rbp
leaq 0x3295(%rip), %r14      # 0x7271
pushq $0x6
popq %r12
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2310
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
callq 0x2200
testl %eax, %eax
je 0x4032
incl %ebx
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2310
cmpl $-0x1, %eax
je 0x41c3
incl %ebx
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2180
testl %eax, %eax
jne 0x4003
jmp 0x3fe0
leaq 0x3235(%rip), %r14      # 0x726e
leaq 0x20(%rsp), %r12
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2310
movq %r12, %rdi
callq 0x2300
movq 0x8(%rsp), %r13
movq %rax, 0x10(%r13)
xorl %eax, %eax
movl %eax, (%r13)
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2310
cmpl $0x444e45, 0x20(%rsp)   # imm = 0x444E45
je 0x4097
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2310
movl (%r13), %eax
incl %eax
jmp 0x4061
addl $0x2, %ebx
movq %r15, %rdi
callq 0x2150
leaq 0x3a09(%rip), %rsi      # 0x7ab2
movq 0x10(%rsp), %rdi
callq 0x21a0
movq %rax, %r14
xorl %eax, %eax
testl %ebx, %ebx
cmovlel %eax, %ebx
leaq 0x31aa(%rip), %r15      # 0x726e
leaq 0x20(%rsp), %r12
subl $0x1, %ebx
jb 0x40e0
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2310
jmp 0x40c9
movslq (%r13), %rbp
movq %rbp, %rax
shlq $0x4, %rax
testq %rbp, %rbp
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, 0x8(%r13)
xorl %ebx, %ebx
leaq 0x3167(%rip), %r15      # 0x726e
leaq 0x20(%rsp), %r12
xorl %r13d, %r13d
movslq %ebp, %rax
cmpq %rax, %r13
jge 0x41a9
xorl %ebp, %ebp
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2310
movq %r12, %rdi
callq 0x2300
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq %rax, (%rcx,%rbx)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2310
cmpl $0x746e49, 0x20(%rsp)   # imm = 0x746E49
je 0x418e
movq %r12, %rdi
leaq 0x30fd(%rip), %rsi      # 0x7263
pushq $0x7
popq %rdx
callq 0x2180
pushq $0x1
popq %rbp
testl %eax, %eax
je 0x418e
movq %r12, %rdi
leaq 0x30dd(%rip), %rsi      # 0x725c
pushq $0x7
popq %rdx
callq 0x2180
pushq $0x2
popq %rbp
testl %eax, %eax
jne 0x41de
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
movl %ebp, 0x8(%rax,%rbx)
incq %r13
movl (%rcx), %ebp
addq $0x10, %rbx
jmp 0x410f
movq %r14, %rdi
callq 0x2150
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6e26(%rip), %rdi      # 0xaff0
leaq 0x30de(%rip), %rsi      # 0x72af
callq 0x2140
pushq $0x1
popq %rdi
callq 0x21c0
movq 0x6ddb(%rip), %rdi      # 0xafc0
leaq 0x30fa(%rip), %rsi      # 0x72e6
callq 0x2140
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx,%rbx), %rsi
movq %rax, %rdi
callq 0x2140
leaq 0x3958(%rip), %rsi      # 0x7b65
movq %rax, %rdi
jmp 0x41d1
movq 0x6da7(%rip), %rdi      # 0xafc0
leaq 0x3057(%rip), %rsi      # 0x7277
jmp 0x41d1