// fc.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <WINDOWS.H>
using namespace std;
int movefile(const char* source_multibyte, const char *destination_multibyte)
{
//char * source_multibyte = "D:\\chuax\\nist_encryption__2018.pdf";//源文件
//char * destination_multibyte = "D:\\chuax\\backup\\nist_encryption_FINAL.pdf";//目标文件
WCHAR source_widechar[1024];
memset(source_widechar, 0, sizeof(source_widechar));
MultiByteToWideChar(CP_ACP, 0, source_multibyte, strlen(source_multibyte) + 1, source_widechar, sizeof(source_widechar) / sizeof(source_widechar[0]));
WCHAR destination_widechar[1024];
memset(destination_widechar, 0, sizeof(destination_widechar));
MultiByteToWideChar(CP_ACP, 0, destination_multibyte, strlen(destination_multibyte) + 1, destination_widechar, sizeof(destination_widechar) / sizeof(destination_widechar[0]));
//char* szStr = "测试字符串";
//WCHAR wszClassName[256];
//memset(wszClassName, 0, sizeof(wszClassName));
//MultiByteToWideChar(CP_ACP, 0, szStr, strlen(szStr) + 1, wszClassName, sizeof(wszClassName) / sizeof(wszClassName[0]));
CopyFile(source_widechar, destination_widechar, FALSE);//false代表覆盖,true不覆盖
DeleteFile(source_widechar);
return 0;
}
class CMD
{
string function_name_;
map<string, string> parameter_;
public:
void parse_cmd(char* cmd);
void checking_cmd_integrity();
int exec_func();
};
void CMD::parse_cmd(char* cmd)
{
// remove '\n'
int end = strlen(cmd);
cmd[end-1] = ' ';
vector<string> str;
while (strlen(cmd) != 0)
{
bool begin_pos_found = false;
int begin_pos = 0;
int end_pos = 0;
int length = strlen(cmd);
for (int i = 0; i < length; i++)
{
if (!begin_pos_found && (' ' != cmd[i]))
{
begin_pos = i;
begin_pos_found = true;
}
if (begin_pos_found && (' ' == cmd[i]))
{
end_pos = i;
begin_pos_found = false;
int str_length = end_pos - begin_pos;
string str_element = ((string)cmd).substr(begin_pos, end_pos);
if (i != length - 1)
{
cmd += str_length + 1;
}
else
{
cmd += str_length;
}
str.push_back(str_element);
break;
}
if (!begin_pos_found && (i == length - 1))
{
cmd = "";
}
}
}
for (int i =0; i < str.size();)
{
if (i == 0)
{
function_name_ = str[0];
i++;
}
else
{
string key = str[i];
i++;
parameter_[key] = str[i];
i++;
}
}
cout << "done" << endl;
}
void CMD::checking_cmd_integrity()
{
}
int CMD::exec_func()
{
if (function_name_ == "mv")
{
auto it = parameter_.begin();
//some error code for map is empty, wait to sovle.
if (it == parameter_.end())
{
cout << "wrong input! Please check your input and retry!" << endl;
return 1;
}
string src = it->first;
string dst = it->second;
return movefile(src.c_str(), dst.c_str());
//auto it = parameter_.find("src");
//if (it == parameter_.end())
//{
// cout << "Miss source file! Please check your input and retry!" << endl;
// return 1;
//}
//string src = it->second;
//it = parameter_.find("dst");
//if (it == parameter_.end())
//{
// cout << "Miss dstination file! Please check your input and retry!" << endl;
// return 1;
//}
//string dst = it->second;
//return movefile(src.c_str(), dst.c_str());
}
}
int main()
{
while (true)
{
cout << "please input your working directory." << endl;
char parse_obj[1024];
CMD cmd;
if (nullptr != fgets(parse_obj, 1024, stdin))
{
cmd.parse_cmd(parse_obj);
int rt = cmd.exec_func();
if (rt != 0)
{
cout << "operation fail!"<< endl;
}
}
else
{
cout << "fail to get input!! Please report to the developer." << endl;
system("pause");
return 1;
}
}
return 0;
}
//
#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <WINDOWS.H>
using namespace std;
int movefile(const char* source_multibyte, const char *destination_multibyte)
{
//char * source_multibyte = "D:\\chuax\\nist_encryption__2018.pdf";//源文件
//char * destination_multibyte = "D:\\chuax\\backup\\nist_encryption_FINAL.pdf";//目标文件
WCHAR source_widechar[1024];
memset(source_widechar, 0, sizeof(source_widechar));
MultiByteToWideChar(CP_ACP, 0, source_multibyte, strlen(source_multibyte) + 1, source_widechar, sizeof(source_widechar) / sizeof(source_widechar[0]));
WCHAR destination_widechar[1024];
memset(destination_widechar, 0, sizeof(destination_widechar));
MultiByteToWideChar(CP_ACP, 0, destination_multibyte, strlen(destination_multibyte) + 1, destination_widechar, sizeof(destination_widechar) / sizeof(destination_widechar[0]));
//char* szStr = "测试字符串";
//WCHAR wszClassName[256];
//memset(wszClassName, 0, sizeof(wszClassName));
//MultiByteToWideChar(CP_ACP, 0, szStr, strlen(szStr) + 1, wszClassName, sizeof(wszClassName) / sizeof(wszClassName[0]));
CopyFile(source_widechar, destination_widechar, FALSE);//false代表覆盖,true不覆盖
DeleteFile(source_widechar);
return 0;
}
class CMD
{
string function_name_;
map<string, string> parameter_;
public:
void parse_cmd(char* cmd);
void checking_cmd_integrity();
int exec_func();
};
void CMD::parse_cmd(char* cmd)
{
// remove '\n'
int end = strlen(cmd);
cmd[end-1] = ' ';
vector<string> str;
while (strlen(cmd) != 0)
{
bool begin_pos_found = false;
int begin_pos = 0;
int end_pos = 0;
int length = strlen(cmd);
for (int i = 0; i < length; i++)
{
if (!begin_pos_found && (' ' != cmd[i]))
{
begin_pos = i;
begin_pos_found = true;
}
if (begin_pos_found && (' ' == cmd[i]))
{
end_pos = i;
begin_pos_found = false;
int str_length = end_pos - begin_pos;
string str_element = ((string)cmd).substr(begin_pos, end_pos);
if (i != length - 1)
{
cmd += str_length + 1;
}
else
{
cmd += str_length;
}
str.push_back(str_element);
break;
}
if (!begin_pos_found && (i == length - 1))
{
cmd = "";
}
}
}
for (int i =0; i < str.size();)
{
if (i == 0)
{
function_name_ = str[0];
i++;
}
else
{
string key = str[i];
i++;
parameter_[key] = str[i];
i++;
}
}
cout << "done" << endl;
}
void CMD::checking_cmd_integrity()
{
}
int CMD::exec_func()
{
if (function_name_ == "mv")
{
auto it = parameter_.begin();
//some error code for map is empty, wait to sovle.
if (it == parameter_.end())
{
cout << "wrong input! Please check your input and retry!" << endl;
return 1;
}
string src = it->first;
string dst = it->second;
return movefile(src.c_str(), dst.c_str());
//auto it = parameter_.find("src");
//if (it == parameter_.end())
//{
// cout << "Miss source file! Please check your input and retry!" << endl;
// return 1;
//}
//string src = it->second;
//it = parameter_.find("dst");
//if (it == parameter_.end())
//{
// cout << "Miss dstination file! Please check your input and retry!" << endl;
// return 1;
//}
//string dst = it->second;
//return movefile(src.c_str(), dst.c_str());
}
}
int main()
{
while (true)
{
cout << "please input your working directory." << endl;
char parse_obj[1024];
CMD cmd;
if (nullptr != fgets(parse_obj, 1024, stdin))
{
cmd.parse_cmd(parse_obj);
int rt = cmd.exec_func();
if (rt != 0)
{
cout << "operation fail!"<< endl;
}
}
else
{
cout << "fail to get input!! Please report to the developer." << endl;
system("pause");
return 1;
}
}
return 0;
}