00001 /* 00002 * This file is part of ALVAR, A Library for Virtual and Augmented Reality. 00003 * 00004 * Copyright 2007-2012 VTT Technical Research Centre of Finland 00005 * 00006 * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi> 00007 * <http://www.vtt.fi/multimedia/alvar.html> 00008 * 00009 * ALVAR is free software; you can redistribute it and/or modify it under the 00010 * terms of the GNU Lesser General Public License as published by the Free 00011 * Software Foundation; either version 2.1 of the License, or (at your option) 00012 * any later version. 00013 * 00014 * This library is distributed in the hope that it will be useful, but WITHOUT 00015 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00016 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 00017 * for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public License 00020 * along with ALVAR; if not, see 00021 * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>. 00022 */ 00023 00024 #include "DirectoryIterator_private.h" 00025 00026 #include <windows.h> 00027 00028 namespace alvar { 00029 00030 class DirectoryIteratorPrivateData 00031 { 00032 public: 00033 DirectoryIteratorPrivateData() 00034 : mHandle(INVALID_HANDLE_VALUE) 00035 , mData() 00036 { 00037 } 00038 00039 HANDLE mHandle; 00040 WIN32_FIND_DATA mData; 00041 }; 00042 00043 DirectoryIteratorPrivate::DirectoryIteratorPrivate(const std::string &path) 00044 : d(new DirectoryIteratorPrivateData()) 00045 , mDirectory(path) 00046 , mEntry() 00047 , mValid(false) 00048 { 00049 if (mDirectory.at(mDirectory.length() - 1) != '\\') { 00050 mDirectory.append("\\"); 00051 } 00052 } 00053 00054 DirectoryIteratorPrivate::~DirectoryIteratorPrivate() 00055 { 00056 FindClose(d->mHandle); 00057 delete d; 00058 } 00059 00060 bool DirectoryIteratorPrivate::hasNext() 00061 { 00062 if (d->mHandle == INVALID_HANDLE_VALUE) { 00063 std::string search = mDirectory + "*"; 00064 d->mHandle = FindFirstFile(search.data(), &d->mData); 00065 00066 if (d->mHandle != INVALID_HANDLE_VALUE) { 00067 mValid = true; 00068 skip(); 00069 } 00070 } 00071 00072 return mValid; 00073 } 00074 00075 std::string DirectoryIteratorPrivate::next() 00076 { 00077 if (!hasNext()) { 00078 return ""; 00079 } 00080 00081 mEntry = std::string(d->mData.cFileName); 00082 00083 if (!FindNextFile(d->mHandle, &d->mData)) { 00084 mValid = false; 00085 } 00086 else { 00087 skip(); 00088 } 00089 00090 return mEntry; 00091 } 00092 00093 void DirectoryIteratorPrivate::skip() 00094 { 00095 while (true) { 00096 if (std::string(d->mData.cFileName) != "." && std::string(d->mData.cFileName) != "..") { 00097 return; 00098 } 00099 00100 if (!FindNextFile(d->mHandle, &d->mData)) { 00101 mValid = false; 00102 return; 00103 } 00104 } 00105 } 00106 00107 } // namespace alvar