Browse Source

init

master
Robin Thoni 7 years ago
commit
61ac6a811d

+ 34
- 0
.gitignore View File

@@ -0,0 +1,34 @@
1
+
2
+
3
+/.idea
4
+/build
5
+*.swp
6
+
7
+# Compiled Object files
8
+*.slo
9
+*.lo
10
+*.o
11
+*.obj
12
+
13
+# Precompiled Headers
14
+*.gch
15
+*.pch
16
+
17
+# Compiled Dynamic libraries
18
+*.so
19
+*.dylib
20
+*.dll
21
+
22
+# Fortran module files
23
+*.mod
24
+
25
+# Compiled Static libraries
26
+*.lai
27
+*.la
28
+*.a
29
+*.lib
30
+
31
+# Executables
32
+*.exe
33
+*.out
34
+*.app

+ 10
- 0
CMakeLists.txt View File

@@ -0,0 +1,10 @@
1
+cmake_minimum_required(VERSION 2.8)
2
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/")
3
+set(PROJECT_NAME project)
4
+project(${PROJECT_NAME})
5
+find_package(GTest)
6
+add_subdirectory(src)
7
+if (GTEST_FOUND)
8
+    enable_testing()
9
+    add_subdirectory(tests)
10
+endif (GTEST_FOUND)

+ 46
- 0
CMakeModules/FindJsonCpp.cmake View File

@@ -0,0 +1,46 @@
1
+# -*- cmake -*-
2
+# - Find JSONCpp
3
+# Find the JSONCpp includes and library
4
+# This module defines
5
+#  JSONCPP_INCLUDE_DIRS, where to find json.h, etc.
6
+#  JSONCPP_LIBRARIES, the libraries needed to use jsoncpp.
7
+#  JSONCPP_FOUND, If false, do not try to use jsoncpp.
8
+#  also defined, but not for general use are
9
+#  JSONCPP_LIBRARIES, where to find the jsoncpp library.
10
+
11
+FIND_PATH(JSONCPP_INCLUDE_DIRS json/json.h
12
+        /usr/include
13
+        /usr/local/include
14
+        ${CMAKE_SOURCE_DIR}/win32-deps/include
15
+        PATH_SUFFIXES jsoncpp/json jsoncpp
16
+        )
17
+
18
+
19
+FIND_LIBRARY(JSONCPP_LIBRARIES NAMES jsoncpp HINTS /usr/lib /usr/local/lib 	${CMAKE_SOURCE_DIR}/win32-deps/lib)
20
+
21
+IF (JSONCPP_LIBRARIES AND JSONCPP_INCLUDE_DIRS)
22
+    SET(JSONCPP_LIBRARIES ${JSONCPP_LIBRARIES})
23
+    SET(JSONCPP_FOUND "YES")
24
+ELSE (JSONCPP_LIBRARIES AND JSONCPP_INCLUDE_DIRS)
25
+    SET(JSONCPP_FOUND "NO")
26
+ENDIF (JSONCPP_LIBRARIES AND JSONCPP_INCLUDE_DIRS)
27
+
28
+
29
+IF (JSONCPP_FOUND)
30
+    IF (NOT JSONCPP_FIND_QUIETLY)
31
+        MESSAGE(STATUS "Found JSONCpp: ${JSONCPP_LIBRARIES}")
32
+    ENDIF (NOT JSONCPP_FIND_QUIETLY)
33
+ELSE (JSONCPP_FOUND)
34
+    IF (JSONCPP_FIND_REQUIRED)
35
+        MESSAGE(FATAL_ERROR "Could not find JSONCPP library include: ${JSONCPP_INCLUDE_DIRS}, lib: ${JSONCPP_LIBRARIES}")
36
+    ENDIF (JSONCPP_FIND_REQUIRED)
37
+ENDIF (JSONCPP_FOUND)
38
+
39
+# Deprecated declarations.
40
+SET (NATIVE_JSONCPP_INCLUDE_PATH ${JSONCPP_INCLUDE_DIRS} )
41
+GET_FILENAME_COMPONENT (NATIVE_JSONCPP_LIB_PATH ${JSONCPP_LIBRARIES} PATH)
42
+
43
+MARK_AS_ADVANCED(
44
+        JSONCPP_LIBRARIES
45
+        JSONCPP_INCLUDE_DIRS
46
+)

