X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fpfm-read.c;h=7be8ea05472d3defb3143c7675fac4ecb6b57e4f;hb=205eaea8e2d95e20baa2c00a495b0ac4f9646372;hp=ef07642700168bd0fa226399b682008446cd1e8a;hpb=fcb9e49b2a2d57af7c001ae5d2eda9ac443ba36b;p=pspp-builds.git diff --git a/src/pfm-read.c b/src/pfm-read.c index ef076427..7be8ea05 100644 --- a/src/pfm-read.c +++ b/src/pfm-read.c @@ -17,25 +17,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* AIX requires this to be the first thing in the file. */ #include -#if __GNUC__ -#define alloca __builtin_alloca -#else -#if HAVE_ALLOCA_H -#include -#else -#ifdef _AIX -#pragma alloca -#else -#ifndef alloca /* predefined by HP cc +Olibcalls */ -char *alloca (); -#endif -#endif -#endif -#endif - -#include +#include "pfm.h" +#include "error.h" #include #include #include @@ -43,14 +27,15 @@ char *alloca (); #include #include #include "alloc.h" -#include "avl.h" +#include "case.h" #include "file-handle.h" #include "format.h" #include "getline.h" +#include "hash.h" #include "magic.h" #include "misc.h" -#include "pfm.h" #include "str.h" +#include "value-labels.h" #include "var.h" #include "debug-print.h" @@ -78,7 +63,7 @@ static struct fh_ext_class pfm_r_class; static int corrupt_msg (struct file_handle *h, const char *format,...) - __attribute__ ((format (printf, 2, 3))); + PRINTF_FORMAT (2, 3); /* Displays a corruption error. */ static int @@ -98,12 +83,14 @@ corrupt_msg (struct file_handle *h, const char *format, ...) { char *title; struct error e; + const char *filename; e.class = ME; getl_location (&e.where.filename, &e.where.line_number); - e.title = title = local_alloc (strlen (h->fn) + 80); + filename = handle_get_filename (h); + e.title = title = local_alloc (strlen (filename) + 80); sprintf (title, _("portable file %s corrupt at offset %ld: "), - h->fn, ftell (ext->file) - (82 - (long) (ext->bp - ext->buf))); + filename, ftell (ext->file) - (82 - (long) (ext->bp - ext->buf))); e.text = buf; err_vmsg (&e); @@ -116,12 +103,13 @@ corrupt_msg (struct file_handle *h, const char *format, ...) /* Closes a portable file after we're done with it. */ static void -pfm_close (struct file_handle * h) +pfm_close (struct file_handle *h) { struct pfm_fhuser_ext *ext = h->ext; if (EOF == fclose (ext->file)) - msg (ME, _("%s: Closing portable file: %s."), h->fn, strerror (errno)); + msg (ME, _("%s: Closing portable file: %s."), + handle_get_filename (h), strerror (errno)); free (ext->vars); free (ext->trans); free (h->ext); @@ -236,20 +224,21 @@ pfm_read_dictionary (struct file_handle *h, struct pfm_read_info *inf) { msg (ME, _("Cannot read file %s as portable file: already opened " "for %s."), - fh_handle_name (h), h->class->name); + handle_get_name (h), h->class->name); return NULL; } msg (VM (1), _("%s: Opening portable-file handle %s for reading."), - fh_handle_filename (h), fh_handle_name (h)); + handle_get_filename (h), handle_get_name (h)); /* Open the physical disk file. */ ext = xmalloc (sizeof (struct pfm_fhuser_ext)); - ext->file = fopen (h->norm_fn, "rb"); + ext->file = fopen (handle_get_filename (h), "rb"); if (ext->file == NULL) { msg (ME, _("An error occurred while opening \"%s\" for reading " - "as a portable file: %s."), h->fn, strerror (errno)); + "as a portable file: %s."), + handle_get_filename (h), strerror (errno)); err_cond_fail (); free (ext); return NULL; @@ -286,9 +275,6 @@ pfm_read_dictionary (struct file_handle *h, struct pfm_read_info *inf) msg (VM (2), _("Read portable-file dictionary successfully.")); -#if DEBUGGING - dump_dictionary (ext->dict); -#endif return ext->dict; lossage: @@ -297,7 +283,7 @@ pfm_read_dictionary (struct file_handle *h, struct pfm_read_info *inf) fclose (ext->file); if (ext && ext->dict) - free_dictionary (ext->dict); + dict_destroy (ext->dict); free (ext); h->class = NULL; h->ext = NULL; @@ -420,7 +406,7 @@ read_float (struct file_handle *h) } /* Read an integer and return its value, or NOT_INT on failure. */ -int +static int read_int (struct file_handle *h) { double f = read_float (h); @@ -633,11 +619,9 @@ static int convert_format (struct file_handle *h, int fmt[3], struct fmt_spec *v, struct variable *vv) { - if (fmt[0] < 0 - || (size_t) fmt[0] >= sizeof translate_fmt / sizeof *translate_fmt) - lose ((h, _("%s: Bad format specifier byte %d."), vv->name, fmt[0])); - - v->type = translate_fmt[fmt[0]]; + v->type = translate_fmt (fmt[0]); + if (v->type == -1) + lose ((h, _("%s: Bad format specifier byte (%d)."), vv->name, fmt[0])); v->w = fmt[1]; v->d = fmt[2]; @@ -682,6 +666,7 @@ static int read_variables (struct file_handle *h) { struct pfm_fhuser_ext *ext = h->ext; + char *weight_name = NULL; int i; if (!match (68 /* 4 */)) @@ -702,16 +687,21 @@ read_variables (struct file_handle *h) corrupt_msg (h, _("Unexpected flag value %d."), x); } - ext->dict = new_dictionary (0); + ext->dict = dict_create (); if (match (70 /* 6 */)) { - char *name = read_string (h); - if (!name) + weight_name = read_string (h); + if (!weight_name) goto lossage; - strcpy (ext->dict->weight_var, name); - asciify (ext->dict->weight_var); + asciify (weight_name); + if (strlen (weight_name) > 8) + { + corrupt_msg (h, _("Weight variable name (%s) truncated."), + weight_name); + weight_name[8] = '\0'; + } } for (i = 0; i < ext->nvars; i++) @@ -786,7 +776,7 @@ read_variables (struct file_handle *h) if (width < 0 || width > 255) lose ((h, "Bad width %d for variable %s.", width, name)); - v = create_variable (ext->dict, name, width ? ALPHA : NUMERIC, width); + v = dict_create_var (ext->dict, name, width); v->get.fv = v->fv; if (v == NULL) lose ((h, _("Duplicate variable name %s."), name)); @@ -851,16 +841,22 @@ read_variables (struct file_handle *h) asciify (v->label); } } - ext->case_size = ext->dict->nval; - if (ext->dict->weight_var[0] != 0 - && !find_dict_variable (ext->dict, ext->dict->weight_var)) - lose ((h, _("Weighting variable %s not present in dictionary."), - ext->dict->weight_var)); + if (weight_name != NULL) + { + struct variable *weight_var = dict_lookup_var (ext->dict, weight_name); + if (weight_var == NULL) + lose ((h, _("Weighting variable %s not present in dictionary."), + weight_name)); + free (weight_name); + + dict_set_weight (ext->dict, weight_var); + } return 1; lossage: + free (weight_name); return 0; } @@ -921,7 +917,7 @@ read_value_label (struct file_handle *h) goto lossage; asciify (name); - v[i] = find_dict_variable (ext->dict, name); + v[i] = dict_lookup_var (ext->dict, name); if (v[i] == NULL) lose ((h, _("Unknown variable %s while parsing value labels."), name)); @@ -939,7 +935,6 @@ read_value_label (struct file_handle *h) { union value val; char *label; - struct value_label *vl; int j; @@ -951,35 +946,20 @@ read_value_label (struct file_handle *h) goto lossage; asciify (label); - /* Create a label. */ - vl = xmalloc (sizeof *vl); - vl->v = val; - vl->s = xstrdup (label); - vl->ref_count = nv; - /* Assign the value_label's to each variable. */ for (j = 0; j < nv; j++) { struct variable *var = v[j]; - struct value_label *old; - - /* Create AVL tree if necessary. */ - if (!var->val_lab) - var->val_lab = avl_create (NULL, val_lab_cmp, - (void *) (var->width)); - old = avl_replace (var->val_lab, vl); - if (old == NULL) + if (!val_labs_replace (var->val_labs, val, label)) continue; if (var->type == NUMERIC) lose ((h, _("Duplicate label for value %g for variable %s."), - vl->v.f, var->name)); + val.f, var->name)); else lose ((h, _("Duplicate label for value `%.*s' for variable %s."), - var->width, vl->v.s, var->name)); - - free_value_label (old); + var->width, val.s, var->name)); } } free (v); @@ -990,12 +970,13 @@ read_value_label (struct file_handle *h) return 0; } -/* Reads one case from portable file H into the value array PERM +/* Reads one case from portable file H into PERM according to the instuctions given in associated dictionary DICT, which must have the get.fv elements appropriately set. Returns nonzero only if successful. */ int -pfm_read_case (struct file_handle *h, union value *perm, struct dictionary *dict) +pfm_read_case (struct file_handle *h, struct ccase *perm, + struct dictionary *dict) { struct pfm_fhuser_ext *ext = h->ext; @@ -1031,17 +1012,17 @@ pfm_read_case (struct file_handle *h, union value *perm, struct dictionary *dict /* Translate a case in data file format to a case in active file format. */ - for (i = 0; i < dict->nvar; i++) + for (i = 0; i < dict_get_var_cnt (dict); i++) { - struct variable *v = dict->var[i]; + struct variable *v = dict_get_var (dict, i); if (v->get.fv == -1) continue; if (v->type == NUMERIC) - perm[v->fv].f = temp[v->get.fv].f; + case_data_rw (perm, v->fv)->f = temp[v->get.fv].f; else - memcpy (&perm[v->fv].s, &temp[v->get.fv], v->width); + memcpy (case_data_rw (perm, v->fv)->s, &temp[v->get.fv], v->width); } local_free (temp);