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