Skip to content

Commit

Permalink
let's keep the codebase simple with sanitizeStr()
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Mar 11, 2024
1 parent 9b4d40f commit c2703b0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 30 deletions.
13 changes: 7 additions & 6 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ struct is_string {
};

// https://stackoverflow.com/questions/874134/find-out-if-string-ends-with-another-string-in-c#874160
bool hasEnding(std::string const& fullString, std::string const& ending);
bool hasStart(std::string const& fullString, std::string const& start);
void log_printf(int log, std::string fmt, ...);
std::string expandHome(std::string& str);
bool hasEnding(string const& fullString, string const& ending);
bool hasStart(string const& fullString, string const& start);
void log_printf(int log, string fmt, ...);
string expandHome(string& str);
void sanitizeStr(string& str);
std::vector<string> split(string text, char delim);


template <typename T>
T sanitize(T beg, T end) {
Expand All @@ -65,6 +68,4 @@ T sanitize(T beg, T end) {
return dest;
}

std::vector<std::string> split(std::string text, char delim);

#endif
1 change: 0 additions & 1 deletion src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ void Config::loadConfigFile(string filename) {
}

this->sudo = this->getConfigValue<string>("general.sudo", "sudo");
this->sudo.erase(sanitize(this->sudo.begin(), this->sudo.end()), this->sudo.end());
this->useGit = this->getConfigValue<bool>("general.useGit", true);
this->aurOnly = this->getConfigValue<bool>("general.aurOnly", false);
this->makepkgBin = this->getConfigValue<string>("bins.makepkgBin", "makepkg");
Expand Down
5 changes: 2 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ bool installPkg(string pkgName, TaurBackend *backend) {

if (url == "") {
string name = pkg.value().name;
string sudo = config.sudo;
sudo.erase(sanitize(sudo.begin(), sudo.end()), sudo.end());
name.erase(sanitize(name.begin(), name.end()), name.end());
sanitizeStr(config.sudo);
sanitizeStr(name);

return system((config.sudo + " pacman -S " + name).c_str()) == 0;
}
Expand Down
22 changes: 11 additions & 11 deletions src/taur.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Functions for TabAUR, These include printing stuff and others.
// main.cpp simply pieces each function together to make the program work.
#include "util.hpp"
#include <taur.hpp>
#include <config.hpp>
#include <optional>
Expand Down Expand Up @@ -78,7 +79,7 @@ bool TaurBackend::download_tar(string url, string out_path) {
// if this is in a directory, it will change to that directory first.
bool isNested = out_path.find("/") != -1;

out_path.erase(sanitize(out_path.begin(), out_path.end()), out_path.end());
sanitizeStr(out_path);
if (isNested)
return system(("cd \"" + out_path.substr(0, out_path.rfind("/")) + "\" && tar -xf \"" + out_path.substr(out_path.rfind("/") + 1) + "\"").c_str()) == 0;
else
Expand Down Expand Up @@ -179,19 +180,18 @@ string exec(string cmd) {
}

bool sanitizeAndRemove(string& input) {
string sudo = config.sudo;
sudo.erase(sanitize(sudo.begin(), sudo.end()), sudo.end());
input.erase(sanitize(input.begin(), input.end()), input.end());
sanitizeStr(config.sudo);
sanitizeStr(input);

return system((sudo + " pacman -R " + input).c_str()) == 0;
return system((config.sudo + " pacman -R " + input).c_str()) == 0;
}

bool TaurBackend::remove_pkg(string pkgName, bool searchForeignPackagesOnly) {
/*if (!pkg) {
std::cerr << "Failed to find your package " << pkgName << " in the TabAUR DB" << std::endl;
return false;
}*/
pkgName.erase(sanitize(pkgName.begin(), pkgName.end()), pkgName.end());
sanitizeStr(pkgName);

string packages_str;
if (searchForeignPackagesOnly)
Expand Down Expand Up @@ -245,8 +245,8 @@ bool TaurBackend::install_pkg(TaurPkg_t pkg, string extracted_path, bool useGit)
string makepkg_bin = this->config.makepkgBin;

// never forget to sanitize
extracted_path.erase(sanitize(extracted_path.begin(), extracted_path.end()), extracted_path.end());
makepkg_bin.erase(sanitize(makepkg_bin.begin(), makepkg_bin.end()), makepkg_bin.end());
sanitizeStr(extracted_path);
sanitizeStr(makepkg_bin);

if (pkg.depends.empty())
return system(("cd \"" + extracted_path + "\" && " + makepkg_bin + " -si").c_str()) == 0;
Expand Down Expand Up @@ -297,7 +297,7 @@ bool TaurBackend::install_pkg(TaurPkg_t pkg, string extracted_path, bool useGit)

bool TaurBackend::update_all_pkgs(path cacheDir, bool useGit) {
string sudo = config.sudo;
sudo.erase(sanitize(sudo.begin(), sudo.end()), sudo.end());
sanitizeStr(sudo);

// first thing first
bool pacmanUpgradeSuccess = system((sudo + " pacman -Syu").c_str()) == 0;
Expand Down Expand Up @@ -343,7 +343,7 @@ bool TaurBackend::update_all_pkgs(path cacheDir, bool useGit) {
log_printf(LOG_INFO, _("Downloading %s, This could take a bit.\n"), pkgs[pkgIndex].name.c_str());

string pkgFolder = cacheDir / onlinePkgs[i].name;
pkgFolder.erase(sanitize(pkgFolder.begin(), pkgFolder.end()), pkgFolder.end());
sanitizeStr(pkgFolder);

bool downloadSuccess = this->download_pkg(onlinePkgs[i].url, pkgFolder);

Expand Down Expand Up @@ -424,7 +424,7 @@ vector<string> TaurBackend::getPkgFromJson(rapidjson::Document& doc, bool useGit
}

vector<TaurPkg_t> TaurBackend::search_pac(string query) {
query.erase(sanitize(query.begin(), query.end()), query.end());
sanitizeStr(query);
string cmd = "pacman -Qn | grep \"" + query + "\"";
vector<string> pkgs_string = split(exec(cmd), '\n');

Expand Down
22 changes: 13 additions & 9 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
#include <util.hpp>

// https://stackoverflow.com/questions/874134/find-out-if-string-ends-with-another-string-in-c#874160
bool hasEnding(std::string const& fullString, std::string const& ending) {
bool hasEnding(string const& fullString, std::string const& ending) {
if (ending.length() > fullString.length())
return false;
return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
}

bool hasStart(std::string const& fullString, std::string const& start) {
bool hasStart(string const& fullString, std::string const& start) {
if (start.length() > fullString.length())
return false;
return (0 == fullString.compare(0, start.length(), start));
}

void log_printf(int log, std::string fmt, ...){
void log_printf(int log, string fmt, ...){
va_list args;
va_start(args, fmt);
switch(log){
Expand All @@ -30,10 +30,14 @@ void log_printf(int log, std::string fmt, ...){
std::cout << NOCOLOR;
}

std::string expandHome(std::string& str) {
std::string ret = str;
void sanitizeStr(string& str){
str.erase(sanitize(str.begin(), str.end()), str.end());
}

string expandHome(std::string& str) {
string ret = str;
size_t found = ret.find("~");
if (found != std::string::npos) {
if (found != string::npos) {
const char* homeDir = getenv("HOME");
if (homeDir != nullptr)
ret.replace(found, 1, homeDir);
Expand All @@ -45,9 +49,9 @@ std::string expandHome(std::string& str) {
return ret;
}

std::vector<std::string> split(std::string text, char delim) {
std::string line;
std::vector<std::string> vec;
std::vector<string> split(std::string text, char delim) {
string line;
std::vector<string> vec;
std::stringstream ss(text);
while(std::getline(ss, line, delim)) {
vec.push_back(line);
Expand Down

0 comments on commit c2703b0

Please sign in to comment.