source: main/tags/2.25a/gsdl/lib/fileutil.cpp@ 24532

Last change on this file since 24532 was 1485, checked in by sjboddie, 24 years ago

More improvements to collector

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.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 char *dirpath = dirname.getcstr();
159 strcat (dirpath, "\\*");
160
161 hSearch = FindFirstFile(dirpath, &FileData);
162 delete dirpath;
163
164 if (hSearch == INVALID_HANDLE_VALUE) {
165 return false;
166 }
167 return true;
168}
169
170bool read_dir (const text_t &dirname, text_tarray &filelist) {
171
172 WIN32_FIND_DATA FileData;
173 HANDLE hSearch;
174 char *dirpath = dirname.getcstr();
175 strcat (dirpath, "\\*");
176
177 hSearch = FindFirstFile(dirpath, &FileData);
178 delete dirpath;
179
180 if (hSearch == INVALID_HANDLE_VALUE) {
181 return false;
182 }
183
184 text_t filename = FileData.cFileName;
185 if (filename != "." && filename != ".." && filename != "CVS")
186 filelist.push_back (filename);
187
188 while (FindNextFile(hSearch, &FileData)) {
189 filename = FileData.cFileName;
190 if (filename == "." || filename == ".." || filename == "CVS")
191 continue;
192 filelist.push_back (filename);
193 }
194
195 FindClose(hSearch);
196
197 return true;
198}
199
200#else
201
202#include <dirent.h>
203
204bool directory_exists (const text_t &dirname) {
205
206 char *tmp = dirname.getcstr();
207 DIR *dirin = opendir (tmp);
208 delete tmp;
209
210 if (dirin == NULL) return false;
211 closedir (dirin);
212 return true;
213}
214
215bool read_dir (const text_t &dirname, text_tarray &filelist) {
216
217 filelist.erase (filelist.begin(), filelist.end());
218
219 char *tmp = dirname.getcstr();
220 DIR *dirin = opendir (tmp);
221 delete tmp;
222
223 if (dirin == NULL) return false;
224
225 dirent *dirp;
226
227 text_t filename;
228 while ((dirp = readdir (dirin)) != NULL) {
229 filename = dirp->d_name;
230 if (filename == "." || filename == ".." || filename == "CVS")
231 continue;
232 filelist.push_back (filename);
233 }
234 closedir (dirin);
235 return true;
236}
237
238#endif
239
240// returns true if things look like they happened ok
241bool file_copy (const text_t &fromfile, const text_t &tofile) {
242
243 char *fromfilec = fromfile.getcstr();
244 char *tofilec = tofile.getcstr();
245 bool fail = false;
246
247#ifdef __WIN32__
248 if (CopyFile (fromfilec, tofilec, FALSE) == 0) fail = true;
249
250#else
251
252 // I'm sure there's a better way to do this - for now I don't have
253 // time to find it though
254 ifstream from (fromfilec);
255 if (!from) {
256 fail = true;
257 } else {
258 ofstream to (tofilec);
259 if (!to) {
260 fail = true;
261 from.close();
262 } else {
263 char c;
264 from.get(c);
265 while (!from.eof ()) {
266 to.put(c);
267 from.get(c);
268 }
269 from.close();
270 to.close();
271 }
272 }
273
274#endif
275
276 delete fromfilec;
277 delete tofilec;
278 if (fail) return false;
279 return true;
280}
281
282
283text_t file_tail (const text_t &filename, int numlines) {
284 if (numlines < 1) numlines = 1;
285 int numchars = 256*numlines;
286
287 text_tarray lines;
288 text_t ret;
289
290 char *filenamec = filename.getcstr();
291 char linec[256];
292 ifstream file_in (filenamec);
293 delete filenamec;
294 if (file_in) {
295
296 // this should be here to keep things reasonably fast
297 // when there's a long file to tail but I can't work out
298 // how to tell when it's rewound past the beginning of the file
299 // (which causes some problems)
300
301 // file_in.seekg (-numchars, ios::end);
302
303 while (!file_in.eof()) {
304 file_in.getline (linec, 256);
305 ret.setcstr(linec);
306 text_t::const_iterator here = ret.begin();
307 text_t::const_iterator end = ret.end();
308 // make sure line has content
309 while (here != end) {
310 if (*here != '\n' && *here != ' ') {
311 lines.push_back (ret);
312 break;
313 }
314 here ++;
315 }
316 }
317 file_in.close();
318 }
319
320 ret.clear();
321 int numlinesgot = lines.size();
322 int sindex = 0;
323 if (numlinesgot > numlines) sindex = numlinesgot - numlines;
324 for (int i = sindex; i < numlinesgot; i++) {
325 ret += lines[i] + "\n";
326 }
327
328 return ret;
329}
330
331// returns the last numlines lines (or last numlines*256
332// characters) of file
333/*
334text_t file_tail (const text_t &filename) {
335
336
337 text_t return_str, tmpstr;
338 char *filenamec = filename.getcstr();
339 char linec[256];
340
341 ifstream file_in (filenamec);
342 delete filenamec;
343 if (file_in) {
344 file_in.seekg (-256, ios::end);
345 file_in.getline (linec, 256, EOF);
346 file_in.close();
347
348 tmpstr.setcstr (linec);
349 text_t::const_iterator here = tmpstr.begin();
350 text_t::const_iterator end = tmpstr.end();
351 while (here != end) {
352 // handle trailing carriage returns
353 while (*here == '\n') {
354 here++;
355 if (here == end) return return_str;
356 else if (*here != '\n') return_str.clear();
357 }
358 if (*here == '\\') return_str.push_back ('\\');
359 return_str.push_back (*here);
360 here ++;
361 }
362 }
363 return return_str;
364}
365*/
366#ifdef __WIN32__
367
368#include <direct.h>
369// returns true if directory was created successfully
370bool mk_dir (const text_t &dirname) {
371 char *dirnamec = dirname.getcstr();
372 int rv = _mkdir (dirnamec);
373 delete dirnamec;
374 if (rv == 0) return true;
375 return false;
376}
377
378#else
379
380#include <sys/stat.h>
381#include <sys/types.h>
382#include <fcntl.h>
383#include <unistd.h>
384// returns true if directory was created successfully
385bool mk_dir (const text_t &dirname) {
386 mode_t mode = 0777;
387 char *dirnamec = dirname.getcstr();
388 int rv = mkdir (dirnamec, mode);
389 delete dirnamec;
390 if (rv == 0) return true;
391 return false;
392}
393
394#endif
395
396// read in file from filename and load into content
397bool read_file (const text_t &filename, text_t &content) {
398
399 content.clear();
400
401 char *filenamec = filename.getcstr();
402#ifdef GSDL_USE_IOS_H
403 ifstream file_in (filenamec, ios::in | ios::nocreate);
404#else
405 ifstream file_in (filenamec, ios::in);
406#endif
407 delete filenamec;
408
409 if (file_in) {
410 char c;
411 file_in.get(c);
412 while (!file_in.eof ()) {
413 content.push_back(c);
414 file_in.get(c);
415 }
416 file_in.close();
417 } else {
418 return false;
419 }
420 return true;
421}
Note: See TracBrowser for help on using the repository browser.