[100] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * fileutil.cpp --
|
---|
| 4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
| 5 | *
|
---|
[534] | 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.
|
---|
[100] | 9 | *
|
---|
[534] | 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 | *
|
---|
[100] | 24 | * $Id: fileutil.cpp 960 2000-02-21 21:53:19Z sjboddie $
|
---|
| 25 | *
|
---|
| 26 | *********************************************************************/
|
---|
| 27 |
|
---|
| 28 | /*
|
---|
| 29 | $Log$
|
---|
[960] | 30 | Revision 1.12 2000/02/21 21:53:19 sjboddie
|
---|
| 31 | removed old comment
|
---|
| 32 |
|
---|
[785] | 33 | Revision 1.11 1999/11/21 01:09:30 sjboddie
|
---|
| 34 | added readdir function for windows
|
---|
| 35 |
|
---|
[745] | 36 | Revision 1.10 1999/10/25 22:27:51 sjboddie
|
---|
| 37 | added read_dir function - doesn't support windows yet
|
---|
| 38 |
|
---|
[726] | 39 | Revision 1.9 1999/10/19 03:52:25 davidb
|
---|
| 40 | added some useful functions for collection building
|
---|
| 41 | from webpages
|
---|
| 42 |
|
---|
[534] | 43 | Revision 1.8 1999/09/07 04:57:42 sjboddie
|
---|
| 44 | added gpl notice
|
---|
| 45 |
|
---|
[478] | 46 | Revision 1.7 1999/08/31 08:00:43 rjmcnab
|
---|
| 47 | Fixed a windows specific error in the file_exists function.
|
---|
| 48 |
|
---|
[224] | 49 | Revision 1.6 1999/04/06 22:16:49 rjmcnab
|
---|
| 50 | Added more options for filename_cat.
|
---|
| 51 |
|
---|
[114] | 52 | Revision 1.5 1999/01/19 01:38:14 rjmcnab
|
---|
| 53 |
|
---|
| 54 | Made the source more portable.
|
---|
| 55 |
|
---|
[112] | 56 | Revision 1.4 1999/01/12 01:50:59 rjmcnab
|
---|
| 57 |
|
---|
| 58 | Standard header.
|
---|
| 59 |
|
---|
[100] | 60 | Revision 1.3 1999/01/08 02:33:14 rjmcnab
|
---|
| 61 |
|
---|
| 62 | Added standard header to source files.
|
---|
| 63 |
|
---|
| 64 | */
|
---|
| 65 |
|
---|
| 66 |
|
---|
[89] | 67 | #include "fileutil.h"
|
---|
| 68 |
|
---|
[114] | 69 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
| 70 | # include <ospace\std\iostream>
|
---|
| 71 | # include <ospace\std\fstream>
|
---|
| 72 | #elif defined(GSDL_USE_IOS_H)
|
---|
[91] | 73 | # include <iostream.h>
|
---|
| 74 | # include <fstream.h>
|
---|
| 75 | #else
|
---|
[114] | 76 | # include <iostream>
|
---|
| 77 | # include <fstream>
|
---|
[91] | 78 | #endif
|
---|
| 79 |
|
---|
| 80 |
|
---|
[89] | 81 | // returns the proper concatenation of the two paths
|
---|
| 82 | text_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 |
|
---|
[224] | 120 | text_t filename_cat (text_t path1, text_t path2, text_t path3) {
|
---|
| 121 | return filename_cat(filename_cat(path1,path2),path3);
|
---|
| 122 | }
|
---|
[91] | 123 |
|
---|
[224] | 124 | text_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 |
|
---|
| 128 | text_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 |
|
---|
[91] | 134 | // returns true if filename can be opened
|
---|
| 135 | bool file_exists (const text_t &filename) {
|
---|
| 136 | char *cstr = filename.getcstr();
|
---|
[478] | 137 | ifstream filestream (cstr, ios::in | ios::nocreate);
|
---|
[91] | 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 | }
|
---|
[726] | 149 |
|
---|
| 150 | // returns true if filename can be opened
|
---|
| 151 | bool 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 | }
|
---|
[745] | 165 |
|
---|
| 166 | #if defined __WIN32__
|
---|
| 167 |
|
---|
[785] | 168 | #include <windows.h>
|
---|
| 169 |
|
---|
| 170 | BOOL 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 |
|
---|
[745] | 199 | #else
|
---|
| 200 |
|
---|
| 201 | #include <dirent.h>
|
---|
| 202 |
|
---|
| 203 | bool 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
|
---|