Skip to content

Commit

Permalink
fs.move(path1, path2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Imrglop committed Dec 1, 2024
1 parent 0f0c129 commit 9268002
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
25 changes: 23 additions & 2 deletions src/client/script/lib/libraries/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ JsValueRef Filesystem::initialize(JsValueRef parent) {
Chakra::DefineFunc(ret, appendSync, L"append", this);
Chakra::DefineFunc(ret, deleteFile, L"delete", this);
Chakra::DefineFunc(ret, readdirSync, L"readDirectory", this);
Chakra::DefineFunc(ret, moveSync, L"move", this);
return ret;
}

Expand Down Expand Up @@ -271,8 +272,6 @@ JsValueRef Filesystem::readdirSync(JsValueRef callee, bool isConstructor, JsValu
auto ret = Chakra::GetUndefined();
if (!Chakra::VerifyArgCount(argCount, 2)) return ret;
if (!Chakra::VerifyParameters({ {arguments[1], JsString} })) return ret;
std::wifstream ifs;
std::wstringstream wss;
auto thi = reinterpret_cast<Filesystem*>(callbackState);

auto path = thi->getPath(Chakra::GetString(arguments[1]));
Expand All @@ -298,6 +297,28 @@ JsValueRef Filesystem::readdirSync(JsValueRef callee, bool isConstructor, JsValu
return array;
}

JsValueRef Filesystem::moveSync(JsValueRef callee, bool isConstructor, JsValueRef* arguments, unsigned short argCount, void* callbackState) {
auto ret = Chakra::GetUndefined();
if (!Chakra::VerifyArgCount(argCount, 3)) return ret;
if (!Chakra::VerifyParameters({ {arguments[1], JsString}, {arguments[1], JsString} })) return ret;
std::wifstream ifs;
std::wstringstream wss;
auto thi = reinterpret_cast<Filesystem*>(callbackState);

auto path1 = thi->getPath(Chakra::GetString(arguments[1]));
auto path2 = thi->getPath(Chakra::GetString(arguments[1]));

std::error_code errCode;
fs::rename(path1, path2, errCode);

if (errCode.value() != ERROR_ALREADY_EXISTS && errCode.value() != 0) {
Chakra::ThrowError(L"Filesystem error: " + util::StrToWStr(errCode.message()));
return JS_INVALID_REFERENCE;
}

return Chakra::GetUndefined();
}

void Filesystem::FSAsyncOperation::getArgs() {
JsValueRef err;
JS::JsIntToNumber(this->err, &err);
Expand Down
28 changes: 3 additions & 25 deletions src/client/script/lib/libraries/Filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,6 @@ class Filesystem : public JsLibrary {
Filesystem(JsScript* owner) : JsLibrary(owner, L"filesystem") {}
private:


//template <typename T>
//void runAsyncOperation(std::function<void(T)> func)

// can someone tell past me about std::vector....
/*
struct BufferRef {
BYTE* data;
size_t size;
BufferRef(size_t sz) : size(sz), data(nullptr) {}
void alloc(size_t sz) {
if (data) delete[] data;
data = new BYTE[sz];
size = sz;
}
~BufferRef() {
if (data) {
delete[] data;
}
}
};*/

class FSAsyncOperation : public JsScript::AsyncOperation {
public:

Expand Down Expand Up @@ -86,5 +61,8 @@ class Filesystem : public JsLibrary {
static JsValueRef CALLBACK readdirSync(JsValueRef callee, bool isConstructor,
JsValueRef* arguments, unsigned short argCount,
void* callbackState);
static JsValueRef CALLBACK moveSync(JsValueRef callee, bool isConstructor,
JsValueRef* arguments, unsigned short argCount,
void* callbackState);

};

0 comments on commit 9268002

Please sign in to comment.