Ejemplo n.º 1
0
void print(char *file, HashTable *hasht)
{
    int i;

    for(i = 0; i < hasht->size; i++)
    {
        if(hasht->buckets[i] != NULL)
        {
            print_bucket(i, file, hasht);
        }
    }
}
Ejemplo n.º 2
0
/*
 * Parses the commands and calls the necessary functons.
 * If the command is empthy it returns 0.
 * If the command is ok it return 1
 * If the command does not exist it return -1
 */
int parser(struct table *hash_table, char *operatie)
{
	char *tok;
	char *tok2;
	int found;
	FILE *fw;

	tok = strtok(operatie, DELIM);

	if (strcmp(operatie, "") == 0 || strcmp(operatie, "\n") == 0)
		return 0;

	if (strcmp(tok, "add") == 0) {
		tok = strtok(NULL, DELIM);

		if (tok == NULL)
			printf("Error, incomplete command add!\n");
		else
			add(hash_table, tok);

		return 1;
	}

	if (strcmp(tok, "find") == 0) {
		tok = strtok(NULL, DELIM);
		found = find(hash_table, tok);
		tok = strtok(NULL, DELIM);

		if (tok != NULL) {
			fw = fopen(tok, "a");

			if (found == 1)
				fprintf(fw, "True\n");
			else
				fprintf(fw, "False\n");

			fclose(fw);
			return 1;
		}

		if (found == 1)
			printf("True\n");
		else
			printf("False\n");

		return 1;
	}

	if (strcmp(tok, "remove") == 0) {
		tok = strtok(NULL, DELIM);

		if (tok == NULL)
			printf("Error, incomplete command remove!\n");
		else
			remove_word(hash_table, tok);

		return 1;
	}

	if (strcmp(tok, "clear") == 0) {
		clear(hash_table);
		return 1;
	}

	if (strcmp(tok, "print_bucket") == 0) {
		tok = strtok(NULL, DELIM);
		tok2 = strtok(NULL, DELIM);
		if (tok2 == NULL)
			print_bucket(hash_table, atoi(tok), "\0", 1);
		else
			print_bucket(hash_table, atoi(tok), tok2, 0);

		return 1;
	}

	if (strcmp(tok, "print") == 0) {
		tok = strtok(NULL, DELIM);

		if (tok == NULL) {
			print(hash_table, "\0", 1);
			printf("\n");
		} else {
			print(hash_table, tok, 0);
			fw = fopen(tok, "a");
			fprintf(fw, "\n");
			fclose(fw);
		}
		return 1;
	}


	if (strcmp(tok, "resize") == 0) {
		tok = strtok(NULL, DELIM);

		if (tok == NULL) {
			printf("Error, incomplete command resize!\n");
			return 1;
		}

		if (strcmp(tok, "double") == 0) {
			resize_double(hash_table);
			return 1;
		}

		if (strcmp(tok, "halve") == 0) {
			resize_halve(hash_table);
			return 1;
		}
	}

	return -1;
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{
	FILE *f;
	elem *hashtable, *old_hashtable;
	char *token, *file_name, buffer[20000];
	int size = 1, i, index, old_size, cleanup = 0;

	DIE(argc < 2, "Invalid use, call with <size> [file_name].");
	size = atoi(argv[1]);
	DIE(size < 1, "Invalid size.");
	
	hashtable = (elem*) malloc(size * sizeof(elem));
	
	for(i = 0; i < size; i++)
		hashtable[i].next = NULL;

	if(argc > 2)
	{
		for(i = 2; i < argc; i++)
		{
			f = fopen(argv[i], "r");
			
			if(f == NULL)			/* Ignore file if it doesn't exist. */
				continue;
			else
				cleanup = 1;			/* Close file descriptor at the end */

			while(fgets(buffer, 20000, f) != NULL)
			{
				token = strtok(buffer, " \n");

				if(token == NULL)
					continue;

				if(!strcmp(token, "add"))
				{
					token = strtok(NULL, " \n");
					DIE(token == NULL, "Invalid command.");
					add(hashtable, size, token);
					continue;
				}

				if(!strcmp(token, "remove"))
				{
					token = strtok(NULL, " \n");
					DIE(token == NULL, "Invalid command.");
					remove_element(hashtable, size, token);
					continue;
				}

				if(!strcmp(token, "find"))
				{
					token = strtok(NULL, " \n");
					DIE(token == NULL, "Invalid command.");
					file_name = strtok(NULL, " \n");
					find(hashtable, size, token, file_name);
					continue;
				}

				if(!strcmp(token, "print"))
				{
					file_name = strtok(NULL, " \n");
					print(hashtable, size, file_name);
					continue;
				}

				if(!strcmp(token, "clear"))
				{
					clear(hashtable, size);
					continue;
				}

				if(!strcmp(token, "print_bucket"))
				{
					token = strtok(NULL, " \n");
					DIE(token == NULL, "Invalid command.");
					index = atoi(token);
					file_name = strtok(NULL, " \n");
					print_bucket(hashtable, index, file_name);
					continue;
				}

				if(!strcmp(token, "resize"))
				{
					token = strtok(NULL, " \n");
					DIE(token == NULL, "Invalid command.");
					old_size = size;
			
					if(!strcmp(token, "double"))
						size *= 2;

					if(!strcmp(token, "halve"))
						size /= 2;

					old_hashtable = hashtable;
					hashtable = resize(hashtable, size, old_size);
					free(old_hashtable);

					continue;
				}

				DIE(1, "Unknown command.");		/* If the code reaches here, the command is unknown. */
				return -2;
			}
		}
	}else 
	{
		while(fgets(buffer, 20000, stdin) != NULL)
		{
			token = strtok(buffer, " \n");

			if(token == NULL)
				continue;

			if(!strcmp(token, "add"))
			{
				token = strtok(NULL, " \n");
				DIE(token == NULL, "Invalid command.");
				add(hashtable, size, token);
				continue;
			}

			if(!strcmp(token, "remove"))
			{
				token = strtok(NULL, " \n");
				DIE(token == NULL, "Invalid command.");
				remove_element(hashtable, size, token);
				continue;
			}

			if(!strcmp(token, "find"))
			{
				token = strtok(NULL, " \n");
				DIE(token == NULL, "Invalid command.");
				file_name = strtok(NULL, " \n");
				find(hashtable, size, token, file_name);
				continue;
			}

			if(!strcmp(token, "print"))
			{
				file_name = strtok(NULL, " \n");
				print(hashtable, size, file_name);
				continue;
			}

			if(!strcmp(token, "clear"))
			{
				clear(hashtable, size);
				continue;
			}

			if(!strcmp(token, "print_bucket"))
			{
				token = strtok(NULL, " \n");
				DIE(token == NULL, "Invalid command.");
				index = atoi(token);
				file_name = strtok(NULL, " \n");
				print_bucket(hashtable, index, file_name);
				continue;
			}

			if(!strcmp(token, "resize"))
			{
				token = strtok(NULL, " \n");
				DIE(token == NULL, "Invalid command.");
				old_size = size;
			
				if(!strcmp(token, "double"))
					size *= 2;

				if(!strcmp(token, "halve"))
					size /= 2;

				old_hashtable = hashtable;
				hashtable = resize(hashtable, size, old_size);
				free(old_hashtable);

				continue;
			}

			DIE(1, "Unknown command.");
			return -2;
		}
	}

	/*	CLEAN UP	*/
	if(cleanup)
		fclose(f);	

	clear(hashtable, size);		
	free(hashtable);

	return 0;
}
void print_bucket_report(char const* bucket_name, bucket_t bucket, int expected) {
    printf("=== %s ===\n", bucket_name);
    printf("distance from expected: %u\n", distance_from_expected(bucket, expected));
    print_bucket(bucket);
}