73e118cdf848f81e1e6405983fec02b45f0a6a9c
[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 #include <libpspp/legacy-encoding.h>
23
24 /* What a file handle refers to.
25    (Ordinarily only a single value is allowed, but fh_open()
26    and fh_parse() take a mask.) */
27 enum fh_referent
28   {
29     FH_REF_FILE = 001,          /* Ordinary file (the most common case). */
30     FH_REF_INLINE = 002,        /* The inline file. */
31     FH_REF_SCRATCH = 004        /* Temporary dataset. */
32   };
33
34 /* File modes. */
35 enum fh_mode
36   {
37     FH_MODE_TEXT,               /* New-line delimited lines. */
38     FH_MODE_FIXED,              /* Fixed-length records. */
39     FH_MODE_VARIABLE,           /* Binary variable-length records. */
40     FH_MODE_360_VARIABLE,       /* IBM 360 variable-length records. */
41     FH_MODE_360_SPANNED,        /* IBM 360 variable-length, spanned records. */
42   };
43
44 /* Ways to access a file. */
45 enum fh_access
46   {
47     FH_ACC_READ,                /* Read from it. */
48     FH_ACC_WRITE                /* Write to it. */
49   };
50
51 /* Properties of a file handle. */
52 struct fh_properties
53   {
54     enum fh_mode mode;          /* File mode. */
55     size_t record_width;        /* Length of fixed-format records. */
56     size_t tab_width;           /* Tab width, 0=do not expand tabs. */
57     enum legacy_encoding encoding;/* ASCII or EBCDIC? */
58   };
59
60 void fh_init (void);
61 void fh_done (void);
62
63 /* Creating file handles. */
64 struct file_handle *fh_create_file (const char *handle_name,
65                                     const char *file_name,
66                                     const struct fh_properties *);
67 struct file_handle *fh_create_scratch (const char *handle_name);
68 const struct fh_properties *fh_default_properties (void);
69
70 /* Reference management. */
71 struct file_handle *fh_ref (struct file_handle *);
72 void fh_unref (struct file_handle *);
73 void fh_unname (struct file_handle *);
74
75 /* Finding file handles. */
76 struct file_handle *fh_from_id (const char *handle_name);
77 struct file_handle *fh_from_file_name (const char *file_name);
78 struct file_handle *fh_inline_file (void);
79
80 /* Generic properties of file handles. */
81 const char *fh_get_id (const struct file_handle *);
82 const char *fh_get_name (const struct file_handle *);
83 enum fh_referent fh_get_referent (const struct file_handle *);
84
85 /* Properties of FH_REF_FILE file handles. */
86 const char *fh_get_file_name (const struct file_handle *);
87 enum fh_mode fh_get_mode (const struct file_handle *) ;
88
89 /* Properties of FH_REF_FILE and FH_REF_INLINE file handles. */
90 size_t fh_get_record_width (const struct file_handle *);
91 size_t fh_get_tab_width (const struct file_handle *);
92 enum legacy_encoding fh_get_legacy_encoding (const struct file_handle *);
93
94 /* Properties of FH_REF_SCRATCH file handles. */
95 struct scratch_handle *fh_get_scratch_handle (const struct file_handle *);
96 void fh_set_scratch_handle (struct file_handle *, struct scratch_handle *);
97
98 /* Mutual exclusion for access . */
99 struct fh_lock *fh_lock (struct file_handle *, enum fh_referent mask,
100                          const char *type, enum fh_access, bool exclusive);
101 bool fh_unlock (struct fh_lock *);
102 void *fh_lock_get_aux (const struct fh_lock *);
103 void fh_lock_set_aux (struct fh_lock *, void *aux);
104 bool fh_is_locked (const struct file_handle *, enum fh_access);
105
106 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
107    commands. */
108 struct file_handle *fh_get_default_handle (void);
109 void fh_set_default_handle (struct file_handle *);
110
111 #endif