[Positioning] OutputFileMedium -> TextFileWriter
TextFileWriter: - OutputFileMedium becomes TextFileWriter. - Method write_string_to_file() becomes write_text() and is made public. - Add a unit test. InputLogCSV and OutputCSV do not extend OutputFileMedium anymore, but use a TextFileWriter as an attribute.
This commit is contained in:
parent
56c45b3334
commit
64715f7a79
|
@ -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 \
|
||||
|
|
|
@ -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)) ;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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)) ;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
#ifndef _OWLPS_POSITIONING_OUTPUTFILEMEDIUM_HH_
|
||||
#define _OWLPS_POSITIONING_OUTPUTFILEMEDIUM_HH_
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
/// 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_
|
|
@ -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 ;
|
|
@ -0,0 +1,21 @@
|
|||
#ifndef _OWLPS_POSITIONING_TEXTFILEWRITER_HH_
|
||||
#define _OWLPS_POSITIONING_TEXTFILEWRITER_HH_
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
/// 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_
|
|
@ -0,0 +1,53 @@
|
|||
#include <cxxtest/TestSuite.h>
|
||||
|
||||
#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() ;
|
||||
}
|
||||
|
||||
} ;
|
Loading…
Reference in New Issue