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/fastfile.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 = fastfile_create (sink->value_cnt);
59 /* Writes case C to the storage sink SINK.
60 Returns true if successful, false if an I/O error occurred. */
62 storage_sink_write (struct case_sink *sink, const struct ccase *c)
64 struct storage_sink_info *info = get_storage_sink_info (sink);
65 return casefile_append (info->casefile, c);
68 /* Destroys internal data in SINK. */
70 storage_sink_destroy (struct case_sink *sink)
72 struct storage_sink_info *info = get_storage_sink_info (sink);
73 casefile_destroy (info->casefile);
77 /* Closes the sink and returns a storage source to read back the
79 static struct case_source *
80 storage_sink_make_source (struct case_sink *sink)
82 struct storage_sink_info *info = get_storage_sink_info (sink);
83 struct case_source *source = storage_source_create (info->casefile);
84 info->casefile = NULL;
89 const struct case_sink_class storage_sink_class =
95 storage_sink_make_source,
100 struct storage_source_info
102 struct casefile *casefile; /* Storage. */
103 struct casereader *reader; /* Reader. */
106 static struct storage_source_info *
107 get_storage_source_info (const struct case_source *source)
109 assert (source->class == &storage_source_class);
113 /* Returns the number of cases that will be read by
114 storage_source_read(). */
116 storage_source_count (const struct case_source *source)
118 struct storage_source_info *info = get_storage_source_info (source);
119 return casefile_get_case_cnt (info->casefile);
122 /* Reads one case into OUTPUT_CASE.
123 Returns true if successful, false at end of file or if an
124 I/O error occurred. */
126 storage_source_read (struct case_source *source, struct ccase *output_case)
128 struct storage_source_info *info = get_storage_source_info (source);
129 struct ccase casefile_case;
131 if (info->reader == NULL)
132 info->reader = casefile_get_reader (info->casefile, NULL);
134 if (casereader_read (info->reader, &casefile_case))
136 case_copy (output_case, 0,
138 casefile_get_value_cnt (info->casefile));
145 /* Destroys the source.
146 Returns true if successful read, false if an I/O occurred
147 during destruction or previously. */
149 storage_source_destroy (struct case_source *source)
151 struct storage_source_info *info = get_storage_source_info (source);
155 ok = !casefile_error (info->casefile);
156 casefile_destroy (info->casefile);
162 /* Returns the casefile encapsulated by SOURCE. */
164 storage_source_get_casefile (struct case_source *source)
166 struct storage_source_info *info = get_storage_source_info (source);
167 return info->casefile;
170 /* Destroys SOURCE and returns the casefile that it
173 storage_source_decapsulate (struct case_source *source)
175 struct storage_source_info *info = get_storage_source_info (source);
176 struct casefile *casefile = info->casefile;
177 assert (info->reader == NULL);
178 info->casefile = NULL;
179 free_case_source (source);
183 /* Creates and returns a new storage source that encapsulates
186 storage_source_create (struct casefile *casefile)
188 struct storage_source_info *info;
190 info = xmalloc (sizeof *info);
191 info->casefile = casefile;
194 return create_case_source (&storage_source_class, info);
197 /* Storage source. */
198 const struct case_source_class storage_source_class =
201 storage_source_count,
203 storage_source_destroy,