Home | History | Annotate | Download | only in decpp
      1 /*-------------------------------------------------------------------------
      2  * drawElements Quality Program Tester Core
      3  * ----------------------------------------
      4  *
      5  * Copyright 2014 The Android Open Source Project
      6  *
      7  * Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *      http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  *
     19  *//*!
     20  * \file
     21  * \brief Directory iterator.
     22  *//*--------------------------------------------------------------------*/
     23 
     24 #include "deDirectoryIterator.hpp"
     25 #include "deString.h"
     26 
     27 #if (DE_DIRITER == DE_DIRITER_WIN32)
     28 #	include <direct.h> /* _chdir() */
     29 #	include <io.h> /* _findfirst(), _findnext() */
     30 #endif
     31 
     32 namespace de
     33 {
     34 
     35 #if (DE_DIRITER == DE_DIRITER_WIN32)
     36 
     37 DirectoryIterator::DirectoryIterator (const FilePath& path)
     38 	: m_path(FilePath::normalize(path))
     39 {
     40 	DE_CHECK_RUNTIME_ERR(m_path.exists());
     41 	DE_CHECK_RUNTIME_ERR(m_path.getType() == FilePath::TYPE_DIRECTORY);
     42 
     43 	m_handle	= _findfirst32((std::string(m_path.getPath()) + "/*").c_str(), &m_fileInfo);
     44 	m_hasItem	= m_handle != -1;
     45 
     46 	skipCurAndParent();
     47 }
     48 
     49 DirectoryIterator::~DirectoryIterator (void)
     50 {
     51 	if (m_handle != -1)
     52 		_findclose(m_handle);
     53 }
     54 
     55 bool DirectoryIterator::hasItem (void) const
     56 {
     57 	return m_hasItem;
     58 }
     59 
     60 FilePath DirectoryIterator::getItem (void) const
     61 {
     62 	DE_ASSERT(hasItem());
     63 	return FilePath::join(m_path, m_fileInfo.name);
     64 }
     65 
     66 void DirectoryIterator::next (void)
     67 {
     68 	m_hasItem = (_findnext32(m_handle, &m_fileInfo) == 0);
     69 	skipCurAndParent();
     70 }
     71 
     72 void DirectoryIterator::skipCurAndParent (void)
     73 {
     74 	while (m_hasItem && (deStringEqual(m_fileInfo.name, "..") || deStringEqual(m_fileInfo.name, ".")))
     75 		m_hasItem = (_findnext32(m_handle, &m_fileInfo) == 0);
     76 }
     77 
     78 #elif (DE_DIRITER == DE_DIRITER_POSIX)
     79 
     80 DirectoryIterator::DirectoryIterator (const FilePath& path)
     81 	: m_path	(FilePath::normalize(path))
     82 	, m_handle	(DE_NULL)
     83 	, m_curEntry(DE_NULL)
     84 {
     85 	DE_CHECK_RUNTIME_ERR(m_path.exists());
     86 	DE_CHECK_RUNTIME_ERR(m_path.getType() == FilePath::TYPE_DIRECTORY);
     87 
     88 	m_handle = opendir(m_path.getPath());
     89 	DE_CHECK_RUNTIME_ERR(m_handle);
     90 
     91 	// Find first entry
     92 	next();
     93 }
     94 
     95 DirectoryIterator::~DirectoryIterator (void)
     96 {
     97 	closedir(m_handle);
     98 }
     99 
    100 bool DirectoryIterator::hasItem (void) const
    101 {
    102 	return (m_curEntry != DE_NULL);
    103 }
    104 
    105 FilePath DirectoryIterator::getItem (void) const
    106 {
    107 	DE_ASSERT(hasItem());
    108 	return FilePath::join(m_path, m_curEntry->d_name);
    109 }
    110 
    111 void DirectoryIterator::next (void)
    112 {
    113 	do
    114 	{
    115 		m_curEntry = readdir(m_handle);
    116 	} while (m_curEntry && (deStringEqual(m_curEntry->d_name, "..") || deStringEqual(m_curEntry->d_name, ".")));
    117 }
    118 
    119 #endif
    120 
    121 } // de
    122