Browse Source

[bugfix] invalid file, empty input

isundil 9 years ago
parent
commit
c7b280f4dd
5 changed files with 24 additions and 6 deletions
  1. 1 1
      include/jsonObjectEntry.hh
  2. 1 0
      include/params.hh
  3. 8 3
      src/main.cpp
  4. 13 1
      src/params.cpp
  5. 1 1
      src/streamConsumer.cpp

+ 1 - 1
include/jsonObjectEntry.hh

@@ -8,7 +8,7 @@ class JSonObjectEntry: public JSonElement
 {
     public:
         JSonObjectEntry(JSonObject*, const std::string &key, JSonElement *item);
-        ~JSonObjectEntry();
+        virtual ~JSonObjectEntry();
 
         std::string stringify() const;
 

+ 1 - 0
include/params.hh

@@ -9,6 +9,7 @@ class Params
 {
     public:
         Params(int ac, char **av);
+        virtual ~Params();
 
         std::basic_istream<char> &getInput() const;
         bool isValid() const;

+ 8 - 3
src/main.cpp

@@ -6,11 +6,16 @@ void run(Params *params)
 {
     StreamConsumer *stream;
     CurseOutput *out;
+    JSonElement *root;
 
     stream = StreamConsumer::read(params->getInput());
-    out = new CurseOutput(stream->getRoot());
-    out->run();
-    delete out;
+    root = stream->getRoot();
+    if (root)
+    {
+        out = new CurseOutput(root);
+        out->run();
+        delete out;
+    }
     delete stream;
 }
 

+ 13 - 1
src/params.cpp

@@ -18,7 +18,13 @@ Params::Params(int ac, char **av) :progName(*av), params(std::list<std::string>(
                 tmp = *(++av);
                 if (!*av)
                     throw std::runtime_error("Invalid use of -f without argument");
-                this->input = new std::ifstream(tmp);
+                std::ifstream *in = new std::ifstream(tmp);
+                if (!in->is_open())
+                {
+                    delete in;
+                    throw std::runtime_error("Cannot open " +tmp +" for reading");
+                }
+                this->input = in;
             }
             else if (tmp == "--")
             {
@@ -38,6 +44,12 @@ Params::Params(int ac, char **av) :progName(*av), params(std::list<std::string>(
         this->input = input;
 }
 
+Params::~Params()
+{
+    if (input)
+        delete input;
+}
+
 std::basic_istream<char> &Params::getInput() const
 {
     if (input != nullptr)

+ 1 - 1
src/streamConsumer.cpp

@@ -208,6 +208,6 @@ JSonElement *StreamConsumer::consumeToken(JSonContainer *parent, std::string &bu
 
 bool StreamConsumer::ignoreChar(char c) const noexcept
 {
-    return (c <= 32 || c >= 127);
+    return (c <= 32 || c >= 127 || c == '\n');
 }