Moved the parsing functions to SfzHelpers
This commit is contained in:
parent
3c9f84adb3
commit
c16936e225
5 changed files with 101 additions and 97 deletions
|
|
@ -27,7 +27,7 @@ include (SfizzSIMDSourceFilesCheck)
|
|||
|
||||
# Parser core library
|
||||
add_library (sfizz_parser STATIC)
|
||||
target_sources (sfizz_parser PRIVATE sfizz/Parser.cpp sfizz/Opcode.cpp)
|
||||
target_sources (sfizz_parser PRIVATE sfizz/Parser.cpp sfizz/Opcode.cpp sfizz/SfzHelpers.cpp)
|
||||
target_include_directories (sfizz_parser PUBLIC sfizz)
|
||||
target_include_directories (sfizz_parser PUBLIC external)
|
||||
target_include_directories (sfizz_parser PRIVATE ${re2_SOURCE_DIR})
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
#include "Parser.h"
|
||||
#include "Config.h"
|
||||
#include "StringViewHelpers.h"
|
||||
#include "SfzHelpers.h"
|
||||
#include "absl/strings/str_join.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include <algorithm>
|
||||
|
|
@ -36,55 +37,6 @@ void removeCommentOnLine(absl::string_view& line)
|
|||
line.remove_suffix(line.size() - position);
|
||||
}
|
||||
|
||||
bool findHeader(absl::string_view& source, absl::string_view& header, absl::string_view& members)
|
||||
{
|
||||
auto openHeader = source.find("<");
|
||||
if (openHeader == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
auto closeHeader = source.find(">", openHeader);
|
||||
if (openHeader == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
auto nextHeader = source.find("<", closeHeader);
|
||||
header = source.substr(openHeader + 1, closeHeader - openHeader - 1);
|
||||
if (nextHeader == absl::string_view::npos) {
|
||||
members = trim(source.substr(closeHeader + 1));
|
||||
source.remove_prefix(source.length());
|
||||
} else {
|
||||
members = trim(source.substr(closeHeader + 1, nextHeader - closeHeader - 1));
|
||||
source.remove_prefix(nextHeader);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool findOpcode(absl::string_view& source, absl::string_view& opcode, absl::string_view& value)
|
||||
{
|
||||
auto opcodeEnd = source.find("=");
|
||||
if (opcodeEnd == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto valueStart = opcodeEnd + 1;
|
||||
const auto nextOpcodeEnd = source.find("=", valueStart);
|
||||
|
||||
if (nextOpcodeEnd == absl::string_view::npos) {
|
||||
opcode = source.substr(0, opcodeEnd);
|
||||
value = source.substr(valueStart);
|
||||
source.remove_prefix(source.length());
|
||||
return true;
|
||||
}
|
||||
|
||||
auto valueEnd = nextOpcodeEnd;
|
||||
while (source[valueEnd] != ' ' && valueEnd != valueStart)
|
||||
valueEnd--;
|
||||
|
||||
opcode = source.substr(0, opcodeEnd);
|
||||
value = source.substr(valueStart, valueEnd - valueStart);
|
||||
source.remove_prefix(valueEnd);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool sfz::Parser::loadSfzFile(const fs::path& file)
|
||||
{
|
||||
const auto sfzFile = file.is_absolute() ? file : originalDirectory / file;
|
||||
|
|
@ -117,50 +69,6 @@ bool sfz::Parser::loadSfzFile(const fs::path& file)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool findDefine(absl::string_view line, absl::string_view& variable, absl::string_view& value)
|
||||
{
|
||||
const auto defPosition = line.find("#define");
|
||||
if (defPosition == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto variableStart = line.find("$", 7);
|
||||
if (variableStart == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto variableEnd = line.find_first_of(" \r\t\n\f\v", variableStart);
|
||||
if (variableEnd == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto valueStart = line.find_first_not_of(" \r\t\n\f\v", variableEnd);
|
||||
if (valueStart == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto valueEnd = line.find_first_of(" \r\t\n\f\v", valueStart);
|
||||
variable = line.substr(variableStart, variableEnd - variableStart);
|
||||
value = valueEnd != absl::string_view::npos
|
||||
? line.substr(valueStart, valueEnd - valueStart)
|
||||
: line.substr(valueStart);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool findInclude(absl::string_view line, std::string& path)
|
||||
{
|
||||
const auto defPosition = line.find("#include");
|
||||
if (defPosition == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto pathStart = line.find("\"", 8);
|
||||
if (pathStart == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto pathEnd = line.find("\"", pathStart + 1);
|
||||
if (pathEnd == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
path = std::string(line.substr(pathStart + 1, pathEnd - pathStart - 1));
|
||||
return true;
|
||||
}
|
||||
|
||||
void sfz::Parser::readSfzFile(const fs::path& fileName, std::vector<std::string>& lines) noexcept
|
||||
{
|
||||
std::ifstream fileStream(fileName.c_str());
|
||||
|
|
|
|||
|
|
@ -169,3 +169,97 @@ absl::optional<uint8_t> sfz::readNoteValue(const absl::string_view& value)
|
|||
default: return {};
|
||||
}
|
||||
}
|
||||
|
||||
bool sfz::findHeader(absl::string_view& source, absl::string_view& header, absl::string_view& members)
|
||||
{
|
||||
auto openHeader = source.find("<");
|
||||
if (openHeader == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
auto closeHeader = source.find(">", openHeader);
|
||||
if (openHeader == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
auto nextHeader = source.find("<", closeHeader);
|
||||
header = source.substr(openHeader + 1, closeHeader - openHeader - 1);
|
||||
if (nextHeader == absl::string_view::npos) {
|
||||
members = trim(source.substr(closeHeader + 1));
|
||||
source.remove_prefix(source.length());
|
||||
} else {
|
||||
members = trim(source.substr(closeHeader + 1, nextHeader - closeHeader - 1));
|
||||
source.remove_prefix(nextHeader);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool sfz::findOpcode(absl::string_view& source, absl::string_view& opcode, absl::string_view& value)
|
||||
{
|
||||
auto opcodeEnd = source.find("=");
|
||||
if (opcodeEnd == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto valueStart = opcodeEnd + 1;
|
||||
const auto nextOpcodeEnd = source.find("=", valueStart);
|
||||
|
||||
if (nextOpcodeEnd == absl::string_view::npos) {
|
||||
opcode = source.substr(0, opcodeEnd);
|
||||
value = source.substr(valueStart);
|
||||
source.remove_prefix(source.length());
|
||||
return true;
|
||||
}
|
||||
|
||||
auto valueEnd = nextOpcodeEnd;
|
||||
while (source[valueEnd] != ' ' && valueEnd != valueStart)
|
||||
valueEnd--;
|
||||
|
||||
opcode = source.substr(0, opcodeEnd);
|
||||
value = source.substr(valueStart, valueEnd - valueStart);
|
||||
source.remove_prefix(valueEnd);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool sfz::findDefine(absl::string_view line, absl::string_view& variable, absl::string_view& value)
|
||||
{
|
||||
const auto defPosition = line.find("#define");
|
||||
if (defPosition == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto variableStart = line.find("$", 7);
|
||||
if (variableStart == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto variableEnd = line.find_first_of(" \r\t\n\f\v", variableStart);
|
||||
if (variableEnd == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto valueStart = line.find_first_not_of(" \r\t\n\f\v", variableEnd);
|
||||
if (valueStart == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto valueEnd = line.find_first_of(" \r\t\n\f\v", valueStart);
|
||||
variable = line.substr(variableStart, variableEnd - variableStart);
|
||||
value = valueEnd != absl::string_view::npos
|
||||
? line.substr(valueStart, valueEnd - valueStart)
|
||||
: line.substr(valueStart);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool sfz::findInclude(absl::string_view line, std::string& path)
|
||||
{
|
||||
const auto defPosition = line.find("#include");
|
||||
if (defPosition == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto pathStart = line.find("\"", 8);
|
||||
if (pathStart == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
const auto pathEnd = line.find("\"", pathStart + 1);
|
||||
if (pathEnd == absl::string_view::npos)
|
||||
return false;
|
||||
|
||||
path = std::string(line.substr(pathStart + 1, pathEnd - pathStart - 1));
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -126,5 +126,9 @@ inline float ccSwitchedValue(const SfzCCArray& ccValues, const absl::optional<CC
|
|||
*/
|
||||
absl::optional<uint8_t> readNoteValue(const absl::string_view& value);
|
||||
|
||||
bool findHeader(absl::string_view& source, absl::string_view& header, absl::string_view& members);
|
||||
bool findOpcode(absl::string_view& source, absl::string_view& opcode, absl::string_view& value);
|
||||
bool findDefine(absl::string_view line, absl::string_view& variable, absl::string_view& value);
|
||||
bool findInclude(absl::string_view line, std::string& path);
|
||||
} // namespace sfz
|
||||
|
||||
|
|
|
|||
|
|
@ -76,12 +76,10 @@ constexpr uint64_t Fnv1aPrime = 0x01000193;
|
|||
* @param h the hashing seed to use
|
||||
* @return uint64_t
|
||||
*/
|
||||
inline constexpr uint64_t hash(absl::string_view s, uint64_t h = Fnv1aBasis)
|
||||
constexpr uint64_t hash(absl::string_view s, uint64_t h = Fnv1aBasis)
|
||||
{
|
||||
if (s.length() > 0)
|
||||
return hash( { s.data() + 1, s.length() - 1 }, (h ^ s.front()) * Fnv1aPrime );
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue