Adopt use of gnulib for portability.
[pspp-builds.git] / src / var.h
index 277bfde8ad3ccd1f3afda2aa01c2077633f85fe6..3bfc43df7b731a4b1e937075e7225f8167dbf836 100644 (file)
--- a/src/var.h
+++ b/src/var.h
 
    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. */
 
 #if !var_h
 #define var_h 1
 
+
 #include <stddef.h>
+#include "config.h"
+#include <stdbool.h>
 #include "format.h"
-#include "t-test.h"
 #include "val.h"
 
-/* Frequency tables. */
-
-/* Frequency table entry. */
-struct freq
-  {
-    union value v;             /* The value. */
-    double c;                  /* The number of occurrences of the value. */
-  };
-
-/* Types of frequency tables. */
-enum
-  {
-    FRQM_GENERAL,
-    FRQM_INTEGER
-  };
-
-/* Entire frequency table. */
-struct freq_tab
-  {
-    int mode;                  /* FRQM_GENERAL or FRQM_INTEGER. */
-
-    /* General mode. */
-    struct hsh_table *data;    /* Undifferentiated data. */
-
-    /* Integer mode. */
-    double *vector;            /* Frequencies proper. */
-    int min, max;              /* The boundaries of the table. */
-    double out_of_range;       /* Sum of weights of out-of-range values. */
-    double sysmis;             /* Sum of weights of SYSMIS values. */
-
-    /* All modes. */
-    struct freq *valid;         /* Valid freqs. */
-    int n_valid;               /* Number of total freqs. */
-
-    struct freq *missing;      /* Missing freqs. */
-    int n_missing;             /* Number of missing freqs. */
-
-    /* Statistics. */
-    double total_cases;                /* Sum of weights of all cases. */
-    double valid_cases;                /* Sum of weights of valid cases. */
-  };
-\f
-/* Procedures' private per-variable data. */
-
-/* Structure name suffixes for private data:
-   _proc: for a procedure (i.e., LIST -> list_proc).
-   _trns: for a transformation (i.e., COMPUTE -> compute_trns.
-   _pgm: for an input program (i.e., DATA LIST -> data_list_pgm). */
-
-/* CROSSTABS private data. */
-struct crosstab_proc
-  {
-    /* Integer mode only. */
-    int min;                   /* Minimum value. */
-    int max;                   /* Maximum value + 1. */
-    int count;                 /* max - min. */
-  };
-
-
-/* FREQUENCIES private data. */
-enum
-  {
-    frq_mean = 0, frq_semean, frq_median, frq_mode, frq_stddev, frq_variance,
-    frq_kurt, frq_sekurt, frq_skew, frq_seskew, frq_range, frq_min, frq_max,
-    frq_sum, frq_n_stats
-  };
-
-struct frequencies_proc
-  {
-    int used;                   /* 1=This variable already used. */
-
-    /* Freqency table. */
-    struct freq_tab tab;       /* Frequencies table to use. */
-
-    /* Percentiles. */
-    int n_groups;              /* Number of groups. */
-    double *groups;            /* Groups. */
-
-    /* Statistics. */
-    double stat[frq_n_stats];
-  };
-
-/* LIST private data. */
-struct list_proc
-  {
-    int newline;               /* Whether a new line begins here. */
-    int width;                 /* Field width. */
-    int vert;                  /* Whether to print the varname vertically. */
-  };
-
-/* DESCRIPTIVES private data.  Note that the DESCRIPTIVES procedure also
-   has a transformation, descriptives_trns. */
-enum
-  {
-    /* As these are used as bit indexes, there must be 32 or fewer.
-       Be very careful in adjusting these, see the structure below
-       and the table in descriptives.q. */
-    dsc_mean = 0, dsc_semean, dsc_stddev, dsc_variance, dsc_kurt,
-    dsc_sekurt, dsc_skew, dsc_seskew, dsc_range, dsc_min,
-    dsc_max, dsc_sum, dsc_n_stats
-  };
-
-struct descriptives_proc
-  {
-    /* Miscellaneous. */
-    int dup;                   /* Finds duplicates in list of
-                                  variables. */
-    char zname[10];            /* Name for z-score variable. */
-
-    /* Counts. */
-    double valid, miss;                /* Valid, missing--general. */
-
-    /* Mean, moments about the mean. */
-    double X_bar, M2, M3, M4;
-    double min, max;
-
-    /* Statistics. */
-    double stats[dsc_n_stats]; /* Everything glommed together. */
-  };
-
-/* GET private data. */
-struct get_proc
-  {
-    int fv, nv;                        /* First, # of values. */
-  };
-
-/* Sort order. */
-enum
-  {
-    SRT_ASCEND,                        /* A, B, C, ..., X, Y, Z. */
-    SRT_DESCEND                        /* Z, Y, X, ..., C, B, A. */
-  };
-
-/* SORT CASES private data. */
-struct sort_cases_proc
-  {
-    int order;                 /* SRT_ASCEND or SRT_DESCEND. */
-  };
 
