X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fsys-file-writer.c;h=b1cb7c22c0dcd5e7a1343041b66d75fcac28cd36;hb=9ade26c8349b4434008c46cf09bc7473ec743972;hp=a761d4a29fc4e7262e1520104aa0aae0e32f4e47;hpb=c5ad65b0351ab1d897eb072eeaec06fb37802b01;p=pspp-builds.git diff --git a/src/data/sys-file-writer.c b/src/data/sys-file-writer.c index a761d4a2..b1cb7c22 100644 --- a/src/data/sys-file-writer.c +++ b/src/data/sys-file-writer.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011 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 @@ -47,6 +47,7 @@ #include "libpspp/message.h" #include "libpspp/misc.h" #include "libpspp/str.h" +#include "libpspp/string-array.h" #include "libpspp/version.h" #include "gl/xmemdup0.h" @@ -144,8 +145,8 @@ static void put_cmp_opcode (struct sfm_writer *, uint8_t); static void put_cmp_number (struct sfm_writer *, double); static void put_cmp_string (struct sfm_writer *, const void *, size_t); -bool write_error (const struct sfm_writer *); -bool close_writer (struct sfm_writer *); +static bool write_error (const struct sfm_writer *); +static bool close_writer (struct sfm_writer *); /* Returns default options for writing a system file. */ struct sfm_write_options @@ -159,8 +160,7 @@ sfm_writer_default_options (void) } /* Opens the system file designated by file handle FH for writing - cases from dictionary D according to the given OPTS. If - COMPRESS is nonzero, the system file will be compressed. + cases from dictionary D according to the given OPTS. No reference to D is retained, so it may be modified or destroyed at will after this function returns. D is not @@ -209,14 +209,14 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d, goto error; /* Create the file on disk. */ - mode = S_IRUSR | S_IRGRP | S_IROTH; + mode = 0444; if (opts.create_writeable) - mode |= S_IWUSR | S_IWGRP | S_IWOTH; + mode |= 0222; w->rf = replace_file_start (fh_get_file_name (fh), "wb", mode, &w->file, NULL); if (w->rf == NULL) { - msg (ME, _("Error opening \"%s\" for writing as a system file: %s."), + msg (ME, _("Error opening `%s' for writing as a system file: %s."), fh_get_file_name (fh), strerror (errno)); goto error; } @@ -239,7 +239,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d, idx += sfm_width_to_octs (var_get_width (v)); } - if (dict_get_documents (d) != NULL) + if (dict_get_document_line_cnt (d) > 0) write_documents (w, d); write_integer_info_record (w); @@ -269,10 +269,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d, write_int (w, 0); if (write_error (w)) - { - close_writer (w); - return NULL; - } + goto error; return casewriter_create (dict_get_proto (d), &sys_file_casewriter_class, w); @@ -556,11 +553,22 @@ write_value_labels (struct sfm_writer *w, struct variable *v, int idx) static void write_documents (struct sfm_writer *w, const struct dictionary *d) { - size_t line_cnt = dict_get_document_line_cnt (d); + const struct string_array *docs = dict_get_documents (d); + const char *enc = dict_get_encoding (d); + size_t i; write_int (w, 6); /* Record type. */ - write_int (w, line_cnt); - write_bytes (w, dict_get_documents (d), line_cnt * DOC_LINE_LENGTH); + write_int (w, docs->n); + for (i = 0; i < docs->n; i++) + { + char *s = recode_string (enc, "UTF-8", docs->strings[i], -1); + size_t s_len = strlen (s); + size_t write_len = MIN (s_len, DOC_LINE_LENGTH); + + write_bytes (w, s, write_len); + write_spaces (w, DOC_LINE_LENGTH - write_len); + free (s); + } } static void @@ -576,10 +584,10 @@ put_attrset (struct string *string, const struct attrset *attrs) size_t j; ds_put_cstr (string, attribute_get_name (attr)); - ds_put_char (string, '('); + ds_put_byte (string, '('); for (j = 0; j < n_values; j++) ds_put_format (string, "'%s'\n", attribute_get_value (attr, j)); - ds_put_char (string, ')'); + ds_put_byte (string, ')'); } } @@ -619,7 +627,7 @@ write_variable_attributes (struct sfm_writer *w, const struct dictionary *d) if (attrset_count (attrs)) { if (n_attrsets++) - ds_put_char (&s, '/'); + ds_put_byte (&s, '/'); ds_put_format (&s, "%s:", var_get_short_name (v, 0)); put_attrset (&s, attrs); } @@ -662,7 +670,7 @@ write_mrsets (struct sfm_writer *w, const struct dictionary *dict, if (mrset->cat_source == MRSET_COUNTEDVALUES) ds_put_format (&s, "E %d ", mrset->label_from_var_label ? 11 : 1); else - ds_put_char (&s, 'D'); + ds_put_byte (&s, 'D'); if (mrset->width == 0) counted = xasprintf ("%.0f", mrset->counted.f); @@ -673,17 +681,17 @@ write_mrsets (struct sfm_writer *w, const struct dictionary *dict, free (counted); } else - ds_put_char (&s, 'C'); - ds_put_char (&s, ' '); + ds_put_byte (&s, 'C'); + ds_put_byte (&s, ' '); label = mrset->label && !mrset->label_from_var_label ? mrset->label : ""; ds_put_format (&s, "%zu %s", strlen (label), label); for (j = 0; j < mrset->n_vars; j++) ds_put_format (&s, " %s", var_get_short_name (mrset->vars[j], 0)); - ds_put_char (&s, '\n'); + ds_put_byte (&s, '\n'); } - write_attribute_record (w, &s, 7); + write_attribute_record (w, &s, pre_v14 ? 7 : 19); ds_destroy (&s); } @@ -847,7 +855,7 @@ write_longvar_table (struct sfm_writer *w, const struct dictionary *dict) char *longname = recode_string (dict_get_encoding (dict), UTF8, var_get_name (v), -1); if (i) - ds_put_char (&map, '\t'); + ds_put_byte (&map, '\t'); ds_put_format (&map, "%s=%s", var_get_short_name (v, 0), longname); free (longname); @@ -947,7 +955,7 @@ sys_file_casewriter_destroy (struct casewriter *writer, void *w_) } /* Returns true if an I/O error has occurred on WRITER, false otherwise. */ -bool +static bool write_error (const struct sfm_writer *writer) { return ferror (writer->file); @@ -955,7 +963,7 @@ write_error (const struct sfm_writer *writer) /* Closes a system file after we're done with it. Returns true if successful, false if an I/O error occurred. */ -bool +static bool close_writer (struct sfm_writer *w) { bool ok; @@ -976,7 +984,7 @@ close_writer (struct sfm_writer *w) /* Seek back to the beginning and update the number of cases. This is just a courtesy to later readers, so there's no need to check return values or report errors. */ - if (ok && w->case_cnt <= INT32_MAX && !fseek (w->file, 80, SEEK_SET)) + if (ok && w->case_cnt <= INT32_MAX && !fseeko (w->file, 80, SEEK_SET)) { write_int (w, w->case_cnt); clearerr (w->file); @@ -986,7 +994,7 @@ close_writer (struct sfm_writer *w) ok = false; if (!ok) - msg (ME, _("An I/O error occurred writing system file \"%s\"."), + msg (ME, _("An I/O error occurred writing system file `%s'."), fh_get_file_name (w->fh)); if (ok ? !replace_file_commit (w->rf) : !replace_file_abort (w->rf))