Allow output files to overwrite input files (bug #21280). Thanks to
[pspp-builds.git] / src / data / file-handle-def.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2005, 2006 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 #ifndef FILE_HANDLE_DEF_H
18 #define FILE_HANDLE_DEF_H
19
20 #include <stdbool.h>
21 #include <stddef.h>
22
23 /* What a file handle refers to.
24    (Ordinarily only a single value is allowed, but fh_open()
25    and fh_parse() take a mask.) */
26 enum fh_referent
27   {
28     FH_REF_FILE = 001,          /* Ordinary file (the most common case). */
29     FH_REF_INLINE = 002,        /* The inline file. */
30     FH_REF_SCRATCH = 004        /* Temporary dataset. */
31   };
32
33 /* File modes. */
34 enum fh_mode
35   {
36     FH_MODE_TEXT,               /* New-line delimited lines. */
37     FH_MODE_BINARY              /* Fixed-length records. */
38   };
39
40 /* Ways to access a file. */
41 enum fh_access
42   {
43     FH_ACC_READ,                /* Read from it. */
44     FH_ACC_WRITE                /* Write to it. */
45   };
46
47 /* Properties of a file handle. */
48 struct fh_properties
49   {
50     enum fh_mode mode;          /* File mode. */
51     size_t record_width;        /* Length of fixed-format records. */
52     size_t tab_width;           /* Tab width, 0=do not expand tabs. */
53   };
54
55 void fh_init (void);
56 void fh_done (void);
57
58 /* Creating file handles. */
59 struct file_handle *fh_create_file (const char *handle_name,
60                                     const char *file_name,
61                                     const struct fh_properties *);
62 struct file_handle *fh_create_scratch (const char *handle_name);
63 const struct fh_properties *fh_default_properties (void);
64
65 /* Reference management. */
66 struct file_handle *fh_ref (struct file_handle *);
67 void fh_unref (struct file_handle *);
68 void fh_unname (struct file_handle *);
69
70 /* Finding file handles. */
71 struct file_handle *fh_from_id (const char *handle_name);
72 struct file_handle *fh_from_file_name (const char *file_name);
73 struct file_handle *fh_inline_file (void);
74
75 /* Generic properties of file handles. */
76 const char *fh_get_id (const struct file_handle *);
77 const char *fh_get_name (const struct file_handle *);
78 enum fh_referent fh_get_referent (const struct file_handle *);
79
80 /* Properties of FH_REF_FILE file handles. */
81 const char *fh_get_file_name (const struct file_handle *);
82 enum fh_mode fh_get_mode (const struct file_handle *) ;
83
84 /* Properties of FH_REF_FILE and FH_REF_INLINE file handles. */
85 size_t fh_get_record_width (const struct file_handle *);
86 size_t fh_get_tab_width (const struct file_handle *);
87
88 /* Properties of FH_REF_SCRATCH file handles. */
89 struct scratch_handle *fh_get_scratch_handle (const struct file_handle *);
90 void fh_set_scratch_handle (struct file_handle *, struct scratch_handle *);
91
92 /* Mutual exclusion for access . */
93 struct fh_lock *fh_lock (struct file_handle *, enum fh_referent mask,
94                          const char *type, enum fh_access, bool exclusive);
95 bool fh_unlock (struct fh_lock *);
96 void *fh_lock_get_aux (const struct fh_lock *);
97 void fh_lock_set_aux (struct fh_lock *, void *aux);
98 bool fh_is_locked (const struct file_handle *, enum fh_access);
99
100 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
101    commands. */
102 struct file_handle *fh_get_default_handle (void);
103 void fh_set_default_handle (struct file_handle *);
104
105 #endif