| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- #include "EncoderArguments.h"
- #include "StringUtil.h"
- #ifdef WIN32
- #define PATH_MAX _MAX_PATH
- #define realpath(A,B) _fullpath(B,A,PATH_MAX)
- #endif
- EncoderArguments::EncoderArguments(size_t argc, const char** argv) :
- _fontSize(0),
- _parseError(false),
- _fontPreview(false),
- _textOutput(false),
- _daeOutput(false)
- {
- if (argc > 1)
- {
- size_t filePathIndex = argc - 1;
- if (argv[filePathIndex])
- {
- _filePath.assign(getRealPath(argv[filePathIndex]));
- }
-
- // read the options
- std::vector<std::string> options;
- for (size_t i = 1; i < filePathIndex; i++)
- {
- options.push_back(argv[i]);
- }
-
- for (size_t i = 0; i < options.size(); i++)
- {
- if (options[i][0] == '-')
- {
- readOption(options, &i);
- }
- }
- }
- else
- {
- _parseError = true;
- }
- }
- EncoderArguments::~EncoderArguments(void)
- {
- }
- const std::string& EncoderArguments::getFilePath() const
- {
- return _filePath;
- }
- const char* EncoderArguments::getFilePathPointer() const
- {
- return _filePath.c_str();
- }
- const std::string& EncoderArguments::getDAEOutputPath() const
- {
- return _daeOutputPath;
- }
- const std::vector<std::string>& EncoderArguments::getGroupAnimationNodeId() const
- {
- return _groupAnimationNodeId;
- }
- const std::vector<std::string>& EncoderArguments::getGroupAnimationAnimationId() const
- {
- return _groupAnimationAnimationId;
- }
- bool EncoderArguments::parseErrorOccured() const
- {
- return _parseError;
- }
- bool EncoderArguments::fileExists() const
- {
- if (_filePath.length() > 0)
- {
- struct stat buf;
- if (stat(_filePath.c_str(), &buf) != -1)
- {
- return true;
- }
- }
- return false;
- }
- void EncoderArguments::printUsage() const
- {
- fprintf(stderr,"Usage: gameplay-encoder [options] <filepath>\n");
- fprintf(stderr,".dae file options:\n");
- fprintf(stderr," -i <id>\tFilter by node ID\n");
- fprintf(stderr," -t\tWrite text/xml\n");
- fprintf(stderr," -groupAnimations <nodeID> <animationID>\tGroup all animation channels targetting the nodes into a new animation\n");
- fprintf(stderr," -dae <filepath>\tOutput optimized DAE\n");
- fprintf(stderr,".ttf file options:\n");
- fprintf(stderr," -s <size of font> -p \n");
- exit(8);
- }
- bool EncoderArguments::fontPreviewEnabled() const
- {
- return _fontPreview;
- }
- bool EncoderArguments::textOutputEnabled() const
- {
- return _textOutput;
- }
- bool EncoderArguments::DAEOutputEnabled() const
- {
- return _daeOutput;
- }
- const char* EncoderArguments::getNodeId() const
- {
- if (_nodeId.length() == 0)
- {
- return NULL;
- }
- return _nodeId.c_str();
- }
- unsigned int EncoderArguments::getFontSize() const
- {
- return _fontSize;
- }
- EncoderArguments::FileFormat EncoderArguments::getFileFormat() const
- {
- if (_filePath.length() < 5)
- {
- return FILEFORMAT_UNKNOWN;
- }
- // Extract the extension
- std::string ext = "";
- size_t pos = _filePath.find_last_of(".");
- if (pos != std::string::npos)
- {
- ext = _filePath.substr(pos + 1);
- }
-
- // Match every supported extension with its format constant
- if (ext.compare("dae") == 0 || ext.compare("DAE") == 0)
- {
- return FILEFORMAT_DAE;
- }
- if (ext.compare("ttf") == 0 || ext.compare("TTF") == 0)
- {
- return FILEFORMAT_TTF;
- }
- if (ext.compare("gpb") == 0 || ext.compare("GPB") == 0)
- {
- return FILEFORMAT_GPB;
- }
- return FILEFORMAT_UNKNOWN;
- }
- void EncoderArguments::readOption(const std::vector<std::string>& options, size_t *index)
- {
- const std::string& str = options[*index];
- if (str.length() == 0 && str[0] != '-')
- {
- return;
- }
- switch (str[1])
- {
- case 'd':
- if (str.compare("-dae") == 0)
- {
- // read one string, make sure not to go out of bounds
- if ((*index + 1) >= options.size())
- {
- fprintf(stderr, "Error: -dae requires 1 argument.\n");
- _parseError = true;
- return;
- }
- (*index)++;
- _daeOutputPath = options[*index];
- _daeOutput = true;
- }
- break;
- case 'g':
- if (str.compare("-groupAnimations") == 0)
- {
- // read two strings, make sure not to go out of bounds
- if ((*index + 2) >= options.size())
- {
- fprintf(stderr, "Error: -groupAnimations requires 2 arguments.\n");
- _parseError = true;
- return;
- }
- (*index)++;
- _groupAnimationNodeId.push_back(options[*index]);
- (*index)++;
- _groupAnimationAnimationId.push_back(options[*index]);
- }
- break;
- case 'i':
- case 'o':
- // Node ID
- (*index)++;
- if (*index < options.size())
- {
- _nodeId.assign(options[*index]);
- }
- else
- {
- fprintf(stderr, "Error: missing arguemnt for -%c.\n", str[1]);
- _parseError = true;
- return;
- }
- case 'p':
- _fontPreview = true;
- break;
- case 's':
- // Font Size
- // old format was -s##
- if (str.length() > 2)
- {
- char n = str[2];
- if (n > '0' && n <= '9')
- {
- const char* number = str.c_str() + 2;
- _fontSize = atoi(number);
- break;
- }
- }
- (*index)++;
- if (*index < options.size())
- {
- _fontSize = atoi(options[*index].c_str());
- }
- else
- {
- fprintf(stderr, "Error: missing arguemnt for -%c.\n", str[1]);
- _parseError = true;
- return;
- }
- break;
- case 't':
- _textOutput = true;
- break;
- default:
- break;
- }
- }
- std::string EncoderArguments::getRealPath(const std::string& filepath)
- {
- char path[PATH_MAX + 1]; /* not sure about the "+ 1" */
- realpath(filepath.c_str(), path);
- replace_char(path, '\\', '/');
- return std::string(path);
- }
- void EncoderArguments::replace_char(char* str, char oldChar, char newChar)
- {
- for (; *str != '\0'; str++)
- {
- if (*str == oldChar)
- {
- *str = newChar;
- }
- }
- }
|