-/* MEANS private data. */
-struct means_proc
-  {
-    double min, max;           /* Range for integer mode. */
-  };
-
-/* Different types of variables for MATRIX DATA procedure.  Order is
-   important: these are used for sort keys. */
-enum
-  {
-    MXD_SPLIT,                 /* SPLIT FILE variables. */
-    MXD_ROWTYPE,               /* ROWTYPE_. */
-    MXD_FACTOR,                        /* Factor variables. */
-    MXD_VARNAME,               /* VARNAME_. */
-    MXD_CONTINUOUS,            /* Continuous variables. */
-
-    MXD_COUNT
-  };
-
-/* MATRIX DATA private data. */
-struct matrix_data_proc
-  {
-    int vartype;               /* Variable type. */
-    int subtype;               /* Subtype. */
-  };
-
-/* MATCH FILES private data. */
-struct match_files_proc
-  {
-    struct variable *master;   /* Corresponding master file variable. */
-  };
 
-\f
 /* Script variables. */
 
 /* Variable type. */
 enum
   {
     NUMERIC,                   /* A numeric variable. */
-    ALPHA                      /* A string variable.  (STRING is pre-empted by lexer.h) */
+    ALPHA                      /* A string variable.
+                                   (STRING is pre-empted by lexer.h.) */
   };
 
 /* Types of missing values.  Order is significant, see
-   mis-val.c:parse_numeric(), sfm-read.c:sfm_read_dictionary()
-   sfm-write.c:sfm_write_dictionary(),
+   mis-val.c:parse_numeric(), sfm-read.c, sfm-write.c,
    sysfile-info.c:cmd_sysfile_info(), mis-val.c:copy_missing_values(),
    pfm-read.c:read_variables(), pfm-write.c:write_variables(),
    apply-dict.c:cmd_apply_dictionary(), and more (?). */
@@ -227,20 +59,21 @@ enum
     MISSING_COUNT
   };
 
+
 /* A variable's dictionary entry.  */
 struct variable
   {
-    char name[9];              /* As a string. */
-    int index;                 /* Index into its dictionary's var[]. */
+    /* Basic information. */
+    char name[LONG_NAME_LEN + 1]; /* Variable name.  Mixed case. */
     int type;                   /* NUMERIC or ALPHA. */
-
-    /* Also important but parse_variables() doesn't need it.  Still,
-       check before reordering. */
     int width;                 /* Size of string variables in chars. */
     int fv, nv;                        /* Index into `value's, number of values. */
     unsigned init : 1;          /* 1=VFM must init and possibly reinit. */
     unsigned reinit : 1;        /* Cases are: 1=reinitialized; 0=left. */
 
+    /* Data for use by containing dictionary. */
+    int index;                 /* Dictionary index. */
+
     /* Missing values. */
     int miss_type;             /* One of the MISSING_* constants. */
     union value missing[3];    /* User-missing value. */
@@ -250,29 +83,46 @@ struct variable
     struct fmt_spec write;     /* Default format for WRITE. */
 
     /* Labels. */
-    struct val_labs *val_labs;
+    struct val_labs *val_labs;  /* Value labels. */
     char *label;               /* Variable label. */
 
-    /* Per-procedure info. */
+    /* GUI display parameters. */
+    enum measure measure;       /* Nominal ordinal or continuous */
+    int display_width;          /* Width of data editor column */
+    enum alignment alignment;   /* Alignment of data in gui */
+
+    /* Short name, used only for system and portable file input
+       and output.  Upper case only.  There is no index for short
+       names.  Short names are not necessarily unique.  Any
+       variable may have no short name, indicated by an empty
+       string. */
+    char short_name[SHORT_NAME_LEN + 1];
+
+    /* Per-command info. */
     void *aux;
-    struct get_proc get;
-    union
-      {
-       struct crosstab_proc crs;
-       struct descriptives_proc dsc;
-       struct frequencies_proc frq;
-       struct list_proc lst;
-       struct means_proc mns;
-       struct sort_cases_proc srt;
-       struct matrix_data_proc mxd;
-       struct match_files_proc mtf;
-       struct t_test_proc t_t;
-      }
-    p;
+    void (*aux_dtor) (struct variable *);
   };
 
