* Search a textfile file next to the given content. * @param type The type of the textfile to search for. * @param dir The subdirectory to search in. * @param filename The filename of the content to look for. * @return The path to the textfile, \c nullptr otherwise. */
| 937 | * @return The path to the textfile, \c nullptr otherwise. |
| 938 | */ |
| 939 | std::optional<std::string> GetTextfile(TextfileType type, Subdirectory dir, std::string_view filename) |
| 940 | { |
| 941 | static const std::string_view prefixes[] = { |
| 942 | "readme", |
| 943 | "changelog", |
| 944 | "license", |
| 945 | }; |
| 946 | static_assert(lengthof(prefixes) == TFT_CONTENT_END); |
| 947 | |
| 948 | /* Only the generic text file types allowed for this function */ |
| 949 | if (type >= TFT_CONTENT_END) return std::nullopt; |
| 950 | |
| 951 | std::string_view prefix = prefixes[type]; |
| 952 | |
| 953 | if (filename.empty()) return std::nullopt; |
| 954 | |
| 955 | auto slash = filename.find_last_of(PATHSEPCHAR); |
| 956 | if (slash == std::string::npos) return std::nullopt; |
| 957 | |
| 958 | std::string_view base_path = filename.substr(0, slash + 1); |
| 959 | |
| 960 | static const std::initializer_list<const std::string_view> extensions{ |
| 961 | "txt", |
| 962 | "md", |
| 963 | #if defined(WITH_ZLIB) |
| 964 | "txt.gz", |
| 965 | "md.gz", |
| 966 | #endif |
| 967 | #if defined(WITH_LIBLZMA) |
| 968 | "txt.xz", |
| 969 | "md.xz", |
| 970 | #endif |
| 971 | }; |
| 972 | |
| 973 | for (auto &extension : extensions) { |
| 974 | std::string file_path = fmt::format("{}{}_{}.{}", base_path, prefix, GetCurrentLanguageIsoCode(), extension); |
| 975 | if (FioCheckFileExists(file_path, dir)) return file_path; |
| 976 | |
| 977 | file_path = fmt::format("{}{}_{:.2s}.{}", base_path, prefix, GetCurrentLanguageIsoCode(), extension); |
| 978 | if (FioCheckFileExists(file_path, dir)) return file_path; |
| 979 | |
| 980 | file_path = fmt::format("{}{}.{}", base_path, prefix, extension); |
| 981 | if (FioCheckFileExists(file_path, dir)) return file_path; |
| 982 | } |
| 983 | return std::nullopt; |
| 984 | } |
no test coverage detected