EncoderArguments.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "EncoderArguments.h"
  2. #include "StringUtil.h"
  3. #ifdef WIN32
  4. #define PATH_MAX _MAX_PATH
  5. #define realpath(A,B) _fullpath(B,A,PATH_MAX)
  6. #endif
  7. EncoderArguments::EncoderArguments(size_t argc, const char** argv) :
  8. _fontSize(0),
  9. _parseError(false),
  10. _fontPreview(false),
  11. _textOutput(false),
  12. _daeOutput(false)
  13. {
  14. if (argc > 1)
  15. {
  16. size_t filePathIndex = argc - 1;
  17. if (argv[filePathIndex])
  18. {
  19. _filePath.assign(getRealPath(argv[filePathIndex]));
  20. }
  21. // read the options
  22. std::vector<std::string> options;
  23. for (size_t i = 1; i < filePathIndex; i++)
  24. {
  25. options.push_back(argv[i]);
  26. }
  27. for (size_t i = 0; i < options.size(); i++)
  28. {
  29. if (options[i][0] == '-')
  30. {
  31. readOption(options, &i);
  32. }
  33. }
  34. }
  35. else
  36. {
  37. _parseError = true;
  38. }
  39. }
  40. EncoderArguments::~EncoderArguments(void)
  41. {
  42. }
  43. const std::string& EncoderArguments::getFilePath() const
  44. {
  45. return _filePath;
  46. }
  47. const char* EncoderArguments::getFilePathPointer() const
  48. {
  49. return _filePath.c_str();
  50. }
  51. const std::string& EncoderArguments::getDAEOutputPath() const
  52. {
  53. return _daeOutputPath;
  54. }
  55. const std::vector<std::string>& EncoderArguments::getGroupAnimationNodeId() const
  56. {
  57. return _groupAnimationNodeId;
  58. }
  59. const std::vector<std::string>& EncoderArguments::getGroupAnimationAnimationId() const
  60. {
  61. return _groupAnimationAnimationId;
  62. }
  63. bool EncoderArguments::parseErrorOccured() const
  64. {
  65. return _parseError;
  66. }
  67. bool EncoderArguments::fileExists() const
  68. {
  69. if (_filePath.length() > 0)
  70. {
  71. struct stat buf;
  72. if (stat(_filePath.c_str(), &buf) != -1)
  73. {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. void EncoderArguments::printUsage() const
  80. {
  81. fprintf(stderr,"Usage: gameplay-encoder [options] <filepath>\n");
  82. fprintf(stderr,".dae file options:\n");
  83. fprintf(stderr," -i <id>\tFilter by node ID\n");
  84. fprintf(stderr," -t\tWrite text/xml\n");
  85. fprintf(stderr," -groupAnimations <nodeID> <animationID>\tGroup all animation channels targetting the nodes into a new animation\n");
  86. fprintf(stderr," -dae <filepath>\tOutput optimized DAE\n");
  87. fprintf(stderr,".ttf file options:\n");
  88. fprintf(stderr," -s <size of font> -p \n");
  89. exit(8);
  90. }
  91. bool EncoderArguments::fontPreviewEnabled() const
  92. {
  93. return _fontPreview;
  94. }
  95. bool EncoderArguments::textOutputEnabled() const
  96. {
  97. return _textOutput;
  98. }
  99. bool EncoderArguments::DAEOutputEnabled() const
  100. {
  101. return _daeOutput;
  102. }
  103. const char* EncoderArguments::getNodeId() const
  104. {
  105. if (_nodeId.length() == 0)
  106. {
  107. return NULL;
  108. }
  109. return _nodeId.c_str();
  110. }
  111. unsigned int EncoderArguments::getFontSize() const
  112. {
  113. return _fontSize;
  114. }
  115. EncoderArguments::FileFormat EncoderArguments::getFileFormat() const
  116. {
  117. if (_filePath.length() < 5)
  118. {
  119. return FILEFORMAT_UNKNOWN;
  120. }
  121. // Extract the extension
  122. std::string ext = "";
  123. size_t pos = _filePath.find_last_of(".");
  124. if (pos != std::string::npos)
  125. {
  126. ext = _filePath.substr(pos + 1);
  127. }
  128. // Match every supported extension with its format constant
  129. if (ext.compare("dae") == 0 || ext.compare("DAE") == 0)
  130. {
  131. return FILEFORMAT_DAE;
  132. }
  133. if (ext.compare("ttf") == 0 || ext.compare("TTF") == 0)
  134. {
  135. return FILEFORMAT_TTF;
  136. }
  137. if (ext.compare("gpb") == 0 || ext.compare("GPB") == 0)
  138. {
  139. return FILEFORMAT_GPB;
  140. }
  141. return FILEFORMAT_UNKNOWN;
  142. }
  143. void EncoderArguments::readOption(const std::vector<std::string>& options, size_t *index)
  144. {
  145. const std::string& str = options[*index];
  146. if (str.length() == 0 && str[0] != '-')
  147. {
  148. return;
  149. }
  150. switch (str[1])
  151. {
  152. case 'd':
  153. if (str.compare("-dae") == 0)
  154. {
  155. // read one string, make sure not to go out of bounds
  156. if ((*index + 1) >= options.size())
  157. {
  158. fprintf(stderr, "Error: -dae requires 1 argument.\n");
  159. _parseError = true;
  160. return;
  161. }
  162. (*index)++;
  163. _daeOutputPath = options[*index];
  164. _daeOutput = true;
  165. }
  166. break;
  167. case 'g':
  168. if (str.compare("-groupAnimations") == 0)
  169. {
  170. // read two strings, make sure not to go out of bounds
  171. if ((*index + 2) >= options.size())
  172. {
  173. fprintf(stderr, "Error: -groupAnimations requires 2 arguments.\n");
  174. _parseError = true;
  175. return;
  176. }
  177. (*index)++;
  178. _groupAnimationNodeId.push_back(options[*index]);
  179. (*index)++;
  180. _groupAnimationAnimationId.push_back(options[*index]);
  181. }
  182. break;
  183. case 'i':
  184. case 'o':
  185. // Node ID
  186. (*index)++;
  187. if (*index < options.size())
  188. {
  189. _nodeId.assign(options[*index]);
  190. }
  191. else
  192. {
  193. fprintf(stderr, "Error: missing arguemnt for -%c.\n", str[1]);
  194. _parseError = true;
  195. return;
  196. }
  197. case 'p':
  198. _fontPreview = true;
  199. break;
  200. case 's':
  201. // Font Size
  202. // old format was -s##
  203. if (str.length() > 2)
  204. {
  205. char n = str[2];
  206. if (n > '0' && n <= '9')
  207. {
  208. const char* number = str.c_str() + 2;
  209. _fontSize = atoi(number);
  210. break;
  211. }
  212. }
  213. (*index)++;
  214. if (*index < options.size())
  215. {
  216. _fontSize = atoi(options[*index].c_str());
  217. }
  218. else
  219. {
  220. fprintf(stderr, "Error: missing arguemnt for -%c.\n", str[1]);
  221. _parseError = true;
  222. return;
  223. }
  224. break;
  225. case 't':
  226. _textOutput = true;
  227. break;
  228. default:
  229. break;
  230. }
  231. }
  232. std::string EncoderArguments::getRealPath(const std::string& filepath)
  233. {
  234. char path[PATH_MAX + 1]; /* not sure about the "+ 1" */
  235. realpath(filepath.c_str(), path);
  236. replace_char(path, '\\', '/');
  237. return std::string(path);
  238. }
  239. void EncoderArguments::replace_char(char* str, char oldChar, char newChar)
  240. {
  241. for (; *str != '\0'; str++)
  242. {
  243. if (*str == oldChar)
  244. {
  245. *str = newChar;
  246. }
  247. }
  248. }