1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #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>
30 #include <data/casefile-factory.h>
36 /* Information about storage sink. */
37 struct storage_sink_info
39 struct casefile *casefile; /* Storage. */
42 static struct storage_sink_info *
43 get_storage_sink_info (struct case_sink *sink)
45 assert (sink->class == &storage_sink_class);
49 /* Initializes a storage sink. */
51 storage_sink_open (struct case_sink *sink)
53 struct storage_sink_info *info;
55 sink->aux = info = xmalloc (sizeof *info);
56 info->casefile = sink->factory->create_casefile (sink->factory,
60 /* Writes case C to the storage sink SINK.
61 Returns true if successful, false if an I/O error occurred. */
63 storage_sink_write (struct case_sink *sink, const struct ccase *c)
65 struct storage_sink_info *info = get_storage_sink_info (sink);
66 return casefile_append (info->casefile, c);
69 /* Destroys internal data in SINK. */
71 storage_sink_destroy (struct case_sink *sink)
73 struct storage_sink_info *info = get_storage_sink_info (sink);
74 casefile_destroy (info->casefile);
78 /* Closes the sink and returns a storage source to read back the
80 static struct case_source *
81 storage_sink_make_source (struct case_sink *sink)
83 struct storage_sink_info *info = get_storage_sink_info (sink);
84 struct case_source *source = storage_source_create (info->casefile);
85 info->casefile = NULL;
90 const struct case_sink_class storage_sink_class =
96 storage_sink_make_source,
101 struct storage_source_info
103 struct casefile *casefile; /* Storage. */
104 struct casereader *reader; /* Reader. */
107 static struct storage_source_info *
108 get_storage_source_info (const struct case_source *source)
110 assert (source->class == &storage_source_class);
114 /* Returns the number of cases that will be read by
115 storage_source_read(). */
117 storage_source_count (const struct case_source *source)
119 struct storage_source_info *info = get_storage_source_info (source);
120 return casefile_get_case_cnt (info->casefile);
123 /* Reads one case into OUTPUT_CASE.
124 Returns true if successful, false at end of file or if an
125 I/O error occurred. */
127 storage_source_read (struct case_source *source, struct ccase *output_case)
129 struct storage_source_info *info = get_storage_source_info (source);
130 struct ccase casefile_case;
132 if (info->reader == NULL)
133 info->reader = casefile_get_reader (info->casefile, NULL);
135 if (casereader_read (info->reader, &casefile_case))
137 case_copy (output_case, 0,
139 casefile_get_value_cnt (info->casefile));
146 /* Destroys the source.
147 Returns true if successful read, false if an I/O occurred
148 during destruction or previously. */
150 storage_source_destroy (struct case_source *source)
152 struct storage_source_info *info = get_storage_source_info (source);
156 ok = !casefile_error (info->casefile);
157 casefile_destroy (info->casefile);
163 /* Returns the casefile encapsulated by SOURCE. */
165 storage_source_get_casefile (struct case_source *source)
167 struct storage_source_info *info = get_storage_source_info (source);
168 return info->casefile;
171 /* Destroys SOURCE and returns the casefile that it
174 storage_source_decapsulate (struct case_source *source)
176 struct storage_source_info *info = get_storage_source_info (source);
177 struct casefile *casefile = info->casefile;
178 assert (info->reader == NULL);
179 info->casefile = NULL;
180 free_case_source (source);
184 /* Creates and returns a new storage source that encapsulates
187 storage_source_create (struct casefile *casefile)
189 struct storage_source_info *info;
191 info = xmalloc (sizeof *info);
192 info->casefile = casefile;
195 return create_case_source (&storage_source_class, info);
198 /* Storage source. */
199 const struct case_source_class storage_source_class =
202 storage_source_count,
204 storage_source_destroy,