/********************************************************************** * * fileutil.cpp -- * Copyright (C) 1999 The New Zealand Digital Library Project * * A component of the Greenstone digital library software * from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: fileutil.cpp 745 1999-10-25 22:27:51Z sjboddie $ * *********************************************************************/ /* $Log$ Revision 1.10 1999/10/25 22:27:51 sjboddie added read_dir function - doesn't support windows yet Revision 1.9 1999/10/19 03:52:25 davidb added some useful functions for collection building from webpages Revision 1.8 1999/09/07 04:57:42 sjboddie added gpl notice Revision 1.7 1999/08/31 08:00:43 rjmcnab Fixed a windows specific error in the file_exists function. Revision 1.6 1999/04/06 22:16:49 rjmcnab Added more options for filename_cat. Revision 1.5 1999/01/19 01:38:14 rjmcnab Made the source more portable. Revision 1.4 1999/01/12 01:50:59 rjmcnab Standard header. Revision 1.3 1999/01/08 02:33:14 rjmcnab Added standard header to source files. */ #include "fileutil.h" #if defined(GSDL_USE_OBJECTSPACE) # include # include #elif defined(GSDL_USE_IOS_H) # include # include #else # include # include #endif // returns the proper concatenation of the two paths text_t filename_cat (text_t path1, text_t path2) { text_t::iterator here; text_t::iterator begin; text_t::iterator end; // make sure there is just one slash, of the correct type, // at the end of path1 (unless path1 is an empty string). if (!path1.empty()) { // remove all trailing slashes here = path1.end(); here--; begin = path1.begin(); while (here != begin && (*here == '/' || *here == '\\')) { here--; } here++; path1.erase(here,path1.end()); // add one final slash #ifdef __WIN32__ path1 += "\\"; #else path1 += "/"; #endif } // remove all slashes from the start of path2 here = path2.begin(); end = path2.end(); while (here != end && (*here == '/' || *here == '\\')) { here++; } path2.erase (path2.begin(), here); // return the concatenation of the two strings return path1 + path2; } text_t filename_cat (text_t path1, text_t path2, text_t path3) { return filename_cat(filename_cat(path1,path2),path3); } text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4) { return filename_cat(filename_cat(filename_cat(path1,path2),path3),path4); } text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4, text_t path5) { return filename_cat(filename_cat(filename_cat(filename_cat(path1,path2),path3), path4),path5); } // returns true if filename can be opened bool file_exists (const text_t &filename) { char *cstr = filename.getcstr(); ifstream filestream (cstr, ios::in | ios::nocreate); delete cstr; if (filestream) { // file exists filestream.close (); return true; } // file does not exist return false; } // returns true if filename can be opened bool file_writable (const text_t &filename) { char *cstr = filename.getcstr(); ifstream filestream (cstr, ios::out | ios::nocreate); delete cstr; if (filestream) { // file exists filestream.close (); return true; } // file does not exist return false; } // need to do a windows version of this some time ... #if defined __WIN32__ bool read_dir (const text_t &dirname, text_tarray &filelist) {} #else #include bool read_dir (const text_t &dirname, text_tarray &filelist) { filelist.erase (filelist.begin(), filelist.end()); char *tmp = dirname.getcstr(); DIR *dirin = opendir (tmp); delete tmp; if (dirin == NULL) return false; dirent *dirp; text_t filename; while ((dirp = readdir (dirin)) != NULL) { filename = dirp->d_name; if (filename == "." || filename == ".." || filename == "CVS") continue; filelist.push_back (filename); } closedir (dirin); return true; } #endif