xref: /aosp_15_r20/external/deqp/framework/delibs/decpp/deDirectoryIterator.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
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 
DirectoryIterator(const FilePath & path)37 DirectoryIterator::DirectoryIterator(const FilePath &path) : m_path(FilePath::normalize(path))
38 {
39     DE_CHECK_RUNTIME_ERR(m_path.exists());
40     DE_CHECK_RUNTIME_ERR(m_path.getType() == FilePath::TYPE_DIRECTORY);
41 
42     m_handle  = _findfirst32((std::string(m_path.getPath()) + "/*").c_str(), &m_fileInfo);
43     m_hasItem = m_handle != -1;
44 
45     skipCurAndParent();
46 }
47 
~DirectoryIterator(void)48 DirectoryIterator::~DirectoryIterator(void)
49 {
50     if (m_handle != -1)
51         _findclose(m_handle);
52 }
53 
hasItem(void) const54 bool DirectoryIterator::hasItem(void) const
55 {
56     return m_hasItem;
57 }
58 
getItem(void) const59 FilePath DirectoryIterator::getItem(void) const
60 {
61     DE_ASSERT(hasItem());
62     return FilePath::join(m_path, m_fileInfo.name);
63 }
64 
next(void)65 void DirectoryIterator::next(void)
66 {
67     m_hasItem = (_findnext32(m_handle, &m_fileInfo) == 0);
68     skipCurAndParent();
69 }
70 
skipCurAndParent(void)71 void DirectoryIterator::skipCurAndParent(void)
72 {
73     while (m_hasItem && (deStringEqual(m_fileInfo.name, "..") || deStringEqual(m_fileInfo.name, ".")))
74         m_hasItem = (_findnext32(m_handle, &m_fileInfo) == 0);
75 }
76 
77 #elif (DE_DIRITER == DE_DIRITER_POSIX)
78 
79 DirectoryIterator::DirectoryIterator(const FilePath &path)
80     : m_path(FilePath::normalize(path))
81     , m_handle(DE_NULL)
82     , m_curEntry(DE_NULL)
83 {
84     DE_CHECK_RUNTIME_ERR(m_path.exists());
85     DE_CHECK_RUNTIME_ERR(m_path.getType() == FilePath::TYPE_DIRECTORY);
86 
87     m_handle = opendir(m_path.getPath());
88     DE_CHECK_RUNTIME_ERR(m_handle);
89 
90     // Find first entry
91     next();
92 }
93 
94 DirectoryIterator::~DirectoryIterator(void)
95 {
96     closedir(m_handle);
97 }
98 
99 bool DirectoryIterator::hasItem(void) const
100 {
101     return (m_curEntry != DE_NULL);
102 }
103 
104 FilePath DirectoryIterator::getItem(void) const
105 {
106     DE_ASSERT(hasItem());
107     return FilePath::join(m_path, m_curEntry->d_name);
108 }
109 
110 void DirectoryIterator::next(void)
111 {
112     do
113     {
114         m_curEntry = readdir(m_handle);
115     } while (m_curEntry && (deStringEqual(m_curEntry->d_name, "..") || deStringEqual(m_curEntry->d_name, ".")));
116 }
117 
118 #endif
119 
120 } // namespace de
121