-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.cpp
84 lines (75 loc) · 2.92 KB
/
read.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <filesystem>
#include <map>
#include <nowide/convert.hpp>
#include <nowide/fstream.hpp>
#include <nowide/iostream.hpp>
#include "read.h"
#include "common.h"
#include "error.h"
typedef std::map<std::string, std::string> NfoData;
NfoData readNfoFile(const std::filesystem::path &filePath) {
const std::string path { nowide::narrow(filePath.wstring()) }; // not quite right, but using `filePath` or `filePath.8string()` directly causes issues.
nowide::cout << "Reading file: " << path << std::endl;
nowide::ifstream file = nowide::ifstream ( path );
NfoData data;
if (file.is_open()) {
std::string key;
std::string value;
while (std::getline(file, key, '=')) {
#if FLSPO_VERBOSE
nowide::cout << "Key: " << key << std::endl;
#endif
// split string into key & value
std::getline(file, value);
#if FLSPO_VERBOSE
nowide::cout << "Value: " << value << std::endl;
#endif
// Store in NfoData
data[key] = value;
}
}
return data;
}
void walkDirectory(const std::filesystem::path &rootDirectory, PluginByVendorMap &pluginMap) {
for (const auto &entry: std::filesystem::recursive_directory_iterator(rootDirectory)) {
const auto path { entry.path() };
#if FLSPO_VERBOSE
nowide::cout << (nowide::narrow(path.wstring())) << std::endl;
#endif
if (entry.is_regular_file()
&& path.extension() == ".nfo"
&& path.filename() != "VerifiedIDs.nfo" // Some FL Studio thing, not a plugin.
&& !path.parent_path().u8string().ends_with(u8"New") // ignore the "New" dir, duplicates other dirs
) {
// Read file as map of values.
NfoData nfoData { readNfoFile(path) };
// Extract the plugin details.
const std::string vendor { nfoData["ps_file_vendorname_0"] };
if (!vendor.empty()) {
const auto rawPluginType { nfoData["ps_file_type_0"] };
const int pluginType { std::stoi(nfoData["ps_file_type_0"]) };
if (pluginType < PluginType::Effect || pluginType > PluginType::Generator) {
nowide::cerr << "Unrecognised plugin type: " << rawPluginType;
throw UnexpectedValueError();
}
PluginData pluginData {
nfoData["ps_name"],
entry.path(),
static_cast<PluginType>(pluginType)
};
#if FLSPO_VERBOSE
nowide::cout << pluginData << std::endl;
#endif
pluginMap.emplace(vendor, pluginData);
}
}
}
}
PluginByVendorMap walkDirectories(const std::filesystem::path &pluginDbPath) {
PluginByVendorMap pluginMap;
walkDirectory(pluginDbPath, pluginMap);
// for (const auto &entry : pluginMap) {
// std::cout << entry.first << ": " << entry.second << std::endl;
// }
return pluginMap;
}