1
0

loader.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //------------------------------------------------------------------------
  2. //
  3. // iTunes XML Library Reader
  4. // Copyright (C) 2003-2011 Nullsoft, Inc.
  5. //
  6. //------------------------------------------------------------------------
  7. #include "loader.h"
  8. #include <windows.h>
  9. #include <commdlg.h>
  10. #include <bfc/string/stringdict.h>
  11. #define ATTRIB_TRUE 256
  12. #define ATTRIB_FALSE 257
  13. //------------------------------------------------------------------------
  14. // xml tags
  15. //------------------------------------------------------------------------
  16. BEGIN_STRINGDICTIONARY(_itunestypes)
  17. SDI(L"key", PLISTDATA_KEY);
  18. SDI(L"dict", PLISTDATA_DICT);
  19. SDI(L"integer", PLISTDATA_INTEGER);
  20. SDI(L"string", PLISTDATA_STRING);
  21. SDI(L"date", PLISTDATA_DATE);
  22. SDI(L"array", PLISTDATA_ARRAY);
  23. SDI(L"data", PLISTDATA_RAW);
  24. SDI(L"true", ATTRIB_TRUE);
  25. SDI(L"false", ATTRIB_FALSE);
  26. END_STRINGDICTIONARY(_itunestypes, itunestypes)
  27. //------------------------------------------------------------------------
  28. plistLoader::plistLoader() : plistKey(L"root")
  29. {
  30. m_context.push(this);
  31. }
  32. //------------------------------------------------------------------------
  33. plistLoader::~plistLoader()
  34. {
  35. }
  36. //------------------------------------------------------------------------
  37. // element opens (or singleton), push new key/dictionary/array, handle value singletons (true/false), defer job to closing tag for other data types
  38. //------------------------------------------------------------------------
  39. void plistLoader::xmlReaderOnStartElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params) {
  40. m_cdata = 0;
  41. int a = itunestypes.getId(xmltag);
  42. switch (a) {
  43. case PLISTDATA_KEY:
  44. m_cdata = L"";
  45. m_context.push(new plistKey());
  46. m_dict.top()->addKey((plistKey *)m_context.top());
  47. break;
  48. case PLISTDATA_DICT:
  49. {
  50. m_dict.push(new plistDict());
  51. plistData *contextTop = m_context.top();
  52. contextTop->setData(static_cast<plistDict *>(m_dict.top()));
  53. }
  54. break;
  55. case PLISTDATA_ARRAY:
  56. {
  57. plistArray *new_array = new plistArray;
  58. plistData *contextTop = m_context.top();
  59. contextTop->setData(new_array);
  60. m_context.push(new_array);
  61. m_dict.push(new_array);
  62. break;
  63. }
  64. case PLISTDATA_STRING:
  65. break;
  66. case PLISTDATA_INTEGER:
  67. break;
  68. case PLISTDATA_DATE:
  69. break;
  70. case PLISTDATA_RAW:
  71. break;
  72. case PLISTDATA_REAL:
  73. break;
  74. case ATTRIB_TRUE: {
  75. m_context.top()->setData(new plistBoolean(true));
  76. if (m_context.top()->getType() == PLISTDATA_KEY) m_context.pop();
  77. break;
  78. }
  79. case ATTRIB_FALSE: {
  80. m_context.top()->setData(new plistBoolean(false));
  81. if (m_context.top()->getType() == PLISTDATA_KEY) m_context.pop();
  82. break;
  83. }
  84. }
  85. }
  86. //------------------------------------------------------------------------
  87. // element closes, set data for last key/array, pop key/array/dictionary from stack
  88. //------------------------------------------------------------------------
  89. void plistLoader::xmlReaderOnEndElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag) {
  90. int a = itunestypes.getId(xmltag);
  91. switch (a) {
  92. case PLISTDATA_KEY:
  93. ASSERT(m_context.top()->getType() == PLISTDATA_KEY);
  94. ((plistKey*)m_context.top())->setName(m_cdata);
  95. break;
  96. case PLISTDATA_DICT:
  97. if (m_context.top()->getType() == PLISTDATA_KEY) m_context.pop();
  98. m_dict.pop();
  99. break;
  100. case PLISTDATA_ARRAY: {
  101. /*if (m_context.top()->getType() == PLISTDATA_KEY)*/ m_context.pop();
  102. //ASSERT(m_context.top()->getType() == PLISTDATA_ARRAY);
  103. //plistArray *a = (plistArray *)m_context.top();
  104. //m_context.pop();
  105. //m_context.top()->setData(a);
  106. m_dict.pop();
  107. break;
  108. }
  109. case PLISTDATA_STRING: {
  110. plistString *s = new plistString(m_cdata);
  111. m_context.top()->setData(s);
  112. if (m_context.top()->getType() == PLISTDATA_KEY) m_context.pop();
  113. break;
  114. }
  115. case PLISTDATA_INTEGER: {
  116. plistInteger *i = new plistInteger();
  117. i->setString(m_cdata);
  118. m_context.top()->setData(i);
  119. if (m_context.top()->getType() == PLISTDATA_KEY) m_context.pop();
  120. break;
  121. }
  122. case PLISTDATA_REAL: {
  123. plistReal *r = new plistReal();
  124. r->setString(m_cdata);
  125. m_context.top()->setData(r);
  126. if (m_context.top()->getType() == PLISTDATA_KEY) m_context.pop();
  127. break;
  128. }
  129. case PLISTDATA_DATE: {
  130. plistDate *d = new plistDate();
  131. d->setString(m_cdata);
  132. m_context.top()->setData(d);
  133. if (m_context.top()->getType() == PLISTDATA_KEY) m_context.pop();
  134. break;
  135. }
  136. case PLISTDATA_RAW: {
  137. plistRaw *r = new plistRaw();
  138. r->setString(m_cdata);
  139. m_context.top()->setData(r);
  140. if (m_context.top()->getType() == PLISTDATA_KEY) m_context.pop();
  141. break;
  142. }
  143. }
  144. m_cdata = 0;
  145. }
  146. //------------------------------------------------------------------------
  147. // record c_data
  148. //------------------------------------------------------------------------
  149. void plistLoader::xmlReaderOnCharacterDataCallback(const wchar_t *xmlpath, const wchar_t *xmltag, const wchar_t *str) {
  150. m_cdata += str;
  151. }
  152. #define CBCLASS plistLoader
  153. START_DISPATCH;
  154. VCB(ONSTARTELEMENT, xmlReaderOnStartElementCallback)
  155. VCB(ONCHARDATA, xmlReaderOnCharacterDataCallback)
  156. VCB(ONENDELEMENT, xmlReaderOnEndElementCallback)
  157. END_DISPATCH;
  158. #undef CBCLASS