files.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <fstream>
7 #include <string>
8 #include <iostream> // for debug output
9 #include <iomanip>
10 #include <sys/stat.h>
11 
12 #include <boost/system/error_code.hpp>
13 
14 #ifdef TESTING
15 #include "gtest/gtest.h"
16 #endif
17 
18 namespace dpyFiles {
19 /********************************************************************************
20  **********************************INLINE FUNCTIONS *****************************
21  ********************************************************************************/
22 
28 inline bool fileExist(const std::string& name)
29 {
30  struct stat buffer;
31  return (stat(name.c_str(), &buffer) == 0);
32 }
33 
34 }
43 class Files
44 {
45 public:
46  Files();
47  virtual ~Files();
48 
49 #ifdef TESTING
50  virtual bool readFile(boost::system::error_code& ec, const std::string& sPath, std::string& result, const bool binary=false);
51  virtual void readContentandClose(std::ifstream& iFile, std::string& result);
52  virtual bool writeFile(boost::system::error_code& ec, const std::string& sPath, const std::string& property_value);
53  virtual bool createAndWrite(boost::system::error_code& ec, const std::string& sPath, const std::string& property_value, const bool atomic=false);
54  virtual void writeContentandClose(std::ofstream& oFile, const std::string& value);
55  virtual bool addContentToFile(boost::system::error_code& ec, const std::string& sPath, const std::string& content);
56  virtual bool createAndAddContentToFile(boost::system::error_code& ec, const std::string& sPath, const std::string& content, const bool atomic=false);
57  virtual bool readNumOfLines(boost::system::error_code& ec, const std::string& sPath, std::string& result, const int num, const bool binary=false);
58  virtual void getLines(std::ifstream& iFile, const int num, std::string& result);
59  virtual bool removeFile(const std::string& sPath);
60  virtual bool isPath(boost::system::error_code& ec, const std::string& path);
61  virtual bool isRegularFile(boost::system::error_code& ec, const std::string& path);
62  virtual bool isDirectory(boost::system::error_code& ec, const std::string& path);
63  virtual bool isFileOpened(std::ifstream& iFile);
64  virtual bool isFileOpened(std::ofstream& oFile);
65 #else
66  bool readFile(boost::system::error_code& ec, const std::string& sPath, std::string& result, const bool binary = false);
67  void readContentandClose(std::ifstream& iFile, std::string& result);
68  bool writeFile(boost::system::error_code& ec, const std::string& sPath, const std::string& property_value);
69  bool createAndWrite(boost::system::error_code& ec, const std::string& sPath, const std::string& property_value, const bool atomic=false);
70  void writeContentandClose(std::ofstream& oFile, const std::string& value);
71  bool addContentToFile(boost::system::error_code& ec, const std::string& sPath, const std::string& content);
72  bool createAndAddContentToFile(boost::system::error_code& ec, const std::string& sPath, const std::string& content, const bool atomic=false);
73  bool readNumOfLines(boost::system::error_code& ec, const std::string& sPath, std::string& result, const int num, const bool binary = false);
74  void getLines(std::ifstream& iFile, const int num, std::string& result);
75  bool removeFile(const std::string& sPath);
76  bool isPath(boost::system::error_code& ec, const std::string& path);
77  bool isRegularFile(boost::system::error_code& ec, const std::string& path);
78  bool isDirectory(boost::system::error_code& ec, const std::string& path);
79  bool isFileOpened(std::ifstream& iFile);
80  bool isFileOpened(std::ofstream& oFile);
81 
82 #endif
83 
84  bool createDir(boost::system::error_code& ec, const std::string& path);
85  std::string getFilename(const std::string& path);
86  std::string getFoldername(const std::string &path);
87 
88 #ifdef TESTING
89 public:
90  FRIEND_TEST(Basic_files_test, fileExist_OK);
91  FRIEND_TEST(Basic_files_test, fileExist_FAIL);
92  FRIEND_TEST(Basic_files_test, createDir_OK);
93  FRIEND_TEST(Basic_files_test, createDir_FAIL);
94  FRIEND_TEST(Basic_files_test, isDirectory_OK);
95  FRIEND_TEST(Basic_files_test, isDirectory_FAIL);
96  FRIEND_TEST(Basic_files_test, isRegularFile_OK);
97  FRIEND_TEST(Basic_files_test, isRegularFile_FAIL);
98  FRIEND_TEST(Basic_files_test, isPath_OK);
99  FRIEND_TEST(Basic_files_test, isPath_FAIL);
100  FRIEND_TEST(Basic_files_test, writeFile_OK);
101  FRIEND_TEST(Basic_files_test, writeFile_FAIL);
102  FRIEND_TEST(Basic_files_test, readFile_OK);
103  FRIEND_TEST(Basic_files_test, readFile_FAIL);
104 
105  FRIEND_TEST(Basic_files_test_mock, writeFile_error_path);
106  FRIEND_TEST(Basic_files_test_mock, writeFile_error_regular_file);
107  FRIEND_TEST(Basic_files_test_mock, writeFile_error_opened_file);
108  FRIEND_TEST(Basic_files_test_mock, readFile_error_path);
109  FRIEND_TEST(Basic_files_test_mock, readFile_error_regular_file);
110  FRIEND_TEST(Basic_files_test_mock, readFile_error_opened_file);
111 
112 #endif
113 };
This class implementes the read/write operations to work with text files stored in the file sytem...
Definition: files.h:43
bool fileExist(const std::string &name)
check if the file already exists
Definition: files.h:28
Definition: files.h:18