iSpike  2.1
Spike conversion library for robotics
D:/Home/Programs/iSpike/src/Reader/FileAngleReader.cpp
Go to the documentation of this file.
00001 //iSpike includes
00002 #include <iSpike/Reader/FileAngleReader.hpp>
00003 #include <iSpike/ISpikeException.hpp>
00004 #include <iSpike/Log/Log.hpp>
00005 using namespace ispike;
00006 
00007 //Other includes
00008 #include <boost/regex.hpp>
00009 #include <vector>
00010 #include <iostream>
00011 #include <fstream>
00012 using namespace std;
00013 
00014 
00015 FileAngleReader::FileAngleReader() {
00016         // Define the properties of this reader
00017         addProperty(Property("anglesIn.txt", "File Name", "The file where the angles will be read from", true));
00018 
00019         //Create description
00020         readerDescription = Description("File Angle Reader", "This is a file angle reader", "Angle Reader");
00021 }
00022 
00023 
00024 FileAngleReader::~FileAngleReader(){
00025 }
00026 
00027 
00028 /*--------------------------------------------------------------------*/
00029 /*---------                 PUBLIC METHODS                     -------*/
00030 /*--------------------------------------------------------------------*/
00031 
00032 //Inherited from Reader
00033 void FileAngleReader::initialize(map<string, Property>& properties){
00034         setProperties(properties);
00035         setInitialized(true);
00036 }
00037 
00038 
00039 //Inherited from PropertyHolder
00040 void FileAngleReader::setProperties(map<string, Property>& properties){
00041         string fileName = updateStringProperty(properties["File Name"]);
00042         LOG(LOG_INFO) << "FileAngleReader: Reading angles from: " << fileName;
00043         readAngleFromFile(fileName);
00044 }
00045 
00046 
00047 /*--------------------------------------------------------------------*/
00048 /*---------                PRIVATE METHODS                     -------*/
00049 /*--------------------------------------------------------------------*/
00050 
00052 void FileAngleReader::readAngleFromFile(string& fileName) {
00053         ifstream fileStream;
00054         string contents;
00055 
00056         fileStream.open(fileName.c_str(), std::ios::in | std::ios::binary);
00057 
00058         if (!fileStream) {
00059                 std::ostringstream messageStream;
00060                 messageStream << "Can't read angles: " << fileName;
00061                 std::string message(messageStream.str());
00062                 throw ISpikeException(message);
00063         }
00064         fileStream >> contents;
00065 
00066         LOG(LOG_INFO) << "FileAngleReader: Read the following angles: " << contents;
00067 
00068         if (fileStream.fail()) {
00069                 std::ostringstream messageStream;
00070                 messageStream << "Can't read angles: " << fileName;
00071                 std::string message(messageStream.str());
00072                 throw ISpikeException(message);
00073         }
00074         fileStream.close();
00075 
00076         vector<double> buffer;
00077         boost::regex split_string(" ");
00078         list<string> lines;
00079         boost::regex_split(std::back_inserter(lines), contents, split_string);
00080         while(lines.size() > 0) {
00081                 std::string current_string = *(lines.begin());
00082                 lines.pop_front();
00083                 double angle = boost::lexical_cast<double>(current_string);
00084                 buffer.push_back(angle);
00085         }
00086 
00087         if(!buffer.empty()) {
00088                 setAngle(buffer[0]);
00089         } else {
00090                 throw ISpikeException("FileAngleReader: No angles found in file.");
00091         }
00092 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines