Make string short, so regex won't segfault with libstdc++, replace simple regex with c++ functions

This commit is contained in:
zvon 2019-01-03 20:16:28 +01:00
parent 49a490061e
commit c58dfab990
2 changed files with 23 additions and 15 deletions

View File

@ -4,14 +4,12 @@ CFLAGS ?= -O2 -g -Wall -Wextra -std=c++11
default: tv_rename default: tv_rename
# using libc++ because libstdc++ has a bug in regex that causes seg fault with long lines
tv_rename: functions.o filesystem.o tv_rename.cpp tv_rename: functions.o filesystem.o tv_rename.cpp
$(CXX) $(CFLAGS) -stdlib=libc++ -o tv_rename tv_rename.cpp functions.o filesystem.o -lcurl $(CXX) $(CFLAGS) -o tv_rename tv_rename.cpp functions.o filesystem.o -lcurl
filesystem.o: filesystem.cpp filesystem.o: filesystem.cpp
$(CXX) $(CFLAGS) -stdlib=libc++ -c filesystem.cpp $(CXX) $(CFLAGS) -c filesystem.cpp
functions.o: functions.cpp functions.o: functions.cpp
$(CXX) $(CFLAGS) -stdlib=libc++ -c functions.cpp $(CXX) $(CFLAGS) -c functions.cpp

View File

@ -38,7 +38,10 @@ int main(int argc, char** argv) {
show = argv[x+1]; show = argv[x+1];
x++; x++;
} else if ( !(strcmp("-n", argv[x]) && strcmp("--season", argv[x])) ) { } else if ( !(strcmp("-n", argv[x]) && strcmp("--season", argv[x])) ) {
seasons = std::regex_replace(argv[x+1], std::regex("[^[0-9 ]]*"), ""); size_t pos{0};
while((argv[x+1][pos] < '0' || argv[x+1][pos] > '9') && argv[x+1][pos] != ' ')
pos++;
seasons = std::string(&argv[x+1][pos]);
x++; x++;
} else if ( !(strcmp("-c", argv[x]) && strcmp("--correct-path", argv[x])) ) { } else if ( !(strcmp("-c", argv[x]) && strcmp("--correct-path", argv[x])) ) {
change_dir = false; change_dir = false;
@ -91,7 +94,9 @@ int main(int argc, char** argv) {
} }
if( show.empty() ) { if( show.empty() ) {
show = std::regex_replace(FSLib::canonical(path).c_str(), std::regex(".*/"), ""); auto pos = show.find_last_of('/');
if( pos != std::string::npos )
show = show.substr(++pos);
std::cout << "Is this the right show name? " << show << std::endl; std::cout << "Is this the right show name? " << show << std::endl;
std::string response; std::string response;
std::cin >> response; std::cin >> response;
@ -126,20 +131,24 @@ void singleSeason( const std::string &path, const std::string &show, int season,
url += std::to_string(season); url += std::to_string(season);
//get source code of season's page //get source code of season's page
auto season_code = c.execute(url); auto season_code = c.execute(url);
//remove newlines cause regex can't multiline //remove newlines
season_code = std::regex_replace(season_code, std::regex("[\r\n]"), ""); season_code.erase(std::remove_if(season_code.begin(), season_code.end(), [](char x) {return x == '\r' || x == '\n';}), season_code.end());
//first 900 chars or so are useless to us, no need to regex through them //first 900 chars or so are useless to us
season_code = season_code.substr(900); season_code = season_code.substr(900);
//get only the episode names //get only the episode names
std::smatch season_match; auto pos = season_code.find("\"translations\"");
if( std::regex_search(season_code, season_match, std::regex("<table class=.*?id=\"translations\">.*</table>")) ) { if( pos != std::string::npos ) {
season_code = season_match[0]; season_code = season_code.substr(pos);
pos = season_code.find("table");
if( pos != std::string::npos )
season_code = season_code.substr(0,pos);
else
return;
} else { } else {
return; return;
} }
std::regex title(".*<span.*?language=\"" + language + "\".*?>\\s*(.*?)\\s*?</span>.*"); std::regex title(".*<span.*?language=\"" + language + "\".*?>\\s*(.*?)\\s*?</span>.*");
std::regex episode_link("<td>.*?</td>"); std::regex episode_link("<td>.*?</td>");
std::regex language_reg("<span.*?</span>");
std::smatch ep_match; std::smatch ep_match;
std::vector<std::string> episodes; std::vector<std::string> episodes;
//get episode names in all languages //get episode names in all languages
@ -177,7 +186,8 @@ void singleSeason( const std::string &path, const std::string &show, int season,
} }
num -= 1; num -= 1;
if( num < episodes.size() ) { if( num < episodes.size() ) {
name = std::regex_replace(name, std::regex("(.*)\\.(.*)"), "$1 - " + episodes[num] + ".$2"); auto pos = name.find_last_of('.');
name.insert(pos, " - " + episodes[num]);
if( !linux ) { if( !linux ) {
name = std::regex_replace(name, std::regex("[\\?\"\\\\|\\*]"), ""); name = std::regex_replace(name, std::regex("[\\?\"\\\\|\\*]"), "");
name = std::regex_replace(name, std::regex("<"), "is less than"); name = std::regex_replace(name, std::regex("<"), "is less than");