![]() |
|
|
This documents describes a proposed API for the JavaScript File object. A lot of the API has been implemented (but not tested), and there is work yet to be done. Please send comments to norris@netscape.com. Many scripts in JavaScript require I/O capabilities and particularly access to normal files. This document describes such a File object that can be implemented by host implementations. The interface is a mix of the Server Side JS File object and the Java File object and tries to follow existing implicit JavaScript usage ( readln instead of readLine, for example ). The File object gives two ways to access the data inside a file: a text oriented access, based on characters, and a binary oriented access, based on bytes. In the text mode, maximum string length is currently 256. Different encodings are allowed, with ASCII, UTF8 (unicode), and UCS2 (binary) currently supported. The implementation is currenly based mostly on NSPR PRFileDesc, (sorry, no public link for this yet) but there is also a back door that allows one to create files based on standard C FILE type. This is used to give the ability to initialize JavaScript files based on standard streams stdin, stdout, and stderr. This is also how pipes are implemented. With FILE-style files, however, not all operations are uniformly supported everywhere, after all it is the goal of NSPR to hide the discrepancies among different platforms. 0.0 Changelog
1.0 Filenames
Examples of possible prefix strings are "/" to indicate the Unix root directory, "c:" to specify a Windows drive letter, and "file:" to indicate a file URL. When a file is constructed, leading and trailing spaces are removed from the filename, so new File(" abc.txt ") just creates a file called abc.txt. Filenames starting and ending with '|' are interpreted as pipes a la Perl. We should be able to do things like this:
mail.writeln("I love JavaScript.\nPipe support is especially good!"); mail.close(); 2.0 Properties of the File constructor
Note: The first tree properties should probably be enabled only in certain settings, not in the browser context, for example. Keep in mind that some methods such as isFile, for example, will return an error if called on one of them. 2.1 Properties of File instances
2.2 Instance methods
3.0 Properties of the File constructor
File constructor. Expects filename as the only argument. The filename can be preceeded with "file://", which is stripped off. If no argument is supplied, current working directory is the file object that is returned. A File object that represents the standard input stream. Initialized in certain settings. A File object that represents the standard output stream. Initialized in certain settings A File object that represents the standard error stream. Initialized in certain settings. This property contains a File object pointing to the current directory. Assigning to this property causes the current directory to change. The following syntax is supported:
which is functionally equivalent to File.currentDir = "/"; The system name separator. For example, "/" on Unix. 3.1 Properties of File instances
This property contains the name of the file. Defined as "Standard input(output/error) stream" for streams. Undefined for pipes. This property contains the canonical path to the file. Defined as "Standard input(output/error) stream" for streams. Returns the path that was used to create the pipe for pipes. Initial and trailing spaces are removed. For a directory, the number of files in it not counting the current directory and parent directory entries. For a file, this property contains the number of characters in the file. Undefined for pipes and standard streams. This property contains a File object pointing to the directory containing the file. If the file isn't contained in anything (root directory), the property is null. Undefined for pipes and standard streams. This property contains a string specifying the type of data or encoding used when the file was opened. Specific text encodings supported are "ascii", "unicode", and "binary". Undefined for directories and closed files. This property contains the mode of the file. It can be somewhat different from what was passed to open if you have repeating attributes ("read,read") or if some attributes were ignored. Returns undefined if the file is closed. This property contains the current offset in the file. this is the only property that may be set. This property is undefined if the file is not in random access mode or if it is a pipe or a standard stream. Also undefined for closed files. Setting this property performs a seek operation on the file. This property contains true if the file is a regular data file. False for pipes and standard streams. Note that on some platforms, both "isDirectory" and "isFile" may return false for some types of files (for example, unix named pipes). This property contains true if the file is a directory. False for pipes and standard streams. This property equals true if the file exists. False for pipes and standard streams. This property contains true if the file has been successfully opened and is still open. Keep in mind that standard streams and pipes are open by default and don't requre opening. This property contains true for pipes and standard streams. This property contains a Date object for the time the file was last modified. Undefined for pipes and standard streams. This property contains true if the file can be read. The mode with which the file was opened (if it was) is respected. true for File.input, false for other streams. true for pipes starting with '|', false otherwise. This property contains true if the file can be written. The mode with which the file was opened (if it was) is respected. true for File.output and File.error, false for File.input. true for pipes ending with '|', false otherwise. This property is true if the file was open for appending. False for pipes and standard streams. Undefined for closed files. This property is true if the file was open with replace flag enabled. False for pipes and standard streams. Undefined for closed files. This property contains a flag indicating that the file is being opened in random access mode. This means the "position" property can be read and set. False for pipes and standard streams. Undefined for closed files. This property contains true if the file is in automatic flush mode. If this is set to true, each call to writeln will flush the stream. False for pipes and standard streams. Undefined for closed files. 3.1.1 Special properties for directories
These properties have the same names as the files in the directory.
3.2 Instance methods
If no mode is specified, the default mode that is used for regular files
is "read,append,create". Pipes are automatically open at creation time
with mode defaulting to "read" or "write" depending on pipe type. If no
type is specified, the default type is "text".
You can also use the format "create=yes, append=yes", etc. Please note
that the case of letters is important.
Causes a warning if the file is a native file or if the file is a directory. In both these cases the return value is true. If the file is non-native and already open, a warning is generated and the file is reopened. This might make sense in a case when you want to go to the beginning of the file,etc., but you can probably use seek instead. This method closes the file. Causes a warning if the file is not open or is a native file and returns false. This method is called automatically on an open file object when the object goes out of scope. This method removes the file or removes the directory given as argument. Normally if remove is called on a directory it is only removed if the directory is empty. Fails if the file is open. This method tries to make a raw copy of the source file into a destination file. It will fail if the source file doesn't exist, or is a directory. This method moves/renames the file. Fails if the file is open. This method forces to flush the output buffers of the file. Fails if the file is closed. This methods skips the next numChars characters or bytes depending on the current access mode for the file. If the file is closed, it is opened before performing the operation.This function can accept both positive and negative parameters. Reports a warning and returns undefined if called on a directory. In the operation is successful, returns the current position in file. This method reads numChars characters and returns them in a string. If there are less than numChars characters left to read, then the available characters are returned. If the file is at EOF, then null is returned. This methods reads characters until an end of line/file is encountered then returns the string for these characters (without any end of line character). This methods reads the rest of the file and returns an array with a slot for each line of the file. This method converts each argument to a string, then writes it to the file (without separators). This method is similar to write, but adds a platform dependent end of line after outputting the last argument. This method takes an array as an argument, and calls writeln() on each element. This method returns an array. if the file is a directory, an slot is created for each file inside that directory, and a property with the file name is created too. Both point to a File object referring to the file. filter is an optional argument that should be a function or
a regular expression, and is used to filter the array. If the argument
is a regular expression, the array will only contain files for which the
pattern matches. If the argument is a function, the array will only contain
files for which the function returned true when called with the file name
as argument.
This method attempts to create a directory inside the file directory. For instance,
f.mkdir("win95")
f.mkdir("win95") This method returns the canonical path to the file. return the name of the file as a "file:" URL. Undefined for pipes and standard streams. 4.0 I want to playFlaming disclaimer: Do not play with the File object if you are not prepared to have your hard drive erased, smashed, and broken into little bits! It mostly works right now, but no guarantees. Currently the File object can be built into the core JS engine using a define. This is wrong. Once XPCOM settles down, the File object should be COM-ified. To play with/hack on the File object, get the latest and greatest (usually) JavaScript using CVS: cvs co -rSpiderMonkeyDev_BRANCH mozilla/js/src Be warned that this gives you the active development branch for JavaScript, which offers few guarantees of stability. To build (the reference interpreter on UNIX in this example) with the File object, define JS_HAS_FILE_OBJECT while building. You also need to build against NSPR which you can do by defining JS_THREADSAFE. cd mozilla/js/src
5.0 Status
Questions to Norris Boyd Last modified: Fri Dec 18 20:47:48 PST 1998
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Copyright © 1998 The Mozilla Organization. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||