source: branches/New_Config_Format-branch/gsdl/lib/fileutil.cpp@ 1279

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

merged changes to trunk into New_Config_Format branch

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