Ejemplo n.º 1
0
/**
 * @function readXml
 */
bool DartLoader::readFileToString(
  const common::ResourceRetrieverPtr& _resourceRetriever,
  const common::Uri& _uri,
  std::string &_output)
{
  const common::ResourcePtr resource = _resourceRetriever->retrieve(_uri);
  if (!resource)
    return false;

  // Safe because std::string is guaranteed to be contiguous in C++11.
  const size_t size = resource->getSize();
  _output.resize(size);
  resource->read(&_output.front(), size, 1);

  return true;
}
Ejemplo n.º 2
0
void openXMLFile(
  tinyxml2::XMLDocument& doc, const char* const filename,
  const common::ResourceRetrieverPtr& _retriever)
{
    common::ResourceRetrieverPtr retriever;
    if(_retriever)
      retriever = _retriever;
    else
      retriever = std::make_shared<common::LocalResourceRetriever>();

    const common::ResourcePtr resource = retriever->retrieve(filename);
    if(!resource)
    {
      dtwarn << "[openXMLFile] Failed opening URI '"
             << filename << "'.\n";
      throw std::runtime_error("Failed opening URI.");
    }

    // C++11 guarantees that std::string has contiguous storage.
    const size_t size = resource->getSize();
    std::string content;
    content.resize(size);
    if(resource->read(&content.front(), size, 1) != 1)
    {
      dtwarn << "[openXMLFile] Failed reading from URI '"
             << filename << "'.\n";
      throw std::runtime_error("Failed reading from URI.");
    }

    int const result = doc.Parse(&content.front());
    if(result != tinyxml2::XML_SUCCESS)
    {
      dtwarn << "[openXMLFile] Failed parsing XML: TinyXML2 returned error"
                " code " << result << ".\n";
      throw std::runtime_error("Failed parsing XML.");
    }
}