1
0

info.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "info.h"
  2. #include "read.h"
  3. nsavi::Info::Info()
  4. {
  5. }
  6. nsavi::Info::~Info()
  7. {
  8. for (auto itr = this->begin(); itr != this->end(); itr++)
  9. {
  10. free((void*)itr->second);
  11. }
  12. }
  13. int nsavi::Info::Read(nsavi::avi_reader* reader, uint32_t data_len)
  14. {
  15. while (data_len)
  16. {
  17. riff_chunk chunk;
  18. uint32_t bytes_read = 0;
  19. nsavi::read_riff_chunk(reader, &chunk, &bytes_read);
  20. data_len -= bytes_read;
  21. size_t malloc_size = chunk.size + 1;
  22. if (malloc_size == 0)
  23. return READ_INVALID_DATA;
  24. char* str = (char*)calloc(malloc_size, sizeof(char));
  25. if (!str)
  26. return READ_OUT_OF_MEMORY;
  27. reader->Read(str, chunk.size, &bytes_read);
  28. str[chunk.size] = 0;
  29. data_len -= bytes_read;
  30. Set(chunk.id, str);
  31. if (chunk.size & 1)
  32. {
  33. reader->Skip(1);
  34. data_len--;
  35. }
  36. }
  37. return 0;
  38. }
  39. void nsavi::Info::Set(uint32_t chunk_id, const char* data)
  40. {
  41. auto it = this->find(chunk_id);
  42. if (this->end() == it)
  43. {
  44. this->insert({ chunk_id, data });
  45. }
  46. else
  47. {
  48. it->second = data;
  49. }
  50. }
  51. const char* nsavi::Info::GetMetadata(uint32_t id)
  52. {
  53. InfoMap::iterator itr = InfoMap::find(id);
  54. if (itr != InfoMap::end())
  55. {
  56. return itr->second;
  57. }
  58. return 0;
  59. }