1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <data/storage-stream.h>
26 #include <data/case-sink.h>
27 #include <data/case-source.h>
28 #include <data/case.h>
29 #include <data/casefile.h>
33 /* Information about storage sink or source. */
34 struct storage_stream_info
36 struct casefile *casefile; /* Storage. */
41 /* Initializes a storage sink. */
43 storage_sink_open (struct case_sink *sink)
45 struct storage_stream_info *info;
47 sink->aux = info = xmalloc (sizeof *info);
48 info->casefile = casefile_create (sink->value_cnt);
51 /* Destroys storage stream represented by INFO. */
53 destroy_storage_stream_info (struct storage_stream_info *info)
57 casefile_destroy (info->casefile);
62 /* Writes case C to the storage sink SINK.
63 Returns true if successful, false if an I/O error occurred. */
65 storage_sink_write (struct case_sink *sink, const struct ccase *c)
67 struct storage_stream_info *info = sink->aux;
69 return casefile_append (info->casefile, c);
72 /* Destroys internal data in SINK. */
74 storage_sink_destroy (struct case_sink *sink)
76 destroy_storage_stream_info (sink->aux);
79 /* Closes the sink and returns a storage source to read back the
81 static struct case_source *
82 storage_sink_make_source (struct case_sink *sink)
84 struct case_source *source
85 = create_case_source (&storage_source_class, sink->aux);
91 const struct case_sink_class storage_sink_class =
97 storage_sink_make_source,
100 /* Storage source. */
102 /* Returns the number of cases that will be read by
103 storage_source_read(). */
105 storage_source_count (const struct case_source *source)
107 struct storage_stream_info *info = source->aux;
109 return casefile_get_case_cnt (info->casefile);
112 /* Reads all cases from the storage source and passes them one by one to
115 storage_source_read (struct case_source *source,
116 struct ccase *output_case,
117 write_case_func *write_case, write_case_data wc_data)
119 struct storage_stream_info *info = source->aux;
120 struct ccase casefile_case;
121 struct casereader *reader;
124 for (reader = casefile_get_reader (info->casefile);
125 ok && casereader_read (reader, &casefile_case);
126 case_destroy (&casefile_case))
128 case_copy (output_case, 0,
130 casefile_get_value_cnt (info->casefile));
131 ok = write_case (wc_data);
133 casereader_destroy (reader);
138 /* Destroys the source's internal data. */
140 storage_source_destroy (struct case_source *source)
142 destroy_storage_stream_info (source->aux);
145 /* Storage source. */
146 const struct case_source_class storage_source_class =
149 storage_source_count,
151 storage_source_destroy,
154 /* Returns the casefile encapsulated by SOURCE. */
156 storage_source_get_casefile (struct case_source *source)
158 struct storage_stream_info *info = source->aux;
160 assert (source->class == &storage_source_class);
161 return info->casefile;
164 /* Destroys SOURCE and returns the casefile that it
167 storage_source_decapsulate (struct case_source *source)
169 struct storage_stream_info *info = source->aux;
170 struct casefile *casefile;
172 assert (source->class == &storage_source_class);
173 casefile = info->casefile;
174 info->casefile = NULL;
175 free_case_source (source);
179 /* Creates and returns a new storage stream that encapsulates
182 storage_source_create (struct casefile *casefile)
184 struct storage_stream_info *info;
186 info = xmalloc (sizeof *info);
187 info->casefile = casefile;
189 return create_case_source (&storage_source_class, info);