aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_downloads/XMLWriter.cpp
blob: fdc8f2ca4eddefbed4172751b03707aecd74296f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "Main.h"
#include "RFCDate.h"
#include "Downloaded.h"
#include "../nu/AutoLock.h"
#include "Defaults.h"
#include "../Agave/Language/api_language.h"

using namespace Nullsoft::Utility;

static void XMLWriteString(FILE *fp, const wchar_t *str)
{
	for (const wchar_t *itr = str; *itr; itr=CharNextW(itr))
	{
		switch (*itr)
		{
			case '<':	fputws(L"&lt;", fp);	break;
			case '>':	fputws(L"&gt;", fp);	break;
			case '&':	fputws(L"&amp;", fp);	break;
			case '\"':	fputws(L"&quot;", fp);	break;
			case '\'':	fputws(L"&apos;", fp);	break;
			default:	fputwc(*itr, fp);		break;
		}
	}
}

static void InstaProp(FILE *fp, const wchar_t *property, const wchar_t *value)
{
	fwprintf(fp, L" %s=\"", property);
	XMLWriteString(fp, value);
	fputws(L"\"", fp);
}

static void InstaProp(FILE *fp, const wchar_t *property, int value)
{
	fwprintf(fp, L" %s=\"%i\"", property, value);
}

static void InstaPropI64(FILE *fp, const wchar_t *property, int64_t value)
{
	fwprintf(fp, L" %s=\"%I64d\"", property, value);
}

static void InstaPropTime(FILE *fp, const wchar_t *property, __time64_t value)
{
	fwprintf(fp, L" %s=\"%I64u\"", property, value);
}

static void InstaProp(FILE *fp, const wchar_t *property, float value)
{
	_fwprintf_l(fp, L" %s=\"%3.3f\"", WASABI_API_LNG->Get_C_NumericLocale(), property, value);
}

static void InstaProp(FILE *fp, const wchar_t *property, bool value)
{
	fwprintf(fp, L" %s=\"", property);
	if (value)
		fputws(L"true\"", fp);
	else
		fputws(L"false\"", fp);
}

static void InstaTag(FILE *fp, const wchar_t *tag, const wchar_t *content)
{
	if (content && !((INT_PTR)content < 65536) && *content)
	{
		fwprintf(fp, L"<%s>", tag);
		XMLWriteString(fp, content);
		fwprintf(fp, L"</%s>\r\n", tag);
	}
}

static void InstaTag(FILE *fp, const wchar_t *tag, unsigned int uint)
{
	fwprintf(fp, L"<%s>%u</%s>", tag, uint, tag);
}

static void InstaTag(FILE *fp, const wchar_t *tag, __time64_t uint)
{
	fwprintf(fp, L"<%s>%I64u</%s>", tag, uint, tag);
}

static void BuildXMLDownloads(FILE *fp, DownloadList &downloads)
{
	fputws(L"<downloads", fp);
	InstaProp(fp, L"downloadsTitleWidth",    downloadsTitleWidth);
	InstaProp(fp, L"downloadsProgressWidth", downloadsProgressWidth);
	InstaProp(fp, L"downloadsDateWidth",     downloadsDateWidth);
	InstaProp(fp, L"downloadsSourceWidth",   downloadsSourceWidth);
	InstaProp(fp, L"downloadsPathWidth",     downloadsPathWidth);
	InstaProp(fp, L"downloadsItemSort",      downloadsItemSort);
	InstaProp(fp, L"downloadsSortAscending", downloadsSortAscending);
	fputws(L">\r\n", fp);

	AutoLock lock (downloads);
	DownloadList::const_iterator itr;
	for ( itr = downloads.begin(); itr != downloads.end(); itr++ )
	{
		fputws(L"<download>", fp);
		InstaTag(fp, L"source",       itr->source);
		InstaTag(fp, L"title",        itr->title);
		InstaTag(fp, L"url",          itr->url);
		InstaTag(fp, L"path",         itr->path);
		InstaTag(fp, L"size",         (unsigned int)itr->totalSize);
		InstaTag(fp, L"downloadDate", itr->downloadDate);
		wchar_t status[64] = {0};
		_itow(itr->downloadStatus, status, 10);
		InstaTag(fp, L"downloadStatus", status);
		fputws(L"</download>\r\n", fp);
	}

	fputws(L"</downloads>", fp);
}

void SaveDownloads(const wchar_t *fileName, DownloadList &downloads)
{
	FILE *fp = _wfopen(fileName, L"wb");
	if (fp)
	{
		wchar_t BOM = 0xFEFF;
		fwrite(&BOM, sizeof(BOM), 1, fp);
		fputws(L"<?xml version=\"1.0\" encoding=\"UTF-16\"?>\r\n", fp);
		BuildXMLDownloads(fp, downloads);
		fclose(fp);
	}
}