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