Permalink
Browse files

Partially implement file/directory enumeration for virtual volumes

Support the Create and CreateNew FileModes
  • Loading branch information...
1 parent 90c11ca commit 5ce3dd8156f30ad30a28b27c5867c4a9dfab178b @kg kg committed Jun 7, 2012
Showing with 77 additions and 12 deletions.
  1. +43 −1 Libraries/JSIL.Storage.js
  2. +34 −11 Libraries/JSIL.XNAStorage.js
View
@@ -118,7 +118,11 @@ JSIL.MakeClass($jsilcore.System.Object, "VirtualVolume", true, [], function ($)
}
return path;
- });
+ });
+
+ $.RawMethod(false, "enumerate", function (nodeType, searchPattern) {
+ return this.rootDirectory.enumerate(nodeType, searchPattern);
+ });
$.RawMethod(false, "createDirectory", function (path) {
path = this.normalizePath(path);
@@ -320,6 +324,44 @@ JSIL.MakeClass($jsilcore.System.Object, "VirtualDirectory", true, [], function (
}
});
+ $.RawMethod(false, "enumerate", function (nodeType, searchPattern) {
+ var result = [];
+ var predicate = function (fn) { return true; };
+
+ if (searchPattern) {
+ var starRegex = /\*/g;
+ var questionMarkRegex = /\?/g;
+ var dotRegex = /\./g
+
+ var regexText = searchPattern
+ .replace(dotRegex, "\\.")
+ .replace(starRegex, "(.*)")
+ .replace(questionMarkRegex, ".");
+
+ var regex = new RegExp(regexText, "i");
+
+ predicate = function (fn) {
+ return regex.test(fn);
+ };
+ }
+
+ if (nodeType !== "directory") {
+ for (var k in this.files) {
+ if (predicate(k))
+ result.push(k)
+ }
+ }
+
+ if (nodeType !== "file") {
+ for (var k in this.directories) {
+ if (predicate(k))
+ result.push(k)
+ }
+ }
+
+ return result;
+ });
+
$.RawMethod(false, "toString", function () {
return "<Virtual Directory '" + this.path + "' in volume '" + this.volume.name + "'>";
});
@@ -51,8 +51,7 @@ JSIL.ImplementExternals("Microsoft.Xna.Framework.Storage.StorageContainer", func
if (!this.volume)
throw new Error("No storage providers loaded");
- this.volume.createFile(file, true);
- return this.OpenFileInternal(file);
+ return this.OpenFileInternal(file, true);
}
);
@@ -147,36 +146,51 @@ JSIL.ImplementExternals("Microsoft.Xna.Framework.Storage.StorageContainer", func
$.Method({Static:false, Public:true }, "GetDirectoryNames",
(new JSIL.MethodSignature($jsilcore.TypeRef("System.Array", [$.String]), [], [])),
function GetDirectoryNames () {
- throw new Error('Not implemented');
+ if (!this.volume)
+ return [];
+
+ return this.volume.enumerate("directory");
}
);
$.Method({Static:false, Public:true }, "GetDirectoryNames",
(new JSIL.MethodSignature($jsilcore.TypeRef("System.Array", [$.String]), [$.String], [])),
function GetDirectoryNames (searchPattern) {
- throw new Error('Not implemented');
+ if (!this.volume)
+ return [];
+
+ return this.volume.enumerate("directory", searchPattern);
}
);
$.Method({Static:false, Public:true }, "GetFileNames",
(new JSIL.MethodSignature($jsilcore.TypeRef("System.Array", [$.String]), [], [])),
function GetFileNames () {
- throw new Error('Not implemented');
+ if (!this.volume)
+ return [];
+
+ return this.volume.enumerate("file");
}
);
$.Method({Static:false, Public:true }, "GetFileNames",
(new JSIL.MethodSignature($jsilcore.TypeRef("System.Array", [$.String]), [$.String], [])),
function GetFileNames (searchPattern) {
- throw new Error('Not implemented');
+ if (!this.volume)
+ return [];
+
+ return this.volume.enumerate("file", searchPattern);
}
);
- $.RawMethod(false, "OpenFileInternal", function (filename) {
+ $.RawMethod(false, "OpenFileInternal", function (filename, createNew) {
if (!this.volume)
throw new Error("No storage providers loaded");
- var file = this.volume.resolvePath(filename, true);
+ var file = this.volume.resolvePath(filename, !createNew);
+
+ if (createNew && !file)
+ file = this.volume.createFile(filename, true);
var fileStream = JSIL.CreateInstanceOfType(
System.IO.FileStream.__Type__, "$fromVirtualFile", [file]
@@ -188,7 +202,10 @@ JSIL.ImplementExternals("Microsoft.Xna.Framework.Storage.StorageContainer", func
$.Method({Static:false, Public:true }, "OpenFile",
(new JSIL.MethodSignature($xnaasms.corlib.TypeRef("System.IO.Stream"), [$.String, $xnaasms.corlib.TypeRef("System.IO.FileMode")], [])),
function OpenFile (file, fileMode) {
- return this.OpenFileInternal(file);
+ return this.OpenFileInternal(
+ file,
+ (fileMode == System.IO.FileMode.Create) || (fileMode == System.IO.FileMode.CreateNew)
+ );
}
);
@@ -198,7 +215,10 @@ JSIL.ImplementExternals("Microsoft.Xna.Framework.Storage.StorageContainer", func
$xnaasms.corlib.TypeRef("System.IO.FileAccess")
], [])),
function OpenFile (file, fileMode, fileAccess) {
- return this.OpenFileInternal(file);
+ return this.OpenFileInternal(
+ file,
+ (fileMode == System.IO.FileMode.Create) || (fileMode == System.IO.FileMode.CreateNew)
+ );
}
);
@@ -208,7 +228,10 @@ JSIL.ImplementExternals("Microsoft.Xna.Framework.Storage.StorageContainer", func
$xnaasms.corlib.TypeRef("System.IO.FileAccess"), $xnaasms.corlib.TypeRef("System.IO.FileShare")
], [])),
function OpenFile (file, fileMode, fileAccess, fileShare) {
- return this.OpenFileInternal(file);
+ return this.OpenFileInternal(
+ file,
+ (fileMode == System.IO.FileMode.Create) || (fileMode == System.IO.FileMode.CreateNew)
+ );
}
);

0 comments on commit 5ce3dd8

Please sign in to comment.