1
0

bm.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /** (c) Nullsoft, Inc. C O N F I D E N T I A L
  2. ** Filename:
  3. ** Project:
  4. ** Description:
  5. ** Author:
  6. ** Created:
  7. **/
  8. #include "main.h"
  9. #include "../Plugins/Library/ml_bookmarks/bookmark.h"
  10. #include <malloc.h>
  11. #include "api.h"
  12. #include "../nu/AutoCharFn.h"
  13. #include "../nu/AutoChar.h"
  14. #include <strsafe.h>
  15. void Bookmark_additem(wchar_t *fn, wchar_t *ft)
  16. {
  17. if ((!fn || fn && !*fn) || (!ft || ft && !*ft)) return;
  18. BookmarkWriter bookmarks;
  19. bookmarks.Open(BOOKMARKFILE);
  20. bookmarks.Write(AutoCharFn(fn), AutoCharFn(ft));
  21. bookmarks.Close();
  22. bookmarks.Open(BOOKMARKFILE8);
  23. bookmarks.Write(AutoChar(fn, CP_UTF8), AutoChar(ft, CP_UTF8));
  24. bookmarks.Close();
  25. size_t bufSize = wcslen(fn)+wcslen(ft)+3;
  26. wchar_t *buf=(wchar_t*)_malloca(bufSize*sizeof(wchar_t)), *buf2pass = buf;
  27. if (buf)
  28. {
  29. StringCchCopyExW(buf, bufSize, fn, &buf, &bufSize, 0);
  30. buf++; bufSize--;
  31. StringCchCopyW(buf, bufSize, ft);
  32. SendMessageW(hMainWindow,WM_WA_IPC,(WPARAM)buf2pass,IPC_ADDBOOKMARKW);
  33. AGAVE_API_STATS->IncrementStat(api_stats::BOOKMARK_COUNT);
  34. _freea(buf);
  35. }
  36. }
  37. void Bookmark_AddCommandline(wchar_t *lpszCmdParam)
  38. {
  39. wchar_t *p = lpszCmdParam;
  40. if (p)
  41. {
  42. if (*p == L'\"') { p++; p[lstrlenW(p) - 1] = 0; }
  43. Bookmark_additem(p, PlayList_gettitle(p, 1));
  44. }
  45. }
  46. void WriteEscaped(FILE *fp, const char *str)
  47. {
  48. // TODO: for speed optimization,
  49. // we should wait until we hit a special character
  50. // and write out everything else so before it,
  51. // like how ASX loader does it
  52. while (str && *str)
  53. {
  54. switch(*str)
  55. {
  56. case '&':
  57. fputs("&amp;", fp);
  58. break;
  59. case '>':
  60. fputs("&gt;", fp);
  61. break;
  62. case '<':
  63. fputs("&lt;", fp);
  64. break;
  65. case '\'':
  66. fputs("&apos;", fp);
  67. break;
  68. case '\"':
  69. fputs("&quot;", fp);
  70. break;
  71. default:
  72. fputc(*str, fp);
  73. break;
  74. }
  75. // write out the whole character
  76. char *next = CharNextA(str);
  77. while (++str != next)
  78. fputc(*str, fp);
  79. }
  80. }
  81. void Bookmark_WriteAsXML(const wchar_t *filename, int max)
  82. {
  83. FILE *destFp=_wfopen(filename,L"wb");
  84. FILE *fp=_wfopen(BOOKMARKFILE8,L"rt");
  85. if (fp && destFp)
  86. {
  87. //fprintf(destFp, "<?xml version=\"1.0\" encoding=\"windows-%u\"?>", GetACP());
  88. fprintf(destFp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  89. fputs("<bookmarks>\n", destFp);
  90. int x=0;
  91. for (;;)
  92. {
  93. char ft[4096] = {0}, fn[4096] = {0};
  94. fgets(fn,4096,fp);
  95. if (feof(fp)) break;
  96. fgets(ft,4096,fp);
  97. if (feof(fp)) break;
  98. if (ft[0] && fn[0])
  99. {
  100. if (fn[strlen(fn)-1]=='\n') fn[strlen(fn)-1]=0;
  101. if (ft[strlen(ft)-1]=='\n') ft[strlen(ft)-1]=0;
  102. if (ft[0] && fn[0])
  103. {
  104. fputs("<bookmark file=\"", destFp);
  105. WriteEscaped(destFp, fn);
  106. fputs("\" title=\"", destFp);
  107. WriteEscaped(destFp, ft);
  108. fputs("\"/>\n", destFp);
  109. x++;
  110. }
  111. }
  112. if (x == max)
  113. break;
  114. }
  115. fputs("</bookmarks>", destFp);
  116. }
  117. if (fp)
  118. fclose(fp);
  119. if (destFp)
  120. fclose(destFp);
  121. }