DirectoryIterator_unix.cpp
Go to the documentation of this file.
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 <dirent.h>
00027 
00028 namespace alvar {
00029 
00030 class DirectoryIteratorPrivateData
00031 {
00032 public:
00033     DirectoryIteratorPrivateData()
00034         : mHandle(NULL)
00035         , mData(NULL)
00036     {
00037     }
00038     
00039     DIR *mHandle;
00040     dirent *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     closedir(d->mHandle);
00057     delete d;
00058 }
00059 
00060 bool DirectoryIteratorPrivate::hasNext()
00061 {
00062     if (d->mHandle == NULL) {
00063         d->mHandle = opendir(mDirectory.data());
00064 
00065         if (d->mHandle != NULL) {
00066             d->mData = readdir(d->mHandle);
00067             
00068             if (d->mData != NULL) {
00069                 mValid = true;
00070                 skip();
00071             }
00072         }
00073     }
00074 
00075     return mValid;
00076 }
00077 
00078 std::string DirectoryIteratorPrivate::next()
00079 {
00080     if (!hasNext()) {
00081         return "";
00082     }
00083 
00084     mEntry = std::string(d->mData->d_name);
00085 
00086     d->mData = readdir(d->mHandle);
00087     if (d->mData == NULL) {
00088         mValid = false;
00089     }
00090     else {
00091         skip();
00092     }
00093 
00094     return mEntry;
00095 }
00096 
00097 void DirectoryIteratorPrivate::skip()
00098 {
00099     while (true) {
00100         if (std::string(d->mData->d_name) != "." && std::string(d->mData->d_name) != "..") {
00101             return;
00102         }
00103 
00104         d->mData = readdir(d->mHandle);
00105         if (d->mData == NULL) {
00106             mValid = false;
00107             return;
00108         }
00109     }
00110 }
00111 
00112 } // namespace alvar


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Sat Dec 28 2013 16:46:15