-int compare_variables (const void *, const void *, void *);
-unsigned hash_variable (const void *, void *);
+/* Variable names. */
+bool var_is_valid_name (const char *, bool issue_error);
+int compare_var_names (const void *, const void *, void *);
+unsigned hash_var_name (const void *, void *);
+
+/* Short names. */
+void var_set_short_name (struct variable *, const char *);
+void var_set_short_name_suffix (struct variable *, const char *, int suffix);
+void var_clear_short_name (struct variable *);
+
+/* Pointers to `struct variable', by name. */
+int compare_var_ptr_names (const void *, const void *, void *);
+unsigned hash_var_ptr_name (const void *, void *);
+
+/* Variable auxiliary data. */
+void *var_attach_aux (struct variable *,
+                      void *aux, void (*aux_dtor) (struct variable *));
+void var_clear_aux (struct variable *);
+void *var_detach_aux (struct variable *);
+void var_dtor_free (struct variable *);
 
 /* Classes of variables. */
 enum dict_class 
@@ -289,92 +139,11 @@ const char *dict_class_to_name (enum dict_class dict_class);
 struct vector
   {
     int idx;                    /* Index for dict_get_vector(). */
-    char name[9];              /* Name. */
+    char name[LONG_NAME_LEN + 1]; /* Name. */
     struct variable **var;     /* Vector of variables. */
     int cnt;                   /* Number of variables. */
   };
 \f
-/* Cases. */
-
-/* A single case.  (This doesn't need to be a struct anymore, but it
-   remains so for hysterical raisins.) */
-struct ccase
-  {
-    union value data[1];
-  };
-\f
-/* Dictionary. */ 
-
-/* Complete dictionary state. */
-struct dictionary;
-
-struct dictionary *dict_create (void);
-struct dictionary *dict_clone (const struct dictionary *);
-void dict_clear (struct dictionary *);
-void dict_destroy (struct dictionary *);
-
-size_t dict_get_var_cnt (const struct dictionary *);
-struct variable *dict_get_var (const struct dictionary *, size_t idx);
-void dict_get_vars (const struct dictionary *,
-                    struct variable ***vars, size_t *cnt,
-                    unsigned exclude_classes);
-
-struct variable *dict_create_var (struct dictionary *, const char *,
-                                  int width);
-struct variable *dict_create_var_assert (struct dictionary *, const char *,
-                                  int width);
-struct variable *dict_clone_var (struct dictionary *, const struct variable *,
-                                 const char *);
-void dict_rename_var (struct dictionary *, struct variable *, const char *);
-
-struct variable *dict_lookup_var (const struct dictionary *, const char *);
-struct variable *dict_lookup_var_assert (const struct dictionary *,
-                                         const char *);
-int dict_contains_var (const struct dictionary *, const struct variable *);
-void dict_delete_var (struct dictionary *, struct variable *);
-void dict_delete_vars (struct dictionary *,
-                       struct variable *const *, size_t count);
-void dict_reorder_vars (struct dictionary *,
-                        struct variable *const *, size_t count);
-int dict_rename_vars (struct dictionary *,
-                      struct variable **, char **new_names,
-                      size_t count, char **err_name);
-
-struct variable *dict_get_weight (const struct dictionary *);
-double dict_get_case_weight (const struct dictionary *, const struct ccase *);
-void dict_set_weight (struct dictionary *, struct variable *);
-
-struct variable *dict_get_filter (const struct dictionary *);
-void dict_set_filter (struct dictionary *, struct variable *);
-
-int dict_get_case_limit (const struct dictionary *);
-void dict_set_case_limit (struct dictionary *, int);
-
-int dict_get_next_value_idx (const struct dictionary *);
-size_t dict_get_case_size (const struct dictionary *);
-void dict_compact_values (struct dictionary *);
-
-struct variable *const *dict_get_split_vars (const struct dictionary *);
-size_t dict_get_split_cnt (const struct dictionary *);
-void dict_set_split_vars (struct dictionary *,
-                          struct variable *const *, size_t cnt);
-
-const char *dict_get_label (const struct dictionary *);
-void dict_set_label (struct dictionary *, const char *);
-
-const char *dict_get_documents (const struct dictionary *);
-void dict_set_documents (struct dictionary *, const char *);
-
-int dict_create_vector (struct dictionary *,
-                        const char *name,
-                        struct variable **, size_t cnt);
-const struct vector *dict_get_vector (const struct dictionary *,
-                                      size_t idx);
-size_t dict_get_vector_cnt (const struct dictionary *);
-const struct vector *dict_lookup_vector (const struct dictionary *,
-                                         const char *name);
-void dict_clear_vectors (struct dictionary *);
-\f
 void discard_variables (void);
 
 /* This is the active file dictionary. */
