1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "dir.h" 17 18 #if defined(_WIN32) && (_WIN32) 19 #include <windows.h> 20 #include <cstring> 21 #include <string> 22 struct DIR { 23 HANDLE handle; 24 WIN32_FIND_DATAA data; 25 dirent cur; 26 }; opendir(const char * path)27DIR* opendir(const char* path) 28 { 29 DIR* d = new DIR(); 30 std::string tmp = path; 31 tmp.append("\\"); 32 tmp.append("*.*"); 33 d->handle = FindFirstFileA(tmp.c_str(), &d->data); 34 if (d->handle == INVALID_HANDLE_VALUE) { 35 delete d; 36 return nullptr; 37 } 38 return d; 39 } readdir(DIR * d)40struct dirent* readdir(DIR* d) 41 { 42 if (d->data.cFileName[0] == 0) { 43 // end. 44 return nullptr; 45 } 46 strcpy_s(d->cur.d_name, d->data.cFileName); 47 if (d->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { 48 d->cur.d_type = DT_DIR; 49 } else { 50 d->cur.d_type = DT_REG; 51 } 52 if (FindNextFileA(d->handle, &d->data) == false) { 53 // clear; 54 d->data = {}; 55 } 56 return &d->cur; 57 } closedir(DIR * d)58void closedir(DIR* d) 59 { 60 FindClose(d->handle); 61 } 62 #else 63 #include <sys/stat.h> 64 #include <sys/types.h> 65 #include <dirent.h> 66 #include <string> 67 #endif 68