diff --git a/src/Binding/JSWindow.cpp b/src/Binding/JSWindow.cpp index 67de9c66..f243c739 100644 --- a/src/Binding/JSWindow.cpp +++ b/src/Binding/JSWindow.cpp @@ -702,16 +702,15 @@ bool JSWindow::JS_openURL(JSContext *cx, JS::CallArgs &args) bool JSWindow::JS_exec(JSContext *cx, JS::CallArgs &args) { - JS::RootedString url(cx); if (!JS_ConvertArguments(cx, args, "S", url.address())) { return false; } JSAutoByteString curl(cx, url); - const char *ret = SystemInterface::GetInstance()->execute(curl.ptr()); + std::unique_ptr ret(SystemInterface::GetInstance()->execute(curl.ptr()); - JS::RootedString retStr(cx, JS_NewStringCopyZ(cx, ret)); + JS::RootedString retStr(cx, JS_NewStringCopyZ(cx, ret.get())); args.rval().setString(retStr); return true; diff --git a/src/Interface/linux/System.cpp b/src/Interface/linux/System.cpp index 7845e076..12f40797 100644 --- a/src/Interface/linux/System.cpp +++ b/src/Interface/linux/System.cpp @@ -183,7 +183,6 @@ void System::initSystemUI() } } - const char *System::cwd() { static char dir[MAXPATHLEN]; @@ -192,14 +191,10 @@ const char *System::cwd() return dir; } + const char *System::getLanguage() { - - const char *lang; - - lang = setlocale(LC_IDENTIFICATION, NULL); - - return lang; + return setlocale(LC_IDENTIFICATION, NULL); } void System::sendNotification(const char *title, @@ -216,26 +211,27 @@ void System::sendNotification(const char *title, const char *System::execute(const char *cmd) { - char buffer[128]; - FILE *fp; + // caller must free the allocated string when done. std::string *result = new std::string(); + result.reserve(128); - fp = popen(cmd, "r"); - if (fp == nullptr) { - return nullptr; - } + struct pipeDeleter { + void operator()(FILE *ptr) const noexcept { + pclose(ptr); + } + }; - while (!feof(fp)) { - if (fgets(buffer, 128, fp) != nullptr) { - result->append(buffer); + std::unique_ptr pipe(popen(cmd, "r")); + + if (pipe) { + while (!feof(pipe.get())) { + if (fgets(const_cast(result.data()), result.capacity(), pipe.get()) != nullptr) { + result += result.data(); + } } } - pclose(fp); - - // FIXME : Memory leak, caller should have to free the - // memory but osx implementation is different from linux - return result->c_str(); + return result.c_str(); } // }}} diff --git a/src/Interface/windows/System.cpp b/src/Interface/windows/System.cpp new file mode 100644 index 00000000..24d46919 --- /dev/null +++ b/src/Interface/windows/System.cpp @@ -0,0 +1,14 @@ +#include "System.h" + +System::System(){} + +const char* System::getUserDirectory() { + // caller must free memory after receiving result + TCHAR *path = new TCHAR[128]; + SHGetSpecialFolderPath(NULL, + path, + CSIDL_PERSONAL, + FALSE); + return path; +} + diff --git a/src/Interface/windows/System.h b/src/Interface/windows/System.h new file mode 100644 index 00000000..28557688 --- /dev/null +++ b/src/Interface/windows/System.h @@ -0,0 +1,37 @@ +/* + Copyright 2016 Nidium Inc. All rights reserved. + Use of this source code is governed by a MIT license + that can be found in the LICENSE file. +*/ + +#ifndef interface_windows_system_h__ +#define interface_windows_system_h__ + + +#include "SystemInterface.h" +#include +#include + +namespace Nidium{ +namespace Interface{ + +class System: public SystemInterface +{ +public: + System() = default; + ~System() = default; + const char* getCacheDirectory(); + const char* getEmbedDirectory(); + const char* getUserDirectory(); + const char* getLanguage(); + const char* getCwd(); + void sendNotification(const char *title, const char *content, bool sound); + const char *execute(const char *cmd); + +private: + bool m_SystemUIReady; + char *m_Emebedpath; +} +} // namepspace Interface +} // namepspace Nidium +#endif \ No newline at end of file