source: trunk/mgpp/text/mg_files.cpp@ 3365

Last change on this file since 3365 was 3365, checked in by kjdon, 22 years ago

Initial revision

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