source: trunk/indexers/mgpp/text/mg_files.cpp@ 13477

Last change on this file since 13477 was 13477, checked in by shaoqun, 17 years ago

added code for accentfolding

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 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/* [JFG - Mar 06: Accent folding patch] */
97/* This generates a suffixe for a file name. It places the name in the
98 buffer specified or if that is NULL, it uses a static buffer.
99 Please do not specify buffers under 512 or the data to be written. */
100char *
101make_suffix (const char *suffix_format, const char suffix_arg, char *buffer)
102{
103 static char suffix[512];
104 if (!buffer)
105 buffer = suffix;
106 sprintf (buffer, suffix_format, suffix_arg);
107 return buffer;
108}
109
110
111
112/* This will open the specified file and check its magic number.
113 Mode may take on the following values
114 MG_ABORT : causes an error message to be generated and the
115 program aborted if there is an error.
116 MG_MESSAGE : causes a message to be generated and a NULL value to
117 be returned if there is an error.
118 MG_CONTINUE : causes a NULL value to be returned if there is an error.
119
120 On success if returns the FILE *. On failure it will return a NULL value
121 and possibly generate an error message, or it will exit the program with
122 an error message. */
123FILE *
124open_named_file (const char *name, const char *mode,
125 u_long magic_num, int err_mode)
126{
127 unsigned long magic;
128 FILE *f = NULL;
129 char *err;
130 f = fopen (name, mode);
131
132 if (!f)
133 {
134 err = "Unable to open \"%s\"";
135 goto error;
136 }
137
138 if (magic_num)
139 {
140 if (fread (&magic, sizeof (magic), 1, f) != 1)
141 {
142 err = "No magic number \"%s\"";
143 goto error;
144 }
145
146 NTOHUL(magic); /* [RPAP - Jan 97: Endian Ordering] */
147
148 if (!IS_MAGIC (magic))
149 {
150 err = "No MG magic number \"%s\"";
151 goto error;
152 }
153
154 if (magic != magic_num)
155 {
156 err = "Wrong MG magic number \"%s\"";
157 goto error;
158 }
159 }
160 return f;
161
162error:
163 if (f)
164 fclose (f);
165 if (err_mode == MG_ABORT)
166 FatalError (1, err, name);
167 if (err_mode == MG_MESSAGE)
168 Message (err, name);
169 return NULL;
170}
171
172
173
174/* This will open the specified file and check its magic number.
175 Mode may take on the following values
176 MG_ABORT : causes an error message to be generated and the
177 program aborted if there is an error.
178 MG_MESSAGE : causes a message to be generated and a NULL value to
179 be returned if there is an error.
180 MG_CONTINUE : causes a NULL value to be returned if there is an error.
181
182 On success if returns the FILE *. On failure it will return a NULL value
183 and possibly generate an error message, or it will exit the program with
184 an error message. */
185FILE *
186open_file (const char *name, const char *suffix, const char *mode,
187 u_long magic_num, int err_mode)
188{
189 char path[512];
190 if (!basepath)
191 set_basepath (getenv ("MGDATA"));
192 sprintf (path, FILE_NAME_FORMAT, basepath, name, suffix); /* [RPAP - Feb 97: WIN32 Port] */
193 return open_named_file (path, mode, magic_num, err_mode);
194}
195
196
197
198
199
200
201/* This will create the specified file and set its magic number.
202
203 Mode may take on the following values
204 MG_ABORT : causes an error message to be generated and the
205 program aborted if there is an error.
206 MG_MESSAGE : causes a message to be generated and a NULL value to
207 be returned if there is an error.
208 MG_CONTINUE : causes a NULL value to be returned if there is an error.
209
210 On success if returns the FILE *. On failure it will return a NULL value
211 and possibly generate an error message, or it will exit the program with
212 an error message. */
213FILE *
214create_named_file (const char *name, const char *mode,
215 u_long magic_num, int err_mode)
216{
217 FILE *f = NULL;
218 char *err;
219 f = fopen (name, mode);
220
221 if (!f)
222 {
223 err = "Unable to open \"%s\"";
224 goto error;
225 }
226
227 if (magic_num)
228 HTONUL(magic_num); /* [RPAP - Jan 97: Endian Ordering] */
229 if (fwrite (&magic_num, sizeof (magic_num), 1, f) != 1)
230 {
231 err = "Couldn't write magic number \"%s\"";
232 goto error;
233 }
234
235 return f;
236
237error:
238 if (f)
239 fclose (f);
240 if (err_mode == MG_ABORT)
241 FatalError (1, err, name);
242 if (err_mode == MG_MESSAGE)
243 Message (err, name);
244 return NULL;
245}
246
247
248
249
250
251
252
253/* This will create the specified file and set its magic number.
254
255 Mode may take on the following values
256 MG_ABORT : causes an error message to be generated and the
257 program aborted if there is an error.
258 MG_MESSAGE : causes a message to be generated and a NULL value to
259 be returned if there is an error.
260 MG_CONTINUE : causes a NULL value to be returned if there is an error.
261
262 On success if returns the FILE *. On failure it will return a NULL value
263 and possibly generate an error message, or it will exit the program with
264 an error message. */
265FILE *
266create_file (const char *name, const char *suffix, const char *mode,
267 u_long magic_num, int err_mode)
268{
269 char path[512];
270 if (!basepath)
271 set_basepath (getenv ("MGDATA"));
272 sprintf (path, FILE_NAME_FORMAT, basepath, name, suffix); /* [RPAP - Feb 97: WIN32 Port] */
273 return create_named_file (path, mode, magic_num, err_mode);
274}
Note: See TracBrowser for help on using the repository browser.