Allow garbage-collecting object internal state
[jpcrr.git] / org / jpc / luaextensions / TextOutFile.java
blobcc0f66bce322ec2f6c65dad98ef2fc8db8fa1ba2
1 /*
2 JPC-RR: A x86 PC Hardware Emulator
3 Release 1
5 Copyright (C) 2007-2009 Isis Innovation Limited
6 Copyright (C) 2009 H. Ilari Liusvaara
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Based on JPC x86 PC Hardware emulator,
22 A project from the Physics Dept, The University of Oxford
24 Details about original JPC can be found at:
26 www-jpc.physics.ox.ac.uk
30 package org.jpc.luaextensions;
32 import mnj.lua.*;
34 import java.io.*;
35 import java.util.*;
36 import java.lang.reflect.*;
38 import org.jpc.plugins.LuaPlugin;
39 import org.jpc.jrsr.*;
40 import static org.jpc.Misc.parseStringToComponents;
41 import static org.jpc.Misc.errorDialog;
42 import static org.jpc.Misc.tempname;
43 import static org.jpc.Misc.nextParseLine;
44 import static org.jpc.Misc.parseString;
45 import static org.jpc.Misc.encodeLine;
47 //Locking this class is used for preventing termination and when terminating.
48 public class TextOutFile extends LuaPlugin.LuaResource
50 UTFOutputLineStream object;
52 public TextOutFile(LuaPlugin plugin, OutputStream os) throws IOException
54 super(plugin);
55 object = new UTFOutputLineStream(os);
58 public void destroy() throws IOException
60 object.close();
61 object = null;
64 public int luaCB_write(Lua l, LuaPlugin plugin)
66 l.pushNil();
67 try {
68 object.writeLine(l.checkString(2));
69 l.pushBoolean(true);
70 } catch(IOException e) {
71 l.pushBoolean(false);
72 l.pushString("IOException: " + e.getMessage());
73 return 2;
75 return 1;
78 public int luaCB_write_component(Lua l, LuaPlugin plugin)
80 l.pushNil();
81 try {
82 int c = l.objLen(l.value(2));
83 if(c <= 0) {
84 l.pushBoolean(false);
85 l.pushString("Required at least one component to write.");
86 return 2;
88 Object tab = l.value(2);
89 String[] x = new String[c];
90 for(int i = 0; i < c; i++) {
91 Object obj = l.getTable(tab, new Double(i + 1));
92 if(obj instanceof String)
93 x[i] = (String)obj;
94 else
95 l.error("Can't write non-string component");
97 object.encodeLine((Object[])x);
98 l.pushBoolean(true);
99 } catch(IOException e) {
100 l.pushBoolean(false);
101 l.pushString("IOException: " + e.getMessage());
102 return 2;
104 return 1;
107 public int luaCB_close(Lua l, LuaPlugin plugin)
109 try {
110 plugin.destroyLuaObject(l);
111 l.pushBoolean(true);
112 } catch(IOException e) {
113 l.pushBoolean(false);
114 l.pushString("IOException: " + e.getMessage());
115 return 2;
117 return 1;