Skip to content

Commit

Permalink
refactor(util): decouple move and copy files into class
Browse files Browse the repository at this point in the history
  • Loading branch information
thederickff committed Feb 19, 2018
1 parent 3c98b3b commit e83cd6f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 48 deletions.
19 changes: 8 additions & 11 deletions src/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

bool Util::FileExists(const char* fileName)
{
std::fstream file(fileName, std::ios::in);
std::fstream file(fileName, std::ios::binary | std::ios::in);
if (file.is_open()) {
file.close();
return true;
Expand All @@ -15,20 +15,17 @@ bool Util::FileExists(const char* fileName)

int Util::CopyBinary(const char* fileNameFrom, const char* fileNameTo)
{
if (fileExists(fileNameTo)) {
std::ifstream in(fileNameFrom, std::ios::binary);
std::ofstream out(fileNameTo, std::ios::binary);
out << in.rdbuf();
out.close();
in.close();
return 0;
}
return -1;
std::ifstream in(fileNameFrom, std::ios::binary);
std::ofstream out(fileNameTo, std::ios::binary);
out << in.rdbuf();
out.close();
in.close();
return 0;
}

void Util::MoveBinary(const char* fileNameFrom, const char* fileNameTo)
{
if (copyBinary(fileNameFrom, fileNameTo) != 0) {
if (CopyBinary(fileNameFrom, fileNameTo) != 0) {
std::cout << "Error copying file.\n";
} else {
if (remove(fileNameFrom) != 0)
Expand Down
39 changes: 2 additions & 37 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,10 @@
#include "Scanner.h"
#include "Cwsq.h"
#include "Mindtct.h"
#include "Util.h"

int debug = 0;

bool fileExists(const char* fileName)
{
std::fstream file(fileName, std::ios::in);
if (file.is_open()) {
file.close();
return true;
}
file.close();
return false;
}

int copyBinary(const char* fileNameFrom, const char* fileNameTo)
{
if (fileExists(fileNameTo)) {
std::ifstream in(fileNameFrom, std::ios::binary);
std::ofstream out(fileNameTo, std::ios::binary);
out << in.rdbuf();
out.close();
in.close();
return 0;
}
return -1;
}

void moveBinary(const char* fileNameFrom, const char* fileNameTo)
{
if (copyBinary(fileNameFrom, fileNameTo) != 0) {
std::cout << "Error copying file.\n";
} else {
if (remove(fileNameFrom) != 0)
std::cout << "Error deleting file.\n";
else if (debug > 0)
std::cout << "File succesfully deleted";
}
}

int main(int argc, char *argv[])
{
if (argc < 3) {
Expand All @@ -65,7 +30,7 @@ int main(int argc, char *argv[])
Mindtct mindtct(directory + name);
mindtct.Execute();

moveBinary((directory + name + bmp_ext).c_str(), (directory + std::string("bmp\\") + name + bmp_ext).c_str());
Util::MoveBinary((directory + name + bmp_ext).c_str(), (directory + std::string("bmp\\") + name + bmp_ext).c_str());

return 0;
}

0 comments on commit e83cd6f

Please sign in to comment.