@@ -411,7 +180,9 @@ void cancel_temporary (void);
 \f
 /* Functions. */
 
+struct ccase;
 void dump_split_vars (const struct ccase *);
+typedef int (* is_missing_func )(const union value *, const struct variable *);
 
 int is_num_user_missing (double, const struct variable *);
 int is_str_user_missing (const unsigned char[], const struct variable *);
@@ -422,17 +193,16 @@ void copy_missing_values (struct variable *dest, const struct variable *src);
 \f
 /* Transformations. */
 
+struct trns_header;
+typedef int trns_proc_func (struct trns_header *, struct ccase *, int);
+typedef void trns_free_func (struct trns_header *);
+
 /* Header for all transformations. */
 struct trns_header
   {
-    /* Index into t_trns[]. */
-    int index;
-
-    /* Transformation proc. */
-    int (*proc) (struct trns_header *, struct ccase *);
-
-    /* Garbage collector proc. */
-    void (*free) (struct trns_header *);
+    int index;                  /* Index into t_trns[]. */
+    trns_proc_func *proc;       /* Transformation proc. */
+    trns_free_func *free;       /* Garbage collector proc. */
   };
 
 /* Array of transformations */
@@ -450,14 +220,16 @@ void cancel_transformations (void);
 \f
 struct var_set;
 
-struct var_set *var_set_create_from_dict (struct dictionary *d);
-struct var_set *var_set_create_from_array (struct variable **var, size_t);
+struct var_set *var_set_create_from_dict (const struct dictionary *d);
+struct var_set *var_set_create_from_array (struct variable *const *var,
+                                           size_t);
 
-size_t var_set_get_cnt (struct var_set *vs);
-struct variable *var_set_get_var (struct var_set *vs, size_t idx);
-struct variable *var_set_lookup_var (struct var_set *vs, const char *name);
+size_t var_set_get_cnt (const struct var_set *vs);
+struct variable *var_set_get_var (const struct var_set *vs, size_t idx);
+struct variable *var_set_lookup_var (const struct var_set *vs,
+                                     const char *name);
+int var_set_lookup_var_idx (const struct var_set *vs, const char *name);
 void var_set_destroy (struct var_set *vs);
-
 \f
 /* Variable parsers. */
 
@@ -471,16 +243,25 @@ enum
     PV_NUMERIC = 0020,         /* Vars must be numeric. */
     PV_STRING = 0040,          /* Vars must be string. */
     PV_SAME_TYPE = 00100,      /* All vars must be the same type. */
-    PV_NO_SCRATCH = 00200,     /* Disallow scratch variables. */
+    PV_NO_SCRATCH = 00200      /* Disallow scratch variables. */
   };
 
 struct variable *parse_variable (void);
-struct variable *parse_dict_variable (struct dictionary *);
-int parse_variables (struct dictionary *, struct variable ***, int *,
+struct variable *parse_dict_variable (const struct dictionary *);
+int parse_variables (const struct dictionary *, struct variable ***, int *,
                      int opts);
-int parse_var_set_vars (struct var_set *, struct variable ***, int *,
+int parse_var_set_vars (const struct var_set *, struct variable ***, int *,
                         int opts);
 int parse_DATA_LIST_vars (char ***names, int *cnt, int opts);
 int parse_mixed_vars (char ***names, int *cnt, int opts);
 
+
+
+/* Return a string representing this variable, in the form most 
+   appropriate from a human factors perspective.
+   (IE: the label if it has one, otherwise the name )
+*/
+const char * var_to_string(const struct variable *var);
+
+
 #endif /* !var_h */