void Tagger::Clear() { /* Write c_new arrays */ if (odictfile != NULL) { if (Option(verbose)) printf("Writing dictionary (%d entries)\n", dict.size); write_dict(odictfile, &dict, Option(training) || Option(reestimate)); fclose(odictfile); } if (otranfile != NULL) { if (Option(verbose)) printf("Writing transitions\n"); if (Option(training) || Option(reestimate)) write_ascii_trans(otranfile, &c_newtrans); else write_ascii_trans(otranfile, &trans); fclose(otranfile); } if (Option(unknown_morph)) { if (obadwordname[0] != 0) fclose(features->obadwordfile); } free_dict( &dict ); if ( Option(bdbm)) /* Berkeley DB: close it */ dbp->close(dbp,(u_int32_t)0); }
void free_dict(struct dict *dict) { if (dict == NULL) return; for (int i=0; i < dict->used; i++) { struct dict_node *node = dict->nodes[i]; if (! dict->marked) node->mark = node->entry; while (node->mark != NULL) { struct dict_entry *del = node->mark; node->mark = del->next; free_skel(del->skel); free_dict(del->dict); free(del); } free(node->key); FREE(node); } FREE(dict->nodes); FREE(dict); }
int main( int argc, char** argv ){ XRHash * dict = xr_init_hash( xr_hash__strhash, xr_hash__strcmp ); /* add things to the hash */ if ( xr_hash_contains( dict, "mary" ) != XRHASH_EXISTS_TRUE ) printf("does not contain mary\n"); xr_hash_add( dict, "fred", strdup("my name is fred") ); xr_hash_add( dict, "mary", strdup("my name is mary") ); xr_hash_add( dict, "sally", strdup("my name is sally") ); xr_hash_add( dict, "bob", strdup("my name is bob") ); if ( xr_hash_contains( dict, "mary" ) == XRHASH_EXISTS_TRUE ) printf("does contain mary\n"); /* iterate the hash */ xrhash_fast_iterator * iter = xr_init_fasthashiterator( dict ); char * hashkey = NULL; do { hashkey = (char*) xr_hash_fastiteratekey( iter ); if ( hashkey != NULL ){ char * data; xr_hash_get( dict, (void*)hashkey, (void**)&data ); printf("key = %s, value = %s\n", hashkey, data ); } else { break; } } while ( 1 ); xr_hash_fastiterator_free( iter ); free_dict( dict ); return 0; }
void free_some_mem( void ) { if( token_buf != NULL ) { mem_free( token_buf ); } if( alt_ext != NULL ) { mem_free( alt_ext ); } if( def_ext != NULL ) { mem_free( def_ext ); } if( master_fname != NULL ) { mem_free( master_fname ); } if( master_fname_attr != NULL ) { mem_free( master_fname_attr ); } if( dev_name != NULL ) { mem_free( dev_name ); } if( lay_files != NULL ) { laystack * lwk; while( lay_files != NULL ) { lwk = lay_files->next; mem_free( lay_files ); lay_files = lwk; } } if( out_file != NULL ) { mem_free( out_file ); } if( out_file_attr != NULL ) { mem_free( out_file_attr ); } if( global_dict != NULL ) { free_dict( &global_dict ); } if( macro_dict != NULL ) { free_macro_dict( ¯o_dict ); } if( ref_dict != NULL ) { free_ref_dict( &ref_dict ); } if( fig_dict != NULL ) { free_ref_dict( &fig_dict ); } if( fn_dict != NULL ) { free_ref_dict( &fn_dict ); } if( tag_dict != NULL ) { free_tag_dict( &tag_dict ); } if( index_dict != NULL ) { free_index_dict( &index_dict ); } if( buff2 != NULL ) { mem_free( buff2 ); } if( workbuf != NULL ) { mem_free( workbuf ); } if( t_line != NULL ) { add_text_chars_to_pool( t_line ); add_text_line_to_pool( t_line ); } if( t_element != NULL ) { clear_doc_element( t_element ); } if( t_page.top_ban != NULL ) { if( t_page.top_ban->first != NULL ) { clear_doc_element( t_page.top_ban->first ); add_doc_el_to_pool( t_page.top_ban->first ); } add_ban_col_to_pool( t_page.top_ban ); } if( t_page.page_width != NULL ) { clear_doc_element( t_page.page_width ); add_doc_el_to_pool( t_page.page_width ); } if( t_page.main != NULL ) { if( t_page.main->main != NULL ) { clear_doc_element( t_page.main->main ); add_doc_el_to_pool( t_page.main->main ); } if( t_page.main->bot_fig != NULL ) { clear_doc_element( t_page.main->bot_fig ); add_doc_el_to_pool( t_page.main->bot_fig ); } if( t_page.main->footnote != NULL ) { clear_doc_element( t_page.main->footnote ); add_doc_el_to_pool( t_page.main->footnote ); } add_doc_col_to_pool( t_page.main ); } if( t_page.bot_ban != NULL ) { if( t_page.bot_ban->first != NULL ) { clear_doc_element( t_page.bot_ban->first ); add_doc_el_to_pool( t_page.bot_ban->first ); } add_ban_col_to_pool( t_page.bot_ban ); } if( n_page.col_top != NULL ) { clear_doc_element( n_page.col_top ); add_doc_el_to_pool( n_page.col_top ); } if( n_page.col_main != NULL ) { clear_doc_element( n_page.col_main ); add_doc_el_to_pool( n_page.col_main ); } if( n_page.col_bot != NULL ) { clear_doc_element( n_page.col_bot ); add_doc_el_to_pool( n_page.col_bot ); } if( n_page.col_fn != NULL ) { clear_doc_element( n_page.col_fn ); add_doc_el_to_pool( n_page.col_fn ); } free_layout_banner(); free_pool_storage(); }
int write_file (struct wnn_file *wf, char *n) { FILE *fp; int mode = 3; struct wnn_file_head fh; #ifdef WRITE_CHECK char *tmp = NULL, *backup = NULL; check_backup (n); #endif if ((fp = fopen (n, "r")) != NULL) { /* Old File Exist */ if (input_file_header (fp, &fh) == -1) { wnn_errorno = WNN_NOT_A_FILE; fclose (fp); return (-1); } mode = must_write_file (wf, &(fh.file_uniq)); fclose (fp); if (mode == -1) return -1; } if (mode == 0) { return (0); /* Need Not Write */ } else if (mode == 1 || mode == 3) { /* 3 when the file is not the one to be read. */ #ifdef WRITE_CHECK backup = make_backup_file (n); if ((tmp = make_tmp_file (n, 0, &fp)) == NULL) { delete_tmp_file (backup); #else /* WRITE_CHECK */ if ((fp = fopen (n, "w+")) == NULL) { #endif /* WRITE_CHECK */ wnn_errorno = WNN_FILE_WRITE_ERROR; return (-1); } } else if (mode == 2) { #ifdef WRITE_CHECK backup = make_backup_file (n); if ((tmp = make_tmp_file (n, 1, &fp)) == NULL) { delete_tmp_file (backup); #else /* WRITE_CHECK */ if ((fp = fopen (n, "r+")) == NULL) { /* New File */ #endif /* WRITE_CHECK */ wnn_errorno = WNN_FILE_WRITE_ERROR; return (-1); } } if (write_file_real (wf, fp, mode) == -1) { fclose (fp); #ifdef WRITE_CHECK delete_tmp_file (tmp); delete_tmp_file (backup); #endif /* WRITE_CHECK */ return -1; } fclose (fp); #ifdef WRITE_CHECK move_tmp_to_org (tmp, n, 1); delete_tmp_file (backup); #endif /* WRITE_CHECK */ if ((mode == 1) || (mode == 2)) { clear_dirty_bit (wf); } return (0); } static int write_file_real (struct wnn_file *wf, FILE *fp, int mode /* 1 For All, 2 For only hindo */ ) { struct wnn_file_head fh; if (fp) rewind (fp); bcopy ((char *) &wf->f_uniq, (char *) &(fh.file_uniq), WNN_F_UNIQ_LEN); bcopy ((char *) &wf->f_uniq_org, (char *) &(fh.file_uniq_org), WNN_F_UNIQ_LEN); bcopy (wf->passwd, fh.file_passwd, WNN_PASSWD_LEN); fh.file_type = wf->file_type; if (output_file_header (fp, &fh) == -1) { wnn_errorno = WNN_FILE_WRITE_ERROR; goto ERROR_RET; } switch (fh.file_type) { case WNN_FT_DICT_FILE: { struct JT *jt2; struct JT *jt = (struct JT *) wf->area; if (little_endian () && jt->dirty) { if ((jt2 = copy_dict ((struct JT *) wf->area)) == NULL) goto ERROR_RET; revdic (jt2, 1); if (writedict (jt2, fp) == -1) goto ERROR_RET; jt2 = free_dict (jt2); } else { /* if(writedict(wf->area, fp) == -1)goto ERROR_RET; */ if (mode == 2) { if (write_hindo_of_dict (wf->area, fp) == -1) goto ERROR_RET; } else { if (writedict (wf->area, fp) == -1) goto ERROR_RET; } } } break; case WNN_FT_HINDO_FILE: if (writehindo (wf->area, fp) == -1) goto ERROR_RET; break; case WNN_FT_FUZOKUGO_FILE: wnn_errorno = NOT_SUPPORTED_OPERATION; goto ERROR_RET; } return (0); ERROR_RET: return (-1); } static int writedict (struct JT *jt1, FILE *fp) { if (output_header_jt (fp, jt1) == -1) return (-1); #ifdef WRITE_CHECK if ((vfwrite (jt1->comment, 2, jt1->maxcomment, fp) == -1) || (vfwrite (jt1->hinsi_list, 2, jt1->maxhinsi_list, fp) == -1) || (vfwrite (jt1->hindo, 1, jt1->maxserial, fp) == -1) || (vfwrite (jt1->hinsi, 2, jt1->maxserial, fp) == -1)) return (-1); #ifdef CONVERT_with_SiSheng if (jt1->syurui == CWNN_REV_DICT) /* for Chinese PinYin dic only */ if (vfwrite (jt1->sisheng, 2, jt1->maxserial, fp) == -1) return (-1); #endif /* CONVERT_with_SiSheng */ if ((vfwrite (jt1->kanji, 1, jt1->maxkanji, fp) == -1) || (vfwrite (jt1->table, sizeof (struct uind1), jt1->maxtable, fp) == -1) || (vfwrite (jt1->ri1[D_YOMI], sizeof (struct rind1), jt1->maxri1[D_YOMI], fp) == -1) || (vfwrite (jt1->ri1[D_KANJI], sizeof (struct rind1), jt1->maxri1[D_KANJI], fp) == -1) || (vfwrite (jt1->hontai, 1, jt1->maxhontai, fp) == -1) || (vfwrite (jt1->ri2, sizeof (struct rind2), jt1->maxri2, fp) == -1)) return (-1); #else /* WRITE_CHECK */ vfwrite (jt1->comment, 2, jt1->maxcomment, fp); vfwrite (jt1->hinsi_list, 2, jt1->maxhinsi_list, fp); vfwrite (jt1->hindo, 1, jt1->maxserial, fp); vfwrite (jt1->hinsi, 2, jt1->maxserial, fp); #ifdef CONVERT_with_SiSheng if (jt1->syurui == CWNN_REV_DICT) /* for Chinese PinYin dic only */ vfwrite (jt1->sisheng, 2, jt1->maxserial, fp); #endif /* CONVERT_with_SiSheng */ vfwrite (jt1->kanji, 1, jt1->maxkanji, fp); vfwrite (jt1->table, sizeof (struct uind1), jt1->maxtable, fp); vfwrite (jt1->ri1[D_YOMI], sizeof (struct rind1), jt1->maxri1[D_YOMI], fp); vfwrite (jt1->ri1[D_KANJI], sizeof (struct rind1), jt1->maxri1[D_KANJI], fp); vfwrite (jt1->hontai, 1, jt1->maxhontai, fp); vfwrite (jt1->ri2, sizeof (struct rind2), jt1->maxri2, fp); #endif /* WRITE_CHECK */ return (0); } static int write_hindo_of_dict (struct JT *jt1, FILE *fp) { if (output_header_jt (fp, jt1) == -1) return (-1); #ifdef WRITE_CHECK if ((vfwrite (jt1->comment, 2, jt1->maxcomment, fp) == -1) || (vfwrite (jt1->hindo, 1, jt1->maxserial, fp) == -1)) return (-1); #else /* WRITE_CHECK */ vfwrite (jt1->comment, 2, jt1->maxcomment, fp); vfwrite (jt1->hindo, 1, jt1->maxserial, fp); #endif /* WRITE_CHECK */ return (0); } int discardfile (struct wnn_file *wf) { #ifdef nodef FILE *fp; if (wf->localf == LOCAL) { if ((fp = fopen (wf->name, "r")) == NULL) { log_err ("discardfile:No file %s.", wf->name); return (-1); } fclose (fp); } #endif switch (wf->file_type) { case WNN_FT_DICT_FILE: wf->area = free_dict (wf->area); break; case WNN_FT_HINDO_FILE: wf->area = free_hindo (wf->area); break; case WNN_FT_FUZOKUGO_FILE: /* fzk_discard(wf->area); */ break; } return (0); }
static struct JT * readdict (FILE *fp) { struct JT *jt1; long x; jt1 = (struct JT *) malloc (sizeof (struct JT)); jt1->node = 0; if (input_header_jt (fp, jt1) == -1) { wnn_errorno = WNN_NOT_A_FILE; free (jt1); return (NULL); } if (jt1->syurui == WNN_UD_DICT) { jt1->bufsize_serial = (jt1->maxserial + MAXSERIAL); jt1->bufsize_kanji = (jt1->maxkanji + MAXKANJI); jt1->bufsize_hontai = (jt1->maxhontai + MAXHONTAI); jt1->bufsize_table = (jt1->maxtable + MAXTABLE); jt1->bufsize_ri1[D_YOMI] = 0; jt1->bufsize_ri1[D_KANJI] = 0; #if defined(CONVERT_by_STROKE) || defined(CONVERT_with_SiSheng) } else if ((jt1->syurui & 0xff) == WNN_REV_DICT) { #else } else if (jt1->syurui == WNN_REV_DICT) { #endif /* CONVERT_by_STROKE || CONVERT_with_SiSheng */ jt1->bufsize_serial = (jt1->maxserial + MAXSERIAL); jt1->bufsize_kanji = (jt1->maxkanji + MAXKANJI); jt1->bufsize_hontai = (jt1->maxhontai + MAXHONTAI); jt1->bufsize_table = 0; jt1->bufsize_ri1[D_YOMI] = (jt1->maxri1[D_YOMI] + MAXTABLE); jt1->bufsize_ri1[D_KANJI] = (jt1->maxri1[D_KANJI] + MAXTABLE); } else if (jt1->syurui == WNN_STATIC_DICT) { /* WNN_STATIC_DICT */ jt1->bufsize_serial = jt1->maxserial; jt1->bufsize_kanji = jt1->maxkanji; jt1->bufsize_hontai = jt1->maxhontai; jt1->bufsize_table = 0; jt1->bufsize_ri1[D_YOMI] = 0; jt1->bufsize_ri1[D_KANJI] = 0; } else { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); free (jt1); return (NULL); } if (alloc_dict (jt1) == -1) { free (jt1); return (NULL); } if (vfread (jt1->comment, 2, jt1->maxcomment, fp) != jt1->maxcomment) { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); goto error; } if (vfread (jt1->hinsi_list, 2, jt1->maxhinsi_list, fp) != jt1->maxhinsi_list) { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); goto error; } if (vfread (jt1->hindo, 1, jt1->maxserial, fp) != jt1->maxserial) { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); goto error; } if (vfread (jt1->hinsi, 2, jt1->maxserial, fp) != jt1->maxserial) { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); goto error; } #ifdef CONVERT_with_SiSheng if (jt1->syurui == CWNN_REV_DICT) if (vfread (jt1->sisheng, 2, jt1->maxserial, fp) != jt1->maxserial) { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); goto error; } #endif /* CONVERT_with_SiSheng */ if (vfread (jt1->kanji, 1, jt1->maxkanji, fp) != jt1->maxkanji) { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); goto error; } if (vfread (jt1->table, sizeof (struct uind1), jt1->maxtable, fp) != jt1->maxtable) { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); goto error; } if (vfread (jt1->ri1[D_YOMI], sizeof (struct rind1), jt1->maxri1[D_YOMI], fp) != jt1->maxri1[D_YOMI]) { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); goto error; } if (vfread (jt1->ri1[D_KANJI], sizeof (struct rind1), jt1->maxri1[D_KANJI], fp) != jt1->maxri1[D_KANJI]) { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); goto error; } if (vfread (jt1->hontai, 1, jt1->maxhontai, fp) != jt1->maxhontai) { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); goto error; } if (vfread (jt1->ri2, sizeof (struct rind2), jt1->maxri2, fp) != jt1->maxri2) { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); goto error; } if (fp != NULL) { x = ftell (fp); fseek (fp, 0, 2); if (x != ftell (fp)) { wnn_errorno = WNN_NOT_A_DICT; log_err ("not a correct dictionary."); goto error; } } make_hinsi_list (jt1); if (jt1->maxhontai == 0 && (jt1->syurui == WNN_UD_DICT || jt1->syurui == WNN_STATIC_DICT)) { jt1->maxhontai = 4; } if (little_endian ()) { revdic (jt1, 0); } jt1->dirty = 0; jt1->hdirty = 0; #ifdef CONVERT_by_STROKE if (jt1->syurui == BWNN_REV_DICT) if ((jt1->max_bnode = create_b_index (jt1)) == -1) { goto error; } #endif /* CONVERT_by_STROKE */ return (jt1); error: jt1 = free_dict (jt1); return (NULL); }