1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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, see <http://www.gnu.org/licenses/>. */
18 #include "any-reader.h"
24 #include <libpspp/assertion.h>
25 #include <libpspp/message.h>
26 #include "file-handle-def.h"
27 #include "file-name.h"
28 #include "por-file-reader.h"
29 #include "sys-file-reader.h"
30 #include <libpspp/str.h>
31 #include "scratch-reader.h"
35 #define _(msgid) gettext (msgid)
37 /* Result of type detection. */
40 YES, /* It is this type. */
41 NO, /* It is not this type. */
42 IO_ERROR /* File couldn't be opened. */
45 /* Tries to detect whether HANDLE represents a given type of
46 file, by opening the file and passing it to DETECT, and
47 returns a detect_result. */
48 static enum detect_result
49 try_detect (struct file_handle *handle, bool (*detect) (FILE *))
54 file = fn_open (fh_get_file_name (handle), "rb");
57 msg (ME, _("An error occurred while opening \"%s\": %s."),
58 fh_get_file_name (handle), strerror (errno));
62 is_type = detect (file);
64 fn_close (fh_get_file_name (handle), file);
66 return is_type ? YES : NO;
69 /* Returns a casereader for HANDLE. On success, returns the new
70 casereader and stores the file's dictionary into *DICT. On
71 failure, returns a null pointer. */
73 any_reader_open (struct file_handle *handle, struct dictionary **dict)
75 switch (fh_get_referent (handle))
79 enum detect_result result;
81 result = try_detect (handle, sfm_detect);
82 if (result == IO_ERROR)
84 else if (result == YES)
85 return sfm_open_reader (handle, dict, NULL);
87 result = try_detect (handle, pfm_detect);
88 if (result == IO_ERROR)
90 else if (result == YES)
91 return pfm_open_reader (handle, dict, NULL);
93 msg (SE, _("\"%s\" is not a system or portable file."),
94 fh_get_file_name (handle));
99 msg (SE, _("The inline file is not allowed here."));
103 return scratch_reader_open (handle, dict);