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>
27 #include <data/case-sink.h>
28 #include <data/case-source.h>
29 #include <data/case.h>
30 #include <data/casefile.h>
31 #include <data/fastfile.h>
35 /* Information about storage sink or source. */
36 struct storage_stream_info
38 struct casefile *casefile; /* Storage. */
43 /* Initializes a storage sink. */
45 storage_sink_open (struct case_sink *sink)
47 struct storage_stream_info *info;
49 sink->aux = info = xmalloc (sizeof *info);
50 info->casefile = fastfile_create (sink->value_cnt);
53 /* Destroys storage stream represented by INFO. */
55 destroy_storage_stream_info (struct storage_stream_info *info)
59 casefile_destroy (info->casefile);
64 /* Writes case C to the storage sink SINK.
65 Returns true if successful, false if an I/O error occurred. */
67 storage_sink_write (struct case_sink *sink, const struct ccase *c)
69 struct storage_stream_info *info = sink->aux;
71 return casefile_append (info->casefile, c);
74 /* Destroys internal data in SINK. */
76 storage_sink_destroy (struct case_sink *sink)
78 destroy_storage_stream_info (sink->aux);
81 /* Closes the sink and returns a storage source to read back the
83 static struct case_source *
84 storage_sink_make_source (struct case_sink *sink)
86 struct case_source *source
87 = create_case_source (&storage_source_class, sink->aux);
93 const struct case_sink_class storage_sink_class =
99 storage_sink_make_source,
102 /* Storage source. */
104 /* Returns the number of cases that will be read by
105 storage_source_read(). */
107 storage_source_count (const struct case_source *source)
109 struct storage_stream_info *info = source->aux;
111 return casefile_get_case_cnt (info->casefile);
114 /* Reads all cases from the storage source and passes them one by one to
117 storage_source_read (struct case_source *source,
118 struct ccase *output_case,
119 write_case_func *write_case, write_case_data wc_data)
121 struct storage_stream_info *info = source->aux;
122 struct ccase casefile_case;
123 struct casereader *reader;
126 for (reader = casefile_get_reader (info->casefile, NULL);
127 ok && casereader_read (reader, &casefile_case);
128 case_destroy (&casefile_case))
130 case_copy (output_case, 0,
132 casefile_get_value_cnt (info->casefile));
133 ok = write_case (wc_data);
135 casereader_destroy (reader);
140 /* Destroys the source's internal data. */
142 storage_source_destroy (struct case_source *source)
144 destroy_storage_stream_info (source->aux);
147 /* Storage source. */
148 const struct case_source_class storage_source_class =
151 storage_source_count,
153 storage_source_destroy,
156 /* Returns the casefile encapsulated by SOURCE. */
158 storage_source_get_casefile (struct case_source *source)
160 struct storage_stream_info *info = source->aux;
162 assert (source->class == &storage_source_class);
163 return info->casefile;
166 /* Destroys SOURCE and returns the casefile that it
169 storage_source_decapsulate (struct case_source *source)
171 struct storage_stream_info *info = source->aux;
172 struct casefile *casefile;
174 assert (source->class == &storage_source_class);
175 casefile = info->casefile;
176 info->casefile = NULL;
177 free_case_source (source);
181 /* Creates and returns a new storage stream that encapsulates
184 storage_source_create (struct casefile *casefile)
186 struct storage_stream_info *info;
188 info = xmalloc (sizeof *info);
189 info->casefile = casefile;
191 return create_case_source (&storage_source_class, info);