aboutsummaryrefslogtreecommitdiffstats
path: root/src/base/songloading
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2010-01-15 05:10:30 +0100
committerAlexander Sulfrian <alexander@sulfrian.net>2013-01-05 17:17:44 +0100
commitf5d2afb357c7581536da91022986872cd41b68d3 (patch)
tree2422faa56d75827d265223248832df611974a45b /src/base/songloading
parent750505e136531c44d0c6530562509eacfb1e3e56 (diff)
downloadusdx-f5d2afb357c7581536da91022986872cd41b68d3.tar.gz
usdx-f5d2afb357c7581536da91022986872cd41b68d3.tar.xz
usdx-f5d2afb357c7581536da91022986872cd41b68d3.zip
added txt header loading with test and some preparations for loading notes
Diffstat (limited to 'src/base/songloading')
-rw-r--r--src/base/songloading/songloader.cpp4
-rw-r--r--src/base/songloading/songloader.hpp4
-rw-r--r--src/base/songloading/songloading_strategy.hpp3
-rw-r--r--src/base/songloading/songloading_strategy_txt.cpp137
-rw-r--r--src/base/songloading/songloading_strategy_txt.hpp27
-rw-r--r--src/base/songloading/songloading_strategy_xml.cpp7
-rw-r--r--src/base/songloading/songloading_strategy_xml.hpp3
7 files changed, 174 insertions, 11 deletions
diff --git a/src/base/songloading/songloader.cpp b/src/base/songloading/songloader.cpp
index c4d1054d..8bffa607 100644
--- a/src/base/songloading/songloader.cpp
+++ b/src/base/songloading/songloader.cpp
@@ -56,7 +56,7 @@ namespace usdx
return instance;
}
- Song *Songloader::load(std::string filename)
+ Song *Songloader::load_header(std::string filename)
{
std::string extension = "";
@@ -71,6 +71,6 @@ namespace usdx
throw "Unknown file format.";
}
- return it->second->loadSong(filename);
+ return it->second->load_header(filename);
}
};
diff --git a/src/base/songloading/songloader.hpp b/src/base/songloading/songloader.hpp
index 3c0b8e6f..8a5cbe03 100644
--- a/src/base/songloading/songloader.hpp
+++ b/src/base/songloading/songloader.hpp
@@ -49,11 +49,11 @@ namespace usdx
static Songloader *instance;
public:
- Songloader *get_instance(void);
+ static Songloader *get_instance(void);
virtual ~Songloader(void);
- Song *load(std::string filename);
+ Song *load_header(std::string filename);
};
};
diff --git a/src/base/songloading/songloading_strategy.hpp b/src/base/songloading/songloading_strategy.hpp
index 4f5edd0f..5cded02d 100644
--- a/src/base/songloading/songloading_strategy.hpp
+++ b/src/base/songloading/songloading_strategy.hpp
@@ -40,7 +40,8 @@ namespace usdx
public:
virtual ~SongloadingStrategy() {};
- virtual Song* loadSong(std::string filename) = 0;
+ virtual Song* load_song(Song* song) = 0;
+ virtual Song* load_header(const std::string& filename) = 0;
};
};
diff --git a/src/base/songloading/songloading_strategy_txt.cpp b/src/base/songloading/songloading_strategy_txt.cpp
index e8238f4a..31604191 100644
--- a/src/base/songloading/songloading_strategy_txt.cpp
+++ b/src/base/songloading/songloading_strategy_txt.cpp
@@ -24,10 +24,17 @@
* $Id$
*/
+#include <string>
+#include <algorithm>
#include "songloading_strategy_txt.hpp"
+#include "utils/file.hpp"
namespace usdx
{
+ log4cxx::LoggerPtr SongloadingStrategyTxt::log =
+ log4cxx::Logger::getLogger(
+ "usdx.base.songloading.SongloadingStrategyTxt");
+
SongloadingStrategyTxt::SongloadingStrategyTxt(void)
{
}
@@ -36,9 +43,133 @@ namespace usdx
{
}
- Song* SongloadingStrategyTxt::loadSong(std::string filename)
+ std::pair<std::string, std::string> SongloadingStrategyTxt::split_header_field(std::string &line)
+ {
+ std::size_t pos = line.find(':');
+
+ if (line[0] != '#' || pos == std::string::npos) {
+ LOG4CXX_DEBUG(log, "Tried to parse invalid header line: '" << line << "'");
+ throw "Invalid header!";
+ }
+
+ std::pair<std::string, std::string> result;
+
+ // copy the substring to : without # to result.first and
+ // transform to upper case
+ result.first.resize(pos - 1);
+ std::transform(line.begin() + 1, line.begin() + pos,
+ result.first.begin(), toupper);
+
+ // line is already ltrimmed
+ rtrim(result.first);
+
+ result.second = line.substr(pos + 1);
+
+ // line is already rtrimmed
+ ltrim(result.second);
+
+ LOG4CXX_DEBUG(log, "Found header: '" << result.first << "' with value '" << result.second << "'");
+
+ return result;
+ }
+
+ std::string& SongloadingStrategyTxt::ltrim(std::string& line)
+ {
+ std::size_t found = line.find_first_not_of(" \t\n\r");
+ if (found != std::string::npos && found >= 1) {
+ line.erase(0, found - 1);
+ }
+
+ return line;
+ }
+
+ std::string& SongloadingStrategyTxt::rtrim(std::string& line)
+ {
+ std::size_t found = line.find_last_not_of(" \t\n\r");
+ if (found != std::string::npos) {
+ line.erase(found + 1);
+ }
+
+ return line;
+ }
+
+ std::string& SongloadingStrategyTxt::trim(std::string& line)
{
- // TODO
- return NULL;
+ return ltrim(rtrim(line));
+ }
+
+ Song* SongloadingStrategyTxt::load_song(Song *song)
+ {
+ File file(song->get_filename());
+ std::string line;
+
+ while (file.stream().good()) {
+ file.stream() >> line;
+
+ // do not remove spaces at line ending, that are maybe
+ // spaces in lyrics
+ ltrim(line);
+
+ if (line[0] == '#') {
+ // ignore, header already read
+ }
+ else if (line[0] == 'E') {
+ // song end
+ break;
+ }
+ else if (line[0] == '-') {
+ // line break
+ }
+ else if (line[0] == 'B') {
+ // new bpm
+ }
+ else {
+ // normal line
+ }
+ }
+
+ // fill song
+
+ return song;
+ }
+
+ Song* SongloadingStrategyTxt::load_header(const std::string& filename)
+ {
+ File file(filename);
+ std::string line;
+ std::map<std::string, std::string> header_fields;
+
+ bool header = true, notes_found = false;
+ while (file.stream().good()) {
+ std::getline(file.stream(), line);
+
+ trim(line);
+ LOG4CXX_DEBUG(log, "Line: " << line);
+
+ if (header && line[0] == '#') {
+
+ // header
+ header_fields.insert(split_header_field(line));
+ }
+ else {
+ if (header) {
+ // end of header
+ header = false;
+ }
+
+ if (line[0] == ':' || line[0] == '*' || line[0] == 'F') {
+ notes_found = true;
+ break;
+ }
+ }
+ }
+
+ if (! notes_found) {
+ LOG4CXX_WARN(log, "Song: '" << filename << "' has no notes. Ignoring!");
+ throw "No notes.";
+ }
+
+ // fill song
+ return new Song(filename, header_fields);
}
};
diff --git a/src/base/songloading/songloading_strategy_txt.hpp b/src/base/songloading/songloading_strategy_txt.hpp
index 7a298e41..31a9815a 100644
--- a/src/base/songloading/songloading_strategy_txt.hpp
+++ b/src/base/songloading/songloading_strategy_txt.hpp
@@ -28,17 +28,42 @@
#define SONGLOADING_STRATEGY_TXT_HPP
#include <string>
+#include <log4cxx/logger.h>
#include "songloading_strategy.hpp"
namespace usdx
{
class SongloadingStrategyTxt : public SongloadingStrategy
{
+ private:
+ static log4cxx::LoggerPtr log;
+
+ /**
+ * Split the header field in name and value.
+ */
+ std::pair<std::string, std::string> split_header_field(std::string &line);
+
+ /**
+ * Removes whitespaces in front of the string.
+ */
+ std::string& ltrim(std::string& line);
+
+ /**
+ * Removes whitespaces in behind the string.
+ */
+ std::string& rtrim(std::string& line);
+
+ /**
+ * Removes whitespaces in front of the string and behind it.
+ */
+ std::string& trim(std::string& line);
+
public:
SongloadingStrategyTxt();
virtual ~SongloadingStrategyTxt();
- virtual Song* loadSong(std::string filename);
+ virtual Song* load_song(Song* song);
+ virtual Song* load_header(const std::string& filename);
};
};
diff --git a/src/base/songloading/songloading_strategy_xml.cpp b/src/base/songloading/songloading_strategy_xml.cpp
index 52d1f7cf..f7fea886 100644
--- a/src/base/songloading/songloading_strategy_xml.cpp
+++ b/src/base/songloading/songloading_strategy_xml.cpp
@@ -36,9 +36,14 @@ namespace usdx
{
}
- Song* SongloadingStrategyXml::loadSong(std::string filename)
+ Song* SongloadingStrategyXml::load_song(Song *song)
{
// TODO
+ return song;
+ }
+
+ Song* SongloadingStrategyXml::load_header(const std::string& filename)
+ {
return NULL;
}
};
diff --git a/src/base/songloading/songloading_strategy_xml.hpp b/src/base/songloading/songloading_strategy_xml.hpp
index b656fe9a..a01f9c11 100644
--- a/src/base/songloading/songloading_strategy_xml.hpp
+++ b/src/base/songloading/songloading_strategy_xml.hpp
@@ -37,7 +37,8 @@ namespace usdx
SongloadingStrategyXml(void);
virtual ~SongloadingStrategyXml(void);
- virtual Song* loadSong(std::string filename);
+ virtual Song* load_song(Song* song);
+ virtual Song* load_header(const std::string& filename);
};
};