Пример #1
0
/*******************************************************************************
* Purpose:			Stores the contents of the symbol table to file $stable.ste
* Author:			Skye Turriff
* History:			Version 1, 21 November 2015
* Called functions:	fopen(), fprintf(), st_get_type(), printf()
* Parameters:		STD sym_table with valid st_size (>0)
* Return value:		On success, the number of records printed, else -1
*******************************************************************************/
int st_store(STD sym_table) {
	FILE* fp;	/* Output file handle */
	char type;	/* Type of variable */
	int i; 

	/* Check for valid symbol table and fp */
	if (sym_table.st_size == 0) 
		return ERR_FAIL2;
	if ((fp = fopen("$stable.ste", "wt")) == NULL)
		return ERR_FAIL1;

	/* Store symbol table contents in file */
	fprintf(fp, "%d", sym_table.st_size);
	for (i = 0; i < sym_table.st_offset; i++) {
		type = st_get_type(sym_table, i);
		fprintf(fp, " %hX %d %s %d %s", 
			sym_table.pstvr[i].status_field,	/* status field */
			strlen(sym_table.pstvr[i].plex),	/* lexeme length */
			sym_table.pstvr[i].plex,			/* lexeme */
			sym_table.pstvr[i].o_line,			/* line number */
			(type == 'S' ? "-1" :				/* -1 if string, else */
			(type == 'I' ? "0" : "0.00")));		/* 0 if int, else 0.00 */
	}

	fclose(fp);
	printf("Symbol Table stored.\n");

	return i;	/* Numer of records stored */
}
Пример #2
0
/********************************************************************
Function name:      st_store()
Purpose:            Stores symbol table in a file
Author:             Warren Rainey
History/Versions:   1.0
Called functions:   fopen(), fclose()
In parameters:      STD
Return Value:       Int
**********************************************************************/  
int st_store(STD sym_table){
	int i = 0;
	FILE * fptr;
	char * fileName = "$stable.ste";

		if (sym_table.st_size == 0){
		return R_FAIL_1;
	}
	fptr=fopen(fileName,"w+");/*Fopen Unsafe*/
	if(fptr == NULL){
		return R_FAIL_1;
	}
	/*printf("Store1\n");Test*/
	fprintf(fptr, "%d", sym_table.st_size);
	for (i = 0; i < sym_table.st_offset; i++){		
		fprintf(fptr, " %X %d %s %d ", sym_table.pstvr[i].status_field, strlen(sym_table.pstvr[i].plex),sym_table.pstvr[i].plex, sym_table.pstvr[i].o_line);
		switch(st_get_type(sym_table, i)){
		case 'I':/*INT*/
			fprintf(fptr,"%d", 0); 
			break;
		case 'F':/*FLOAT*/
			fprintf(fptr,"%.2f", 0);
			break;
		case 'S':/*STRING*/
			fprintf(fptr,"%d", -1);
			break;
		}		
	}
	/*printf("Store2\n");Test*/
	fclose(fptr);	
	printf("\nSymbol Table stored.\n");
	return sym_table.st_size;
}
Пример #3
0
/**********************************************************************************************************
Purpose:				Test the st_update_type funtion. 
Author:					Chris Whitten
History/Versions:		10.18.13
Called functions:		st_get_type(), st_update_type(), st_store()
Parameters:				STD sym_table
Return value:			None
Algorithm:				Iterate of the symbol table getting each elements type and trying to change the type,
						then call the store function inroder to print out the status_filed in hex. 
**********************************************************************************************************/
void test_update_type(STD sym_table)
{
	int i; /* Used as an iterator*/
	if(! sym_table.st_size)
	{
		return;
	}
	for(i = 0; i<sym_table.st_offset; i++)
	{
		switch(st_get_type(sym_table,i))
		{
		case FLT:
			st_update_type(sym_table,i,STR);
			st_update_type(sym_table,i,INT);
			break;
		case INT:
			st_update_type(sym_table,i,FLT);
			break;
		case STR:
			st_update_type(sym_table, i, INT);
			break;
		}
	}
	st_store(sym_table);
}
Пример #4
0
/**********************************************************************************************************
Purpose:				Store the elements of the symbol table into the file $stable.ste if the symbold table
						ever fills up causing the program to crash. 
Author:					Chris Whitten
History/Versions:		10.18.13
Called functions:		chk_sym_tbl(), fprintf(), st_get_type(),fclose(),printf()
Parameters:				STD sym_table
Return value:			int representing the number of records stored in the file on success -1 on failure
Algorithm:				Open the file, iterate over every element in the symbol table saving  their
						attributes into the file, then get the type of the element and print out it's 
						initial value correctly.
**********************************************************************************************************/
int st_store(STD sym_table)
{
	int i;		/* to iterate over the symbol tables elemens. */
	FILE *fi;	/* file pointer to work with. */
	chk_sym_tbl(sym_table);
	fi = fopen("$stable.ste", "w+"); /* Will throw warning. */
	if(fi)
	{
		fprintf(fi,"%d", sym_table.st_size);
		/* Iterate over the entire symbol table */
		for(i=0; i<sym_table.st_offset; i++)
		{
			fprintf(fi, " %4X %d %s %d", sym_table.pstvr[i].status_field, strlen(sym_table.pstvr[i].plex), sym_table.pstvr[i].plex, sym_table.pstvr[i].o_line);
			/* Get the type of the in order to print out the value correctly.  */
			switch(st_get_type(sym_table,i))
			{
			case FLT:
				fprintf(fi," %.2f", sym_table.pstvr[i].i_value.fpl_val);
				break;
			case INT:
				fprintf(fi," %d", sym_table.pstvr[i].i_value.int_val);
				break;
			case STR:
				fprintf(fi," %d", sym_table.pstvr[i].i_value.str_offset);
				break;
			}
		}
		/* Close the file and diplay to the user that the table ha sbeen stored successfully before exiting the function. */
		fclose(fi);
		printf("\nSymbol Table stored.\n");
		return i;	
	}	
	return FILE_NT_FND;
}
Пример #5
0
/*
Purpose: Stores the symbol table into a file named $stable.ste
Author: Kyle Hinskens
Version: 13.11.14
Called functions: fopen(), fprintf(), fclose(), printf()
Parameters: STD sym_table The symbol table
Return value: int The number of records stored, or -1 on failure.
*/
int st_store(STD sym_table) {
	FILE *fp; /* File pointer */
	int i; /* Loop counter */
	char *fname = "$stable.ste"; /* Define a file name */

	/* Open a text file for writing ("wt") and check for sym_table validity */ 
	if ((fp = fopen(fname,"wt")) == NULL || sym_table.st_size <= 0) {
		return -1;
	}

	/* Put the symbol table size into the file */
	fprintf(fp, "%d", sym_table.st_size);

	/* Traverse the entire symbol table and put data into the file */
	for (i = 0; i < sym_table.st_offset; ++i) {
		fprintf(fp, " %X", sym_table.pstvr[i].status_field);
		fprintf(fp, " %i", strlen(sym_table.pstvr[i].plex));
		fprintf(fp, " %s", sym_table.pstvr[i].plex);
		fprintf(fp, " %i", sym_table.pstvr[i].o_line);

		/* Determine the type and print accordingly */
		switch(st_get_type(sym_table, i)) {
		case 'I':
			fprintf(fp, " %i", sym_table.pstvr[i].i_value.int_val);
			break;
		case 'F':
			fprintf(fp, " %.2f", sym_table.pstvr[i].i_value.fpl_val);
			break;
		case 'S':
			fprintf(fp, " %i", sym_table.pstvr[i].i_value.str_offset);
			break;
		default:
			return -1;
		}
	}

	fclose(fp); /* Close the file */
	printf("\nSymbol Table stored.\n");

	return i; /* Return the number of entries in the table */
}