+ 0
- 0
src/Business/.gitkeep View File


+ 16
- 0
src/CMakeLists.txt View File

@@ -0,0 +1,16 @@
1
+include_directories(.)
2
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
3
+set(SOURCE_FILES
4
+  main.cpp
5
+  Interface/MainClass.cpp
6
+  Interface/MainClass.h
7
+  Interface/CommandLineParser.cpp
8
+  Interface/CommandLineParser.h
9
+  DBO/CommandLineOption.cpp
10
+  DBO/CommandLineOption.h
11
+  DBO/Result.hxx
12
+  DBO/Result.h
13
+  )
14
+
15
+add_executable(${PROJECT_NAME} ${SOURCE_FILES})
16
+target_link_libraries(${PROJECT_NAME} ${LIBS})

+ 98
- 0
src/DBO/CommandLineOption.cpp View File

@@ -0,0 +1,98 @@
1
+//
2
+// Created by robin on 8/8/15.
3
+//
4
+
5
+#include "CommandLineOption.h"
6
+
7
+CommandLineOption::CommandLineOption(const std::string &longName, char shortName, const std::string &description,
8
+                                     const std::string &valueName, const std::string &defaultValue)
9
+    : _longName(longName)
10
+    , _shortName(shortName)
11
+    , _description(description)
12
+    , _valueName(valueName)
13
+    , _defaultValue(defaultValue)
14
+    , _isSet(false)
15
+{
16
+}
17
+
18
+const std::string &CommandLineOption::getLongName() const
19
+{
20
+    return _longName;
21
+}
22
+
23
+void CommandLineOption::setLongName(const std::string &longName)
24
+{
25
+    _longName = longName;
26
+}
27
+
28
+char CommandLineOption::getShortName() const
29
+{
30
+    return _shortName;
31
+}
32
+
33
+void CommandLineOption::setShortName(char shortName)
34
+{
35
+    _shortName = shortName;
36
+}
37
+
38
+const std::string &CommandLineOption::getDescription() const
39
+{
40
+    return _description;
41
+}
42
+
43
+void CommandLineOption::setDescription(const std::string &description)
44
+{
45
+    _description = description;
46
+}
47
+
48
+const std::string &CommandLineOption::getValueName() const
49
+{
50
+    return _valueName;
51
+}
52
+
53
+void CommandLineOption::setValueName(const std::string &valueName)
54
+{
55
+    _valueName = valueName;
56
+}
57
+
58
+const std::string &CommandLineOption::getDefaultValue() const
59
+{
60
+    return _defaultValue;
61
+}
62
+
63
+void CommandLineOption::setDefaultValue(const std::string &defaultValue)
64
+{
65
+    _defaultValue = defaultValue;
66
+}
67
+
68
+const std::vector<std::string> &CommandLineOption::getValues() const
69
+{
70
+    return _values;
71
+}
72
+
73
+void CommandLineOption::addValue(const std::string &value)
74
+{
75
+    _values.push_back(value);
76
+}
77
+
78
+bool CommandLineOption::isSet() const
79
+{
80
+    return _isSet;
81
+}
82
+
83
+void CommandLineOption::setIsSet(bool isSet)
84
+{
85
+    _isSet = isSet;
86
+}
87
+
88
+bool CommandLineOption::hasValue() const
89
+{
90
+    return !_valueName.empty();
91
+}
92
+
93
+const std::string &CommandLineOption::getValue() const
94
+{
95
+    if (_values.empty())
96
+        return _defaultValue;
97
+    return _values[_values.size() - 1];
98
+}

+ 66
- 0
src/DBO/CommandLineOption.h View File

