[Positioning] Add class OutputFileMedium

OutputFileMedium is designed to be a super-class for classes that need
to write to a text file. It could also be a simple writer if the method
write_string_to_file() was public (maybe we should change that and
rename OutputFileMedium to OutputFileWriter or something like that).

InputLogCSV:
- Now extends InputLogMedium *and* OutputFileMedium.
- Make log_request() inline.
This commit is contained in:
Matteo Cypriani 2010-03-08 17:23:20 +01:00
parent 5f0bb90d45
commit 84789f2ed2
6 changed files with 94 additions and 47 deletions

View File

@ -63,6 +63,7 @@ OBJ_LIST = \
inputlogcsv.o
OBJ_NOTEST_LIST = \
posexcept.o \
outputfilemedium.o \
inputmedium.o
INTERFACES_LIST = \
inputlogmedium.hh \
@ -127,6 +128,8 @@ calibrationrequest.o: \
$(OBJ_DIR)/request.o \
$(OBJ_DIR)/referencepoint.o \
$(OBJ_DIR)/direction.o
outputfilemedium.o: \
$(OBJ_DIR)/posexcept.o
inputcsv.o: \
$(OBJ_DIR)/inputmedium.o \
$(OBJ_DIR)/request.o \

View File

@ -7,6 +7,10 @@
#include <fstream>
/// Reads \link Request requests \endlink from a CSV file
/**
* CSV format is:
* Mobile_MAC;Timestamp;X;Y;Z;Direction;AP_MAC_1;SS_1;;AP_MAC_n;SS_n
*/
class InputCSV: public InputMedium
{
protected:

View File

@ -11,47 +11,9 @@ using std::tr1::unordered_map ;
/* *** Constructors *** */
/**
* Prepares the InputLogCSV to write to a CSV file.
* @param filename The name of the file to open.
* @throw error_opening_input_file if the file cannot be opened.
*/
InputLogCSV::InputLogCSV(const string &filename):
log_file_name(filename)
{
log_file.open(log_file_name.c_str()) ;
if (! log_file)
throw error_opening_input_file(log_file_name) ;
}
InputLogCSV::~InputLogCSV()
{
log_file.close() ;
}
/* *** Operations *** */
/**
* @return true if the request has been logged successfuly, or false
* if not.
*/
bool InputLogCSV::log_request(const Request &request)
{
if (! log_file)
return false ;
log_file << request_to_csv(request).c_str() ;
return true ;
}
const string InputLogCSV::request_to_csv(const Request &request) const
{
ostringstream csv_line ;

View File

@ -2,22 +2,20 @@
#define _OWLPS_POSITIONING_INPUTLOGCSV_HH_
#include "inputlogmedium.hh"
#include <string>
#include <fstream>
#include "outputfilemedium.hh"
/// Log \link Request requests \endlink to a CSV file
class InputLogCSV: public InputLogMedium
/**
* CSV format is the same as the one read by InputCSV.
*/
class InputLogCSV: public InputLogMedium, public OutputFileMedium
{
protected:
std::string log_file_name ;
std::ofstream log_file ;
const std::string request_to_csv(const Request &request) const ;
public:
InputLogCSV(const std::string &filename) ;
~InputLogCSV(void) ;
InputLogCSV(const std::string &filename):
OutputFileMedium(filename) {}
/** @name Operations */
//@{
@ -25,4 +23,16 @@ public:
//@}
} ;
/* *** Operations *** */
inline bool InputLogCSV::log_request(const Request &request)
{
return write_string_to_file(request_to_csv(request)) ;
}
#endif // _OWLPS_POSITIONING_INPUTLOGCSV_HH_

View File

@ -0,0 +1,46 @@
#include "outputfilemedium.hh"
#include "posexcept.hh"
using namespace std ;
/* *** Constructors *** */
/**
* Prepares the OutputFileMedium to write to a CSV file.
* @param filename The name of the file to open.
* @throw error_opening_input_file if the file cannot be opened.
*/
OutputFileMedium::OutputFileMedium(const string &_file_name):
file_name(_file_name)
{
file.open(file_name.c_str()) ;
if (! file)
throw error_opening_input_file(file_name) ;
}
OutputFileMedium::~OutputFileMedium()
{
file.close() ;
}
/* *** Operations *** */
/**
* @return true if the string has been written successfuly, or false
* if not.
*/
bool OutputFileMedium::write_string_to_file(const string &text)
{
if (! file)
return false ;
file << text.c_str() ;
return true ;
}

View File

@ -0,0 +1,22 @@
#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_