00001 #ifndef _NVINSTRUCTION_ANSWER_PARSING_H_
00002 #define _NVINSTRUCTION_ANSWER_PARSING_H_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include <vector>
00031 #include <string>
00032 #include <sstream>
00033 #include <iostream>
00034
00046 template<typename T>
00047 bool convertAnswerToVector(const std::string& answer, std::vector<T>& result, unsigned int numberOfExpectedValues = 3)
00048 {
00049 if (result.size() != numberOfExpectedValues)
00050 {
00051 std::cout << "result vector is of size " << result.size()
00052 << " but should be " << numberOfExpectedValues << std::endl;
00053 return false;
00054 }
00055 if ((answer.length() <= 0) || (0 == answer.compare("\r\n")))
00056 {
00057 std::cout << "answer string is empty!!" << std::endl;
00058 return false;
00059 }
00060 std::istringstream converter(answer);
00061
00062 for (unsigned int i = 0; i < numberOfExpectedValues; ++i)
00063 {
00064 converter >> result.at(i);
00065 if (converter.fail())
00066 {
00067 std::cout
00068 << "Problem decoding the answer. Return value at position: "
00069 << i << " was malformed." << std::endl;
00070 return false;
00071 }
00072 }
00073 return true;
00074 }
00075
00085 template<typename T>
00086 bool convertAnswerToType(const std::string& answer, T& result)
00087 {
00088 if ((answer.length() <= 0) || (0 == answer.compare("\r\n")))
00089 {
00090 std::cout << "answer string is empty!!" << std::endl;
00091 return false;
00092 }
00093 std::istringstream converter(answer);
00094
00095 converter >> result;
00096 if (converter.fail())
00097 {
00098 std::cout << "Problem decoding the answer." << std::endl;
00099 return false;
00100 }
00101 return true;
00102 }
00103
00104 #endif // _NVINSTRUCTION_ANSWER_PARSING_H_