diff --git a/owlps-positioning/Makefile b/owlps-positioning/Makefile index 6d3d7ea..acd9aff 100644 --- a/owlps-positioning/Makefile +++ b/owlps-positioning/Makefile @@ -55,6 +55,7 @@ OBJ_LIST = \ realposition.o \ configuration.o \ userinterface.o \ + textfilewriter.o \ output.o \ outputterminal.o \ outputcsv.o \ @@ -64,7 +65,6 @@ OBJ_LIST = \ inputlogcsv.o OBJ_NOTEST_LIST = \ posexcept.o \ - outputfilemedium.o \ inputmedium.o INTERFACES_LIST = \ inputlogmedium.hh \ @@ -129,7 +129,7 @@ calibrationrequest.o: \ $(OBJ_DIR)/request.o \ $(OBJ_DIR)/referencepoint.o \ $(OBJ_DIR)/direction.o -outputfilemedium.o: \ +textfilewriter.o: \ $(OBJ_DIR)/posexcept.o inputcsv.o: \ $(OBJ_DIR)/inputmedium.o \ @@ -151,7 +151,7 @@ outputterminal.o: \ $(OBJ_DIR)/result.o outputcsv.o: \ $(SRC_DIR)/outputmedium.hh \ - $(OBJ_DIR)/outputfilemedium.o \ + $(OBJ_DIR)/textfilewriter.o \ $(OBJ_DIR)/result.o output.o: \ $(OBJ_DIR)/outputterminal.o \ diff --git a/owlps-positioning/src/inputlogcsv.hh b/owlps-positioning/src/inputlogcsv.hh index f5984fc..88ff843 100644 --- a/owlps-positioning/src/inputlogcsv.hh +++ b/owlps-positioning/src/inputlogcsv.hh @@ -2,20 +2,22 @@ #define _OWLPS_POSITIONING_INPUTLOGCSV_HH_ #include "inputlogmedium.hh" -#include "outputfilemedium.hh" +#include "textfilewriter.hh" /// Log \link Request requests \endlink to a CSV file /** * CSV format is the same as the one read by InputCSV. */ -class InputLogCSV: public InputLogMedium, public OutputFileMedium +class InputLogCSV: public InputLogMedium { protected: + TextFileWriter file ; + const std::string request_to_csv(const Request &request) const ; public: InputLogCSV(const std::string &filename): - OutputFileMedium(filename) {} + file(filename) {} /** @name Operations */ //@{ @@ -30,7 +32,7 @@ public: inline bool InputLogCSV::log_request(const Request &request) { - return write_string_to_file(request_to_csv(request)) ; + return file.write_text(request_to_csv(request)) ; } diff --git a/owlps-positioning/src/outputcsv.hh b/owlps-positioning/src/outputcsv.hh index e06a26d..6dda9bf 100644 --- a/owlps-positioning/src/outputcsv.hh +++ b/owlps-positioning/src/outputcsv.hh @@ -2,21 +2,23 @@ #define _OWLPS_POSITIONING_OUTPUTCSV_HH_ #include "outputmedium.hh" -#include "outputfilemedium.hh" +#include "textfilewriter.hh" /// Writes a result to a CSV file /** * CSV format is: * Mobile_MAC;Timestamp;X;Y;Z */ -class OutputCSV: public OutputMedium, public OutputFileMedium +class OutputCSV: public OutputMedium { protected: + TextFileWriter file ; + const std::string result_to_csv(const Result &result) ; public: OutputCSV(const std::string &filename): - OutputFileMedium(filename) {} + file(filename) {} void write(const Result &result) ; } ; @@ -28,7 +30,7 @@ public: inline void OutputCSV::write(const Result &result) { - write_string_to_file(result_to_csv(result)) ; + file.write_text(result_to_csv(result)) ; } diff --git a/owlps-positioning/src/outputfilemedium.hh b/owlps-positioning/src/outputfilemedium.hh deleted file mode 100644 index 9fd4730..0000000 --- a/owlps-positioning/src/outputfilemedium.hh +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef _OWLPS_POSITIONING_OUTPUTFILEMEDIUM_HH_ -#define _OWLPS_POSITIONING_OUTPUTFILEMEDIUM_HH_ - -#include -#include - -/// Super-class of all classes that write things to a file -class OutputFileMedium -{ -private: - std::string file_name ; - std::ofstream file ; - -protected: - bool write_string_to_file(const std::string &text) ; - -public: - OutputFileMedium(const std::string &_file_name) ; - virtual ~OutputFileMedium(void) ; -} ; - -#endif // _OWLPS_POSITIONING_OUTPUTFILEMEDIUM_HH_ diff --git a/owlps-positioning/src/outputfilemedium.cc b/owlps-positioning/src/textfilewriter.cc similarity index 62% rename from owlps-positioning/src/outputfilemedium.cc rename to owlps-positioning/src/textfilewriter.cc index a163ea1..db093e0 100644 --- a/owlps-positioning/src/outputfilemedium.cc +++ b/owlps-positioning/src/textfilewriter.cc @@ -1,4 +1,4 @@ -#include "outputfilemedium.hh" +#include "textfilewriter.hh" #include "posexcept.hh" using namespace std ; @@ -9,11 +9,10 @@ using namespace std ; /** - * Prepares the OutputFileMedium to write to a CSV file. - * @param filename The name of the file to open. + * @param _file_name The name of the file to open. * @throw error_opening_input_file if the file cannot be opened. */ -OutputFileMedium::OutputFileMedium(const string &_file_name): +TextFileWriter::TextFileWriter(const string &_file_name): file_name(_file_name) { file.open(file_name.c_str()) ; @@ -22,7 +21,7 @@ OutputFileMedium::OutputFileMedium(const string &_file_name): } -OutputFileMedium::~OutputFileMedium() +TextFileWriter::~TextFileWriter() { file.close() ; } @@ -36,7 +35,7 @@ OutputFileMedium::~OutputFileMedium() * @return true if the string has been written successfuly, or false * if not. */ -bool OutputFileMedium::write_string_to_file(const string &text) +bool TextFileWriter::write_text(const string &text) { if (! file) return false ; diff --git a/owlps-positioning/src/textfilewriter.hh b/owlps-positioning/src/textfilewriter.hh new file mode 100644 index 0000000..cd2fff6 --- /dev/null +++ b/owlps-positioning/src/textfilewriter.hh @@ -0,0 +1,21 @@ +#ifndef _OWLPS_POSITIONING_TEXTFILEWRITER_HH_ +#define _OWLPS_POSITIONING_TEXTFILEWRITER_HH_ + +#include +#include + +/// Write text to a file +class TextFileWriter +{ +private: + std::string file_name ; + std::ofstream file ; + +public: + TextFileWriter(const std::string &_file_name) ; + virtual ~TextFileWriter(void) ; + + bool write_text(const std::string &text) ; +} ; + +#endif // _OWLPS_POSITIONING_TEXTFILEWRITER_HH_ diff --git a/owlps-positioning/tests/textfilewriter_test.hh b/owlps-positioning/tests/textfilewriter_test.hh new file mode 100644 index 0000000..1a626ae --- /dev/null +++ b/owlps-positioning/tests/textfilewriter_test.hh @@ -0,0 +1,53 @@ +#include + +#include "textfilewriter.hh" + +class TextFileWriter_test: public CxxTest::TestSuite +{ +private: + std::string test_file_name ; + +public: + + TextFileWriter_test(void) + { + Stock::clear() ; + test_file_name = "/tmp/TextFileWriter_test_file.csv" ; + } + + + ~TextFileWriter_test(void) + { + TestUtil::remove_file(test_file_name) ; + } + + + static TextFileWriter_test* createSuite(void) + { + return new TextFileWriter_test() ; + } + + + static void destroySuite(TextFileWriter_test *suite) + { + delete suite ; + } + + + void test_textfilewriter(void) + { + TextFileWriter *textfilewriter = + new TextFileWriter(test_file_name) ; + TS_ASSERT(textfilewriter->write_text("Hello\nWorld\n")) ; + delete textfilewriter ; + + std::ifstream input_file(test_file_name.c_str()) ; + std::string line ; + getline(input_file, line) ; + TS_ASSERT_EQUALS(line, "Hello") ; + getline(input_file, line) ; + TS_ASSERT_EQUALS(line, "World") ; + input_file.close() ; + } + +} ;