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

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

Removed CVS logging information from source files

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.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 read_dir (const text_t &dirname, text_tarray &filelist) {
150
151 WIN32_FIND_DATA FileData;
152 HANDLE hSearch;
153 char *dirpath = dirname.getcstr();
154 strcat (dirpath, "\\*");
155
156 hSearch = FindFirstFile(dirpath, &FileData);
157 if (hSearch == INVALID_HANDLE_VALUE) return false;
158
159 text_t filename = FileData.cFileName;
160 if (filename != "." && filename != ".." && filename != "CVS")
161 filelist.push_back (filename);
162
163 while (FindNextFile(hSearch, &FileData)) {
164 filename = FileData.cFileName;
165 if (filename == "." || filename == ".." || filename == "CVS")
166 continue;
167 filelist.push_back (filename);
168 }
169
170 FindClose(hSearch);
171
172 return true;
173}
174
175#else
176
177#include <dirent.h>
178
179bool read_dir (const text_t &dirname, text_tarray &filelist) {
180
181 filelist.erase (filelist.begin(), filelist.end());
182
183 char *tmp = dirname.getcstr();
184 DIR *dirin = opendir (tmp);
185 delete tmp;
186
187 if (dirin == NULL) return false;
188
189 dirent *dirp;
190
191 text_t filename;
192 while ((dirp = readdir (dirin)) != NULL) {
193 filename = dirp->d_name;
194 if (filename == "." || filename == ".." || filename == "CVS")
195 continue;
196 filelist.push_back (filename);
197 }
198 closedir (dirin);
199 return true;
200}
201
202#endif
Note: See TracBrowser for help on using the repository browser.