file_ext entirely.
+Sat Apr 15 19:36:47 2006 Ben Pfaff <blp@gnu.org>
+
+ Remove ill-considered file routines that are no longer used.
+
+ * filename.c: (fn_open_ext) Removed.
+ (fn_close_ext) Removed.
+
+ * filename.h: (struct file_ext) Removed.
+
Mon Apr 3 13:22:39 2006 Ben Pfaff <blp@gnu.org>
* variable.c (var_is_valid_name): Move declarations before code
else
return fclose (f);
}
-\f
-/* More extensive file handling. */
-
-/* File open routine that extends fn_open(). Opens or reopens a
- file according to the contents of file_ext F. Returns nonzero on
- success. If 0 is returned, errno is set to a sensible value. */
-int
-fn_open_ext (struct file_ext *f)
-{
- char *p;
-
- p = strstr (f->filename, "%d");
- if (p)
- {
- char *s = local_alloc (strlen (f->filename)
- + INT_STRLEN_BOUND (int) - 1);
- char *cp;
-
- memcpy (s, f->filename, p - f->filename);
- cp = spprintf (&s[p - f->filename], "%d", *f->sequence_no);
- strcpy (cp, &p[2]);
-
- if (f->file)
- {
- int error = 0;
-
- if (f->preclose)
- if (f->preclose (f) == 0)
- error = errno;
-
- if (EOF == fn_close (f->filename, f->file) || error)
- {
- f->file = NULL;
- local_free (s);
-
- if (error)
- errno = error;
-
- return 0;
- }
-
- f->file = NULL;
- }
-
- f->file = fn_open (s, f->mode);
- local_free (s);
-
- if (f->file && f->postopen)
- if (f->postopen (f) == 0)
- {
- int error = errno;
- fn_close (f->filename, f->file);
- errno = error;
-
- return 0;
- }
-
- return (f->file != NULL);
- }
- else if (f->file)
- return 1;
- else
- {
- f->file = fn_open (f->filename, f->mode);
-
- if (f->file && f->postopen)
- if (f->postopen (f) == 0)
- {
- int error = errno;
- fn_close (f->filename, f->file);
- errno = error;
-
- return 0;
- }
-
- return (f->file != NULL);
- }
-}
-
-/* Properly closes the file associated with file_ext F, if any.
- Return nonzero on success. If zero is returned, errno is set to a
- sensible value. */
-int
-fn_close_ext (struct file_ext *f)
-{
- if (f->file)
- {
- int error = 0;
-
- if (f->preclose)
- if (f->preclose (f) == 0)
- error = errno;
-
- if (EOF == fn_close (f->filename, f->file) || error)
- {
- f->file = NULL;
-
- if (error)
- errno = error;
-
- return 0;
- }
-
- f->file = NULL;
- }
- return 1;
-}
#ifdef unix
/* A file's identity. */
void fn_free_identity (struct file_identity *);
int fn_compare_file_identities (const struct file_identity *,
const struct file_identity *);
-\f
-/* Extended file routines. */
-struct file_ext;
-
-typedef int (*file_callback) (struct file_ext *);
-
-/* File callbacks may not return zero to indicate failure unless they
- set errno to a sensible value. */
-struct file_ext
- {
- char *filename; /* Filename. */
- const char *mode; /* Open mode, i.e, "wb". */
- FILE *file; /* File. */
- int *sequence_no; /* Page number, etc. */
- void *param; /* User data. */
- file_callback postopen; /* Called after FILE opened. */
- file_callback preclose; /* Called before FILE closed. */
- };
-
-int fn_open_ext (struct file_ext *file);
-int fn_close_ext (struct file_ext *file);
#endif /* filename_h */
+Sat Apr 15 19:38:13 2006 Ben Pfaff <blp@gnu.org>
+
+ Remove last users of struct file_ext so we can get rid of it
+ entirely.
+
+ * data-reader.c: (struct dfm_reader) Change file member from
+ struct file_ext to FILE *. Updated all references.
+ (dfm_close_reader) Close file with fn_close() instead of
+ fn_close_ext(). Also, make a copy of the file name from the file
+ handle before closing it, because we can't extract it after we
+ close the file.
+ (dfm_open_reader) Open file with fn_open() instead of
+ fn_open_ext().
+
+ * data-writer.c: (struct dfm_writer) Change file member
+ struct file_ext to FILE *. Updated all references.
+ (dfm_close_writer) Close file with fn_close() instead of
+ fn_close_ext(). Also, make a copy of the file name from the file
+ handle before closing it, because we can't extract it after we
+ close the file.
+ (dfm_open_writer) Open file with fn_open() instead of
+ fn_open_ext().
+
Sat Apr 15 18:00:32 2006 Ben Pfaff <blp@gnu.org>
* data-list.c: Add prototype to suppress warning for
struct string line; /* Current line. */
struct string scratch; /* Extra line buffer. */
enum dfm_reader_flags flags; /* Zero or more of DFM_*. */
- struct file_ext file; /* Associated file. */
+ FILE *file; /* Associated file. */
size_t pos; /* Offset in line of current character. */
unsigned eof_cnt; /* # of attempts to advance past EOF. */
};
{
int still_open;
bool is_inline;
+ char *file_name;
if (r == NULL)
return;
is_inline = r->fh == fh_inline_file ();
+ file_name = is_inline ? NULL : xstrdup (fh_get_filename (r->fh));
still_open = fh_close (r->fh, "data file", "rs");
- if (still_open)
- return;
-
- if (!is_inline)
+ if (still_open)
{
- fn_close_ext (&r->file);
- free (r->file.filename);
- r->file.filename = NULL;
+ free (file_name);
+ return;
}
+
+ if (!is_inline)
+ fn_close (file_name, r->file);
else
{
/* Skip any remaining data on the inline file. */
ds_destroy (&r->line);
ds_destroy (&r->scratch);
free (r);
+ free (file_name);
}
/* Opens the file designated by file handle FH for reading as a
{
r->where.filename = fh_get_filename (fh);
r->where.line_number = 0;
- r->file.file = NULL;
- r->file.filename = xstrdup (fh_get_filename (r->fh));
- r->file.mode = "rb";
- r->file.file = NULL;
- r->file.sequence_no = NULL;
- r->file.param = NULL;
- r->file.postopen = NULL;
- r->file.preclose = NULL;
- if (!fn_open_ext (&r->file))
+ r->file = fn_open (fh_get_filename (fh), "rb");
+ if (r->file == NULL)
{
msg (ME, _("Could not open \"%s\" for reading as a data file: %s."),
fh_get_filename (r->fh), strerror (errno));
bool
dfm_reader_error (const struct dfm_reader *r)
{
- return fh_get_referent (r->fh) == FH_REF_FILE && ferror (r->file.file);
+ return fh_get_referent (r->fh) == FH_REF_FILE && ferror (r->file);
}
/* Reads a record from the inline file into R.
if (fh_get_mode (r->fh) == FH_MODE_TEXT)
{
ds_clear (&r->line);
- if (!ds_gets (&r->line, r->file.file))
+ if (!ds_gets (&r->line, r->file))
{
- if (ferror (r->file.file))
+ if (ferror (r->file))
msg (ME, _("Error reading file %s: %s."),
fh_get_name (r->fh), strerror (errno));
return false;
if (ds_length (&r->line) < record_width)
ds_rpad (&r->line, record_width, 0);
- amt = fread (ds_c_str (&r->line), 1, record_width,
- r->file.file);
+ amt = fread (ds_c_str (&r->line), 1, record_width, r->file);
if (record_width != amt)
{
- if (ferror (r->file.file))
+ if (ferror (r->file))
msg (ME, _("Error reading file %s: %s."),
fh_get_name (r->fh), strerror (errno));
else if (amt != 0)
struct dfm_writer
{
struct file_handle *fh; /* File handle. */
- struct file_ext file; /* Associated file. */
+ FILE *file; /* Associated file. */
char *bounce; /* Bounce buffer for fixed-size fields. */
};
w = *aux = xmalloc (sizeof *w);
w->fh = fh;
- w->file.file = NULL;
+ w->file = fn_open (fh_get_filename (w->fh), "wb");
w->bounce = NULL;
- w->file.filename = xstrdup (fh_get_filename (w->fh));
- w->file.mode = "wb";
- w->file.file = NULL;
- w->file.sequence_no = NULL;
- w->file.param = NULL;
- w->file.postopen = NULL;
- w->file.preclose = NULL;
-
- if (!fn_open_ext (&w->file))
+ if (w->file == NULL)
{
msg (ME, _("An error occurred while opening \"%s\" for writing "
"as a data file: %s."),
bool
dfm_write_error (const struct dfm_writer *writer)
{
- return ferror (writer->file.file);
+ return ferror (writer->file);
}
/* Writes record REC having length LEN to the file corresponding to
len = rec_width;
}
- fwrite (rec, len, 1, w->file.file);
+ fwrite (rec, len, 1, w->file);
return !dfm_write_error (w);
}
bool
dfm_close_writer (struct dfm_writer *w)
{
+ char *file_name;
bool ok;
if (w == NULL)
return true;
+ file_name = xstrdup (fh_get_name (w->fh));
if (fh_close (w->fh, "data file", "ws"))
- return true;
+ {
+ free (file_name);
+ return true;
+ }
ok = true;
- if (w->file.file != NULL)
+ if (w->file != NULL)
{
- ok = !dfm_write_error (w);
- if (!fn_close_ext (&w->file))
- ok = false;
+ ok = !dfm_write_error (w) && !fn_close (file_name, w->file);
if (!ok)
- msg (ME, _("I/O error occurred writing data file \"%s\"."),
- fh_get_filename (w->fh));
-
- free (w->file.filename);
- w->file.filename = NULL;
+ msg (ME, _("I/O error occurred writing data file \"%s\"."), file_name);
}
free (w->bounce);
free (w);
+ free (file_name);
return ok;
}