throw new ExistenceException(this, 1, "source_sink", a1, "");
}
PushbackReader in = new PushbackReader(new BufferedReader(
new FileReader(file)), Prolog.PUSHBACK_SIZE);
streamObject = new JavaObjectTerm(in);
opts = new ListTerm(SYM_INPUT, opts);
} else if (a2.equals(SYM_WRITE)) {
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(file, false)));
streamObject = new JavaObjectTerm(out);
opts = new ListTerm(SYM_OUTPUT, opts);
} else if (a2.equals(SYM_APPEND)) {
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(file, true)));
streamObject = new JavaObjectTerm(out);
opts = new ListTerm(SYM_OUTPUT, opts);
} else {
throw new IllegalDomainException(this, 2, "io_mode", a2);
}
} catch (IOException e) {
throw new PermissionException(this, "open", "source_sink", a1, "");
}
if (engine.getStreamManager().containsKey(streamObject)) {
throw new InternalException("stream object is duplicated");
}
// stream_options
a4 = a4.dereference();
Term tmp = a4;
while (!tmp.isNil()) {
if (tmp.isVariable()) {
throw new PInstantiationException(this, 4);
}
if (!tmp.isList()) {
throw new IllegalTypeException(this, 4, "list", a4);
}
Term car = ((ListTerm) tmp).car().dereference();
if (car.isVariable()) {
throw new PInstantiationException(this, 4);
}
if (car.isStructure()) {
SymbolTerm functor = ((StructureTerm) car).functor();
Term[] args = ((StructureTerm) car).args();
if (functor.equals(SYM_ALIAS_1)) {
alias = args[0].dereference();
if (!alias.isSymbol()) {
throw new IllegalDomainException(this, 4,
"stream_option", car);
}
if (engine.getStreamManager().containsKey(alias)) {
throw new PermissionException(this, "open",
"source_sink", car, "");
}
} else {
throw new IllegalDomainException(this, 4, "stream_option",
car);
}
} else {
throw new IllegalDomainException(this, 4, "stream_option", car);
}
tmp = ((ListTerm) tmp).cdr().dereference();
}
Term[] args1 = { SYM_TEXT };
Term[] args2 = { a2 };
Term[] args3 = { SymbolTerm.makeSymbol(file.getAbsolutePath()) };
opts = new ListTerm(new StructureTerm(SYM_TYPE_1, args1), opts);
opts = new ListTerm(new StructureTerm(SYM_MODE_1, args2), opts);
opts = new ListTerm(new StructureTerm(SYM_FILE_NAME_1, args3), opts);
if (alias != null) {
engine.getStreamManager().put(alias, streamObject);
Term[] as = { alias };
opts = new ListTerm(new StructureTerm(SYM_ALIAS_1, as), opts);
}
((VariableTerm) a3).bind(streamObject, engine.trail);
engine.getStreamManager().put(streamObject, opts);
return cont;
}