ace0c6845bae170807a7f19fd80be0c1086b825c
[pspp] / src / data / file-handle-def.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2005, 2006, 2010, 2011, 2013 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 struct dataset;
24
25 /* What a file handle refers to.
26    (Ordinarily only a single value is allowed, but fh_open()
27    and fh_parse() take a mask.) */
28 enum fh_referent
29   {
30     FH_REF_FILE = 001,          /* Ordinary file (the most common case). */
31     FH_REF_INLINE = 002,        /* The inline file. */
32     FH_REF_DATASET = 004        /* Dataset. */
33   };
34
35 /* File modes. */
36 enum fh_mode
37   {
38     FH_MODE_TEXT,               /* New-line delimited lines. */
39     FH_MODE_FIXED,              /* Fixed-length records. */
40     FH_MODE_VARIABLE,           /* Binary variable-length records. */
41     FH_MODE_360_VARIABLE,       /* IBM 360 variable-length records. */
42     FH_MODE_360_SPANNED,        /* IBM 360 variable-length, spanned records. */
43   };
44
45 /* Ways to access a file. */
46 enum fh_access
47   {
48     FH_ACC_READ,                /* Read from it. */
49     FH_ACC_WRITE                /* Write to it. */
50   };
51
52 /* How a line ends.
53
54    This affects only writing FH_MODE_TEXT files.  Writing in other modes does
55    not use line ends, and reading in FH_MODE_TEXT mode accepts all forms of
56    line ends. */
57 enum fh_line_ends
58   {
59     FH_END_LF,                  /* Unix line ends (\n). */
60     FH_END_CRLF                 /* MS-DOS line ends (\r\n). */
61   };
62
63 /* Properties of a file handle. */
64 struct fh_properties
65   {
66     enum fh_mode mode;          /* File mode. */
67     enum fh_line_ends line_ends; /* Line ends for text files. */
68     size_t record_width;        /* Length of fixed-format records. */
69     size_t tab_width;           /* Tab width, 0=do not expand tabs. */
70     const char *encoding;       /* Charset for contents. */
71   };
72
73 void fh_init (void);
74 void fh_done (void);
75
76 /* Creating file handles. */
77 struct file_handle *fh_create_file (const char *handle_name,
78                                     const char *file_name, const char *file_name_encoding,
79                                     const struct fh_properties *);
80 struct file_handle *fh_create_dataset (struct dataset *);
81 const struct fh_properties *fh_default_properties (void);
82
83 /* Reference management. */
84 struct file_handle *fh_ref (struct file_handle *);
85 void fh_unref (struct file_handle *);
86 void fh_unname (struct file_handle *);
87
88 /* Finding file handles. */
89 struct file_handle *fh_from_id (const char *handle_name);
90 struct file_handle *fh_from_file_name (const char *file_name);
91 struct file_handle *fh_inline_file (void);
92
93 /* Generic properties of file handles. */
94 const char *fh_get_id (const struct file_handle *);
95 const char *fh_get_name (const struct file_handle *);
96 enum fh_referent fh_get_referent (const struct file_handle *);
97 const char *fh_get_encoding (const struct file_handle *);
98
99 /* Properties of FH_REF_FILE file handles. */
100 const char *fh_get_file_name (const struct file_handle *);
101 const char *fh_get_file_name_encoding (const struct file_handle *handle);
102 enum fh_mode fh_get_mode (const struct file_handle *) ;
103 enum fh_line_ends fh_get_line_ends (const struct file_handle *);
104
105 /* Properties of FH_REF_FILE and FH_REF_INLINE file handles. */
106 size_t fh_get_record_width (const struct file_handle *);
107 size_t fh_get_tab_width (const struct file_handle *);
108
109 /* Properties of FH_REF_DATASET file handles. */
110 struct dataset *fh_get_dataset (const struct file_handle *);
111
112 /* Mutual exclusion for access . */
113 struct fh_lock *fh_lock (struct file_handle *, enum fh_referent mask,
114                          const char *type, enum fh_access, bool exclusive);
115 bool fh_unlock (struct fh_lock *);
116 void *fh_lock_get_aux (const struct fh_lock *);
117 void fh_lock_set_aux (struct fh_lock *, void *aux);
118 bool fh_is_locked (const struct file_handle *, enum fh_access);
119
120 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
121    commands. */
122 struct file_handle *fh_get_default_handle (void);
123 void fh_set_default_handle (struct file_handle *);
124
125 #endif