source: main/tags/2.35a/gsdl/lib/fileutil.cpp@ 33178

Last change on this file since 33178 was 2302, checked in by sjboddie, 23 years ago

Fixed a bug that's been annoying me for a while but that I was far too
stupid to spot. Thanks to Manuel in Romania for pointing it out.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1/**********************************************************************
2 *
3 * fileutil.cpp --
4 * Copyright (C) 1999 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "fileutil.h"
27
28#if defined(GSDL_USE_OBJECTSPACE)
29# include <ospace\std\iostream>
30# include <ospace\std\fstream>
31#elif defined(GSDL_USE_IOS_H)
32# include <iostream.h>
33# include <fstream.h>
34#else
35# include <iostream>
36# include <fstream>
37#endif
38
39
40// returns the proper concatenation of the two paths
41text_t filename_cat (text_t path1, text_t path2) {
42 text_t::iterator here;
43 text_t::iterator begin;
44 text_t::iterator end;
45
46 // make sure there is just one slash, of the correct type,
47 // at the end of path1 (unless path1 is an empty string).
48 if (!path1.empty()) {
49 // remove all trailing slashes
50 here = path1.end();
51 here--;
52 begin = path1.begin();
53 while (here != begin && (*here == '/' || *here == '\\')) {
54 here--;
55 }
56 here++;
57 path1.erase(here,path1.end());
58
59 // add one final slash
60#ifdef __WIN32__
61 path1 += "\\";
62#else
63 path1 += "/";
64#endif
65 }
66
67 // remove all slashes from the start of path2
68 here = path2.begin();
69 end = path2.end();
70 while (here != end && (*here == '/' || *here == '\\')) {
71 here++;
72 }
73 path2.erase (path2.begin(), here);
74
75 text_t fullpath = path1 + path2;
76
77 // make sure all the right slashes are used
78 here = fullpath.begin();
79 end = fullpath.end();
80 while (here != end) {
81#ifdef __WIN32__
82 if (*here == '/') *here = '\\';
83#else
84 if (*here == '\\') *here = '/';
85#endif
86 here ++;
87 }
88 return fullpath;
89}
90
91text_t filename_cat (text_t path1, text_t path2, text_t path3) {
92 return filename_cat(filename_cat(path1,path2),path3);
93}
94
95text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4) {
96 return filename_cat(filename_cat(filename_cat(path1,path2),path3),path4);
97}
98
99text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4,
100 text_t path5) {
101 return filename_cat(filename_cat(filename_cat(filename_cat(path1,path2),path3),
102 path4),path5);
103}
104
105text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4,
106 text_t path5, text_t path6) {
107 return filename_cat(filename_cat(path1,path2,path3,path4,path5),path6);
108}
109
110// returns true if filename can be opened
111bool file_exists (const text_t &filename) {
112 char *cstr = filename.getcstr();
113#ifdef GSDL_USE_IOS_H
114 ifstream filestream (cstr, ios::in | ios::nocreate);
115#else
116 ifstream filestream (cstr, ios::in);
117#endif
118 delete cstr;
119
120 if (filestream) {
121 // file exists
122 filestream.close ();
123 return true;
124 }
125
126 // file does not exist
127 return false;
128}
129
130// returns true if filename can be opened
131bool file_writable (const text_t &filename) {
132 char *cstr = filename.getcstr();
133#ifdef GSDL_USE_IOS_H
134 ifstream filestream (cstr, ios::out | ios::nocreate);
135#else
136 ifstream filestream (cstr, ios::out);
137#endif
138 delete cstr;
139
140 if (filestream) {
141 // file exists
142 filestream.close ();
143 return true;
144 }
145
146 // file does not exist
147 return false;
148}
149
150#if defined(__WIN32__) && !defined(__GNUC__)
151
152#include <windows.h>
153
154bool directory_exists (const text_t &dirname) {
155
156 WIN32_FIND_DATA FileData;
157 HANDLE hSearch;
158
159 text_t dir = dirname + "\\*";
160 char *dirpath = dir.getcstr();
161
162 hSearch = FindFirstFile(dirpath, &FileData);
163 if (hSearch == INVALID_HANDLE_VALUE) {
164 delete dirpath;
165 return false;
166 }
167
168 FindClose (hSearch);
169 delete dirpath;
170 return true;
171}
172
173bool read_dir (const text_t &dirname, text_tset &filelist) {
174
175 WIN32_FIND_DATA FileData;
176 HANDLE hSearch;
177
178 text_t dir = dirname + "\\*";
179 char *dirpath = dir.getcstr();
180
181 hSearch = FindFirstFile(dirpath, &FileData);
182 if (hSearch == INVALID_HANDLE_VALUE) {
183 delete dirpath;
184 return false;
185 }
186
187 text_t filename = FileData.cFileName;
188 if (filename != "." && filename != ".." && filename != "CVS")
189 filelist.insert (filename);
190
191 while (FindNextFile(hSearch, &FileData)) {
192 filename = FileData.cFileName;
193 if (filename == "." || filename == ".." || filename == "CVS")
194 continue;
195 filelist.insert (filename);
196 }
197
198 FindClose(hSearch);
199 delete dirpath;
200
201 return true;
202}
203
204bool read_dir (const text_t &dirname, text_tarray &filelist) {
205
206 WIN32_FIND_DATA FileData;
207 HANDLE hSearch;
208
209 text_t dir = dirname + "\\*";
210 char *dirpath = dir.getcstr();
211
212 hSearch = FindFirstFile(dirpath, &FileData);
213 if (hSearch == INVALID_HANDLE_VALUE) {
214 delete dirpath;
215 return false;
216 }
217
218 text_t filename = FileData.cFileName;
219 if (filename != "." && filename != ".." && filename != "CVS")
220 filelist.push_back (filename);
221
222 while (FindNextFile(hSearch, &FileData)) {
223 filename = FileData.cFileName;
224 if (filename == "." || filename == ".." || filename == "CVS")
225 continue;
226 filelist.push_back (filename);
227 }
228
229 FindClose(hSearch);
230 delete dirpath;
231
232 return true;
233}
234
235#else
236
237#include <dirent.h>
238
239bool directory_exists (const text_t &dirname) {
240
241 char *tmp = dirname.getcstr();
242 DIR *dirin = opendir (tmp);
243 delete tmp;
244
245 if (dirin == NULL) return false;
246 closedir (dirin);
247 return true;
248}
249
250bool read_dir (const text_t &dirname, text_tset &filelist) {
251
252 char *tmp = dirname.getcstr();
253 DIR *dirin = opendir (tmp);
254 delete tmp;
255
256 if (dirin == NULL) return false;
257
258 dirent *dirp;
259
260 text_t filename;
261 while ((dirp = readdir (dirin)) != NULL) {
262 filename = dirp->d_name;
263 if (filename == "." || filename == ".." || filename == "CVS")
264 continue;
265 filelist.insert (filename);
266 }
267 closedir (dirin);
268 return true;
269}
270
271bool read_dir (const text_t &dirname, text_tarray &filelist) {
272
273 char *tmp = dirname.getcstr();
274 DIR *dirin = opendir (tmp);
275 delete tmp;
276
277 if (dirin == NULL) return false;
278
279 dirent *dirp;
280
281 text_t filename;
282 while ((dirp = readdir (dirin)) != NULL) {
283 filename = dirp->d_name;
284 if (filename == "." || filename == ".." || filename == "CVS")
285 continue;
286 filelist.push_back (filename);
287 }
288 closedir (dirin);
289 return true;
290}
291
292#endif
293
294// returns true if things look like they happened ok
295bool file_copy (const text_t &fromfile, const text_t &tofile) {
296
297 char *fromfilec = fromfile.getcstr();
298 char *tofilec = tofile.getcstr();
299 bool fail = false;
300
301#ifdef __WIN32__
302 if (CopyFile (fromfilec, tofilec, FALSE) == 0) fail = true;
303
304#else
305
306 // I'm sure there's a better way to do this - for now I don't have
307 // time to find it though
308 ifstream from (fromfilec);
309 if (!from) {
310 fail = true;
311 } else {
312 ofstream to (tofilec);
313 if (!to) {
314 fail = true;
315 from.close();
316 } else {
317 char c;
318 from.get(c);
319 while (!from.eof ()) {
320 to.put(c);
321 from.get(c);
322 }
323 from.close();
324 to.close();
325 }
326 }
327
328#endif
329
330 delete fromfilec;
331 delete tofilec;
332 if (fail) return false;
333 return true;
334}
335
336// This is a fairly quick and nasty attempt at doing a "tail" on a file
337// (i.e. returning the last numlines lines of filename). It has one
338// important limitation in that it expects lines to be linelength
339// characters long on average. This of course makes it possible that it
340// won't return numlines lines if there are some lines that are longer than
341// linelength. It also makes it fairly inefficient as it always reads in
342// numlines*linelength characters when it most often doesn't need them all.
343// For current needs it's fine though.
344// -- Pass a linelength of 0 to use the default linelength (256 chars).
345// -- The maximum value of linelength is MAXLINELENGTH
346text_t file_tail (const text_t &filename, int numlines, int linelength) {
347
348#define MAXLINELENGTH 2048
349
350 if (numlines < 1) numlines = 1;
351 if (linelength < 1) linelength = 256;
352 if (linelength > MAXLINELENGTH) linelength = MAXLINELENGTH;
353
354 streampos numchars = linelength*numlines;
355
356 text_tarray lines;
357 text_t ret;
358
359 char *filenamec = filename.getcstr();
360 char linec[MAXLINELENGTH];
361 ifstream file_in (filenamec);
362 delete filenamec;
363 if (file_in) {
364 file_in.seekg (0, ios::end);
365 streampos file_length = file_in.tellg();
366 if (file_length < numchars) numchars = file_length;
367 file_in.seekg (-numchars, ios::end);
368
369 while (!file_in.eof()) {
370 file_in.getline (linec, linelength);
371 ret.setcstr(linec);
372 text_t::const_iterator here = ret.begin();
373 text_t::const_iterator end = ret.end();
374 // make sure line has content
375 while (here != end) {
376 if (*here != '\n' && *here != ' ') {
377 lines.push_back (ret);
378 break;
379 }
380 here ++;
381 }
382 }
383 file_in.close();
384 }
385
386 ret.clear();
387 int numlinesgot = lines.size();
388 int sindex = 0;
389 if (numlinesgot > numlines) sindex = numlinesgot - numlines;
390 for (int i = sindex; i < numlinesgot; i++) {
391 ret += lines[i] + "\n";
392 }
393
394 return ret;
395}
396
397#ifdef __WIN32__
398
399#include <direct.h>
400// returns true if directory was created successfully
401bool mk_dir (const text_t &dirname) {
402 char *dirnamec = dirname.getcstr();
403 int rv = _mkdir (dirnamec);
404 delete dirnamec;
405 if (rv == 0) return true;
406 return false;
407}
408
409#else
410
411#include <sys/stat.h>
412#include <sys/types.h>
413#include <fcntl.h>
414#include <unistd.h>
415// returns true if directory was created successfully
416bool mk_dir (const text_t &dirname) {
417 mode_t mode = 0777;
418 char *dirnamec = dirname.getcstr();
419 int rv = mkdir (dirnamec, mode);
420 delete dirnamec;
421 if (rv == 0) return true;
422 return false;
423}
424
425#endif
426
427// read in file from filename and load into content
428bool read_file (const text_t &filename, text_t &content) {
429
430 content.clear();
431
432 char *filenamec = filename.getcstr();
433#ifdef GSDL_USE_IOS_H
434 ifstream file_in (filenamec, ios::in | ios::nocreate);
435#else
436 ifstream file_in (filenamec, ios::in);
437#endif
438 delete filenamec;
439
440 if (file_in) {
441 char c;
442 file_in.get(c);
443 while (!file_in.eof ()) {
444 content.push_back(c);
445 file_in.get(c);
446 }
447 file_in.close();
448 } else {
449 return false;
450 }
451 return true;
452}
Note: See TracBrowser for help on using the repository browser.