Walnut/Ordinary Programming/InputOutput

From Erights

(Difference between revisions)
Jump to: navigation, search
 
Line 3: Line 3:
===I/O===
===I/O===
-
E file objects are created with the <file:name> expression (which produces a [http://www.erights.org/javadoc/java/io/File.html "tamed" Java File Object], i.e., a File object that follows capability security discipline). If you can hard-code the name, you usually do not need to use quotes (unless there is a space or another invalid url character). If the name is contained in a variable or is retrieved with a function call, you must <font color="#FF0000">XXX</font> place a space between the colon and the representation of the name. If the word "file" is replaced with a single letter like "c", the letter is assumed to be the drive letter on a Windows machine:
+
E file objects are created with the <file:name> expression (which produces a [http://www.erights.org/javadoc/java/io/File.html "tamed" Java File Object], i.e., a File object that follows capability security discipline). If the word "file" is replaced with a single letter like "c", the letter is assumed to be the drive letter on a Windows machine. This form can only be used when the file name is known literally, and contains only valid URI characters.
 +
 
 +
If the file name is calculated, i.e., contained in a variable or retrieved with a function call, or if it contains invalid URI characters like space, you must use the calculated name to index into a parent directory or the file system itself. The file system is represented by the <file> expression.
<pre>
<pre>
Line 17: Line 19:
   
   
  #Using a single character to specify a Windows drive
  #Using a single character to specify a Windows drive
-
  def file4 := <c:/docs/myFile.txt>
+
  def file4 := <file:c:/docs/myFile.txt>
-
  def file5 := <c:\docs\myFile.txt></nowiki>
+
  def file5 := <c:/docs/myFile.txt>
 +
def file6 := <c:\docs\myFile.txt></nowiki>
</pre>
</pre>
-
Note that in the file4 example, we used slashes, not backslashes, to separate directories on a Windows drive. ''In <span class="e">''E''</span>, the slash always works as a directory separator no matter the underlying operating system. ''
+
Note that in the file5 example, we used slashes, not backslashes, to separate directories on a Windows drive. ''In <span class="e">''E''</span>, the slash always works as a directory separator no matter the underlying operating system. ''
-
When constructing the filePath string we used double backslashes because in strings the backslash must be escaped by a backslash. A double backslash was not required in the hardwired file (file5).
+
When constructing the filePath string we used double backslashes because in strings the backslash must be escaped by a backslash. A double backslash was not required in the hardwired file (file6).
File objects can represent directories as well as simple files. Files inside the directory can be accessed by indexing using square brackets:
File objects can represent directories as well as simple files. Files inside the directory can be accessed by indexing using square brackets:
Line 49: Line 52:
<span style="color:red">stdout and stderr are both TextWriters in the E environment.</span>
<span style="color:red">stdout and stderr are both TextWriters in the E environment.</span>
-
==== Example: looping through a directory tree searching for a file that contains a particular line====
+
==== Example: recursing through a directory tree searching for a file ====
-
<span style="color:red">write the example</span>
+
def findFile(targetFileName, dir) :void {
 +
 +
    def iterator(filename, file) {
 +
        if (filename == targetFileName) {
 +
            println(`Found $targetFileName at ${file.getPath()}`)
 +
        }
 +
        if (file.isDirectory()) {
 +
            findFile(targetFileName, file)
 +
        }
 +
    }
 +
    dir.iterate(iterator)
 +
}
 +
 +
def ehomeDir := <file>[interp.getProps()["e.home"]]
 +
findFile("Substituter.class", ehomeDir)

Latest revision as of 22:24, 26 February 2007


I/O

E file objects are created with the <file:name> expression (which produces a "tamed" Java File Object, i.e., a File object that follows capability security discipline). If the word "file" is replaced with a single letter like "c", the letter is assumed to be the drive letter on a Windows machine. This form can only be used when the file name is known literally, and contains only valid URI characters.

If the file name is calculated, i.e., contained in a variable or retrieved with a function call, or if it contains invalid URI characters like space, you must use the calculated name to index into a parent directory or the file system itself. The file system is represented by the <file> expression.


 # E sample
 #File objects for hardwired files:
 def file1 := <file:myFile.txt>
 def file2 := <file:/home/marcs/myFile.txt>
 
 #Using a variable for a file name:
 def filePath := "c:\\docs\\myFile.txt"
 def file3 := <file>[filePath]
 
 #Using a single character to specify a Windows drive
 def file4 := <file:c:/docs/myFile.txt>
 def file5 := <c:/docs/myFile.txt>
 def file6 := <c:\docs\myFile.txt>

Note that in the file5 example, we used slashes, not backslashes, to separate directories on a Windows drive. In E, the slash always works as a directory separator no matter the underlying operating system.

When constructing the filePath string we used double backslashes because in strings the backslash must be escaped by a backslash. A double backslash was not required in the hardwired file (file6).

File objects can represent directories as well as simple files. Files inside the directory can be accessed by indexing using square brackets:


 # E syntax
 def dir := <file:/home/marcs/dataDir/>
 def file6 := dir["myFile.txt"]

Both text files and directories work in conjunction with the "for" loop. With a directory, the loop iterates through the files in the directory. With a text file, the loop iterates through the lines of the file; each line-object is terminated with a newline ('\n') character. The lines in the loop are terminated with newline regardless of the underlying operating system, regardless of the operating system's end-of-line designation.

Files respond to the following messages (see the Edoc for a complete list).

  • exists -- returns true if the file exists
  • deepReadOnly -- returns a file object that can be read, but not written. If the file is a directory, the readOnly nature is transitive to all files and directories reached via this object.
  • setText(textString) -- creates the file and/or replaces the file contents with the text. When written out, the end-of-line designator is automatically converted to the local end-of-line mark for the underlying operating system. On Windows, for example, each line is automatically terminated by a cr/lf pair.
  • getText -- returns the text from the file. Regardless of underlying operating system, the returned text uses a single newline character as the end-of-line mark.
  • getBytes() -- returns the bytes of the file in a byte ConstList
  • setBytes(byteList)

stdout and stderr are both TextWriters in the E environment.

Example: recursing through a directory tree searching for a file

def findFile(targetFileName, dir) :void {

    def iterator(filename, file) {
        if (filename == targetFileName) {
            println(`Found $targetFileName at ${file.getPath()}`)
        }
        if (file.isDirectory()) {
            findFile(targetFileName, file)
        }
    }
    dir.iterate(iterator)
}

def ehomeDir := <file>[interp.getProps()["e.home"]]
findFile("Substituter.class", ehomeDir)
Personal tools
more tools