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