int putl_category(const char *s, FILE *f) { if (s && fputs(s, f) == EOF) { errf(strerror(errno)); delete_category(f); return (EOF); } if (fputc('\n', f) == EOF) { errf(strerror(errno)); delete_category(f); return (EOF); } return (0); }
void close_category(FILE *f) { if (fchmod(fileno(f), 0644) < 0 || fclose(f) != 0) { errf(strerror(errno)); delete_category(f); } if (verbose) { (void) fprintf(stdout, _("done.\n")); (void) fflush(stdout); } }
int main() { int i; char chose[MAXLEN]; struct cell_f **f; struct cell_c **c; struct cell_f *head_f = NULL; struct cell_c *head_c = NULL; struct cell_f *h_f = NULL; FILE *fp_f, *fp_c; printf("Welcome!\n"); if ((fp_f = fopen("original_file.dat", "r")) == NULL) { fprintf(stderr, "Cannot open file: original_filet.dat\n"); return; } if ((fp_c = fopen("category_list.dat", "r")) == NULL) { fprintf(stderr, "Cannot open file: category_list.dat\n"); return; } f = make_original_file_list(&head_f, fp_f); c = make_original_category_list(&head_c, fp_c); i = 0; while(i != 1) { printf("機能一覧を表示します。\n"); printf("0: 新規ファイルの登録 \n1: 既存ファイルの削除 \n2: 任意のファイル、任意の項目への修正 \n3: 既存のファイルの検索 \n4: 新たな分類項目の追加 \n5: 既存の分類項目の削除 \n6: 任意のファイルのコメント抽出\n"); printf(">"); scanf("%s", chose); if(strlen(chose) > 1) { printf("入力エラーです。\n"); continue; } switch (chose[0]) { case '0': insert_cell_file(c, f); break; case '1': delete_file(f); break; case '2': rename_file(f); break; case '3': search_f(&h_f, f); delete_cell(&h_f); break; case '4': insert_cell_category(c); break; case '5': delete_category(c); break; case '6': sub_function(f); break; default: printf("入力エラーです。\n"); break; } printf("アプリケーションの実行を続けますか?\n"); printf("0: Yes 1: No\n"); printf(">"); scanf("%d", &i); while(i != 0 && i != 1) { printf("アプリケーションの実行を続けますか?\n"); printf("0: Yes 1: No\n"); printf(">"); scanf("%d", &i); } } fclose(fp_f); fclose(fp_c); if ((fp_f = fopen("original_file.dat", "w")) == NULL) { fprintf(stderr, "Cannot open file: original_filet.dat\n"); return; } if ((fp_c = fopen("category_list.dat", "w")) == NULL) { fprintf(stderr, "Cannot open file: original_filet.dat\n"); return; } while((*f) != NULL) { fprintf(fp_f, "%s %s %s %s %d\n", (*f)->main_title, (*f)->sub_title, (*f)->date, (*f)->path, (*f)->category); (*f) = (*f)->next; } while((*c) != NULL) { fprintf(fp_c, "%d %s\n", (*c)->number, (*c)->category); (*c) = (*c)->next; } fclose(fp_f); fclose(fp_c); return 0; }
void dump_collate(void) { FILE *f; int i, j, n; size_t sz; int32_t pri; collelem_t *ce; collchar_t *cc; subst_t *sb; char vers[COLLATE_STR_LEN]; collate_char_t chars[UCHAR_MAX + 1]; collate_large_t *large; collate_subst_t *subst[COLL_WEIGHTS_MAX]; collate_chain_t *chain; /* * We have to run throught a preliminary pass to identify all the * weights that we use for each sorting level. */ for (i = 0; i < NUM_WT; i++) { add_weight(pri_ignore, i); } for (i = 0; i < NUM_WT; i++) { for (sb = avl_first(&substs[i]); sb; sb = AVL_NEXT(&substs[i], sb)) { for (j = 0; sb->ref[j]; j++) { add_weight(sb->ref[j], i); } } } for (ce = avl_first(&elem_by_expand); ce != NULL; ce = AVL_NEXT(&elem_by_expand, ce)) { add_weights(ce->ref); } for (cc = avl_first(&collchars); cc; cc = AVL_NEXT(&collchars, cc)) { add_weights(cc->ref); } /* * Now we walk the entire set of weights, removing the gaps * in the weights. This gives us optimum usage. The walk * occurs in priority. */ for (i = 0; i < NUM_WT; i++) { weight_t *w; for (w = avl_first(&weights[i]); w; w = AVL_NEXT(&weights[i], w)) { w->opt = nweight[i]; nweight[i] += 1; } } (void) memset(&chars, 0, sizeof (chars)); (void) memset(vers, 0, COLLATE_STR_LEN); (void) strlcpy(vers, COLLATE_VERSION, sizeof (vers)); /* * We need to make sure we arrange for the UNDEFINED field * to show up. Also, set the total weight counts. */ for (i = 0; i < NUM_WT; i++) { if (resolve_pri(pri_undefined[i]) == -1) { set_pri(pri_undefined[i], -1, RESOLVED); /* they collate at the end of everything else */ collinfo.undef_pri[i] = COLLATE_MAX_PRIORITY; } collinfo.pri_count[i] = nweight[i]; } collinfo.pri_count[NUM_WT] = max_wide(); collinfo.undef_pri[NUM_WT] = COLLATE_MAX_PRIORITY; collinfo.directive[NUM_WT] = DIRECTIVE_UNDEFINED; /* * Ordinary character priorities */ for (i = 0; i <= UCHAR_MAX; i++) { if ((cc = get_collchar(i, 0)) != NULL) { for (j = 0; j < NUM_WT; j++) { chars[i].pri[j] = get_weight(cc->ref[j], j); } } else { for (j = 0; j < NUM_WT; j++) { chars[i].pri[j] = get_weight(pri_undefined[j], j); } /* * Per POSIX, for undefined characters, we * also have to add a last item, which is the * character code. */ chars[i].pri[NUM_WT] = i; } } /* * Substitution tables */ for (i = 0; i < NUM_WT; i++) { collate_subst_t *st = NULL; n = collinfo.subst_count[i] = avl_numnodes(&substs[i]); if ((st = calloc(sizeof (collate_subst_t) * n, 1)) == NULL) { errf(_("out of memory")); return; } n = 0; for (sb = avl_first(&substs[i]); sb; sb = AVL_NEXT(&substs[i], sb)) { if ((st[n].key = resolve_pri(sb->key)) < 0) { /* by definition these resolve! */ INTERR; } if (st[n].key != (n | COLLATE_SUBST_PRIORITY)) { INTERR; } for (j = 0; sb->ref[j]; j++) { st[n].pri[j] = get_weight(sb->ref[j], i); } n++; } if (n != collinfo.subst_count[i]) INTERR; subst[i] = st; } /* * Chains, i.e. collating elements */ collinfo.chain_count = avl_numnodes(&elem_by_expand); chain = calloc(sizeof (collate_chain_t), collinfo.chain_count); if (chain == NULL) { errf(_("out of memory")); return; } for (n = 0, ce = avl_first(&elem_by_expand); ce != NULL; ce = AVL_NEXT(&elem_by_expand, ce), n++) { (void) wsncpy(chain[n].str, ce->expand, COLLATE_STR_LEN); for (i = 0; i < NUM_WT; i++) { chain[n].pri[i] = get_weight(ce->ref[i], i); } } if (n != collinfo.chain_count) INTERR; /* * Large (> UCHAR_MAX) character priorities */ large = calloc(sizeof (collate_large_t) * avl_numnodes(&collchars), 1); if (large == NULL) { errf(_("out of memory")); return; } i = 0; for (cc = avl_first(&collchars); cc; cc = AVL_NEXT(&collchars, cc)) { int undef = 0; /* we already gathered those */ if (cc->wc <= UCHAR_MAX) continue; for (j = 0; j < NUM_WT; j++) { if ((pri = get_weight(cc->ref[j], j)) < 0) { undef = 1; } if (undef && (pri >= 0)) { /* if undefined, then all priorities are */ INTERR; } else { large[i].pri.pri[j] = pri; } } if (!undef) { large[i].val = cc->wc; collinfo.large_count = i++; } } if ((f = open_category()) == NULL) { return; } /* Time to write the entire data set out */ if ((wr_category(vers, COLLATE_STR_LEN, f) < 0) || (wr_category(&collinfo, sizeof (collinfo), f) < 0) || (wr_category(&chars, sizeof (chars), f) < 0)) { delete_category(f); return; } for (i = 0; i < NUM_WT; i++) { sz = sizeof (collate_subst_t) * collinfo.subst_count[i]; if (wr_category(subst[i], sz, f) < 0) { delete_category(f); return; } } sz = sizeof (collate_chain_t) * collinfo.chain_count; if (wr_category(chain, sz, f) < 0) { delete_category(f); return; } sz = sizeof (collate_large_t) * collinfo.large_count; if (wr_category(large, sz, f) < 0) { delete_category(f); return; } close_category(f); }