root/lang/php/ARGF/trunk/ARGF.php

Revision 16026, 3.7 kB (checked in by bto, 4 years ago)

空配列の時にうまくいかないのを修正

Line 
1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4class ARGF
5{
6    var $fp = null;
7    var $filename = null;
8    var $lineno = 0;
9
10    var $argc = null;
11    var $argv = null;
12    var $stdin = true;
13
14    function ARGF($argv = null)
15    {
16        if (is_array($argv)) {
17            array_unshift($argv, null);
18            $this->argv =& $argv;
19        } else {
20            $this->argc =& $this->ARGC();
21            $this->argv =& $this->ARGV();
22        }
23    }
24
25    function &ARGC()
26    {
27        global $argc;
28        if ($argc) {
29            return $argc;
30        }
31
32        if (isset($_SERVER['argc'])) {
33            return $_SERVER['argc'];
34        }
35
36        if (isset($GLOBALS['HTTP_SERVER_VARS']['argc'])) {
37            return $GLOBALS['HTTP_SERVER_VARS']['argc'];
38        }
39
40        $result = 0;
41        return $result;
42    }
43
44    function &ARGV()
45    {
46        global $argv;
47        if (is_array($argv)) {
48            return $argv;
49        }
50
51        if (@is_array($_SERVER['argv'])) {
52            return $_SERVER['argv'];
53        }
54
55        if (@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
56            return $GLOBALS['HTTP_SERVER_VARS']['argv'];
57        }
58
59        $result = array();
60        return $result;
61    }
62
63    function initializeStream()
64    {
65        if ($this->fp === false) {
66            return false;
67        }
68
69        if ($this->fp === null) {
70            $this->nextFile();
71        }
72
73        return $this->fp;
74    }
75
76    function &each()
77    {
78        if (!($result = $this->initializeStream())) {
79            return $result;
80        }
81
82        while ($this->fp && !($line = fgets($this->fp))) {
83            $this->nextFile();
84        }
85
86        if ($line) {
87            $this->lineno++;
88        }
89
90        return $line;
91    }
92
93    function eachByte()
94    {
95        if (!($result = $this->initializeStream())) {
96            return $result;
97        }
98
99        while ($this->fp && !($c = fgetc($this->fp))) {
100            $this->nextFile();
101        }
102
103        if ($c === "\n") {
104            $this->lineno++;
105        }
106
107        return $c;
108    }
109
110    function &getInstance()
111    {
112        static $instance = null;
113        if ($instance === null) {
114            $class = __CLASS__;
115            $instance = new $class();
116        }
117        return $instance;
118    }
119
120    function nextFile()
121    {
122        if (isset($this->argv[1])) {
123            $this->argc--;
124            list($this->filename) = array_splice($this->argv, 1, 1);
125            $this->fp = fopen($this->filename, 'r');
126            $this->stdin = false;
127        } else if ($this->stdin) {
128            $this->fp = STDIN;
129            $this->stdin = false;
130        } else {
131            $this->fp = false;
132        }
133    }
134
135    function &read($length)
136    {
137        if (!($result = $this->initializeStream())) {
138            return $result;
139        }
140
141        $data = '';
142        $length_org = $length;
143        do {
144            $string = fread($this->fp, $length);
145            $length -= strlen($string);
146            $data .= $string;
147            if (strlen($data) >= $length_org) {
148                break;
149            }
150            $this->nextFile();
151        } while ($this->fp);
152
153        return $data;
154    }
155
156    function &toArray()
157    {
158        $array = array();
159        while ($line =& $this->each()) {
160            $array[] =& $line;
161        }
162        return $array;
163    }
164
165    function &toString()
166    {
167        $data = '';
168        while ($string = $this->read(4096)) {
169            $data .= $string;
170        }
171        return $data;
172    }
173}
174
175
176$ARGF = new ARGF();
177$ARGC =& $ARGF->ARGC();
178$ARGV =& $ARGF->ARGV();
179
180if (debug_backtrace()) {
181    return;
182}
183
184/*
185 * sample code
186 */
187while ($line = $ARGF->each()) {
188    echo $ARGF->lineno.": $line";
189}
Note: See TracBrowser for help on using the browser.