File: //home/bk/efi/eficenter.ru/inc/class.ParseCSVFormat.php
<?php
class ParseCSVFormat {
var $aParsingFile = NULL;
var $outFields = NULL;
var $aFileDescriptor = NULL;
var $aStruct = NULL;
var $aDefReadStrings = 1000;
var $aMaxLenString = 100000;
var $aBufferStrings = array();
var $aParceFields = array();
var $aSeparator = ';';
var $aCurrentTable = NULL;
function ParseCSVFormat($struct = NULL, $file = NULL) {
$this->aStruct = &$struct;
$this->aParsingFile = $file;
$this->aFileDescriptor = fopen($this->aParsingFile['tmp_name'], 'r');
}
function doExportParse($table = NULL, $arr = NULL) {
if ($table && $arr) {
$aCurTable = NULL;
$struct = &$this->aStruct;
$marker = $struct->getLineMarker($table);
$separator = $this->getSeparator();
for ($i = 0, $j = sizeof($arr); $i < $j; $i++) {
if ($aCurTable != $table) {
$aTableStruct = $struct->getTable($table);
$aCurTable = $table;
}
if ($aTableStruct['cdbtree'] && $arr[$i][$aTableStruct['key_of_table'][1]] != 0 && $arr[$i][$aTableStruct['key_of_table'][1]] != '') {
$string .= $marker.$separator.str_replace("\r\n", '', implode($separator, $arr[$i]))."\r\n";
} else if (!$aTableStruct['cdbtree']) {
$string .= $marker.$separator.str_replace("\r\n", '', implode($separator, $arr[$i]))."\r\n";
}
}
return $string;
}
return FALSE;
}
function doInputParse() {
if ($this->aStruct) {
$struct = &$this->aStruct;
if (!$this->aCurrentTable) {
$this->aCurrentTable = $struct->getNextTable();
$this->aBufferStrings = NULL;
$this->aParceFields = NULL;
$this->outFields = NULL;
$this->readCSVFile(TRUE);
$this->parseFields();
} else if ($this->aCurrentTable && !feof($this->aFileDescriptor)) {
$this->aBufferStrings = NULL;
$this->aParceFields = NULL;
$this->outFields = NULL;
$this->readCSVFile();
$this->parseFields();
} else if ($this->aCurrentTable && feof($this->aFileDescriptor)) {
$this->aCurrentTable = $struct->getNextTable();
$this->aBufferStrings = NULL;
$this->aParceFields = NULL;
$this->outFields = NULL;
$this->readCSVFile(TRUE);
$this->parseFields();
}
return $this->aCurrentTable;
}
}
function parseCSVString($buffer = NULL)
{
$buffer = str_replace('""', '"', $buffer);
$n = strlen($buffer);
$i = 0;
$del = false;
$data = array();
$str = '';
while($i < $n) {
$part = substr($buffer, $i);
if ((substr($part, 0, 1) == ';' && !$del)
|| (substr($part, 0, 2) == '";' && $del)
) {
$i++;
if ($del) {
$str = substr($str, 1, strlen($str) - 1);
$i++;
}
$data[] = $str;
$del = false;
$str = '';
} else {
if ($part[0] == '"') {
$del = true;
}
$str .= $part[0];
$i++;
}
}
if ($str) $data[] = $str;
return $data;
/*
if ($buffer) {
$aFieldsPos = array();
$level = 0;
$flag_1 = FALSE;
$flag_2 = FALSE;
$flag_3 = FALSE;
$flag_4 = FALSE;
for ($ii = 0, $jj = strlen($buffer); $ii < $jj; $ii++) {
if (';' == $buffer[$ii]) {
if ($flag_3 && $level > 0) {
$flag_4 = TRUE;
$flag_1 = TRUE;
$level--;
} else if ($flag_3 && $level == 0) {
$flag_4 = TRUE;
} else {
$flag_1 = TRUE;
}
} else if ('"' == $buffer[$ii]) {
if ($flag_1) {
$flag_2 = TRUE;
$level++;
} else {
$flag_3 = TRUE;
}
} else {
$flag_1 = FALSE;
$flag_2 = FALSE;
$flag_3 = FALSE;
$flag_4 = FALSE;
}
if ($flag_1 && $flag_2) {
$flag_1 = FALSE;
$flag_2 = FALSE;
$aFieldsPos[sizeof($aFieldsPos) - 1] = array('escaped' => $aFieldsPos[sizeof($aFieldsPos) - 1]);
} else if ($flag_3 && $flag_4) {
$flag_3 = FALSE;
$flag_4 = FALSE;
$aFieldsPos[] = $ii;
} else if ($flag_1 && $level == 0) {
$aFieldsPos[] = $ii;
}
}
for ($i = 0, $j = sizeof($aFieldsPos); $i < $j; $i++) {
if ($i == 0) {
$fields[] = substr($buffer, 0, 2);
}
if (($i + 1) < $j) {
if (is_array($aFieldsPos[$i])) {
if (is_array($aFieldsPos[($i + 1)])) {
$l = $aFieldsPos[($i + 1)]['escaped'];
} else {
$l = $aFieldsPos[($i + 1)];
}
$len = (($l - $aFieldsPos[$i]['escaped']) - 3);
$fields[] = substr($buffer, ($aFieldsPos[$i]['escaped'] + 2), $len);
} else {
if (is_array($aFieldsPos[($i + 1)])) {
$l = $aFieldsPos[($i + 1)]['escaped'];
} else {
$l = $aFieldsPos[($i + 1)];
}
$len = (($l - $aFieldsPos[$i]) - 1);
$fields[] = substr($buffer, ($aFieldsPos[$i] + 1), $len);
}
} else {
if (is_array($aFieldsPos[$i])) {
$str = substr($buffer, (($aFieldsPos[$i]['escaped'])));
$fields[] = substr($buffer, $aFieldsPos[$i]['escaped'] + 2, strlen($str) - 5);
} else {
$fields[] = substr($buffer, ($aFieldsPos[$i] + 1));
}
}
}
return $fields;
}
return FALSE;
*/
}
function readCSVFile($aEndOfFile = FALSE) {
if ($this->aCurrentTable) {
if ($aEndOfFile) {
fseek($this->aFileDescriptor, 0);
}
for ($i = 0; $i < $this->aDefReadStrings && !feof($this->aFileDescriptor); $i++) {
$buffer = fgets($this->aFileDescriptor, $this->aMaxLenString);
$fields = $this->parseCSVString($buffer);
$this->aBufferStrings[] = $fields;
//print_r($fields); echo "<hr>";
}
/*
$line = 0;
while ($line < $this->aDefReadStrings
&& $fields = fgetcsv($this->aFileDescriptor, $this->aMaxLenString, $this->aSeparator) ) {
$this->aBufferStrings[] = $fields;
}
*/
return TRUE;
}
return FALSE;
}
function getStruct() {
return $this->aStruct;
}
function parseFields() {
if (sizeof($this->aBufferStrings) > 0) {
for ($i = 0, $j = sizeof($this->aBufferStrings); $i < $j; $i++) {
if ($this->aBufferStrings[$i][0] == $this->aCurrentTable[0]['marker']) {
$this->aParceFields[] = $this->aBufferStrings[$i];
}
}
for ($i = 0, $j = sizeof($this->aParceFields); $i < $j; $i++) {
$buffer = NULL;
$buffer['marker'] = $this->aCurrentTable[0]['marker'];
$buffer['updateable'] = $this->aCurrentTable[0]['updateable'];
$buffer['key_of_table'] = $this->aCurrentTable[0]['key_of_table'];
$buffer['cdbtree'] = $this->aCurrentTable[0]['cdbtree'];
$buffer['table'] = $this->aCurrentTable[1];
for ($ii = 1, $jj = sizeof($this->aParceFields[$i]); $ii < $jj; $ii++) {
if ($buffer['cdbtree']) {
$buffer['fields'][$this->aCurrentTable[0]['fields'][($ii - 1)]] = str_replace("\r\n", '', str_replace('""', '"', $this->aParceFields[$i][$ii]));
} else {
$buffer['fields'][$this->aCurrentTable[0]['fields'][($ii - 1)]] = str_replace("\r\n", '', $this->aParceFields[$i][$ii]);
}
}
$this->outFields[] = $buffer;
}
return TRUE;
}
return FALSE;
}
function getMaxLenString() {
return $this->aMaxLenString;
}
function setMaxLenString($bytes = NULL) {
if ($bytes) {
$this->aMaxLenString = $bytes;
return TRUE;
}
return FALSE;
}
function getDefReadStrings() {
return $this->aDefReadStrings;
}
function setDefReadStrings($strings = NULL) {
if ($strings) {
$this->aDefReadStrings = $strings;
return TRUE;
}
return FALSE;
}
function getSeparator() {
return $this->aSeparator;
}
function setSeparator($separator = NULL) {
if ($separator) {
$this->aSeparator = $separator;
return TRUE;
}
return FALSE;
}
}
?>