b26440158c67033d0039cf8b73d1c62d6c71ced4
[pspp] / src / data / any-writer.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-writer.h"
20
21 #include <errno.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "data/dataset-writer.h"
27 #include "data/file-handle-def.h"
28 #include "data/file-name.h"
29 #include "data/por-file-writer.h"
30 #include "data/sys-file-writer.h"
31 #include "libpspp/assertion.h"
32 #include "libpspp/message.h"
33 #include "libpspp/str.h"
34
35 #include "gl/xalloc.h"
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 /* Creates and returns a writer for HANDLE with the given DICT. */
41 struct casewriter *
42 any_writer_open (struct file_handle *handle, struct dictionary *dict)
43 {
44   switch (fh_get_referent (handle))
45     {
46     case FH_REF_FILE:
47       {
48         struct casewriter *writer;
49         char *extension;
50
51         extension = fn_extension (handle);
52         str_lowercase (extension);
53
54         if (!strcmp (extension, ".por"))
55           writer = pfm_open_writer (handle, dict,
56                                     pfm_writer_default_options ());
57         else
58           writer = sfm_open_writer (handle, dict,
59                                     sfm_writer_default_options ());
60         free (extension);
61
62         return writer;
63       }
64
65     case FH_REF_INLINE:
66       msg (ME, _("The inline file is not allowed here."));
67       return NULL;
68
69     case FH_REF_DATASET:
70       return dataset_writer_open (handle, dict);
71     }
72
73   NOT_REACHED ();
74 }