tamlModuleIdUpdateVisitor.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TAML_MODULE_ID_UPDATE_VISITOR_H_
  23. #define _TAML_MODULE_ID_UPDATE_VISITOR_H_
  24. #ifndef _TAML_XMLPARSER_H_
  25. #include "persistence//taml/xml/tamlXmlParser.h"
  26. #endif
  27. #ifndef _STRINGTABLE_H_
  28. #include "string/stringTable.h"
  29. #endif
  30. #ifndef _ASSET_FIELD_TYPES_H_
  31. #include "assets/assetFieldTypes.h"
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. class TamlModuleIdUpdateVisitor : public TamlXmlVisitor
  35. {
  36. protected:
  37. virtual bool visit( TiXmlElement* pXmlElement, TamlXmlParser& xmlParser )
  38. {
  39. // Iterate attributes.
  40. for ( TiXmlAttribute* pAttribute = pXmlElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->Next() )
  41. {
  42. // Fetch attribute value.
  43. const char* pAttributeValue = pAttribute->Value();
  44. // Fetch value length.
  45. const U32 valueLenth = dStrlen(pAttributeValue);
  46. char newAttributeValueBuffer[1024];
  47. // Is this an expando?
  48. if ( *pAttributeValue == '^' )
  49. {
  50. // Yes, so skip if it's not the correct length.
  51. if ( valueLenth < mModuleIdLengthFrom+1 )
  52. continue;
  53. // Is this the module Id?
  54. if ( dStrnicmp( pAttributeValue+1, mModuleIdFrom, mModuleIdLengthFrom ) == 0 )
  55. {
  56. // Yes, so format a new value.
  57. dSprintf( newAttributeValueBuffer, sizeof(newAttributeValueBuffer), "^%s%s",
  58. mModuleIdTo, pAttributeValue+1+mModuleIdLengthFrom );
  59. // Assign new value.
  60. pAttribute->SetValue( newAttributeValueBuffer );
  61. }
  62. // Skip to next attribute.
  63. continue;
  64. }
  65. // Does the field start with the module Id?
  66. if ( dStrnicmp( pAttributeValue, mModuleIdFrom, mModuleIdLengthFrom ) == 0 )
  67. {
  68. // Yes, so format a new value.
  69. dSprintf( newAttributeValueBuffer, sizeof(newAttributeValueBuffer), "%s%s",
  70. mModuleIdTo, pAttributeValue+mModuleIdLengthFrom );
  71. // Assign new value.
  72. pAttribute->SetValue( newAttributeValueBuffer );
  73. }
  74. }
  75. return true;
  76. }
  77. virtual bool visit( TiXmlAttribute* pAttribute, TamlXmlParser& xmlParser ) { return true; }
  78. public:
  79. TamlModuleIdUpdateVisitor() :
  80. mModuleIdFrom( StringTable->EmptyString ),
  81. mModuleIdTo( StringTable->EmptyString ),
  82. mModuleIdLengthFrom( 0 ),
  83. mModuleIdLengthTo( 0 )
  84. {}
  85. virtual ~TamlModuleIdUpdateVisitor() {}
  86. bool parse( const char* pFilename )
  87. {
  88. TamlXmlParser parser;
  89. return parser.parse( pFilename, *this, true );
  90. }
  91. void setModuleIdFrom( const char* pModuleIdFrom )
  92. {
  93. // Sanity!
  94. AssertFatal( pModuleIdFrom != NULL, "Module Id from cannot be NULL." );
  95. // Set module Id.
  96. mModuleIdFrom = StringTable->insert( pModuleIdFrom );
  97. mModuleIdLengthFrom = dStrlen(mModuleIdFrom);
  98. }
  99. StringTableEntry getModuleIdFrom( void ) const { return mModuleIdFrom; }
  100. void setModuleIdTo( const char* pModuleIdTo )
  101. {
  102. // Sanity!
  103. AssertFatal( pModuleIdTo != NULL, "Module Id to cannot be NULL." );
  104. // Set module Id.
  105. mModuleIdTo = StringTable->insert( pModuleIdTo );
  106. mModuleIdLengthTo = dStrlen(mModuleIdTo);
  107. }
  108. const char* getModuleIdTo( void ) const { return mModuleIdTo; }
  109. private:
  110. StringTableEntry mModuleIdFrom;
  111. StringTableEntry mModuleIdTo;
  112. U32 mModuleIdLengthFrom;
  113. U32 mModuleIdLengthTo;
  114. };
  115. #endif // _TAML_MODULE_ID_UPDATE_VISITOR_H_