This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileList.h
277 lines (250 loc) · 6.57 KB
/
FileList.h
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
西安电子科技大学
TS001001工程概论
第20小组
刘
俞
张
2020年5月19日
训练项目,严格测试前,请勿用于正式生产
*/
#ifndef _FILELIST_H
#define _FILELIST_H 1
#include <cstdio>
#include <string>
#include <vector>
#include <dirent.h>
#include <climits>
#include <algorithm>
#include "status.h"
namespace filelist{
const bool LOWER_CASE = false;
const bool UPPER_CASE = true;
const int BASENAME_PART = 1;
const int EXTENSION_PART = 2;
struct File
{
bool hidden;
std::string oldName;
std::string newName;
std::string oldExtension;
std::string newExtension;
File(const std::string &s);
std::string oldFullName();
std::string newFullName();
};
class FileList
{
private:
std::vector<File> files;
std::string directory;
bool iHidden;
public:
FileList();
~FileList(){}
bool read(const std::string &dir, bool ignoreHidden = true);
void preview(int maxitem = 0);
bool modify(const std::string &namestr, int begin = 1, int step = 1, int digit = 1);
bool modifyExtension(const std::string &e);
bool modifyCase(int part, bool u_l);
bool replace(const std::string &source, const std::string &str);
bool insert(const std::string &source, int pos);
bool del(int pos, int num);
bool execute();
};
File::File(const std::string &s)
{
hidden = false;
std::string::size_type dotPos = s.find_last_of('.');
if(dotPos == 0){
hidden = true;
newName = oldName = s.substr(1);
}else if(dotPos == std::string::npos){
newName = oldName = s;
}else{
newName = oldName = s.substr(0, dotPos);
newExtension = oldExtension = s.substr(dotPos+1);
}
}
std::string File::oldFullName()
{
return std::string(
( hidden ? "." : "" )
+ oldName
+ ( oldExtension.empty() ? "" : ("." + oldExtension) )
);
}
std::string File::newFullName()
{
return std::string(
( hidden ? "." : "" )
+ newName
+ ( newExtension.empty() ? "" : ("." + newExtension) )
);
}
FileList::FileList()
{
iHidden = true;
}
bool FileList::read(const std::string &dir, bool ignoreHidden)
{
iHidden = ignoreHidden;
directory = dir;
if(directory[directory.length()-1] != '/'){
directory += '/';
}
DIR *d = opendir(directory.c_str());
if(d == NULL){
status::printInfo(status::OPEN_DIR_ERROR);
return 1;
}
dirent *d_ent = NULL;
while( (d_ent = readdir(d)) != NULL ){
if(strcmp(d_ent->d_name, ".") != 0
&& strcmp(d_ent->d_name, "..") != 0
&& d_ent->d_type != DT_DIR){
if(iHidden && d_ent->d_name[0] == '.'){
continue;
}
if(strchr(d_ent->d_name, '*') != NULL || strchr(d_ent->d_name, '$') != NULL){
status::printInfo(status::RESERVED_CHAR_IN_OLD_NAME);
closedir(d);
return 1;
}
files.push_back(File(d_ent->d_name));
}
}
closedir(d);
if(files.size() == 0){
status::printInfo(status::NO_FILE);
return 1;
}
return 0;
}
void FileList::preview(int maxitem)
{
std::cout << "-----PREVIEW-----" << std::endl;
int lineNum = 1;
for(std::vector<File>::iterator i = files.begin(); i != files.end() && (lineNum <= maxitem || maxitem == 0); ++i, ++lineNum){
std::cout << lineNum << "." << std::endl;
std::cout << "\t" << (*i).oldFullName() << std::endl;
std::cout << "\t\t\t----->\t\t" << (*i).newFullName() << std::endl;
}
std::cout << "-------END-------" << std::endl;
if(maxitem && int(files.size()-maxitem) > 0){
std::cout << int(files.size() - maxitem) << " file(s) are not showed." << std::endl;
}
}
bool FileList::modify(const std::string &namestr, int begin, int step, int digit)
{
if( begin + step*files.size() >= INT_MAX){
status::printInfo(status::NUM_RANGE_LARGE);
return 1;
}
int number = begin;
char numStr[20];
for(std::vector<File>::iterator i = files.begin(); i != files.end(); ++i){
(*i).newName = namestr;
std::string::size_type placeHolderPos;
while( (placeHolderPos = (*i).newName.find('*')) != std::string::npos){
(*i).newName.erase(placeHolderPos, 1);
(*i).newName.insert(placeHolderPos, (*i).oldName);
}
sprintf(numStr, "%*d", digit, number);
for(int i = 0; numStr[i]; ++i){
if(numStr[i] == ' '){
numStr[i] = '0';
}
}
while( (placeHolderPos = (*i).newName.find('$')) != std::string::npos){
(*i).newName.erase(placeHolderPos, 1);
(*i).newName.insert(placeHolderPos, numStr);
}
number += step;
}
return 0;
}
bool FileList::modifyExtension(const std::string &e)
{
for(std::vector<File>::iterator i = files.begin(); i != files.end(); ++i){
(*i).newExtension = e;
}
return 0;
}
bool FileList::modifyCase(int part, bool u_l)
{
if(part & BASENAME_PART){
std::cout << "TEST\tBASENAME_PART" << std::endl;
for(std::vector<File>::iterator i = files.begin(); i != files.end(); ++i){
transform((*i).newName.begin(), (*i).newName.end(), (*i).newName.begin(), (u_l == UPPER_CASE ? ( (int (*)(int))toupper ) : ( (int (*)(int))tolower ) ));
}
}
if(part & EXTENSION_PART){
std::cout << "TEST\tEXTENSION_PART" << std::endl;
for(std::vector<File>::iterator i = files.begin(); i != files.end(); ++i){
transform((*i).newExtension.begin(), (*i).newExtension.end(), (*i).newExtension.begin(), (u_l == UPPER_CASE ? ( (int (*)(int))toupper ) : ( (int (*)(int))tolower ) ));
}
}
return 0;
}
bool FileList::replace(const std::string &source, const std::string &str)
{
for(std::vector<File>::iterator i = files.begin(); i != files.end(); ++i){
std::size_t pos = 0;
while( ( pos = (*i).newName.find(source, pos) ) != std::string::npos ){
(*i).newName.erase(pos, source.length());
(*i).newName.insert(pos, str);
pos += str.length();
if(pos > (*i).newName.length()){
break;
}
}
if( (*i).newName.length() == 0 ){
status::printInfo(status::FILENAME_EMPTY);
return 1;
}
}
return 0;
}
bool FileList::insert(const std::string &source, int pos)
{
for(std::vector<File>::iterator i = files.begin(); i != files.end(); ++i){
if(pos >= int((*i).newName.length())){
(*i).newName.append(source);
}else{
(*i).newName.insert(pos, source);
}
}
return 0;
}
bool FileList::del(int pos, int num)
{
for(std::vector<File>::iterator i = files.begin(); i != files.end(); ++i){
if(pos >= int((*i).newName.length())){
continue;
}else{
(*i).newName.erase(pos, num);
if( (*i).newName.length() == 0 ){
status::printInfo(status::FILENAME_EMPTY);
return 1;
}
}
}
return 0;
}
bool FileList::execute()
{
for(std::vector<File>::iterator i = files.begin(); i != files.end(); ++i){
if( (*i).newFullName().find('/') != std::string::npos ){
status::printInfo(status::RESERVED_CHAR_IN_NEW_NAME);
return 1;
}
if(rename( (directory + (*i).oldFullName()).c_str(), (directory + (*i).newFullName()).c_str() )){
status::printInfo(status::RENAME_ERROR);
return 1;
}
}
return 0;
}
}
#endif