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

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

oops, fixed bug in unix version of file_copy function

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 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 *********************************************************************/
25
26#include "fileutil.h"
27
28#if defined(GSDL_USE_OBJECTSPACE)
29# include <ospace\std\iostream>
30# include <ospace\std\fstream>
31#elif defined(GSDL_USE_IOS_H)
32# include <iostream.h>
33# include <fstream.h>
34#else
35# include <iostream>
36# include <fstream>
37#endif
38
39
40// returns the proper concatenation of the two paths
41text_t filename_cat (text_t path1, text_t path2) {
42 text_t::iterator here;
43 text_t::iterator begin;
44 text_t::iterator end;
45
46 // make sure there is just one slash, of the correct type,
47 // at the end of path1 (unless path1 is an empty string).
48 if (!path1.empty()) {
49 // remove all trailing slashes
50 here = path1.end();
51 here--;
52 begin = path1.begin();
53 while (here != begin && (*here == '/' || *here == '\\')) {
54 here--;
55 }
56 here++;
57 path1.erase(here,path1.end());
58
59 // add one final slash
60#ifdef __WIN32__
61 path1 += "\\";
62#else
63 path1 += "/";
64#endif
65 }
66
67 // remove all slashes from the start of path2
68 here = path2.begin();
69 end = path2.end();
70 while (here != end && (*here == '/' || *here == '\\')) {
71 here++;
72 }
73 path2.erase (path2.begin(), here);
74
75 text_t fullpath = path1 + path2;
76
77 // make sure all the right slashes are used
78 here = fullpath.begin();
79 end = fullpath.end();
80 while (here != end) {
81#ifdef __WIN32__
82 if (*here == '/') *here = '\\';
83#else
84 if (*here == '\\') *here = '/';
85#endif
86 here ++;
87 }
88 return fullpath;
89}
90
91text_t filename_cat (text_t path1, text_t path2, text_t path3) {
92 return filename_cat(filename_cat(path1,path2),path3);
93}
94
95text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4) {
96 return filename_cat(filename_cat(filename_cat(path1,path2),path3),path4);
97}
98
99text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4,
100 text_t path5) {
101 return filename_cat(filename_cat(filename_cat(filename_cat(path1,path2),path3),
102 path4),path5);
103}
104
105// returns true if filename can be opened
106bool file_exists (const text_t &filename) {
107 char *cstr = filename.getcstr();
108#ifdef GSDL_USE_IOS_H
109 ifstream filestream (cstr, ios::in | ios::nocreate);
110#else
111 ifstream filestream (cstr, ios::in);
112#endif
113 delete cstr;
114
115 if (filestream) {
116 // file exists
117 filestream.close ();
118 return true;
119 }
120
121 // file does not exist
122 return false;
123}
124
125// returns true if filename can be opened
126bool file_writable (const text_t &filename) {
127 char *cstr = filename.getcstr();
128#ifdef GSDL_USE_IOS_H
129 ifstream filestream (cstr, ios::out | ios::nocreate);
130#else
131 ifstream filestream (cstr, ios::out);
132#endif
133 delete cstr;
134
135 if (filestream) {
136 // file exists
137 filestream.close ();
138 return true;
139 }
140
141 // file does not exist
142 return false;
143}
144
145#if defined(__WIN32__) && !defined(__GNUC__)
146
147#include <windows.h>
148
149bool directory_exists (const text_t &dirname) {
150
151 WIN32_FIND_DATA FileData;
152 HANDLE hSearch;
153 char *dirpath = dirname.getcstr();
154 strcat (dirpath, "\\*");
155
156 hSearch = FindFirstFile(dirpath, &FileData);
157 delete dirpath;
158
159 if (hSearch == INVALID_HANDLE_VALUE) {
160 return false;
161 }
162 return true;
163}
164
165bool read_dir (const text_t &dirname, text_tarray &filelist) {
166
167 WIN32_FIND_DATA FileData;
168 HANDLE hSearch;
169 char *dirpath = dirname.getcstr();
170 strcat (dirpath, "\\*");
171
172 hSearch = FindFirstFile(dirpath, &FileData);
173 delete dirpath;
174
175 if (hSearch == INVALID_HANDLE_VALUE) {
176 return false;
177 }
178
179 text_t filename = FileData.cFileName;
180 if (filename != "." && filename != ".." && filename != "CVS")
181 filelist.push_back (filename);
182
183 while (FindNextFile(hSearch, &FileData)) {
184 filename = FileData.cFileName;
185 if (filename == "." || filename == ".." || filename == "CVS")
186 continue;
187 filelist.push_back (filename);
188 }
189
190 FindClose(hSearch);
191
192 return true;
193}
194
195#else
196
197#include <dirent.h>
198
199bool directory_exists (const text_t &dirname) {
200
201 char *tmp = dirname.getcstr();
202 DIR *dirin = opendir (tmp);
203 delete tmp;
204
205 if (dirin == NULL) return false;
206 closedir (dirin);
207 return true;
208}
209
210bool read_dir (const text_t &dirname, text_tarray &filelist) {
211
212 filelist.erase (filelist.begin(), filelist.end());
213
214 char *tmp = dirname.getcstr();
215 DIR *dirin = opendir (tmp);
216 delete tmp;
217
218 if (dirin == NULL) return false;
219
220 dirent *dirp;
221
222 text_t filename;
223 while ((dirp = readdir (dirin)) != NULL) {
224 filename = dirp->d_name;
225 if (filename == "." || filename == ".." || filename == "CVS")
226 continue;
227 filelist.push_back (filename);
228 }
229 closedir (dirin);
230 return true;
231}
232
233#endif
234
235// I'm sure there's a better way to do this - for now I don't have
236// time to find it though. -- This will probably break compiling
237// under windows ports of gcc
238void file_copy (const text_t &fromfile, const text_t &tofile) {
239
240#ifdef __WIN32__
241 char *fromfilec = fromfile.getcstr();
242 char *tofilec = tofile.getcstr();
243 CopyFile (fromfilec, tofilec, FALSE);
244 delete fromfilec;
245 delete tofilec;
246
247#else
248 text_t cp_cmd = "cp " + fromfile + " " + tofile;
249 char *cp_cmdc = cp_cmd.getcstr();
250 system (cp_cmdc);
251 delete cp_cmdc;
252
253#endif
254}
Note: See TracBrowser for help on using the repository browser.