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 | // This was added for Solaris, but it makes things worse on Solaris for me...
|
---|
24 | // #define _XOPEN_SOURCE_EXTENDED 1
|
---|
25 |
|
---|
26 | #include "sysfuncs.h"
|
---|
27 | #include "memlib.h"
|
---|
28 | #include "messages.h"
|
---|
29 | #include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
|
---|
30 |
|
---|
31 | #include "mg_files.h"
|
---|
32 |
|
---|
33 |
|
---|
34 | /* This must contain a valid path without a trailing slash */
|
---|
35 | static char *basepath = NULL;
|
---|
36 |
|
---|
37 | /* This sets the base path for all file operations */
|
---|
38 | void
|
---|
39 | set_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 | delete []basepath;
|
---|
50 | basepath = NULL;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (!bp || *bp == '\0')
|
---|
54 | bp = ".";
|
---|
55 |
|
---|
56 | s = strrchr ((char*)bp, '/');
|
---|
57 | if (s && *(s + 1) == '\0')
|
---|
58 | {
|
---|
59 | basepath = new char[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 */
|
---|
73 | char *
|
---|
74 | get_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. */
|
---|
85 | char *
|
---|
86 | make_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 | /* [JFG - Mar 06: Accent folding patch] */
|
---|
99 | /* This generates a suffixe for a file name. It places the name in the
|
---|
100 | buffer specified or if that is NULL, it uses a static buffer.
|
---|
101 | Please do not specify buffers under 512 or the data to be written. */
|
---|
102 | char *
|
---|
103 | make_suffix (const char *suffix_format, const char suffix_arg, char *buffer)
|
---|
104 | {
|
---|
105 | static char suffix[512];
|
---|
106 | if (!buffer)
|
---|
107 | buffer = suffix;
|
---|
108 | sprintf (buffer, suffix_format, suffix_arg);
|
---|
109 | return buffer;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 |
|
---|
114 | /* This will open the specified file and check its magic number.
|
---|
115 | Mode may take on the following values
|
---|
116 | MG_ABORT : causes an error message to be generated and the
|
---|
117 | program aborted if there is an error.
|
---|
118 | MG_MESSAGE : causes a message to be generated and a NULL value to
|
---|
119 | be returned if there is an error.
|
---|
120 | MG_CONTINUE : causes a NULL value to be returned if there is an error.
|
---|
121 |
|
---|
122 | On success if returns the FILE *. On failure it will return a NULL value
|
---|
123 | and possibly generate an error message, or it will exit the program with
|
---|
124 | an error message. */
|
---|
125 | FILE *
|
---|
126 | open_named_file (const char *name, const char *mode,
|
---|
127 | mg_u_long magic_num, int err_mode)
|
---|
128 | {
|
---|
129 | mg_u_long magic;
|
---|
130 | FILE *f = NULL;
|
---|
131 | char *err;
|
---|
132 | f = fopen (name, mode);
|
---|
133 |
|
---|
134 | if (!f)
|
---|
135 | {
|
---|
136 | err = (char*)"Unable to open \"%s\"";
|
---|
137 | goto error;
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (magic_num)
|
---|
141 | {
|
---|
142 | if (fread (&magic, sizeof (magic), 1, f) != 1)
|
---|
143 | {
|
---|
144 | err = (char*)"No magic number \"%s\"";
|
---|
145 | goto error;
|
---|
146 | }
|
---|
147 |
|
---|
148 | NTOHUL(magic); /* [RPAP - Jan 97: Endian Ordering] */
|
---|
149 |
|
---|
150 | if (!IS_MAGIC (magic))
|
---|
151 | {
|
---|
152 | err = (char*)"No MG magic number \"%s\"";
|
---|
153 | goto error;
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (magic != magic_num)
|
---|
157 | {
|
---|
158 | err = (char*)"Wrong MG magic number \"%s\"";
|
---|
159 | goto error;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | return f;
|
---|
163 |
|
---|
164 | error:
|
---|
165 | if (f)
|
---|
166 | fclose (f);
|
---|
167 | if (err_mode == MG_ABORT)
|
---|
168 | FatalError (1, err, name);
|
---|
169 | if (err_mode == MG_MESSAGE)
|
---|
170 | Message (err, name);
|
---|
171 | return NULL;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 |
|
---|
176 | /* This will open the specified file and check its magic number.
|
---|
177 | Mode may take on the following values
|
---|
178 | MG_ABORT : causes an error message to be generated and the
|
---|
179 | program aborted if there is an error.
|
---|
180 | MG_MESSAGE : causes a message to be generated and a NULL value to
|
---|
181 | be returned if there is an error.
|
---|
182 | MG_CONTINUE : causes a NULL value to be returned if there is an error.
|
---|
183 |
|
---|
184 | On success if returns the FILE *. On failure it will return a NULL value
|
---|
185 | and possibly generate an error message, or it will exit the program with
|
---|
186 | an error message. */
|
---|
187 | FILE *
|
---|
188 | open_file (const char *name, const char *suffix, const char *mode,
|
---|
189 | mg_u_long magic_num, int err_mode)
|
---|
190 | {
|
---|
191 | char path[512];
|
---|
192 | if (!basepath)
|
---|
193 | set_basepath (getenv ("MGDATA"));
|
---|
194 | sprintf (path, FILE_NAME_FORMAT, basepath, name, suffix); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
195 | return open_named_file (path, mode, magic_num, err_mode);
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 |
|
---|
200 |
|
---|
201 |
|
---|
202 |
|
---|
203 | /* This will create the specified file and set its magic number.
|
---|
204 |
|
---|
205 | Mode may take on the following values
|
---|
206 | MG_ABORT : causes an error message to be generated and the
|
---|
207 | program aborted if there is an error.
|
---|
208 | MG_MESSAGE : causes a message to be generated and a NULL value to
|
---|
209 | be returned if there is an error.
|
---|
210 | MG_CONTINUE : causes a NULL value to be returned if there is an error.
|
---|
211 |
|
---|
212 | On success if returns the FILE *. On failure it will return a NULL value
|
---|
213 | and possibly generate an error message, or it will exit the program with
|
---|
214 | an error message. */
|
---|
215 | FILE *
|
---|
216 | create_named_file (const char *name, const char *mode,
|
---|
217 | mg_u_long magic_num, int err_mode)
|
---|
218 | {
|
---|
219 | FILE *f = NULL;
|
---|
220 | char *err;
|
---|
221 | f = fopen (name, mode);
|
---|
222 |
|
---|
223 | if (!f)
|
---|
224 | {
|
---|
225 | err = (char*)"Unable to open \"%s\"";
|
---|
226 | goto error;
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (magic_num)
|
---|
230 | HTONUL(magic_num); /* [RPAP - Jan 97: Endian Ordering] */
|
---|
231 | if (fwrite (&magic_num, sizeof (magic_num), 1, f) != 1)
|
---|
232 | {
|
---|
233 | err = (char*)"Couldn't write magic number \"%s\"";
|
---|
234 | goto error;
|
---|
235 | }
|
---|
236 |
|
---|
237 | return f;
|
---|
238 |
|
---|
239 | error:
|
---|
240 | if (f)
|
---|
241 | fclose (f);
|
---|
242 | if (err_mode == MG_ABORT)
|
---|
243 | FatalError (1, err, name);
|
---|
244 | if (err_mode == MG_MESSAGE)
|
---|
245 | Message (err, name);
|
---|
246 | return NULL;
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 |
|
---|
251 |
|
---|
252 |
|
---|
253 |
|
---|
254 |
|
---|
255 | /* This will create the specified file and set its magic number.
|
---|
256 |
|
---|
257 | Mode may take on the following values
|
---|
258 | MG_ABORT : causes an error message to be generated and the
|
---|
259 | program aborted if there is an error.
|
---|
260 | MG_MESSAGE : causes a message to be generated and a NULL value to
|
---|
261 | be returned if there is an error.
|
---|
262 | MG_CONTINUE : causes a NULL value to be returned if there is an error.
|
---|
263 |
|
---|
264 | On success if returns the FILE *. On failure it will return a NULL value
|
---|
265 | and possibly generate an error message, or it will exit the program with
|
---|
266 | an error message. */
|
---|
267 | FILE *
|
---|
268 | create_file (const char *name, const char *suffix, const char *mode,
|
---|
269 | mg_u_long magic_num, int err_mode)
|
---|
270 | {
|
---|
271 | char path[512];
|
---|
272 | if (!basepath)
|
---|
273 | set_basepath (getenv ("MGDATA"));
|
---|
274 | sprintf (path, FILE_NAME_FORMAT, basepath, name, suffix); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
275 | return create_named_file (path, mode, magic_num, err_mode);
|
---|
276 | }
|
---|