Browse code
refactor file_reader class to mirror system calls
jbalcita authored on 24/04/2017 19:33:07
Showing 3 changed files
Showing 3 changed files
... | ... |
@@ -12,6 +12,9 @@ |
12 | 12 |
#include <string> |
13 | 13 |
#include <vector> |
14 | 14 |
#include <memory> |
15 |
+#include <fstream> |
|
16 |
+#include <sstream> |
|
17 |
+#include <iostream> |
|
15 | 18 |
|
16 | 19 |
#include "token.h" |
17 | 20 |
#include "user.h" |
... | ... |
@@ -26,18 +29,31 @@ public: |
26 | 29 |
impl (file_reader &file_reader) : file_reader_ (file_reader) {} |
27 | 30 |
std::string token (user &user) |
28 | 31 |
{ |
29 |
- std::string file_path (user.home_directory() + "/.dual_control"); |
|
30 |
- std::string fetched_token (file_reader_.read (file_path)); |
|
31 |
- return fetched_token; |
|
32 |
+ const std::string file_path (user.home_directory() + "/.dual_control"); |
|
33 |
+ std::ifstream token_file; |
|
34 |
+ std::string fetched_token; |
|
35 |
+ |
|
36 |
+ bool token_file_opened = file_reader_.open(token_file, file_path); |
|
37 |
+ |
|
38 |
+ if (!token_file_opened) { |
|
39 |
+ return ""; |
|
40 |
+ } |
|
41 |
+ |
|
42 |
+ std::string result = file_reader_.getline(token_file, fetched_token); |
|
43 |
+ return result; |
|
32 | 44 |
} |
33 | 45 |
}; |
34 | 46 |
|
35 | 47 |
class file_reader_impl : public file_reader_ifc |
36 | 48 |
{ |
37 | 49 |
public: |
38 |
- std::string read (std::string file_path) |
|
39 |
- { |
|
40 |
- return file_path; |
|
50 |
+ bool open(std::ifstream &token_file, const std::string &file_path) { |
|
51 |
+ token_file.open(file_path); |
|
52 |
+ return token_file.good(); |
|
53 |
+ } |
|
54 |
+ std::string getline(std::ifstream &token_file, std::string &fetched_token) { |
|
55 |
+ std::getline(token_file, fetched_token); |
|
56 |
+ return fetched_token; |
|
41 | 57 |
} |
42 | 58 |
}; |
43 | 59 |
} |
... | ... |
@@ -29,6 +29,12 @@ public: |
29 | 29 |
class file_reader_ifc |
30 | 30 |
{ |
31 | 31 |
public: |
32 |
+ virtual std::string getline(std::ifstream &token_file, std::string &fetched_token) { |
|
33 |
+ return ""; |
|
34 |
+ } |
|
35 |
+ virtual bool open(std::ifstream &token_file, const std::string &file_path) { |
|
36 |
+ return false; |
|
37 |
+ } |
|
32 | 38 |
virtual std::string read (std::string file_path) |
33 | 39 |
{ |
34 | 40 |
return ""; |
... | ... |
@@ -45,6 +51,12 @@ public: |
45 | 51 |
file_reader (delegate delegate) : |
46 | 52 |
delegate_ (delegate) {} |
47 | 53 |
file_reader() : delegate_ (delegate (new file_reader_ifc)) {} |
54 |
+ bool open(std::ifstream &token_file, const std::string &file_path) { |
|
55 |
+ return delegate_->open(token_file, file_path); |
|
56 |
+ } |
|
57 |
+ std::string getline(std::ifstream &token_file, std::string &fetched_token) { |
|
58 |
+ return delegate_->getline(token_file, fetched_token); |
|
59 |
+ } |
|
48 | 60 |
std::string read (std::string file_path) |
49 | 61 |
{ |
50 | 62 |
return delegate_->read (file_path); |
... | ... |
@@ -14,6 +14,8 @@ |
14 | 14 |
#include <pwd.h> |
15 | 15 |
#include <cstdio> |
16 | 16 |
#include <sys/stat.h> |
17 |
+#include <fstream> |
|
18 |
+#include <istream> |
|
17 | 19 |
|
18 | 20 |
#include "token.h" |
19 | 21 |
#include "test_util.h" |
... | ... |
@@ -21,11 +23,16 @@ |
21 | 23 |
|
22 | 24 |
class fake_file_reader : public file_reader_ifc |
23 | 25 |
{ |
24 |
-public: |
|
25 |
- std::string read (std::string file_path) |
|
26 |
- { |
|
27 |
- return file_path; |
|
28 |
- } |
|
26 |
+ private: |
|
27 |
+ std::string file_path_; |
|
28 |
+ public: |
|
29 |
+ bool open(std::ifstream &token_file, const std::string &file_path) { |
|
30 |
+ file_path_ = file_path; |
|
31 |
+ return true; |
|
32 |
+ } |
|
33 |
+ std::string getline(std::ifstream &token_file, std::string &fetched_token) { |
|
34 |
+ return file_path_; |
|
35 |
+ } |
|
29 | 36 |
}; |
30 | 37 |
|
31 | 38 |
class fake_user : public user_ifc |