From 07ac699856b490c66e8052121f87ec5fc0a034f1 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 15 Jan 2010 19:25:55 +0100 Subject: added parsing of song lyric lines --- src/base/songloading/songloader.cpp | 18 ++++++ src/base/songloading/songloader.hpp | 3 +- src/base/songloading/songloading_strategy_txt.cpp | 74 ++++++++++++++++++++--- test/base/songloading.cpp | 2 +- 4 files changed, 86 insertions(+), 11 deletions(-) diff --git a/src/base/songloading/songloader.cpp b/src/base/songloading/songloader.cpp index 8bffa607..1b900bd4 100644 --- a/src/base/songloading/songloader.cpp +++ b/src/base/songloading/songloader.cpp @@ -73,4 +73,22 @@ namespace usdx return it->second->load_header(filename); } + + Song* Songloader::load_song(Song* song) + { + std::string extension = ""; + + size_t found = song->get_filename().rfind('.'); + if (found != std::string::npos) { + extension = song->get_filename().substr(found); + } + + std::map::iterator it = strategies.find(extension); + if (it == strategies.end()) { + LOG4CXX_WARN(log, "No SongloadingStrategy found for file extension: '" << extension << "'"); + throw "Unknown file format."; + } + + return it->second->load_song(song); + } }; diff --git a/src/base/songloading/songloader.hpp b/src/base/songloading/songloader.hpp index 8a5cbe03..41f7e034 100644 --- a/src/base/songloading/songloader.hpp +++ b/src/base/songloading/songloader.hpp @@ -53,7 +53,8 @@ namespace usdx virtual ~Songloader(void); - Song *load_header(std::string filename); + Song* load_header(std::string filename); + Song* load_song(Song* song); }; }; diff --git a/src/base/songloading/songloading_strategy_txt.cpp b/src/base/songloading/songloading_strategy_txt.cpp index 841e6253..6b9a03b7 100644 --- a/src/base/songloading/songloading_strategy_txt.cpp +++ b/src/base/songloading/songloading_strategy_txt.cpp @@ -25,9 +25,11 @@ */ #include +#include #include #include "songloading_strategy_txt.hpp" #include "utils/file.hpp" +#include "utils/locale_independent_float.hpp" namespace usdx { @@ -100,31 +102,81 @@ namespace usdx Song* SongloadingStrategyTxt::load_song(Song *song) { + LOG4CXX_DEBUG(log, "Starting loading song from file: " << song->get_filename()); + File file(song->get_filename()); std::string line; + char type; + + int line_number = 0;; while (file.stream().good()) { - file.stream() >> line; + std::getline(file.stream(), line); + ++line_number; - // do not remove spaces at line ending, that are maybe - // spaces in lyrics - ltrim(line); + std::istringstream linestream(line); + linestream >> std::skipws >> type; - if (line[0] == '#') { + if (type == '#') { // ignore, header already read } - else if (line[0] == 'E') { + else if (type == 'E') { // song end + if (file.stream().eof()) { + LOG4CXX_WARN(log, "End marker found in line " << line_number << + " before end of file: '" << song->get_filename() << "'."); + } + break; } - else if (line[0] == '-') { + else if (type == '-') { // line break + int line_out, line_in = -1; + + linestream >> line_out; + if (linestream.good()) { + linestream >> line_in; + LOG4CXX_DEBUG(log, "Found newline in line " << + line_number << " with out of last line with " + << line_out << " and in of next line " << line_in); + } + else { + LOG4CXX_DEBUG(log, "Found newline in line " << + line_number << " with out of last line with " + << line_out); + } + +// song.new_line(line_out, line_in); } - else if (line[0] == 'B') { + else if (type == 'B') { // new bpm + int beat; + LocaleIndependentFloat new_bpm; + + linestream >> beat >> new_bpm; + LOG4CXX_DEBUG(log, "Found new bpm in line " << + line_number << " starting at beat: " << + beat << " and new bpm of " << new_bpm.get_value()); +// song.new_bpm(new_beat, new_bpm); } - else { + else if (type == ':' || type == 'F' || type == '*') { // normal line + int beat, length, height; + std::string lyric; + + linestream >> beat >> length >> height >> std::noskipws; + linestream.ignore(); + getline(linestream, lyric); + + LOG4CXX_DEBUG(log, "Found lyric: '" << lyric << "' at line: " << line_number << + " at beat: " << beat << " with length: " << length << + " at height: " << height); +// song.new_note(beat, length, height, lyric); + } + else { + LOG4CXX_WARN(log, "Unknown line in song: '" << line << + "' in file: " << song->get_filename() << + " at line " << line_number); } } @@ -133,6 +185,10 @@ namespace usdx return song; } +/* void SongloadingStrategyTxt::parse_line(const std::string& line, const int line_num) + { + } +*/ Song* SongloadingStrategyTxt::load_header(const std::string& filename) { File file(filename); diff --git a/test/base/songloading.cpp b/test/base/songloading.cpp index 3119d0c4..1dbd3e27 100644 --- a/test/base/songloading.cpp +++ b/test/base/songloading.cpp @@ -49,7 +49,7 @@ namespace usdx { Song *song = Songloader::get_instance()->load_header("testdata/testsong_correct.txt"); - CPPUNIT_ASSERT( "Test Ärtist" == song->get_artist() ); + CPPUNIT_ASSERT( "Test Ärtist" == song->get_artist() ); CPPUNIT_ASSERT( "Test Title" == song->get_title() ); CPPUNIT_ASSERT( "Test.mp3" == song->get_mp3() ); // TODO bpm array -- cgit v1.2.3