XMLAutoInclude.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <precomp.h>
  2. #include <bfc/wasabi_std.h>
  3. #include "XMLAutoInclude.h"
  4. #include <bfc/file/wildcharsenum.h>
  5. #include <api/util/varmgr.h>
  6. void LoadXmlFile(obj_xml *parser, const wchar_t *filename);
  7. XMLAutoInclude::XMLAutoInclude(obj_xml *_parser, const wchar_t *_path)
  8. {
  9. path=_path;
  10. parser = _parser;
  11. if (parser)
  12. {
  13. parser->xmlreader_registerCallback(L"*\finclude", this);
  14. }
  15. }
  16. XMLAutoInclude::~XMLAutoInclude()
  17. {
  18. if (parser)
  19. {
  20. parser->xmlreader_unregisterCallback(this);
  21. }
  22. }
  23. static const wchar_t *varmgr_translate(const wchar_t *str)
  24. {
  25. static StringW ret;
  26. StringW *s = PublicVarManager::translate_nocontext(str);
  27. if (s)
  28. {
  29. ret.swap(s);
  30. delete s;
  31. return ret.getValueSafe();
  32. }
  33. return str;
  34. }
  35. void XMLAutoInclude::xmlReaderOnStartElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
  36. {
  37. const wchar_t *includeFile = params->getItemValue(L"file");
  38. const wchar_t *trans = /*WASABI_API_WNDMGR->*/varmgr_translate(includeFile);
  39. if (trans)
  40. {
  41. //const char *path = WASABI_API_SKIN->getSkinsPath();
  42. if (!Wasabi::Std::isRootPath(trans))
  43. includeFn=StringPathCombine(path, trans);
  44. else
  45. includeFn=trans;
  46. }
  47. }
  48. void XMLAutoInclude::Include(const wchar_t *filename)
  49. {
  50. if (filename && *filename)
  51. {
  52. parser->xmlreader_interrupt();
  53. StringW oldPath = path;
  54. const wchar_t *file = Wasabi::Std::filename(filename);
  55. int fnlen = wcslen(file);
  56. path = filename;
  57. path.trunc(-fnlen);
  58. LoadXmlFile(parser, filename);
  59. path = oldPath;
  60. parser->xmlreader_resume();
  61. }
  62. }
  63. void XMLAutoInclude::xmlReaderOnEndElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag)
  64. {
  65. if (!includeFn.isempty())
  66. {
  67. WildcharsEnumerator e(includeFn);
  68. if (e.getNumFiles() > 0) // if we're including multiple files
  69. {
  70. for (int i = 0;i < e.getNumFiles();i++)
  71. {
  72. Include(e.enumFile(i));
  73. }
  74. }
  75. else
  76. Include(includeFn);
  77. }
  78. }