@@ -0,0 +1,66 @@
1
+//
2
+// Created by robin on 8/8/15.
3
+//
4
+
5
+#ifndef PDNS_SLAVE_COMMANDLINEOPTION_H
6
+#define PDNS_SLAVE_COMMANDLINEOPTION_H
7
+
8
+# include <string>
9
+# include <vector>
10
+
11
+class CommandLineOption
12
+{
13
+public:
14
+    CommandLineOption(const std::string& longName, char shortName, const std::string& description,
15
+                      const std::string& valueName = "", const std::string& defaultValue = "");
16
+
17
+    const std::string &getLongName() const;
18
+
19
+    void setLongName(const std::string &longName);
20
+
21
+    char getShortName() const;
22
+
23
+    void setShortName(char shortName);
24
+
25
+    const std::string &getDescription() const;
26
+
27
+    void setDescription(const std::string &description);
28
+
29
+    const std::string &getValueName() const;
30
+
31
+    void setValueName(const std::string &valueName);
32
+
33
+    const std::string &getDefaultValue() const;
34
+
35
+    void setDefaultValue(const std::string &defaultValue);
36
+
37
+    const std::vector<std::string> &getValues() const;
38
+
39
+    const std::string &getValue() const;
40
+
41
+    void addValue(const std::string &value);
42
+
43
+    bool isSet() const;
44
+
45
+    void setIsSet(bool isSet);
46
+
47
+    bool hasValue() const;
48
+
49
+private:
50
+    std::string _longName;
51
+
52
+    char _shortName;
53
+
54
+    std::string _description;
55
+
56
+    std::string _valueName;
57
+
58
+    std::string _defaultValue;
59
+
60
+    std::vector<std::string> _values;
61
+
62
+    bool _isSet;
63
+};
64
+
65
+
66
+#endif //PDNS_SLAVE_COMMANDLINEOPTION_H

+ 52
- 0
src/DBO/Result.h View File

@@ -0,0 +1,52 @@
1
+//
2
+// Created by robin on 8/9/15.
3
+//
4
+
5
+#ifndef PDNS_SLAVE_RESULT_H
6
+#define PDNS_SLAVE_RESULT_H
7
+
8
+# include <string>
9
+# include <vector>
10
+
11
+template <class T> class Result
12
+{
13
+public:
14
+    Result();
15
+
16
+    Result(const T& data);
17
+
18
+    virtual ~Result();
19
+
20
+    Result<T>& ok(const T& data);
21
+
22
+    T& getData();
23
+
24
+    bool isSuccess() const;
25
+
26
+    Result<T>& addWarning(const std::string& warning);
27
+
28
+    const std::vector<std::string>& getWarnings() const;
29
+
30
+    Result<T>& error(const std::string& error);
31
+
32
+    const std::string& getError() const;
33
+
34
+    bool operator !() const;
35
+
36
+    operator bool() const;
37
+
38
+    const Result<T>& print() const;
39
+
40
+private:
41
+    T _data;
42
+
43
+    std::vector<std::string> _warnings;
44
+
45
+    std::string _error;
46
+};
47
+
48
+typedef Result<bool> BResult;
49
+
50
+# include "DBO/Result.hxx"
51
+
52
+#endif //PDNS_SLAVE_RESULT_H

+ 89
- 0
src/DBO/Result.hxx View File

