Add support for printing to stdout in mj_saveLastXML.
PiperOrigin-RevId: 546849864 Change-Id: I799ea3fcd723cf036f82ef5c1ef8152c314072dc
This commit is contained in:
committed by
Copybara-Service
parent
5cfbb6ac8b
commit
24c1ec9aec
+5
-24
@@ -92,37 +92,18 @@ class LocaleOverride {
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
// Main writer function - calls mjXWrite
|
// Main writer function - calls mjXWrite
|
||||||
bool mjWriteXML(mjCModel* model, string filename, char* error, int error_sz) {
|
string mjWriteXML(mjCModel* model, char* error, int error_sz) {
|
||||||
LocaleOverride locale_override;
|
LocaleOverride locale_override;
|
||||||
|
|
||||||
// check for empty model
|
// check for empty model
|
||||||
if (!model) {
|
if (!model) {
|
||||||
mjCopyError(error, "Cannot write empty model", error_sz);
|
mjCopyError(error, "Cannot write empty model", error_sz);
|
||||||
return false;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
// write
|
mjXWriter writer;
|
||||||
FILE* fp = fopen(filename.c_str(), "w");
|
writer.SetModel(model);
|
||||||
if (!fp) {
|
return writer.Write(error, error_sz);
|
||||||
mjCopyError(error, "File not found", error_sz);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
mjXWriter writer;
|
|
||||||
writer.SetModel(model);
|
|
||||||
writer.Write(fp);
|
|
||||||
}
|
|
||||||
|
|
||||||
// catch known errors
|
|
||||||
catch (mjXError err) {
|
|
||||||
mjCopyError(error, err.message, error_sz);
|
|
||||||
fclose(fp);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(fp);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -23,7 +23,7 @@
|
|||||||
// Top level API
|
// Top level API
|
||||||
|
|
||||||
// Main writer function
|
// Main writer function
|
||||||
bool mjWriteXML(mjCModel* model, std::string filename, char* error, int error_sz);
|
std::string mjWriteXML(mjCModel* model, char* error, int error_sz);
|
||||||
|
|
||||||
// Main parser function
|
// Main parser function
|
||||||
mjCModel* mjParseXML(const char* filename, int vfs_provider, char* error, int error_sz);
|
mjCModel* mjParseXML(const char* filename, int vfs_provider, char* error, int error_sz);
|
||||||
|
|||||||
+20
-7
@@ -137,24 +137,37 @@ mjModel* mj_loadXML(const char* filename, const mjVFS* vfs,
|
|||||||
int mj_saveLastXML(const char* filename, const mjModel* m, char* error, int error_sz) {
|
int mj_saveLastXML(const char* filename, const mjModel* m, char* error, int error_sz) {
|
||||||
// serialize access to themodel
|
// serialize access to themodel
|
||||||
std::lock_guard<std::mutex> lock(themutex);
|
std::lock_guard<std::mutex> lock(themutex);
|
||||||
|
FILE *fp = stdout;
|
||||||
|
|
||||||
if (!themodel.model) {
|
if (!themodel.model) {
|
||||||
mjCopyError(error, "No XML model loaded", error_sz);
|
mjCopyError(error, "No XML model loaded", error_sz);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
themodel.model->CopyBack(m);
|
if (filename != nullptr && filename[0] != '\0') {
|
||||||
if (mjWriteXML(themodel.model, filename, error, error_sz)) {
|
fp = fopen(filename, "w");
|
||||||
if (error) {
|
if (!fp) {
|
||||||
error[0] = 0;
|
mjCopyError(error, "File not found", error_sz);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
themodel.model->CopyBack(m);
|
||||||
|
std::string result = mjWriteXML(themodel.model, error, error_sz);
|
||||||
|
|
||||||
|
if (!result.empty()) {
|
||||||
|
fprintf(fp, "%s", result.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fp != stdout) {
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
return !result.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// free last XML
|
// free last XML
|
||||||
void mj_freeLastXML(void) {
|
void mj_freeLastXML(void) {
|
||||||
// serialize access to themodel
|
// serialize access to themodel
|
||||||
|
|||||||
+4
-1
@@ -15,6 +15,7 @@
|
|||||||
#ifndef MUJOCO_SRC_XML_XML_BASE_H_
|
#ifndef MUJOCO_SRC_XML_XML_BASE_H_
|
||||||
#define MUJOCO_SRC_XML_XML_BASE_H_
|
#define MUJOCO_SRC_XML_XML_BASE_H_
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "tinyxml2.h"
|
#include "tinyxml2.h"
|
||||||
@@ -82,7 +83,9 @@ class mjXBase : public mjXUtil {
|
|||||||
virtual void Parse(tinyxml2::XMLElement* root) {};
|
virtual void Parse(tinyxml2::XMLElement* root) {};
|
||||||
|
|
||||||
// write: implemented in derived writer class
|
// write: implemented in derived writer class
|
||||||
virtual void Write(FILE* fp) {};
|
virtual std::string Write(char *error, std::size_t error_sz) {
|
||||||
|
return "";
|
||||||
|
};
|
||||||
|
|
||||||
// set the model allocated externally
|
// set the model allocated externally
|
||||||
void SetModel(mjCModel*);
|
void SetModel(mjCModel*);
|
||||||
|
|||||||
@@ -59,11 +59,15 @@ class mj_XMLPrinter : public tinyxml2::XMLPrinter {
|
|||||||
|
|
||||||
|
|
||||||
// save XML file using custom 2-space indentation
|
// save XML file using custom 2-space indentation
|
||||||
tinyxml2::XMLError SaveFile(XMLDocument& doc, FILE* fp) {
|
static string WriteDoc(XMLDocument& doc, char *error, size_t error_sz) {
|
||||||
doc.ClearError();
|
doc.ClearError();
|
||||||
mj_XMLPrinter stream(fp, /*compact=*/false);
|
mj_XMLPrinter stream(nullptr, /*compact=*/false);
|
||||||
doc.Print(&stream);
|
doc.Print(&stream);
|
||||||
return doc.ErrorID();
|
if (doc.ErrorID()) {
|
||||||
|
mjCopyError(error, doc.ErrorStr(), error_sz);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return string(stream.CStr());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -682,10 +686,11 @@ mjXWriter::mjXWriter(void) {
|
|||||||
|
|
||||||
|
|
||||||
// save existing model in MJCF canonical format, must be compiled
|
// save existing model in MJCF canonical format, must be compiled
|
||||||
void mjXWriter::Write(FILE* fp) {
|
string mjXWriter::Write(char *error, size_t error_sz) {
|
||||||
// check model
|
// check model
|
||||||
if (!model || !model->IsCompiled()) {
|
if (!model || !model->IsCompiled()) {
|
||||||
throw mjXError(0, "XML Write error: Only compiled model can be written");
|
mjCopyError(error, "XML Write error: Only compiled model can be written", error_sz);
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
// create document and root
|
// create document and root
|
||||||
@@ -722,8 +727,7 @@ void mjXWriter::Write(FILE* fp) {
|
|||||||
Sensor(root);
|
Sensor(root);
|
||||||
Keyframe(root);
|
Keyframe(root);
|
||||||
|
|
||||||
// save file
|
return WriteDoc(doc, error, error_sz);
|
||||||
SaveFile(doc, fp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
#ifndef MUJOCO_SRC_XML_XML_NATIVE_WRITER_H_
|
#ifndef MUJOCO_SRC_XML_XML_NATIVE_WRITER_H_
|
||||||
#define MUJOCO_SRC_XML_XML_NATIVE_WRITER_H_
|
#define MUJOCO_SRC_XML_XML_NATIVE_WRITER_H_
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "xml/xml_base.h"
|
#include "xml/xml_base.h"
|
||||||
@@ -24,7 +25,9 @@ class mjXWriter : public mjXBase {
|
|||||||
public:
|
public:
|
||||||
mjXWriter(); // constructor
|
mjXWriter(); // constructor
|
||||||
virtual ~mjXWriter() = default; // destructor
|
virtual ~mjXWriter() = default; // destructor
|
||||||
void Write(FILE* fp); // write XML document
|
|
||||||
|
// write XML document to string
|
||||||
|
std::string Write(char *error, std::size_t error_sz);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// insert end child with given name, return child
|
// insert end child with given name, return child
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
// Tests for xml/xml_api.cc.
|
// Tests for xml/xml_api.cc.
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -30,6 +31,7 @@ namespace {
|
|||||||
|
|
||||||
using ::testing::IsNull;
|
using ::testing::IsNull;
|
||||||
using ::testing::NotNull;
|
using ::testing::NotNull;
|
||||||
|
using ::testing::StartsWith;
|
||||||
|
|
||||||
// ---------------------------- test mj_loadXML --------------------------------
|
// ---------------------------- test mj_loadXML --------------------------------
|
||||||
|
|
||||||
@@ -54,16 +56,34 @@ TEST_F(LoadXmlTest, EmptyModel) {
|
|||||||
|
|
||||||
TEST_F(LoadXmlTest, InvalidXmlFailsToLoad) {
|
TEST_F(LoadXmlTest, InvalidXmlFailsToLoad) {
|
||||||
static constexpr char invalid_xml[] = "<mujoc";
|
static constexpr char invalid_xml[] = "<mujoc";
|
||||||
char error[1024];
|
std::array<char, 1024> error;
|
||||||
size_t error_sz = 1024;
|
mjModel* model = LoadModelFromString(invalid_xml, error.data(), error.size());
|
||||||
mjModel* model = LoadModelFromString(invalid_xml, error, error_sz);
|
|
||||||
EXPECT_THAT(model, IsNull()) << "Expected model loading to fail.";
|
EXPECT_THAT(model, IsNull()) << "Expected model loading to fail.";
|
||||||
EXPECT_GT(std::strlen(error), 0);
|
EXPECT_GT(std::strlen(error.data()), 0);
|
||||||
if (model) {
|
if (model) {
|
||||||
mj_deleteModel(model);
|
mj_deleteModel(model);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO(nimrod): Add more tests for mj_loadXML.
|
// TODO(nimrod): Add more tests for mj_loadXML.
|
||||||
|
|
||||||
|
using SaveLastXmlTest = MujocoTest;
|
||||||
|
|
||||||
|
TEST_F(SaveLastXmlTest, EmptyModel) {
|
||||||
|
static constexpr char xml[] = "<mujoco/>";
|
||||||
|
mjModel* model = LoadModelFromString(xml, 0, 0);
|
||||||
|
mjData* data = mj_makeData(model);
|
||||||
|
|
||||||
|
std::array<char, 1024> error;
|
||||||
|
error.data()[0] = '\0';
|
||||||
|
|
||||||
|
testing::internal::CaptureStdout();
|
||||||
|
mj_saveLastXML(nullptr, model, error.data(), error.size());
|
||||||
|
|
||||||
|
EXPECT_THAT(testing::internal::GetCapturedStdout(), StartsWith("<mujoco"));
|
||||||
|
|
||||||
|
mj_deleteData(data);
|
||||||
|
mj_deleteModel(model);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace mujoco
|
} // namespace mujoco
|
||||||
|
|||||||
Reference in New Issue
Block a user