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

Last change on this file since 1076 was 1076, checked in by cs025, 24 years ago

Correcting a correction - reinstated all lib files due to silly
CVS confusion.

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