18a28c6d1ed14231dcb1be18c90d18c7d1dac08e
[pspp-builds.git] / src / data / any-reader.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
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.
8
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.
13
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
17    02110-1301, USA. */
18
19 #include <config.h>
20 #include "any-reader.h"
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.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"
34 #include "xalloc.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 /* Result of type detection. */
40 enum detect_result
41   {
42     YES,                        /* It is this type. */
43     NO,                         /* It is not this type. */
44     IO_ERROR                    /* File couldn't be opened. */
45   };
46
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 *))
52 {
53   FILE *file;
54   bool is_type;
55
56   file = fn_open (fh_get_file_name (handle), "rb");
57   if (file == NULL)
58     {
59       msg (ME, _("An error occurred while opening \"%s\": %s."),
60            fh_get_file_name (handle), strerror (errno));
61       return IO_ERROR;
62     }
63
64   is_type = detect (file);
65
66   fn_close (fh_get_file_name (handle), file);
67
68   return is_type ? YES : NO;
69 }
70
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. */
74 struct casereader *
75 any_reader_open (struct file_handle *handle, struct dictionary **dict)
76 {
77   switch (fh_get_referent (handle))
78     {
79     case FH_REF_FILE:
80       {
81         enum detect_result result;
82
83         result = try_detect (handle, sfm_detect);
84         if (result == IO_ERROR)
85           return NULL;
86         else if (result == YES)
87           return sfm_open_reader (handle, dict, NULL);
88
89         result = try_detect (handle, pfm_detect);
90         if (result == IO_ERROR)
91           return NULL;
92         else if (result == YES)
93           return pfm_open_reader (handle, dict, NULL);
94
95         msg (SE, _("\"%s\" is not a system or portable file."),
96              fh_get_file_name (handle));
97         return NULL;
98       }
99
100     case FH_REF_INLINE:
101       msg (SE, _("The inline file is not allowed here."));
102       return NULL;
103
104     case FH_REF_SCRATCH:
105       return scratch_reader_open (handle, dict);
106     }
107   NOT_REACHED ();
108 }