1
0

XMLWriter.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "Main.h"
  2. #include "RFCDate.h"
  3. #include "Downloaded.h"
  4. #include "../nu/AutoLock.h"
  5. #include "Defaults.h"
  6. #include "../Agave/Language/api_language.h"
  7. using namespace Nullsoft::Utility;
  8. static void XMLWriteString(FILE *fp, const wchar_t *str)
  9. {
  10. for (const wchar_t *itr = str; *itr; itr=CharNextW(itr))
  11. {
  12. switch (*itr)
  13. {
  14. case '<': fputws(L"&lt;", fp); break;
  15. case '>': fputws(L"&gt;", fp); break;
  16. case '&': fputws(L"&amp;", fp); break;
  17. case '\"': fputws(L"&quot;", fp); break;
  18. case '\'': fputws(L"&apos;", fp); break;
  19. default: fputwc(*itr, fp); break;
  20. }
  21. }
  22. }
  23. static void InstaProp(FILE *fp, const wchar_t *property, const wchar_t *value)
  24. {
  25. fwprintf(fp, L" %s=\"", property);
  26. XMLWriteString(fp, value);
  27. fputws(L"\"", fp);
  28. }
  29. static void InstaProp(FILE *fp, const wchar_t *property, int value)
  30. {
  31. fwprintf(fp, L" %s=\"%i\"", property, value);
  32. }
  33. static void InstaPropI64(FILE *fp, const wchar_t *property, int64_t value)
  34. {
  35. fwprintf(fp, L" %s=\"%I64d\"", property, value);
  36. }
  37. static void InstaPropTime(FILE *fp, const wchar_t *property, __time64_t value)
  38. {
  39. fwprintf(fp, L" %s=\"%I64u\"", property, value);
  40. }
  41. static void InstaProp(FILE *fp, const wchar_t *property, float value)
  42. {
  43. _fwprintf_l(fp, L" %s=\"%3.3f\"", WASABI_API_LNG->Get_C_NumericLocale(), property, value);
  44. }
  45. static void InstaProp(FILE *fp, const wchar_t *property, bool value)
  46. {
  47. fwprintf(fp, L" %s=\"", property);
  48. if (value)
  49. fputws(L"true\"", fp);
  50. else
  51. fputws(L"false\"", fp);
  52. }
  53. static void InstaTag(FILE *fp, const wchar_t *tag, const wchar_t *content)
  54. {
  55. if (content && !((INT_PTR)content < 65536) && *content)
  56. {
  57. fwprintf(fp, L"<%s>", tag);
  58. XMLWriteString(fp, content);
  59. fwprintf(fp, L"</%s>\r\n", tag);
  60. }
  61. }
  62. static void InstaTag(FILE *fp, const wchar_t *tag, unsigned int uint)
  63. {
  64. fwprintf(fp, L"<%s>%u</%s>", tag, uint, tag);
  65. }
  66. static void InstaTag(FILE *fp, const wchar_t *tag, __time64_t uint)
  67. {
  68. fwprintf(fp, L"<%s>%I64u</%s>", tag, uint, tag);
  69. }
  70. static void BuildXMLDownloads(FILE *fp, DownloadList &downloads)
  71. {
  72. fputws(L"<downloads", fp);
  73. InstaProp(fp, L"downloadsTitleWidth", downloadsTitleWidth);
  74. InstaProp(fp, L"downloadsProgressWidth", downloadsProgressWidth);
  75. InstaProp(fp, L"downloadsDateWidth", downloadsDateWidth);
  76. InstaProp(fp, L"downloadsSourceWidth", downloadsSourceWidth);
  77. InstaProp(fp, L"downloadsPathWidth", downloadsPathWidth);
  78. InstaProp(fp, L"downloadsItemSort", downloadsItemSort);
  79. InstaProp(fp, L"downloadsSortAscending", downloadsSortAscending);
  80. fputws(L">\r\n", fp);
  81. AutoLock lock (downloads);
  82. DownloadList::const_iterator itr;
  83. for ( itr = downloads.begin(); itr != downloads.end(); itr++ )
  84. {
  85. fputws(L"<download>", fp);
  86. InstaTag(fp, L"source", itr->source);
  87. InstaTag(fp, L"title", itr->title);
  88. InstaTag(fp, L"url", itr->url);
  89. InstaTag(fp, L"path", itr->path);
  90. InstaTag(fp, L"size", (unsigned int)itr->totalSize);
  91. InstaTag(fp, L"downloadDate", itr->downloadDate);
  92. wchar_t status[64] = {0};
  93. _itow(itr->downloadStatus, status, 10);
  94. InstaTag(fp, L"downloadStatus", status);
  95. fputws(L"</download>\r\n", fp);
  96. }
  97. fputws(L"</downloads>", fp);
  98. }
  99. void SaveDownloads(const wchar_t *fileName, DownloadList &downloads)
  100. {
  101. FILE *fp = _wfopen(fileName, L"wb");
  102. if (fp)
  103. {
  104. wchar_t BOM = 0xFEFF;
  105. fwrite(&BOM, sizeof(BOM), 1, fp);
  106. fputws(L"<?xml version=\"1.0\" encoding=\"UTF-16\"?>\r\n", fp);
  107. BuildXMLDownloads(fp, downloads);
  108. fclose(fp);
  109. }
  110. }