1 /* PSPP - computes sample statistics.
2 Copyright (C) 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
20 #include "any-reader.h"
26 #include <libpspp/assertion.h>
27 #include <libpspp/message.h>
28 #include "file-handle-def.h"
29 #include "file-name.h"
30 #include "por-file-reader.h"
31 #include "sys-file-reader.h"
32 #include <libpspp/str.h>
33 #include "scratch-reader.h"
37 #define _(msgid) gettext (msgid)
39 /* Result of type detection. */
42 YES, /* It is this type. */
43 NO, /* It is not this type. */
44 IO_ERROR /* File couldn't be opened. */
47 /* Tries to detect whether HANDLE represents a given type of
48 file, by opening the file and passing it to DETECT, and
49 returns a detect_result. */
50 static enum detect_result
51 try_detect (struct file_handle *handle, bool (*detect) (FILE *))
56 file = fn_open (fh_get_file_name (handle), "rb");
59 msg (ME, _("An error occurred while opening \"%s\": %s."),
60 fh_get_file_name (handle), strerror (errno));
64 is_type = detect (file);
66 fn_close (fh_get_file_name (handle), file);
68 return is_type ? YES : NO;
71 /* Returns a casereader for HANDLE. On success, returns the new
72 casereader and stores the file's dictionary into *DICT. On
73 failure, returns a null pointer. */
75 any_reader_open (struct file_handle *handle, struct dictionary **dict)
77 switch (fh_get_referent (handle))
81 enum detect_result result;
83 result = try_detect (handle, sfm_detect);
84 if (result == IO_ERROR)
86 else if (result == YES)
87 return sfm_open_reader (handle, dict, NULL);
89 result = try_detect (handle, pfm_detect);
90 if (result == IO_ERROR)
92 else if (result == YES)
93 return pfm_open_reader (handle, dict, NULL);
95 msg (SE, _("\"%s\" is not a system or portable file."),
96 fh_get_file_name (handle));
101 msg (SE, _("The inline file is not allowed here."));
105 return scratch_reader_open (handle, dict);