source: main/tags/2.20/gsdl/lib/fileutil.cpp@ 33021

Last change on this file since 33021 was 1150, checked in by sjboddie, 24 years ago

modifications for windows ports of GCC

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 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 * $Id: fileutil.cpp 1150 2000-05-04 08:32:31Z sjboddie $
25 *
26 *********************************************************************/
27
28/*
29 $Log$
30 Revision 1.15 2000/05/04 08:27:28 sjboddie
31 modifications for windows ports of GCC
32
33 Revision 1.14 2000/04/06 19:57:59 cs025
34 Correcting a correction - reinstated all lib files due to silly
35 CVS confusion.
36
37 Revision 1.12 2000/02/21 21:53:19 sjboddie
38 removed old comment
39
40 Revision 1.11 1999/11/21 01:09:30 sjboddie
41 added readdir function for windows
42
43 Revision 1.10 1999/10/25 22:27:51 sjboddie
44 added read_dir function - doesn't support windows yet
45
46 Revision 1.9 1999/10/19 03:52:25 davidb
47 added some useful functions for collection building
48 from webpages
49
50 Revision 1.8 1999/09/07 04:57:42 sjboddie
51 added gpl notice
52
53 Revision 1.7 1999/08/31 08:00:43 rjmcnab
54 Fixed a windows specific error in the file_exists function.
55
56 Revision 1.6 1999/04/06 22:16:49 rjmcnab
57 Added more options for filename_cat.
58
59 Revision 1.5 1999/01/19 01:38:14 rjmcnab
60
61 Made the source more portable.
62
63 Revision 1.4 1999/01/12 01:50:59 rjmcnab
64
65 Standard header.
66
67 Revision 1.3 1999/01/08 02:33:14 rjmcnab
68
69 Added standard header to source files.
70
71 */
72
73
74#include "fileutil.h"
75
76#if defined(GSDL_USE_OBJECTSPACE)
77# include <ospace\std\iostream>
78# include <ospace\std\fstream>
79#elif defined(GSDL_USE_IOS_H)
80# include <iostream.h>
81# include <fstream.h>
82#else
83# include <iostream>
84# include <fstream>
85#endif
86
87
88// returns the proper concatenation of the two paths
89text_t filename_cat (text_t path1, text_t path2) {
90 text_t::iterator here;
91 text_t::iterator begin;
92 text_t::iterator end;
93
94 // make sure there is just one slash, of the correct type,
95 // at the end of path1 (unless path1 is an empty string).
96 if (!path1.empty()) {
97 // remove all trailing slashes
98 here = path1.end();
99 here--;
100 begin = path1.begin();
101 while (here != begin && (*here == '/' || *here == '\\')) {
102 here--;
103 }
104 here++;
105 path1.erase(here,path1.end());
106
107 // add one final slash
108#ifdef __WIN32__
109 path1 += "\\";
110#else
111 path1 += "/";
112#endif
113 }
114
115 // remove all slashes from the start of path2
116 here = path2.begin();
117 end = path2.end();
118 while (here != end && (*here == '/' || *here == '\\')) {
119 here++;
120 }
121 path2.erase (path2.begin(), here);
122
123 // return the concatenation of the two strings
124 return path1 + path2;
125}
126
127text_t filename_cat (text_t path1, text_t path2, text_t path3) {
128 return filename_cat(filename_cat(path1,path2),path3);
129}
130
131text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4) {
132 return filename_cat(filename_cat(filename_cat(path1,path2),path3),path4);
133}
134
135text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4,
136 text_t path5) {
137 return filename_cat(filename_cat(filename_cat(filename_cat(path1,path2),path3),
138 path4),path5);
139}
140
141// returns true if filename can be opened
142bool file_exists (const text_t &filename) {
143 char *cstr = filename.getcstr();
144 ifstream filestream (cstr, ios::in | ios::nocreate);
145 delete cstr;
146
147 if (filestream) {
148 // file exists
149 filestream.close ();
150 return true;
151 }
152
153 // file does not exist
154 return false;
155}
156
157// returns true if filename can be opened
158bool file_writable (const text_t &filename) {
159 char *cstr = filename.getcstr();
160 ifstream filestream (cstr, ios::out | ios::nocreate);
161 delete cstr;
162
163 if (filestream) {
164 // file exists
165 filestream.close ();
166 return true;
167 }
168
169 // file does not exist
170 return false;
171}
172
173#if defined(__WIN32__) && !defined(__GNUC__)
174
175#include <windows.h>
176
177bool read_dir (const text_t &dirname, text_tarray &filelist) {
178
179 WIN32_FIND_DATA FileData;
180 HANDLE hSearch;
181 char *dirpath = dirname.getcstr();
182 strcat (dirpath, "\\*");
183
184 hSearch = FindFirstFile(dirpath, &FileData);
185 if (hSearch == INVALID_HANDLE_VALUE) return false;
186
187 text_t filename = FileData.cFileName;
188 if (filename != "." && filename != ".." && filename != "CVS")
189 filelist.push_back (filename);
190
191 while (FindNextFile(hSearch, &FileData)) {
192 filename = FileData.cFileName;
193 if (filename == "." || filename == ".." || filename == "CVS")
194 continue;
195 filelist.push_back (filename);
196 }
197
198 FindClose(hSearch);
199
200 return true;
201}
202
203#else
204
205#include <dirent.h>
206
207bool read_dir (const text_t &dirname, text_tarray &filelist) {
208
209 filelist.erase (filelist.begin(), filelist.end());
210
211 char *tmp = dirname.getcstr();
212 DIR *dirin = opendir (tmp);
213 delete tmp;
214
215 if (dirin == NULL) return false;
216
217 dirent *dirp;
218
219 text_t filename;
220 while ((dirp = readdir (dirin)) != NULL) {
221 filename = dirp->d_name;
222 if (filename == "." || filename == ".." || filename == "CVS")
223 continue;
224 filelist.push_back (filename);
225 }
226 closedir (dirin);
227 return true;
228}
229
230#endif
Note: See TracBrowser for help on using the repository browser.