Uniformized error codes (return values) in assimp_cmd.

pull/2958/head
Marc-Antoine Lortie 2020-01-26 13:10:21 -05:00
parent a1fbe74e87
commit b23c0b0637
7 changed files with 118 additions and 53 deletions

View File

@ -885,19 +885,19 @@ int Assimp_CompareDump (const char* const* params, unsigned int num)
// --help // --help
if ((num == 1 && !strcmp( params[0], "-h")) || !strcmp( params[0], "--help") || !strcmp( params[0], "-?") ) { if ((num == 1 && !strcmp( params[0], "-h")) || !strcmp( params[0], "--help") || !strcmp( params[0], "-?") ) {
printf("%s",AICMD_MSG_CMPDUMP_HELP); printf("%s",AICMD_MSG_CMPDUMP_HELP);
return 0; return AssimpCmdError::Success;
} }
// assimp cmpdump actual expected // assimp cmpdump actual expected
if (num < 2) { if (num < 2) {
std::cout << "assimp cmpdump: Invalid number of arguments. " std::cout << "assimp cmpdump: Invalid number of arguments. "
"See \'assimp cmpdump --help\'\r\n" << std::endl; "See \'assimp cmpdump --help\'\r\n" << std::endl;
return 1; return AssimpCmdError::InvalidNumberOfArguments;
} }
if(!strcmp(params[0],params[1])) { if(!strcmp(params[0],params[1])) {
std::cout << "assimp cmpdump: same file, same content." << std::endl; std::cout << "assimp cmpdump: same file, same content." << std::endl;
return 0; return AssimpCmdError::Success;
} }
class file_ptr class file_ptr
@ -924,13 +924,13 @@ int Assimp_CompareDump (const char* const* params, unsigned int num)
if (!actual) { if (!actual) {
std::cout << "assimp cmpdump: Failure reading ACTUAL data from " << std::cout << "assimp cmpdump: Failure reading ACTUAL data from " <<
params[0] << std::endl; params[0] << std::endl;
return -5; return AssimpCmdError::FailedToLoadInputFile;
} }
file_ptr expected(fopen(params[1],"rb")); file_ptr expected(fopen(params[1],"rb"));
if (!expected) { if (!expected) {
std::cout << "assimp cmpdump: Failure reading EXPECT data from " << std::cout << "assimp cmpdump: Failure reading EXPECT data from " <<
params[1] << std::endl; params[1] << std::endl;
return -6; return AssimpCmdCompareDumpError::FailedToLoadExpectedInputFile;
} }
comparer_context comp(actual,expected); comparer_context comp(actual,expected);
@ -940,17 +940,17 @@ int Assimp_CompareDump (const char* const* params, unsigned int num)
} }
catch(const compare_fails_exception& ex) { catch(const compare_fails_exception& ex) {
printf("%s",ex.what()); printf("%s",ex.what());
return -1; return AssimpCmdCompareDumpError::FileComparaisonFailure;
} }
catch(...) { catch(...) {
// we don't bother checking too rigourously here, so // we don't bother checking too rigourously here, so
// we might end up here ... // we might end up here ...
std::cout << "Unknown failure, are the input files well-defined?"; std::cout << "Unknown failure, are the input files well-defined?";
return -3; return AssimpCmdCompareDumpError::UnknownFailure;
} }
std::cout << "Success (totally " << std::dec << comp.get_num_chunks() << std::cout << "Success (totally " << std::dec << comp.get_num_chunks() <<
" chunks)" << std::endl; " chunks)" << std::endl;
return 0; return AssimpCmdError::Success;
} }

View File

