| 54 | using BloombergLP::blpapi::MessageIterator; |
| 55 | |
| 56 | Rcpp::DataFrame processResponseEvent(Event event, const bool verbose) { |
| 57 | |
| 58 | MessageIterator msgIter(event); // create message iterator |
| 59 | if (!msgIter.next()) throw std::logic_error("Not a valid MessageIterator."); |
| 60 | |
| 61 | Message msg = msgIter.message(); // get message |
| 62 | if (verbose) msg.print(Rcpp::Rcout); |
| 63 | |
| 64 | Element response = msg.asElement(); // view as element |
| 65 | if (std::strcmp(response.name().string(), "BeqsResponse") != 0) |
| 66 | throw std::logic_error("Not a valid EQSDataResponse."); |
| 67 | |
| 68 | Element data = msg.getElement("data"); // get data payload, extract field with display units |
| 69 | Element fieldDisplayUnits = data.getElement("fieldDisplayUnits"); |
| 70 | if (verbose) fieldDisplayUnits.print(Rcpp::Rcout); |
| 71 | |
| 72 | int cols = fieldDisplayUnits.numElements(); // copy display units into column names |
| 73 | std::vector<std::string> colnames(cols); |
| 74 | for (int i=0; i<cols; i++) { |
| 75 | colnames[i] = fieldDisplayUnits.getElement(i).name().string(); |
| 76 | } |
| 77 | |
| 78 | Rcpp::List lst(cols); // Rcpp 'List' container of given number of columns |
| 79 | Rcpp::LogicalVector chk(cols, false); |
| 80 | bool allgood; |
| 81 | |
| 82 | Element results = data.getElement("securityData"); // get security data payload == actual result set |
| 83 | int rows = results.numValues(); // total number of rows in result set |
| 84 | if (verbose) results.print(Rcpp::Rcout); |
| 85 | if (verbose) Rcpp::Rcout << rows << " Rows expected" << std::endl; |
| 86 | |
| 87 | for (int j = 0; j < rows; j++) { // look at all rows to infer types, break if all found |
| 88 | response = results.getValueAsElement(j); // pick j-th element to infer types |
| 89 | data = response.getElement("fieldData"); // get data payload of first elemnt |
| 90 | if (verbose) data.print(Rcpp::Rcout); |
| 91 | |
| 92 | allgood = true; |
| 93 | for (int i=0; i<cols; i++) { // loop over first data set, and infer types |
| 94 | if (!chk(i) && // column has not been set yet |
| 95 | data.hasElement(colnames[i].c_str())) { |
| 96 | Element val = data.getElement(colnames[i].c_str()); |
| 97 | if (val.datatype() == BLPAPI_DATATYPE_STRING) { |
| 98 | lst[i] = Rcpp::CharacterVector(rows, R_NaString); |
| 99 | chk[i] = true; |
| 100 | } else if (val.datatype() == BLPAPI_DATATYPE_FLOAT64) { |
| 101 | lst[i] = Rcpp::NumericVector(rows, NA_REAL); |
| 102 | chk[i] = true; |
| 103 | } else if (val.datatype() == BLPAPI_DATATYPE_DATE) { |
| 104 | lst[i] = Rcpp::DateVector(rows); |
| 105 | chk[i] = true; |
| 106 | } else { // fallback |
| 107 | //Rcpp::Rcout << "Seeing type " << val.datatype() << std::endl; |
| 108 | lst[i] = Rcpp::CharacterVector(rows, R_NaString); |
| 109 | chk[i] = true; |
| 110 | } |
| 111 | } |
| 112 | allgood = allgood and chk[i]; |
| 113 | } |