Work on getting rid of trns_chain_finalize().
[pspp] / src / data / transformations.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2011, 2013, 2015 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 #include <config.h>
18
19 #include "data/transformations.h"
20
21 #include <assert.h>
22 #include <stdlib.h>
23
24 #include "libpspp/str.h"
25 #include "data/control-stack.h" /* XXX layering violation */
26
27 #include "gl/xalloc.h"
28
29 /* A single transformation. */
30 struct transformation
31   {
32     /* Offset to add to EXECUTE's return value, if it returns a
33        transformation index.  Normally 0 but set to the starting
34        index of a spliced chain after splicing. */
35     int idx_ofs;
36     trns_proc_func *execute;            /* Executes the transformation. */
37     trns_free_func *free;               /* Garbage collector proc. */
38     void *aux;                          /* Auxiliary data. */
39   };
40
41 /* A chain of transformations. */
42 struct trns_chain
43   {
44     struct transformation *trns;        /* Array of transformations. */
45     size_t trns_cnt;                    /* Number of transformations. */
46     size_t trns_cap;                    /* Allocated capacity. */
47   };
48
49 /* Allocates and returns a new transformation chain. */
50 struct trns_chain *
51 trns_chain_create (void)
52 {
53   struct trns_chain *chain = xmalloc (sizeof *chain);
54   chain->trns = NULL;
55   chain->trns_cnt = 0;
56   chain->trns_cap = 0;
57   return chain;
58 }
59
60 /* Destroys CHAIN. */
61 bool
62 trns_chain_destroy (struct trns_chain *chain)
63 {
64   bool ok = true;
65
66   if (chain != NULL)
67     {
68       size_t i;
69
70       for (i = 0; i < chain->trns_cnt; i++)
71         {
72           struct transformation *trns = &chain->trns[i];
73           if (trns->free != NULL)
74             ok = trns->free (trns->aux) && ok;
75         }
76       free (chain->trns);
77       free (chain);
78     }
79
80   return ok;
81 }
82
83 /* Returns true if CHAIN contains any transformations,
84    false otherwise. */
85 bool
86 trns_chain_is_empty (const struct trns_chain *chain)
87 {
88   return chain->trns_cnt == 0;
89 }
90
91 /* Adds a transformation to CHAIN with execute function EXECUTE, free function
92    FREE, and auxiliary data AUX. */
93 void
94 trns_chain_append (struct trns_chain *chain, trns_proc_func *execute,
95                    trns_free_func *free, void *aux)
96 {
97   struct transformation *trns;
98
99   if (chain->trns_cnt == chain->trns_cap)
100     chain->trns = x2nrealloc (chain->trns, &chain->trns_cap,
101                               sizeof *chain->trns);
102
103   trns = &chain->trns[chain->trns_cnt++];
104   trns->idx_ofs = 0;
105   trns->execute = execute;
106   trns->free = free;
107   trns->aux = aux;
108 }
109
110 /* Appends the transformations in SRC to those in DST, and destroys SRC. */
111 void
112 trns_chain_splice (struct trns_chain *dst, struct trns_chain *src)
113 {
114   size_t i;
115
116   if (dst->trns_cnt + src->trns_cnt > dst->trns_cap)
117     {
118       dst->trns_cap = dst->trns_cnt + src->trns_cnt;
119       dst->trns = xnrealloc (dst->trns, dst->trns_cap, sizeof *dst->trns);
120     }
121
122   for (i = 0; i < src->trns_cnt; i++)
123     {
124       struct transformation *d = &dst->trns[i + dst->trns_cnt];
125       const struct transformation *s = &src->trns[i];
126       *d = *s;
127       d->idx_ofs += src->trns_cnt;
128     }
129   dst->trns_cnt += src->trns_cnt;
130
131   src->trns_cnt = 0;
132   trns_chain_destroy (src);
133 }
134
135 /* Returns the index that a transformation execution function may
136    return to "jump" to the next transformation to be added. */
137 size_t
138 trns_chain_next (struct trns_chain *chain)
139 {
140   return chain->trns_cnt;
141 }
142
143 /* Executes the given CHAIN of transformations on *C,
144    passing CASE_NR as the case number.
145    *C may be replaced by a new case.
146    Returns the result code that caused the transformations to
147    terminate, or TRNS_CONTINUE if the transformations finished
148    due to "falling off the end" of the set of transformations. */
149 enum trns_result
150 trns_chain_execute (const struct trns_chain *chain, enum trns_result start,
151                     struct ccase **c, casenumber case_nr)
152 {
153   int i;
154
155   for (i = start < 0 ? 0 : start; i < chain->trns_cnt; )
156     {
157       struct transformation *trns = &chain->trns[i];
158       int retval;
159
160       retval = trns->execute (trns->aux, c, case_nr);
161       switch (retval)
162         {
163         case TRNS_CONTINUE:
164           i++;
165           break;
166
167         case TRNS_END_CASE:
168           return i + 1;
169
170         case TRNS_DROP_CASE:
171         case TRNS_ERROR:
172         case TRNS_END_FILE:
173           return retval;
174
175         default:
176           i += retval;
177           assert (i <= chain->trns_cnt);
178           break;
179         }
180     }
181
182   return TRNS_CONTINUE;
183 }