@ -76,13 +76,13 @@ int Assimp_Export(const char* const* params, unsigned int num)
const char* const invalid = "assimp export: Invalid number of arguments. See \'assimp export --help\'\n"; const char* const invalid = "assimp export: Invalid number of arguments. See \'assimp export --help\'\n";
if (num < 1) { if (num < 1) {
printf(invalid); printf(invalid);
return 1; return AssimpCmdError::InvalidNumberOfArguments;
} }
// --help // --help
if (!strcmp( params[0], "-h") || !strcmp( params[0], "--help") || !strcmp( params[0], "-?") ) { if (!strcmp( params[0], "-h") || !strcmp( params[0], "--help") || !strcmp( params[0], "-?") ) {
printf("%s",AICMD_MSG_EXPORT_HELP_E); printf("%s",AICMD_MSG_EXPORT_HELP_E);
return 0; return AssimpCmdError::Success;
} }
std::string in = std::string(params[0]); std::string in = std::string(params[0]);
@ -156,7 +156,7 @@ int Assimp_Export(const char* const* params, unsigned int num)
// import the model // import the model
const aiScene* scene = ImportModel(import,in); const aiScene* scene = ImportModel(import,in);
if (!scene) { if (!scene) {
return -39; return AssimpCmdExportError::FailedToImportModel;
} }
// derive the final file name // derive the final file name
@ -164,10 +164,10 @@ int Assimp_Export(const char* const* params, unsigned int num)
// and call the export routine // and call the export routine
if(!ExportModel(scene, import, out,e->id)) { if(!ExportModel(scene, import, out,e->id)) {
return -25; return AssimpCmdExportError::FailedToExportModel;
} }
printf("assimp export: wrote output file: %s\n",out.c_str()); printf("assimp export: wrote output file: %s\n",out.c_str());
return 0; return AssimpCmdError::Success;
} }
#endif // no export #endif // no export

View File

@ -219,9 +219,9 @@ int DoExport(const aiTexture* tx, FILE* p, const std::string& extension,
} }
else { else {
printf("assimp extract: No available texture encoder found for %s\n", extension.c_str()); printf("assimp extract: No available texture encoder found for %s\n", extension.c_str());
return 1; return AssimpCmdExtractError::NoAvailableTextureEncoderFound;
} }
return 0; return AssimpCmdError::Success;
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
@ -232,13 +232,13 @@ int Assimp_Extract (const char* const* params, unsigned int num)
// assimp extract in out [options] // assimp extract in out [options]
if (num < 1) { if (num < 1) {
printf(invalid); printf(invalid);
return 1; return AssimpCmdError::InvalidNumberOfArguments;
} }
// --help // --help
if (!strcmp( params[0], "-h") || !strcmp( params[0], "--help") || !strcmp( params[0], "-?") ) { if (!strcmp( params[0], "-h") || !strcmp( params[0], "--help") || !strcmp( params[0], "-?") ) {
printf("%s",AICMD_MSG_DUMP_HELP_E); printf("%s",AICMD_MSG_DUMP_HELP_E);
return 0; return AssimpCmdError::Success;
} }
@ -308,7 +308,7 @@ int Assimp_Extract (const char* const* params, unsigned int num)
const aiScene* scene = ImportModel(import,in); const aiScene* scene = ImportModel(import,in);
if (!scene) { if (!scene) {
printf("assimp extract: Unable to load input file %s\n",in.c_str()); printf("assimp extract: Unable to load input file %s\n",in.c_str());
return 5; return AssimpCmdError::FailedToLoadInputFile;
} }
// get the texture(s) to be exported // get the texture(s) to be exported
@ -318,7 +318,7 @@ int Assimp_Extract (const char* const* params, unsigned int num)
if (texIdx >= scene->mNumTextures) { if (texIdx >= scene->mNumTextures) {
::printf("assimp extract: Texture %i requested, but there are just %i textures\n", ::printf("assimp extract: Texture %i requested, but there are just %i textures\n",
texIdx, scene->mNumTextures); texIdx, scene->mNumTextures);
return 6; return AssimpCmdExtractError::TextureIndexIsOutOfRange;
} }
} }
else { else {
@ -358,12 +358,14 @@ int Assimp_Extract (const char* const* params, unsigned int num)
FILE* p = ::fopen(out_cpy.c_str(),"wb"); FILE* p = ::fopen(out_cpy.c_str(),"wb");
if (!p) { if (!p) {
printf("assimp extract: Unable to open output file %s\n",out_cpy.c_str()); printf("assimp extract: Unable to open output file %s\n",out_cpy.c_str());
return 7; return AssimpCmdError::FailedToOpenOutputFile;
} }
int m; int m;
if (!tex->mHeight) { if (!tex->mHeight) {
m = (1 != fwrite(tex->pcData,tex->mWidth,1,p)); m = (1 != fwrite(tex->pcData,tex->mWidth,1,p))
? AssimpCmdError::Success
: AssimpCmdExtractError::FailedToExportCompressedTexture;
} }
else m = DoExport(tex,p,extension,flags); else m = DoExport(tex,p,extension,flags);
::fclose(p); ::fclose(p);
@ -372,5 +374,5 @@ int Assimp_Extract (const char* const* params, unsigned int num)
if (texIdx != 0xffffffff) if (texIdx != 0xffffffff)
return m; return m;
} }
return 0; return AssimpCmdError::Success;
} }

