X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fdata-io%2Fdata-writer.c;h=f38bc6896d94d96f6468d8e8cec8476091b13190;hb=f5d1ac814687386b4cd6af9fa9e6f2f98913aa7d;hp=5270db0e8119f86236ba5e64e68a510b8cf6840c;hpb=2814862a2c45a39f9822cf4c64ca3884822d064d;p=pspp diff --git a/src/language/data-io/data-writer.c b/src/language/data-io/data-writer.c index 5270db0e81..f38bc6896d 100644 --- a/src/language/data-io/data-writer.c +++ b/src/language/data-io/data-writer.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-2004, 2006, 2010, 2011, 2012 Free Software Foundation, Inc. + Copyright (C) 1997-2004, 2006, 2010, 2011, 2012, 2013 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -49,8 +49,10 @@ struct dfm_writer FILE *file; /* Associated file. */ struct replace_file *rf; /* Atomic file replacement support. */ char *encoding; /* Encoding. */ + enum fh_line_ends line_ends; /* Line ends for text files. */ int unit; /* Unit width, in bytes. */ + char cr[MAX_UNIT]; /* \r in encoding, 'unit' bytes long. */ char lf[MAX_UNIT]; /* \n in encoding, 'unit' bytes long. */ char spaces[32]; /* 32 bytes worth of ' ' in encoding. */ }; @@ -90,10 +92,11 @@ dfm_open_writer (struct file_handle *fh, const char *encoding) w = xmalloc (sizeof *w); w->fh = fh_ref (fh); w->lock = lock; - w->rf = replace_file_start (fh_get_file_name (w->fh), "wb", 0666, - &w->file, NULL); + w->rf = replace_file_start (w->fh, "wb", 0666, &w->file); w->encoding = xstrdup (encoding); + w->line_ends = fh_get_line_ends (fh); w->unit = ei.unit; + memcpy (w->cr, ei.cr, sizeof w->cr); memcpy (w->lf, ei.lf, sizeof w->lf); for (ofs = 0; ofs + ei.unit <= sizeof w->spaces; ofs += ei.unit) memcpy (&w->spaces[ofs], ei.space, ei.unit); @@ -111,7 +114,7 @@ dfm_open_writer (struct file_handle *fh, const char *encoding) return w; } -/* Returns false if an I/O error occurred on WRITER, true otherwise. */ +/* Returns true if an I/O error occurred on WRITER, false otherwise. */ bool dfm_write_error (const struct dfm_writer *writer) { @@ -134,6 +137,8 @@ dfm_put_record (struct dfm_writer *w, const char *rec, size_t len) { case FH_MODE_TEXT: fwrite (rec, len, 1, w->file); + if (w->line_ends == FH_END_CRLF) + fwrite (w->cr, w->unit, 1, w->file); fwrite (w->lf, w->unit, 1, w->file); break; @@ -198,6 +203,25 @@ dfm_put_record (struct dfm_writer *w, const char *rec, size_t len) return !dfm_write_error (w); } +/* Writes record REC (which need not be null-terminated) having length LEN to + the file corresponding to HANDLE. REC is encoded in UTF-8, which this + function recodes to the correct encoding for W before writing. Adds any + needed formatting, such as a trailing new-line. Returns true on success, + false on failure. */ +bool +dfm_put_record_utf8 (struct dfm_writer *w, const char *rec, size_t len) +{ + if (is_encoding_utf8 (w->encoding)) + return dfm_put_record (w, rec, len); + else + { + char *recoded = recode_string (w->encoding, UTF8, rec, len); + bool ok = dfm_put_record (w, recoded, strlen (recoded)); + free (recoded); + return ok; + } +} + /* Closes data file writer W. */ bool dfm_close_writer (struct dfm_writer *w) @@ -213,7 +237,7 @@ dfm_close_writer (struct dfm_writer *w) if (w->file != NULL) { const char *file_name = fh_get_file_name (w->fh); - ok = !dfm_write_error (w) && !fn_close (file_name, w->file); + ok = !dfm_write_error (w) && !fn_close (w->fh, w->file); if (!ok) msg (ME, _("I/O error occurred writing data file `%s'."), file_name);