Created
March 23, 2015 14:10
-
-
Save jgrar/2d2ba7bafdd91894862a to your computer and use it in GitHub Desktop.
fml.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g++ -std=c++0x -lboost_unit_test_framework ./parseFilename.cpp -oparseFilename && ./parseFilename |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BOOST_TEST_DYN_LINK | |
#define BOOST_TEST_MODULE whatislife | |
#include <boost/test/unit_test.hpp> | |
#include <iostream> | |
std::string ParseFilename(std::string s, std::string mask) | |
{ | |
std::ostringstream result; | |
std::vector<std::string> separators; | |
std::vector< std::pair<char, std::string> > tags; | |
std::string file = s.substr(0, s.rfind(".")); | |
for (size_t i = mask.find("%"); i != std::string::npos; i = mask.find("%")) | |
{ | |
tags.push_back(std::make_pair(mask.at(i+1), "")); | |
mask = mask.substr(i+2); | |
i = mask.find("%"); | |
if (!mask.empty()) | |
separators.push_back(mask.substr(0, i)); | |
} | |
size_t i = 0; | |
for (auto it = separators.begin(); it != separators.end(); ++it, ++i) | |
{ | |
size_t j = file.find(*it); | |
tags.at(i).second = file.substr(0, j); | |
if (j+it->length() > file.length()) { | |
goto PARSE_FAILED; | |
} | |
file = file.substr(j+it->length()); | |
} | |
if (!file.empty()) | |
{ | |
if (i >= tags.size()) { | |
goto PARSE_FAILED; | |
} | |
tags.at(i).second = file; | |
} | |
if (0) // tss... | |
{ | |
PARSE_FAILED: | |
return "Error while parsing filename!\n"; | |
} | |
for (auto it = tags.begin(); it != tags.end(); ++it) | |
{ | |
for (std::string::iterator j = it->second.begin(); j != it->second.end(); ++j) | |
if (*j == '_') | |
*j = ' '; | |
result << "%" << it->first << ": " << it->second << "\n"; | |
} | |
return result.str(); | |
} | |
BOOST_AUTO_TEST_CASE (whatislife) { | |
BOOST_CHECK( | |
ParseFilename("01 - Album - Song.mp3", "%n - %a - %t") == | |
"%n: 01\n%a: Album\n%t: Song\n" | |
); | |
BOOST_CHECK( | |
ParseFilename("(01) [Album] Song.mp3", "(%n) [%a] %t") == | |
"%n: 01\n%a: Album\n%t: Song\n" | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment