3df80cb08fb8d38acc302e0654a0217dd666287a
[pspp-builds.git] / src / data / casereader.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 /* Casereader client interface.
18
19    A casereader abstracts interfaces through which cases may be
20    read.  A casereader may be a front-end for a system file, a
21    portable file, the active file in a data set, or anything else
22    on which a casereader interface has been overlaid.  Casereader
23    layering, in which a casereader acts as a filter or translator
24    on top of another casereader, is also supported.
25
26    There is no central interface for obtaining casereaders: a
27    casereader for reading a system file is obtained from the
28    system file reading module, and so on.  Once a casereader has
29    been obtained, by whatever means, the interface to it is
30    uniform.  The most important functions for casereader usage
31    are:
32
33      - casereader_read: Reads a case from the casereader.  The
34        case is consumed and cannot be read again.  The caller is
35        responsible for destroying the case.
36
37      - casereader_clone: Makes a copy of a casereader.  May be
38        used to read one or a set of cases from a casereader
39        repeatedly.
40
41      - casereader_destroy: Destroys a casereader.
42
43    Casereaders can encounter error conditions, such as I/O
44    errors, as they read cases.  Error conditions prevent any more
45    cases from being read from the casereader.  Error conditions
46    are reported by casereader_error.  Error condition may be
47    propagated to or from a casereader with taint_propagate using
48    the casereader's taint object, which may be obtained with
49    casereader_get_taint. */
50
51 #ifndef DATA_CASEREADER_H
52 #define DATA_CASEREADER_H 1
53
54 #include <libpspp/compiler.h>
55 #include <data/case.h>
56 #include <data/missing-values.h>
57
58 struct dictionary;
59 struct casereader;
60 struct casewriter;
61
62 bool casereader_read (struct casereader *, struct ccase *);
63 bool casereader_destroy (struct casereader *);
64
65 struct casereader *casereader_clone (const struct casereader *);
66 void casereader_split (struct casereader *,
67                        struct casereader **, struct casereader **);
68 struct casereader *casereader_rename (struct casereader *);
69 void casereader_swap (struct casereader *, struct casereader *);
70
71 bool casereader_peek (struct casereader *, casenumber, struct ccase *)
72      WARN_UNUSED_RESULT;
73 bool casereader_is_empty (struct casereader *);
74
75 bool casereader_error (const struct casereader *);
76 void casereader_force_error (struct casereader *);
77 const struct taint *casereader_get_taint (const struct casereader *);
78
79 casenumber casereader_get_case_cnt (struct casereader *);
80 casenumber casereader_count_cases (struct casereader *);
81 size_t casereader_get_value_cnt (struct casereader *);
82
83 void casereader_transfer (struct casereader *, struct casewriter *);
84 \f
85 struct casereader *
86 casereader_create_filter_func (struct casereader *,
87                                bool (*include) (const struct ccase *,
88                                                 void *aux),
89                                bool (*destroy) (void *aux),
90                                void *aux,
91                                struct casewriter *exclude);
92 struct casereader *
93 casereader_create_filter_weight (struct casereader *,
94                                  const struct dictionary *dict,
95                                  bool *warn_on_invalid,
96                                  struct casewriter *exclude);
97 struct casereader *
98 casereader_create_filter_missing (struct casereader *,
99                                   const struct variable **vars, size_t var_cnt,
100                                   enum mv_class,
101                                   casenumber *n_missing,
102                                   struct casewriter *exclude);
103
104 struct casereader *
105 casereader_create_counter (struct casereader *, casenumber *counter,
106                            casenumber initial_value);
107
108 struct casereader *
109 casereader_create_translator (struct casereader *, size_t output_value_cnt,
110                               void (*translate) (struct ccase *input,
111                                                  struct ccase *output,
112                                                  void *aux),
113                               bool (*destroy) (void *aux),
114                               void *aux);
115
116 /* A function which creates a numberic value from an existing case */
117 typedef double new_value_func (const struct ccase *, casenumber, void *);
118
119 struct casereader *
120 casereader_create_append_numeric (struct casereader *subreader,
121                                   new_value_func func, void *aux,
122                                   void (*destroy) (void *aux));
123
124 struct casereader *
125 casereader_create_arithmetic_sequence (struct casereader *,
126                                        double first, double increment);
127
128 enum rank_error
129   {
130     RANK_ERR_NONE = 0,
131     RANK_ERR_NEGATIVE_WEIGHT = 0x01,
132     RANK_ERR_UNSORTED = 0x02
133   };
134
135
136 typedef void distinct_func (double v, casenumber n, double w, void *aux);
137
138 struct casereader *
139 casereader_create_append_rank (struct casereader *,
140                                const struct variable *v, const struct variable *w,
141                                enum rank_error *err,
142                                distinct_func *distinct_callback, void *aux);
143
144
145 #endif /* data/casereader.h */