diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e03c47f --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +# Compiler and flags +CXX = c++ +CXXFLAGS = -std=c++98 -Iincludes -Wall -Wextra +LDFLAGS = + +# Source files and object files +SRCS = $(wildcard src/*.cpp) main.cpp schema.cpp +OBJS = $(SRCS:.cpp=.o) + +# Target executable +TARGET = json_validator + +# Default rule +all: $(TARGET) + +# Linking rule +$(TARGET): $(OBJS) + $(CXX) $(LDFLAGS) -o $(TARGET) $(OBJS) + +# Compilation rule +%.o: %.cpp + $(CXX) $(CXXFLAGS) -c $< -o $@ + +# Clean rule +clean: + rm -f $(OBJS) $(TARGET) + +.PHONY: all clean diff --git a/includes/schema.hpp b/includes/schema.hpp new file mode 100644 index 0000000..61ba45f --- /dev/null +++ b/includes/schema.hpp @@ -0,0 +1,8 @@ +#ifndef SCHEMA_HPP +#define SCHEMA_HPP + +#include "JsonValidator.hpp" + +extern ArrayValidator ServerSchema; + +#endif diff --git a/json_validator b/json_validator new file mode 100755 index 0000000..216f492 Binary files /dev/null and b/json_validator differ diff --git a/main.cpp b/main.cpp index a7f1c04..f42f9ee 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,6 @@ #include "Json.hpp" #include "JsonValidator.hpp" -#include "schema.cpp" +#include "schema.hpp" #include int main() { diff --git a/main.o b/main.o new file mode 100644 index 0000000..0dc3fbf Binary files /dev/null and b/main.o differ diff --git a/schema.cpp b/schema.cpp index 6c05009..c596823 100644 --- a/schema.cpp +++ b/schema.cpp @@ -1,4 +1,4 @@ -#include "JsonValidator.hpp" +#include "schema.hpp" ObjectValidator locationSchema = obj() .property("allowed_methods",arr().item( diff --git a/schema.o b/schema.o new file mode 100644 index 0000000..ff63d67 Binary files /dev/null and b/schema.o differ diff --git a/src/AJsonValue.o b/src/AJsonValue.o new file mode 100644 index 0000000..8fab2e3 Binary files /dev/null and b/src/AJsonValue.o differ diff --git a/src/Json.o b/src/Json.o new file mode 100644 index 0000000..c0424f9 Binary files /dev/null and b/src/Json.o differ diff --git a/src/JsonTypes.o b/src/JsonTypes.o new file mode 100644 index 0000000..f523816 Binary files /dev/null and b/src/JsonTypes.o differ diff --git a/src/JsonValidator.cpp b/src/JsonValidator.cpp index 969809f..b7259fe 100644 --- a/src/JsonValidator.cpp +++ b/src/JsonValidator.cpp @@ -100,16 +100,14 @@ ObjectValidator::ObjectValidator() } ObjectValidator::ObjectValidator(const ObjectValidator &obj) - : allowAdditional_(obj.allowAdditional_), matchMode_(obj.matchMode_), - emptyCheck(obj.emptyCheck), key_validator(NULL), val_validator(NULL), - last_(NULL) { + : AJsonValidator(obj), allowAdditional_(obj.allowAdditional_), + matchMode_(obj.matchMode_), emptyCheck(obj.emptyCheck), + key_validator(NULL), val_validator(NULL), last_(NULL) { if (obj.val_validator) val_validator = obj.val_validator->clone(); if (obj.key_validator) key_validator = dynamic_cast(obj.key_validator->clone()); - optional_ = obj.optional_; exceptedType_ = OBJECT; - hasDefault_ = obj.hasDefault_; if (obj.defaultValue_) defaultValue_ = dynamic_cast(obj.defaultValue_->clone()); ValidatorMap::const_iterator it = obj.properties_.begin(); @@ -269,9 +267,9 @@ ArrayValidator::ArrayValidator() } ArrayValidator::ArrayValidator(const ArrayValidator &obj) - : validator_(NULL), min_(obj.min_), max_(obj.max_), defaultValue_(NULL) { + : AJsonValidator(obj), validator_(NULL), min_(obj.min_), max_(obj.max_), + defaultValue_(NULL) { exceptedType_ = ARRAY; - hasDefault_ = obj.hasDefault_; if (obj.defaultValue_) defaultValue_ = dynamic_cast(obj.defaultValue_->clone()); if (obj.validator_) { @@ -375,7 +373,8 @@ ArrayValidator::~ArrayValidator() { ORValidator::ORValidator() {} -ORValidator::ORValidator(const ORValidator &obj) : msg_(obj.msg_) { +ORValidator::ORValidator(const ORValidator &obj) + : AJsonValidator(obj), msg_(obj.msg_) { for (unsigned long i = 0; i < obj.conditions_.size(); i++) { conditions_.push_back(obj.conditions_[i]->clone()); } @@ -444,11 +443,10 @@ StringValidator::StringValidator() } StringValidator::StringValidator(const StringValidator &obj) - : min_(obj.min_), max_(obj.max_), checkMin(obj.checkMin), - checkMax(obj.checkMax), defaultValue_(obj.defaultValue_) { - optional_ = obj.optional_; + : AJsonValidator(obj), min_(obj.min_), max_(obj.max_), + checkMin(obj.checkMin), checkMax(obj.checkMax), + defaultValue_(obj.defaultValue_) { exceptedType_ = STRING; - hasDefault_ = obj.hasDefault_; std::map::const_iterator it = obj.checkers.begin(); for (; it != obj.checkers.end(); it++) { funcCheck copied = it->second; @@ -625,11 +623,10 @@ NumberValidator::NumberValidator() } NumberValidator::NumberValidator(const NumberValidator &obj) - : min_(obj.min_), max_(obj.max_), checkMin(obj.checkMin), - checkMax(obj.checkMax), defaultValue_(obj.defaultValue_) { - optional_ = obj.optional_; + : AJsonValidator(obj), min_(obj.min_), max_(obj.max_), + checkMin(obj.checkMin), checkMax(obj.checkMax), + defaultValue_(obj.defaultValue_) { exceptedType_ = NUMBER; - hasDefault_ = obj.hasDefault_; } AJsonValidator *NumberValidator::clone() const { @@ -711,8 +708,7 @@ NumberValidator::~NumberValidator() {} BoolValidator::BoolValidator() {} -BoolValidator::BoolValidator(const BoolValidator &obj) { - hasDefault_ = obj.hasDefault_; +BoolValidator::BoolValidator(const BoolValidator &obj) : AJsonValidator(obj) { defaultValue_ = obj.get_default(); } diff --git a/src/JsonValidator.o b/src/JsonValidator.o new file mode 100644 index 0000000..b8cf9d2 Binary files /dev/null and b/src/JsonValidator.o differ diff --git a/src/parser.cpp b/src/parser.cpp index 4cdb095..946ae22 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -87,22 +87,34 @@ token extract_decimal(char &c, std::istream &in) { token extract_json_types(char &c, std::istream &in) { token tk; - tk.token.push_back(c); - int i = 1; - int len = (c == 'f' ? 5 : 4); - while (i < len) { - if (in.eof()) - break; - in.get(c); - tk.token.push_back(c); - i++; + tk.type = TK_UNDEFINED; + std::string expected; + + if (c == 't') + expected = "true"; + else if (c == 'f') + expected = "false"; + else if (c == 'n') + expected = "null"; + + tk.token += c; + for (size_t i = 1; i < expected.length(); ++i) { + if (!in.get(c) || c != expected[i]) { + // Restore the read characters to the stream to not mess up the parsing + // for other tokens + for (size_t j = 0; j < tk.token.length(); j++) { + in.unget(); + } + return tk; + } + tk.token += c; } - if (tk.token == "true" || tk.token == "false") + + if (expected == "true" || expected == "false") tk.type = TK_BOOLEAN; - else if (tk.token == "null") + else if (expected == "null") tk.type = TK_NIL; - else - tk.type = TK_UNDEFINED; + return tk; } @@ -150,31 +162,34 @@ void Tokenizer::parse(std::istream &in, std::vector &tokens) { } std::string match_token_name(token_type type) { - if (type == CB_OPEN) + switch (type) { + case CB_OPEN: return "CB_OPEN"; - else if (type == CB_CLOSE) + case CB_CLOSE: return "CB_CLOSE"; - else if (type == SB_OPEN) + case SB_OPEN: return "SB_OPEN"; - else if (type == SB_CLOSE) + case SB_CLOSE: return "SB_CLOSE"; - else if (type == TK_STRING) + case TK_STRING: return "STRING"; - else if (type == TK_DOUBLE) + case TK_DOUBLE: return "DOUBLE"; - else if (type == TK_NUMBER) + case TK_NUMBER: return "NUMBER"; - else if (type == COMMA) + case COMMA: return "COMMA"; - else if (type == COLON) + case COLON: return "COLON"; - else if (type == TK_BOOLEAN) + case TK_BOOLEAN: return "BOOLEAN"; - else if (type == TK_NIL) + case TK_NIL: return "NULL"; - else if (type == END) + case END: return "END"; - return "TK_UNDEFINED"; + default: + return "TK_UNDEFINED"; + } } std::ostream &operator<<(std::ostream &os, token &tk) { diff --git a/src/parser.o b/src/parser.o new file mode 100644 index 0000000..426b4a5 Binary files /dev/null and b/src/parser.o differ diff --git a/src/utils.o b/src/utils.o new file mode 100644 index 0000000..f620c4b Binary files /dev/null and b/src/utils.o differ diff --git a/src/validators.o b/src/validators.o new file mode 100644 index 0000000..43fac22 Binary files /dev/null and b/src/validators.o differ