X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fmrset.c;h=c5e6cd2ec2b80b8302e277cd7a6fbfd4c491eea1;hb=5d301c6713f7fe211a3626ee53388cb4c270c04e;hp=2f05edb845886786caab5b5ce94503ea22e206c5;hpb=c5ad65b0351ab1d897eb072eeaec06fb37802b01;p=pspp diff --git a/src/data/mrset.c b/src/data/mrset.c index 2f05edb845..c5e6cd2ec2 100644 --- a/src/data/mrset.c +++ b/src/data/mrset.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2010 Free Software Foundation, Inc. + Copyright (C) 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 @@ -21,11 +21,16 @@ #include #include "data/dictionary.h" +#include "data/identifier.h" #include "data/val-type.h" #include "data/variable.h" +#include "libpspp/message.h" #include "gl/xalloc.h" +#include "gettext.h" +#define _(msgid) gettext (msgid) + /* Creates and returns a clone of OLD. The caller is responsible for freeing the new multiple response set (using mrset_destroy()). */ struct mrset * @@ -35,7 +40,7 @@ mrset_clone (const struct mrset *old) new = xmalloc (sizeof *new); new->name = xstrdup (old->name); - new->label = old->label != NULL ? xstrdup (old->label) : NULL; + new->label = xstrdup_if_nonnull (old->label); new->type = old->type; new->vars = xmemdup (old->vars, old->n_vars * sizeof *old->vars); new->n_vars = old->n_vars; @@ -58,12 +63,51 @@ mrset_destroy (struct mrset *mrset) free (mrset->label); free (mrset->vars); value_destroy (&mrset->counted, mrset->width); + free (mrset); + } +} + +/* Checks whether the UTF-8 encoded NAME is a valid name for a multiple + response set in a dictionary encoded in DICT_ENCODING. Return NULL if it + is, otherwise an error message that the caller must free(). */ +char * WARN_UNUSED_RESULT +mrset_is_valid_name__ (const char *name, const char *dict_encoding) +{ + char *error = id_is_valid__ (name, dict_encoding); + if (error) + return error; + + if (name[0] != '$') + return xasprintf (_("%s is not a valid name for a multiple response " + "set. Multiple response set names must begin with " + "`$'."), name); + + return NULL; +} + +static bool +error_to_bool (char *error) +{ + if (error) + { + free (error); + return false; } + else + return true; +} + +/* Returns true if the UTF-8 encoded NAME is a valid name for a multiple + response set in a dictionary encoded in DICT_ENCODING, false otherwise. */ +bool +mrset_is_valid_name (const char *name, const char *dict_encoding) +{ + return error_to_bool (mrset_is_valid_name__ (name, dict_encoding)); } /* Checks various constraints on MRSET: - - MRSET has a valid name for a multiple response set (beginning with '$'). + - MRSET's name begins with '$' and is valid as an identifier in DICT. - MRSET has a valid type. @@ -84,7 +128,7 @@ mrset_ok (const struct mrset *mrset, const struct dictionary *dict) size_t i; if (mrset->name == NULL - || mrset->name[0] != '$' + || !mrset_is_valid_name (mrset->name, dict_get_encoding (dict)) || (mrset->type != MRSET_MD && mrset->type != MRSET_MC) || mrset->vars == NULL || mrset->n_vars < 2)