source: trunk/gsdl/lib/fileutil.cpp@ 1170

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

minor modifications to get web library compiling under VC++ 6.0

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