filemodule.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,844 行 · 第 1/5 页

JAVA
2,844
字号
				     ReadStream is,				     boolean processSections)    throws IOException  {    ArrayValue top = new ArrayValueImpl();    ArrayValue section = top;        int ch;    while ((ch = is.read()) >= 0) {      if (Character.isWhitespace(ch)) {      }      else if (ch == ';') {	for (; ch >= 0 && ch != '\r' && ch != '\n'; ch = is.read()) {	}      }      else if (ch == '[') {	StringBuilder sb = new StringBuilder();	for (ch = is.read(); ch >= 0 && ch != ']'; ch = is.read()) {	  sb.append((char) ch);	}	String name = sb.toString().trim();	if (processSections) {	  section = new ArrayValueImpl();	  top.put(env.createString(name), section);	}      }      else if (isValidIniKeyChar((char) ch)) {	StringBuilder sb = new StringBuilder();	for (; isValidIniKeyChar((char) ch); ch = is.read()) {	  sb.append((char) ch);	}	String key = sb.toString().trim();	for (; ch >= 0 && ch != '='; ch = is.read()) {	}	for (ch = is.read(); ch == ' ' || ch == '\t'; ch = is.read()) {	}	Value value = parseIniValue(env, ch, is);	section.put(env.createString(key), value);      }    }    return top;  }  private static Value parseIniValue(Env env, int ch, ReadStream is)    throws IOException  {    if (ch == '\r' || ch == '\n')      return NullValue.NULL;    if (ch == '"') {      StringValue sb = env.createUnicodeBuilder();            for (ch = is.read(); ch >= 0 && ch != '"'; ch = is.read()) {	sb.append((char) ch);      }      skipToEndOfLine(ch, is);      return sb;    }    else if (ch == '\'') {      StringValue sb = env.createUnicodeBuilder();            for (ch = is.read(); ch >= 0 && ch != '\''; ch = is.read()) {	sb.append((char) ch);      }      skipToEndOfLine(ch, is);      return sb;    }    else {      StringBuilder sb = new StringBuilder();      for (;	   ch >= 0 && ch != '\r' && ch != '\n';	   ch = is.read()) {		if (ch == ';') {	  skipToEndOfLine(ch, is);	  break;	}	else if (ch == '$') {	  int peek = is.read();	  if (peek == '{') {	    StringBuilder var = new StringBuilder();	    for (ch = is.read();		 ch >= 0 && ch != '\r' && ch != '\n' && ch != '}';		 ch = is.read()) {	      var.append((char) ch);	    }	    	    Value value = env.getIni(var.toString());	    if (value != null)	      sb.append(value);	  }	  else {	    sb.append('$');	    is.unread();	  }	}	else	  sb.append((char) ch);      }      String value = sb.toString().trim();      if (value.equalsIgnoreCase("null"))	return env.getEmptyString();      else if (value.equalsIgnoreCase("true")	       || value.equalsIgnoreCase("yes"))	return env.createString("1");      else if (value.equalsIgnoreCase("false")	       || value.equalsIgnoreCase("no"))	return env.getEmptyString();      if (env.isDefined(value))	return env.createString(env.getConstant(value).toString());      else	return env.createString(value);    }  }  private static boolean isValidIniKeyChar(char ch)  {    if (ch <= 0	|| ch == '='	|| ch == ';'	|| ch == '{'	|| ch == '}'	|| ch == '|'	|| ch == '&'	|| ch == '~'	|| ch == '!'	|| ch == '['	|| ch == '('	|| ch == ')'	|| ch == '"')      return false;    else      return true;  }    private static void skipToEndOfLine(int ch, ReadStream is)    throws IOException  {    for (; ch > 0 && ch != '\r' && ch != '\n'; ch = is.read()) {    }  }  /**   * Parses the path, splitting it into parts.   */  public static Value pathinfo(Env env, String path, @Optional Value optionsV)  {    if (optionsV == null)      return env.getEmptyString();        if (path == null) {      if (! (optionsV instanceof DefaultValue)) {        return env.getEmptyString();      }      ArrayValueImpl value = new ArrayValueImpl();      value.put(env.createString("basename"), env.createString(""));      value.put(env.createString("filename"), env.createString(""));      return value;    }    int p = path.lastIndexOf('/');    String dirname;    if (p >= 0) {      dirname = path.substring(0, p);      path = path.substring(p + 1);    }    else {      dirname = ".";    }    p = path.indexOf('.');        String filename = path;    String ext = "";        if (p > 0) {      filename = path.substring(0, p);      ext = path.substring(p + 1);    }    if (! (optionsV instanceof DefaultValue)) {      int options = optionsV.toInt();      if ((options & PATHINFO_DIRNAME) == PATHINFO_DIRNAME)        return env.createString(dirname);      else if ((options & PATHINFO_BASENAME) == PATHINFO_BASENAME)        return env.createString(path);      else if ((options & PATHINFO_EXTENSION) == PATHINFO_EXTENSION)        return env.createString(ext);      else if ((options & PATHINFO_FILENAME) == PATHINFO_FILENAME)        return env.createString(filename);      else        return env.getEmptyString();    }    else {      ArrayValueImpl value = new ArrayValueImpl();      value.put(env.createString("dirname"), env.createString(dirname));      value.put(env.createString("basename"), env.createString(path));      value.put(env.createString("extension"), env.createString(ext));      value.put(env.createString("filename"), env.createString(filename));      return value;    }  }  public static int pclose(Env env, @NotNull BinaryStream stream)  {    if (stream instanceof PopenInput)      return ((PopenInput) stream).pclose();    else if (stream instanceof PopenOutput)      return ((PopenOutput) stream).pclose();    else {      env.warning(L.l("{0} was not returned by popen()", stream));      return -1;    }  }  @ReturnNullAsFalse   public static BinaryStream popen(Env env,                                    @NotNull String command,                                    @NotNull StringValue mode)  {    boolean doRead = false;    if (mode.toString().equalsIgnoreCase("r"))      doRead = true;    else if (mode.toString().equalsIgnoreCase("w"))      doRead = false;    else      return null;    String []args = new String[3];    try {      if (Path.isWindows()) {        args[0] = "cmd";        args[1] = "/c";      }      else {        args[0] = "sh";        args[1] = "-c";      }      args[2] = command;      Process process = Runtime.getRuntime().exec(args);      if (doRead)        return new PopenInput(env, process);      else        return new PopenOutput(env, process);    } catch (Exception e) {      env.warning(e.getMessage(), e);      return null;    }  }    /**   * Reads the next entry   *   * @param dirV the directory resource   */  public static Value readdir(Env env, @NotNull DirectoryValue dir)  {    if (dir == null)      return BooleanValue.FALSE;        return dir.readdir();  }  /**   * Read the contents of a file and write them out.   */  public Value readfile(Env env,                        StringValue filename,                        @Optional boolean useIncludePath,                        @Optional Value context)  {    if (filename.length() == 0)      return BooleanValue.FALSE;    BinaryStream s = fopen(env, filename, "r", useIncludePath, context);    if (! (s instanceof BinaryInput))      return BooleanValue.FALSE;    BinaryInput is = (BinaryInput) s;    try {      return fpassthru(env, is);    } finally {      is.close();    }  }  /**   * The readlink   */  public static Value readlink(Env env, Path path)  {    String link = path.readLink();    if (link == null)      return BooleanValue.FALSE;    else      return env.createString(link);  }  /**   * Returns the actual path name.   */  public static Value realpath(Env env, Path path)  {    if (path == null)      return BooleanValue.FALSE;    String pathStr = path.realPath();    if (pathStr == null)      pathStr = path.getFullPath();    StringValue sb = env.createStringBuilder();        // php/164c    if (pathStr.endsWith("/"))      return sb.append(pathStr, 0, pathStr.length()- 1);    else      return sb.append(pathStr, 0, pathStr.length());  }  /**   * Renames a file   *   * @param fromPath the path to change to   * @param toPath the path to change to   */  public static boolean rename(Env env, StringValue from, StringValue to)  {    ProtocolWrapper wrapper = getProtocolWrapper(env, from);    if (wrapper != null)      return wrapper.rename(env, from, to);    Path fromPath = env.getPwd().lookup(from.toString());    Path toPath = env.getPwd().lookup(to.toString());    if (!fromPath.canRead()) {      env.warning(L.l("{0} cannot be read", fromPath.getFullPath()));      return false;    }    try {      return fromPath.renameTo(toPath);    } catch (IOException e) {      log.log(Level.FINE, e.toString(), e);      return false;    }  }  /**   * Rewinds the stream.   *   * @param is the file resource   */  public static Value rewind(Env env,			     @NotNull BinaryStream binaryStream)  {    if (binaryStream == null)      return BooleanValue.FALSE;    fseek(env, binaryStream, 0, SEEK_SET);        return BooleanValue.TRUE;  }  /**   * Rewinds the directory listing   *   * @param dirV the directory resource   */  public static void rewinddir(Env env, @NotNull DirectoryValue dir)  {    if (dir == null)      return;        dir.rewinddir();  }  /**   * remove a directory   */  public static boolean rmdir(Env env,                              StringValue filename,                              @Optional Value context)  {    ProtocolWrapper wrapper = getProtocolWrapper(env, filename);    if (wrapper != null)      // XXX options?      return wrapper.rmdir(env, filename, LongValue.ZERO);    // quercus/160s    // XXX: safe_mode    try {      Path path = env.getPwd().lookup(filename.toString());      if (!path.isDirectory()) {        env.warning(L.l("{0} is not a directory", path.getFullPath()));        return false;      }      return path.remove();    } catch (IOException e) {      log.log(Level.FINE, e.toString(), e);      return false;    }  }  /**   * Closes the directory   *   * @param dirV the directory resource   */  public static Value closedir(Env env, @NotNull DirectoryValue dirV)  {    if (dirV != null)      dirV.close();    return NullValue.NULL;  }  /**   * Scan the directory   *   * @param fileName the directory   */  public static Value scandir(Env env, String fileName,                              @Optional("1") int order,                              @Optional Value context)  {    if (fileName == null) {      env.warning(L.l("file name must not be NULL"));      return BooleanValue.FALSE;    }    try {      Path path = env.getPwd().lookup(fileName);      if (!path.isDirectory()) {        env.warning(L.l("{0} is not a directory", path.getFullPath()));        return BooleanValue.FALSE;      }      String []values = path.list();      Arrays.sort(values);      ArrayValue result = new ArrayValueImpl();      if (order == 1) {        for (int i = 0; i < values.length; i++)          result.append(new LongValue(i), env.createString(values[i]));      }      else {        for (int i = values.length - 1; i >= 0; i--) {          result.append(new LongValue(values.length - i - 1),          env.createString(values[i]));        }      }      return result;    } catch (IOException e) {      throw new QuercusModuleException(e);    }  }  /**   * Sets the write buffer.   */  public static int set_file_buffer(Env env, BinaryOutput stream,                                    int bufferSize)  {    return StreamModule.stream_set_write_buffer(env, stream, bufferSize);  }  /**   * Returns file statistics   */  public static Value stat(Env env, StringValue filename)  {    ProtocolWrapper wrapper = getProtocolWrapper(env, filename);    if (wrapper != null)      // XXX flags?      return wrapper.url_stat(env, filename, LongValue.ZERO);    Path path = env.getPwd().lookup(filename.toString());    return statImpl(env, path);  }  static Value statImpl(Env env, Path path)  {    if (! path.exists()) {      env.warning(L.l("stat failed for {0}", path.getFullPath().toString()));      return BooleanValue.FALSE;    }    ArrayValue result = new ArrayValueImpl();    result.put(path.getDevice());    result.put(path.getInode());    result.put(path.getMode());    result.put(path.getNumberOfLinks());    result.put(path.getUser());    result.put(path.getGroup());    result.put(path.getDeviceId());    result.put(path.getLength());    result.put(path.getLastAccessTime() / 1000L);    result.put(path.getLastModified() / 1000L);    result.put(path.getLastStatusChangeTime() / 1000L);    result.put(path.getBlockSize());    result.put(path.getBlockCount());    result.put("dev", path.getDevice());    result.put("ino", path.getInode());        result.put("mode", path.getMode());    result.put("nlink", path.getNumberOfLinks());    result.put("uid", path.getUser());    result.put("gid", path.getGroup());    result.put("rdev", path.getDeviceId());        result.put("size", path.ge

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?