/dev/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/dev/trunk

« back to all changes in this revision

Viewing changes to classes/stream/io.php

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
require("streamwriter.php");
 
3
require("streamobject.php");
 
4
require("streamreader.php");
 
5
 
 
6
class IO extends VIRTUALStreamReaderWriter implements STREAMObjectInterface {
 
7
 var $output;
 
8
 
 
9
 var $action;
 
10
 var $close;
 
11
 var $stdout;
 
12
 
 
13
 var $read;
 
14
 
 
15
 
 
16
 function __construct($output = NULL, $read_mode = false) {
 
17
    $this->output = $output;
 
18
    $this->read = $read_mode;
 
19
 }
 
20
 
 
21
 function Open(&$output = NULL, $flags = 0) {
 
22
    if (!$output) $output = $this->output;
 
23
 
 
24
    if (is_resource($output)) {
 
25
        $this->action = $output;
 
26
 
 
27
        if ($flags&STREAM::GIFT) $this->close = true;
 
28
        else $this->close = false;
 
29
        $this->stdout = false;
 
30
    } elseif (is_string($output)) {
 
31
        $this->action = fopen($output, $this->read?"rw":"w");
 
32
        if (!$this->action)
 
33
            throw new ADEIException(translate("Access to the stream outlet (%s) is failed", $action));
 
34
 
 
35
        $this->close = true;
 
36
        $this->stdout = false;
 
37
    } elseif ($output instanceof STREAMWriterInterface) {
 
38
        if (($this->read)&&(!($output instanceof STREAMReaderWriterInterface))) {
 
39
            throw new ADEIException(translate("STREAMReader interface is not supported by object"));
 
40
        }
 
41
 
 
42
        $this->action = $output;
 
43
 
 
44
        if ($output instanceof STREAMSequenceInterface) {
 
45
            $this->action->SequenceStart();
 
46
            $this->action->Start();
 
47
        
 
48
            $this->close = true;
 
49
        } else {
 
50
            $this->close = false;
 
51
        }
 
52
        
 
53
    } elseif ($output) {
 
54
        throw new ADEIException(translate("Unsupported stream output"));
 
55
    } else {
 
56
        if ($this->read) {
 
57
            throw new ADEIException(translate("There are no read possibility for STDOUT objects"));
 
58
        }
 
59
        $this->action = fopen("php://output", "w");
 
60
        $this->close = true;
 
61
        $this->stdout = true;
 
62
    }
 
63
 }
 
64
 
 
65
 function Close(STREAMWriterInterface $h = NULL) {
 
66
    if ($this->read) {
 
67
        if ($h) $this->Stream($h);
 
68
        else $res = $this->GetContent($h);      
 
69
    }
 
70
 
 
71
    if (is_resource($this->action)) {
 
72
        if ($this->close) {
 
73
            fclose($this->action);
 
74
            $this->close = false;
 
75
        }
 
76
    } else {
 
77
        if ($this->close) {
 
78
            $this->action->End();
 
79
            $this->action->SequenceEnd();
 
80
        }
 
81
    }
 
82
    
 
83
    unset($this->action);
 
84
    
 
85
    return $res;
 
86
 }
 
87
 
 
88
 function WriteFile($file) {
 
89
    if ($this->stdout) readfile($file);
 
90
    else parent::WriteFile($file, $flags);
 
91
 }
 
92
 
 
93
 function WriteData(&$data) {
 
94
    if (is_resource($this->action))
 
95
        fwrite($this->action, $data);
 
96
    else
 
97
        $this->action->WriteData($data, $flags);
 
98
 }
 
99
 
 
100
 function ReadData($limit = 0, $flags = 0) {
 
101
    if (!$this->read)
 
102
        throw new ADEIException(translate("The read is not supported"));
 
103
    
 
104
    $this->read = 2; // At least single read is performed
 
105
    
 
106
    if (is_resource($this->action)) {
 
107
        if (($flags&STREAM::BLOCK)||(stream_select($r=array($this->action), $w=NULL, $e=NULL, 0)>0)) {
 
108
            if ($limit) return fread($this->action, $limit);
 
109
            else return fread($this->action, STREAM::BUFFER_SIZE);
 
110
        } 
 
111
        return "";
 
112
    } else
 
113
        $this->action->ReadData($flags);
 
114
 }
 
115
 
 
116
 function EOS() {
 
117
    if (!$this->read)
 
118
        throw new ADEIException(translate("The read is not supported"));
 
119
 
 
120
    if (is_resource($this->action))
 
121
        return feof($this->action);
 
122
    else
 
123
        return $this->action->EOS($flags);
 
124
 }
 
125
}
 
126
 
 
127
?>
 
 
b'\\ No newline at end of file'