-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo1.php
70 lines (60 loc) · 2.15 KB
/
demo1.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
<?php
//
// at CRP's project root, `php demo/demo1.php | more`
// USAGE:
// php src/demo1.php
// php src/demo1.php 1 > part1_OfDemo1Htm
// php src/demo1.php 2 > part2_OfDemo1Htm
//
include('convert.php');
$f = 'data/CEP-to-CRP.csv';
$tab = NULL;
$modeJs = isset($argv[1])? $argv[1]: 0;
if (file_exists($f))
$tab = array_map('str_getcsv', file($f));
else
die("\nERRO $f não existe.\n");
$head = array_shift($tab);
$c = new CRPconvert();
$n=0;
foreach($tab as $r) {
$a = array_combine($head,$r);
$x = trim($a['CRP-from']);
if ($modeJs && $x) { // ideal gerando por jQuery... mas por hora Jvascript puro e simples.
$msg = '';
$n++;
for($i=0;$i<3;$i++) {
if ($i==0) {$input = $a['CRP-from']; $out = $a['CEP-from'];} //from
elseif ($i==2) {$input = $a['CRP-to']; $out = $a['CEP-to'];} // to
else { // middle
$c->set($a['CRP-from']); $from = $c->crp_int;
$c->set($a['CRP-to']);
$mid = round( ($c->crp_int+$from)/2 ) + rand(1, 999) - 499;
//echo "\n .. debug22: {$c->crp_int}+$from ".( round( ($c->crp_int+$from)/2 ));
$c->setPart( $mid ); $input = $c->crp; $out = $c->asCEP();
}
$sep = $i? "; ": '';
if ($modeJs==1) // CRP to CEP
$msg .= "$sep$input=<script>document.write( assertMsg(cc.asCEP('$input'),'$out') )</script>"; // or <span>$input</span>
else // CEP to CRP
$msg .= "$sep$out=<script>document.write( assertMsg(cc.set('$out').crp,'$input') )</script>";
} // for
echo "\n<li>$msg</li>"; //or "\n<li id=\"x$modeJs-$n\">$msg</li>";
} elseif (!$modeJs && $x) { // CEP to
$c->set($x);
$cep = $c->asCEP();
assertRocks($cep==$a['CEP-from'], "i=$x: CRP({$a['CEP-from']})={$c->crp} =ctx+{$c->crp_int} (CEP1 $cep)");
$x =$a['CEP-to'];
$c->set($x);
$cep = $c->asCEP();
assertRocks($c->asCEP()==$a['CEP-to'], "i=$x: CRP({$a['CEP-to']})={$c->crp} =ctx+{$c->crp_int} (CEP2 $cep)");
$c->set($a['CEP-from']);
assertRocks($c->asCEP()==$a['CEP-from'],"any({$a['CEP-from']})={$c->crp} =ctx+{$c->crp_int}");
print "\n";
} // if
} // for
/// LIB:
function assertRocks($x,$msg=''){
if ($x) echo "\n-- ASSERT: $msg.";
else echo "\n-- assert ERROR at '$msg'.";
}