|
| 1 | +#include "stdafx.h" |
| 2 | +#include "Copy.h" |
| 3 | +////////////////////////////////////////////////////////////////////////// |
| 4 | +CCopy::CCopy(fileVector& source):src(source),s(false), d(false) |
| 5 | +{ |
| 6 | +} |
| 7 | +////////////////////////////////////////////////////////////////////////// |
| 8 | +CCopy::~CCopy() |
| 9 | +{ |
| 10 | +} |
| 11 | +////////////////////////////////////////////////////////////////////////// |
| 12 | +//void CCopy::SetSource(fileVector& source) |
| 13 | +// { |
| 14 | +// src = source; |
| 15 | +// } |
| 16 | +////////////////////////////////////////////////////////////////////////// |
| 17 | +void CCopy::SetDestination(const fs::path& destination) |
| 18 | +{ |
| 19 | + dst = destination; |
| 20 | +} |
| 21 | + |
| 22 | +void CCopy::SetRecursive(bool _s, bool _d) |
| 23 | +{ |
| 24 | + s = _s; |
| 25 | + d = _d; |
| 26 | +} |
| 27 | +////////////////////////////////////////////////////////////////////////// |
| 28 | +bool CCopy::CheckDir() |
| 29 | +{ |
| 30 | + std::error_code e; |
| 31 | + bool ret = fs::exists(dst, e); |
| 32 | + PrintError(e); |
| 33 | + |
| 34 | + if(ret == false) |
| 35 | + { |
| 36 | + std::wcout << _T("Error: Path \"") << dst << _T("\" doesn't exist.") << std::endl; |
| 37 | + return false; |
| 38 | + } |
| 39 | + return true; |
| 40 | +} |
| 41 | +////////////////////////////////////////////////////////////////////////// |
| 42 | +bool CCopy::CollectFiles() |
| 43 | +{ |
| 44 | + if (CheckDir()) |
| 45 | + { |
| 46 | + std::wcout << _T("Collect files from ") << dst << std::endl; |
| 47 | + GetAllFilesFromFolder(dst, d, dst_list, mask); |
| 48 | + std::wcout << dst_list.size() << _T(" files collected") << std::endl; |
| 49 | + return !dst_list.empty(); |
| 50 | + } |
| 51 | + return false; |
| 52 | +} |
| 53 | +////////////////////////////////////////////////////////////////////////// |
| 54 | +bool CCopy::Process() |
| 55 | +{ |
| 56 | + bool retCode = false; |
| 57 | + int count = 0; |
| 58 | + std::error_code e; |
| 59 | + |
| 60 | + MakeMaskVector(); |
| 61 | + |
| 62 | + if (CollectFiles() == true) |
| 63 | + { |
| 64 | + for (auto i = src.begin(); i != src.end(); i++) |
| 65 | + { |
| 66 | + fileVector src_list; |
| 67 | + fs::path src_file(*i); |
| 68 | + if (!fs::is_regular_file(src_file, e)) |
| 69 | + { |
| 70 | + PrintError(e); |
| 71 | + src_file = src_file.parent_path(); |
| 72 | + if (src_file.empty() == true) |
| 73 | + src_file = fs::current_path(e); |
| 74 | + PrintError(e); |
| 75 | + GetAllFilesFromFolder(src_file, s, src_list,(*i).filename().generic_wstring()); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + src_list.push_back(*i); |
| 80 | + } |
| 81 | + PrintError(e); |
| 82 | + for(fileIterator k = src_list.begin(); k != src_list.end(); k++) |
| 83 | + { |
| 84 | + fileVector targetFiles; |
| 85 | + Find(*k, targetFiles); |
| 86 | + if (!targetFiles.empty()) |
| 87 | + { |
| 88 | + for (fileIterator j = targetFiles.begin(); j != targetFiles.end(); j++) |
| 89 | + { |
| 90 | + std::wcout << _T("Copy ") << *k << " to " << *j << std::endl; |
| 91 | + |
| 92 | + SetFileAttributes(j->c_str(),GetFileAttributes(j->c_str()) & ~FILE_ATTRIBUTE_READONLY); |
| 93 | + if (CopyFile(k->c_str(), j->c_str(), false) == false) |
| 94 | + { |
| 95 | + std::wcout << _T("Error: ") << GetLastErrorStr(); |
| 96 | + continue; |
| 97 | + } |
| 98 | + count++; |
| 99 | + } |
| 100 | + retCode = true; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + std::wcout << count << _T(" files copied") << std::endl; |
| 107 | + return retCode; |
| 108 | +} |
| 109 | + |
| 110 | +////////////////////////////////////////////////////////////////////////// |
| 111 | +void CCopy::Find(const fs::path & s, fileVector & found) |
| 112 | +{ |
| 113 | + for (auto i = dst_list.begin(); i != dst_list.end(); i++) |
| 114 | + { |
| 115 | + if ((*i).has_filename()) |
| 116 | + { |
| 117 | + if(CheckFilesForCopy(s, *i)) |
| 118 | + { |
| 119 | + found.push_back(*i); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +bool CCopy::CheckFilesForCopy(const fs::path & s, const fs::path & d) |
| 126 | +{ |
| 127 | + if (s.filename() != d.filename()) |
| 128 | + return false; |
| 129 | + |
| 130 | + std::error_code e; |
| 131 | + bool ret = fs::equivalent(s, d, e); |
| 132 | + PrintError(e); |
| 133 | + if(ret == true) |
| 134 | + { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | +// auto stime = fs::last_write_time(s,e); |
| 139 | +// PrintError(e); |
| 140 | +// |
| 141 | +// auto dtime = fs::last_write_time(d,e); |
| 142 | +// PrintError(e); |
| 143 | +// |
| 144 | +// if (stime < dtime) |
| 145 | +// { |
| 146 | +// return false; |
| 147 | +// } |
| 148 | + return true; |
| 149 | +} |
| 150 | + |
| 151 | +void CCopy::MakeMaskVector() |
| 152 | +{ |
| 153 | + mask.empty(); |
| 154 | + |
| 155 | + for (auto i = src.begin(); i != src.end(); i++) |
| 156 | + { |
| 157 | + mask.push_back((*i).filename()); |
| 158 | + } |
| 159 | +} |
| 160 | + |
0 commit comments