@@ -0,0 +1,89 @@
1
+//
2
+// Created by robin on 8/9/15.
3
+//
4
+
5
+#include <iostream>
6
+#include "Result.h"
7
+
8
+template<class T>
9
+Result<T>::Result()
10
+{
11
+}
12
+
13
+template<class T>
14
+Result<T>::Result(const T &data)
15
+        : _data(data)
16
+{
17
+}
18
+
19
+template<class T>
20
+Result<T>::~Result()
21
+{
22
+}
23
+
24
+template<class T>
25
+Result<T> &Result<T>::ok(const T &data)
26
+{
27
+    _data = data;
28
+    return *this;
29
+}
30
+
31
+template<class T>
32
+T &Result<T>::getData()
33
+{
34
+    return _data;
35
+}
36
+
37
+template<class T>
38
+bool Result<T>::isSuccess() const
39
+{
40
+    return _error.empty();
41
+}
42
+
43
+template<class T>
44
+const std::vector<std::string>& Result<T>::getWarnings() const
45
+{
46
+    return _warnings;
47
+}
48
+
49
+template<class T>
50
+Result<T>& Result<T>::addWarning(const std::string &warning)
51
+{
52
+    _warnings.push_back(warning);
53
+    return *this;
54
+}
55
+
56
+template<class T>
57
+bool Result<T>::operator!() const
58
+{
59
+    return !isSuccess();
60
+}
61
+
62
+template<class T>
63
+Result<T>::operator bool() const
64
+{
65
+    return isSuccess();
66
+}
67
+
68
+template<class T>
69
+Result<T> &Result<T>::error(const std::string &error)
70
+{
71
+    _error = error;
72
+    return *this;
73
+}
74
+
75
+template<class T>
76
+const std::string &Result<T>::getError() const
77
+{
78
+    return _error;
79
+}
80
+
81
+template<class T>
82
+const Result<T>& Result<T>::print() const
83
+{
84
+    for (auto warning : _warnings)
85
+        std::cerr << "WARNING: " << warning << std::endl;
86
+    if (!isSuccess())
87
+        std::cerr << "ERROR: " << _error << std::endl;
88
+    return *this;
89
+}

+ 0
- 0
src/DataAccess/.gitkeep View File


+ 74
- 0
src/Interface/CommandLineParser.cpp View File

@@ -0,0 +1,74 @@
1
+//
2
+// Created by robin on 8/8/15.
3
+//
4
+
5
+#include <getopt.h>
6
+#include <iostream>
7
+#include "CommandLineParser.h"
8
+
9
+CommandLineParser::CommandLineParser(int argc, char **argv)
10
+    : _argc(argc)
11
+    , _argv(argv)
12
+{
13
+}
14
+
15
+bool CommandLineParser::parse()
16
+{
17
+    std::string shortOpts;
18
+    option opts[_options.size() + 1];
19
+    for (unsigned i = 0; i < _options.size(); ++i)
20
+    {
21
+        auto opt = _options[i];
22
+        shortOpts += opt->getShortName();
23
+        if (opt->hasValue())
24
+            shortOpts += ":";
25
+        opts[i].name = opt->getLongName().c_str();
26
+        opts[i].has_arg = opt->hasValue();
27
+        opts[i].flag = 0;
28
+        opts[i].val = opt->getShortName();
29
+    }
30
+    opts[_options.size()] = {0, 0, 0, 0};
31
+    int option;
32
+    extern int optind;
33
+    extern char* optarg;
34
+    bool valid = true;
35
+    while ((option = getopt_long(_argc, _argv, shortOpts.c_str(), opts, 0)) != -1)
36
+    {
37
+        bool optValid = false;
38
+        for (unsigned i = 0; i < _options.size(); ++i)
39
+        {
40
+            auto opt = _options[i];
41
+            if (opt->getShortName() == option)
42
+            {
43
+                optValid = true;
44
+                opt->setIsSet(true);
45
+                if (opt->hasValue())
46
+                    opt->addValue(optarg);
47
+            }
48
+        }
49
+        if (!optValid)
50
+            valid = false;
51
+    }
52
+    return optind == _argc && valid;
53
+}
54
+
55
+void CommandLineParser::addOption(CommandLineOption* opt)
56
+{
57
+    _options.push_back(opt);
58
+}
59
+
60
+int CommandLineParser::showHelp(int status, bool stdErr)
61
+{
62
+    auto& out = stdErr ? std::cerr : std::cout;
63
+    out << "Options:" << std::endl;
64
+    for (auto opt : _options)
65
+    {
66
+        out << "  -" << opt->getShortName() << ", --" << opt->getLongName();
67
+        if (opt->hasValue())
68
+        {
69
+            out << " <" << opt->getValueName() << "=" << opt->getDefaultValue() << ">";
70
+        }
71
+        out << " " << opt->getDescription() << std::endl;
72
+    }
73
+    return status;
74
+}

