-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
89 lines (82 loc) · 2.51 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdexcept>
#include <thread>
#include "operation.h"
#include "encode.h"
#include "decode.h"
#define defaultBlockSize 50000
#define defaultThreadsQuantity 1
using namespace std;
void help();
char* programName;
int main(int argc, char *argv[])
{
programName = argv[0];
if (argc != 5 && argc != 6) help();
int threadsQuantity = atoi(argv[4]);
if (threadsQuantity < 0) help();
if (!threadsQuantity)
{
threadsQuantity = thread::hardware_concurrency();
if (!threadsQuantity)
{
threadsQuantity = defaultThreadsQuantity;
fputs("The number of threads is not set and can not be computed.", stderr);
fprintf(stderr, "%i worker threads will be used.\n", defaultThreadsQuantity);
}
}
Operation *op = NULL;
if (!strcmp(argv[1], "-e"))
{
int blockSize = defaultBlockSize;
if (argc == 6)
{
blockSize = atoi(argv[5]);
if (blockSize <= 0) help();
}
Encode *encodeOp = new Encode(argv[2], argv[3], threadsQuantity, blockSize);
op = encodeOp;
}
else if (!strcmp(argv[1], "-d"))
{
Decode *decodeOp = new Decode(argv[2], argv[3], threadsQuantity);
op = decodeOp;
}
else help();
try
{
// К этому моменту op всегда инициализирована, иначе программа уже завершилась по help().
op->performOperation();
}
catch (const length_error& err)
{
fprintf(stderr, "Error reading from file: %s\n", err.what());
}
catch (const logic_error& err)
{
fputs(err.what(), stderr);
}
delete op;
return 0;
}
/**
* Выводит help и завершает работу программы.
*/
void help()
{
printf("usage: bzip -e inFile outFile threadsQuantity [blockSize]\n");
printf(" bzip -d inFile outFile threadsQuantity\n\n");
puts("positional arguments:");
puts(" -e compress file");
puts(" -d decompress file");
puts(" inFile input file");
puts(" outFile output file");
puts(" threadsQuantity number of worker threads. If 0 the program will try");
puts(" to determine number of logical processors.");
puts("");
puts("optional positional arguments:");
puts(" blockSize int, >0, default 50000");
exit(0);
}