forked from FuShengDuoBuYu/Encode-Decode-Tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
144 lines (137 loc) · 4.17 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "util.h"
char encode10to2(string s){
bitset<8> bit(s);
char res = bit.to_ulong();
return res;
}
int getEncodeOrDecode(){
cout << "======================Welcome To Use Haffman Encode & Decode Tool========================" << endl
<< endl << "Encode: input '1'" << endl << "Decode: input '2' :" << endl;
char mode;
cin >> mode;
while(1){
if(mode==49){
return 1;
}
else if(mode==50){
return 2;
}
else{
cout << "your input is invalid,input it again:" << endl;
cin >> mode;
}
}
}
int encode2to10(string s){
bitset<256> bit(s);
int res = bit.to_ulong();
return res;
}
string encode10to2(int length, int value){
bitset<256> bit(value);
string temp = bit.to_string();
return temp.substr(temp.length()-length,length);
}
vector<string> getEncodeName(){
vector<string> res;
//输入文件(夹)
cout << "------------------------------------" << endl;
cout << "please input the filename that you want to encode:" << endl << "(include the suffix)" << endl;
cin.sync();
//输入的文件(夹)目录
while(1){
string sourceFileName;
getline(cin,sourceFileName);
path str(sourceFileName);
directory_entry entry(str);
//文件夹
if(entry.status().type() == file_type::directory){
res.push_back("0");
res.push_back(sourceFileName);
break;
}
//未找到文件
else if(entry.status().type() == file_type::not_found){
cout << "not found file or dir. input again:" << endl;
}
//文件
else{
res.push_back("1");
res.push_back(sourceFileName);
break;
}
}
cout << "------------------------------------" << endl;
//输出文件(夹)
cout << "please input the output filename that you want:" << endl << "(include the suffix)" << endl;
string desFileName;
getline(cin,desFileName);
res.push_back(desFileName);
return res;
}
vector<string> getDecodeName(){
//输入文件
cout << "------------------------------------" << endl;
cout << "please input the filename that you want to decode:" << endl << "(include the suffix)" << endl;
string sourceFileName;
cin.sync();
getline(cin,sourceFileName);
fstream testFile;
vector<string> res;
while(1){
testFile.open(sourceFileName);
if(!testFile){
cout << "can not find this file,please input again:" << endl;
getline(cin,sourceFileName);
}else{
//判断是文件夹还是文件
string firstLine;
getline(testFile, firstLine);
if(firstLine=="0"){
//文件夹
res.push_back("0");
}
else{
if(atoi(firstLine.c_str())==0){
//文件
res.push_back("1");
}
else{
//文件夹
res.push_back("0");
}
}
testFile.close();
res.push_back(sourceFileName);
break;
}
}
cout << "------------------------------------" << endl;
//输出文件
cout << "please input the output file/dir name that you want:" << endl << "(include the suffix)" << endl;
string desFileName;
getline(cin,desFileName);
res.push_back(desFileName);
return res;
}
vector<long long> getAfterSize(string path,int nums){
ifstream fin("test.hfm", ios::ate);
fin.seekg(-1, fin.cur);
// 查看前一个字符是否为回车符
while ((char)fin.peek() != '\n'){
fin.seekg(-1, fin.cur);
}
fin.seekg(2, fin.cur);
string line;
getline(fin, line);
fin.clear();
fin.close();
vector<long long> res;
istringstream istr(line);
for (int i = 0; i < nums;i++){
string temp;
istr >> temp;
res.push_back(atoll(temp.c_str()));
}
return res;
}