From c58dfab9903ea7228b814f6f76d2c2518b3ea0a6 Mon Sep 17 00:00:00 2001 From: zvon Date: Thu, 3 Jan 2019 20:16:28 +0100 Subject: [PATCH] Make string short, so regex won't segfault with libstdc++, replace simple regex with c++ functions --- Makefile | 8 +++----- tv_rename.cpp | 30 ++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index fcc72c8..7fce707 100644 --- a/Makefile +++ b/Makefile @@ -4,14 +4,12 @@ CFLAGS ?= -O2 -g -Wall -Wextra -std=c++11 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 - $(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 - $(CXX) $(CFLAGS) -stdlib=libc++ -c filesystem.cpp + $(CXX) $(CFLAGS) -c filesystem.cpp functions.o: functions.cpp - $(CXX) $(CFLAGS) -stdlib=libc++ -c functions.cpp + $(CXX) $(CFLAGS) -c functions.cpp diff --git a/tv_rename.cpp b/tv_rename.cpp index d294dc3..a1c018f 100644 --- a/tv_rename.cpp +++ b/tv_rename.cpp @@ -38,7 +38,10 @@ int main(int argc, char** argv) { show = argv[x+1]; 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++; } else if ( !(strcmp("-c", argv[x]) && strcmp("--correct-path", argv[x])) ) { change_dir = false; @@ -91,7 +94,9 @@ int main(int argc, char** argv) { } 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::string 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); //get source code of season's page auto season_code = c.execute(url); - //remove newlines cause regex can't multiline - season_code = std::regex_replace(season_code, std::regex("[\r\n]"), ""); - //first 900 chars or so are useless to us, no need to regex through them + //remove newlines + 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 season_code = season_code.substr(900); //get only the episode names - std::smatch season_match; - if( std::regex_search(season_code, season_match, std::regex(".*
")) ) { - season_code = season_match[0]; + auto pos = season_code.find("\"translations\""); + if( pos != std::string::npos ) { + 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 { return; } std::regex title(".*\\s*(.*?)\\s*?.*"); std::regex episode_link(".*?"); - std::regex language_reg(""); std::smatch ep_match; std::vector episodes; //get episode names in all languages @@ -177,7 +186,8 @@ void singleSeason( const std::string &path, const std::string &show, int season, } num -= 1; 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 ) { name = std::regex_replace(name, std::regex("[\\?\"\\\\|\\*]"), ""); name = std::regex_replace(name, std::regex("<"), "is less than");