From dcd42316aada9f791f02599440ff6dd78e38d4ad Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 22 Aug 2012 13:05:29 -0700 Subject: [PATCH] DRY up how native IO is performed Introduce a new io_utils class with helpers --- Atom-Linux/Makefile | 2 +- Atom-Linux/client_handler.cpp | 14 ++++------- Atom-Linux/io_utils.cpp | 36 ++++++++++++++++++++++++++++ Atom-Linux/io_utils.h | 17 +++++++++++++ Atom-Linux/native_handler.cpp | 11 ++------- Atom-Linux/onig_regexp_extension.cpp | 25 +++++-------------- 6 files changed, 66 insertions(+), 39 deletions(-) create mode 100644 Atom-Linux/io_utils.cpp create mode 100644 Atom-Linux/io_utils.h diff --git a/Atom-Linux/Makefile b/Atom-Linux/Makefile index b5e2c52a2..a059f3f3a 100644 --- a/Atom-Linux/Makefile +++ b/Atom-Linux/Makefile @@ -59,7 +59,7 @@ LIBS := -lX11 \ -lcef_dll_wrapper \ -lonig -SOURCES=atom.cpp native_handler.cpp client_handler.cpp onig_regexp_extension.cpp +SOURCES=atom.cpp native_handler.cpp client_handler.cpp onig_regexp_extension.cpp io_utils.cpp OBJECTS=$(SOURCES:.cpp=.o) INSTALLDIR=/usr/local/atom diff --git a/Atom-Linux/client_handler.cpp b/Atom-Linux/client_handler.cpp index 538cb3b99..b3611a11b 100644 --- a/Atom-Linux/client_handler.cpp +++ b/Atom-Linux/client_handler.cpp @@ -11,6 +11,7 @@ #include "include/cef_frame.h" #include "atom.h" #include "native_handler.h" +#include "io_utils.h" #include #include @@ -91,16 +92,9 @@ void ClientHandler::OnLoadStart(CefRefPtr browser, CefRefPtr atom = CefV8Value::CreateObject(NULL, NULL); global->SetValue("atom", atom, V8_PROPERTY_ATTRIBUTE_NONE); - std::string relativePath(AppPath()); - relativePath.append("/.."); - char* realLoadPath; - realLoadPath = realpath(relativePath.c_str(), NULL); - if (realLoadPath != NULL) { - std::string resolvedLoadPath(realLoadPath); - free(realLoadPath); - - CefRefPtr loadPath = CefV8Value::CreateString( - resolvedLoadPath); + std::string realLoadPath = io_utils_real_app_path("/.."); + if (!realLoadPath.empty()) { + CefRefPtr loadPath = CefV8Value::CreateString(realLoadPath); atom->SetValue("loadPath", loadPath, V8_PROPERTY_ATTRIBUTE_NONE); } diff --git a/Atom-Linux/io_utils.cpp b/Atom-Linux/io_utils.cpp new file mode 100644 index 000000000..6320fb948 --- /dev/null +++ b/Atom-Linux/io_utils.cpp @@ -0,0 +1,36 @@ +#include "io_utils.h" +#include "atom.h" +#include +#include +#include + +#define BUFFER_SIZE 8192 + +using namespace std; + +int io_utils_read(string path, string* output) { + int fd = open(path.c_str(), O_RDONLY); + if (fd <= 0) + return 0; + + char buffer[BUFFER_SIZE]; + unsigned int bytesRead = 0; + unsigned int totalRead = 0; + while ((bytesRead = read(fd, buffer, BUFFER_SIZE)) > 0) { + output->append(buffer, 0, bytesRead); + totalRead += bytesRead; + } + close(fd); + return totalRead; +} + +string io_utils_real_app_path(string relativePath) { + string path = AppPath() + relativePath; + char* realPath = realpath(path.c_str(), NULL); + if (realPath != NULL) { + string realAppPath(realPath); + free(realPath); + return realAppPath; + } else + return ""; +} \ No newline at end of file diff --git a/Atom-Linux/io_utils.h b/Atom-Linux/io_utils.h new file mode 100644 index 000000000..adca46a4c --- /dev/null +++ b/Atom-Linux/io_utils.h @@ -0,0 +1,17 @@ +#ifndef IO_UTILS_H_ +#define IO_UTILS_H_ +#pragma once + +#include + +/** + * Read file at path and append to output string + */ +int io_utils_read(std::string path, std::string* output); + +/** + * Get realpath for given path that is relative to the app path + */ +std::string io_utils_real_app_path(std::string relativePath); + +#endif diff --git a/Atom-Linux/native_handler.cpp b/Atom-Linux/native_handler.cpp index 5c0ec5ab5..43c7a3d5d 100644 --- a/Atom-Linux/native_handler.cpp +++ b/Atom-Linux/native_handler.cpp @@ -2,6 +2,7 @@ #include "include/cef_base.h" #include "include/cef_runnable.h" #include "client_handler.h" +#include "io_utils.h" #include #include #include @@ -109,16 +110,8 @@ void NativeHandler::Read(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception) { string path = arguments[0]->GetStringValue().ToString(); - int fd = open(path.c_str(), O_RDONLY); - if (fd < 0) - return; - - char buffer[BUFFER_SIZE]; - int r; string value; - while ((r = read(fd, buffer, sizeof buffer)) > 0) - value.append(buffer, 0, r); - close(fd); + io_utils_read(path, &value); retval = CefV8Value::CreateString(value); } diff --git a/Atom-Linux/onig_regexp_extension.cpp b/Atom-Linux/onig_regexp_extension.cpp index a6eed55aa..e46d6c210 100644 --- a/Atom-Linux/onig_regexp_extension.cpp +++ b/Atom-Linux/onig_regexp_extension.cpp @@ -2,12 +2,9 @@ #include "include/cef_base.h" #include "include/cef_runnable.h" #include -#include "atom.h" #include #include -#include -#include -#include +#include "io_utils.h" using namespace std; @@ -129,22 +126,12 @@ IMPLEMENT_REFCOUNTING(OnigRegexpUserData) OnigRegexpExtension::OnigRegexpExtension() : CefV8Handler() { - string filePath = AppPath() + "/../src/stdlib/onig-reg-exp-extension.js"; - char* realFilePath; - realFilePath = realpath(filePath.c_str(), NULL); - if (realFilePath != NULL) { - int fd = open(realFilePath, O_RDONLY); - if (fd > 0) { - char buffer[8192]; - int r; - string extensionCode; - while ((r = read(fd, buffer, sizeof buffer)) > 0) - extensionCode.append(buffer, 0, r); + string realFilePath = io_utils_real_app_path( + "/../src/stdlib/onig-reg-exp-extension.js"); + if (!realFilePath.empty()) { + string extensionCode; + if (io_utils_read(realFilePath, &extensionCode) > 0) CefRegisterExtension("v8/oniguruma", extensionCode, this); - - close(fd); - free(realFilePath); - } } }