Update of /cvsroot/objecthandler/ObjectHandler/oh
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv11495/oh
Modified Files:
utilities.cpp utilities.hpp
Log Message:
add function ohParseField()
Index: utilities.cpp
===================================================================
RCS file: /cvsroot/objecthandler/ObjectHandler/oh/utilities.cpp,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** utilities.cpp 19 Jun 2006 12:58:14 -0000 1.9
--- utilities.cpp 30 Jun 2006 09:33:39 -0000 1.10
***************
*** 25,28 ****
--- 25,30 ----
#include <oh/exception.hpp>
#include <sstream>
+ #include <boost/regex.hpp>
+ #include <boost/lexical_cast.hpp>
namespace ObjHandler {
***************
*** 80,83 ****
}
! }
--- 82,130 ----
}
! // parse a whitespace-delimited list of symbols
! // into a vector of strings
! std::vector<std::string> split(const std::string &line) {
! std::vector<std::string> ret;
! std::string::const_iterator start = line.begin();
! std::string::const_iterator end = line.end();
! boost::match_results<std::string::const_iterator> m;
! static const boost::regex r("[^[:space:]]+");
! while (boost::regex_search(start, end, m, r)) {
! ret.push_back(std::string(m[0].first, m[0].second));
! start = m[0].second;
! }
! return ret;
! }
+ double parseField(const std::string &line,
+ const long &index /* index - 1-base for user, 0-base internally */ ) {
+ if (index<1) {
+ std::stringstream msg;
+ msg << "the index of the requested field - " << index
+ << " - is invalid - minimum value is 1.";
+ throw Exception(msg.str());
+ }
+ unsigned int i = static_cast<unsigned int>(index);
+ std::vector<std::string> fields = split(line);
+ if (i>fields.size()) {
+ std::stringstream msg;
+ msg << "Error parsing string -" << std::endl
+ << line << std::endl << "- requested field index " << i
+ << " exceeds #fields (" << fields.size() << ") found in string";
+ throw Exception(msg.str());
+ }
+ double ret;
+ try {
+ ret = boost::lexical_cast<double>(fields[i-1]);
+ } catch (boost::bad_lexical_cast &) {
+ std::stringstream msg;
+ msg << "Error parsing string -" << std::endl
+ << line << std::endl << "- requested field #" << i
+ << " comprising text '" << fields[i-1]
+ << "' could not be converted to a number";
+ throw Exception(msg.str());
+ }
+ return ret;
+ }
+
+ }
Index: utilities.hpp
===================================================================
RCS file: /cvsroot/objecthandler/ObjectHandler/oh/utilities.hpp,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** utilities.hpp 9 Jun 2006 18:58:27 -0000 1.6
--- utilities.hpp 30 Jun 2006 09:33:39 -0000 1.7
***************
*** 80,83 ****
--- 80,88 ----
void logAllObjects();
+ //! Extract the ith number from a whitespace-delimited list of fields.
+ /*! Used to parse data returned from Reuters function RtGet().
+ */
+ double parseField(const std::string &line, const long &i);
+
//@}
}
|