001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.quercus.lib.file;
031:
032: import com.caucho.quercus.env.BooleanValue;
033: import com.caucho.quercus.env.Env;
034: import com.caucho.quercus.env.ResourceValue;
035: import com.caucho.quercus.env.UnicodeValueImpl;
036: import com.caucho.quercus.env.Value;
037: import com.caucho.vfs.Path;
038:
039: import java.io.IOException;
040:
041: /**
042: * Represents a PHP directory listing
043: */
044: public class DirectoryValue extends ResourceValue {
045: private Env _env;
046: private Path _path;
047: private String[] _list;
048: private int _index;
049:
050: protected DirectoryValue(Env env) {
051: _env = env;
052: }
053:
054: public DirectoryValue(Env env, Path path) throws IOException {
055: _env = env;
056: _path = path;
057:
058: _list = path.list();
059: }
060:
061: /**
062: * Returns the next value.
063: */
064: public Value readdir() {
065: if (_index < _list.length)
066: return _env.createString(_list[_index++]);
067: else
068: return BooleanValue.FALSE;
069: }
070:
071: /**
072: * Rewinds the directory
073: */
074: public void rewinddir() {
075: _index = 0;
076: }
077:
078: /**
079: * Calls the given method.
080: */
081: @Override
082: public Value callMethod(Env env, int hash, char[] name, int nameLen) {
083: String method = new String(name, 0, nameLen);
084:
085: if ("read".equals(method))
086: return readdir();
087: else if ("rewind".equals(method)) {
088: rewinddir();
089:
090: return BooleanValue.TRUE;
091: } else if ("close".equals(method)) {
092: close();
093:
094: return BooleanValue.TRUE;
095: } else
096: return super .callMethod(env, hash, name, nameLen);
097: }
098:
099: /**
100: * Converts to a string.
101: * @param env
102: */
103: public String toString() {
104: return "Directory[" + _path + "]";
105: }
106: }
|