Skip to content

Commit

Permalink
Issue #13 FoxPro memo field bugfix (#14)
Browse files Browse the repository at this point in the history
* #13 - fix fox pro memo loading

* #13 add throw annotations, doc etc

* #13 foxpro write

* #13 fix

* #13 fix travis

* #13 fix

* #13 fix

* #13 fix

* #13 disable travis php 7.3
  • Loading branch information
majkel89 authored Sep 15, 2018
1 parent e87bd25 commit f132071
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.tar
*.copy*
/composer.lock
/tests/fixtures/materie-2.*
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
language: php
php:
- 5.6
- 5.5
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
# - 7.3
- hhvm
- nightly

Expand Down
2 changes: 2 additions & 0 deletions DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ http://www.clicketyclick.dk/databases/xbase/format/dbf.html#DBF_STRUCT
http://www.dbase.com/Knowledgebase/INT/db7_file_fmt.htm

http://www.digitalpreservation.gov/formats/fdd/fdd000325.shtml

http://web.tiscali.it/SilvioPitti/
2 changes: 2 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public static function fromTable(Table $table) {
/**
* @param string $filePath
* @return \org\majkel\dbase\Builder
* @throws Exception
*/
public static function fromFile($filePath) {
return self::fromTable(Table::fromFile($filePath, Table::MODE_READ));
Expand Down Expand Up @@ -120,6 +121,7 @@ public function setMemoType($memoType) {
/**
* @param string $filePath
* @return Table
* @throws Exception
*/
public function build($filePath) {
$header = $this->getHeader();
Expand Down
1 change: 1 addition & 0 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function getSize() {
* @param string $mode
* @param string $class
* @return \SplFileObject
* @throws \ReflectionException
*/
public static function getObject($path, $mode, $class = '\org\majkel\dbase\File') {
$reflection = new ReflectionClass($class);
Expand Down
36 changes: 30 additions & 6 deletions src/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class Format {

/** @var \SplFileObject File handle */
protected $file;
/** @var \SplFileObject File handle */
/** @var \org\majkel\dbase\memo\MemoInterface File handle */
protected $memoFile;
/** @var \org\majkel\dbase\Header */
protected $header;
Expand All @@ -53,6 +53,7 @@ abstract class Format {
/**
* @param string $filePath
* @param string $mode
* @throws \ReflectionException
*/
public function __construct($filePath, $mode) {
$this->mode = $mode;
Expand All @@ -61,6 +62,7 @@ public function __construct($filePath, $mode) {

/**
* @return \org\majkel\dbase\Header
* @throws Exception
*/
public function getHeader() {
if (is_null($this->header)) {
Expand All @@ -71,6 +73,7 @@ public function getHeader() {

/**
* @return boolean
* @throws Exception
*/
public function isValid() {
return $this->getHeader()->isValid();
Expand All @@ -85,6 +88,7 @@ public function getFileInfo() {

/**
* @return \SplFileInfo
* @throws Exception
*/
public function getMemoFileInfo() {
return $this->getMemo()->getFileInfo();
Expand All @@ -100,6 +104,7 @@ public function getName() {
/**
* @param integer $index
* @return Record
* @throws Exception
*/
public function getRecord($index) {
$records = $this->getRecords($index, 1);
Expand All @@ -117,13 +122,18 @@ public function getRecords($index, $length) {
list($start, $stop) = $this->getReadBoundaries($index, $length);
$file = $this->getFile();
$rSz = $this->getHeader()->getRecordSize();
$file->fseek($this->getHeader()->getHeaderSize() + $start * $rSz);
$offset = $this->getHeader()->getHeaderSize() + $start * $rSz;
$file->fseek($offset);
$format = $this->getRecordFormat();
$allData = $file->fread($rSz * $length);
$records = array();
for ($i = 0; $start < $stop; ++$start, ++$i) {
$data = unpack($format, strlen($allData) === $rSz
? $allData : substr($allData, $i * $rSz, $rSz));
if (strlen($allData) === $rSz) {
$recordData = $allData;
} else {
$recordData = substr($allData, $i * $rSz, $rSz);
}
$data = unpack($format, $recordData);
$records[$start] = $this->createRecord($data);
}
return $records;
Expand Down Expand Up @@ -227,8 +237,8 @@ public function isTransaction() {
}

/**
* @return HeaderInterface
* @return boolean
* @throws Exception
*/
protected function checkIfTransaction() {
$currentHeader = $this->readHeader();
Expand All @@ -241,6 +251,7 @@ protected function checkIfTransaction() {

/**
* @param boolean $enabled
* @throws Exception
*/
protected function setTransactionStatus($enabled) {
$enabled = (boolean) $enabled;
Expand Down Expand Up @@ -312,6 +323,7 @@ public function markDeleted($index, $deleted) {

/**
* @return string
* @throws Exception
*/
protected function getWriteRecordFormat() {
if (is_null($this->writeRecordFormat)) {
Expand Down Expand Up @@ -351,6 +363,7 @@ protected function serializeRecord(Record $record) {
/**
* @param integer $index
* @return integer
* @throws Exception
*/
private function getRecordOffset($index) {
return $index * $this->getHeader()->getRecordSize() + $this->getHeader()->getHeaderSize();
Expand All @@ -359,6 +372,7 @@ private function getRecordOffset($index) {
/**
* @param \org\majkel\dbase\Record $data
* @return integer
* @throws Exception
*/
public function insert(Record $data) {
$header = $this->getHeader();
Expand All @@ -378,9 +392,10 @@ public function insert(Record $data) {
}

/**
* @param integer $index
* @param integer $index
* @param \org\majkel\dbase\Record $data
* @return void
* @throws Exception
*/
public function update($index, Record $data) {
list($offset) = $this->getReadBoundaries($index, 0);
Expand All @@ -405,6 +420,7 @@ protected function getWriteHeaderFormat() {

/**
* @return void
* @throws Exception
*/
protected function writeHeader() {
$file = $this->getFile();
Expand All @@ -427,6 +443,7 @@ protected function writeHeader() {

/**
* @return Header
* @throws Exception
*/
protected function readHeader() {
$file = $this->getFile();
Expand Down Expand Up @@ -497,6 +514,7 @@ protected function createField($data) {

/**
* @return string
* @throws Exception
*/
protected function getRecordFormat() {
if (is_null($this->recordFormat)) {
Expand All @@ -512,6 +530,7 @@ protected function getRecordFormat() {
/**
* @param integer $index
* @return string
* @throws Exception
*/
protected function readMemoEntry($index) {
return $this->getMemo()->getEntry($index);
Expand All @@ -520,6 +539,7 @@ protected function readMemoEntry($index) {
/**
* @param array $data
* @return \org\majkel\dbase\Record
* @throws Exception
*/
protected function createRecord($data) {
$record = new Record;
Expand Down Expand Up @@ -553,6 +573,7 @@ abstract public function getType();
/**
* @param \org\majkel\dbase\Header $header
* @return $this
* @throws Exception
*/
public function create(Header $header) {
$this->getFile()->ftruncate(0);
Expand All @@ -575,6 +596,7 @@ public function create(Header $header) {

/**
* @return integer
* @throws Exception
*/
protected function calculateRecordSize() {
$result = 1;
Expand All @@ -586,6 +608,7 @@ protected function calculateRecordSize() {

/**
* @return integer
* @throws Exception
*/
protected function calculateHeaderSize() {
$result = self::HEADER_SIZE + $this->getHeader()->getFieldsCount() * self::FIELD_SIZE + 2;
Expand All @@ -601,6 +624,7 @@ protected function getWriteFieldFormat() {

/**
* @return void
* @throws Exception
*/
protected function writeRecords() {
$file = $this->getFile();
Expand Down
2 changes: 2 additions & 0 deletions src/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,15 @@ public function offsetSet($offset, $value) {

/**
* @param integer $offset
* @throws Exception
*/
public function offsetUnset($offset) {
$this->removeField($offset);
}

/**
* {@inheritdoc}
* @throws Exception
*/
public function getField($name) {
if ($this->offsetExists($name)) {
Expand Down
Loading

0 comments on commit f132071

Please sign in to comment.