[5503] | 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
|
---|
| 41 | text_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 |
|
---|
| 91 | text_t filename_cat (text_t path1, text_t path2, text_t path3) {
|
---|
| 92 | return filename_cat(filename_cat(path1,path2),path3);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | text_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 |
|
---|
| 99 | text_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 | text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4,
|
---|
| 106 | text_t path5, text_t path6) {
|
---|
| 107 | return filename_cat(filename_cat(path1,path2,path3,path4,path5),path6);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | // returns true if filename can be opened
|
---|
| 111 | bool file_exists (const text_t &filename) {
|
---|
| 112 | char *cstr = filename.getcstr();
|
---|
| 113 | #ifdef GSDL_USE_IOS_H
|
---|
| 114 | ifstream filestream (cstr, ios::in | ios::nocreate);
|
---|
| 115 | #else
|
---|
| 116 | ifstream filestream (cstr, ios::in);
|
---|
| 117 | #endif
|
---|
| 118 | delete cstr;
|
---|
| 119 |
|
---|
| 120 | if (filestream) {
|
---|
| 121 | // file exists
|
---|
| 122 | filestream.close ();
|
---|
| 123 | return true;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | // file does not exist
|
---|
| 127 | return false;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | // returns true if filename can be opened
|
---|
| 131 | bool file_writable (const text_t &filename) {
|
---|
| 132 | char *cstr = filename.getcstr();
|
---|
| 133 | #ifdef GSDL_USE_IOS_H
|
---|
| 134 | ifstream filestream (cstr, ios::out | ios::nocreate);
|
---|
| 135 | #else
|
---|
| 136 | ifstream filestream (cstr, ios::out);
|
---|
| 137 | #endif
|
---|
| 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 | #if defined(__WIN32__) && !defined(__GNUC__)
|
---|
| 151 |
|
---|
| 152 | #include <windows.h>
|
---|
| 153 |
|
---|
| 154 | bool directory_exists (const text_t &dirname) {
|
---|
| 155 |
|
---|
| 156 | WIN32_FIND_DATA FileData;
|
---|
| 157 | HANDLE hSearch;
|
---|
| 158 |
|
---|
| 159 | text_t dir = dirname + "\\*";
|
---|
| 160 | char *dirpath = dir.getcstr();
|
---|
| 161 |
|
---|
| 162 | hSearch = FindFirstFile(dirpath, &FileData);
|
---|
| 163 | if (hSearch == INVALID_HANDLE_VALUE) {
|
---|
| 164 | delete dirpath;
|
---|
| 165 | return false;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | FindClose (hSearch);
|
---|
| 169 | delete dirpath;
|
---|
| 170 | return true;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | bool read_dir (const text_t &dirname, text_tset &filelist) {
|
---|
| 174 |
|
---|
| 175 | WIN32_FIND_DATA FileData;
|
---|
| 176 | HANDLE hSearch;
|
---|
| 177 |
|
---|
| 178 | text_t dir = dirname + "\\*";
|
---|
| 179 | char *dirpath = dir.getcstr();
|
---|
| 180 |
|
---|
| 181 | hSearch = FindFirstFile(dirpath, &FileData);
|
---|
| 182 | if (hSearch == INVALID_HANDLE_VALUE) {
|
---|
| 183 | delete dirpath;
|
---|
| 184 | return false;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | text_t filename = FileData.cFileName;
|
---|
| 188 | if (filename != "." && filename != ".." && filename != "CVS")
|
---|
| 189 | filelist.insert (filename);
|
---|
| 190 |
|
---|
| 191 | while (FindNextFile(hSearch, &FileData)) {
|
---|
| 192 | filename = FileData.cFileName;
|
---|
| 193 | if (filename == "." || filename == ".." || filename == "CVS")
|
---|
| 194 | continue;
|
---|
| 195 | filelist.insert (filename);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | FindClose(hSearch);
|
---|
| 199 | delete dirpath;
|
---|
| 200 |
|
---|
| 201 | return true;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | bool read_dir (const text_t &dirname, text_tarray &filelist) {
|
---|
| 205 |
|
---|
| 206 | WIN32_FIND_DATA FileData;
|
---|
| 207 | HANDLE hSearch;
|
---|
| 208 |
|
---|
| 209 | text_t dir = dirname + "\\*";
|
---|
| 210 | char *dirpath = dir.getcstr();
|
---|
| 211 |
|
---|
| 212 | hSearch = FindFirstFile(dirpath, &FileData);
|
---|
| 213 | if (hSearch == INVALID_HANDLE_VALUE) {
|
---|
| 214 | delete dirpath;
|
---|
| 215 | return false;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | text_t filename = FileData.cFileName;
|
---|
| 219 | if (filename != "." && filename != ".." && filename != "CVS")
|
---|
| 220 | filelist.push_back (filename);
|
---|
| 221 |
|
---|
| 222 | while (FindNextFile(hSearch, &FileData)) {
|
---|
| 223 | filename = FileData.cFileName;
|
---|
| 224 | if (filename == "." || filename == ".." || filename == "CVS")
|
---|
| 225 | continue;
|
---|
| 226 | filelist.push_back (filename);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | FindClose(hSearch);
|
---|
| 230 | delete dirpath;
|
---|
| 231 |
|
---|
| 232 | return true;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | #else
|
---|
| 236 |
|
---|
| 237 | #include <dirent.h>
|
---|
| 238 |
|
---|
| 239 | bool directory_exists (const text_t &dirname) {
|
---|
| 240 |
|
---|
| 241 | char *tmp = dirname.getcstr();
|
---|
| 242 | DIR *dirin = opendir (tmp);
|
---|
| 243 | delete tmp;
|
---|
| 244 |
|
---|
| 245 | if (dirin == NULL) return false;
|
---|
| 246 | closedir (dirin);
|
---|
| 247 | return true;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | bool read_dir (const text_t &dirname, text_tset &filelist) {
|
---|
| 251 |
|
---|
| 252 | char *tmp = dirname.getcstr();
|
---|
| 253 | DIR *dirin = opendir (tmp);
|
---|
| 254 | delete tmp;
|
---|
| 255 |
|
---|
| 256 | if (dirin == NULL) return false;
|
---|
| 257 |
|
---|
| 258 | dirent *dirp;
|
---|
| 259 |
|
---|
| 260 | text_t filename;
|
---|
| 261 | while ((dirp = readdir (dirin)) != NULL) {
|
---|
| 262 | filename = dirp->d_name;
|
---|
| 263 | if (filename == "." || filename == ".." || filename == "CVS")
|
---|
| 264 | continue;
|
---|
| 265 | filelist.insert (filename);
|
---|
| 266 | }
|
---|
| 267 | closedir (dirin);
|
---|
| 268 | return true;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | bool read_dir (const text_t &dirname, text_tarray &filelist) {
|
---|
| 272 |
|
---|
| 273 | char *tmp = dirname.getcstr();
|
---|
| 274 | DIR *dirin = opendir (tmp);
|
---|
| 275 | delete tmp;
|
---|
| 276 |
|
---|
| 277 | if (dirin == NULL) return false;
|
---|
| 278 |
|
---|
| 279 | dirent *dirp;
|
---|
| 280 |
|
---|
| 281 | text_t filename;
|
---|
| 282 | while ((dirp = readdir (dirin)) != NULL) {
|
---|
| 283 | filename = dirp->d_name;
|
---|
| 284 | if (filename == "." || filename == ".." || filename == "CVS")
|
---|
| 285 | continue;
|
---|
| 286 | filelist.push_back (filename);
|
---|
| 287 | }
|
---|
| 288 | closedir (dirin);
|
---|
| 289 | return true;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | #endif
|
---|
| 293 |
|
---|
| 294 | // returns true if things look like they happened ok
|
---|
| 295 | bool file_copy (const text_t &fromfile, const text_t &tofile) {
|
---|
| 296 |
|
---|
| 297 | char *fromfilec = fromfile.getcstr();
|
---|
| 298 | char *tofilec = tofile.getcstr();
|
---|
| 299 | bool fail = false;
|
---|
| 300 |
|
---|
| 301 | #ifdef __WIN32__
|
---|
| 302 | if (CopyFile (fromfilec, tofilec, FALSE) == 0) fail = true;
|
---|
| 303 |
|
---|
| 304 | #else
|
---|
| 305 |
|
---|
| 306 | ifstream from (fromfilec);
|
---|
| 307 | if (!from) {
|
---|
| 308 | fail = true;
|
---|
| 309 | } else {
|
---|
| 310 | ofstream to (tofilec);
|
---|
| 311 | if (!to) {
|
---|
| 312 | fail = true;
|
---|
| 313 | from.close();
|
---|
| 314 | } else {
|
---|
| 315 | from >> to.rdbuf();
|
---|
| 316 | from.close();
|
---|
| 317 | to.close();
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | #endif
|
---|
| 322 |
|
---|
| 323 | delete fromfilec;
|
---|
| 324 | delete tofilec;
|
---|
| 325 | if (fail) return false;
|
---|
| 326 | return true;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | // This is a fairly quick and nasty attempt at doing a "tail" on a file
|
---|
| 330 | // (i.e. returning the last numlines lines of filename). It has one
|
---|
| 331 | // important limitation in that it expects lines to be linelength
|
---|
| 332 | // characters long on average. This of course makes it possible that it
|
---|
| 333 | // won't return numlines lines if there are some lines that are longer than
|
---|
| 334 | // linelength. It also makes it fairly inefficient as it always reads in
|
---|
| 335 | // numlines*linelength characters when it most often doesn't need them all.
|
---|
| 336 | // For current needs it's fine though.
|
---|
| 337 | // -- Pass a linelength of 0 to use the default linelength (256 chars).
|
---|
| 338 | // -- The maximum value of linelength is MAXLINELENGTH
|
---|
| 339 | text_t file_tail (const text_t &filename, int numlines, int linelength) {
|
---|
| 340 |
|
---|
| 341 | #define MAXLINELENGTH 2048
|
---|
| 342 |
|
---|
| 343 | if (numlines < 1) numlines = 1;
|
---|
| 344 | if (linelength < 1) linelength = 256;
|
---|
| 345 | if (linelength > MAXLINELENGTH) linelength = MAXLINELENGTH;
|
---|
| 346 |
|
---|
| 347 | streampos numchars = linelength*numlines;
|
---|
| 348 |
|
---|
| 349 | text_tarray lines;
|
---|
| 350 | text_t ret;
|
---|
| 351 |
|
---|
| 352 | char *filenamec = filename.getcstr();
|
---|
| 353 | char linec[MAXLINELENGTH];
|
---|
| 354 | ifstream file_in (filenamec);
|
---|
| 355 | delete filenamec;
|
---|
| 356 | if (file_in) {
|
---|
| 357 | file_in.seekg (0, ios::end);
|
---|
| 358 | streampos file_length = file_in.tellg();
|
---|
| 359 | if (file_length < numchars) numchars = file_length;
|
---|
| 360 | file_in.seekg (-numchars, ios::end);
|
---|
| 361 |
|
---|
| 362 | while (!file_in.eof()) {
|
---|
| 363 | file_in.getline (linec, linelength);
|
---|
| 364 | ret.setcstr(linec);
|
---|
| 365 | text_t::const_iterator here = ret.begin();
|
---|
| 366 | text_t::const_iterator end = ret.end();
|
---|
| 367 | // make sure line has content
|
---|
| 368 | while (here != end) {
|
---|
| 369 | if (*here != '\n' && *here != ' ') {
|
---|
| 370 | lines.push_back (ret);
|
---|
| 371 | break;
|
---|
| 372 | }
|
---|
| 373 | here ++;
|
---|
| 374 | }
|
---|
| 375 | }
|
---|
| 376 | file_in.close();
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | ret.clear();
|
---|
| 380 | int numlinesgot = lines.size();
|
---|
| 381 | int sindex = 0;
|
---|
| 382 | if (numlinesgot > numlines) sindex = numlinesgot - numlines;
|
---|
| 383 | for (int i = sindex; i < numlinesgot; i++) {
|
---|
| 384 | ret += lines[i] + "\n";
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | return ret;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | #ifdef __WIN32__
|
---|
| 391 |
|
---|
| 392 | #include <direct.h>
|
---|
| 393 | // returns true if directory was created successfully
|
---|
| 394 | bool mk_dir (const text_t &dirname) {
|
---|
| 395 | char *dirnamec = dirname.getcstr();
|
---|
| 396 | int rv = _mkdir (dirnamec);
|
---|
| 397 | delete dirnamec;
|
---|
| 398 | if (rv == 0) return true;
|
---|
| 399 | return false;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | #else
|
---|
| 403 |
|
---|
| 404 | #include <sys/stat.h>
|
---|
| 405 | #include <sys/types.h>
|
---|
| 406 | #include <fcntl.h>
|
---|
| 407 | #include <unistd.h>
|
---|
| 408 | // returns true if directory was created successfully
|
---|
| 409 | bool mk_dir (const text_t &dirname) {
|
---|
| 410 | mode_t mode = 0777;
|
---|
| 411 | char *dirnamec = dirname.getcstr();
|
---|
| 412 | int rv = mkdir (dirnamec, mode);
|
---|
| 413 | delete dirnamec;
|
---|
| 414 | if (rv == 0) return true;
|
---|
| 415 | return false;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | #endif
|
---|
| 419 |
|
---|
| 420 | // read in file from filename and load into content
|
---|
| 421 | bool read_file (const text_t &filename, text_t &content) {
|
---|
| 422 |
|
---|
| 423 | content.clear();
|
---|
| 424 |
|
---|
| 425 | char *filenamec = filename.getcstr();
|
---|
| 426 | #ifdef GSDL_USE_IOS_H
|
---|
| 427 | ifstream file_in (filenamec, ios::in | ios::nocreate);
|
---|
| 428 | #else
|
---|
| 429 | ifstream file_in (filenamec, ios::in);
|
---|
| 430 | #endif
|
---|
| 431 | delete filenamec;
|
---|
| 432 |
|
---|
| 433 | if (file_in) {
|
---|
| 434 | char c;
|
---|
| 435 | file_in.get(c);
|
---|
| 436 | while (!file_in.eof ()) {
|
---|
| 437 | content.push_back(c);
|
---|
| 438 | file_in.get(c);
|
---|
| 439 | }
|
---|
| 440 | file_in.close();
|
---|
| 441 | } else {
|
---|
| 442 | return false;
|
---|
| 443 | }
|
---|
| 444 | return true;
|
---|
| 445 | }
|
---|