[47d66f]: src / tools / pto_move.cpp Maximize Restore History

Download this file

pto_move.cpp    504 lines (485 with data), 18.6 kB

  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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// -*- c-basic-offset: 4 -*-
/** @file pto_move.cpp
*
* @brief helper program for moving project with all images at once
*
*
* @author T. Modes
*
*/
/* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <hugin_version.h>
#include <iostream>
#include <string>
#include <fstream>
#define BOOST_FILESYSTEM_VERSION 3
#include <boost/filesystem.hpp>
#include <getopt.h>
#include <panodata/Panorama.h>
#include <hugin_utils/stl_utils.h>
namespace fs=boost::filesystem;
std::string IncludeTrailingDelimiter(std::string path)
{
std::string s=path;
#ifdef _WIN32
if(s.compare(s.length()-1,1,"\\")!=0 && s.compare(s.length()-1,1,"/")!=0)
{
s.append("\\");
};
#else
if(s.compare(s.length()-1,1,"/")!=0)
{
s.append("/");
};
#endif
return s;
};
// rebase a filename from relative to srcPath to relative to destPath and return absolute new dest path
bool RebaseFilename(fs::path srcFile, fs::path& destFile, std::string srcPath, std::string destPath)
{
fs::path input=fs::absolute(srcFile);
std::string fullInputPath=input.string();
std::string srcPathWithTrailingDelimiter=IncludeTrailingDelimiter(srcPath);
if(fullInputPath.compare(0, srcPathWithTrailingDelimiter.length(), srcPathWithTrailingDelimiter)!=0)
{
return false;
};
fullInputPath.replace(0, srcPathWithTrailingDelimiter.length(), IncludeTrailingDelimiter(destPath));
destFile=fs::path(fullInputPath);
return true;
};
bool checkDestinationDirectory(std::string dir, fs::path & pathTo)
{
pathTo=fs::path(dir);
try
{
// check if a destination directory is given
if(pathTo.extension().string().length()>0)
{
std::cerr << "ERROR: Your destination is a file. Copy/Move several files to " << std::endl
<< "a single file is not allowed." << std::endl
<< "Canceled operation." << std::endl;
return false;
};
// create destination directory if not exists
if(!fs::exists(pathTo))
{
if(!fs::create_directories(pathTo))
{
std::cerr << "ERROR: Could not create destination directory: " << pathTo.string() << std::endl
<< "Maybe you have not sufficent rights to create this directory." << std::endl;
return false;
};
};
}
catch (const fs::filesystem_error& ex)
{
std::cout << ex.what() << std::endl;
return false;
}
pathTo=fs::absolute(pathTo);
return true;
};
typedef std::vector<fs::path> pathVec;
bool PTOCopyMove(bool movingFile, fs::path src, fs::path dest, bool overwriteAllFiles)
{
fs::path destFile(hugin_utils::GetAbsoluteFilename(dest.string()));
std::cout << (movingFile ? "Moving project file " : "Copying project file ") << src.filename() << std::endl
<< " from " << src.parent_path() << std::endl
<< " to " << destFile.parent_path() << std::endl;
// open project file
HuginBase::Panorama pano;
std::string input=src.string();
std::ifstream prjfile(input.c_str());
if (!prjfile.good())
{
std::cerr << "ERROR: Could not open script: " << src.string() << endl;
return false;
}
std::string inputPathPrefix=hugin_utils::getPathPrefix(input);
std::string outputPathPrefix=hugin_utils::getPathPrefix(destFile.string());
pano.setFilePrefix(inputPathPrefix);
AppBase::DocumentData::ReadWriteError err = pano.readData(prjfile);
if (err != AppBase::DocumentData::SUCCESSFUL)
{
std::cerr << "ERROR: error while parsing panos tool script: " << input << std::endl
<< "DocumentData::ReadWriteError code: " << err << std::endl;
return false;
};
prjfile.close();
if(pano.getNrOfImages()==0)
{
std::cerr << "ERROR: project " << input << " does not contain any images." << std::endl;
return false;
};
pathVec imagesFrom, imagesTo;
// check if all images exists
for(size_t i=0; i<pano.getNrOfImages(); i++)
{
fs::path p(pano.getImage(i).getFilename());
imagesFrom.push_back(p);
if(!fs::exists(p) || !fs::is_regular_file(p))
{
std::cerr << "ERROR: image " << p.string() << " not found on disc." << std::endl
<< "Skipping project " << input << std::endl;
return false;
};
p=fs::absolute(p);
// now build now image filename
fs::path newFilename;
if(RebaseFilename(p, newFilename, inputPathPrefix, outputPathPrefix))
{
pano.setImageFilename(i, newFilename.string());
imagesTo.push_back(newFilename);
};
};
if(imagesFrom.size()>0)
{
if(imagesFrom.size()==imagesTo.size())
{
fs::path targetDir(destFile);
targetDir.remove_filename();
if(!checkDestinationDirectory(targetDir.string(), targetDir))
{
return false;
};
if(fs::exists(destFile) && !overwriteAllFiles)
{
std::cout << "Project file " << destFile << " does already exists." << std::endl
<< " Overwrite this file? [Y|N] ";
std::string userAnswer;
while(userAnswer.length()==0)
{
std::cin >> userAnswer;
};
userAnswer=hugin_utils::toupper(userAnswer);
if(userAnswer!="YES" && userAnswer!="Y")
{
std::cout << std::endl << "Moving/Copying of project file " << input << " canceled." << std::endl << std::endl;
return false;
}
};
//copy/moving images
for(size_t i=0; i<imagesFrom.size(); i++)
{
// check if target directory already exists
targetDir=fs::path(imagesTo[i]);
targetDir.remove_filename();
if(!checkDestinationDirectory(targetDir.string(), targetDir))
{
return false;
};
//check if target image file already exists
if(fs::exists(imagesTo[i]) && !overwriteAllFiles)
{
std::cout << "Images file " << imagesTo[i] << " does already exists." << std::endl
<< " Overwrite this file? [Y|N] ";
std::string userAnswer;
while(userAnswer.length()==0)
{
std::cin >> userAnswer;
};
userAnswer=hugin_utils::toupper(userAnswer);
if(userAnswer!="YES" && userAnswer!="Y")
{
std::cout << std::endl << "Moving/Copying of project file " << input << " canceled." << std::endl << std::endl;
return false;
}
};
if(movingFile)
{
try
{
fs::rename(imagesFrom[i], imagesTo[i]);
}
catch (const fs::filesystem_error& ex)
{
std::cout << ex.what() << std::endl;
return false;
}
}
else
{
try
{
fs::copy_file(imagesFrom[i], imagesTo[i], fs::copy_option::overwrite_if_exists);
}
catch (const fs::filesystem_error& ex)
{
std::cout << ex.what() << std::endl;
return false;
}
};
}; // for loop for all images
// now create pano file in new destination
// write output
HuginBase::UIntSet imgs;
fill_set(imgs, 0, pano.getNrOfImages()-1);
std::ofstream of(destFile.string().c_str());
pano.printPanoramaScript(of, pano.getOptimizeVector(), pano.getOptions(), imgs, false, outputPathPrefix);
of.close();
if(movingFile)
{
try
{
fs::remove(src);
}
catch (const fs::filesystem_error& ex)
{
std::cout << "Could not remove original file: " << input << std::endl;
std::cout << ex.what() << std::endl;
return false;
}
};
}
else
{
// the images in the project file are not all relative to the same base path
std::cout << "WARNING: Images location in project file are not consistent. " << std::endl
<< "So don't move/copy project file " << src.string() << std::endl;
return false;
};
}
else
{
// now create pano file in new destination, project contains images in paths
// not relative to base directory
// so create only the new project file without copying/moving image files
HuginBase::UIntSet imgs;
fill_set(imgs, 0, pano.getNrOfImages()-1);
std::ofstream of(destFile.string().c_str());
pano.printPanoramaScript(of, pano.getOptimizeVector(), pano.getOptions(), imgs, false, outputPathPrefix);
of.close();
if(movingFile)
{
try
{
fs::remove(src);
}
catch (const fs::filesystem_error& ex)
{
std::cout << "Could not remove original file: " << input << std::endl;
std::cout << ex.what() << std::endl;
return false;
}
};
};
return true;
};
template <class iteratorType>
bool iterateFileSystem(std::string src, pathVec& projectFiles)
{
try
{
for(iteratorType it(src); it != iteratorType(); it++)
{
std::string ext=hugin_utils::toupper(it->path().extension().string());
if(ext==".PTO")
{
projectFiles.push_back(*it);
};
}
}
catch(fs::filesystem_error e)
{
std::cout << e.what() << std::endl;
return false;
}
return true;
};
void SearchPTOFilesInDirectory(pathVec &projectFiles, std::string src, bool recursive)
{
if(recursive)
{
iterateFileSystem<fs::recursive_directory_iterator>(src, projectFiles);
}
else
{
iterateFileSystem<fs::directory_iterator>(src, projectFiles);
};
};
static void usage(const char * name)
{
std::cout << name << ": move a project file with all images in it" << std::endl
<< name << " version " << DISPLAY_VERSION << std::endl
<< std::endl
<< "Usage: pto_move [options] path1/source.pto path2/dest.pto" << std::endl
<< " Rename project file path1/source.pto to " << std::endl
<< " path2/dest.pto. All images contained in the project will" << std::endl
<< " be moved accordingly." << std::endl << std::endl
<< " pto_move [options] sourceFolder destFolder" << std::endl
<< " Moves all project files in the source folder to " << std::endl
<< " the destination folder including all images." << std::endl
<< std::endl
<< "Options: " << std::endl
<< " --copy Copy project files and images instead of moving" << std::endl
<< " --recursive Only effective in use case 2. Go recursive in the" << std::endl
<< " the source filder and move all project files with images" << std::endl
<< " to destination folder by maintaining the folder structure" << std::endl
<< " relative to source folder." << std::endl
<< " --overwrite Overwrite all existing files. Otherwise you will be asked" << std::endl
<< " for each existing file." << std::endl
<< std::endl
<< std::endl;
};
int main(int argc, char *argv[])
{
// parse arguments
const char* optstring = "croh";
static struct option longOptions[] =
{
{"copy", no_argument, NULL, 'c' },
{"recursive", no_argument, NULL, 'r' },
{"overwrite", no_argument, NULL, 'o' },
{"help", no_argument, NULL, 'h' },
0
};
bool movingFiles=true; //movingFiles: false->copy, true->move
bool recursive=false;
bool forceOverwrite=false;
int c;
int optionIndex = 0;
while ((c = getopt_long (argc, argv, optstring, longOptions,&optionIndex)) != -1)
{
switch (c)
{
case 'h':
usage(argv[0]);
return 0;
case 'c':
movingFiles=false;
break;
case 'r':
recursive=true;
break;
case 'o':
forceOverwrite=true;
break;
case '?':
break;
default:
abort ();
}
}
if(argc-optind<2)
{
std::cout << "PTO_MOVE: You need to give at least a source and a destination project file or directory." << std::endl;
return 1;
};
try
{
fs::path p(argv[optind]);
if(fs::exists(p))
{
p=fs::absolute(p);
if(fs::is_directory(p))
{
// first parameter is a directory
fs::path pathTo;
if(!checkDestinationDirectory(std::string(argv[argc-1]), pathTo))
{
return 1;
};
// search all pto files in directory
pathVec projectFiles;
std::cout << "Searching project files in " << p << std::endl;
SearchPTOFilesInDirectory(projectFiles, p.string(), recursive);
if(projectFiles.size()==0)
{
std::cout << "No project files found in given directory " << p.string() << std::endl;
return 0;
};
std::cout << "Found " << projectFiles.size() << " project files." << std::endl << std::endl;
for(pathVec::const_iterator it=projectFiles.begin(); it!=projectFiles.end(); it++)
{
fs::path newPath;
if(RebaseFilename(*it, newPath, p.string(), pathTo.string()))
{
PTOCopyMove(movingFiles, *it, newPath, forceOverwrite);
};
};
}
else
{
if(argc-optind>2)
{
// several files given
// check if destination is a directory and create it if necessary
fs::path pathTo;
if(!checkDestinationDirectory(std::string(argv[argc-1]), pathTo))
{
return 1;
};
while(optind<argc-1)
{
p=fs::path(argv[optind]);
std::string ext=hugin_utils::toupper(p.extension().string());
// work only on pto files
if(ext==".PTO")
{
if(fs::exists(p) && fs::is_regular_file(p))
{
p=fs::absolute(p);
fs::path newPath = pathTo / p.filename();
PTOCopyMove(movingFiles, p, newPath, forceOverwrite);
}
else
{
std::cout << "WARNING: File " << p << " does not exists" << std::endl
<< "Skipping this file." << std::endl;
};
};
optind++;
};
}
else
{
// exactly 2 files given
fs::path pathTo(argv[argc-1]);
if(pathTo.extension().string().length()>0)
{
// user has given filename with extension
// so simply copy/move file
pathTo=fs::absolute(pathTo);
PTOCopyMove(movingFiles, p, pathTo, forceOverwrite);
}
else
{
// target is a directory
if(!checkDestinationDirectory(std::string(argv[argc-1]), pathTo))
{
return 1;
};
pathTo=pathTo / p.filename();
if(p==pathTo)
{
std::cerr << "ERROR: Target and destination file are the same." << std::endl
<< "Skipping file processing." << std::endl;
return 1;
};
PTOCopyMove(movingFiles, p, pathTo, forceOverwrite);
};
};
};
};
}
catch (const fs::filesystem_error& ex)
{
std::cout << ex.what() << std::endl;
}
return 0;
}