git.fiddlerwoaroof.com
Browse code

factor out file reader to own module

Greg Wiley authored on 25/04/2017 17:56:08
Showing 6 changed files
... ...
@@ -2,7 +2,7 @@ CXXFLAGS += -fPIC -fno-stack-protector -std=c++14
2 2
 CFLAGS += -fPIC -fno-stack-protector
3 3
 
4 4
 OBJS = dual_control.o request.o dual_control_integrate.o validator.o conversation.o user.o \
5
-	   sys_unistd.o sys_pwd.o token.o
5
+	   sys_unistd.o sys_pwd.o token.o file_reader.o
6 6
 TESTS = dual_control_test validator_test conversation_test request_test user_test token_test
7 7
 TESTOBJS = $(patsubst %,%.o,$(TESTS))
8 8
 SRCS := $(OBJS:.o=.cc) $(TESTOBJS:.o=.cc)
9 9
new file mode 100644
... ...
@@ -0,0 +1,28 @@
1
+#include <fstream>
2
+
3
+#include "file_reader.h"
4
+
5
+namespace {
6
+class file_reader_impl : public file_reader_ifc
7
+{
8
+public:
9
+    bool open (std::ifstream &token_file, const std::string &file_path)
10
+    {
11
+        token_file.open (file_path);
12
+        return token_file.good();
13
+    }
14
+    std::string getline (std::ifstream &token_file, std::string &line)
15
+    {
16
+        std::getline (token_file, line);
17
+        return line;
18
+    }
19
+};
20
+
21
+}
22
+
23
+file_reader file_reader::create ()
24
+{
25
+    return file_reader (file_reader::delegate (new
26
+                        file_reader_impl ));
27
+}
28
+
0 29
new file mode 100644
... ...
@@ -0,0 +1,58 @@
1
+/* Copyright (C) CJ Affiliate
2
+ *
3
+ * You may use, distribute and modify this code under  the
4
+ * terms of the  GNU General Public License  version 2  or
5
+ * later.
6
+ *
7
+ * You should have received a copy of the license with this
8
+ * file. If not, you will find a copy in the "LICENSE" file
9
+ * at https://github.com/cjdev/dual-control.
10
+ */
11
+
12
+#ifndef _FILE_READER_H
13
+#define _FILE_READER_H
14
+
15
+#include <string>
16
+#include <memory>
17
+
18
+#include "user.h"
19
+
20
+
21
+class file_reader_ifc
22
+{
23
+public:
24
+    virtual bool open (std::ifstream &token_file, const std::string &file_path)
25
+    {
26
+        return false;
27
+    }
28
+    virtual std::string getline (std::ifstream &token_file,
29
+                                 std::string &line)
30
+    {
31
+        return "";
32
+    }
33
+};
34
+
35
+class file_reader : public file_reader_ifc
36
+{
37
+public:
38
+    typedef std::shared_ptr<file_reader_ifc> delegate;
39
+private:
40
+    delegate delegate_;
41
+public:
42
+    file_reader (delegate delegate) :
43
+        delegate_ (delegate) {}
44
+    file_reader() : delegate_ (delegate (new file_reader_ifc)) {}
45
+    bool open (std::ifstream &token_file, const std::string &file_path)
46
+    {
47
+        return delegate_->open (token_file, file_path);
48
+    }
49
+    std::string getline (std::ifstream &token_file, std::string &line)
50
+    {
51
+        return delegate_->getline (token_file, line);
52
+    }
53
+
54
+    static file_reader create ();
55
+};
56
+
57
+#endif
58
+
... ...
@@ -41,29 +41,7 @@ public:
41 41
         return file_reader_.getline (token_file, fetched_token);
42 42
     }
43 43
 };
44
-
45
-class file_reader_impl : public file_reader_ifc
46
-{
47
-public:
48
-    bool open (std::ifstream &token_file, const std::string &file_path)
49
-    {
50
-        token_file.open (file_path);
51
-        return token_file.good();
52
-    }
53
-    std::string getline (std::ifstream &token_file, std::string &line)
54
-    {
55
-        std::getline (token_file, line);
56
-        return line;
57
-    }
58
-};
59
-}
60
-
61
-file_reader file_reader::create ()
62
-{
63
-    return file_reader (file_reader::delegate (new
64
-                        file_reader_impl ));
65 44
 }
66
-
67 45
 user_token_supplier user_token_supplier::create (file_reader &file_reader)
68 46
 {
69 47
     return user_token_supplier (user_token_supplier::delegate
... ...
@@ -16,6 +16,7 @@
16 16
 #include <memory>
17 17
 
18 18
 #include "user.h"
19
+#include "file_reader.h"
19 20
 
20 21
 class user_token_supplier_ifc
21 22
 {
... ...
@@ -27,42 +28,6 @@ public:
27 28
     }
28 29
 };
29 30
 
30
-class file_reader_ifc
31
-{
32
-public:
33
-    virtual bool open (std::ifstream &token_file, const std::string &file_path)
34
-    {
35
-        return false;
36
-    }
37
-    virtual std::string getline (std::ifstream &token_file,
38
-                                 std::string &line)
39
-    {
40
-        return "";
41
-    }
42
-};
43
-
44
-class file_reader : public file_reader_ifc
45
-{
46
-public:
47
-    typedef std::shared_ptr<file_reader_ifc> delegate;
48
-private:
49
-    delegate delegate_;
50
-public:
51
-    file_reader (delegate delegate) :
52
-        delegate_ (delegate) {}
53
-    file_reader() : delegate_ (delegate (new file_reader_ifc)) {}
54
-    bool open (std::ifstream &token_file, const std::string &file_path)
55
-    {
56
-        return delegate_->open (token_file, file_path);
57
-    }
58
-    std::string getline (std::ifstream &token_file, std::string &line)
59
-    {
60
-        return delegate_->getline (token_file, line);
61
-    }
62
-
63
-    static file_reader create ();
64
-};
65
-
66 31
 class user_token_supplier : public user_token_supplier_ifc
67 32
 {
68 33
 public:
... ...
@@ -19,6 +19,7 @@
19 19
 #include "token.h"
20 20
 #include "test_util.h"
21 21
 #include "user.h"
22
+#include "file_reader.h"
22 23
 
23 24
 class fake_file_reader : public file_reader_ifc
24 25
 {