// File: 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:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0x100(%rsp)
movq %rsi, 0xf8(%rsp)
movq %rdx, 0xf0(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xf8(%rsp), %rdi
leaq 0x6d37(%rip), %rsi      # 0xcac2
callq 0x21a0
movq %rax, 0xe8(%rsp)
movq 0xe8(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x6464(%rip), %rsi      # 0xc210
movb $0x0, %al
callq 0x22f0
movl $0x1, 0x1c(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x644c(%rip), %rsi      # 0xc213
callq 0x2200
cmpl $0x0, %eax
je 0x5dee
movq 0xa1e0(%rip), %rdi      # 0xffb8
leaq 0x643a(%rip), %rsi      # 0xc219
callq 0x2140
movl $0x1, %edi
callq 0x21c0
jmp 0x5df0
movq 0xe8(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x640c(%rip), %rsi      # 0xc210
movb $0x0, %al
callq 0x22f0
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
leaq 0x20(%rsp), %rdi
movq 0xf0(%rsp), %rsi
callq 0x2200
cmpl $0x0, %eax
je 0x5e93
jmp 0x5e2f
movq 0xe8(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x63cd(%rip), %rsi      # 0xc210
movb $0x0, %al
callq 0x22f0
cmpl $-0x1, %eax
jne 0x5e6c
movq 0xa192(%rip), %rdi      # 0xffe8
leaq 0x63f4(%rip), %rsi      # 0xc251
callq 0x2140
movl $0x1, %edi
callq 0x21c0
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x6390(%rip), %rsi      # 0xc213
callq 0x2200
cmpl $0x0, %eax
jne 0x5e8f
jmp 0x5e91
jmp 0x5e2f
jmp 0x5e95
jmp 0x5e9a
jmp 0x5df0
movq 0xe8(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x6362(%rip), %rsi      # 0xc210
movb $0x0, %al
callq 0x22f0
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x22e0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, (%rax)
movq 0xe8(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x6320(%rip), %rsi      # 0xc210
movb $0x0, %al
callq 0x22f0
leaq 0x20(%rsp), %rdi
leaq 0x6385(%rip), %rsi      # 0xc288
callq 0x2200
cmpl $0x0, %eax
jne 0x5f0f
jmp 0x5f38
movq 0xe8(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x62ed(%rip), %rsi      # 0xc210
movb $0x0, %al
callq 0x22f0
movq 0x8(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x5edc
movq 0xe8(%rsp), %rdi
callq 0x2150
movq 0xf8(%rsp), %rdi
leaq 0x6b6e(%rip), %rsi      # 0xcac2
callq 0x21a0
movq %rax, 0xe8(%rsp)
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jge 0x5f9b
movq 0xe8(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x6289(%rip), %rsi      # 0xc210
movb $0x0, %al
callq 0x22f0
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x5f69
movq 0x8(%rsp), %rax
movslq (%rax), %rax
movl $0x10, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, 0x14(%rsp)
movq 0x8(%rsp), %rcx
movl 0x14(%rsp), %eax
cmpl (%rcx), %eax
jge 0x6148
movq 0xe8(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x6213(%rip), %rsi      # 0xc210
movb $0x0, %al
callq 0x22f0
leaq 0x20(%rsp), %rdi
callq 0x22e0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x14(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq 0xe8(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x61d3(%rip), %rsi      # 0xc210
movb $0x0, %al
callq 0x22f0
leaq 0x20(%rsp), %rdi
leaq 0x623c(%rip), %rsi      # 0xc28c
callq 0x2200
cmpl $0x0, %eax
jne 0x607b
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x14(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
jmp 0x6136
leaq 0x20(%rsp), %rdi
leaq 0x6209(%rip), %rsi      # 0xc290
callq 0x2200
cmpl $0x0, %eax
jne 0x60b2
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x14(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0x1, 0x8(%rax)
jmp 0x6134
leaq 0x20(%rsp), %rdi
leaq 0x61d9(%rip), %rsi      # 0xc297
callq 0x2200
cmpl $0x0, %eax
jne 0x60e6
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x14(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0x2, 0x8(%rax)
jmp 0x6132
movq 0x9ecb(%rip), %rdi      # 0xffb8
leaq 0x61aa(%rip), %rsi      # 0xc29e
callq 0x2140
movq %rax, %rdi
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x14(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x2140
movq %rax, %rdi
leaq 0x6a52(%rip), %rsi      # 0xcb75
callq 0x2140
movl $0x1, %edi
callq 0x21c0
jmp 0x6134
jmp 0x6136
jmp 0x6138
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x5fd8
movq 0xe8(%rsp), %rdi
callq 0x2150
addq $0x108, %rsp            # imm = 0x108
retq
nopl (%rax)