-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.cpp
More file actions
139 lines (136 loc) · 4.09 KB
/
Core.cpp
File metadata and controls
139 lines (136 loc) · 4.09 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
#include "Core.h"
Core::Core() : numberwords(0){}
void Core::getCommand(SessionsManager& smanager) {
std::getline(std::cin, line);
numberwords = countWords();
}
int Core::countWords() {
if (line[0] != '\0') {
int count = 1;
for (int i = 0; line[i] != '\0'; i++)
{
if (line[i] == ' ')
count++;
}
return count;
}
throw std::exception("For information about the commands, type: help");
}
std::string Core::getFirstWord() {
int pos = line.find(' ');
std::string sub = line.substr(0, pos);
return sub;
}
std::string Core::getSecondWord() {
int pos1 = line.find(' ');
std::cout << pos1 << '\n';
int pos2 = line.find('\0');
std::cout << pos2 << '\n';
std::string sub = line.substr(pos1 + 1,-1 - pos1);
return sub;
}
std::string Core::getIndexWord(int i) {
std::string res = line;
assert(i >0);
while (i) {
if (i == 1) {
int pos = res.find(' ');
res = res.substr(0, pos);
return res;
}
else {
int pos = res.find(' ');
res = res.substr(pos + 1);
i--;
}
}
}
void Core::readCommand(SessionsManager& sm) {
if (numberwords == 1) {
if (line == "monochrome")
command.monochrome(sm);
else if (line == "negative")
command.negative(sm);
else if (line == "grayscale")
command.grayscale(sm);
else if (line == "undo")
command.undo(sm);
else if (line == "save")
command.save(sm);
else if (line == "exit")
command.exitProgram();
else if (line == "help")
command.help();
else if (line == "close")
command.close(sm);
else std::cout << "Wrong command!\n";
}
else if (numberwords == 2) {
// std::string first = getFirstWord();
std::string first = getIndexWord(1);
//std::string second = getSecondWord();
std::string second = getIndexWord(2);
if (first == "load") {
Image* i = createImage(second);
command.load(sm, i);
}
else if (first == "add") {
Image* i = createImage(second);
command.add(sm, i);
}
else if (first == "saveas") {
command.saveAs(sm, second);
}
else if (first == "rotate")
{
if (second == "right")
command.rotateRight(sm);
else if (second == "left")
command.rotateLeft(sm);
}
else if (first == "switch") {
int id = std::stoi(second);
command.switchSession(sm, id);
}
else if (first == "session" && second == "info")
command.sessionInfo(sm);
else std::cout << "Wrong command!\n";
}
else std::cout << "Wrong command!\n";
}
Image* Core::createImage(std::string& _name) {
std::ifstream in(_name);
if (!in.good())
throw std::exception("Something is wrong with the file.");
Image* image;
std::string magicNumber;
in >> magicNumber;
if (magicNumber == "P1") {
unsigned int width;
unsigned int height;
in >> width >> height;
image = new PBM("P1", width, height, _name);
image->readPixels(in);
return image;
}
else if (magicNumber == "P2" ) {
unsigned int width;
unsigned int height;
unsigned int maxColor;
in >> width >> height >> maxColor;
image = new PGM("P2", width, height, _name, maxColor);
image->readPixels(in);
return image;
}
else if (magicNumber == "P3") {
unsigned int width;
unsigned int height;
unsigned int maxColor;
in >> width >> height >> maxColor;
image = new PPM("P3", width, height, _name, maxColor);
image->readPixels(in);
return image;
}
else
throw std::exception("Incorrect image type");
}