/*======================================+ * set_output_file -- Open specified output file * (Closes any previously open output file) * Calls msg_error & returns FALSE, if problem *=====================================*/ static BOOLEAN set_output_file (STRING outfname, BOOLEAN append) { STRING modestr = append ? LLAPPENDTEXT:LLWRITETEXT; STRING rptdir=0; if (Poutfp) { finishrassa(); fclose(Poutfp); Poutfp = NULL; } rptdir = getlloptstr("LLREPORTS", "."); Poutfp = fopenpath(outfname, modestr, rptdir, NULL, uu8, NULL); if (!Poutfp) { /* TODO: need to forward this through rptui */ msg_error(_("Could not open file %s"), outfname); return FALSE; } /* if appending to existing non-empty file, don't add BOM */ if (append) { long offset = 0; fseek(Poutfp, 0, SEEK_END); offset = ftell(Poutfp); if (offset > 0) return TRUE; } prefix_file_for_report(Poutfp); return TRUE; }
static FILE * ask_for_file_worker (STRING mode, STRING ttl, STRING *pfname, STRING *pfullpath, STRING path, STRING ext, DIRECTION direction) { FILE *fp; char prompt[MAXPATHLEN]; char fname[MAXPATHLEN]; int elen, flen; BOOLEAN rtn; make_fname_prompt(prompt, sizeof(prompt), ext); if (direction==INPUT) rtn = ask_for_input_filename(ttl, path, prompt, fname, sizeof(fname)); else rtn = ask_for_output_filename(ttl, path, prompt, fname, sizeof(fname)); if (pfname) { if (fname && fname[0]) *pfname = strdup(fname); else *pfname = 0; } if (pfullpath) *pfullpath = 0; /* 0 indicates we didn't try to open */ if (!rtn || !fname[0]) return NULL; if (!expand_special_fname_chars(fname, sizeof(fname), uu8)) { msg_error(_(qSfn2long)); return NULL; } ask_for_file_try: /* try name as given */ if (ISNULL(path)) { /* bare filename was given */ if ((fp = fopen(fname, mode)) != NULL) { if (pfname) strupdate(pfname, fname); return fp; } } else { /* fully qualified path was given */ if ((fp = fopenpath(fname, mode, path, ext, uu8, pfullpath)) != NULL) { return fp; } } /* try default extension */ if (ext) { elen = strlen(ext); flen = strlen(fname); if (elen<flen && path_match(fname+flen-elen, ext)) { ext = NULL; /* the file name has the extension already */ } else { /* add extension and go back and retry */ llstrapps(fname, sizeof(fname), uu8, ext); ext = NULL; /* only append extension once! */ goto ask_for_file_try; } } /* failed to open it, give up */ msg_error(_(qSnofopn), fname); return NULL; }