69 lines
1.6 KiB
C++
69 lines
1.6 KiB
C++
|
#include <unistd.h>
|
||
|
#include <pwd.h>
|
||
|
|
||
|
#include "../include/calibratetext.hpp"
|
||
|
#include "../include/exceptionhandler.hpp"
|
||
|
|
||
|
CalibrateText::CalibrateText(const std::string &path, const std::string &md5, const std::string &before, const std::string &after, const CalibrateFlag cf)
|
||
|
: Calibrate(path, md5), _before(before), _after(after), _cf(cf) {}
|
||
|
|
||
|
void CalibrateText::Execute() const
|
||
|
{
|
||
|
std::cout << "Calibrating " << _path << "\n";
|
||
|
|
||
|
std::string cmd = "sed -i -- 's/" + _before + "/" + _after + "/g' " + _path;
|
||
|
std::string ucs2_to_utf8;
|
||
|
std::string utf8_to_ucs2;
|
||
|
|
||
|
switch (_cf)
|
||
|
{
|
||
|
case UCS2:
|
||
|
cmd += "_utf8";
|
||
|
ucs2_to_utf8 = "iconv -f ucs-2 -t utf-8 -o " + _path + "_utf8" + " " + _path;
|
||
|
utf8_to_ucs2 = "iconv -f utf-8 -t ucs-2 -o " + _path + " " + _path + "_utf8";
|
||
|
|
||
|
/* Converting .txt file from UCS-2 to UTF-8 into a new .txt_utf8 file */
|
||
|
if (system(ucs2_to_utf8.c_str()))
|
||
|
{
|
||
|
throw ExceptionHandler(PROCESS_ERROR, _path);
|
||
|
}
|
||
|
break;
|
||
|
case NORMAL:
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (system(cmd.c_str()))
|
||
|
{
|
||
|
throw ExceptionHandler(PROCESS_ERROR, _path);
|
||
|
}
|
||
|
|
||
|
switch (_cf)
|
||
|
{
|
||
|
case UCS2:
|
||
|
/* Converting back from UTF-8 to UCS-2 in .txt*/
|
||
|
if (system(utf8_to_ucs2.c_str()))
|
||
|
{
|
||
|
throw ExceptionHandler(PROCESS_ERROR, _path);
|
||
|
}
|
||
|
|
||
|
|
||
|
break;
|
||
|
case NORMAL:
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/** NOT FUNCTIONAL
|
||
|
|
||
|
// Verifying MD5 checksum
|
||
|
md5sum(path[i], md5_local);
|
||
|
if ((strcmp(to_hex(md5_local).c_str(), path_md5[i]) != 0))
|
||
|
{
|
||
|
print_md5_sum(md5_local);
|
||
|
std::cerr << " != " << path_md5[i] << "\nFailed to calibrate preferences.empire_script.txt !" << "\n";
|
||
|
}*/
|
||
|
}
|