92 lines
1.8 KiB
C++
92 lines
1.8 KiB
C++
#include <unistd.h>
|
|
#include <pwd.h>
|
|
#include <string>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <algorithm>
|
|
|
|
std::string retrieve_path()
|
|
{
|
|
/* Retrieve home directory */
|
|
const char* char_homedir;
|
|
if ((char_homedir = getenv("HOME")) == NULL)
|
|
{
|
|
struct passwd *pw = getpwuid(getuid());
|
|
char_homedir = pw->pw_dir;
|
|
}
|
|
|
|
/* Convert char pointer to string */
|
|
const std::string s(char_homedir);
|
|
|
|
return s;
|
|
}
|
|
|
|
int replace_string(const std::string& file, const std::string& old_str, const std::string& new_str)
|
|
{
|
|
std::string cmd = "sed -i -- 's/" + old_str + "/" + new_str + "/g' " + file;
|
|
|
|
if (system(cmd.c_str()))
|
|
{
|
|
std::cerr << "Failed to calibrate: " << file;
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* 0 = true
|
|
* 1 = false
|
|
*/
|
|
int dir_not_exists(const std::string& dir)
|
|
{
|
|
struct stat sb;
|
|
|
|
if (stat(dir.c_str(), &sb) != 0)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
return !S_ISDIR(sb.st_mode);
|
|
}
|
|
|
|
int file_not_exists(const std::string& dir)
|
|
{
|
|
struct stat sb;
|
|
|
|
if (stat(dir.c_str(), &sb) != 0)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
return !S_ISREG(sb.st_mode);
|
|
}
|
|
|
|
int yn_query(const std::string& query, bool b_yes)
|
|
{
|
|
const std::string str_yn = b_yes ? " [Y/n] " : " [y/N] " ;
|
|
char yn;
|
|
|
|
std::cout << query << str_yn;
|
|
std::cin.get(yn);
|
|
|
|
do
|
|
{
|
|
switch (yn)
|
|
{
|
|
case 'Y':
|
|
case 'y': std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); return 1;
|
|
case 'N':
|
|
case 'n': std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); return 0;
|
|
case '\n': return b_yes ? 1 : 0;
|
|
default:
|
|
std::cin.clear();
|
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
|
std::cerr << "Error! \"" << yn << "\" is not a valid input!\n" << query << str_yn;
|
|
std::cin.get(yn);
|
|
}
|
|
} while (1);
|
|
}
|