mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
input and output can now be closed
This commit is contained in:
@@ -138,7 +138,11 @@ static int nativeClose(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments
|
|||||||
Toy_File* file = (Toy_File*)TOY_AS_OPAQUE(selfLiteral);
|
Toy_File* file = (Toy_File*)TOY_AS_OPAQUE(selfLiteral);
|
||||||
|
|
||||||
int result = 0;
|
int result = 0;
|
||||||
if (file->fp != NULL) {
|
if (
|
||||||
|
file->fp != stdout &&
|
||||||
|
file->fp != stdin &&
|
||||||
|
file->fp != NULL
|
||||||
|
) {
|
||||||
result = fclose(file->fp);
|
result = fclose(file->fp);
|
||||||
file->fp = NULL;
|
file->fp = NULL;
|
||||||
}
|
}
|
||||||
@@ -811,18 +815,30 @@ int Toy_hookFileIO(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Lit
|
|||||||
createToyVariableInt(&variables[1], "MAX_FILES_OPEN", FOPEN_MAX);
|
createToyVariableInt(&variables[1], "MAX_FILES_OPEN", FOPEN_MAX);
|
||||||
createToyVariableInt(&variables[2], "END_OF_FILE", EOF);
|
createToyVariableInt(&variables[2], "END_OF_FILE", EOF);
|
||||||
|
|
||||||
|
Toy_RefString* outMode = Toy_createRefString("w");
|
||||||
|
Toy_RefString* outName = Toy_createRefString("output");
|
||||||
|
|
||||||
static Toy_File* outFile;
|
static Toy_File* outFile;
|
||||||
outFile = createToyFile(Toy_createRefString("w"), Toy_createRefString("output"));
|
outFile = createToyFile(outMode, outName);
|
||||||
outFile->fp = stdout;
|
outFile->fp = stdout;
|
||||||
|
|
||||||
createToyVariableFile(&variables[3], "output", outFile);
|
createToyVariableFile(&variables[3], "output", outFile);
|
||||||
|
|
||||||
|
Toy_deleteRefString(outMode);
|
||||||
|
Toy_deleteRefString(outName);
|
||||||
|
|
||||||
|
Toy_RefString* inMode = Toy_createRefString("r");
|
||||||
|
Toy_RefString* inName = Toy_createRefString("input");
|
||||||
|
|
||||||
static Toy_File* inFile;
|
static Toy_File* inFile;
|
||||||
inFile = createToyFile(Toy_createRefString("r"), Toy_createRefString("input"));
|
inFile = createToyFile(inMode, inName);
|
||||||
inFile->fp = stdin;
|
inFile->fp = stdin;
|
||||||
|
|
||||||
createToyVariableFile(&variables[4], "input", inFile);
|
createToyVariableFile(&variables[4], "input", inFile);
|
||||||
|
|
||||||
|
Toy_deleteRefString(inMode);
|
||||||
|
Toy_deleteRefString(inName);
|
||||||
|
|
||||||
// store the library in an aliased dictionary
|
// store the library in an aliased dictionary
|
||||||
if (!TOY_IS_NULL(alias)) {
|
if (!TOY_IS_NULL(alias)) {
|
||||||
// make sure the name isn't taken
|
// make sure the name isn't taken
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ fn reset() {
|
|||||||
assert reader.read(string) == "\ntest", "read string failed";
|
assert reader.read(string) == "\ntest", "read string failed";
|
||||||
|
|
||||||
// invaild types
|
// invaild types
|
||||||
assert input.read(type) == null, "read type failed";
|
assert reader.read(type) == null, "read type failed";
|
||||||
assert input.read(any) == null, "read any failed";
|
assert reader.read(any) == null, "read any failed";
|
||||||
|
|
||||||
reader.close();
|
reader.close();
|
||||||
}
|
}
|
||||||
@@ -76,6 +76,7 @@ fn reset() {
|
|||||||
|
|
||||||
var result = reader.read(string);
|
var result = reader.read(string);
|
||||||
assert (result == "d!\n" || result == "d!\\r\n"), "read in read extended failed";
|
assert (result == "d!\n" || result == "d!\\r\n"), "read in read extended failed";
|
||||||
|
print result;
|
||||||
|
|
||||||
reader.close();
|
reader.close();
|
||||||
reset();
|
reset();
|
||||||
@@ -137,4 +138,7 @@ fn reset() {
|
|||||||
reader.close();
|
reader.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
input.close();
|
||||||
|
output.close();
|
||||||
|
|
||||||
print "All good";
|
print "All good";
|
||||||
Reference in New Issue
Block a user