+ 30
- 0
src/Interface/CommandLineParser.h View File

@@ -0,0 +1,30 @@
1
+//
2
+// Created by robin on 8/8/15.
3
+//
4
+
5
+#ifndef PDNS_SLAVE_COMMANDLINEPARSER_H
6
+#define PDNS_SLAVE_COMMANDLINEPARSER_H
7
+
8
+# include "DBO/CommandLineOption.h"
9
+
10
+class CommandLineParser
11
+{
12
+public:
13
+    CommandLineParser(int argc, char** argv);
14
+
15
+    bool parse();
16
+
17
+    int showHelp(int status = 0, bool stdErr = true);
18
+
19
+    void addOption(CommandLineOption* opt);
20
+
21
+private:
22
+    int _argc;
23
+
24
+    char** _argv;
25
+
26
+    std::vector<CommandLineOption*> _options;
27
+};
28
+
29
+
30
+#endif //PDNS_SLAVE_COMMANDLINEPARSER_H

+ 21
- 0
src/Interface/MainClass.cpp View File

@@ -0,0 +1,21 @@
1
+//
2
+// Created by robin on 8/8/15.
3
+//
4
+
5
+#include <iostream>
6
+#include <sysexits.h>
7
+#include <DBO/Result.h>
8
+#include "CommandLineParser.h"
9
+#include "MainClass.h"
10
+
11
+MainClass::MainClass(int argc, char *argv[])
12
+    : _argc(argc)
13
+    , _argv(argv)
14
+{
15
+}
16
+
17
+int MainClass::main()
18
+{
19
+
20
+    return 0;
21
+}

+ 23
- 0
src/Interface/MainClass.h View File

@@ -0,0 +1,23 @@
1
+//
2
+// Created by robin on 8/8/15.
3
+//
4
+
5
+#ifndef PDNS_SLAVE_MAINCLASS_H
6
+#define PDNS_SLAVE_MAINCLASS_H
7
+
8
+
9
+class MainClass {
10
+public:
11
+    MainClass(int argc, char* argv[]);
12
+
13
+    int main();
14
+
15
+private:
16
+    int _argc;
17
+
18
+    char** _argv;
19
+
20
+};
21
+
22
+
23
+#endif //PDNS_SLAVE_MAINCLASS_H

+ 7
- 0
src/main.cpp View File

@@ -0,0 +1,7 @@
1
+#include "Interface/MainClass.h"
2
+
3
+int main(int argc, char* argv[])
4
+{
5
+    MainClass m(argc, argv);
6
+    return m.main();
7
+}

+ 11
- 0
tests/CMakeLists.txt View File

@@ -0,0 +1,11 @@
1
+enable_testing()
2
+include_directories(${CHECK_INCLUDE_DIRS})
3
+include_directories(. ../src)
4
+find_package (Threads)
5
+set(LIBS ${LIBS} gtest pthread)
6
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
7
+add_executable(test-${PROJECT_NAME}
8
+        test-main.cpp
9
+  )
10
+target_link_libraries(test-${PROJECT_NAME} ${LIBS})
11
+add_test(test-${PROJECT_NAME} ${CMAKE_CURRENT_BINARY_DIR}/test-${PROJECT_NAME})

+ 14
- 0
tests/test-main.cpp View File

@@ -0,0 +1,14 @@
1
+#include <iostream>
2
+#include <string.h>
3
+#include <gtest/gtest.h>
4
+
5
+TEST(None, None)
6
+{
7
+  ASSERT_TRUE(true);
8
+}
9
+
10
+int main(int argc, char* argv[])
11
+{
12
+  ::testing::InitGoogleTest(&argc, argv);
13
+  return RUN_ALL_TESTS();
14
+}

Loading…
Cancel
Save