From d3b63265874c492a4128f93fe55d568ef7d7aa2c Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Tue, 11 Sep 2012 12:58:30 -0700 Subject: [PATCH] Use a single std::istream& to replace file_/data_. --- Console.cpp | 47 ++++++++++++++++++++++------------------------- Cycript.l.in | 14 ++++---------- Handler.mm | 7 +++---- Library.cpp | 9 +++------ Parser.cpp | 6 ++---- Parser.hpp | 27 +++++++++++++++++++++++---- todo.txt | 1 + 7 files changed, 58 insertions(+), 53 deletions(-) diff --git a/Console.cpp b/Console.cpp index 934ab8c..771a785 100644 --- a/Console.cpp +++ b/Console.cpp @@ -202,13 +202,9 @@ int (*append_history$)(int, const char *); static std::string command_; static CYExpression *ParseExpression(CYUTF8String code) { - std::ostringstream str; - str << '(' << code << ')'; - std::string string(str.str()); - - CYDriver driver; - driver.data_ = string.c_str(); - driver.size_ = string.size(); + std::stringstream stream; + stream << '(' << code << ')'; + CYDriver driver(stream); cy::parser parser(driver); Setup(driver, parser); @@ -232,18 +228,16 @@ static char **Complete(const char *word, int start, int end) { rl_attempted_completion_over = TRUE; CYLocalPool pool; - CYDriver driver; - cy::parser parser(driver); - Setup(driver, parser); std::string line(rl_line_buffer, start); - std::string command(command_ + line); - - driver.data_ = command.c_str(); - driver.size_ = command.size(); + std::istringstream stream(command_ + line); + CYDriver driver(stream); driver.auto_ = true; + cy::parser parser(driver); + Setup(driver, parser); + if (parser.parse() != 0 || !driver.errors_.empty()) return NULL; @@ -485,13 +479,13 @@ static void Console(CYOptions &options) { code = command_; else { CYLocalPool pool; - CYDriver driver; + + std::istringstream stream(command_); + CYDriver driver(stream); + cy::parser parser(driver); Setup(driver, parser); - driver.data_ = command_.c_str(); - driver.size_ = command_.size(); - if (parser.parse() != 0 || !driver.errors_.empty()) { for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { cy::position begin(error->location_.begin); @@ -781,17 +775,14 @@ int Main(int argc, char const * const argv[], char const * const envp[]) { Console(options); else { CYLocalPool pool; - CYDriver driver(script ?: ""); - cy::parser parser(driver); - Setup(driver, parser); char *start, *end; + std::istream *indirect; if (script == NULL) { start = NULL; end = NULL; - - driver.file_ = stdin; + indirect = &std::cin; } else { size_t size; start = reinterpret_cast(Map(script, &size)); @@ -806,10 +797,16 @@ int Main(int argc, char const * const argv[], char const * const envp[]) { start = end; } - driver.data_ = start; - driver.size_ = end - start; + indirect = NULL; } + CYStream direct(start, end); + std::istream &stream(indirect == NULL ? direct : *indirect); + CYDriver driver(stream, script ?: ""); + + cy::parser parser(driver); + Setup(driver, parser); + if (parser.parse() != 0 || !driver.errors_.empty()) { for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) std::cerr << i->location_.begin << ": " << i->message_ << std::endl; diff --git a/Cycript.l.in b/Cycript.l.in index d63784e..f2bd9b9 100644 --- a/Cycript.l.in +++ b/Cycript.l.in @@ -90,18 +90,12 @@ int H(char c) { } #define YY_INPUT(data, value, size) { \ - if (yyextra->file_ != NULL) { \ - size_t copy(fread(data, 1, size, yyextra->file_)); \ - value = copy == 0 ? YY_NULL : copy; \ - } else if (yyextra->size_ == 0) \ + if (yyextra->data_.eof()) \ value = YY_NULL; \ else { \ - size_t copy(size); \ - copy = (std::min(copy, yyextra->size_)); \ - memcpy(data, yyextra->data_, copy); \ - yyextra->data_ += copy; \ - yyextra->size_ -= copy; \ - value = copy; \ + yyextra->data_.read(data, size); \ + size_t copy(yyextra->data_.gcount()); \ + value = copy == 0 ? YY_NULL : copy; \ } \ } diff --git a/Handler.mm b/Handler.mm index 1ad9ef6..54d002f 100644 --- a/Handler.mm +++ b/Handler.mm @@ -99,11 +99,10 @@ struct CYClient : return; data[size] = '\0'; - CYDriver driver; - cy::parser parser(driver); + CYStream stream(data, data + size); + CYDriver driver(stream); - driver.data_ = data; - driver.size_ = size; + cy::parser parser(driver); const char *json; if (parser.parse() != 0 || !driver.errors_.empty()) { diff --git a/Library.cpp b/Library.cpp index 84cac4d..1ed182e 100644 --- a/Library.cpp +++ b/Library.cpp @@ -246,14 +246,11 @@ void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value) { extern "C" void CydgetPoolParse(apr_pool_t *remote, const uint16_t **data, size_t *size) { CYLocalPool local; - CYDriver driver; - cy::parser parser(driver); - CYUTF8String utf8(CYPoolUTF8String(local, CYUTF16String(*data, *size))); + CYStream stream(utf8.data, utf8.data + utf8.size); + CYDriver driver(stream); - driver.data_ = utf8.data; - driver.size_ = utf8.size; - + cy::parser parser(driver); if (parser.parse() != 0 || !driver.errors_.empty()) return; diff --git a/Parser.cpp b/Parser.cpp index f2d3ad1..f0ad37f 100644 --- a/Parser.cpp +++ b/Parser.cpp @@ -26,11 +26,9 @@ CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9 CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$ CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9 -CYDriver::CYDriver(const std::string &filename) : +CYDriver::CYDriver(std::istream &data, const std::string &filename) : state_(CYClear), - data_(NULL), - size_(0), - file_(NULL), + data_(data), strict_(false), filename_(filename), program_(NULL), diff --git a/Parser.hpp b/Parser.hpp index ba896b7..bff219b 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -469,6 +469,27 @@ enum CYState { CYNewLine }; +class CYStream : + public std::istream +{ + private: + class CYBuffer : + public std::streambuf + { + public: + CYBuffer(const char *start, const char *end) { + setg(const_cast(start), const_cast(start), const_cast(end)); + } + } buffer_; + + public: + CYStream(const char *start, const char *end) : + std::istream(&buffer_), + buffer_(start, end) + { + } +}; + class CYDriver { public: void *scanner_; @@ -482,9 +503,7 @@ class CYDriver { bool OpenBrace; } no_; - const char *data_; - size_t size_; - FILE *file_; + std::istream &data_; bool strict_; @@ -539,7 +558,7 @@ class CYDriver { void ScannerDestroy(); public: - CYDriver(const std::string &filename = ""); + CYDriver(std::istream &data, const std::string &filename = ""); ~CYDriver(); Condition GetCondition(); diff --git a/todo.txt b/todo.txt index 73b781e..7285880 100644 --- a/todo.txt +++ b/todo.txt @@ -103,3 +103,4 @@ Objective-C strings might should be cyonified using double-quotes apparently you can have random escape sequences in strings, like \! cycript -p with processes that have spaces doesn't work +CYDriver uses std::istream, but it should use std::streambuf -- 2.45.2