Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Hack in a binary file writer.

  • Loading branch information...
commit 2c186ab909f72dbb9ae43b2897ad5f060f2ed7d0 1 parent 834ae01
@munificent authored
View
12 example/FileWriter.mag
@@ -0,0 +1,12 @@
+// A few examples of reading a file.
+
+import io
+
+create("example/out.magc") use with
+ it writeByte(0)
+ it writeByte(128)
+ it writeByte(255)
+ it writeInt(1234567)
+end
+
+print("Done")
View
59 src/com/stuffwithstuff/magpie/intrinsic/IOMethods.java
@@ -13,8 +13,14 @@
import com.stuffwithstuff.magpie.interpreter.Context;
import com.stuffwithstuff.magpie.interpreter.Obj;
import com.stuffwithstuff.magpie.util.FileReader;
+import com.stuffwithstuff.magpie.util.FileWriter;
public class IOMethods {
+ // TODO(bob): There is a big hack here. We use the same "File" Magpie class
+ // to encapsulate both a FileReader and a FileWriter. But the read and
+ // write methods assume one or the other and will blow up if you use the
+ // wrong one.
+
// TODO(bob): Hackish.
@Def("_setClasses(== File)")
public static class SetClasses implements Intrinsic {
@@ -28,8 +34,13 @@ public Obj invoke(Context context, Obj left, Obj right) {
@Doc("Closes the file.")
public static class Close implements Intrinsic {
public Obj invoke(Context context, Obj left, Obj right) {
- FileReader reader = (FileReader)left.getValue();
- reader.close();
+ if (left.getValue() instanceof FileReader) {
+ FileReader reader = (FileReader)left.getValue();
+ reader.close();
+ } else {
+ FileWriter writer = (FileWriter)left.getValue();
+ writer.close();
+ }
return context.nothing();
}
}
@@ -49,6 +60,22 @@ public Obj invoke(Context context, Obj left, Obj right) {
}
}
}
+
+ @Def("create(path is String)")
+ @Doc("Creates a new file at the given path.")
+ public static class Create implements Intrinsic {
+ public Obj invoke(Context context, Obj left, Obj right) {
+ String path = right.asString();
+
+ FileWriter writer;
+ try {
+ writer = new FileWriter(path);
+ return context.instantiate(sFileClass, writer);
+ } catch (IOException e) {
+ throw context.error("IOError", "Could not open file.");
+ }
+ }
+ }
@Def("(is File) isOpen")
@Doc("Returns true if the file is open.")
@@ -106,6 +133,34 @@ public Obj invoke(Context context, Obj left, Obj right) {
}
}
}
+
+ @Def("(is File) writeByte(is Int)")
+ @Doc("Writes the given byte (value from 0 to 255 inclusive) to this File.")
+ public static class WriteByte implements Intrinsic {
+ public Obj invoke(Context context, Obj left, Obj right) {
+ FileWriter writer = (FileWriter)left.getValue();
+ try {
+ writer.writeByte(right.asInt());
+ return right;
+ } catch (IOException e) {
+ throw context.error("IOError", "Could not write.");
+ }
+ }
+ }
+
+ @Def("(is File) writeInt(is Int)")
+ @Doc("Writes the given int to this File.")
+ public static class WriteInt implements Intrinsic {
+ public Obj invoke(Context context, Obj left, Obj right) {
+ FileWriter writer = (FileWriter)left.getValue();
+ try {
+ writer.writeInt(right.asInt());
+ return right;
+ } catch (IOException e) {
+ throw context.error("IOError", "Could not write.");
+ }
+ }
+ }
@Def("(is Directory) _contents")
@Doc("Gets the contents of the directory.")
View
38 src/com/stuffwithstuff/magpie/util/FileWriter.java
@@ -0,0 +1,38 @@
+package com.stuffwithstuff.magpie.util;
+
+import java.io.DataOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+public class FileWriter {
+ public FileWriter(String path) throws IOException {
+ mStream = new DataOutputStream(new FileOutputStream(path));
+ }
+
+ public boolean isOpen() {
+ return mStream != null;
+ }
+
+ public void close() {
+ // Do nothing if already closed.
+ if (mStream == null) return;
+
+ try {
+ mStream.close();
+ mStream = null;
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public void writeByte(int value) throws IOException {
+ mStream.writeByte(value);
+ }
+
+ public void writeInt(int value) throws IOException {
+ mStream.writeInt(value);
+ }
+
+ private DataOutputStream mStream;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.