From: Christian P. <cp...@us...> - 2005-01-24 09:57:40
|
Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2303/src/System Added Files: Directory.win32.cpp Log Message: Added win32 Directory implementation. --- NEW FILE: Directory.win32.cpp --- /*************************************************************************** * Copyright (C) 2004 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library 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 Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "pclasses/System/Directory.h" #include <windows.h> namespace P { namespace System { Directory::Directory(const Unicode::String& path) throw(IO::IOError) : _path(path), _handle(INVALID_HANDLE_VALUE) { reload(); } Directory::~Directory() throw() { if(_handle != INVALID_HANDLE_VALUE) FindClose((HANDLE)_handle); } const Unicode::String& Directory::path() const throw() { return _path; } void Directory::reload() throw(IO::IOError) { _entries.clear(); WIN32_FIND_DATA findData; HANDLE h = FindFirstFile(_path.utf8().c_str(), &findData); if(h == INVALID_HANDLE_VALUE) { DWORD lastError = GetLastError(); if(lastError == ERROR_NO_MORE_FILES) return; throw SystemError(lastError, "Could not read directory", P_SOURCEINFO); } _entries.push_back(findData.cFileName); while(FindNextFile(h, &findData) != 0) { _entries.push_back(findData.cFileName); } DWORD lastError = GetLastError(); FindClose(h); if(lastError != ERROR_NO_MORE_FILES) throw SystemError(lastError, "Could not read directory", P_SOURCEINFO); } Directory::Iterator Directory::begin() const { return _entries.begin(); } Directory::Iterator Directory::end() const { return _entries.end(); } Unicode::String Directory::current() throw(IO::IOError) { size_t cwdSize = PATH_MAX; char* cwd = new char[PATH_MAX]; getCurrentDirectory: DWORD ret = GetCurrentDirectory(cwdSize, cwd); if(ret == 0) { if(ret > cwdSize) { delete[] cwd; cwdSize += cwdSize; cwd = new char[cwdSize]; goto getCurrentDirectory; } throw IO::IOError(errno, "Could not get current working directroy", P_SOURCEINFO); } return Unicode::String(cwd); } void Directory::change(const Unicode::String& path) throw(IO::IOError) { if(!SetCurrentDirectory(path.utf8().c_str())) throw IO::IOError(GetLastError(), "Could not change working directory", P_SOURCEINFO); } void Directory::create(const Unicode::String& path) throw(IO::IOError) { if(!CreateDirectory(path.utf8().c_str(), NULL)) throw IO::IOError(GetLastError(), "Could not change working directory", P_SOURCEINFO); } void Directory::remove(const Unicode::String& path) throw(IO::IOError) { if(!RemoveDirectory(path.utf8().c_str())) throw IO::IOError(GetLastError(), "Could not change working directory", P_SOURCEINFO); } } // !namespace System } // !namespace P |