From 5ff88806a954574522ed2fa1617fe44d2eab93e6 Mon Sep 17 00:00:00 2001 From: Ian Karlsson Date: Sat, 2 Jan 2021 02:09:18 +0100 Subject: [PATCH] Convert tabs to spaces internally before parsing MML, fix #3 --- src/song_manager.cpp | 28 +++++++++++++++++++++++++++- src/song_manager.h | 1 + 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/song_manager.cpp b/src/song_manager.cpp index 03eb540..9c83e67 100644 --- a/src/song_manager.cpp +++ b/src/song_manager.cpp @@ -412,7 +412,7 @@ void Song_Manager::compile_job(std::unique_lock& lock, std::string b std::stringstream stream(buffer); for(; std::getline(stream, str);) { - input.read_line(str, line); + input.read_line(tabs_to_spaces(str), line); temp_lines.get()->insert({line, input.get_track_map()}); line++; } @@ -450,3 +450,29 @@ void Song_Manager::compile_job(std::unique_lock& lock, std::string b error_reference = ref; } +//! Convert all tabs to spaces in a string. +/*! + * Currently tabstop is hardcoded to 4 to match the editor. + */ +std::string Song_Manager::tabs_to_spaces(const std::string& str) const +{ + const unsigned int tabstop = 4; + std::string out = ""; + for(char i : str) + { + if(i == '\t') + { + do + { + out.push_back(' '); + } + while(out.size() % tabstop != 0); + } + else + { + out.push_back(i); + } + } + return out; +} + diff --git a/src/song_manager.h b/src/song_manager.h index 92620b7..e2e02f2 100644 --- a/src/song_manager.h +++ b/src/song_manager.h @@ -74,6 +74,7 @@ class Song_Manager private: void worker(); void compile_job(std::unique_lock& lock, std::string buffer, std::string filename); + std::string tabs_to_spaces(const std::string& str) const; // song status const static int max_channels;