Properties.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. #include "Base.h"
  2. #include "Properties.h"
  3. #include "FileSystem.h"
  4. #include "Quaternion.h"
  5. namespace gameplay
  6. {
  7. /**
  8. * Reads the next character from the stream. Returns EOF if the end of the stream is reached.
  9. */
  10. static signed char readChar(Stream* stream)
  11. {
  12. if (stream->eof())
  13. return EOF;
  14. signed char c;
  15. if (stream->read(&c, 1, 1) != 1)
  16. return EOF;
  17. return c;
  18. }
  19. // Utility functions (shared with SceneLoader).
  20. /** @script{ignore} */
  21. void calculateNamespacePath(const std::string& urlString, std::string& fileString, std::vector<std::string>& namespacePath);
  22. /** @script{ignore} */
  23. Properties* getPropertiesFromNamespacePath(Properties* properties, const std::vector<std::string>& namespacePath);
  24. Properties::Properties()
  25. : _dirPath(NULL), _parent(NULL)
  26. {
  27. }
  28. Properties::Properties(const Properties& copy)
  29. : _namespace(copy._namespace), _id(copy._id), _parentID(copy._parentID), _properties(copy._properties), _dirPath(NULL), _parent(copy._parent)
  30. {
  31. setDirectoryPath(copy._dirPath);
  32. _namespaces = std::vector<Properties*>();
  33. std::vector<Properties*>::const_iterator it;
  34. for (it = copy._namespaces.begin(); it < copy._namespaces.end(); ++it)
  35. {
  36. GP_ASSERT(*it);
  37. _namespaces.push_back(new Properties(**it));
  38. }
  39. rewind();
  40. }
  41. Properties::Properties(Stream* stream)
  42. : _dirPath(NULL), _parent(NULL)
  43. {
  44. readProperties(stream);
  45. rewind();
  46. }
  47. Properties::Properties(Stream* stream, const char* name, const char* id, const char* parentID, Properties* parent)
  48. : _namespace(name), _dirPath(NULL), _parent(parent)
  49. {
  50. if (id)
  51. {
  52. _id = id;
  53. }
  54. if (parentID)
  55. {
  56. _parentID = parentID;
  57. }
  58. readProperties(stream);
  59. rewind();
  60. }
  61. Properties* Properties::create(const char* url)
  62. {
  63. if (!url || strlen(url) == 0)
  64. {
  65. GP_ERROR("Attempting to create a Properties object from an empty URL!");
  66. return NULL;
  67. }
  68. // Calculate the file and full namespace path from the specified url.
  69. std::string urlString = url;
  70. std::string fileString;
  71. std::vector<std::string> namespacePath;
  72. calculateNamespacePath(urlString, fileString, namespacePath);
  73. std::auto_ptr<Stream> stream(FileSystem::open(fileString.c_str()));
  74. if (stream.get() == NULL)
  75. {
  76. GP_ERROR("Failed to open file '%s'.", fileString.c_str());
  77. return NULL;
  78. }
  79. Properties* properties = new Properties(stream.get());
  80. properties->resolveInheritance();
  81. stream->close();
  82. // Get the specified properties object.
  83. Properties* p = getPropertiesFromNamespacePath(properties, namespacePath);
  84. if (!p)
  85. {
  86. GP_ERROR("Failed to load properties from url '%s'.", url);
  87. return NULL;
  88. }
  89. // If the loaded properties object is not the root namespace,
  90. // then we have to clone it and delete the root namespace
  91. // so that we don't leak memory.
  92. if (p != properties)
  93. {
  94. p = p->clone();
  95. SAFE_DELETE(properties);
  96. }
  97. p->setDirectoryPath(FileSystem::getDirectoryName(fileString.c_str()));
  98. return p;
  99. }
  100. void Properties::readProperties(Stream* stream)
  101. {
  102. GP_ASSERT(stream);
  103. char line[2048];
  104. int c;
  105. char* name;
  106. char* value;
  107. char* parentID;
  108. char* rc;
  109. char* rcc;
  110. char* rccc;
  111. bool comment = false;
  112. while (true)
  113. {
  114. // Skip whitespace at the start of lines
  115. skipWhiteSpace(stream);
  116. // Stop when we have reached the end of the file.
  117. if (stream->eof())
  118. break;
  119. // Read the next line.
  120. rc = stream->readLine(line, 2048);
  121. if (rc == NULL)
  122. {
  123. GP_ERROR("Error reading line from file.");
  124. return;
  125. }
  126. // Ignore comments
  127. if (comment)
  128. {
  129. // Check for end of multi-line comment at either start or end of line
  130. if (strncmp(line, "*/", 2) == 0)
  131. comment = false;
  132. else
  133. {
  134. trimWhiteSpace(line);
  135. const int len = strlen(line);
  136. if (len >= 2 && strncmp(line + (len - 2), "*/", 2) == 0)
  137. comment = false;
  138. }
  139. }
  140. else if (strncmp(line, "/*", 2) == 0)
  141. {
  142. // Start of multi-line comment (must be at start of line)
  143. comment = true;
  144. }
  145. else if (strncmp(line, "//", 2) != 0)
  146. {
  147. // If an '=' appears on this line, parse it as a name/value pair.
  148. // Note: strchr() has to be called before strtok(), or a backup of line has to be kept.
  149. rc = strchr(line, '=');
  150. if (rc != NULL)
  151. {
  152. // There could be a '}' at the end of the line, ending a namespace.
  153. rc = strchr(line, '}');
  154. // First token should be the property name.
  155. name = strtok(line, "=");
  156. if (name == NULL)
  157. {
  158. GP_ERROR("Error parsing properties file: attribute without name.");
  159. return;
  160. }
  161. // Remove white-space from name.
  162. name = trimWhiteSpace(name);
  163. // Scan for next token, the property's value.
  164. value = strtok(NULL, "=");
  165. if (value == NULL)
  166. {
  167. GP_ERROR("Error parsing properties file: attribute with name ('%s') but no value.", name);
  168. return;
  169. }
  170. // Remove white-space from value.
  171. value = trimWhiteSpace(value);
  172. // Store name/value pair.
  173. _properties[name] = value;
  174. if (rc != NULL)
  175. {
  176. // End of namespace.
  177. return;
  178. }
  179. }
  180. else
  181. {
  182. parentID = NULL;
  183. // Get the last character on the line (ignoring whitespace).
  184. const char* lineEnd = trimWhiteSpace(line) + (strlen(trimWhiteSpace(line)) - 1);
  185. // This line might begin or end a namespace,
  186. // or it might be a key/value pair without '='.
  187. // Check for '{' on same line.
  188. rc = strchr(line, '{');
  189. // Check for inheritance: ':'
  190. rcc = strchr(line, ':');
  191. // Check for '}' on same line.
  192. rccc = strchr(line, '}');
  193. // Get the name of the namespace.
  194. name = strtok(line, " \t\n{");
  195. name = trimWhiteSpace(name);
  196. if (name == NULL)
  197. {
  198. GP_ERROR("Error parsing properties file: failed to determine a valid token for line '%s'.", line);
  199. return;
  200. }
  201. else if (name[0] == '}')
  202. {
  203. // End of namespace.
  204. return;
  205. }
  206. // Get its ID if it has one.
  207. value = strtok(NULL, ":{");
  208. value = trimWhiteSpace(value);
  209. // Get its parent ID if it has one.
  210. if (rcc != NULL)
  211. {
  212. parentID = strtok(NULL, "{");
  213. parentID = trimWhiteSpace(parentID);
  214. }
  215. if (value != NULL && value[0] == '{')
  216. {
  217. // If the namespace ends on this line, seek back to right before the '}' character.
  218. if (rccc && rccc == lineEnd)
  219. {
  220. if (stream->seek(-1, SEEK_CUR) == false)
  221. {
  222. GP_ERROR("Failed to seek back to before a '}' character in properties file.");
  223. return;
  224. }
  225. while (readChar(stream) != '}')
  226. {
  227. if (stream->seek(-2, SEEK_CUR) == false)
  228. {
  229. GP_ERROR("Failed to seek back to before a '}' character in properties file.");
  230. return;
  231. }
  232. }
  233. if (stream->seek(-1, SEEK_CUR) == false)
  234. {
  235. GP_ERROR("Failed to seek back to before a '}' character in properties file.");
  236. return;
  237. }
  238. }
  239. // New namespace without an ID.
  240. Properties* space = new Properties(stream, name, NULL, parentID, this);
  241. _namespaces.push_back(space);
  242. // If the namespace ends on this line, seek to right after the '}' character.
  243. if (rccc && rccc == lineEnd)
  244. {
  245. if (stream->seek(1, SEEK_CUR) == false)
  246. {
  247. GP_ERROR("Failed to seek to immediately after a '}' character in properties file.");
  248. return;
  249. }
  250. }
  251. }
  252. else
  253. {
  254. // If '{' appears on the same line.
  255. if (rc != NULL)
  256. {
  257. // If the namespace ends on this line, seek back to right before the '}' character.
  258. if (rccc && rccc == lineEnd)
  259. {
  260. if (stream->seek(-1, SEEK_CUR) == false)
  261. {
  262. GP_ERROR("Failed to seek back to before a '}' character in properties file.");
  263. return;
  264. }
  265. while (readChar(stream) != '}')
  266. {
  267. if (stream->seek(-2, SEEK_CUR) == false)
  268. {
  269. GP_ERROR("Failed to seek back to before a '}' character in properties file.");
  270. return;
  271. }
  272. }
  273. if (stream->seek(-1, SEEK_CUR) == false)
  274. {
  275. GP_ERROR("Failed to seek back to before a '}' character in properties file.");
  276. return;
  277. }
  278. }
  279. // Create new namespace.
  280. Properties* space = new Properties(stream, name, value, parentID, this);
  281. _namespaces.push_back(space);
  282. // If the namespace ends on this line, seek to right after the '}' character.
  283. if (rccc && rccc == lineEnd)
  284. {
  285. if (stream->seek(1, SEEK_CUR) == false)
  286. {
  287. GP_ERROR("Failed to seek to immediately after a '}' character in properties file.");
  288. return;
  289. }
  290. }
  291. }
  292. else
  293. {
  294. // Find out if the next line starts with "{"
  295. skipWhiteSpace(stream);
  296. c = readChar(stream);
  297. if (c == '{')
  298. {
  299. // Create new namespace.
  300. Properties* space = new Properties(stream, name, value, parentID, this);
  301. _namespaces.push_back(space);
  302. }
  303. else
  304. {
  305. // Back up from fgetc()
  306. if (stream->seek(-1, SEEK_CUR) == false)
  307. GP_ERROR("Failed to seek backwards a single character after testing if the next line starts with '{'.");
  308. // Store "name value" as a name/value pair, or even just "name".
  309. if (value != NULL)
  310. {
  311. _properties[name] = value;
  312. }
  313. else
  314. {
  315. _properties[name] = std::string();
  316. }
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }
  324. Properties::~Properties()
  325. {
  326. SAFE_DELETE(_dirPath);
  327. for (size_t i = 0, count = _namespaces.size(); i < count; ++i)
  328. {
  329. SAFE_DELETE(_namespaces[i]);
  330. }
  331. }
  332. void Properties::skipWhiteSpace(Stream* stream)
  333. {
  334. signed char c;
  335. do
  336. {
  337. c = readChar(stream);
  338. } while (isspace(c) && c != EOF);
  339. // If we are not at the end of the file, then since we found a
  340. // non-whitespace character, we put the cursor back in front of it.
  341. if (c != EOF)
  342. {
  343. if (stream->seek(-1, SEEK_CUR) == false)
  344. {
  345. GP_ERROR("Failed to seek backwards one character after skipping whitespace.");
  346. }
  347. }
  348. }
  349. char* Properties::trimWhiteSpace(char *str)
  350. {
  351. if (str == NULL)
  352. {
  353. return str;
  354. }
  355. char *end;
  356. // Trim leading space.
  357. while (isspace(*str))
  358. str++;
  359. // All spaces?
  360. if (*str == 0)
  361. {
  362. return str;
  363. }
  364. // Trim trailing space.
  365. end = str + strlen(str) - 1;
  366. while (end > str && isspace(*end))
  367. end--;
  368. // Write new null terminator.
  369. *(end+1) = 0;
  370. return str;
  371. }
  372. void Properties::resolveInheritance(const char* id)
  373. {
  374. // Namespaces can be defined like so:
  375. // "name id : parentID { }"
  376. // This method merges data from the parent namespace into the child.
  377. // Get a top-level namespace.
  378. Properties* derived;
  379. if (id)
  380. {
  381. derived = getNamespace(id);
  382. }
  383. else
  384. {
  385. derived = getNextNamespace();
  386. }
  387. while (derived)
  388. {
  389. // If the namespace has a parent ID, find the parent.
  390. if (!derived->_parentID.empty())
  391. {
  392. Properties* parent = getNamespace(derived->_parentID.c_str());
  393. if (parent)
  394. {
  395. resolveInheritance(parent->getId());
  396. // Copy the child.
  397. Properties* overrides = new Properties(*derived);
  398. // Delete the child's data.
  399. for (size_t i = 0, count = derived->_namespaces.size(); i < count; i++)
  400. {
  401. SAFE_DELETE(derived->_namespaces[i]);
  402. }
  403. // Copy data from the parent into the child.
  404. derived->_properties = parent->_properties;
  405. derived->_namespaces = std::vector<Properties*>();
  406. std::vector<Properties*>::const_iterator itt;
  407. for (itt = parent->_namespaces.begin(); itt < parent->_namespaces.end(); ++itt)
  408. {
  409. GP_ASSERT(*itt);
  410. derived->_namespaces.push_back(new Properties(**itt));
  411. }
  412. derived->rewind();
  413. // Take the original copy of the child and override the data copied from the parent.
  414. derived->mergeWith(overrides);
  415. // Delete the child copy.
  416. SAFE_DELETE(overrides);
  417. }
  418. }
  419. // Resolve inheritance within this namespace.
  420. derived->resolveInheritance();
  421. // Get the next top-level namespace and check again.
  422. if (!id)
  423. {
  424. derived = getNextNamespace();
  425. }
  426. else
  427. {
  428. derived = NULL;
  429. }
  430. }
  431. }
  432. void Properties::mergeWith(Properties* overrides)
  433. {
  434. GP_ASSERT(overrides);
  435. // Overwrite or add each property found in child.
  436. char* value = new char[255];
  437. overrides->rewind();
  438. const char* name = overrides->getNextProperty(&value);
  439. while (name)
  440. {
  441. this->_properties[name] = value;
  442. name = overrides->getNextProperty(&value);
  443. }
  444. SAFE_DELETE_ARRAY(value);
  445. this->_propertiesItr = this->_properties.end();
  446. // Merge all common nested namespaces, add new ones.
  447. Properties* overridesNamespace = overrides->getNextNamespace();
  448. while (overridesNamespace)
  449. {
  450. bool merged = false;
  451. rewind();
  452. Properties* derivedNamespace = getNextNamespace();
  453. while (derivedNamespace)
  454. {
  455. if (strcmp(derivedNamespace->getNamespace(), overridesNamespace->getNamespace()) == 0 &&
  456. strcmp(derivedNamespace->getId(), overridesNamespace->getId()) == 0)
  457. {
  458. derivedNamespace->mergeWith(overridesNamespace);
  459. merged = true;
  460. }
  461. derivedNamespace = getNextNamespace();
  462. }
  463. if (!merged)
  464. {
  465. // Add this new namespace.
  466. Properties* newNamespace = new Properties(*overridesNamespace);
  467. this->_namespaces.push_back(newNamespace);
  468. this->_namespacesItr = this->_namespaces.end();
  469. }
  470. overridesNamespace = overrides->getNextNamespace();
  471. }
  472. }
  473. const char* Properties::getNextProperty(char** value)
  474. {
  475. if (_propertiesItr == _properties.end())
  476. {
  477. // Restart from the beginning
  478. _propertiesItr = _properties.begin();
  479. }
  480. else
  481. {
  482. // Move to the next property
  483. ++_propertiesItr;
  484. }
  485. if (_propertiesItr != _properties.end())
  486. {
  487. const std::string& name = _propertiesItr->first;
  488. if (!name.empty())
  489. {
  490. if (value)
  491. {
  492. strcpy(*value, _propertiesItr->second.c_str());
  493. }
  494. return name.c_str();
  495. }
  496. }
  497. return NULL;
  498. }
  499. Properties* Properties::getNextNamespace()
  500. {
  501. if (_namespacesItr == _namespaces.end())
  502. {
  503. // Restart from the beginning
  504. _namespacesItr = _namespaces.begin();
  505. }
  506. else
  507. {
  508. ++_namespacesItr;
  509. }
  510. if (_namespacesItr != _namespaces.end())
  511. {
  512. Properties* ns = *_namespacesItr;
  513. return ns;
  514. }
  515. return NULL;
  516. }
  517. void Properties::rewind()
  518. {
  519. _propertiesItr = _properties.end();
  520. _namespacesItr = _namespaces.end();
  521. }
  522. Properties* Properties::getNamespace(const char* id, bool searchNames) const
  523. {
  524. GP_ASSERT(id);
  525. Properties* ret = NULL;
  526. std::vector<Properties*>::const_iterator it;
  527. for (it = _namespaces.begin(); it < _namespaces.end(); ++it)
  528. {
  529. ret = *it;
  530. if (strcmp(searchNames ? ret->_namespace.c_str() : ret->_id.c_str(), id) == 0)
  531. {
  532. return ret;
  533. }
  534. // Search recursively.
  535. ret = ret->getNamespace(id, searchNames);
  536. if (ret != NULL)
  537. {
  538. return ret;
  539. }
  540. }
  541. return ret;
  542. }
  543. const char* Properties::getNamespace() const
  544. {
  545. return _namespace.c_str();
  546. }
  547. const char* Properties::getId() const
  548. {
  549. return _id.c_str();
  550. }
  551. bool Properties::exists(const char* name) const
  552. {
  553. GP_ASSERT(name);
  554. return _properties.find(name) != _properties.end();
  555. }
  556. static const bool isStringNumeric(const char* str)
  557. {
  558. GP_ASSERT(str);
  559. // The first character may be '-'
  560. if (*str == '-')
  561. str++;
  562. // The first character after the sign must be a digit
  563. if (!isdigit(*str))
  564. return false;
  565. str++;
  566. // All remaining characters must be digits, with a single decimal (.) permitted
  567. unsigned int decimalCount = 0;
  568. while (*str)
  569. {
  570. if (!isdigit(*str))
  571. {
  572. if (*str == '.' && decimalCount == 0)
  573. {
  574. // Max of 1 decimal allowed
  575. decimalCount++;
  576. }
  577. else
  578. {
  579. return false;
  580. }
  581. }
  582. str++;
  583. }
  584. return true;
  585. }
  586. Properties::Type Properties::getType(const char* name) const
  587. {
  588. const char* value = getString(name);
  589. if (!value)
  590. {
  591. return Properties::NONE;
  592. }
  593. // Parse the value to determine the format
  594. unsigned int commaCount = 0;
  595. char* valuePtr = const_cast<char*>(value);
  596. while (valuePtr = strchr(valuePtr, ','))
  597. {
  598. valuePtr++;
  599. commaCount++;
  600. }
  601. switch (commaCount)
  602. {
  603. case 0:
  604. return isStringNumeric(value) ? Properties::NUMBER : Properties::STRING;
  605. case 1:
  606. return Properties::VECTOR2;
  607. case 2:
  608. return Properties::VECTOR3;
  609. case 3:
  610. return Properties::VECTOR4;
  611. case 15:
  612. return Properties::MATRIX;
  613. default:
  614. return Properties::STRING;
  615. }
  616. }
  617. const char* Properties::getString(const char* name) const
  618. {
  619. if (name)
  620. {
  621. std::map<std::string, std::string>::const_iterator itr = _properties.find(name);
  622. if (itr != _properties.end())
  623. {
  624. return itr->second.c_str();
  625. }
  626. }
  627. else
  628. {
  629. if (_propertiesItr != _properties.end())
  630. {
  631. return _propertiesItr->second.c_str();
  632. }
  633. }
  634. return NULL;
  635. }
  636. bool Properties::getBool(const char* name, bool defaultValue) const
  637. {
  638. const char* valueString = getString(name);
  639. if (valueString)
  640. {
  641. return (strcmp(valueString, "true") == 0);
  642. }
  643. return defaultValue;
  644. }
  645. int Properties::getInt(const char* name) const
  646. {
  647. const char* valueString = getString(name);
  648. if (valueString)
  649. {
  650. int value;
  651. int scanned;
  652. scanned = sscanf(valueString, "%d", &value);
  653. if (scanned != 1)
  654. {
  655. GP_ERROR("Error attempting to parse property '%s' as an integer.", name);
  656. return 0;
  657. }
  658. return value;
  659. }
  660. return 0;
  661. }
  662. float Properties::getFloat(const char* name) const
  663. {
  664. const char* valueString = getString(name);
  665. if (valueString)
  666. {
  667. float value;
  668. int scanned;
  669. scanned = sscanf(valueString, "%f", &value);
  670. if (scanned != 1)
  671. {
  672. GP_ERROR("Error attempting to parse property '%s' as a float.", name);
  673. return 0.0f;
  674. }
  675. return value;
  676. }
  677. return 0.0f;
  678. }
  679. long Properties::getLong(const char* name) const
  680. {
  681. const char* valueString = getString(name);
  682. if (valueString)
  683. {
  684. long value;
  685. int scanned;
  686. scanned = sscanf(valueString, "%ld", &value);
  687. if (scanned != 1)
  688. {
  689. GP_ERROR("Error attempting to parse property '%s' as a long integer.", name);
  690. return 0L;
  691. }
  692. return value;
  693. }
  694. return 0L;
  695. }
  696. bool Properties::getMatrix(const char* name, Matrix* out) const
  697. {
  698. GP_ASSERT(out);
  699. const char* valueString = getString(name);
  700. if (valueString)
  701. {
  702. float m[16];
  703. int scanned;
  704. scanned = sscanf(valueString, "%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f",
  705. &m[0], &m[1], &m[2], &m[3], &m[4], &m[5], &m[6], &m[7],
  706. &m[8], &m[9], &m[10], &m[11], &m[12], &m[13], &m[14], &m[15]);
  707. if (scanned != 16)
  708. {
  709. GP_ERROR("Error attempting to parse property '%s' as a matrix.", name);
  710. out->setIdentity();
  711. return false;
  712. }
  713. out->set(m);
  714. return true;
  715. }
  716. out->setIdentity();
  717. return false;
  718. }
  719. bool Properties::getVector2(const char* name, Vector2* out) const
  720. {
  721. GP_ASSERT(out);
  722. const char* valueString = getString(name);
  723. if (valueString)
  724. {
  725. float x, y;
  726. int scanned;
  727. scanned = sscanf(valueString, "%f,%f", &x, &y);
  728. if (scanned != 2)
  729. {
  730. GP_ERROR("Error attempting to parse property '%s' as a two-dimensional vector.", name);
  731. out->set(0.0f, 0.0f);
  732. return false;
  733. }
  734. out->set(x, y);
  735. return true;
  736. }
  737. out->set(0.0f, 0.0f);
  738. return false;
  739. }
  740. bool Properties::getVector3(const char* name, Vector3* out) const
  741. {
  742. GP_ASSERT(out);
  743. const char* valueString = getString(name);
  744. if (valueString)
  745. {
  746. float x, y, z;
  747. int scanned;
  748. scanned = sscanf(valueString, "%f,%f,%f", &x, &y, &z);
  749. if (scanned != 3)
  750. {
  751. GP_ERROR("Error attempting to parse property '%s' as a three-dimensional vector.", name);
  752. out->set(0.0f, 0.0f, 0.0f);
  753. return false;
  754. }
  755. out->set(x, y, z);
  756. return true;
  757. }
  758. out->set(0.0f, 0.0f, 0.0f);
  759. return false;
  760. }
  761. bool Properties::getVector4(const char* name, Vector4* out) const
  762. {
  763. GP_ASSERT(out);
  764. const char* valueString = getString(name);
  765. if (valueString)
  766. {
  767. float x, y, z, w;
  768. int scanned;
  769. scanned = sscanf(valueString, "%f,%f,%f,%f", &x, &y, &z, &w);
  770. if (scanned != 4)
  771. {
  772. GP_ERROR("Error attempting to parse property '%s' as a four-dimensional vector.", name);
  773. out->set(0.0f, 0.0f, 0.0f, 0.0f);
  774. return false;
  775. }
  776. out->set(x, y, z, w);
  777. return true;
  778. }
  779. out->set(0.0f, 0.0f, 0.0f, 0.0f);
  780. return false;
  781. }
  782. bool Properties::getQuaternionFromAxisAngle(const char* name, Quaternion* out) const
  783. {
  784. GP_ASSERT(out);
  785. const char* valueString = getString(name);
  786. if (valueString)
  787. {
  788. float x, y, z, theta;
  789. int scanned;
  790. scanned = sscanf(valueString, "%f,%f,%f,%f", &x, &y, &z, &theta);
  791. if (scanned != 4)
  792. {
  793. GP_ERROR("Error attempting to parse property '%s' as an axis-angle rotation.", name);
  794. out->set(0.0f, 0.0f, 0.0f, 1.0f);
  795. return false;
  796. }
  797. out->set(Vector3(x, y, z), MATH_DEG_TO_RAD(theta));
  798. return true;
  799. }
  800. out->set(0.0f, 0.0f, 0.0f, 1.0f);
  801. return false;
  802. }
  803. bool Properties::getColor(const char* name, Vector3* out) const
  804. {
  805. GP_ASSERT(out);
  806. const char* valueString = getString(name);
  807. if (valueString)
  808. {
  809. if (strlen(valueString) != 7 ||
  810. valueString[0] != '#')
  811. {
  812. // Not a color string.
  813. GP_ERROR("Error attempting to parse property '%s' as an RGB color (not specified as a color string).", name);
  814. out->set(0.0f, 0.0f, 0.0f);
  815. return false;
  816. }
  817. // Read the string into an int as hex.
  818. unsigned int color;
  819. if (sscanf(valueString+1, "%x", &color) != 1)
  820. {
  821. GP_ERROR("Error attempting to parse property '%s' as an RGB color.", name);
  822. out->set(0.0f, 0.0f, 0.0f);
  823. return false;
  824. }
  825. out->set(Vector3::fromColor(color));
  826. return true;
  827. }
  828. out->set(0.0f, 0.0f, 0.0f);
  829. return false;
  830. }
  831. bool Properties::getColor(const char* name, Vector4* out) const
  832. {
  833. GP_ASSERT(out);
  834. const char* valueString = getString(name);
  835. if (valueString)
  836. {
  837. if (strlen(valueString) != 9 ||
  838. valueString[0] != '#')
  839. {
  840. // Not a color string.
  841. GP_ERROR("Error attempting to parse property '%s' as an RGBA color (not specified as a color string).", name);
  842. out->set(0.0f, 0.0f, 0.0f, 0.0f);
  843. return false;
  844. }
  845. // Read the string into an int as hex.
  846. unsigned int color;
  847. if (sscanf(valueString+1, "%x", &color) != 1)
  848. {
  849. GP_ERROR("Error attempting to parse property '%s' as an RGBA color.", name);
  850. out->set(0.0f, 0.0f, 0.0f, 0.0f);
  851. return false;
  852. }
  853. out->set(Vector4::fromColor(color));
  854. return true;
  855. }
  856. out->set(0.0f, 0.0f, 0.0f, 0.0f);
  857. return false;
  858. }
  859. bool Properties::getPath(const char* name, std::string* path) const
  860. {
  861. GP_ASSERT(name && path);
  862. const char* valueString = getString(name);
  863. if (valueString)
  864. {
  865. if (FileSystem::fileExists(valueString))
  866. {
  867. path->assign(valueString);
  868. return true;
  869. }
  870. else
  871. {
  872. const Properties* prop = this;
  873. while (prop != NULL)
  874. {
  875. // Search for the file path relative to the bundle file
  876. const std::string* dirPath = prop->_dirPath;
  877. if (dirPath != NULL && !dirPath->empty())
  878. {
  879. std::string relativePath = *dirPath;
  880. relativePath.append(valueString);
  881. if (FileSystem::fileExists(relativePath.c_str()))
  882. {
  883. path->assign(relativePath);
  884. return true;
  885. }
  886. }
  887. prop = prop->_parent;
  888. }
  889. }
  890. }
  891. return false;
  892. }
  893. Properties* Properties::clone()
  894. {
  895. Properties* p = new Properties();
  896. p->_namespace = _namespace;
  897. p->_id = _id;
  898. p->_parentID = _parentID;
  899. p->_properties = _properties;
  900. p->_propertiesItr = p->_properties.end();
  901. p->setDirectoryPath(_dirPath);
  902. for (size_t i = 0, count = _namespaces.size(); i < count; i++)
  903. {
  904. GP_ASSERT(_namespaces[i]);
  905. Properties* child = _namespaces[i]->clone();
  906. p->_namespaces.push_back(child);
  907. child->_parent = p;
  908. }
  909. p->_namespacesItr = p->_namespaces.end();
  910. return p;
  911. }
  912. void Properties::setDirectoryPath(const std::string* path)
  913. {
  914. if (path)
  915. {
  916. setDirectoryPath(*path);
  917. }
  918. else
  919. {
  920. SAFE_DELETE(_dirPath);
  921. }
  922. }
  923. void Properties::setDirectoryPath(const std::string& path)
  924. {
  925. if (_dirPath == NULL)
  926. {
  927. _dirPath = new std::string(path);
  928. }
  929. else
  930. {
  931. _dirPath->assign(path);
  932. }
  933. }
  934. void calculateNamespacePath(const std::string& urlString, std::string& fileString, std::vector<std::string>& namespacePath)
  935. {
  936. // If the url references a specific namespace within the file,
  937. // calculate the full namespace path to the final namespace.
  938. size_t loc = urlString.rfind("#");
  939. if (loc != std::string::npos)
  940. {
  941. fileString = urlString.substr(0, loc);
  942. std::string namespacePathString = urlString.substr(loc + 1);
  943. while ((loc = namespacePathString.find("/")) != std::string::npos)
  944. {
  945. namespacePath.push_back(namespacePathString.substr(0, loc));
  946. namespacePathString = namespacePathString.substr(loc + 1);
  947. }
  948. namespacePath.push_back(namespacePathString);
  949. }
  950. else
  951. {
  952. fileString = urlString;
  953. }
  954. }
  955. Properties* getPropertiesFromNamespacePath(Properties* properties, const std::vector<std::string>& namespacePath)
  956. {
  957. // If the url references a specific namespace within the file,
  958. // return the specified namespace or notify the user if it cannot be found.
  959. if (namespacePath.size() > 0)
  960. {
  961. size_t size = namespacePath.size();
  962. properties->rewind();
  963. Properties* iter = properties->getNextNamespace();
  964. for (size_t i = 0; i < size; )
  965. {
  966. while (true)
  967. {
  968. if (iter == NULL)
  969. {
  970. GP_ERROR("Failed to load properties object from url.");
  971. return NULL;
  972. }
  973. if (strcmp(iter->getId(), namespacePath[i].c_str()) == 0)
  974. {
  975. if (i != size - 1)
  976. {
  977. properties = iter->getNextNamespace();
  978. iter = properties;
  979. }
  980. else
  981. properties = iter;
  982. i++;
  983. break;
  984. }
  985. iter = properties->getNextNamespace();
  986. }
  987. }
  988. return properties;
  989. }
  990. else
  991. return properties;
  992. }
  993. }