Change enum legacy_encoding to const char *.
[pspp-builds.git] / src / language / data-io / data-writer.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-2004, 2006 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <language/data-io/data-writer.h>
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <sys/stat.h>
26
27 #include <data/file-name.h>
28 #include <data/make-file.h>
29 #include <language/data-io/file-handle.h>
30 #include <libpspp/assertion.h>
31 #include <libpspp/integer-format.h>
32 #include <libpspp/message.h>
33 #include <libpspp/str.h>
34
35 #include "minmax.h"
36 #include "xalloc.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40 #define N_(msgid) (msgid)
41
42 /* Data file writer. */
43 struct dfm_writer
44   {
45     struct file_handle *fh;     /* File handle. */
46     struct fh_lock *lock;       /* Exclusive access to file. */
47     FILE *file;                 /* Associated file. */
48     struct replace_file *rf;    /* Atomic file replacement support. */
49   };
50
51 /* Opens a file handle for writing as a data file. */
52 struct dfm_writer *
53 dfm_open_writer (struct file_handle *fh)
54 {
55   struct dfm_writer *w;
56   struct fh_lock *lock;
57
58   lock = fh_lock (fh, FH_REF_FILE, N_("data file"), FH_ACC_WRITE, false);
59   if (lock == NULL)
60     return NULL;
61
62   w = fh_lock_get_aux (lock);
63   if (w != NULL)
64     return w;
65
66   w = xmalloc (sizeof *w);
67   w->fh = fh_ref (fh);
68   w->lock = lock;
69   w->rf = replace_file_start (fh_get_file_name (w->fh), "wb",
70                               (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
71                                | S_IROTH | S_IWOTH), &w->file, NULL);
72   if (w->rf == NULL)
73     {
74       msg (ME, _("An error occurred while opening \"%s\" for writing "
75                  "as a data file: %s."),
76            fh_get_file_name (w->fh), strerror (errno));
77       dfm_close_writer (w);
78       return NULL;
79     }
80   fh_lock_set_aux (lock, w);
81
82   return w;
83 }
84
85 /* Returns false if an I/O error occurred on WRITER, true otherwise. */
86 bool
87 dfm_write_error (const struct dfm_writer *writer)
88 {
89   return ferror (writer->file);
90 }
91
92 /* Writes record REC (which need not be null-terminated) having
93    length LEN to the file corresponding to HANDLE.  Adds any
94    needed formatting, such as a trailing new-line.  Returns true
95    on success, false on failure. */
96 bool
97 dfm_put_record (struct dfm_writer *w, const char *rec, size_t len)
98 {
99   assert (w != NULL);
100
101   if (dfm_write_error (w))
102     return false;
103
104   switch (fh_get_mode (w->fh))
105     {
106     case FH_MODE_TEXT:
107       fwrite (rec, len, 1, w->file);
108       putc ('\n', w->file);
109       break;
110
111     case FH_MODE_FIXED:
112       {
113         size_t record_width = fh_get_record_width (w->fh);
114         size_t write_bytes = MIN (len, record_width);
115         size_t pad_bytes = record_width - write_bytes;
116         fwrite (rec, write_bytes, 1, w->file);
117         while (pad_bytes > 0)
118           {
119             static const char spaces[32] = "                                ";
120             size_t chunk = MIN (pad_bytes, sizeof spaces);
121             fwrite (spaces, chunk, 1, w->file);
122             pad_bytes -= chunk;
123           }
124       }
125       break;
126
127     case FH_MODE_VARIABLE:
128       {
129         uint32_t size = len;
130         integer_convert (INTEGER_NATIVE, &size, INTEGER_LSB_FIRST, &size,
131                          sizeof size);
132         fwrite (&size, sizeof size, 1, w->file);
133         fwrite (rec, len, 1, w->file);
134         fwrite (&size, sizeof size, 1, w->file);
135       }
136       break;
137
138     case FH_MODE_360_VARIABLE:
139     case FH_MODE_360_SPANNED:
140       {
141         size_t ofs = 0;
142         if (fh_get_mode (w->fh) == FH_MODE_360_VARIABLE)
143           len = MIN (65527, len);
144         while (ofs < len)
145           {
146             size_t chunk = MIN (65527, len - ofs);
147             uint32_t bdw = (chunk + 8) << 16;
148             int scc = (ofs == 0 && chunk == len ? 0
149                        : ofs == 0 ? 1
150                        : ofs + chunk == len ? 2
151                        : 3);
152             uint32_t rdw = ((chunk + 4) << 16) | (scc << 8);
153
154             integer_convert (INTEGER_NATIVE, &bdw, INTEGER_MSB_FIRST, &bdw,
155                              sizeof bdw);
156             integer_convert (INTEGER_NATIVE, &rdw, INTEGER_MSB_FIRST, &rdw,
157                              sizeof rdw);
158             fwrite (&bdw, 1, sizeof bdw, w->file);
159             fwrite (&rdw, 1, sizeof rdw, w->file);
160             fwrite (rec + ofs, 1, chunk, w->file);
161             ofs += chunk;
162           }
163       }
164       break;
165
166     default:
167       NOT_REACHED ();
168     }
169
170   return !dfm_write_error (w);
171 }
172
173 /* Closes data file writer W. */
174 bool
175 dfm_close_writer (struct dfm_writer *w)
176 {
177   bool ok;
178
179   if (w == NULL)
180     return true;
181   if (fh_unlock (w->lock))
182     return true;
183
184   ok = true;
185   if (w->file != NULL)
186     {
187       const char *file_name = fh_get_file_name (w->fh);
188       ok = !dfm_write_error (w) && !fn_close (file_name, w->file);
189
190       if (!ok)
191         msg (ME, _("I/O error occurred writing data file \"%s\"."), file_name);
192
193       if (ok ? !replace_file_commit (w->rf) : !replace_file_abort (w->rf))
194         ok = false;
195     }
196   fh_unref (w->fh);
197   free (w);
198
199   return ok;
200 }
201
202 /* Returns the legacy character encoding of data written to WRITER. */
203 const char *
204 dfm_writer_get_legacy_encoding (const struct dfm_writer *writer)
205 {
206   return fh_get_legacy_encoding (writer->fh);
207 }