-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffman_demo.rb
77 lines (61 loc) · 1.66 KB
/
huffman_demo.rb
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
require 'sinatra'
load 'huffman.rb'
enable :sessions
get '/' do
erb :index
end
get '/tree/:filename' do
setup_file(params[:filename])
erb :tree
end
get '/encode/:filename' do
if setup_file(params[:filename])
@encoded_bits = @huffman.encode_bytes(@original_bytes)
@current_info[:bit_length] = @encoded_bits.join.length
end
erb :encode
end
get '/reset' do
session.clear
redirect '/'
end
get '/debug' do
erb :debug
end
not_found do
@error = "URL '#{request.path_info}' not found"
erb :index
end
helpers do
def setup_file(filename)
if @current_info = files[@current_filename = filename]
@huffman = Huffman.new
begin
@original_bytes = @huffman.update_counts_from_file(@current_info[:path])
@current_info[:symbol_count] = @huffman.node_lookup.compact.length
@current_info[:entropy] = @huffman.entropy
rescue
@huffman = nil
@error = $!.to_s
end
else
@error = "Filename '#{@current_filename}' not found"
end
@huffman
end
def files
session[:files] ||= Dir['files/*'].inject({}){|hash,filename| (stat = File.stat(filename)).directory? ? hash : (hash[File.basename(filename)] = {:path => filename,:size => stat.size}) && hash}
end
def commafy(s)
s.to_s.reverse.scan(%r/.{1,3}/).join(',').reverse
end
def cycle(*values)
values[(@cycle_index = (@cycle_index || 0) + 1) % values.length] # NOTE - not checking for divide-by-zero
end
def min_format(count)
"%0.#{[count.to_s.length,1].max}f"
end
def truncate(value)
value.to_s.split(' ').collect{| string | string.length > 128 ? string[0..127] + " ... (truncated)" : string}.join(' ')
end
end