#include "Main.h"
#include "Feeds.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"<", fp); break;
case '>': fputws(L">", fp); break;
case '&': fputws(L"&", fp); break;
case '\"': fputws(L""", fp); break;
case '\'': fputws(L"'", 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 BuildXMLString(FILE *fp, const RSS::Item &item)
{
fputws(L"- \r\n", fp);
InstaTag(fp, L"title", item.itemName);
if (item.url && item.url[0])
{
fputws(L"\r\n", fp);
}
InstaTag(fp, L"guid", item.guid);
InstaTag(fp, L"description", item.description);
InstaTag(fp, L"link", item.link);
if (item.duration && item.duration[0])
{
InstaTag(fp, L"itunes:duration", item.duration);
}
if (item.publishDate)
{
wchar_t date_str[128] = {0};
MakeRFCDateString(item.publishDate, date_str, 128);
InstaTag(fp, L"pubDate", date_str);
}
if (item.sourceUrl && item.sourceUrl[0])
{
fputws(L"\r\n", fp);
}
fputws(L"
\r\n", fp);
}
static void BuildXMLString(FILE *fp, Channel &channel)
{
fputws(L"\r\n", fp);
// write update settings for this channel
fputws(L"\r\n", fp);
if (!channel.useDefaultUpdate)
{
fputws(L"\r\n", fp);
}
InstaTag(fp, L"winamp:url", channel.url);
InstaTag(fp, L"title", channel.title);
InstaTag(fp, L"link", channel.link);
InstaTag(fp, L"description", channel.description);
if (channel.ttl)
InstaTag(fp, L"ttl", channel.ttl);
fputws(L"\r\n", fp);
Channel::ItemList::iterator itemItr;
for (itemItr = channel.items.begin();itemItr != channel.items.end(); itemItr++)
BuildXMLString(fp, *itemItr);
fputws(L"\r\n", fp);
}
void SaveChannels(const wchar_t *fileName, ChannelList &channels)
{
FILE *fp = _wfopen(fileName, L"wb");
if (fp)
{
ChannelList::iterator itr;
wchar_t BOM = 0xFEFF;
fwrite(&BOM, sizeof(BOM), 1, fp);
fputws(L"\r\n", fp);
fputws(L"\r\n", fp);
for (itr = channels.begin();itr != channels.end();itr++)
BuildXMLString(fp, *itr);
fputws(L"", fp);
fclose(fp);
}
}
static void BuildXMLPreferences(FILE *fp)
{
fputws(L"\r\n", fp);
fputws(L"\r\n", fp);
fputws(L"\r\n", fp);
fputws(L"\r\n", fp);
fputws(L"\r\n", fp);
}
static void BuildXMLDownloads(FILE *fp, DownloadList &downloads)
{
fputws(L"", fp);
AutoLock lock (downloads);
DownloadList::const_iterator itr;
for ( itr = downloads.begin(); itr != downloads.end(); itr++ )
{
fputws(L"", fp);
InstaTag(fp, L"channel", itr->channel);
InstaTag(fp, L"item", itr->item);
InstaTag(fp, L"url", itr->url);
InstaTag(fp, L"path", itr->path);
InstaTag(fp, L"publishDate", itr->publishDate);
fputws(L"\r\n", fp);
}
fputws(L"", fp);
}
void SaveSettings(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"\r\n", fp);
fputws(L"\r\n", fp);
BuildXMLPreferences(fp);
BuildXMLDownloads(fp, downloads);
fputws(L"", fp);
fclose(fp);
}
}