Update all #include directives to the currently preferred style.
[pspp-builds.git] / src / data / any-reader.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006, 2010, 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "data/any-reader.h"
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "data/file-handle-def.h"
28 #include "data/file-name.h"
29 #include "data/por-file-reader.h"
30 #include "data/scratch-reader.h"
31 #include "data/sys-file-reader.h"
32 #include "libpspp/assertion.h"
33 #include "libpspp/message.h"
34 #include "libpspp/str.h"
35
36 #include "gl/xalloc.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 /* Result of type detection. */
42 enum detect_result
43   {
44     YES,                        /* It is this type. */
45     NO,                         /* It is not this type. */
46     IO_ERROR                    /* File couldn't be opened. */
47   };
48
49 /* Tries to detect whether FILE is a given type of file, by opening the file
50    and passing it to DETECT, and returns a detect_result. */
51 static enum detect_result
52 try_detect (const char *file_name, bool (*detect) (FILE *))
53 {
54   FILE *file;
55   bool is_type;
56
57   file = fn_open (file_name, "rb");
58   if (file == NULL)
59     {
60       msg (ME, _("An error occurred while opening `%s': %s."),
61            file_name, strerror (errno));
62       return IO_ERROR;
63     }
64
65   is_type = detect (file);
66
67   fn_close (file_name, file);
68
69   return is_type ? YES : NO;
70 }
71
72 /* Returns true if any_reader_open() would be able to open FILE as a data
73    file, false otherwise. */
74 bool
75 any_reader_may_open (const char *file)
76 {
77   return (try_detect (file, sfm_detect) == YES
78           || try_detect (file, pfm_detect) == YES);
79 }
80
81 /* Returns a casereader for HANDLE.  On success, returns the new
82    casereader and stores the file's dictionary into *DICT.  On
83    failure, returns a null pointer. */
84 struct casereader *
85 any_reader_open (struct file_handle *handle, struct dictionary **dict)
86 {
87   switch (fh_get_referent (handle))
88     {
89     case FH_REF_FILE:
90       {
91         enum detect_result result;
92
93         result = try_detect (fh_get_file_name (handle), sfm_detect);
94         if (result == IO_ERROR)
95           return NULL;
96         else if (result == YES)
97           return sfm_open_reader (handle, dict, NULL);
98
99         result = try_detect (fh_get_file_name (handle), pfm_detect);
100         if (result == IO_ERROR)
101           return NULL;
102         else if (result == YES)
103           return pfm_open_reader (handle, dict, NULL);
104
105         msg (SE, _("`%s' is not a system or portable file."),
106              fh_get_file_name (handle));
107         return NULL;
108       }
109
110     case FH_REF_INLINE:
111       msg (SE, _("The inline file is not allowed here."));
112       return NULL;
113
114     case FH_REF_SCRATCH:
115       return scratch_reader_open (handle, dict);
116     }
117   NOT_REACHED ();
118 }