From 05198978765aff3d7461f49a994a470a61f734ef Mon Sep 17 00:00:00 2001 From: ralf Date: Mon, 3 Jun 2024 12:27:41 +0200 Subject: [PATCH] Convert Lotus Structured Text to CSV to e.g. use CSV Import --- doc/lotus-structured-text2csv.php | 70 +++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 doc/lotus-structured-text2csv.php diff --git a/doc/lotus-structured-text2csv.php b/doc/lotus-structured-text2csv.php new file mode 100755 index 0000000000..765d573728 --- /dev/null +++ b/doc/lotus-structured-text2csv.php @@ -0,0 +1,70 @@ +#!/usr/bin/env php + + * ./lotus-structured-text.php < + */ +if (PHP_SAPI !== 'cli') +{ + die('This script can only be run from the command line.'); +} + +if (!isset($argv[1])) +{ + $fp = fopen('php://stdin', 'r'); +} +elseif (!($fp = fopen($argv[1], 'r'))) +{ + die("Unable to open '$argv[1]'!"); +} + +$ctrl_l = chr(ord("L")-ord("@")); +$records = $record = $keys = []; +while (($line = fgets($fp)) !== false) +{ + $line = trim($line); + if (($line === '' || $line === $ctrl_l)) + { + if ($record) + { + if (!$keys || array_diff(array_keys($record), $keys)) + { + $keys = array_unique(array_merge($keys, array_keys($record))); + } + $records[] = $record; + } + $record = []; + } + else + { + list($key, $value) = preg_split('/: */', $line, 2)+['', '']; + $record[$key] = $value; + } +} +if ($record) +{ + if (!$keys || array_diff(array_keys($record), $keys)) + { + $keys = array_unique(array_merge($keys, array_keys($record))); + } + $records[] = $record; +} +fputcsv(STDOUT, $keys); +foreach($records as $record) +{ + fputcsv(STDOUT, array_map(static function($key) use ($record) { + return $record[$key] ?? ''; + }, $keys)); +} \ No newline at end of file