-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·76 lines (58 loc) · 1.8 KB
/
index.php
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
<?php
include_once 'includes/HuffmanNode.inc';
include_once 'includes/Huffman.inc';
include_once 'includes/ClasseAuxiliar.inc';
include_once 'vendor/autoload.php';
use TrabalhoGA\Includes\ClasseAuxiliar;
use TrabalhoGA\Includes\Huffman;
\Slim\Slim::registerAutoloader();
// Definindo Twig.
$loader = new Twig_loader_Filesystem('templates');
$twig = new Twig_Environment($loader);
// Iniciando APP com Slim.
$app = new \Slim\Slim();
define('FILE_PATH', 'texto.txt');
/**
* Front Page
*/
$app->get('/', function() use ($app, $twig){
echo $twig->render('index.html');
});
/**
* Página de decodificação.
*/
$app->get('/decode', function() use ($app, $twig) {
$codifyString = '';
$codifyFile = new \SplFileObject('codify.txt');
while (!$codifyFile->eof()) {
$line = $codifyFile->fgetss();
$codifyString .= $line;
}
$classe_auxiliar = new ClasseAuxiliar(FILE_PATH);
$decodeFile = $classe_auxiliar->decodeFile($codifyString);
echo $twig->render('decode.html', array('body' => $decodeFile, 'razao' => $classe_auxiliar->getRazao()));
});
/**
* Página de codificação.
*/
$app->get('/encode', function () use ($app, $twig) {
$classe_auxiliar = new ClasseAuxiliar(FILE_PATH);
$dicionary = $classe_auxiliar->getDicionary();
$huffman = new Huffman($dicionary);
$huffman->buildTree();
$huffman->codify($huffman->getRoot());
$root = $huffman->getRoot();
$codify_array = $huffman->getCodify();
$codifyFile = $classe_auxiliar->codifyFile($codify_array, $dicionary);
$name = 'codify.txt';
$file = fopen($name, 'w');
fwrite($file, $codifyFile);
fclose($file);
echo $twig->render('encode.html', array(
'arquivo' => $codifyFile,
'tamanho' => $classe_auxiliar->getValorMedio($codify_array),
'dicionary' => $codify_array,
));
});
// Rodando o aplicativo slim.
$app->run();