source: trunk/indexers/mg/src/text/mg_files.c@ 3745

Last change on this file since 3745 was 3745, checked in by mdewsnip, 21 years ago

Addition of MG package for search and retrieval

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/**************************************************************************
2 *
3 * mg_files.c -- Routines for handling files for the auxillary programs
4 * Copyright (C) 1994 Neil Sharman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * $Id: mg_files.c 3745 2003-02-20 21:20:24Z mdewsnip $
21 *
22 **************************************************************************/
23
24#include "sysfuncs.h"
25#include "memlib.h"
26#include "messages.h"
27#include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
28
29#include "mg_files.h"
30
31
32/* This must contain a valid path without a trailing slash */
33static char *basepath = NULL;
34
35
36
37/* This sets the base path for all file operations */
38void
39set_basepath (const char *bp)
40{
41/* [RPAP - Feb 97: WIN32 Port] */
42#ifdef __WIN32__
43 basepath = "";
44#else
45 char *s;
46 /* Free the memory for the base path if it has already been allocated */
47 if (basepath)
48 {
49 Xfree (basepath);
50 basepath = NULL;
51 }
52
53 if (!bp || *bp == '\0')
54 bp = ".";
55
56 s = strrchr (bp, '/');
57 if (s && *(s + 1) == '\0')
58 {
59 basepath = Xmalloc (strlen (bp) + 2);
60 if (!basepath)
61 return;
62 strcpy (basepath, bp);
63 strcat (basepath, ".");
64 }
65 else
66 basepath = Xstrdup (bp);
67#endif
68}
69
70
71
72/* return the currently defined basepath */
73char *
74get_basepath (void)
75{
76 if (!basepath)
77 set_basepath (getenv ("MGDATA"));
78 return basepath;
79}
80
81
82
83/* This generates the name of a file. It places the name in the buffer
84 specified or if that is NULL it uses a static buffer. */
85char *
86make_name (const char *name, const char *suffix, char *buffer)
87{
88 static char path[512];
89 if (!buffer)
90 buffer = path;
91 if (!basepath)
92 set_basepath (getenv ("MGDATA"));
93 sprintf (buffer, FILE_NAME_FORMAT, basepath, name, suffix); /* [RPAP - Feb 97: WIN32 Port] */
94 return buffer;
95}
96
97
98
99/* This will open the specified file and check its magic number.
100 Mode may take on the following values
101 MG_ABORT : causes an error message to be generated and the
102 program aborted if there is an error.
103 MG_MESSAGE : causes a message to be generated and a NULL value to
104 be returned if there is an error.
105 MG_CONTINUE : causes a NULL value to be returned if there is an error.
106
107 On success if returns the FILE *. On failure it will return a NULL value
108 and possibly generate an error message, or it will exit the program with
109 an error message. */
110FILE *
111open_named_file (const char *name, const char *mode,
112 u_long magic_num, int err_mode)
113{
114 unsigned long magic;
115 FILE *f = NULL;
116 char *err;
117 f = fopen (name, mode);
118
119 if (!f)
120 {
121 err = "Unable to open \"%s\"";
122 goto error;
123 }
124
125 if (magic_num)
126 {
127 if (fread (&magic, sizeof (magic), 1, f) != 1)
128 {
129 err = "No magic number \"%s\"";
130 goto error;
131 }
132
133 NTOHUL(magic); /* [RPAP - Jan 97: Endian Ordering] */
134
135 if (!IS_MAGIC (magic))
136 {
137 err = "No MG magic number \"%s\"";
138 goto error;
139 }
140
141 if (magic != magic_num)
142 {
143 err = "Wrong MG magic number \"%s\"";
144 goto error;
145 }
146 }
147 return f;
148
149error:
150 if (f)
151 fclose (f);
152 if (err_mode == MG_ABORT)
153 FatalError (1, err, name);
154 if (err_mode == MG_MESSAGE)
155 Message (err, name);
156 return NULL;
157}
158
159
160
161/* This will open the specified file and check its magic number.
162 Mode may take on the following values
163 MG_ABORT : causes an error message to be generated and the
164 program aborted if there is an error.
165 MG_MESSAGE : causes a message to be generated and a NULL value to
166 be returned if there is an error.
167 MG_CONTINUE : causes a NULL value to be returned if there is an error.
168
169 On success if returns the FILE *. On failure it will return a NULL value
170 and possibly generate an error message, or it will exit the program with
171 an error message. */
172FILE *
173open_file (const char *name, const char *suffix, const char *mode,
174 u_long magic_num, int err_mode)
175{
176 char path[512];
177 if (!basepath)
178 set_basepath (getenv ("MGDATA"));
179 sprintf (path, FILE_NAME_FORMAT, basepath, name, suffix); /* [RPAP - Feb 97: WIN32 Port] */
180 return open_named_file (path, mode, magic_num, err_mode);
181}
182
183
184
185
186
187
188/* This will create the specified file and set its magic number.
189
190 Mode may take on the following values
191 MG_ABORT : causes an error message to be generated and the
192 program aborted if there is an error.
193 MG_MESSAGE : causes a message to be generated and a NULL value to
194 be returned if there is an error.
195 MG_CONTINUE : causes a NULL value to be returned if there is an error.
196
197 On success if returns the FILE *. On failure it will return a NULL value
198 and possibly generate an error message, or it will exit the program with
199 an error message. */
200FILE *
201create_named_file (const char *name, const char *mode,
202 u_long magic_num, int err_mode)
203{
204 FILE *f = NULL;
205 char *err;
206 f = fopen (name, mode);
207
208 if (!f)
209 {
210 err = "Unable to open \"%s\"";
211 goto error;
212 }
213
214 if (magic_num)
215 HTONUL(magic_num); /* [RPAP - Jan 97: Endian Ordering] */
216 if (fwrite (&magic_num, sizeof (magic_num), 1, f) != 1)
217 {
218 err = "Couldn't write magic number \"%s\"";
219 goto error;
220 }
221
222 return f;
223
224error:
225 if (f)
226 fclose (f);
227 if (err_mode == MG_ABORT)
228 FatalError (1, err, name);
229 if (err_mode == MG_MESSAGE)
230 Message (err, name);
231 return NULL;
232}
233
234
235
236
237
238
239
240/* This will create the specified file and set its magic number.
241
242 Mode may take on the following values
243 MG_ABORT : causes an error message to be generated and the
244 program aborted if there is an error.
245 MG_MESSAGE : causes a message to be generated and a NULL value to
246 be returned if there is an error.
247 MG_CONTINUE : causes a NULL value to be returned if there is an error.
248
249 On success if returns the FILE *. On failure it will return a NULL value
250 and possibly generate an error message, or it will exit the program with
251 an error message. */
252FILE *
253create_file (const char *name, const char *suffix, const char *mode,
254 u_long magic_num, int err_mode)
255{
256 char path[512];
257 if (!basepath)
258 set_basepath (getenv ("MGDATA"));
259 sprintf (path, FILE_NAME_FORMAT, basepath, name, suffix); /* [RPAP - Feb 97: WIN32 Port] */
260 return create_named_file (path, mode, magic_num, err_mode);
261}
Note: See TracBrowser for help on using the repository browser.