/** * @brief Create a new trie * * @param alpha_map : the alphabet set for the trie * * @return a pointer to the newly created trie, NULL on failure * * Create a new empty trie object based on the given @a alpha_map alphabet * set. The trie contents can then be added and deleted with trie_store() and * trie_delete() respectively. * * The created object must be freed with trie_free(). */ Trie * trie_new (const AlphaMap *alpha_map) { Trie *trie; trie = (Trie *) malloc (sizeof (Trie)); if (!trie) return NULL; trie->alpha_map = alpha_map_clone (alpha_map); if (!trie->alpha_map) goto exit_trie_created; trie->da = da_new (); if (!trie->da) goto exit_alpha_map_created; trie->tail = tail_new (); if (!trie->tail) goto exit_da_created; trie->is_dirty = TRUE; return trie; exit_da_created: da_free (trie->da); exit_alpha_map_created: alpha_map_free (trie->alpha_map); exit_trie_created: free (trie); return NULL; }
Prgrph read_prgrph(FILE *file) { if ( !file ) return prgrph_error; const int buffsize = 81; // 80 char + \n + \0 char *buff = malloc((buffsize + 1) * sizeof(char)); if ( !buff ) { perror("read_plain()"); return prgrph_error; } do { if ( fgets(buff, buffsize + 1, file) == NULL ) { perror("read_prgrph()"); free(buff); return prgrph_error; } else if ( strchr(buff,'\n') == NULL ) { fputs("read_prgrph(): A line must not exceed 80 characters.\n", stderr); free(buff); return prgrph_error; } } while ( buff[0] == '!' ); Darray *da = da_new(sizeof(char *)); do { int linelen = strlen(buff); if ( linelen == buffsize && buff[buffsize-1] != '\n' ) { fputs("read_prgrph(): A line must not exceed 80 characters\n", stderr); Prgrph cancel_p; cancel_p.prgrph = da_unpack(da, &cancel_p.m); free_prgrph(cancel_p); return prgrph_error; } buff[linelen-1] = '\0'; da_push(da, &buff); buff = malloc((buffsize + 1) * sizeof(char)); if ( !buff ) { perror("read_prgrph()"); return prgrph_error; } } while ( fgets(buff, buffsize + 1, file) != NULL ); free(buff); Prgrph prgrph; prgrph.prgrph = da_unpack(da, &prgrph.m); return prgrph; }