-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO.cpp
More file actions
145 lines (114 loc) · 3.18 KB
/
Copy pathIO.cpp
File metadata and controls
145 lines (114 loc) · 3.18 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
//
// Created by Mindaugas on 2020-09-27.
//
#include "IO.h"
#include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <sstream>
IO::IO(){
std::string fileKey = "F";
std::string readKey = "R";
std::cout << "Kad nuskaityti is failo, iveskite [" + fileKey + "]. Kad nuskaityti ranka iveskite [" + readKey + "]. \n";
char response = std::cin.get();
while (!CheckResponse(readKey[0], fileKey[0], response)){
//std::fflush(stdin);
response = std::cin.get();
}
};
IO::IO(std::string filename) {
std::cout << "Hasuojamas failas: " << filename << std::endl;
ReadFile(filename);
}
bool IO::CheckResponse(char a, char b, char response){
if(response == a){
ReadScreen();
return true;
} else if(response == b){
RequestFile();
return true;
} else {
std::cin.clear();
std::cin.ignore(256,'\n');
std::cout << "Iveskite is naujo! [F]ailas arba ivesti [R]anka \n";
return false;
}
};
void IO::ReadFile(std::string fileName){
std::ifstream fd;
fd.open(fileName);
do{
try
{
if(fd.fail())
{
throw std::runtime_error("Nepavyko atidaryti failo! [Enter]\n");
}
}
catch (const std::runtime_error& e)
{
std::cout << e.what();
fileName = "";
std::cin.ignore(256,'\n');
std::cout<<"Iveskite dar karta failo pavadinima: ";
std::cin >> fileName;
fd.open(fileName);
}
} while(fd.fail());
this->input.assign( (std::istreambuf_iterator<char>(fd) ),
(std::istreambuf_iterator<char>() ));
fd.close();
}
void IO::RequestFile(){
std::string fileName;
std::cout << "Iveskite failo pavadinima: ";
std::cin >> fileName;
ReadFile(fileName);
}
std::string IO::ReadScreen(){
std::cout << "Iveskite teksta kuri norite paversti i hasha \n";
std::string input_;
std::cin.clear();
std::cin >> input_;
this->input = input_;
return input_;
}
std::string IO::Output(std::vector<uint8_t> bytes) {
std::stringstream Q;
std::cout << "Sugeneruotas hash kodas: " << std::endl;
for(uint8_t a : bytes){
Q << std::hex << static_cast<int>(a);
}
std::cout << Q.str() << std::endl;
return Q.str();
}
std::string IO::getInput() {
if(input.length() > 0) return input;
else return "";
}
std::vector<std::string> IO::ReadFileWithLines(std::string filename) {
std::ifstream i(filename);
do {
try {
if(i.fail())
{
throw std::runtime_error("Nepavyko atidaryti failo! [Enter]\n");
}
} catch (const std::runtime_error& error) {
std::cout << error.what();
filename = "";
std::cin.ignore(256,'\n');
std::cout<<"Iveskite dar karta failo pavadinima: ";
std::cin >> filename;
i.open(filename);
}
} while (i.fail());
//skaitome
std::vector<std::string> lines;
std::string line;
while(std::getline(i, line)){
lines.push_back(line);
}
return lines;
}