AudioFileUtils.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/IO/FileIO.h>
  10. #include <AzCore/IO/Path/Path.h>
  11. #include <AzCore/std/containers/vector.h>
  12. #include <AzCore/std/functional.h>
  13. #include <AzCore/std/string/string.h>
  14. #include <AzCore/std/string/string_view.h>
  15. #include <AzCore/XML/rapidxml.h>
  16. namespace Audio
  17. {
  18. /*!
  19. * FindFilesInPath
  20. */
  21. static AZStd::vector<AZ::IO::FixedMaxPath> FindFilesInPath(const AZStd::string_view folderPath, const char* filter)
  22. {
  23. AZStd::vector<AZ::IO::FixedMaxPath> foundFiles;
  24. AZ::IO::FileIOBase::FindFilesCallbackType findFilesCallback = [&foundFiles](const char* file) -> bool
  25. {
  26. foundFiles.emplace_back(AZ::IO::PathView{ file }.LexicallyNormal());
  27. return true;
  28. };
  29. if (auto fileIO = AZ::IO::FileIOBase::GetInstance();
  30. fileIO != nullptr)
  31. {
  32. AZ::IO::Result result = fileIO->FindFiles(folderPath.data(), filter, findFilesCallback);
  33. if (result == AZ::IO::ResultCode::Success)
  34. {
  35. return AZStd::move(foundFiles);
  36. }
  37. }
  38. return {};
  39. }
  40. /*!
  41. * ScopedXmlLoader
  42. * Create this object on the stack and it will load an xml file contents to an internal buffer
  43. * and allow you to get the first Xml node.
  44. */
  45. class ScopedXmlLoader
  46. {
  47. public:
  48. ScopedXmlLoader(const AZStd::string_view filePath)
  49. {
  50. auto fileIO = AZ::IO::FileIOBase::GetInstance();
  51. AZ_Assert(fileIO != nullptr, "Audio::ScopedXmlLoader - FileIOBase instance is null!");
  52. AZ::IO::HandleType fileHandle = AZ::IO::InvalidHandle;
  53. AZ::IO::Result result = fileIO->Open(filePath.data(), AZ::IO::OpenMode::ModeRead | AZ::IO::OpenMode::ModeText, fileHandle);
  54. if (!result)
  55. {
  56. m_hasError = true;
  57. }
  58. if (!m_hasError)
  59. {
  60. result = fileIO->Size(fileHandle, m_fileSize);
  61. if (!result)
  62. {
  63. m_hasError = true;
  64. }
  65. }
  66. if (!m_hasError)
  67. {
  68. m_fileBuffer.resize_no_construct(m_fileSize + 1);
  69. m_fileBuffer[m_fileSize] = '\0';
  70. result = fileIO->Read(fileHandle, m_fileBuffer.data(), m_fileSize);
  71. if (!result)
  72. {
  73. m_hasError = true;
  74. }
  75. }
  76. if (!m_hasError)
  77. {
  78. if (!m_xmlDoc.parse<AZ::rapidxml::parse_no_data_nodes>(m_fileBuffer.data()))
  79. {
  80. m_hasError = true;
  81. }
  82. }
  83. fileIO->Close(fileHandle);
  84. }
  85. ~ScopedXmlLoader()
  86. {
  87. }
  88. bool HasError() const
  89. {
  90. return m_hasError;
  91. }
  92. const AZ::rapidxml::xml_node<char>* GetRootNode() const
  93. {
  94. if (!HasError())
  95. {
  96. return m_xmlDoc.first_node();
  97. }
  98. return nullptr;
  99. }
  100. private:
  101. AZStd::vector<char> m_fileBuffer;
  102. AZ::rapidxml::xml_document<char> m_xmlDoc;
  103. AZ::u64 m_fileSize = 0;
  104. bool m_hasError = false;
  105. };
  106. } // namespace Audio