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
<?php
/**
 *  bug #4295  csv file is not well closed
 *
 *  @link  http://pear.php.net/bugs/bug.php?id=4295
 */

require_once 'File/CSV.php';

function dump(&$var, $title = null, $hex = false)
{
    if (isset($title)) {
        echo "<h2>$title</h2>";
    }
    echo '<pre>';
    if ($hex) {
        var_dump(bin2hex($var));
    } else {
        var_dump($var);
    }
    echo '</pre>';
}

//dump(get_included_files());

$filename = 'LISTE_REG.txt';

function searchReg($regKey)
{
    global $filename, $conf;
    $data = array();

    while ($r = File_CSV::read($filename, $conf)) {
        if (trim($r[0]) != $regKey) {
             continue;
        }
        $data[] = array('COD_REG' => trim($r[0]), 'LIB_REG' => trim($r[1]));
        break;
    }
    File::close($filename, FILE_MODE_READ);
    return $data;
}

// 1. content of csv file
$conf = array('fields' => 3, 'sep' => ';', 'crlf' => "\r\n");
dump($conf['crlf'], "crlf used to read file $filename : \"\\r\\n\" (hexa value follow)", true);
$data = file_get_contents($filename);
dump($data);


// 2. search for first region 11 - ile-de-france
$conf = array('fields' => 3, 'sep' => ';', 'crlf' => "\n\r");
dump($conf['crlf'], "crlf used to read file $filename : \"\\n\\r\" (hexa value follow)", true);
$reg = 11;
$data = searchReg($reg);
echo "<h2>Search result of region $reg</h2>";
dump($data);
$filePointers = &PEAR::getStaticProperty('File', 'filePointers');
dump($filePointers, 'file pointers stack after File::close calls');

// 3. search for second region 54 - poitou-charente
$conf = array('fields' => 3, 'sep' => ';', 'crlf' => "\15\12");
dump($conf['crlf'], "crlf used to read file $filename : \"\\15\\12\" (hexa value follow)", true);
$reg = 54;
$data = searchReg($reg);
echo "<h2>Search result of region $reg</h2>";
dump($data);
$filePointers = &PEAR::getStaticProperty('File', 'filePointers');
dump($filePointers, 'file pointers stack after File::close calls');

// 4. search for third region 22 - picardie
$conf = array('fields' => 3, 'sep' => ';', 'crlf' => '\r\n');
dump($conf['crlf'], "crlf used to read file $filename : '\\r\\n' (hexa value follow)", true);
$reg = 22;
$data = searchReg($reg);
echo "<h2>Search result of region $reg</h2>";
dump($data);
$filePointers = &PEAR::getStaticProperty('File', 'filePointers');
dump($filePointers, 'file pointers stack after File::close calls');
?>