added path method and more tests

This commit is contained in:
Add00
2023-08-24 17:09:20 -04:00
parent 214351abaa
commit eb4c44193c
2 changed files with 69 additions and 14 deletions

View File

@@ -9,21 +9,21 @@ typedef struct Toy_File
{ {
FILE* fp; FILE* fp;
Toy_RefString* mode; Toy_RefString* mode;
Toy_RefString* name; Toy_RefString* path;
} Toy_File; } Toy_File;
Toy_File* createToyFile(Toy_RefString* mode, Toy_RefString* name) { Toy_File* createToyFile(Toy_RefString* mode, Toy_RefString* path) {
Toy_File* file = TOY_ALLOCATE(Toy_File, 1); Toy_File* file = TOY_ALLOCATE(Toy_File, 1);
file->fp = NULL; file->fp = NULL;
file->mode = Toy_copyRefString(mode); file->mode = Toy_copyRefString(mode);
file->name = Toy_copyRefString(name); file->path = Toy_copyRefString(path);
return file; return file;
} }
void deleteToyFile(Toy_File* file) { void deleteToyFile(Toy_File* file) {
Toy_deleteRefString(file->mode); Toy_deleteRefString(file->mode);
Toy_deleteRefString(file->name); Toy_deleteRefString(file->path);
TOY_FREE(Toy_File, file); TOY_FREE(Toy_File, file);
} }
@@ -405,14 +405,14 @@ static int nativeRename(Toy_Interpreter* interpreter, Toy_LiteralArray* argument
} }
// rename the file // rename the file
int result = rename(Toy_toCString(file->name), newName); int result = rename(Toy_toCString(file->path), newName);
// open file again // open file again
file->fp = fopen(newName, Toy_toCString(file->mode)); file->fp = fopen(newName, Toy_toCString(file->mode));
// update the file object's name // update the file object's name
Toy_deleteRefString(file->name); Toy_deleteRefString(file->path);
file->name = Toy_createRefString(newName); file->path = Toy_createRefString(newName);
// return result // return result
Toy_Literal resultLiteral = TOY_TO_BOOLEAN_LITERAL(result == 0); Toy_Literal resultLiteral = TOY_TO_BOOLEAN_LITERAL(result == 0);
@@ -493,7 +493,7 @@ static int nativeSeek(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
Toy_RefString* orginString = TOY_AS_STRING(originLiteral); Toy_RefString* orginString = TOY_AS_STRING(originLiteral);
int offset = TOY_AS_INTEGER(offsetLiteral); int offset = TOY_AS_INTEGER(offsetLiteral);
int origin = 0; int origin = -1;
if (Toy_equalsRefStringCString(orginString, "bgn")) { if (Toy_equalsRefStringCString(orginString, "bgn")) {
origin = SEEK_SET; origin = SEEK_SET;
} }
@@ -713,6 +713,41 @@ static int nativeMode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
return 1; return 1;
} }
static int nativePath(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
if (arguments->count != 1) {
interpreter->errorOutput("Too many arguments name() expects zero arguments\n");
return -1;
}
Toy_Literal selfLiteral = Toy_popLiteralArray(arguments);
// parse the self (if it's an identifier)
Toy_Literal selfLiteralIdn = selfLiteral;
if (TOY_IS_IDENTIFIER(selfLiteral) && Toy_parseIdentifierToValue(interpreter, &selfLiteral)) {
Toy_freeLiteral(selfLiteralIdn);
}
// check self type
if (!TOY_IS_OPAQUE(selfLiteral) && TOY_GET_OPAQUE_TAG(selfLiteral) != TOY_OPAQUE_TAG_FILE) {
interpreter->errorOutput("Incorrect self type mode() expects a file type\n");
Toy_freeLiteral(selfLiteral);
return -1;
}
Toy_File* file = (Toy_File*)TOY_AS_OPAQUE(selfLiteral);
// return the result
Toy_Literal resultLiteral = TOY_TO_STRING_LITERAL(Toy_copyRefString(file->path));
Toy_pushLiteralArray(&interpreter->stack, resultLiteral);
// cleanup
Toy_freeLiteral(resultLiteral);
Toy_freeLiteral(selfLiteral);
return 1;
}
// call the hook // call the hook
typedef struct Natives { typedef struct Natives {
char* name; char* name;
@@ -803,6 +838,7 @@ int Toy_hookFileIO(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Lit
{"position", nativePosition}, {"position", nativePosition},
{"size", nativeSize}, {"size", nativeSize},
{"mode", nativeMode}, {"mode", nativeMode},
{"path", nativePath},
{NULL, NULL} {NULL, NULL}
}; };

View File

@@ -42,12 +42,16 @@ fn reset() {
assert writer.write(12.5) == true, "write float failed"; assert writer.write(12.5) == true, "write float failed";
assert writer.write("\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n") == true, "write long string failed"; assert writer.write("\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n") == true, "write long string failed";
// invaild types
assert writer.write([0, 1, 2]) == false, "write array failed";
assert writer.write(["hi": "world"]) == false, "write dict failed";
writer.close(); writer.close();
} }
// test open and close // test open and close
{ {
var reader = open(PATH, "r"); var reader = open(PATH);
assert reader != null, "open failed in reading mode"; assert reader != null, "open failed in reading mode";
assert reader.close() == true, "close failed"; assert reader.close() == true, "close failed";
@@ -75,8 +79,8 @@ fn reset() {
assert reader.write("writen text") == true, "write in read extended failed"; assert reader.write("writen text") == true, "write in read extended failed";
var result = reader.read(string); var result = reader.read(string);
print result; // print result;
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";
reader.close(); reader.close();
reset(); reset();
@@ -109,11 +113,26 @@ fn reset() {
// test seek // test seek
{ {
var reader = open(PATH, "r"); var reader = open(PATH, "r");
assert reader.seek("bgn", 6) == true, "seek operation failed"; assert reader.seek("bgn", 6) == true, "seek from bgn failed";
var contents = reader.read(string); var contents = reader.read(string);
assert contents == " World!\n", "seek failed to move file position"; assert contents == " World!\n", "seek failed to move file position";
assert reader.seek("end", -2) == true, "seek from end failed";
contents = reader.read(string);
assert contents == "!\n", "seek failed to move file position";
assert reader.seek("cur", -2) == true, "seek from cur failed";
contents = reader.read(string);
assert contents == "!\n", "seek failed to move file position";
assert reader.seek("CUR", 0) == false, "seek origin failed";
assert reader.seek("End", 0) == false, "seek origin failed";
assert reader.seek("beG", 0) == false, "seek origin failed";
assert reader.seek("xxx", 0) == false, "seek origin failed";
reader.close(); reader.close();
} }