From 338e4f7dcddd14e0ba80b1bbf0a819140c65f7a8 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Wed, 23 Jun 2010 16:38:39 +0200 Subject: [PATCH] [Positioning] Code cleaning around CSV data reading CSVFileReader: Add function read_point3d(). Point3D: Add function set_coordinates(float[3]). posexcept: Rename malformed_topology -> malformed_input_data. TopologyReaderCSV: - Use CSVFileReader::read_point3d() instead of the custom read_point(). - Rename occurrences of malformed_topology. --- owlps-positioning/src/csvfilereader.cc | 29 ++++++++++++++++++++ owlps-positioning/src/csvfilereader.hh | 5 +++- owlps-positioning/src/point3d.hh | 10 +++++++ owlps-positioning/src/posexcept.cc | 6 ++--- owlps-positioning/src/posexcept.hh | 4 +-- owlps-positioning/src/topologyreadercsv.cc | 31 ++++++++++------------ owlps-positioning/src/topologyreadercsv.hh | 1 - 7 files changed, 62 insertions(+), 24 deletions(-) diff --git a/owlps-positioning/src/csvfilereader.cc b/owlps-positioning/src/csvfilereader.cc index 83ecfdf..a520ad1 100644 --- a/owlps-positioning/src/csvfilereader.cc +++ b/owlps-positioning/src/csvfilereader.cc @@ -1,4 +1,5 @@ #include "csvfilereader.hh" +#include "point3d.hh" #include @@ -44,6 +45,34 @@ bool CSVFileReader::next_line() } +bool CSVFileReader::read_point3d(Point3D &p) +{ + float coord[3] ; + + for (unsigned int i = 0 ; i < 3 ; ++i) + { + if (token_iterator == current_token->end()) + return false ; + ++current_field_nb ; + + try + { + coord[i] = boost::lexical_cast(*token_iterator) ; + } + catch (boost::bad_lexical_cast &e) + { + print_error_cast() ; + return false ; + } + + ++token_iterator ; + } + + p.set_coordinates(coord) ; + return true ; +} + + void CSVFileReader::print_error_cast() { cerr diff --git a/owlps-positioning/src/csvfilereader.hh b/owlps-positioning/src/csvfilereader.hh index 3b9b01f..8b645c3 100644 --- a/owlps-positioning/src/csvfilereader.hh +++ b/owlps-positioning/src/csvfilereader.hh @@ -1,12 +1,13 @@ #ifndef _OWLPS_POSITIONING_CSVFILEREADER_HH_ #define _OWLPS_POSITIONING_CSVFILEREADER_HH_ +class Point3D ; + #include "textfilereader.hh" #include #include - /// Reads a CSV file, line by line, field by field class CSVFileReader: public TextFileReader { @@ -35,6 +36,8 @@ public: bool next_line(void) ; /// Read the next field of the current line template bool read_field(T &field) ; + /// Read the next 3 fields that should form a Point3D + bool read_point3d(Point3D &p) ; //@} } ; diff --git a/owlps-positioning/src/point3d.hh b/owlps-positioning/src/point3d.hh index e432c39..f3f5652 100644 --- a/owlps-positioning/src/point3d.hh +++ b/owlps-positioning/src/point3d.hh @@ -37,6 +37,7 @@ public: void set_y(const float _y) ; void set_z(const float _z) ; void set_coordinates(const float _x, const float _y, const float _z) ; + void set_coordinates(const float source[3]) ; void set_coordinates(const Point3D &source) ; //@} @@ -132,6 +133,15 @@ set_coordinates(const float _x, const float _y, const float _z) } +inline void Point3D:: +set_coordinates(const float source[3]) +{ + x = source[0] ; + y = source[1] ; + z = source[2] ; +} + + /** * Updates x, y, z by passing a Point3D. * This is useful for derivated classes, and different than a direct diff --git a/owlps-positioning/src/posexcept.cc b/owlps-positioning/src/posexcept.cc index ae163c3..717187a 100644 --- a/owlps-positioning/src/posexcept.cc +++ b/owlps-positioning/src/posexcept.cc @@ -37,9 +37,9 @@ bad_configuration(const string &_explanation) throw(): /* *** Input *** */ -malformed_topology:: -malformed_topology(const string &_explanation) throw(): - posexcept("Error reading the topology: " + _explanation) {} +malformed_input_data:: +malformed_input_data(const string &_explanation) throw(): + posexcept("Read error: " + _explanation) {} bad_direction::bad_direction(const char direction) throw() diff --git a/owlps-positioning/src/posexcept.hh b/owlps-positioning/src/posexcept.hh index 3e42a05..27ba7cc 100644 --- a/owlps-positioning/src/posexcept.hh +++ b/owlps-positioning/src/posexcept.hh @@ -65,10 +65,10 @@ public: /* *** Input *** */ -class malformed_topology: public posexcept +class malformed_input_data: public posexcept { public: - malformed_topology(const std::string &_explanation) throw() ; + malformed_input_data(const std::string &_explanation) throw() ; } ; diff --git a/owlps-positioning/src/topologyreadercsv.cc b/owlps-positioning/src/topologyreadercsv.cc index 70266d9..27c32eb 100644 --- a/owlps-positioning/src/topologyreadercsv.cc +++ b/owlps-positioning/src/topologyreadercsv.cc @@ -37,16 +37,21 @@ void TopologyReaderCSV::process_area_line() { string building_name ; if (! areas_file.read_field(building_name)) - throw malformed_topology("Cannot read building name!") ; + throw malformed_input_data("Cannot read building name!") ; Building &building = const_cast( Stock::find_create_building(building_name)) ; string name ; if (! areas_file.read_field(name)) - throw malformed_topology("Cannot read area name!") ; + throw malformed_input_data("Cannot read area name!") ; - Point3D p_min(read_point(areas_file)) ; - Point3D p_max(read_point(areas_file)) ; + Point3D p_min ; + if (! areas_file.read_point3d(p_min)) + throw malformed_input_data("Cannot read area min coordinates!") ; + + Point3D p_max ; + if (! areas_file.read_point3d(p_max)) + throw malformed_input_data("Cannot read area max coordinates!") ; Area *area = new Area(&building, name, p_min, p_max) ; building.add_area(area) ; @@ -55,7 +60,10 @@ void TopologyReaderCSV::process_area_line() void TopologyReaderCSV::process_waypoint_line() { - Point3D coordinates(read_point(waypoints_file)) ; + Point3D coordinates ; + if (! waypoints_file.read_point3d(coordinates)) + throw malformed_input_data("Cannot read waypoint coordinates!") ; + Waypoint &waypoint_ref = const_cast(Stock::find_create_waypoint(coordinates)) ; @@ -65,7 +73,7 @@ void TopologyReaderCSV::process_waypoint_line() buildings.push_back(const_cast( &Stock::find_create_building(building_name))) ; if (buildings.empty()) - throw malformed_topology("Cannot read building name!") ; + throw malformed_input_data("Cannot read building name!") ; for (vector::iterator i = buildings.begin() ; i != buildings.end() ; ++i) @@ -74,14 +82,3 @@ void TopologyReaderCSV::process_waypoint_line() (*i)->add_waypoint(&waypoint_ref) ; } } - - -Point3D TopologyReaderCSV::read_point(CSVFileReader &file) -{ - float coord[3] ; - for (unsigned int i = 0 ; i < 3 ; ++i) - if (! file.read_field(coord[i])) - throw malformed_topology("Cannot read a whole Point3D!") ; - - return Point3D(coord) ; -} diff --git a/owlps-positioning/src/topologyreadercsv.hh b/owlps-positioning/src/topologyreadercsv.hh index f86e02d..9077559 100644 --- a/owlps-positioning/src/topologyreadercsv.hh +++ b/owlps-positioning/src/topologyreadercsv.hh @@ -25,7 +25,6 @@ protected: void read_topology(void) ; void process_area_line(void) ; void process_waypoint_line(void) ; - Point3D read_point(CSVFileReader &file) ; public: TopologyReaderCSV(const std::string &areas_file_name,