Beispiel #1
0
void ws(char *name, FILE *fp) 
{	
	table_t table ;
	char buf[128];
	char *word;
	int *count;
	void **ar;
	int i;

	table = NULL;
	word = NULL;
	ar = NULL;
	memset(buf, 0, sizeof(buf));
	table = table_new(4096, cmpstring, hashcode);
	
	while(getword(fp, buf, sizeof(buf))) {
		word = (char *) mem_calloc(1, strlen(buf) + 1);
		assert(word);
		strcpy(word, buf);		
		
		count = table_get(table, word);
		if(count) {
			(*count)++;
		}else {
			count = (int *) mem_calloc(1, sizeof(*count));
			assert(count);
			*count = 1;
			table_put(table, word, count);	
		}
	}/*while*/
	
	if(name)
		printf("%s:\n",name);
        printf("table has keys:%d\n", table_length(table));	
	ar = table_to_array(table, NULL);
	qsort(ar, table_length(table), 2 * sizeof(*ar), cmp);
	for(i = 0; ar[i]; i += 2) {
		printf("%d\t%s\n", *(int *)ar[i+1], (char *)ar[i]);
		
	}


}
Beispiel #2
0
void wf(const char *name, FILE *fp) 
{
    table_t table = table_new(0, NULL, NULL);
    char buf[BUFSIZ];

    while (getword(fp, buf, sizeof(buf), first, rest)) {
        const char *word;
        int i, *count;
        for (i=0; buf[i] != '\0'; i++)
            buf[i] = tolower(buf[i]);
        word = atom_string(buf);
        count = table_get(table, word);
        if (count) {
            (*count) ++;
        } else {
            count = (int *)zalloc(sizeof (*count));
            *count = 1;
            table_put(table, word, count);
        }
    }

    if (name) {
        printf("%s:\n", name);
    }
    /* print the words */
    int i;
    void **array = table_to_array(table, NULL);
    qsort(array, table_length(table), 2*sizeof(*array), cmp);
    for (i = 0; array[i]; i+=2) {
        printf("%d\t%s\n", *(int *)array[i+1], (char *)array[i]);
    }
    zfree(array);

    /* destroy the table */
    table_free(&table, vfree);
}