/********************************************************************** * * 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 1279 2000-07-12 22:21:53Z sjboddie $ * *********************************************************************/ /* $Log$ Revision 1.14.2.1 2000/07/12 22:20:54 sjboddie merged changes to trunk into New_Config_Format branch Revision 1.16 2000/05/12 03:09:22 sjboddie minor modifications to get web library compiling under VC++ 6.0 Revision 1.15 2000/05/04 08:27:28 sjboddie modifications for windows ports of GCC Revision 1.14 2000/04/06 19:57:59 cs025 Correcting a correction - reinstated all lib files due to silly CVS confusion. Revision 1.12 2000/02/21 21:53:19 sjboddie removed old comment Revision 1.11 1999/11/21 01:09:30 sjboddie added readdir function for windows 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); text_t fullpath = path1 + path2; // make sure all the right slashes are used here = fullpath.begin(); end = fullpath.end(); while (here != end) { #ifdef __WIN32__ if (*here == '/') *here = '\\'; #else if (*here == '\\') *here = '/'; #endif here ++; } return fullpath; } 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(); #ifdef GSDL_USE_IOS_H ifstream filestream (cstr, ios::in | ios::nocreate); #else ifstream filestream (cstr, ios::in); #endif 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(); #ifdef GSDL_USE_IOS_H ifstream filestream (cstr, ios::out | ios::nocreate); #else ifstream filestream (cstr, ios::out); #endif delete cstr; if (filestream) { // file exists filestream.close (); return true; } // file does not exist return false; } #if defined(__WIN32__) && !defined(__GNUC__) #include bool read_dir (const text_t &dirname, text_tarray &filelist) { WIN32_FIND_DATA FileData; HANDLE hSearch; char *dirpath = dirname.getcstr(); strcat (dirpath, "\\*"); hSearch = FindFirstFile(dirpath, &FileData); if (hSearch == INVALID_HANDLE_VALUE) return false; text_t filename = FileData.cFileName; if (filename != "." && filename != ".." && filename != "CVS") filelist.push_back (filename); while (FindNextFile(hSearch, &FileData)) { filename = FileData.cFileName; if (filename == "." || filename == ".." || filename == "CVS") continue; filelist.push_back (filename); } FindClose(hSearch); return true; } #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