Selaa lähdekoodia

[refactor] enable multiple inputs

isundil 9 vuotta sitten
vanhempi
commit
0f589391ad
6 muutettua tiedostoa jossa 69 lisäystä ja 62 poistoa
  1. 6 6
      CMakeLists.txt
  2. 3 3
      include/curseOutput.hh
  3. 2 2
      include/params.hh
  4. 11 8
      src/curseOutput.cpp
  5. 32 28
      src/main.cpp
  6. 15 15
      src/params.cpp

+ 6 - 6
CMakeLists.txt

@@ -6,6 +6,12 @@ SET (DOC_OUTPUT doc/)
 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra")
 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
 
+# Add ncurses
+SET(CURSES_NEED_NCURSES TRUE)
+find_package(Curses)
+include_directories(include ${CURSES_INCLUDE_DIRS})
+
+# jsonstroll
 add_executable(jsonstroll
     src/main.cpp
     src/warning.cpp
@@ -26,14 +32,8 @@ add_executable(jsonstroll
     src/jsonException.cpp
     src/except.cpp
     )
-
 set_property(TARGET jsonstroll PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CUSTOM_BINARY_OUTPUT_DIR})
-
-# Add ncurses
-SET(CURSES_NEED_NCURSES TRUE)
-find_package(Curses)
 target_link_libraries(jsonstroll ${ncurses++_LIBRARIES} ${CURSES_LIBRARIES})
-include_directories(include ${CURSES_INCLUDE_DIRS})
 
 # Add tests
 enable_testing()

+ 3 - 3
include/curseOutput.hh

@@ -23,13 +23,13 @@ class SearchPattern;
 class CurseOutput
 {
     public:
-        CurseOutput(JSonElement *rootData, const Params &);
+        CurseOutput(const Params &);
         virtual ~CurseOutput();
 
         /**
          * Display data, and shutdown ncurses at the end
         **/
-        void run();
+        void run(JSonElement *);
         /**
          * Called on SIG* while displaying data
          * Do not use (private).
@@ -152,7 +152,7 @@ class CurseOutput
         /**
          * Root item
         **/
-        JSonElement * const data;
+        JSonElement *data;
 
         /**
          * selected item

+ 2 - 2
include/params.hh

@@ -42,7 +42,7 @@ class Params: public AParams
          * retun input
          * can be file stream (-f), stringstream ( -- INPUT), or std::cin (none)
         **/
-        std::basic_istream<char> &getInput() const;
+        std::list<std::basic_istream<char>*> getInputs();
 
         /**
          * false if invalid argument is passed
@@ -78,7 +78,7 @@ class Params: public AParams
          * input stream
          * can be null for stdin
         **/
-        std::basic_istream<char> *input;
+        std::list<std::basic_istream<char>*> inputs;
 
         const std::string progName;
         std::list<std::string> params;

+ 11 - 8
src/curseOutput.cpp

@@ -23,21 +23,24 @@
 static CurseOutput *runningInst = nullptr;
 class SelectionOutOfRange { };
 
-CurseOutput::CurseOutput(JSonElement *root, const Params &p): data(root), selection(root), params(p)
-{ }
-
-CurseOutput::~CurseOutput()
-{ }
-
-void CurseOutput::run()
+CurseOutput::CurseOutput(const Params &p): data(nullptr), selection(nullptr), params(p)
 {
     runningInst = this;
     init();
-    loop();
+}
+
+CurseOutput::~CurseOutput()
+{
     shutdown();
     runningInst = nullptr;
 }
 
+void CurseOutput::run(JSonElement *root)
+{
+    selection = data = root;
+    loop();
+}
+
 void CurseOutput::loop()
 {
     breakLoop = false;

+ 32 - 28
src/main.cpp

@@ -20,37 +20,41 @@ void displayException(const Params *params, const std::string &type, const JsonE
 
 void run(Params *params)
 {
-    StreamConsumer stream(StreamConsumer(params->getInput()));
-    stream.withConfig(params);
-    CurseOutput *out;
-    JSonElement *root;
+    std::list<std::basic_istream<char>*> inputs = params->getInputs();
+    CurseOutput *out = new CurseOutput(*params);
 
-    if (!params->isIgnoringUnicode())
-        setlocale(LC_ALL, "");
-    try
+    for (std::basic_istream<char>* input : inputs)
     {
-        root = stream.read()->getRoot();
-        if (!root)
-            throw EofException();
-    }
-    catch (EofException &e)
-    {
-        std::cerr << params->getProgName() << ": " << Warning::getType(e) << " ("  << e.what() << ") error while reading" << std::endl;
-        return;
-    }
-    catch (JsonException &e)
-    {
-        std::cerr << "Error: ";
-        displayException(params, Warning::getType(e), e);
-        return;
-    }
-    for (Warning w : stream.getMessages())
-    {
-        std::cerr << "Warning: ";
-        displayException(params, w.getType(), w());
+        StreamConsumer stream(*input);
+        stream.withConfig(params);
+        JSonElement *root;
+
+        if (!params->isIgnoringUnicode())
+            setlocale(LC_ALL, "");
+        try
+        {
+            root = stream.read()->getRoot();
+            if (!root)
+                throw EofException();
+        }
+        catch (EofException &e)
+        {
+            std::cerr << params->getProgName() << ": " << Warning::getType(e) << " ("  << e.what() << ") error while reading" << std::endl;
+            return;
+        }
+        catch (JsonException &e)
+        {
+            std::cerr << "Error: ";
+            displayException(params, Warning::getType(e), e);
+            return;
+        }
+        for (Warning w : stream.getMessages())
+        {
+            std::cerr << "Warning: ";
+            displayException(params, w.getType(), w());
+        }
+        out->run(root);
     }
-    out = new CurseOutput(root, *params);
-    out->run();
     delete out;
 }
 

+ 15 - 15
src/params.cpp

@@ -13,7 +13,7 @@
 
 #include "config.h"
 
-Params::Params(char **av): input(nullptr), progName(*av), strict(true)
+Params::Params(char **av): progName(*av), strict(true)
 {
     av++;
     while (*av)
@@ -23,6 +23,12 @@ Params::Params(char **av): input(nullptr), progName(*av), strict(true)
     }
 }
 
+Params::~Params()
+{
+    for (std::basic_istream<char> *in : inputs)
+        delete in;
+}
+
 bool Params::read()
 {
     bool written = false;
@@ -36,7 +42,7 @@ bool Params::read()
         if (!input)
         {
             if (tmp == "--")
-                input = new std::stringstream();
+                inputs.push_back(input = new std::stringstream());
             else if (tmp == "-W")
                 strict = false;
             else if (tmp == "--ascii")
@@ -73,7 +79,7 @@ bool Params::read()
                     delete in;
                     throw std::runtime_error("Cannot open " +tmp +" for reading");
                 }
-                this->input = in;
+                inputs.push_back(in);
             }
         }
         else
@@ -85,22 +91,16 @@ bool Params::read()
             input->write(tmp.c_str(), sizeof(char) * tmp.size());
         }
     }
-    if (!this->input)
-        this->input = input;
     return true;
 }
 
-Params::~Params()
-{
-    if (input)
-        delete input;
-}
-
-std::basic_istream<char> &Params::getInput() const
+std::list<std::basic_istream<char>*> Params::getInputs()
 {
-    if (input != nullptr)
-        return *input;
-    return std::cin;
+    if (!inputs.empty())
+        return inputs;
+    std::list<std::basic_istream<char>*> result;
+    result.push_back(&std::cin);
+    return result;
 }
 
 void Params::usage() const noexcept