X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fgnumeric-reader.c;h=5a0c75ede293a21052228a87b295a070398f3e1f;hb=deb4fd96c0c171fc8eb64f7f1e7f5c2af4931416;hp=cc8d335b3cff0dcf9bb8c571d0a1f3e8eb8e508c;hpb=0d70c2c8ed5d4ae5b40bb98c6c31db4f8ba87d68;p=pspp diff --git a/src/data/gnumeric-reader.c b/src/data/gnumeric-reader.c index cc8d335b3c..5a0c75ede2 100644 --- a/src/data/gnumeric-reader.c +++ b/src/data/gnumeric-reader.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007, 2009 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 @@ -60,10 +60,9 @@ gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict) static void gnm_file_casereader_destroy (struct casereader *, void *); -static bool gnm_file_casereader_read (struct casereader *, void *, - struct ccase *); +static struct ccase *gnm_file_casereader_read (struct casereader *, void *); -static struct casereader_class gnm_file_casereader_class = +static const struct casereader_class gnm_file_casereader_class = { gnm_file_casereader_read, gnm_file_casereader_destroy, @@ -172,7 +171,7 @@ struct gnumeric_reader size_t value_cnt; struct dictionary *dict; - struct ccase first_case; + struct ccase *first_case; bool used_first_case; }; @@ -193,7 +192,7 @@ gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_) xmlFreeTextReader (r->xtr); if ( ! r->used_first_case ) - case_destroy (&r->first_case); + case_unref (r->first_case); free (r); } @@ -308,66 +307,6 @@ process_node (struct gnumeric_reader *r) } - -/* - Change SUGGESTION until it's a valid name that can be added to DICT. -*/ -static void -devise_name (const struct dictionary *dict, struct string *name, int *x) -{ - struct string basename; - if ( ds_is_empty (name)) - ds_init_cstr (&basename, "var"); - else - ds_init_string (&basename, name); - do - { - ds_clear (name); - ds_put_format (name, "%s%d", ds_cstr (&basename), ++(*x)); - } - while (NULL != dict_lookup_var (dict, ds_cstr (name)) ); - - ds_destroy (&basename); -} - -/* - Mutate NAME of a variable, which is gauranteed to be valid for the - dictionary DICT. -*/ -static void -munge_name (const struct dictionary *dict, struct string *name) -{ - int x = 0; - - if (! ds_is_empty (name)) - { - /* Change all the invalid characters to valid ones */ - char *s; - - s = ds_data (name); - - if ( !lex_is_id1 (*s)) - *s = '@'; - - s++; - - while (s < ds_data (name) + ds_length (name)) - { - if ( !lex_is_idn (*s)) - *s = '_'; - s++; - } - - assert (var_is_valid_name (ds_cstr (name), false)); - } - - while (ds_is_empty (name) || NULL != dict_lookup_var (dict, ds_cstr (name)) ) - { - devise_name (dict, name, &x); - } -} - - /* Sets the VAR of case C, to the value corresponding to the xml string XV */ @@ -410,6 +349,7 @@ struct var_spec struct casereader * gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict) { + unsigned long int vstart = 0; int ret; casenumber n_cases = CASENUMBER_MAX; int i; @@ -567,25 +507,23 @@ gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict) for (i = 0 ; i < n_var_specs ; ++i ) { - struct string name; + char name[VAR_NAME_LEN + 1]; - /* Probably no data exists for this variable, so allocate a default width */ + /* Probably no data exists for this variable, so allocate a + default width */ if ( var_spec[i].width == -1 ) var_spec[i].width = MAX_SHORT_STRING; r->value_cnt += value_cnt_from_width (var_spec[i].width); - if (var_spec[i].name) - ds_init_cstr (&name, var_spec[i].name); - else - ds_init_empty (&name); - - munge_name (r->dict, &name); - - - dict_create_var (r->dict, ds_cstr (&name), var_spec[i].width); + if ( ! dict_make_unique_var_name (r->dict, var_spec[i].name, + &vstart, name)) + { + msg (ME, _("Cannot create variable name from %s"), var_spec[i].name); + goto error; + } - ds_destroy (&name); + dict_create_var (r->dict, name, var_spec[i].width); } /* Create the first case, and cache it */ @@ -598,15 +536,15 @@ gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict) goto error; } - case_create (&r->first_case, r->value_cnt); - memset (case_data_rw_idx (&r->first_case, 0)->s, + r->first_case = case_create (r->value_cnt); + memset (case_data_rw_idx (r->first_case, 0)->s, ' ', MAX_SHORT_STRING * r->value_cnt); for ( i = 0 ; i < n_var_specs ; ++i ) { const struct variable *var = dict_get_var (r->dict, i); - convert_xml_string_to_value (&r->first_case, var, + convert_xml_string_to_value (r->first_case, var, var_spec[i].first_value); } @@ -641,12 +579,12 @@ gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict) }; -/* Reads one case from READER's file into C. Returns true only - if successful. */ -static bool -gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_, - struct ccase *c) +/* Reads and returns one case from READER's file. Returns a null + pointer on failure. */ +static struct ccase * +gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_) { + struct ccase *c; int ret = 0; struct gnumeric_reader *r = r_; @@ -654,12 +592,11 @@ gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_, if ( !r->used_first_case ) { - *c = r->first_case; r->used_first_case = true; - return true; + return r->first_case; } - case_create (c, r->value_cnt); + c = case_create (r->value_cnt); memset (case_data_rw_idx (c, 0)->s, ' ', MAX_SHORT_STRING * r->value_cnt); @@ -693,7 +630,13 @@ gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_, } - return (ret == 1); + if (ret == 1) + return c; + else + { + case_unref (c); + return NULL; + } }