Add minor enhancements to compile binary.

* If output file exists, ask user whether to overwrite
* Fix erroneous check for whether XML file was successfully saved

PiperOrigin-RevId: 468743682
Change-Id: I06058c498328d333792e4b40d65c3a99ca1842e4
This commit is contained in:
Kevin Zakka
2022-08-19 11:14:37 -07:00
committed by Copybara-Service
parent 7b7cf0c27a
commit ee6fed1230
3 changed files with 17 additions and 11 deletions
+2 -3
View File
@@ -58,9 +58,8 @@ spot such a pattern, feel free to send a PR to update the guide.
Line length is 100 characters. In rare situations, like the collision table at
the top of
[engine_collision_driver.c](https://github.com/deepmind/mujoco/search?q=repo%3Ad
eepmind%2Fmujoco+filename%3Aengine_collision_driver.c), longer lines are alowed
for readability.
[engine_collision_driver.c](https://github.com/deepmind/mujoco/blob/c8ff7b3d341560e8cc33fbdcaffbcdbc4c32327c/src/engine/engine_collision_driver.c#L36),
longer lines are alowed for readability.
#### Comments
+13 -8
View File
@@ -14,17 +14,17 @@
#include <cctype>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <mujoco/mujoco.h>
// help
const char helpstring[] =
static constexpr char helpstring[] =
"\n Usage: compile infile outfile\n"
" infile can be in mjcf, urdf, mjb format\n"
" outfile can be in mjcf, mjb, txt format\n\n"
" Example: compile model.xml model.mjb\n";
" Example: compile model.xml model.mjb\n";
// deallocate and print message
@@ -36,7 +36,7 @@ int finish(const char* msg = 0, mjModel* m = 0) {
// print message
if (msg) {
std::printf("%s\n", msg);
std::cout << msg << std::endl;
}
return 0;
@@ -109,11 +109,16 @@ int main(int argc, const char** argv) {
return finish("Illegal combination of file formats");
}
// make sure output file does not exist
// check if output file exists
std::FILE* fp = std::fopen(argv[2], "r");
if (fp) {
std::fclose(fp);
return finish("Output file already exists");
std::cout << "Output file already exists, overwrite? (Y/n) ";
char c;
std::cin >> c;
if (c!='y' && c!='Y') {
std::fclose(fp);
return finish();
}
}
// load model
@@ -134,7 +139,7 @@ int main(int argc, const char** argv) {
// save model
if (type2==typeXML) {
if (mj_saveLastXML(argv[2], m, error, 1000)) {
if (!mj_saveLastXML(argv[2], m, error, 1000)) {
return finish(error, m);
}
} else if (type2==typeMJB) {
+2
View File
@@ -120,6 +120,8 @@ mjModel* mj_loadXML(const char* filename, const mjVFS* vfs,
// update XML data structures with info from low-level model, save as MJCF
// returns 1 if successful, 0 otherwise
// error can be NULL; otherwise assumed to have size error_sz
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);