Properties.cpp 32 KB

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