• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "NativeFileSystem.h"
17 #include <locale>
18 #include <codecvt>
19 #include <windows.h>
20 #include "PreviewerEngineLog.h"
21 
22 namespace OHOS::Ide {
FindSubfolderByName(const std::string & parentFolderPath,const std::string & subfolderName)23 std::string NativeFileSystem::FindSubfolderByName(const std::string& parentFolderPath,
24     const std::string& subfolderName)
25 {
26     WIN32_FIND_DATAW datas;
27     std::wstring path = std::wstring(parentFolderPath.begin(), parentFolderPath.end());
28     std::wstring searchPath = path + L"\\*";
29     HANDLE handle = FindFirstFileW(searchPath.c_str(), &datas);
30     if (handle == INVALID_HANDLE_VALUE) {
31         ELOG("failed to open directory:%s.", parentFolderPath.c_str());
32         return "";
33     }
34     do {
35         if (datas.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
36             std::wstring name = datas.cFileName;
37             std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
38             std::string dir = converter.to_bytes(name);
39             if (dir.find(subfolderName) == 0) {
40                 FindClose(handle);
41                 std::string filePath = parentFolderPath + "\\" + dir;
42                 return filePath;
43             }
44         }
45     } while (FindNextFileW(handle, &datas) != 0);
46     FindClose(handle);
47     return "";
48 }
49 }