Properties.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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, bool recurse) const
  523. {
  524. GP_ASSERT(id);
  525. for (std::vector<Properties*>::const_iterator it = _namespaces.begin(); it < _namespaces.end(); ++it)
  526. {
  527. Properties* p = *it;
  528. if (strcmp(searchNames ? p->_namespace.c_str() : p->_id.c_str(), id) == 0)
  529. return p;
  530. if (recurse)
  531. {
  532. // Search recursively.
  533. p = p->getNamespace(id, searchNames, true);
  534. if (p)
  535. return p;
  536. }
  537. }
  538. return NULL;
  539. }
  540. const char* Properties::getNamespace() const
  541. {
  542. return _namespace.c_str();
  543. }
  544. const char* Properties::getId() const
  545. {
  546. return _id.c_str();
  547. }
  548. bool Properties::exists(const char* name) const
  549. {
  550. GP_ASSERT(name);
  551. return _properties.find(name) != _properties.end();
  552. }
  553. static const bool isStringNumeric(const char* str)
  554. {
  555. GP_ASSERT(str);
  556. // The first character may be '-'
  557. if (*str == '-')
  558. str++;
  559. // The first character after the sign must be a digit
  560. if (!isdigit(*str))
  561. return false;
  562. str++;
  563. // All remaining characters must be digits, with a single decimal (.) permitted
  564. unsigned int decimalCount = 0;
  565. while (*str)
  566. {
  567. if (!isdigit(*str))
  568. {
  569. if (*str == '.' && decimalCount == 0)
  570. {
  571. // Max of 1 decimal allowed
  572. decimalCount++;
  573. }
  574. else
  575. {
  576. return false;
  577. }
  578. }
  579. str++;
  580. }
  581. return true;
  582. }
  583. Properties::Type Properties::getType(const char* name) const
  584. {
  585. const char* value = getString(name);
  586. if (!value)
  587. {
  588. return Properties::NONE;
  589. }
  590. // Parse the value to determine the format
  591. unsigned int commaCount = 0;
  592. char* valuePtr = const_cast<char*>(value);
  593. while (valuePtr = strchr(valuePtr, ','))
  594. {
  595. valuePtr++;
  596. commaCount++;
  597. }
  598. switch (commaCount)
  599. {
  600. case 0:
  601. return isStringNumeric(value) ? Properties::NUMBER : Properties::STRING;
  602. case 1:
  603. return Properties::VECTOR2;
  604. case 2:
  605. return Properties::VECTOR3;
  606. case 3:
  607. return Properties::VECTOR4;
  608. case 15:
  609. return Properties::MATRIX;
  610. default:
  611. return Properties::STRING;
  612. }
  613. }
  614. const char* Properties::getString(const char* name, const char* defaultValue) const
  615. {
  616. if (name)
  617. {
  618. std::map<std::string, std::string>::const_iterator itr = _properties.find(name);
  619. if (itr != _properties.end())
  620. {
  621. return itr->second.c_str();
  622. }
  623. }
  624. else
  625. {
  626. if (_propertiesItr != _properties.end())
  627. {
  628. return _propertiesItr->second.c_str();
  629. }
  630. }
  631. return defaultValue;
  632. }
  633. bool Properties::getBool(const char* name, bool defaultValue) const
  634. {
  635. const char* valueString = getString(name);
  636. if (valueString)
  637. {
  638. return (strcmp(valueString, "true") == 0);
  639. }
  640. return defaultValue;
  641. }
  642. int Properties::getInt(const char* name) const
  643. {
  644. const char* valueString = getString(name);
  645. if (valueString)
  646. {
  647. int value;
  648. int scanned;
  649. scanned = sscanf(valueString, "%d", &value);
  650. if (scanned != 1)
  651. {
  652. GP_ERROR("Error attempting to parse property '%s' as an integer.", name);
  653. return 0;
  654. }
  655. return value;
  656. }
  657. return 0;
  658. }
  659. float Properties::getFloat(const char* name) const
  660. {
  661. const char* valueString = getString(name);
  662. if (valueString)
  663. {
  664. float value;
  665. int scanned;
  666. scanned = sscanf(valueString, "%f", &value);
  667. if (scanned != 1)
  668. {
  669. GP_ERROR("Error attempting to parse property '%s' as a float.", name);
  670. return 0.0f;
  671. }
  672. return value;
  673. }
  674. return 0.0f;
  675. }
  676. long Properties::getLong(const char* name) const
  677. {
  678. const char* valueString = getString(name);
  679. if (valueString)
  680. {
  681. long value;
  682. int scanned;
  683. scanned = sscanf(valueString, "%ld", &value);
  684. if (scanned != 1)
  685. {
  686. GP_ERROR("Error attempting to parse property '%s' as a long integer.", name);
  687. return 0L;
  688. }
  689. return value;
  690. }
  691. return 0L;
  692. }
  693. bool Properties::getMatrix(const char* name, Matrix* out) const
  694. {
  695. GP_ASSERT(out);
  696. const char* valueString = getString(name);
  697. if (valueString)
  698. {
  699. float m[16];
  700. int scanned;
  701. scanned = sscanf(valueString, "%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f",
  702. &m[0], &m[1], &m[2], &m[3], &m[4], &m[5], &m[6], &m[7],
  703. &m[8], &m[9], &m[10], &m[11], &m[12], &m[13], &m[14], &m[15]);
  704. if (scanned != 16)
  705. {
  706. GP_ERROR("Error attempting to parse property '%s' as a matrix.", name);
  707. out->setIdentity();
  708. return false;
  709. }
  710. out->set(m);
  711. return true;
  712. }
  713. out->setIdentity();
  714. return false;
  715. }
  716. bool Properties::getVector2(const char* name, Vector2* out) const
  717. {
  718. GP_ASSERT(out);
  719. const char* valueString = getString(name);
  720. if (valueString)
  721. {
  722. float x, y;
  723. int scanned;
  724. scanned = sscanf(valueString, "%f,%f", &x, &y);
  725. if (scanned != 2)
  726. {
  727. GP_ERROR("Error attempting to parse property '%s' as a two-dimensional vector.", name);
  728. out->set(0.0f, 0.0f);
  729. return false;
  730. }
  731. out->set(x, y);
  732. return true;
  733. }
  734. out->set(0.0f, 0.0f);
  735. return false;
  736. }
  737. bool Properties::getVector3(const char* name, Vector3* out) const
  738. {
  739. GP_ASSERT(out);
  740. const char* valueString = getString(name);
  741. if (valueString)
  742. {
  743. float x, y, z;
  744. int scanned;
  745. scanned = sscanf(valueString, "%f,%f,%f", &x, &y, &z);
  746. if (scanned != 3)
  747. {
  748. GP_ERROR("Error attempting to parse property '%s' as a three-dimensional vector.", name);
  749. out->set(0.0f, 0.0f, 0.0f);
  750. return false;
  751. }
  752. out->set(x, y, z);
  753. return true;
  754. }
  755. out->set(0.0f, 0.0f, 0.0f);
  756. return false;
  757. }
  758. bool Properties::getVector4(const char* name, Vector4* out) const
  759. {
  760. GP_ASSERT(out);
  761. const char* valueString = getString(name);
  762. if (valueString)
  763. {
  764. float x, y, z, w;
  765. int scanned;
  766. scanned = sscanf(valueString, "%f,%f,%f,%f", &x, &y, &z, &w);
  767. if (scanned != 4)
  768. {
  769. GP_ERROR("Error attempting to parse property '%s' as a four-dimensional vector.", name);
  770. out->set(0.0f, 0.0f, 0.0f, 0.0f);
  771. return false;
  772. }
  773. out->set(x, y, z, w);
  774. return true;
  775. }
  776. out->set(0.0f, 0.0f, 0.0f, 0.0f);
  777. return false;
  778. }
  779. bool Properties::getQuaternionFromAxisAngle(const char* name, Quaternion* out) const
  780. {
  781. GP_ASSERT(out);
  782. const char* valueString = getString(name);
  783. if (valueString)
  784. {
  785. float x, y, z, theta;
  786. int scanned;
  787. scanned = sscanf(valueString, "%f,%f,%f,%f", &x, &y, &z, &theta);
  788. if (scanned != 4)
  789. {
  790. GP_ERROR("Error attempting to parse property '%s' as an axis-angle rotation.", name);
  791. out->set(0.0f, 0.0f, 0.0f, 1.0f);
  792. return false;
  793. }
  794. out->set(Vector3(x, y, z), MATH_DEG_TO_RAD(theta));
  795. return true;
  796. }
  797. out->set(0.0f, 0.0f, 0.0f, 1.0f);
  798. return false;
  799. }
  800. bool Properties::getColor(const char* name, Vector3* out) const
  801. {
  802. GP_ASSERT(out);
  803. const char* valueString = getString(name);
  804. if (valueString)
  805. {
  806. if (strlen(valueString) != 7 ||
  807. valueString[0] != '#')
  808. {
  809. // Not a color string.
  810. GP_ERROR("Error attempting to parse property '%s' as an RGB color (not specified as a color string).", name);
  811. out->set(0.0f, 0.0f, 0.0f);
  812. return false;
  813. }
  814. // Read the string into an int as hex.
  815. unsigned int color;
  816. if (sscanf(valueString+1, "%x", &color) != 1)
  817. {
  818. GP_ERROR("Error attempting to parse property '%s' as an RGB color.", name);
  819. out->set(0.0f, 0.0f, 0.0f);
  820. return false;
  821. }
  822. out->set(Vector3::fromColor(color));
  823. return true;
  824. }
  825. out->set(0.0f, 0.0f, 0.0f);
  826. return false;
  827. }
  828. bool Properties::getColor(const char* name, Vector4* out) const
  829. {
  830. GP_ASSERT(out);
  831. const char* valueString = getString(name);
  832. if (valueString)
  833. {
  834. if (strlen(valueString) != 9 ||
  835. valueString[0] != '#')
  836. {
  837. // Not a color string.
  838. GP_ERROR("Error attempting to parse property '%s' as an RGBA color (not specified as a color string).", name);
  839. out->set(0.0f, 0.0f, 0.0f, 0.0f);
  840. return false;
  841. }
  842. // Read the string into an int as hex.
  843. unsigned int color;
  844. if (sscanf(valueString+1, "%x", &color) != 1)
  845. {
  846. GP_ERROR("Error attempting to parse property '%s' as an RGBA color.", name);
  847. out->set(0.0f, 0.0f, 0.0f, 0.0f);
  848. return false;
  849. }
  850. out->set(Vector4::fromColor(color));
  851. return true;
  852. }
  853. out->set(0.0f, 0.0f, 0.0f, 0.0f);
  854. return false;
  855. }
  856. bool Properties::getPath(const char* name, std::string* path) const
  857. {
  858. GP_ASSERT(name && path);
  859. const char* valueString = getString(name);
  860. if (valueString)
  861. {
  862. if (FileSystem::fileExists(valueString))
  863. {
  864. path->assign(valueString);
  865. return true;
  866. }
  867. else
  868. {
  869. const Properties* prop = this;
  870. while (prop != NULL)
  871. {
  872. // Search for the file path relative to the bundle file
  873. const std::string* dirPath = prop->_dirPath;
  874. if (dirPath != NULL && !dirPath->empty())
  875. {
  876. std::string relativePath = *dirPath;
  877. relativePath.append(valueString);
  878. if (FileSystem::fileExists(relativePath.c_str()))
  879. {
  880. path->assign(relativePath);
  881. return true;
  882. }
  883. }
  884. prop = prop->_parent;
  885. }
  886. }
  887. }
  888. return false;
  889. }
  890. Properties* Properties::clone()
  891. {
  892. Properties* p = new Properties();
  893. p->_namespace = _namespace;
  894. p->_id = _id;
  895. p->_parentID = _parentID;
  896. p->_properties = _properties;
  897. p->_propertiesItr = p->_properties.end();
  898. p->setDirectoryPath(_dirPath);
  899. for (size_t i = 0, count = _namespaces.size(); i < count; i++)
  900. {
  901. GP_ASSERT(_namespaces[i]);
  902. Properties* child = _namespaces[i]->clone();
  903. p->_namespaces.push_back(child);
  904. child->_parent = p;
  905. }
  906. p->_namespacesItr = p->_namespaces.end();
  907. return p;
  908. }
  909. void Properties::setDirectoryPath(const std::string* path)
  910. {
  911. if (path)
  912. {
  913. setDirectoryPath(*path);
  914. }
  915. else
  916. {
  917. SAFE_DELETE(_dirPath);
  918. }
  919. }
  920. void Properties::setDirectoryPath(const std::string& path)
  921. {
  922. if (_dirPath == NULL)
  923. {
  924. _dirPath = new std::string(path);
  925. }
  926. else
  927. {
  928. _dirPath->assign(path);
  929. }
  930. }
  931. void calculateNamespacePath(const std::string& urlString, std::string& fileString, std::vector<std::string>& namespacePath)
  932. {
  933. // If the url references a specific namespace within the file,
  934. // calculate the full namespace path to the final namespace.
  935. size_t loc = urlString.rfind("#");
  936. if (loc != std::string::npos)
  937. {
  938. fileString = urlString.substr(0, loc);
  939. std::string namespacePathString = urlString.substr(loc + 1);
  940. while ((loc = namespacePathString.find("/")) != std::string::npos)
  941. {
  942. namespacePath.push_back(namespacePathString.substr(0, loc));
  943. namespacePathString = namespacePathString.substr(loc + 1);
  944. }
  945. namespacePath.push_back(namespacePathString);
  946. }
  947. else
  948. {
  949. fileString = urlString;
  950. }
  951. }
  952. Properties* getPropertiesFromNamespacePath(Properties* properties, const std::vector<std::string>& namespacePath)
  953. {
  954. // If the url references a specific namespace within the file,
  955. // return the specified namespace or notify the user if it cannot be found.
  956. if (namespacePath.size() > 0)
  957. {
  958. size_t size = namespacePath.size();
  959. properties->rewind();
  960. Properties* iter = properties->getNextNamespace();
  961. for (size_t i = 0; i < size; )
  962. {
  963. while (true)
  964. {
  965. if (iter == NULL)
  966. {
  967. GP_ERROR("Failed to load properties object from url.");
  968. return NULL;
  969. }
  970. if (strcmp(iter->getId(), namespacePath[i].c_str()) == 0)
  971. {
  972. if (i != size - 1)
  973. {
  974. properties = iter->getNextNamespace();
  975. iter = properties;
  976. }
  977. else
  978. properties = iter;
  979. i++;
  980. break;
  981. }
  982. iter = properties->getNextNamespace();
  983. }
  984. }
  985. return properties;
  986. }
  987. else
  988. return properties;
  989. }
  990. }