-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
105 lines (85 loc) · 2.07 KB
/
helper.py
File metadata and controls
105 lines (85 loc) · 2.07 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
from sanatize import *
'''
Encode
Take in user file names for the ouput and input files,
then enocde text using the run algorithim
'''
def encode(inputFile, outputFile):
normalText = 0
totalBuffer = ""
fileIn = open(inputFile, "r")
fileOut = open(outputFile,"w")
fileIn.flush()
fileOut.flush()
#Check if text is valid
if(not(checkEncoding(inputFile))):
print("ERROR: File contains invalid characters")
return
print("\nEncoding in progress ...")
#Encode each line while adding into the output file
buffer = ""
tempChar = ""
for line in fileIn:
run = 0
index = 0
#Check for one character
if(len(line) == 1 and line != " "):
totalBuffer += line
fileOut.write("1" + line)
normalText += 1
elif(line):
for char in line:
index += 1
run += 1
normalText += 1
if(index >= len(line)):
#Last character
if(line[index - 1] == "\n"):
normalText -= 1
break
buffer += str(run) + char
run = 0
elif(char != line[index]):
buffer += str(run) + char
run = 0
totalBuffer += buffer
fileOut.write(buffer + "\n")
buffer = ""
fileIn.close()
fileOut.close()
"Note compression ratio does not include new line characters"
print("Encoding completed ...")
print("Compression Ratio: " + str(normalText) + "/" + str(len(totalBuffer)))
'''
Decode
Take in user file names for the ouput and input files,
then decode text using the run algorithim
'''
def decode(inputFile, outputFile):
#Check if decoding scheme is valid
if(not(checkDecoding(inputFile))):
return
fileIn = open(inputFile, "r")
fileOut = open(outputFile,"w")
print("\nDecoding in progress ...")
count = 0
buffer = ""
fileIn.flush()
fileOut.flush()
for line in fileIn:
i = 0
for char in line:
if(char == "\n"):
buffer += "\n"
count -= 1
elif((count % 2 == 0 and count > 1) or count == 0):
#check for last character
while(i < int(char)):
buffer += line[count + 1]
i += 1
i = 0
count += 1
fileOut.write(buffer)
buffer = ""
count = 0
print("Decoding completed ...")