mirror of
https://github.com/atom/atom.git
synced 2026-01-23 05:48:10 -05:00
DRY up how native IO is performed
Introduce a new io_utils class with helpers
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "include/cef_frame.h"
|
||||
#include "atom.h"
|
||||
#include "native_handler.h"
|
||||
#include "io_utils.h"
|
||||
#include <stdlib.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
@@ -91,16 +92,9 @@ void ClientHandler::OnLoadStart(CefRefPtr<CefBrowser> browser,
|
||||
CefRefPtr<CefV8Value> 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<CefV8Value> loadPath = CefV8Value::CreateString(
|
||||
resolvedLoadPath);
|
||||
std::string realLoadPath = io_utils_real_app_path("/..");
|
||||
if (!realLoadPath.empty()) {
|
||||
CefRefPtr<CefV8Value> loadPath = CefV8Value::CreateString(realLoadPath);
|
||||
atom->SetValue("loadPath", loadPath, V8_PROPERTY_ATTRIBUTE_NONE);
|
||||
}
|
||||
|
||||
|
||||
36
Atom-Linux/io_utils.cpp
Normal file
36
Atom-Linux/io_utils.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "io_utils.h"
|
||||
#include "atom.h"
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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 "";
|
||||
}
|
||||
17
Atom-Linux/io_utils.h
Normal file
17
Atom-Linux/io_utils.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef IO_UTILS_H_
|
||||
#define IO_UTILS_H_
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "include/cef_base.h"
|
||||
#include "include/cef_runnable.h"
|
||||
#include "client_handler.h"
|
||||
#include "io_utils.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
@@ -109,16 +110,8 @@ void NativeHandler::Read(const CefString& name, CefRefPtr<CefV8Value> object,
|
||||
const CefV8ValueList& arguments, CefRefPtr<CefV8Value>& 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,9 @@
|
||||
#include "include/cef_base.h"
|
||||
#include "include/cef_runnable.h"
|
||||
#include <oniguruma.h>
|
||||
#include "atom.h"
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user