* @brief Parse a filter file, and add it to array if valid. * * @param [in] szFilePath Path (w/ filename) to file to load. * @param [out] error Error-code if loading failed (returned `nullptr`). * @return Pointer to new filter, or `nullptr` if error (check error code too). */
| 168 | * @return Pointer to new filter, or `nullptr` if error (check error code too). |
| 169 | */ |
| 170 | FileFilter * FileFilterMgr::LoadFilterFile(const String& szFilepath, int & error) |
| 171 | { |
| 172 | UniMemFile file; |
| 173 | if (!file.OpenReadOnly(szFilepath)) |
| 174 | { |
| 175 | error = FILTER_ERROR_FILEACCESS; |
| 176 | return nullptr; |
| 177 | } |
| 178 | |
| 179 | file.ReadBom(); // in case it is a Unicode file, let UniMemFile handle BOM |
| 180 | if (!file.IsUnicode() && !ucr::CheckForInvalidUtf8( |
| 181 | reinterpret_cast<const char*>(file.GetBase()), static_cast<size_t>(file.GetFileSize()))) |
| 182 | file.SetUnicoding(ucr::UTF8); |
| 183 | |
| 184 | String fileName; |
| 185 | paths::SplitFilename(szFilepath, nullptr, &fileName, nullptr); |
| 186 | FileFilter *pfilter = new FileFilter; |
| 187 | pfilter->fullpath = szFilepath; |
| 188 | pfilter->name = std::move(fileName); // Filename is the default name |
| 189 | |
| 190 | String sLine; |
| 191 | bool lossy = false; |
| 192 | bool bLinesLeft = true; |
| 193 | int lineNumber = 0; |
| 194 | do |
| 195 | { |
| 196 | // Returns false when last line is read |
| 197 | String tmpLine; |
| 198 | bLinesLeft = file.ReadString(tmpLine, &lossy); |
| 199 | sLine = std::move(tmpLine); |
| 200 | sLine = strutils::trim_ws(sLine); |
| 201 | |
| 202 | if (0 == sLine.compare(0, 5, _T("name:"), 5)) |
| 203 | { |
| 204 | // specifies display name |
| 205 | String str = sLine.substr(5); |
| 206 | str = strutils::trim_ws_begin(str); |
| 207 | if (!str.empty()) |
| 208 | pfilter->name = std::move(str); |
| 209 | } |
| 210 | else if (0 == sLine.compare(0, 5, _T("desc:"), 5)) |
| 211 | { |
| 212 | // specifies display name |
| 213 | String str = sLine.substr(5); |
| 214 | str = strutils::trim_ws_begin(str); |
| 215 | if (!str.empty()) |
| 216 | pfilter->description = std::move(str); |
| 217 | } |
| 218 | else if (0 == sLine.compare(0, 4, _T("def:"), 4)) |
| 219 | { |
| 220 | // specifies default |
| 221 | String str = sLine.substr(4); |
| 222 | str = strutils::trim_ws_begin(str); |
| 223 | if (str == _T("0") || str == _T("no") || str == _T("exclude")) |
| 224 | pfilter->default_include = false; |
| 225 | else if (str == _T("1") || str == _T("yes") || str == _T("include")) |
| 226 | pfilter->default_include = true; |
| 227 | } |
nothing calls this directly
no test coverage detected