-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassTxtBancos.php
160 lines (142 loc) · 3.32 KB
/
ClassTxtBancos.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* class implementation to output markdown text from class file comments
*
* @author PICCORO Lenz McKAY <mckaygerhard@gmail.com>
* @copyright Copyright (c) 2019
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
*/
class ClassTxtBancos
{
private $filename = NULL;
private $txtheaders = NULL;
private $arraydata = NULL;
private $withHeader = FALSE;
private $txtfilter = NULL;
public $codestatus = 0;
function __construct($filename = NULL)
{
if ($filename and $filename != NULL)
{
$this->setHasHeader(TRUE);
$this->setFilename($filename);
}
$this->codestatus = 1;
}
public function getFilename()
{
return $this->filename;
}
public function getTxtHeaders()
{
return $this->headers;
}
public function getArraydata()
{
return $this->arraydata;
}
public function getNumRows()
{
$numRows = 0;
if ($this->arraydata)
$numRows = count($this->arraydata);
return $numRows;
}
public function setHasHeader($withHeader)
{
if (is_bool($withHeader))
$this->withHeader = $withHeader;
else
$this->withHeader = FALSE;
}
public function setFilename($filename, $mode = 'r')
{
if (file_exists($filename) AND $mode == 'r')
{
$this->filename = $filename;
$this->txtReadHead();
$this->txtReadData();
return;
}
else if (file_exists($filename) AND $mode == 'w')
{
$this->filename = $filename;
$this->txtWritHead(); // TODO: txt bancos casi nunca traen cabecera
$this->txtWritData(); // TODO set txtHeadWrite(array)
return;
}
else
{
$this->codestatus = 3;
}
}
private function txtReadHead()
{
$handle = @fopen($this->filename, "r");
if ($handle)
{
$firstRow = fgets($handle, 4096);
if ($this->withHeader)
{
$this->headers = preg_split('/(?:\s\s+|\n|\t)/', $firstRow, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
}
else
{
$this->headers = preg_split('/(?:\s\s+|\n|\t)/', $firstRow, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
$numFields = count($this->headers);
for ($x = 0; $x < $numFields; $x++)
{
$this->headers[$x][0] = "Column" . ($x + 1) ;
}
}
fclose($handle);
$this->codestatus = 2;
}
else
{
$this->headers = NULL;
$this->codestatus = 4;
}
}
private function txtReadData()
{
$this->arraydata = array();
$handle = fopen($this->filename, "r");
if ($handle)
{
if ($this->withHeader)
{
$firstRow = fgets($handle, 4096);
}
while (($buffer = fgets($handle, 4096)) !== false)
{
$addThisLine = true;
$fields = preg_split('/(?:\s\s+|\n|\t)/', $buffer, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
if ( count($fields) != count($this->headers) )
{
$this->codestatus = 6;
return;
}
if ($addThisLine === true)
{
$numFields = count($this->headers);
$fieldLength = strlen($buffer);
$rowData = Array();
for ($x = $numFields - 1; $x >= 0; $x--)
{
$fieldLength = $fieldLength - $this->headers[$x][1];
$rowData[$this->headers[$x][0]] = rtrim(substr($buffer, $this->headers[$x][1], $fieldLength));
$fieldLength = $this->headers[$x][1];
}
$this->arraydata[] = $rowData;
}
}
if (!feof($handle))
{
$this->codestatus = 8;
}
fclose($handle);
}
}
}
?>