Add initial bits for Qt6 support
[carla.git] / source / modules / ysfx / sources / ysfx_utils_fts.cpp
blob2226a3548ce5daf0da90265f57ea66384545170c
1 // Copyright 2021 Jean Pierre Cimalando
2 //
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.
15 // SPDX-License-Identifier: Apache-2.0
18 #if !defined(_WIN32)
20 // fts lacks large file support in glibc < 2.23
21 #if defined(YSFX_FTS_LACKS_LFS_SUPPORT)
22 # undef _FILE_OFFSET_BITS
23 #endif
25 #include "ysfx_utils.hpp"
26 #ifndef __EMSCRIPTEN__
27 #include <fts.h>
28 #endif
29 #include <string>
30 #include <cstring>
32 namespace ysfx {
34 void visit_directories(const char *rootpath, bool (*visit)(const std::string &, void *), void *data)
36 #ifndef __EMSCRIPTEN__
37 char *argv[] = {(char *)rootpath, nullptr};
39 #if !defined(__FreeBSD__)
40 using compar_arg_t = const FTSENT **;
41 #else
42 using compar_arg_t = const FTSENT *const *;
43 #endif
45 auto compar = [](compar_arg_t a, compar_arg_t b) -> int {
46 return strcmp((*a)->fts_name, (*b)->fts_name);
49 FTS *fts = fts_open(argv, FTS_NOCHDIR|FTS_PHYSICAL, +compar);
50 if (!fts)
51 return;
52 auto fts_cleanup = defer([fts]() { fts_close(fts); });
54 std::string pathbuf;
55 pathbuf.reserve(1024);
57 while (FTSENT *ent = fts_read(fts)) {
58 if (ent->fts_info == FTS_D) {
59 pathbuf.assign(ent->fts_path);
60 pathbuf.push_back('/');
61 if (!visit(pathbuf, data))
62 return;
65 #endif
68 } // namespace ysfx
70 #endif // !defined(_WIN32)