1 |
// VTB File Utility |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// VTBFileUtil.cpp |
6 |
|
7 |
#ifdef _DEBUG |
8 |
#pragma warning(disable:4786) |
9 |
#endif // _DEBUG |
10 |
|
11 |
#include <iostream> |
12 |
#include <fstream> |
13 |
#include <string> |
14 |
#include <vector> |
15 |
#include <cctype> |
16 |
#include <cstdio> |
17 |
#include <cstdlib> |
18 |
|
19 |
#include <sys/stat.h> |
20 |
|
21 |
using namespace std; |
22 |
|
23 |
const unsigned LENGTH = 1024; |
24 |
const char* CFG = "VTBFileUtil.cfg"; |
25 |
|
26 |
string filename(string& scan); |
27 |
void rename(string& scan, string& good); |
28 |
void move(string& good, string& directory); |
29 |
void size(string& scan); |
30 |
void quit(int code); |
31 |
|
32 |
int main(int argc, char* argv[]) |
33 |
{ |
34 |
string directory; |
35 |
|
36 |
if (argc > 1) |
37 |
{ |
38 |
ifstream fin(CFG); |
39 |
getline(fin, directory); |
40 |
fin.close(); |
41 |
} |
42 |
else |
43 |
{ |
44 |
cout << "VTB File Utility\n" |
45 |
<< "Usage: VTBFileUtil [pdfscan ...]\n"; |
46 |
return 0; |
47 |
} |
48 |
|
49 |
if (directory == "") |
50 |
{ |
51 |
do |
52 |
{ |
53 |
cout << "No directory specified.\nDirectory: "; |
54 |
|
55 |
char* cstr = new char[LENGTH]; |
56 |
cin.getline(cstr, LENGTH); |
57 |
|
58 |
directory = cstr; |
59 |
|
60 |
delete [] cstr; |
61 |
} |
62 |
while (directory == ""); |
63 |
|
64 |
ofstream fout(CFG); |
65 |
fout << directory << "\n"; |
66 |
fout.close(); |
67 |
} |
68 |
|
69 |
for (unsigned index = 1; index < argc; index++) |
70 |
{ |
71 |
if (strpbrk(argv[index], "*?")) continue; |
72 |
|
73 |
string extension; |
74 |
for (int number = 4; number > 0; number--) |
75 |
{ |
76 |
extension += tolower(argv[index][strlen(argv[index]) - number]); |
77 |
} |
78 |
if (extension != ".pdf") |
79 |
{ |
80 |
cout << argv[index] << " is not a PDF.\n"; |
81 |
continue; |
82 |
} |
83 |
|
84 |
ifstream tester(argv[index]); |
85 |
if (!tester.is_open()) |
86 |
{ |
87 |
cout << argv[index] << " does not exist.\n"; |
88 |
continue; |
89 |
} |
90 |
tester.close(); |
91 |
|
92 |
string scan = argv[index]; |
93 |
string command = "start " + scan; |
94 |
system(command.c_str()); |
95 |
|
96 |
string good; |
97 |
bool correct = false; |
98 |
do |
99 |
{ |
100 |
good = filename(scan); |
101 |
|
102 |
cout << "Correct (y/n): "; |
103 |
|
104 |
string answer; |
105 |
|
106 |
char* cstr = new char[LENGTH]; |
107 |
cin.getline(cstr, LENGTH); |
108 |
|
109 |
answer = cstr; |
110 |
|
111 |
delete [] cstr; |
112 |
|
113 |
for (int index = 0; index < answer.length(); index++) |
114 |
{ |
115 |
answer[index] = tolower(answer[index]); |
116 |
} |
117 |
|
118 |
if (answer == "yes" || answer == "y") |
119 |
{ |
120 |
correct = true; |
121 |
} |
122 |
else |
123 |
{ |
124 |
cout << "Continue (y/n): "; |
125 |
|
126 |
cstr = new char[LENGTH]; |
127 |
cin.getline(cstr, LENGTH); |
128 |
|
129 |
answer = cstr; |
130 |
|
131 |
delete [] cstr; |
132 |
|
133 |
if (answer != "yes" && answer != "y") quit(0); |
134 |
} |
135 |
} |
136 |
while (!correct); |
137 |
|
138 |
rename(scan, good); |
139 |
|
140 |
move(good, directory); |
141 |
|
142 |
tester.open(directory.c_str()); |
143 |
if (tester.is_open()) |
144 |
{ |
145 |
tester.close(); |
146 |
rename(directory, good); |
147 |
|
148 |
command = "mkdir " + directory; |
149 |
system(command.c_str()); |
150 |
|
151 |
move(good, directory); |
152 |
} |
153 |
|
154 |
size(directory + "\\" + good); |
155 |
} |
156 |
|
157 |
return 0; |
158 |
} |
159 |
|
160 |
string filename(string& scan) |
161 |
{ |
162 |
string name, file; |
163 |
unsigned number; |
164 |
|
165 |
cout << "Scan PDF: " << scan << "\n" |
166 |
<< "Client Name: "; |
167 |
|
168 |
char* cstr = new char[LENGTH]; |
169 |
cin.getline(cstr, LENGTH); |
170 |
|
171 |
name = cstr; |
172 |
|
173 |
delete [] cstr; |
174 |
|
175 |
cout << "Client Number: "; |
176 |
|
177 |
cstr = new char[LENGTH]; |
178 |
cin.getline(cstr, LENGTH); |
179 |
|
180 |
number = strtoul(cstr, 0, 0); |
181 |
|
182 |
delete [] cstr; |
183 |
|
184 |
cstr = new char[LENGTH]; |
185 |
sprintf(cstr, "%u_", number); |
186 |
|
187 |
file += cstr; |
188 |
|
189 |
delete [] cstr; |
190 |
|
191 |
for (unsigned index = 0; index < name.length(); index++) |
192 |
{ |
193 |
if (name[index] == ',' || name[index] == '.') |
194 |
{ |
195 |
} |
196 |
else if (isspace(name[index])) |
197 |
{ |
198 |
file += '_'; |
199 |
} |
200 |
else |
201 |
{ |
202 |
file += tolower(name[index]); |
203 |
} |
204 |
} |
205 |
|
206 |
file += ".pdf"; |
207 |
|
208 |
cout << "Filename: " << file << "\n"; |
209 |
|
210 |
return file; |
211 |
} |
212 |
|
213 |
void rename(string& scan, string& good) |
214 |
{ |
215 |
string command = "rename \"" + scan + "\" \"" + good + "\""; |
216 |
|
217 |
while (system(command.c_str())) system("pause"); |
218 |
} |
219 |
|
220 |
void move(string& good, string& directory) |
221 |
{ |
222 |
string command = "move \"" + good + "\" \"" + directory + "\""; |
223 |
|
224 |
while (system(command.c_str())) system("pause"); |
225 |
} |
226 |
|
227 |
void size(string& scan) |
228 |
{ |
229 |
struct stat status; |
230 |
|
231 |
if (!stat(scan.c_str(), &status)) |
232 |
{ |
233 |
char* cbytes = new char[LENGTH]; |
234 |
sprintf(cbytes, "%u", status.st_size); |
235 |
|
236 |
string bytes = cbytes; |
237 |
|
238 |
delete [] cbytes; |
239 |
|
240 |
for (int index = bytes.length(), number = 0; index > 0; index--, |
241 |
number++) |
242 |
{ |
243 |
if ((number % 3 == 0) && ((index - 3) > 0)) |
244 |
{ |
245 |
bytes.insert(index - 3, 1, ','); |
246 |
} |
247 |
} |
248 |
|
249 |
char* megabytes = new char[LENGTH]; |
250 |
sprintf(megabytes, "%.2f", (double(status.st_size) / double(1024) / |
251 |
double(1024))); |
252 |
|
253 |
cout << "Filesize: " << bytes << " bytes (" << megabytes << " MB)\n"; |
254 |
|
255 |
delete [] megabytes; |
256 |
} |
257 |
else |
258 |
{ |
259 |
cerr << "VTBFileUtil: stat(scan.c_str(), &status) failed in void size(" |
260 |
<< "string& scan).\n"; |
261 |
exit(1); |
262 |
} |
263 |
} |
264 |
|
265 |
void quit(int code) |
266 |
{ |
267 |
exit(code); |
268 |
} |