Skip to content

Commit

Permalink
Hack in a binary file writer.
Browse files Browse the repository at this point in the history
  • Loading branch information
munificent committed Feb 13, 2012
1 parent 834ae01 commit 2c186ab
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
12 changes: 12 additions & 0 deletions 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")
59 changes: 57 additions & 2 deletions src/com/stuffwithstuff/magpie/intrinsic/IOMethods.java
Expand Up @@ -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 {
Expand All @@ -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();
}
}
Expand All @@ -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.")
Expand Down Expand Up @@ -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.")
Expand Down
38 changes: 38 additions & 0 deletions 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;
}

0 comments on commit 2c186ab

Please sign in to comment.