From 048bf909f18b6cfdcf94258d914012e9e8d00346 Mon Sep 17 00:00:00 2001 From: Eric Defore Date: Mon, 19 Sep 2022 13:44:00 -0400 Subject: [PATCH 1/4] Support Memos for Visual FoxPro Changes were made specifically for Visual FoxPro (TableType::VISUAL_FOXPRO), but it may work for other Versions too --- src/Memo/Creator/AbstractMemoCreator.php | 2 +- src/Memo/Creator/FoxProMemoCreator.php | 24 ++++++++++++++++++++++++ src/Memo/Creator/MemoCreatorFactory.php | 2 ++ src/Memo/MemoFactory.php | 4 +++- 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/Memo/Creator/FoxProMemoCreator.php diff --git a/src/Memo/Creator/AbstractMemoCreator.php b/src/Memo/Creator/AbstractMemoCreator.php index aa740ab..9dab033 100644 --- a/src/Memo/Creator/AbstractMemoCreator.php +++ b/src/Memo/Creator/AbstractMemoCreator.php @@ -25,7 +25,7 @@ public static function getExtension(): string public function createFile(): string { $pi = pathinfo($this->table->filepath); - $memoFilepath = sprintf('%s/%s.%s', $pi['dirname'], $pi['filename'], self::getExtension()); + $memoFilepath = sprintf('%s/%s.%s', $pi['dirname'], $pi['filename'], $this->getExtension()); $stream = Stream::createFromFile($memoFilepath, 'wb+'); $this->writeHeader($stream); diff --git a/src/Memo/Creator/FoxProMemoCreator.php b/src/Memo/Creator/FoxProMemoCreator.php new file mode 100644 index 0000000..f544e47 --- /dev/null +++ b/src/Memo/Creator/FoxProMemoCreator.php @@ -0,0 +1,24 @@ +write(pack('N',8)); //next block + + $stream->seek(4); + $stream->write(str_pad('',2,chr(0))); //reserved + + $stream->seek(6); + $stream->write(pack('n',64)); //Block size. 8 * 64 == 512, which will start records after the header + } +} diff --git a/src/Memo/Creator/MemoCreatorFactory.php b/src/Memo/Creator/MemoCreatorFactory.php index eb1653d..4ff48ee 100644 --- a/src/Memo/Creator/MemoCreatorFactory.php +++ b/src/Memo/Creator/MemoCreatorFactory.php @@ -22,6 +22,8 @@ public static function create(Table $table) case TableType::DBASE_7_MEMO: return new DBase7MemoCreator($table); //todo foxpro + case TableType::VISUAL_FOXPRO: + return new FoxProMemoCreator($table); default: throw new \Exception('Memo creator not realized for table version '.$table->getVersion()); } diff --git a/src/Memo/MemoFactory.php b/src/Memo/MemoFactory.php index c1e6839..da21efe 100644 --- a/src/Memo/MemoFactory.php +++ b/src/Memo/MemoFactory.php @@ -5,6 +5,7 @@ use XBase\DataConverter\Encoder\EncoderInterface; use XBase\Enum\TableType; use XBase\Table\Table; +use XBase\Memo\Creator\MemoCreatorFactory; class MemoFactory { @@ -25,7 +26,8 @@ public static function create(Table $table, EncoderInterface $encoder): ?MemoInt } $memoFilepath = $fileInfo['dirname'].DIRECTORY_SEPARATOR.$fileInfo['filename'].$memoExt; if (!file_exists($memoFilepath)) { - return null; //todo create file? + $memo_creator = MemoCreatorFactory::create($table); + $memo_creator->createFile(); } return $refClass->newInstance($table, $memoFilepath, $encoder); From 786db83a69cf1abb7cf2c40f1fb3664178dc4526 Mon Sep 17 00:00:00 2001 From: Eric Defore Date: Thu, 22 Sep 2022 09:45:12 -0400 Subject: [PATCH 2/4] Ran php-cs-fixer fix on FoxProMemoCreator Referencing #118 --- src/Memo/Creator/FoxProMemoCreator.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Memo/Creator/FoxProMemoCreator.php b/src/Memo/Creator/FoxProMemoCreator.php index f544e47..7533929 100644 --- a/src/Memo/Creator/FoxProMemoCreator.php +++ b/src/Memo/Creator/FoxProMemoCreator.php @@ -1,4 +1,6 @@ -write(pack('N',8)); //next block + $stream->write(pack('N', 8)); //next block $stream->seek(4); - $stream->write(str_pad('',2,chr(0))); //reserved + $stream->write(str_pad('', 2, chr(0))); //reserved $stream->seek(6); - $stream->write(pack('n',64)); //Block size. 8 * 64 == 512, which will start records after the header + $stream->write(pack('n', 64)); //Block size. 8 * 64 == 512, which will start records after the header } } From 2c4dcd0c9fab421ed959da7074db1fe8fd69cecd Mon Sep 17 00:00:00 2001 From: Eric Defore Date: Thu, 22 Sep 2022 10:06:50 -0400 Subject: [PATCH 3/4] Revert change to MemoFactory::create() TableWriter is handling creation of the Memo file, so it doesn't need to be done here. Referencing #118 --- src/Memo/MemoFactory.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Memo/MemoFactory.php b/src/Memo/MemoFactory.php index da21efe..42257e3 100644 --- a/src/Memo/MemoFactory.php +++ b/src/Memo/MemoFactory.php @@ -26,8 +26,7 @@ public static function create(Table $table, EncoderInterface $encoder): ?MemoInt } $memoFilepath = $fileInfo['dirname'].DIRECTORY_SEPARATOR.$fileInfo['filename'].$memoExt; if (!file_exists($memoFilepath)) { - $memo_creator = MemoCreatorFactory::create($table); - $memo_creator->createFile(); + return null; //todo create file? } return $refClass->newInstance($table, $memoFilepath, $encoder); From a7a65c70984bca3f32f8ab1cacc9b20f339ce54c Mon Sep 17 00:00:00 2001 From: Eric Defore Date: Tue, 27 Sep 2022 13:34:16 -0400 Subject: [PATCH 4/4] Fix backlist issues The Backlist is meant to link to a Database (.DBC) file. Some applications (FoxPro specifically, I've noticed) get confused when it is initialized with non-0 values. --- src/Header/Writer/VisualFoxproHeaderWriter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Header/Writer/VisualFoxproHeaderWriter.php b/src/Header/Writer/VisualFoxproHeaderWriter.php index d64eddd..0c8febb 100644 --- a/src/Header/Writer/VisualFoxproHeaderWriter.php +++ b/src/Header/Writer/VisualFoxproHeaderWriter.php @@ -17,6 +17,6 @@ protected function writeRest(Header $header): void parent::writeRest($header); - $this->fp->write(str_pad($header->backlist, 263)); + $this->fp->write(str_pad($header->backlist, 263, chr(0))); } }