View File

@ -283,14 +283,14 @@ int Assimp_Info (const char* const* params, unsigned int num) {
// --help // --help
if (!strcmp( params[0],"-h")||!strcmp( params[0],"--help")||!strcmp( params[0],"-?") ) { if (!strcmp( params[0],"-h")||!strcmp( params[0],"--help")||!strcmp( params[0],"-?") ) {
printf("%s",AICMD_MSG_INFO_HELP_E); printf("%s",AICMD_MSG_INFO_HELP_E);
return 0; return AssimpCmdError::Success;
} }
// asssimp info <file> [-r] // asssimp info <file> [-r]
if (num < 1) { if (num < 1) {
printf("assimp info: Invalid number of arguments. " printf("assimp info: Invalid number of arguments. "
"See \'assimp info --help\'\n"); "See \'assimp info --help\'\n");
return 1; return AssimpCmdError::InvalidNumberOfArguments;
} }
const std::string in = std::string(params[0]); const std::string in = std::string(params[0]);
@ -314,7 +314,7 @@ int Assimp_Info (const char* const* params, unsigned int num) {
// Verbose and silent at the same time are not allowed // Verbose and silent at the same time are not allowed
if ( verbose && silent ) { if ( verbose && silent ) {
printf("assimp info: Invalid arguments, verbose and silent at the same time are forbitten. "); printf("assimp info: Invalid arguments, verbose and silent at the same time are forbitten. ");
return 1; return AssimpCmdInfoError::InvalidCombinaisonOfArguments;
} }
// Parse post-processing flags unless -r was specified // Parse post-processing flags unless -r was specified
@ -333,7 +333,7 @@ int Assimp_Info (const char* const* params, unsigned int num) {
if (!scene) { if (!scene) {
printf("assimp info: Unable to load input file %s\n", printf("assimp info: Unable to load input file %s\n",
in.c_str()); in.c_str());
return 5; return AssimpCmdError::FailedToLoadInputFile;
} }
aiMemoryInfo mem; aiMemoryInfo mem;
@ -391,7 +391,7 @@ int Assimp_Info (const char* const* params, unsigned int num) {
if (silent) if (silent)
{ {
printf("\n"); printf("\n");
return 0; return AssimpCmdError::Success;
} }
// meshes // meshes
@ -473,5 +473,5 @@ int Assimp_Info (const char* const* params, unsigned int num) {
PrintHierarchy(scene->mRootNode,"",verbose); PrintHierarchy(scene->mRootNode,"",verbose);
printf("\n"); printf("\n");
return 0; return AssimpCmdError::Success;
} }

View File

@ -85,7 +85,7 @@ int main (int argc, char* argv[])
{ {
if (argc <= 1) { if (argc <= 1) {
printf("assimp: No command specified. Use \'assimp help\' for a detailed command list\n"); printf("assimp: No command specified. Use \'assimp help\' for a detailed command list\n");
return 0; return AssimpCmdError::Success;
} }
// assimp version // assimp version
@ -102,7 +102,7 @@ int main (int argc, char* argv[])
(flags & ASSIMP_CFLAGS_STLPORT ? "-stlport " : ""), (flags & ASSIMP_CFLAGS_STLPORT ? "-stlport " : ""),
aiGetVersionRevision()); aiGetVersionRevision());
return 0; return AssimpCmdError::Success;
} }
// assimp help // assimp help
@ -110,7 +110,7 @@ int main (int argc, char* argv[])
// because people could try them intuitively) // because people could try them intuitively)
if (!strcmp(argv[1], "help") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) { if (!strcmp(argv[1], "help") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
printf("%s",AICMD_MSG_HELP); printf("%s",AICMD_MSG_HELP);
return 0; return AssimpCmdError::Success;
} }
// assimp cmpdump // assimp cmpdump
@ -137,7 +137,7 @@ int main (int argc, char* argv[])
imp.GetExtensionList(s); imp.GetExtensionList(s);
printf("%s\n",s.data); printf("%s\n",s.data);
return 0; return AssimpCmdError::Success;
} }
#ifndef ASSIMP_BUILD_NO_EXPORT #ifndef ASSIMP_BUILD_NO_EXPORT
@ -155,7 +155,7 @@ int main (int argc, char* argv[])
} }
printf("%s\n",s.data); printf("%s\n",s.data);
return 0; return AssimpCmdError::Success;
} }
@ -166,19 +166,19 @@ int main (int argc, char* argv[])
if (argc<3) { if (argc<3) {
printf("Expected file format id\n"); printf("Expected file format id\n");
return -11; return AssimpCmdError::NoFileFormatSpecified;
} }
for(size_t i = 0, end = exp.GetExportFormatCount(); i < end; ++i) { for(size_t i = 0, end = exp.GetExportFormatCount(); i < end; ++i) {
const aiExportFormatDesc* const e = exp.GetExportFormatDescription(i); const aiExportFormatDesc* const e = exp.GetExportFormatDescription(i);
if (!strcmp(e->id,argv[2])) { if (!strcmp(e->id,argv[2])) {
printf("%s\n%s\n%s\n",e->id,e->fileExtension,e->description); printf("%s\n%s\n%s\n",e->id,e->fileExtension,e->description);
return 0; return AssimpCmdError::Success;
} }
} }
printf("Unknown file format id: \'%s\'\n",argv[2]); printf("Unknown file format id: \'%s\'\n",argv[2]);
return -12; return AssimpCmdError::UnknownFileFormat;
} }
// assimp export // assimp export
@ -194,11 +194,11 @@ int main (int argc, char* argv[])
if (! strcmp(argv[1], "knowext")) { if (! strcmp(argv[1], "knowext")) {
if (argc<3) { if (argc<3) {
printf("Expected file extension"); printf("Expected file extension");
return -10; return AssimpCmdError::NoFileExtensionSpecified;
} }
const bool b = imp.IsExtensionSupported(argv[2]); const bool b = imp.IsExtensionSupported(argv[2]);
printf("File extension \'%s\' is %sknown\n",argv[2],(b?"":"not ")); printf("File extension \'%s\' is %sknown\n",argv[2],(b?"":"not "));
return b?0:-1; return b? AssimpCmdError::Success : AssimpCmdError::UnknownFileExtension;
} }
// assimp info // assimp info
@ -228,7 +228,7 @@ int main (int argc, char* argv[])
} }
printf("Unrecognized command. Use \'assimp help\' for a detailed command list\n"); printf("Unrecognized command. Use \'assimp help\' for a detailed command list\n");
return 1; return AssimpCmdError::UnrecognizedCommand;
} }
@ -505,7 +505,7 @@ int ProcessStandardArguments(
fill.log = true; fill.log = true;
} }
return 0; return AssimpCmdError::Success;
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
@ -517,5 +517,5 @@ int Assimp_TestBatchLoad (
globalImporter->ReadFile(params[i],aiProcessPreset_TargetRealtime_MaxQuality); globalImporter->ReadFile(params[i],aiProcessPreset_TargetRealtime_MaxQuality);
// we're totally silent. scene destructs automatically. // we're totally silent. scene destructs automatically.
} }
return 0; return AssimpCmdError::Success;
} }

