You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- 02111-1307, USA. */
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA. */
#include <config.h>
-#include <assert.h>
+#include "error.h"
#include <stdlib.h>
#include "alloc.h"
+#include "case.h"
#include "command.h"
+#include "dictionary.h"
#include "error.h"
#include "hash.h"
#include "lexer.h"
#include "var.h"
#include "vfm.h"
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
/* FIXME: Implement PRINT subcommand. */
/* Explains how to recode one value. `from' must be first element. */
/* AUTORECODE transformation. */
struct autorecode_trns
{
- struct trns_header h;
- struct pool *owner; /* Contains AUTORECODE specs. */
+ struct pool *pool; /* Contains AUTORECODE specs. */
struct arc_spec *specs; /* AUTORECODE specifications. */
- int spec_cnt; /* Number of specifications. */
+ size_t spec_cnt; /* Number of specifications. */
};
/* Descending or ascending sort order. */
char **dst_names; /* Target variable names. */
struct variable **dst_vars; /* Target variables. */
struct hsh_table **src_values; /* `union value's of source vars. */
- int var_cnt; /* Number of variables. */
+ size_t var_cnt; /* Number of variables. */
struct pool *src_values_pool; /* Pool used by src_values. */
enum direction direction; /* Sort order. */
int print; /* Print mapping table if nonzero. */
cmd_autorecode (void)
{
struct autorecode_pgm arc;
- int dst_cnt;
- int i;
+ size_t dst_cnt;
+ size_t i;
arc.src_vars = NULL;
arc.dst_names = NULL;
goto lossage;
if (dst_cnt != arc.var_cnt)
{
- int i;
+ size_t i;
- msg (SE, _("Source variable count (%d) does not match "
- "target variable count (%d)."), arc.var_cnt, dst_cnt);
+ msg (SE, _("Source variable count (%u) does not match "
+ "target variable count (%u)."),
+ (unsigned) arc.var_cnt, (unsigned) dst_cnt);
for (i = 0; i < dst_cnt; i++)
free (arc.dst_names[i]);
goto lossage;
}
for (j = 0; j < i; j++)
- if (!strcmp (arc.dst_names[i], arc.dst_names[j]))
+ if (!strcasecmp (arc.dst_names[i], arc.dst_names[j]))
{
msg (SE, _("Duplicate variable name %s among target variables."),
arc.dst_names[i]);
}
arc.src_values_pool = pool_create ();
- arc.dst_vars = xmalloc (sizeof *arc.dst_vars * arc.var_cnt);
- arc.src_values = xmalloc (sizeof *arc.src_values * arc.var_cnt);
+ arc.dst_vars = xnmalloc (arc.var_cnt, sizeof *arc.dst_vars);
+ arc.src_values = xnmalloc (arc.var_cnt, sizeof *arc.src_values);
for (i = 0; i < dst_cnt; i++)
if (arc.src_vars[i]->type == ALPHA)
arc.src_values[i] = hsh_create (10, compare_alpha_value,
free (arc->src_vars);
if (arc->dst_names != NULL)
{
- int i;
+ size_t i;
for (i = 0; i < arc->var_cnt; i++)
free (arc->dst_names[i]);
free (arc->dst_vars);
if (arc->src_values != NULL)
{
- int i;
+ size_t i;
for (i = 0; i < arc->var_cnt; i++)
hsh_destroy (arc->src_values[i]);
static void
recode (const struct autorecode_pgm *arc)
{
- struct autorecode_trns *t;
- struct pool *pool;
- int i;
-
- pool = pool_create ();
- t = xmalloc (sizeof *t);
- t->h.proc = autorecode_trns_proc;
- t->h.free = autorecode_trns_free;
- t->owner = pool;
- t->specs = pool_alloc (t->owner, sizeof *t->specs * arc->var_cnt);
- t->spec_cnt = arc->var_cnt;
+ struct autorecode_trns *trns;
+ size_t i;
+
+ trns = pool_create_container (struct autorecode_trns, pool);
+ trns->specs = pool_nalloc (trns->pool, arc->var_cnt, sizeof *trns->specs);
+ trns->spec_cnt = arc->var_cnt;
for (i = 0; i < arc->var_cnt; i++)
{
- struct arc_spec *spec = &t->specs[i];
- void **p = hsh_sort (arc->src_values[i]);
+ struct arc_spec *spec = &trns->specs[i];
+ void *const *p = hsh_sort (arc->src_values[i]);
int count = hsh_count (arc->src_values[i]);
int j;
for (j = 0; *p; p++, j++)
{
- struct arc_item *item = pool_alloc (t->owner, sizeof *item);
+ struct arc_item *item = pool_alloc (trns->pool, sizeof *item);
union value *vp = *p;
if (arc->src_vars[i]->type == NUMERIC)
item->from.f = vp->f;
else
- item->from.c = pool_strdup (t->owner, vp->c);
+ item->from.c = pool_strdup (trns->pool, vp->c);
item->to = arc->direction == ASCENDING ? j + 1 : count - j;
hsh_force_insert (spec->items, item);
}
}
- add_transformation (&t->h);
+ add_transformation (autorecode_trns_proc, autorecode_trns_free, trns);
}
static int
-autorecode_trns_proc (struct trns_header * trns, struct ccase * c,
- int case_num UNUSED)
+autorecode_trns_proc (void *trns_, struct ccase *c, int case_idx UNUSED)
{
- struct autorecode_trns *t = (struct autorecode_trns *) trns;
- int i;
+ struct autorecode_trns *trns = trns_;
+ size_t i;
- for (i = 0; i < t->spec_cnt; i++)
+ for (i = 0; i < trns->spec_cnt; i++)
{
- struct arc_spec *spec = &t->specs[i];
+ struct arc_spec *spec = &trns->specs[i];
struct arc_item *item;
+ union value v;
if (spec->src->type == NUMERIC)
- item = hsh_force_find (spec->items, &c->data[spec->src->fv].f);
+ v.f = case_num (c, spec->src->fv);
else
- {
- union value v;
- v.c = c->data[spec->src->fv].s;
- item = hsh_force_find (spec->items, &v);
- }
+ v.c = (char *) case_str (c, spec->src->fv);
+ item = hsh_force_find (spec->items, &v);
- c->data[spec->dest->fv].f = item->to;
+ case_data_rw (c, spec->dest->fv)->f = item->to;
}
return -1;
}
static void
-autorecode_trns_free (struct trns_header * trns)
+autorecode_trns_free (void *trns_)
{
- struct autorecode_trns *t = (struct autorecode_trns *) trns;
- int i;
+ struct autorecode_trns *trns = trns_;
+ size_t i;
- for (i = 0; i < t->spec_cnt; i++)
- hsh_destroy (t->specs[i].items);
- pool_destroy (t->owner);
+ for (i = 0; i < trns->spec_cnt; i++)
+ hsh_destroy (trns->specs[i].items);
+ pool_destroy (trns->pool);
}
\f
/* AUTORECODE procedure. */
autorecode_proc_func (struct ccase *c, void *arc_)
{
struct autorecode_pgm *arc = arc_;
- int i;
+ size_t i;
for (i = 0; i < arc->var_cnt; i++)
{
union value v, *vp, **vpp;
if (arc->src_vars[i]->type == NUMERIC)
- v.f = c->data[arc->src_vars[i]->fv].f;
+ v.f = case_num (c, arc->src_vars[i]->fv);
else
- v.c = c->data[arc->src_vars[i]->fv].s;
+ v.c = (char *) case_str (c, arc->src_vars[i]->fv);
vpp = (union value **) hsh_probe (arc->src_values[i], &v);
if (*vpp == NULL)
{
- vp = pool_alloc (arc->src_values_pool, sizeof (union value));
+ vp = pool_alloc (arc->src_values_pool, sizeof *vp);
if (arc->src_vars[i]->type == NUMERIC)
vp->f = v.f;
else
- vp->c = pool_strndup (arc->src_values_pool,
- v.c, arc->src_vars[i]->width);
+ vp->c = pool_clone (arc->src_values_pool,
+ v.c, arc->src_vars[i]->width);
*vpp = vp;
}
}