Implemented long variable names a la spss V12.
[pspp-builds.git] / src / aggregate.c
index 9d925707c2d819235f03a42cd5b9fed4ea4d1a28..6da6242c670a0be3fad5dfaf321f1db73cfd2495 100644 (file)
@@ -213,7 +213,9 @@ cmd_aggregate (void)
           for (i = 0; i < agr.break_var_cnt; i++)
             {
               struct variable *v = dict_clone_var (agr.dict, agr.break_vars[i],
-                                                   agr.break_vars[i]->name);
+                                                   agr.break_vars[i]->name, 
+                                                  agr.break_vars[i]->longname 
+                                                  );
               assert (v != NULL);
             }
 
@@ -276,7 +278,7 @@ cmd_aggregate (void)
     }
   else
     {
-      agr.writer = sfm_open_writer (out_file, agr.dict, get_scompression ());
+      agr.writer = sfm_open_writer (out_file, agr.dict, get_scompression (), 0);
       if (agr.writer == NULL)
         goto error;
       
@@ -396,7 +398,7 @@ parse_aggregate_functions (struct agr_proc *agr)
        }
       
       for (function = agr_func_tab; function->name; function++)
-       if (!strcmp (function->name, tokid))
+       if (!strcasecmp (function->name, tokid))
          break;
       if (NULL == function->name)
        {
@@ -524,6 +526,7 @@ parse_aggregate_functions (struct agr_proc *agr)
          /* Create the target variable in the aggregate
              dictionary. */
          {
+            static const struct fmt_spec f8_2 = {FMT_F, 8, 2};
            struct variable *destvar;
            
            v->function = func_index;
@@ -539,30 +542,28 @@ parse_aggregate_functions (struct agr_proc *agr)
                  }
 
                if (function->alpha_type == ALPHA)
-                 destvar = dict_clone_var (agr->dict, v->src, dest[i]);
+                 destvar = dict_clone_var (agr->dict, v->src, 0, dest[i] );
                else if (v->src->type == NUMERIC
                          || function->alpha_type == NUMERIC)
                   {
                     destvar = dict_create_var (agr->dict, dest[i], 0);
-                        
-                    if ((func_index == N
-                            || func_index == N_NO_VARS
-                            || func_index == NMISS)
-                        && dict_get_weight (default_dict) != NULL)
+                    if (destvar != NULL) 
                       {
-                        static const struct fmt_spec f8_2 = {FMT_F, 8, 2};
-                            
-                        destvar->print = destvar->write = f8_2; 
+                        if ((func_index == N || func_index == NMISS)
+                            && dict_get_weight (default_dict) != NULL)
+                          destvar->print = destvar->write = f8_2; 
+                        else
+                          destvar->print = destvar->write = function->format;
                       }
-                    else
-                      destvar->print = destvar->write = function->format;
                   }
-                else 
-                  destvar = dict_create_var (agr->dict, dest[i],
-                                             v->src->width);
              } else {
                v->src = NULL;
                destvar = dict_create_var (agr->dict, dest[i], 0);
+                if (func_index == N_NO_VARS
+                    && dict_get_weight (default_dict) != NULL)
+                  destvar->print = destvar->write = f8_2; 
+                else
+                  destvar->print = destvar->write = function->format;
              }
          
            if (!destvar)
@@ -572,7 +573,6 @@ parse_aggregate_functions (struct agr_proc *agr)
                           "the aggregate variables and the break "
                           "variables."),
                     dest[i]);
-               free (dest[i]);
                goto error;
              }
 
@@ -817,9 +817,11 @@ accumulate_aggregate_info (struct agr_proc *agr,
            switch (iter->function)
              {
              case NMISS:
+             case NMISS | FSTRING:
                iter->dbl[0] += weight;
                 break;
              case NUMISS:
+             case NUMISS | FSTRING:
                iter->int1++;
                break;
              }
@@ -832,6 +834,7 @@ accumulate_aggregate_info (struct agr_proc *agr,
          {
          case SUM:
            iter->dbl[0] += v->f * weight;
+            iter->int1 = 1;
            break;
          case MEAN:
             iter->dbl[0] += v->f * weight;
@@ -904,7 +907,7 @@ accumulate_aggregate_info (struct agr_proc *agr,
          case FOUT | FSTRING:
          case POUT | FSTRING:
             if (memcmp (iter->arg[0].c, v->s, iter->src->width) > 0
-                && memcmp (iter->arg[1].c, v->s, iter->src->width) < 0)
+                || memcmp (iter->arg[1].c, v->s, iter->src->width) < 0)
               iter->dbl[0] += weight;
             iter->dbl[1] += weight;
             break;
@@ -994,7 +997,7 @@ dump_aggregate_info (struct agr_proc *agr, struct ccase *output)
            && (i->function & FUNC) != N && (i->function & FUNC) != NU
            && (i->function & FUNC) != NMISS && (i->function & FUNC) != NUMISS)
          {
-           if (i->function & FSTRING)
+           if (i->dest->type == ALPHA)
              memset (v->s, ' ', i->dest->width);
            else
              v->f = SYSMIS;
@@ -1004,7 +1007,7 @@ dump_aggregate_info (struct agr_proc *agr, struct ccase *output)
        switch (i->function)
          {
          case SUM:
-           v->f = i->dbl[0];
+           v->f = i->int1 ? i->dbl[0] : SYSMIS;
            break;
          case MEAN:
            v->f = i->dbl[1] != 0.0 ? i->dbl[0] / i->dbl[1] : SYSMIS;
@@ -1033,16 +1036,14 @@ dump_aggregate_info (struct agr_proc *agr, struct ccase *output)
            else
              memset (v->s, ' ', i->dest->width);
            break;
-         case FGT | FSTRING:
-         case FLT | FSTRING:
-         case FIN | FSTRING:
-         case FOUT | FSTRING:
-           v->f = i->int2 ? (double) i->int1 / (double) i->int2 : SYSMIS;
-           break;
          case FGT:
+         case FGT | FSTRING:
          case FLT:
+         case FLT | FSTRING:
          case FIN:
+         case FIN | FSTRING:
          case FOUT:
+         case FOUT | FSTRING:
            v->f = i->dbl[1] ? i->dbl[0] / i->dbl[1] : SYSMIS;
            break;
          case PGT:
@@ -1104,18 +1105,18 @@ initialize_aggregate_info (struct agr_proc *agr)
   for (iter = agr->agr_vars; iter; iter = iter->next)
     {
       iter->missing = 0;
+      iter->dbl[0] = iter->dbl[1] = iter->dbl[2] = 0.0;
+      iter->int1 = iter->int2 = 0;
       switch (iter->function)
        {
        case MIN:
          iter->dbl[0] = DBL_MAX;
-          iter->int1 = 0;
          break;
        case MIN | FSTRING:
          memset (iter->string, 255, iter->src->width);
          break;
        case MAX:
          iter->dbl[0] = -DBL_MAX;
-          iter->int1 = 0;
          break;
        case MAX | FSTRING:
          memset (iter->string, 0, iter->src->width);
@@ -1126,10 +1127,8 @@ initialize_aggregate_info (struct agr_proc *agr)
           else
             moments1_clear (iter->moments);
           break;
-       default:
-         iter->dbl[0] = iter->dbl[1] = iter->dbl[2] = 0.0;
-         iter->int1 = iter->int2 = 0;
-         break;
+        default:
+          break;
        }
     }
 }