Add support for printing to stdout in mj_saveLastXML.

PiperOrigin-RevId: 546849864
Change-Id: I799ea3fcd723cf036f82ef5c1ef8152c314072dc
This commit is contained in:
Kyle Bayes
2023-07-10 06:07:24 -07:00
committed by Copybara-Service
parent 5cfbb6ac8b
commit 24c1ec9aec
7 changed files with 69 additions and 45 deletions
+20 -7
View File
@@ -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) {
// serialize access to themodel
std::lock_guard<std::mutex> lock(themutex);
FILE *fp = stdout;
if (!themodel.model) {
mjCopyError(error, "No XML model loaded", error_sz);
return 0;
}
themodel.model->CopyBack(m);
if (mjWriteXML(themodel.model, filename, error, error_sz)) {
if (error) {
error[0] = 0;
if (filename != nullptr && filename[0] != '\0') {
fp = fopen(filename, "w");
if (!fp) {
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
void mj_freeLastXML(void) {
// serialize access to themodel