-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASCII.cpp
93 lines (70 loc) · 1.87 KB
/
ASCII.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
#include "ASCII.h"
ASCII::ASCII(std::string fileName_)
{
fileName = fileName_;
}
bool ASCII::convert()
{
bool taskCompleted = false;
if(isConverted())
{
std::cout << "File is already converted to ASCII." << std::endl;
}
else
{
FileHandler file1(fileName, READ);
std::string fileData = file1.read();
file1.closeFile();
FileHandler file2(fileName, WRITE); // deletes the contents of the file
file2.closeFile();
std::string buffer = "";
for(char c : fileData)
{
int character = c;
std::string number = std::to_string(character);
buffer += number + " ";
}
FileHandler file3(fileName, APPEND);
file3.append(buffer);
file3.closeFile();
taskCompleted = true;
}
return taskCompleted;
}
bool ASCII::isConverted()
{
bool isConverted = false;
FileHandler file(fileName, READ);
std::string lineData = file.readLine(1);
file.closeFile();
return isConverted;
}
bool ASCII::revert()
{
bool taskCompleted = false;
FileHandler file1(fileName, READ);
std::string data = file1.read();
file1.closeFile();
std::string bufferedLetter = "";
std::string bufferedFile = "";
for(char c : data)
{
if(c != (char) 32)
{
bufferedLetter += c;
}
else
{
int charNumber = std::stoi(bufferedLetter);
char letter = charNumber;
bufferedFile += letter;
bufferedLetter = "";
}
}
FileHandler file2(fileName, WRITE);
file2.closeFile();
FileHandler file3(fileName, APPEND);
file3.append(bufferedFile);
file3.closeFile();
return taskCompleted;
}