source: trunk/gsdl/src/mgpp/text/mg_files.cpp@ 2382

Last change on this file since 2382 was 2382, checked in by jrm21, 23 years ago

needed some #defines to build on non-BSD systems (after my changes for
compiling on darwin...)

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