View File

@ -114,13 +114,31 @@ struct ImportData {
bool log; bool log;
}; };
/// \enum AssimpCmdError
/// \brief General error codes used among assimp_cmd's utilities.
enum AssimpCmdError {
Success = 0,
InvalidNumberOfArguments,
UnrecognizedCommand,
FailedToLoadInputFile,
FailedToOpenOutputFile,
NoFileFormatSpecified,
UnknownFileFormat,
NoFileExtensionSpecified,
UnknownFileExtension,
// Add new error codes here...
LastAssimpCmdError, // Must be last.
};
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
/** Process standard arguments /** Process standard arguments
* *
* @param fill Filled by function * @param fill Filled by function
* @param params Command line parameters to be processed * @param params Command line parameters to be processed
* @param num NUmber of params * @param num NUmber of params
* @return 0 for success */ * @return An #AssimpCmdError value. */
int ProcessStandardArguments(ImportData& fill, int ProcessStandardArguments(ImportData& fill,
const char* const* params, const char* const* params,
unsigned int num); unsigned int num);
@ -151,43 +169,88 @@ bool ExportModel(const aiScene* pOut,
/** assimp_dump utility /** assimp_dump utility
* @param params Command line parameters to 'assimp dumb' * @param params Command line parameters to 'assimp dumb'
* @param Number of params * @param Number of params
* @return 0 for success*/ * @return An #AssimpCmdError value.*/
int Assimp_Dump ( int Assimp_Dump (
const char* const* params, const char* const* params,
unsigned int num); unsigned int num);
/// \enum AssimpCmdExportError
/// \brief Error codes used by the 'Export' utility.
enum AssimpCmdExportError {
FailedToImportModel = AssimpCmdError::LastAssimpCmdError,
FailedToExportModel,
// Add new error codes here...
LastAssimpCmdExportError, // Must be last.
};
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
/** assimp_export utility /** assimp_export utility
* @param params Command line parameters to 'assimp export' * @param params Command line parameters to 'assimp export'
* @param Number of params * @param Number of params
* @return 0 for success*/ * @return Either an #AssimpCmdError or #AssimpCmdExportError value. */
int Assimp_Export ( int Assimp_Export (
const char* const* params, const char* const* params,
unsigned int num); unsigned int num);
/// \enum AssimpCmdExtractError
/// \brief Error codes used by the 'Image Extractor' utility.
enum AssimpCmdExtractError {
TextureIndexIsOutOfRange = AssimpCmdError::LastAssimpCmdError,
NoAvailableTextureEncoderFound,
FailedToExportCompressedTexture,
// Add new error codes here...
LastAssimpCmdExtractError, // Must be last.
};
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
/** assimp_extract utility /** assimp_extract utility
* @param params Command line parameters to 'assimp extract' * @param params Command line parameters to 'assimp extract'
* @param Number of params * @param Number of params
* @return 0 for success*/ * @return Either an #AssimpCmdError or #AssimpCmdExtractError value. */
int Assimp_Extract ( int Assimp_Extract (
const char* const* params, const char* const* params,
unsigned int num); unsigned int num);
/// \enum AssimpCmdCompareDumpError
/// \brief Error codes used by the 'Compare Dump' utility.
enum AssimpCmdCompareDumpError {
FailedToLoadExpectedInputFile = AssimpCmdError::LastAssimpCmdError,
FileComparaisonFailure,
UnknownFailure,
// Add new error codes here...
LastAssimpCmdCompareDumpError, // Must be last.
};
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
/** assimp_cmpdump utility /** assimp_cmpdump utility
* @param params Command line parameters to 'assimp cmpdump' * @param params Command line parameters to 'assimp cmpdump'
* @param Number of params * @param Number of params
* @return 0 for success*/ * @return Either an #AssimpCmdError or #AssimpCmdCompareDumpError. */
int Assimp_CompareDump ( int Assimp_CompareDump (
const char* const* params, const char* const* params,
unsigned int num); unsigned int num);
/// \enum AssimpCmdInfoError
/// \brief Error codes used by the 'Info' utility.
enum AssimpCmdInfoError {
InvalidCombinaisonOfArguments = AssimpCmdError::LastAssimpCmdError,
// Add new error codes here...
LastAssimpCmdInfoError, // Must be last.
};
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
/** @brief assimp info utility /** @brief assimp info utility
* @param params Command line parameters to 'assimp info' * @param params Command line parameters to 'assimp info'
* @param Number of params * @param Number of params
* @return 0 for success */ * @return Either an #AssimpCmdError or #AssimpCmdInfoError value. */
int Assimp_Info ( int Assimp_Info (
const char* const* params, const char* const* params,
unsigned int num); unsigned int num);
@ -196,7 +259,7 @@ int Assimp_Info (
/** @brief assimp testbatchload utility /** @brief assimp testbatchload utility
* @param params Command line parameters to 'assimp testbatchload' * @param params Command line parameters to 'assimp testbatchload'
* @param Number of params * @param Number of params
* @return 0 for success */ * @return An #AssimpCmdError value. */
int Assimp_TestBatchLoad ( int Assimp_TestBatchLoad (
const char* const* params, const char* const* params,
unsigned int num); unsigned int num);

View File

@ -1341,13 +1341,13 @@ int Assimp_Dump (const char* const* params, unsigned int num)
// --help // --help
if (!strcmp( params[0], "-h") || !strcmp( params[0], "--help") || !strcmp( params[0], "-?") ) { if (!strcmp( params[0], "-h") || !strcmp( params[0], "--help") || !strcmp( params[0], "-?") ) {
printf("%s",AICMD_MSG_DUMP_HELP); printf("%s",AICMD_MSG_DUMP_HELP);
return 0; return AssimpCmdError::Success;
} }
// asssimp dump in out [options] // asssimp dump in out [options]
if (num < 1) { if (num < 1) {
printf("%s", fail); printf("%s", fail);
return 1; return AssimpCmdError::InvalidNumberOfArguments;
} }
std::string in = std::string(params[0]); std::string in = std::string(params[0]);
@ -1405,14 +1405,14 @@ int Assimp_Dump (const char* const* params, unsigned int num)
const aiScene* scene = ImportModel(import,in); const aiScene* scene = ImportModel(import,in);
if (!scene) { if (!scene) {
printf("assimp dump: Unable to load input file %s\n",in.c_str()); printf("assimp dump: Unable to load input file %s\n",in.c_str());
return 5; return AssimpCmdError::FailedToLoadInputFile;
} }
// open the output file and build the dump // open the output file and build the dump
FILE* o = ::fopen(out.c_str(),(binary ? "wb" : "wt")); FILE* o = ::fopen(out.c_str(),(binary ? "wb" : "wt"));
if (!o) { if (!o) {
printf("assimp dump: Unable to open output file %s\n",out.c_str()); printf("assimp dump: Unable to open output file %s\n",out.c_str());
return 12; return AssimpCmdError::FailedToOpenOutputFile;
} }
if (binary) { if (binary) {
@ -1426,6 +1426,6 @@ int Assimp_Dump (const char* const* params, unsigned int num)
} }
printf("assimp dump: Wrote output dump %s\n",out.c_str()); printf("assimp dump: Wrote output dump %s\n",out.c_str());
return 0; return AssimpCmdError::Success;
} }