cvs: pear /Net_DIME DIME.php /Net_DIME/test dime_message_test.php dime_record_test.php
| From: | Shane Caraveo | Date: | Mon, 13 May 2002 03:46:33 +0000 |
| Subject: | cvs: pear /Net_DIME DIME.php /Net_DIME/test dime_message_test.php dime_record_test.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-3793@lists.php.net to get a copy of this message | ||
shane Sun May 12 23:46:33 2002 EDT
Modified files:
/pear/Net_DIME DIME.php
/pear/Net_DIME/test dime_message_test.php dime_record_test.php
Log:
add functions to use DIME without a stream
Index: pear/Net_DIME/DIME.php
diff -u pear/Net_DIME/DIME.php:1.1 pear/Net_DIME/DIME.php:1.2
--- pear/Net_DIME/DIME.php:1.1 Sun May 12 23:02:24 2002
+++ pear/Net_DIME/DIME.php Sun May 12 23:46:32 2002
@@ -16,7 +16,7 @@
// | Authors: Shane Caraveo <shane@caraveo.com> |
// +----------------------------------------------------------------------+
//
-// $Id: DIME.php,v 1.1 2002/05/13 03:02:24 shane Exp $
+// $Id: DIME.php,v 1.2 2002/05/13 03:46:32 shane Exp $
//
require_once 'PEAR.php';
@@ -38,7 +38,7 @@
* see test/dime_mesage_test.php for example of usage
*
* @author Shane Caraveo <shane@caraveo.com>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
* @package Net_DIME
*/
define('DIME_TYPE_UNCHANGED',0x00);
@@ -330,7 +330,7 @@
$this->debug = $debug;
}
- function _sendRecord(&$data, $typestr='', $id=NULL, $type=DIME_TYPE_UNKNOWN)
+ function _makeRecord(&$data, $typestr='', $id=NULL, $type=DIME_TYPE_UNKNOWN)
{
$record = new DIME_Record($this->debug);
if ($this->mb) {
@@ -346,7 +346,7 @@
#if ($this->debug) {
# print str_replace('\0','*',$record->encode());
#}
- return fwrite($this->stream, $record->encode());
+ return $record->encode();
}
function startChunk(&$data, $typestr='', $id=NULL, $type=DIME_TYPE_UNKNOWN)
@@ -360,36 +360,38 @@
} else {
$this->id = md5(time());
}
- $this->_sendRecord($data, $this->typestr, $this->id, $this->type);
+ return $this->_makeRecord($data, $this->typestr, $this->id, $this->type);
}
function doChunk(&$data)
{
$this->me = 0;
$this->cf = 1;
- $this->_sendRecord($data, NULL, NULL, DIME_TYPE_UNCHANGED);
+ return $this->_makeRecord($data, NULL, NULL, DIME_TYPE_UNCHANGED);
}
function endChunk()
{
$this->cf = 0;
$data = NULL;
- $this->_sendRecord($data, NULL, NULL, DIME_TYPE_UNCHANGED);
+ $rec = $this->_makeRecord($data, NULL, NULL, DIME_TYPE_UNCHANGED);
$this->id = 0;
$this->cf = 0;
$this->id = 0;
$this->type = DIME_TYPE_UNKNOWN;
$this->typestr = NULL;
+ return $rec;
}
function endMessage()
{
$this->me = 1;
$data = NULL;
- $this->_sendRecord($data, NULL, NULL, DIME_TYPE_NONE);
+ $rec = $this->_makeRecord($data, NULL, NULL, DIME_TYPE_NONE);
$this->me = 0;
$this->mb = 1;
$this->id = 0;
+ return $rec;
}
/**
@@ -405,16 +407,26 @@
if ($len > $this->record_size) {
$chunk = substr($data, 0, $this->record_size);
$p = $this->record_size;
- $this->startChunk($chunk,$typestr,$id,$type);
+ $rec = $this->startChunk($chunk,$typestr,$id,$type);
+ fwrite($this->stream, $rec);
while ($p < $len) {
$chunk = substr($data, $p, $this->record_size);
$p += $this->record_size;
- $this->doChunk($chunk);
+ $rec = $this->doChunk($chunk);
+ fwrite($this->stream, $rec);
}
- $this->endChunk();
+ $rec = $this->endChunk();
+ fwrite($this->stream, $rec);
return;
}
- $this->_sendRecord($data, $typestr,$id,$type);
+ $rec = $this->_makeRecord($data, $typestr,$id,$type);
+ fwrite($this->stream, $rec);
+ }
+
+ function sendEndMessage()
+ {
+ $rec = $this->endMessage();
+ fwrite($this->stream, $rec);
}
/**
@@ -439,6 +451,55 @@
}
}
+ /**
+ * encodeData
+ *
+ * given data, encode it in DIME
+ *
+ */
+ function encodeData($data, $typestr='', $id=NULL, $type=DIME_TYPE_UNKNOWN)
+ {
+ $len = strlen($data);
+ $resp = '';
+ if ($len > $this->record_size) {
+ $chunk = substr($data, 0, $this->record_size);
+ $p = $this->record_size;
+ $resp .= $this->startChunk($chunk,$typestr,$id,$type);
+ while ($p < $len) {
+ $chunk = substr($data, $p, $this->record_size);
+ $p += $this->record_size;
+ $resp .= $this->doChunk($chunk);
+ }
+ $resp .= $this->endChunk();
+ } else {
+ $resp .= $this->_makeRecord($data, $typestr,$id,$type);
+ }
+ return $resp;
+ }
+
+ /**
+ * sendFile
+ *
+ * given a filename, it reads the file,
+ * creates records and writes them to the stream
+ *
+ */
+ function encodeFile($filename, $typestr='', $id=NULL, $type=DIME_TYPE_UNKNOWN)
+ {
+ $f = fopen($filename, "rb");
+ if ($f) {
+ if ($data = fread($f, $this->record_size)) {
+ $resp = $this->startChunk($data,$typestr,$id,$type);
+ }
+ while ($data = fread($f, $this->record_size)) {
+ $resp = $this->doChunk($data,$typestr,$id,$type);
+ }
+ $resp = $this->endChunk();
+ fclose($f);
+ }
+ return $resp;
+ }
+
/**
* _processData
*
Index: pear/Net_DIME/test/dime_message_test.php
diff -u pear/Net_DIME/test/dime_message_test.php:1.1 pear/Net_DIME/test/dime_message_test.php:1.2
--- pear/Net_DIME/test/dime_message_test.php:1.1 Sun May 12 23:02:24 2002
+++ pear/Net_DIME/test/dime_message_test.php Sun May 12 23:46:33 2002
@@ -1,5 +1,5 @@
<?php
-include 'SOAP/DIME.php';
+include 'Net/DIME.php';
$test='--=_a2cbb051424cc43e72d3c8c8d0b8f70e
Content-Type: text/xml; charset="UTF-8"
@@ -551,7 +551,7 @@
$dime = new DIME_Message($f,4096,$debug);
$dime->sendData($test);
#$dime->sendData($test);
-$dime->endMessage();
+$dime->sendEndMessage();
$size = ftell($f);
print "filesize: $size, datasize: ".strlen($test)."\n";
rewind($f);
Index: pear/Net_DIME/test/dime_record_test.php
diff -u pear/Net_DIME/test/dime_record_test.php:1.1 pear/Net_DIME/test/dime_record_test.php:1.2
--- pear/Net_DIME/test/dime_record_test.php:1.1 Sun May 12 23:02:24 2002
+++ pear/Net_DIME/test/dime_record_test.php Sun May 12 23:46:33 2002
@@ -1,5 +1,5 @@
<?php
-include 'SOAP/DIME.php';
+include 'Net/DIME.php';
$test='--=_a2cbb051424cc43e72d3c8c8d0b8f70e
Content-Type: text/xml; charset="UTF-8"