-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
138 lines (122 loc) · 3.98 KB
/
main.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
/*
* File: main.cpp
* Author: Jay Sprenkle
*
* Created on May 26, 2015
*
* A command line program to rename/move files.
*
* A source file name and destination file name may be provided on the command line to
* copy a single file.
*
*
* expected command line inputs:
* cpXmlCmd [-l outputdocument] ExistingFileName NewFileName
*
* Or
*
* An XML document containing many copy operations may be provided.
* The program does not load the entire XML document into memory so it may be
* used in minimal resource systems with huge lists.
*
* expected command line inputs:
* cpXmlCmd [-l outputdocument] XMLDocumentWithOneOrMoreFilesToCopy
*
* The option switch(es) may be placed anywhere on the command line.
*
* The program returns a non zero exit code in case of a failure.
* Optionally produces an XML document that lists the status of the operation(s).
* By default the status output document is not produced.
* If the output document is requested but no file name is provided then
* the document will be named "cpXmlCmd.result.xml"
*
*/
#include <iostream>
#include <stdexcept>
#include "cpXmlCmdApplication.h"
/*
* program entry point
*/
int main( int argc, char** argv )
{
int rc = 1;
::std::string InputFileName;
::std::string OutputFileName;
::std::string ResultDocumentFileName;
bool ProduceResultDocument = false;
try
{
// <editor-fold defaultstate="collapsed" desc="command line argument parsing">
for ( int i = 1; i < argc; ++i )
{
::std::string arg( argv[i] );
// todo: allow "-lfilename" as an option
// if '-l' then Produce Result Document
if ( arg == "-l" )
{
ProduceResultDocument = true;
ResultDocumentFileName.clear();
continue;
}
if ( ProduceResultDocument && ResultDocumentFileName.empty() )
{
ResultDocumentFileName = arg;
continue;
}
// input file name first
if ( ! arg.empty() && InputFileName.empty() )
{
InputFileName = arg;
continue;
}
// output file name next
if ( ! arg.empty() && OutputFileName.empty() )
{
OutputFileName = arg;
continue;
}
// something unrecognized
throw ::std::runtime_error( ::std::string( "Invalid command line parameter: " ) + arg );
}
if ( InputFileName.empty() )
throw ::std::runtime_error( ::std::string( "No command line parameters. Expected a single command list file name or old and new file names" ) );
// if no output document name given then use default
if ( ProduceResultDocument && ResultDocumentFileName.empty() )
ResultDocumentFileName = "cpXmlCmd.result.xml";
// </editor-fold>
// pass in a file name if an output document is required
cpXmlCmdApplication app( ProduceResultDocument ? ResultDocumentFileName.c_str() : 0, argc, argv );
if ( ! InputFileName.empty() && ! OutputFileName.empty() )
{
const char* destination = OutputFileName.c_str();
app.initialize();
app.validate( destination );
if ( app.copy( InputFileName.c_str(), destination) )
rc = 0;
}
else
{
app.initialize();
if ( app.process( InputFileName.c_str() ) )
rc = 0;
}
}
// <editor-fold defaultstate="collapsed" desc="Error handling">
catch ( ::std::runtime_error ex )
{
::std::cerr << "Run time exception thrown.\n" << ex.what() << "\n";
rc = 2;
}
catch ( ::std::exception ex )
{
::std::cerr << "Exception thrown.\n" << ex.what() << "\n";
rc = 2;
}
catch ( ... )
{
::std::cerr << "Unknown error\n";
rc = 3;
}
// </editor-fold>
return rc;
}