1
0

itunesxmlwrite.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //------------------------------------------------------------------------
  2. //
  3. // iTunes XML Library Writer
  4. // Copyright © 2003-2014 Winamp SA
  5. //
  6. //------------------------------------------------------------------------
  7. #include <windows.h>
  8. #include <commdlg.h>
  9. #include <api/xml/xmlwrite.h>
  10. #include <bfc/string/url.h>
  11. #include "itunesxmlwrite.h"
  12. #include "../plist/types.h"
  13. #include "api__ml_impex.h"
  14. #include "resource.h"
  15. //------------------------------------------------------------------------
  16. iTunesXmlWrite::iTunesXmlWrite() {
  17. }
  18. //------------------------------------------------------------------------
  19. iTunesXmlWrite::~iTunesXmlWrite() {
  20. }
  21. //------------------------------------------------------------------------
  22. int iTunesXmlWrite::pickFile(HWND hwndDlg, const wchar_t *title)
  23. {
  24. wchar_t oldCurPath[MAX_PATH] = {0};
  25. GetCurrentDirectoryW(MAX_PATH, oldCurPath);
  26. OPENFILENAME l={sizeof(l),};
  27. wchar_t *temp;
  28. const int len=256*1024-128;
  29. temp = (wchar_t *)GlobalAlloc(GPTR,len*sizeof(*temp));
  30. l.hwndOwner = hwndDlg;
  31. //l.lpstrFilter = L"iTunes XML Library\0*.xml\0\0"; // IDS_ITUNES_XML_LIBRARY
  32. extern wchar_t* GetFilterListString(void);
  33. l.lpstrFilter = GetFilterListString();//L"iTunes XML Library\0*.xml\0\0"; // IDS_ITUNES_XML_LIBRARY
  34. l.lpstrFile = temp;
  35. l.nMaxFile = len-1;
  36. l.lpstrTitle = title ? title : WASABI_API_LNGSTRINGW(IDS_EXPORT_DATABASE);
  37. l.lpstrDefExt = L"xml";
  38. l.lpstrInitialDir = WASABI_API_APP->path_getWorkingPath();
  39. l.Flags = OFN_HIDEREADONLY|OFN_EXPLORER|OFN_OVERWRITEPROMPT;
  40. if (GetSaveFileName(&l))
  41. {
  42. wchar_t newCurPath[MAX_PATH] = {0};
  43. GetCurrentDirectoryW(MAX_PATH, newCurPath);
  44. WASABI_API_APP->path_setWorkingPath(newCurPath);
  45. SetCurrentDirectoryW(oldCurPath);
  46. file = temp;
  47. return 1;
  48. }
  49. SetCurrentDirectoryW(oldCurPath);
  50. return 0;
  51. }
  52. //------------------------------------------------------------------------
  53. void iTunesXmlWrite::saveXml(plistKey *rootkey) {
  54. if (file.isempty()) return;
  55. XMLWrite w(file, L"plist version=\"1.0\"", L"plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"", 1);
  56. writeData(&w, rootkey->getData());
  57. }
  58. //------------------------------------------------------------------------
  59. void iTunesXmlWrite::writeData(XMLWrite *writer, plistData *data)
  60. {
  61. switch (data->getType())
  62. {
  63. case PLISTDATA_KEY:
  64. {
  65. plistKey *key = (plistKey *)data;
  66. writer->writeAttrib(data->getTypeString(), data->getString(), key->getData()->getType() == PLISTDATA_DICT || key->getData()->getType() == PLISTDATA_ARRAY || key->getData()->getType() == PLISTDATA_RAW);
  67. writeData(writer, key->getData());
  68. break;
  69. }
  70. case PLISTDATA_DICT:
  71. {
  72. plistDict *dict = (plistDict *)data;
  73. writer->pushCategory(data->getTypeString());
  74. for (int i=0;i<dict->getNumKeys();i++)
  75. {
  76. writeData(writer, dict->enumKey(i));
  77. }
  78. writer->popCategory();
  79. break;
  80. }
  81. case PLISTDATA_ARRAY:
  82. {
  83. plistArray *array = (plistArray *)data;
  84. writer->pushCategory(data->getTypeString());
  85. for (int i=0;i<array->getNumItems();i++) {
  86. writeData(writer, array->enumItem(i));
  87. }
  88. writer->popCategory();
  89. break;
  90. }
  91. case PLISTDATA_INTEGER:
  92. case PLISTDATA_DATE:
  93. case PLISTDATA_RAW:
  94. {
  95. const wchar_t *str = data->getString();
  96. if (str && *str)
  97. {
  98. writer->writeAttrib(data->getTypeString(), str, 1, 0);
  99. }
  100. }
  101. break;
  102. case PLISTDATA_STRING:
  103. {
  104. const wchar_t *str = data->getString();
  105. if (str && *str)
  106. {
  107. // not pretty but it'll strip out control characters
  108. // in the 0 - 31 range that will cause import issues
  109. wchar_t *temp = 0;
  110. int len = (int)wcslen(str) + 1;
  111. temp = (wchar_t*)calloc(len, sizeof(wchar_t));
  112. wchar_t *ptr = temp;
  113. while(str && *str)
  114. {
  115. int chr = *str;
  116. if (chr >= 0 && chr <= 31)
  117. {
  118. if(chr == 9 || chr == 10 || chr == 13)
  119. {
  120. *ptr = *str;
  121. ptr++;
  122. }
  123. }
  124. else
  125. {
  126. *ptr = *str;
  127. ptr++;
  128. }
  129. str = CharNextW(str);
  130. }
  131. *ptr=0;
  132. writer->writeAttrib(data->getTypeString(), (temp ? temp : str), 1, 0);
  133. if (temp) free(temp);
  134. }
  135. }
  136. break;
  137. case PLISTDATA_BOOLEAN:
  138. {
  139. //plistBoolean *booldata = (plistBoolean *)data;
  140. writer->writeAttribEmpty(data->getString(), 1, 0);
  141. }
  142. break;
  143. }
  144. }