24 |
#include <stdlib.h> |
#include <stdlib.h> |
25 |
#include <ctype.h> |
#include <ctype.h> |
26 |
#include <errno.h> |
#include <errno.h> |
27 |
|
#include <fcntl.h> |
28 |
|
#include <sys/stat.h> |
29 |
#include <time.h> |
#include <time.h> |
30 |
#if HAVE_UNISTD_H |
#if HAVE_UNISTD_H |
31 |
#include <unistd.h> /* Required by SunOS4. */ |
#include <unistd.h> /* Required by SunOS4. */ |
39 |
#include "hash.h" |
#include "hash.h" |
40 |
#include "magic.h" |
#include "magic.h" |
41 |
#include "misc.h" |
#include "misc.h" |
42 |
|
#include "settings.h" |
43 |
|
#include "stat-macros.h" |
44 |
#include "str.h" |
#include "str.h" |
45 |
#include "value-labels.h" |
#include "value-labels.h" |
46 |
#include "var.h" |
#include "var.h" |
111 |
return v->type == NUMERIC ? 1 : DIV_RND_UP (v->width, sizeof (flt64)); |
return v->type == NUMERIC ? 1 : DIV_RND_UP (v->width, sizeof (flt64)); |
112 |
} |
} |
113 |
|
|
114 |
|
/* Returns default options for writing a system file. */ |
115 |
|
struct sfm_write_options |
116 |
|
sfm_writer_default_options (void) |
117 |
|
{ |
118 |
|
struct sfm_write_options opts; |
119 |
|
opts.create_writeable = true; |
120 |
|
opts.compress = get_scompression (); |
121 |
|
opts.version = 3; |
122 |
|
return opts; |
123 |
|
} |
124 |
|
|
125 |
/* Opens the system file designated by file handle FH for writing |
/* Opens the system file designated by file handle FH for writing |
126 |
cases from dictionary D. If COMPRESS is nonzero, the |
cases from dictionary D according to the given OPTS. If |
127 |
system file will be compressed. If OMIT_LONG_NAMES is nonzero, the |
COMPRESS is nonzero, the system file will be compressed. |
|
long name table will be omitted. |
|
128 |
|
|
129 |
No reference to D is retained, so it may be modified or |
No reference to D is retained, so it may be modified or |
130 |
destroyed at will after this function returns. D is not |
destroyed at will after this function returns. D is not |
131 |
modified by this function, except to assign short names. */ |
modified by this function, except to assign short names. */ |
132 |
struct sfm_writer * |
struct sfm_writer * |
133 |
sfm_open_writer (struct file_handle *fh, |
sfm_open_writer (struct file_handle *fh, struct dictionary *d, |
134 |
struct dictionary *d, int compress, |
struct sfm_write_options opts) |
|
short omit_long_names) |
|
135 |
{ |
{ |
136 |
struct sfm_writer *w = NULL; |
struct sfm_writer *w = NULL; |
137 |
|
mode_t mode; |
138 |
|
int fd; |
139 |
int idx; |
int idx; |
140 |
int i; |
int i; |
141 |
|
|
142 |
|
/* Check version. */ |
143 |
|
if (opts.version != 2 && opts.version != 3) |
144 |
|
{ |
145 |
|
msg (ME, _("Unknown system file version %d. Treating as version %d."), |
146 |
|
opts.version, 3); |
147 |
|
opts.version = 3; |
148 |
|
} |
149 |
|
|
150 |
|
/* Create file. */ |
151 |
|
mode = S_IRUSR | S_IRGRP | S_IROTH; |
152 |
|
if (opts.create_writeable) |
153 |
|
mode |= S_IWUSR | S_IWGRP | S_IWOTH; |
154 |
|
fd = open (handle_get_filename (fh), O_WRONLY | O_CREAT | O_TRUNC, mode); |
155 |
|
if (fd < 0) |
156 |
|
goto open_error; |
157 |
|
|
158 |
|
/* Open file handle. */ |
159 |
if (!fh_open (fh, "system file", "we")) |
if (!fh_open (fh, "system file", "we")) |
160 |
goto error; |
goto error; |
161 |
|
|
162 |
/* Create and initialize writer. */ |
/* Create and initialize writer. */ |
163 |
w = xmalloc (sizeof *w); |
w = xmalloc (sizeof *w); |
164 |
w->fh = fh; |
w->fh = fh; |
165 |
w->file = fopen (handle_get_filename (fh), "wb"); |
w->file = fdopen (fd, "w"); |
166 |
|
|
167 |
w->needs_translation = does_dict_need_translation (d); |
w->needs_translation = does_dict_need_translation (d); |
168 |
w->compress = compress; |
w->compress = opts.compress; |
169 |
w->case_cnt = 0; |
w->case_cnt = 0; |
170 |
w->flt64_cnt = 0; |
w->flt64_cnt = 0; |
171 |
|
|
184 |
} |
} |
185 |
|
|
186 |
/* Check that file create succeeded. */ |
/* Check that file create succeeded. */ |
187 |
if (w->file == NULL) |
if (w->file == NULL) |
188 |
{ |
{ |
189 |
msg (ME, _("Error opening \"%s\" for writing " |
close (fd); |
190 |
"as a system file: %s."), |
goto open_error; |
|
handle_get_filename (w->fh), strerror (errno)); |
|
|
err_cond_fail (); |
|
|
goto error; |
|
191 |
} |
} |
192 |
|
|
193 |
/* Write the file header. */ |
/* Write the file header. */ |
218 |
if (!write_variable_display_parameters (w, d)) |
if (!write_variable_display_parameters (w, d)) |
219 |
goto error; |
goto error; |
220 |
|
|
221 |
if (!omit_long_names) |
if (opts.version >= 3) |
222 |
{ |
{ |
223 |
if (!write_longvar_table (w, d)) |
if (!write_longvar_table (w, d)) |
224 |
goto error; |
goto error; |
254 |
error: |
error: |
255 |
sfm_close_writer (w); |
sfm_close_writer (w); |
256 |
return NULL; |
return NULL; |
257 |
|
|
258 |
|
open_error: |
259 |
|
msg (ME, _("Error opening \"%s\" for writing as a system file: %s."), |
260 |
|
handle_get_filename (w->fh), strerror (errno)); |
261 |
|
err_cond_fail (); |
262 |
|
goto error; |
263 |
} |
} |
264 |
|
|
265 |
static int |
static int |