Пример #1
0
Файл: io.c Проект: JSefara/foma
struct fsm *fsm_read_text_file(char *filename) {
    struct fsm_trie_handle *th;
    char *text, *textp1, *textp2;
    int lastword;

    text = file_to_mem(filename);
    if (text == NULL) {
	return NULL;
    }
    textp1 = text;
    th = fsm_trie_init();

    for (lastword = 0 ; lastword == 0 ; textp1 = textp2+1) {
	for (textp2 = textp1 ; *textp2 != '\n' && *textp2 != '\0'; textp2++) {
	}
	if (*textp2 == '\0') {
	    lastword = 1;
	    if (textp2 == textp1)
		break;
	}
	*textp2 = '\0';
	if (strlen(textp1) > 0)
	    fsm_trie_add_word(th, textp1);
    }
    xxfree(text);
    return(fsm_trie_done(th));
}
Пример #2
0
struct fsm *fsm_read_text_file(const char *buffer) {
    struct fsm_trie_handle *th;
    const char *textp1, *textp2;
    int lastword;

    textp1 = buffer;
    th = fsm_trie_init();

    for (lastword = 0 ; lastword == 0 ; textp1 = textp2+1) {
	for (textp2 = textp1 ; *textp2 != '\n' && *textp2 != '\0'; textp2++) {
	}
	if (*textp2 == '\0') {
	    lastword = 1;
	    if (textp2 == textp1)
		break;
	}
        if (textp2-textp1 > 0) {
          // we need a non-const copy to add the '\0'
          int len = textp2-textp1;
          char *aux = malloc((len+1)*sizeof(char));
          memcpy(aux,textp1,len);
          aux[len]='\0';

          fsm_trie_add_word(th, aux);
          free(aux);
        } 
    }
    return(fsm_trie_done(th));
}