source: main/tags/2.13/gsdl/lib/fileutil.cpp@ 24552

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

removed old comment

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