-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogArgs.cpp
More file actions
159 lines (138 loc) · 3.83 KB
/
Copy pathprogArgs.cpp
File metadata and controls
159 lines (138 loc) · 3.83 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
#include "progArgs.h"
const char szSyntax [] =
"Syntax: ./patch [flags] oldfile.ext newfile.ext [file.pat]\n"
" flags: -c - create patch\n"
" -a - apply patch\n"
" -f - force overwrite\n"
" -i - ignore timestamps\n"
" -s - print stats\n"
" -q - quiet mode\n"
" -v - verbose mode\n"
" -d - disable I/O buffering\n"
" -x - maximize compression\n";
const char szCopyRight []= "Patch maker utility. (c) Yuriy Yakimenko. 1997-2020\n";
static void outputSyntax (bool showAuthor = false)
{
if (showAuthor)
printf ("%s", szCopyRight);
printf ("%s", szSyntax);
}
int progArguments::parseArguments (int argc, char *argv[])
{
bool fileNameSet = false;
char combined_flags[32] = { 0 };
if (argc == 1)
{
outputSyntax (true);
return -1;
}
for (int i = 1; i < argc; i++)
{
if (strncmp (argv[i], "-", 1) == 0) // flags
{
if (fileNameSet)
{
outputSyntax ();
return -1;
}
memset (combined_flags, 0, sizeof (combined_flags));
strncpy (combined_flags, argv[i], sizeof (combined_flags) - 1);
for (int j=1; combined_flags[j]; j++)
{
char flag = combined_flags[j];
if (flag == 'c')
{
flagCreate = true;
}
else if (flag == 'a')
{
flagApply = true;
}
else if (flag == 'f')
{
flagForce = true;
}
else if (flag == 'd')
{
flagDiskIO = true;
}
else if (flag == 'q')
{
flagQuiet = true;
}
else if (flag == 'v')
{
flagVerbose = true;
}
else if (flag == 'i')
{
flagIgnoreTimestamps = true;
}
else if (flag == 's')
{
flagShowStats = true;
}
else if (flag == 'x')
{
flagMaximizeCompression = true;
}
else
{
fprintf (stderr, "Unknown flag -%c\n", flag);
outputSyntax ();
return -1;
}
}
}
else // file name
{
fileNameSet = true;
if (file1 == nullptr)
{
file1 = strdup (argv[i]);
}
else if (file2 == nullptr)
{
file2 = strdup (argv[i]);
}
else if (file3 == nullptr)
{
file3 = strdup (argv[i]);
}
}
}
if (flagQuiet && flagVerbose) // inconsistent args
{
fprintf (stderr, "Cannot combine -v and -q flags\n");
return -1;
}
if (flagApply && flagCreate) // inconsistent args
{
fprintf (stderr, "Cannot combine -a and -c flags\n");
return -1;
}
if (!flagApply && !flagCreate) // inconsistent args
{
fprintf (stderr, "-a and -c flags are missing\n");
outputSyntax ();
return -1;
}
if (file1 == nullptr || file2 == nullptr)
{
fprintf (stderr, "Input file names not set\n");
return -1;
}
if (strcmp (file1, file2) == 0)
{
fprintf (stderr, "Two input files are the same\n");
return -1;
}
if (file3 == nullptr)
{
if (0 != this->CreatePatchFileName())
{
return -1;
}
}
return 0;
}