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

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

added readdir function for windows

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