Beispiel #1
0
void		loop_it(t_tout *tout)
{
	unsigned long	key;
	int				refresh;

	while ((key = 0) || (read(0, &key, 6)) != 0)
	{
		refresh = 1;
		if (key == KEY_ESCAPE || key == 'q')
			abort_exit(0);
		else if (key == KEY_SPACE)
			select_deselect(tout);
		else if (key == KEY_ENTER)
			print_white(tout);
		else if (key == KEY_UP || key == KEY_DOWN)
			up_down(tout, key);
		else if (key == KEY_LEFT || key == KEY_RIGHT)
			left_right(tout, key);
		else if (key == KEY_BACKSPACE || key == KEY_DELETE)
			remove_word(tout);
		else
			refresh = 0;
		if (refresh)
			refresh_screen(0);
	}
}
Beispiel #2
0
/**
 * resize hashtable
 * to dimension of 2*old_hashtable
 * and put the reindex the old elements
 * @in,@out: HashP pointer
 */
HashTable resize_halve(HashP hasht)
{

    HashTable newHash;
    int i;
    unsigned int new_size;
    BucketsList bucket, next;

    new_size = 0.5 * hasht->size;
    newHash.size = new_size;

    if(!(newHash.buckets = calloc(new_size, sizeof(BucketsList))))
    {
        fprintf(stderr, "ERROR: resize_double() failed\n");
    }


    for (i = 0; i < hasht->size; i++)
    {
        for(bucket = hasht->buckets[i]; bucket; bucket = next)
        {
            next = bucket->next_bucket;
            add(bucket->key,&newHash);
            remove_word(bucket->key,hasht);
        }

    }

    free(hasht->buckets);
    hasht->size = newHash.size;
    hasht->buckets = newHash.buckets;

    return newHash;
}
Beispiel #3
0
char *
remove_word_wrapper(char *hay, char *needle)
{
	remove_word(hay, needle);

	return hay;
}
Beispiel #4
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;
}