-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbwm.cpp
225 lines (204 loc) · 6.28 KB
/
bwm.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
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
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <sys/wait.h> /* Wait for Process Termination */
#include <stdlib.h> /* General Utilities */
#include <iostream>
#include <string>
#include <dirent.h>
#include <sys/stat.h>
using namespace std;
int wm(string, string);
int loop(DIR*,string,int,int,string);
int main(int argc, char* argv[])
{
pid_t wgetPID;
int status;
int rlevel;
//Create the temp directory that is going to be used for temp files
pid_t mkdirPID = fork();
if (mkdirPID == 0) {
execl ("/bin/mkdir", "mkdir", "temp", (char*)0);
} else {
wait (&status);
}
//Loop through the arguments and see if -r or --recursive=NUM was specefied,
//otherwise
bool recursive = false;
bool save = false;
string rnum, str;
string URL = argv[argc-2];
for ( int i = 1; i < argc-1; i++) {
str = argv[i];
//If the string length is <2 we need to check
if (str.length() > 2) {
if (str.at(2) == 's' && str.length() > 2) {
save=true;
}
if (str.at(2) == 'r') {
recursive =true;
rnum=argv[i];
rnum = rnum.substr(12, rnum.length()-1);
}
}
//check to see if -r is called
if (str.at(1) == 'r' ) {
recursive=true;
rnum = argv[i+1];
i++;
//check to see if --recursive is called, then set rnum to whatever is after the =
}
if (str.at(1) == 's' ) {
save=true;
}
}
if(recursive){
rlevel = atoi(rnum.c_str());
}else{
rlevel = -1;
}
if (recursive && URL.at(URL.length()-1) == '/') {
//Need to exit because the user tried to call -r or --recursive=NUM on
// a single image.
cout << "bwm will now exit because -r or --recursive=NUM was specefied without specifying a directory" << endl;
exit(1);
}
//If the URL is a directory, then see if -r or --recursive=NUM was specefied
if (URL.at(URL.length()-1) == '/') {
wgetPID = fork();
if (wgetPID == 0) {
//use -r and -l # to recursivly download to a certain depth.
if (recursive) {
execl ("/usr/bin/wget", "wget","-q", "-r","-l",rnum.c_str(), "-P","./temp/","-A", "jpg,tif,png", argv[argc-2], (char*)0);
} else {
execl ("/usr/bin/wget", "wget", "-q","-r", "-P","./temp/","-A", "jpg,tif,png", argv[argc-2], (char*)0);
}
}
else {
wait(&status);
}
} else {
//If it is a single image, download it
pid_t singleImagePID = fork();
if (singleImagePID == 0) {
switch (URL.at(URL.length()-2) ) {
case 'p':
cout << "***Image is a jpg" << endl;
execl ("/usr/bin/wget", "wget", "-q","-P","./temp", argv[argc-2], (char*)0);
break;
case 'n':
cout << "***Image is a png" << endl;
execl ("/usr/bin/wget", "wget", "-q", "-P","./temp", argv[argc-2], (char*)0);
break;
case 'i':
cout << "***Image is a TIFF" << endl;
execl ("/usr/bin/wget", "wget", "-P","./temp", argv[argc-2], (char*)0);
break;
}
} else {
wait(&status);
}
}
//If -s or --save was specefied, copy the directory so that we save the
if (save) {
pid_t rmdirPID = fork();
if (rmdirPID == 0) {
cout << "Copy Originals because -s or --save was specefied." << endl;
execl("/bin/cp","cp","-r","./temp/", "./Original",(char*)0);
}
else {
wait(&status);
}
}
//Create the directory stuff for the looping
DIR* dir;
dir = opendir("./temp/accounts.cs.ou.edu/~phil7017");
string URL2;
string watermark = argv[argc-1];
if (URL.at(URL.length()-1) == '/') {
cout << "*****************Start Sweeping For Images**********************" << endl;
loop(dir,"./temp/accounts.cs.ou.edu/~phil7017",rlevel,0,watermark);
} else {
//Call wm for a single image
URL2 = "./temp/" + URL.substr(URL.find_last_of('/'), URL.length()-1);
wm (URL2, argv[argc-1]);
}
//Fork off the process to upload the images.
pid_t uploadPID = fork();
if (uploadPID == 0) {
if ( URL.at(URL.length()-1) == '/' ) {
cout << "Begin Upload" << endl;
execl ("/usr/bin/scp", "scp", "-r", "./temp/accounts.cs.ou.edu/~phil7017/images/", "[email protected]:~/public_html/", (char*)0);
} else {
cout << "Begin Upload" << endl;
execl ("/usr/bin/scp", "scp", URL2.c_str(), "[email protected]:~/public_html/", (char*)0);
}
} else {
wait(&status);
}
//Fork off process to remove the temp files.
pid_t rmtemp = fork();
if (rmtemp == 0) {
cout << "Remove Temporary Files" << endl;
execl ("/bin/rm", "rm", "-r", "temp",(char*)0);
}
else {
wait(&status);
}
}
/**
Method that loops through the files recursivly and when an image is found
calls the wm method to apply the watermark.
**/
int loop(DIR* dir, string name, int max, int current, string watermark){
DIR* dent = opendir(name.c_str());
struct dirent* ent;
string filepath;
if(dent == NULL){
cout << "error opening directory" << endl;
return -1;
}
while((ent = readdir(dent))){
string file = ent->d_name;
filepath = name + "/" + ent->d_name;
struct stat st;
if(stat(filepath.c_str(), &st)) continue;
if(S_ISDIR(st.st_mode) && (file.compare(".") != 0 && file.compare("..") != 0)){
DIR* cur = opendir(ent->d_name);
loop(cur, filepath,max,current++,watermark);
}else if(file.compare(".") != 0 && file.compare("..") != 0){
if(filepath.at(filepath.length() - 1) != '/') {
cout << "Watermark image " << filepath << endl;
wm(filepath,watermark);
}
}
}
return 1;
}
/**
Method that takes two string values, one for the pathname to the image and the pathname
to the watermark. Applies the watermark and overwrites the original image.
**/
int wm( string file, string watermark){
string filePath = file;
string wmPath = watermark;
/*Create child pid_t so we can wait fork off children and wait on them to finish*/
pid_t childpid2;
int status; /* parent process: child's exit status */
//use this to set the type of image, (1=jpg, 2=png, 3=tif) for saving
//the image as well as when viewing the final image on the web.
childpid2 = fork(); /* Create new child to apply watermark*/
if ( childpid2 >= 0 )
{
if ( childpid2 == 0 )
{
//call the exec in child2 to apply the watermark on the image.
if(filePath.at(filePath.length() - 1) != '/')
execl ("/usr/bin/composite", "composite", "-compose","bumpmap", "-tile", wmPath.c_str(),filePath.c_str(), filePath.c_str(), (char*)0);
}
else {
//wait for child2 to finish applying the watermark.
wait (&status);
}
}
return 0;
}