X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FDirectory.java;fp=src%2Forg%2Fibex%2Fjs%2FDirectory.java;h=5f5ac38e9fb1b7218d097c54767aef94e84c0518;hb=58c5bead1007713bd7e003d3d1729f3f83d554f2;hp=0000000000000000000000000000000000000000;hpb=2d9857ce8ee370ba30a4968942c604c0127e705e;p=org.ibex.core.git diff --git a/src/org/ibex/js/Directory.java b/src/org/ibex/js/Directory.java new file mode 100644 index 0000000..5f5ac38 --- /dev/null +++ b/src/org/ibex/js/Directory.java @@ -0,0 +1,117 @@ +// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] +package org.ibex.js; + +import org.ibex.util.*; +import java.util.*; +import java.io.*; + +// FEATURE: support for move +// FEATURE: support for bytestreams +// FEATURE: cache directories so we can do equality checking on them? +// FEATURE: autoconvert "true" to true and "0.3" to 0.3 on readback + +/** + * A crude mechanism for using a filesystem as object storage. + * + * This object represents a directory; writing a string, number, or + * boolean to any of its properties will create a file with the + * (encoded) property name as its filename and the "stringified" + * value as its contents. + * + * Writing 'null' to one of this object's properties will + * [recursively if necessary] delete the corresponding directory + * entry. + * + * Writing any other object to one of this object's properties will + * create a new Directory object and copy the other object's keys() + * into the new Directory. This means that assigning one directory + * to a property of another directory will copy the directory, + * not move it. There is currently no way to move directories. + * + * If an object is written to a property that already has an entry, + * the old one is deleted (equivalent to writing 'null') first. + * + * WARNING: when instantiating a Directory object with a file + * argument that points to a non-directory File, this class will + * delete that file and create a directory! + */ +public class Directory extends JS { + + File f; + + /** + * Create the directory object. Existing directories will be + * preserved; if a file is present it will be obliterated. + */ + public Directory(File f) throws IOException { + this.f = f; + if (!f.exists()) new Directory(new File(f.getParent())); + if (!f.isDirectory()) destroy(f); + f.mkdirs(); + } + + private static void destroy(File f) throws IOException { + if (!f.exists()) return; + if (f.isDirectory()) { + String[] entries = f.list(); + for(int i=0; i