[ create a new paste ] login | about

Link: http://codepad.org/oAiKD0e7    [ raw code | fork ]

PHP, pasted on Dec 29:
<?php

// column format definition
// key = record type
$columnformat = array(
	'1.1' => array(
		'type' => 'columns',
		'columns' => array(
			array('name' => '0', 'width' => 8),
			array('name' => '1', 'width' => 4),
			array('name' => '2', 'width' => 4),
			array('name' => '3', 'width' => 4),
		),
	),

	'1.2' => array(
		'type' => 'dynamic-columns',
	),

	'1.3' => array(
		'type' => 'dynamic-columns',
	),

	'1.4' => array(
		'type' => 'columns',
		'columns' => array(
			array('name' => '0', 'width' => 1),
			array('name' => '1', 'width' => 11),
			array('name' => '2', 'width' => 1),
			array('name' => '3', 'width' => 8),
			array('name' => '4', 'width' => 4),
			array('name' => '5', 'width' => 4),
			array('name' => '6', 'width' => 4),
			// todo add the rest, fix width
		),
	),

	'1.5' => array(
		'type' => 'dynamic-columns',
	),


	'2.1' => array(
		'type' => 'dynamic-columns',
	),

	'2.2' => array(
		'type' => 'columns',
		'columns' => array(
			array('name' => 'someplace', 'width' => 50),
		),
	),

	'4' => array(
		'type' => 'columns',
		'columns' => array(
			array('name' => 'id', 'width' => 6),
			array('name' => 'name', 'width' => 55),
		),
	),

	'11' => array(
		'type' => 'columns',
		'columns' => array(
			array('name' => 'subtype', 'width' => 4),
			array('name' => '1', 'width' => 5),
			array('name' => '2', 'width' => 5),
			array('name' => '3', 'width' => 6),
			array('name' => '4', 'width' => 6),
			array('name' => '5', 'width' => 4),
			// todo add the rest
		),
	),
);

// record types
$typehandler = array(
	'columns' => 'handle_columns',
	'dynamic-columns' => 'handle_dynacolumns',
);

$document = array();
$fh = fopen("export.txt", "r");

if(!$fh) die("can't open export.txt");
while($line = fgets($fh, 1024)) {
	$line = rtrim($line, "\r\n");
	$offset = 3;
	// possible record types (x marks the beginning of the first col)
	// 1234567
	//  1.1 x
	//  1 x
	//  11 x
	//  11.1 x
	$typeExt = $line[$offset-1];
	if($typeExt == '.' || ($typeExt >= '0' && $typeExt <= '9')) {
		if($line[$offset] == ".") {
			++$offset;
		}
		$offset += 2;
	}
	$ctype = trim(substr($line, 0, $offset));

	// handle record according to record type
	if(isset($columnformat[$ctype])) {
		$format = $columnformat[$ctype];
		if(isset($typehandler[$format['type']])) {
			$typecb = $typehandler[$format['type']];
			$cols = $typecb($line, $offset, $format, $fh);
			$document[] = array('type' => $ctype, 'columns' => $cols);
		}
	} else {
		echo "record type \"$ctype\" not implemented: $line\n";
	}
}

fclose($fh);

var_dump(array_filter($document, function($line) {
	//return $line["type"] == "11";
	return true;
}));

// handle static columns 
function handle_columns($line, $offset, $format, $fh) {
	$result = array();
	foreach($format['columns'] as $column) {
		$col = substr($line, $offset, $column['width']);
		$result[$column['name']] = $col;
		$offset += $column['width'];
	}

	return $result;
}

// handle records which have a dynamic column count
// col1: number of columns, col2..colN: fixed with columns (6 characters)
// caveat: columns may continue on next lines, therefore
// we have to be able to read from the filehandle
function handle_dynacolumns($line, $offset, $format, $fh) {
	$count = substr($line, $offset, 5);
	$count = trim($count);
	$offset += 5;
	$baseoffset = $offset;

	$colwidth = 6;
	$linelen = strlen($line);

	$result = array();
	$i = 0;
	while($i < $count && $offset < $linelen) {
		$col = trim(substr($line, $offset, $colwidth));
		$result[] = $col;

		$offset += $colwidth;
		++$i;

		if($offset >= $linelen && $i < $count) {
			$line = fgets($fh, 1024);
			$linelen = strlen($line);
			$offset = $baseoffset;
		}
	}

	return $result;
}


Create a new paste based on this one


Comments: