| 1 | /* Copyright (C) 2024 by FontForge Authors */ |
| 2 | /* |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are met: |
| 5 | |
| 6 | * Redistributions of source code must retain the above copyright notice, this |
| 7 | * list of conditions and the following disclaimer. |
| 8 | |
| 9 | * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer in the documentation |
| 11 | * and/or other materials provided with the distribution. |
| 12 | |
| 13 | * The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | /* |
| 29 | * Cross-platform directory iteration using C++17 filesystem. |
| 30 | * Provides extern "C" wrappers for use from C code. |
| 31 | */ |
| 32 | |
| 33 | #include <fontforge-config.h> |
| 34 | #include "ffdir.h" |
| 35 | |
| 36 | #include <filesystem> |
| 37 | #include <string> |
| 38 | #include <cstring> |
| 39 | |
| 40 | namespace fs = std::filesystem; |
| 41 | |
| 42 | struct FF_Dir { |
| 43 | std::string path; |
| 44 | fs::directory_iterator iter; |
| 45 | fs::directory_iterator end; |
| 46 | FF_DirEntry current; |
| 47 | bool has_current; |
| 48 | |
| 49 | FF_Dir(const char* p) |
| 50 | : path(p), iter(fs::path(p)), end(), has_current(false) {} |
| 51 | |
| 52 | void rewind() { iter = fs::directory_iterator(fs::path(path)); } |
| 53 | }; |
| 54 | |
| 55 | extern "C" { |
| 56 | |
| 57 | FF_Dir* ff_opendir(const char* path) { |
| 58 | try { |
| 59 | if (!fs::is_directory(path)) { |
| 60 | return nullptr; |
| 61 | } |
| 62 | return new FF_Dir(path); |
| 63 | } catch (...) { |
| 64 | return nullptr; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | FF_DirEntry* ff_readdir(FF_Dir* dir) { |
| 69 | if (!dir) { |
| 70 | return nullptr; |
| 71 | } |
| 72 | try { |
| 73 | if (dir->iter == dir->end) { |
| 74 | return nullptr; |
| 75 | } |
| 76 | const auto& entry = *dir->iter; |
| 77 | std::string name = entry.path().filename().string(); |
| 78 | if (name.length() >= sizeof(dir->current.name)) { |
| 79 | name = name.substr(0, sizeof(dir->current.name) - 1); |
| 80 | } |
| 81 | std::strncpy(dir->current.name, name.c_str(), |
| 82 | sizeof(dir->current.name) - 1); |
| 83 | dir->current.name[sizeof(dir->current.name) - 1] = '\0'; |
| 84 | dir->has_current = true; |
| 85 | ++dir->iter; |
| 86 | return &dir->current; |
| 87 | } catch (...) { |
| 88 | return nullptr; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void ff_closedir(FF_Dir* dir) { delete dir; } |
| 93 | |
| 94 | void ff_rewinddir(FF_Dir* dir) { |
| 95 | if (dir) { |
| 96 | try { |
| 97 | dir->rewind(); |
| 98 | } catch (...) { |
| 99 | /* Ignore errors on rewind */ |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | } /* extern "C" */ |