164 lines
4.1 KiB
C++
164 lines
4.1 KiB
C++
#include <cstdint>
|
|
#include <fstream>
|
|
#include <vector>
|
|
|
|
#include "usenetsearch/Database.h"
|
|
#include "usenetsearch/UsenetClient.h"
|
|
|
|
#include "usenetsearch/Serialize.h"
|
|
|
|
namespace usenetsearch {
|
|
|
|
SerializableFile& operator<<(
|
|
SerializableFile& out, const std::vector<std::uint64_t>& arr)
|
|
{
|
|
const std::uint64_t size = arr.size();
|
|
out << size;
|
|
for (const auto value: arr)
|
|
{
|
|
out << value;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
SerializableFile& operator>>(
|
|
SerializableFile& in, std::vector<std::uint64_t>& arr)
|
|
{
|
|
std::uint64_t size;
|
|
in >> size;
|
|
for (std::uint64_t n = 0; n != size; ++n)
|
|
{
|
|
std::uint64_t value;
|
|
in >> value;
|
|
arr.emplace_back(value);
|
|
}
|
|
return in;
|
|
}
|
|
|
|
SerializableFile& operator<<(
|
|
SerializableFile& out, const ArticleEntry& obj)
|
|
{
|
|
const char preMagic[1]{24};
|
|
out.write(preMagic, sizeof(preMagic));
|
|
out << obj.searchString;
|
|
out << obj.articleIDs;
|
|
const char postMagic[1]{42};
|
|
out.write(postMagic, sizeof(postMagic));
|
|
return out;
|
|
}
|
|
|
|
SerializableFile& operator>>(SerializableFile& in, ArticleEntry& obj)
|
|
{
|
|
char preMagic[1]{};
|
|
in.read(preMagic, sizeof(preMagic));
|
|
if (preMagic[0] != 24)
|
|
{
|
|
throw SerializeException(EBADMSG, "Bad magic number in entry header.");
|
|
}
|
|
in >> obj.searchString;
|
|
in >> obj.articleIDs;
|
|
char postMagic[1]{};
|
|
in.read(postMagic, sizeof(postMagic));
|
|
if (postMagic[0] != 42)
|
|
{
|
|
throw SerializeException(EBADMSG, "Bad magic number in entry footer.");
|
|
}
|
|
return in;
|
|
}
|
|
|
|
SerializableFile& operator<<(SerializableFile& out, const std::string& str)
|
|
{
|
|
const std::uint64_t size = str.size();
|
|
out.write(reinterpret_cast<const char*>(&size), sizeof(size));
|
|
out.write(reinterpret_cast<const char*>(str.c_str()), size);
|
|
return out;
|
|
}
|
|
|
|
SerializableFile& operator>>(SerializableFile& in, std::string& str)
|
|
{
|
|
std::uint64_t size{0};
|
|
in.read(reinterpret_cast<char*>(&size), sizeof(size));
|
|
char buf[size + 1];
|
|
in.read(buf, size);
|
|
buf[size] = 0;
|
|
str = buf;
|
|
return in;
|
|
}
|
|
|
|
SerializableFile& operator<<(SerializableFile& out, const std::wstring& str)
|
|
{
|
|
const std::uint64_t size = str.size();
|
|
out.write(reinterpret_cast<const char*>(&size), sizeof(size));
|
|
out.write(
|
|
reinterpret_cast<const char*>(str.c_str()),
|
|
size * sizeof(wchar_t)
|
|
);
|
|
return out;
|
|
}
|
|
|
|
SerializableFile& operator>>(SerializableFile& in, std::wstring& str)
|
|
{
|
|
std::uint64_t size{0};
|
|
in.read(reinterpret_cast<char*>(&size), sizeof(size));
|
|
wchar_t buf[size + 1];
|
|
in.read(reinterpret_cast<char*>(buf), size * sizeof(wchar_t));
|
|
buf[size] = 0;
|
|
str = buf;
|
|
return in;
|
|
}
|
|
|
|
SerializableFile& operator<<(SerializableFile& out, const NntpHeader& obj)
|
|
{
|
|
out.write(
|
|
reinterpret_cast<const char*>(&obj.articleID),
|
|
sizeof(obj.articleID)
|
|
);
|
|
out << obj.subject;
|
|
return out;
|
|
}
|
|
|
|
SerializableFile& operator>>(SerializableFile& in, NntpHeader& obj)
|
|
{
|
|
in.read(
|
|
reinterpret_cast<char*>(&obj.articleID),
|
|
sizeof(obj.articleID)
|
|
);
|
|
in >> obj.articleID;
|
|
in >> obj.subject;
|
|
return in;
|
|
}
|
|
|
|
SerializableFile& operator<<(SerializableFile& out, const NntpListEntry& obj)
|
|
{
|
|
out.write(reinterpret_cast<const char*>(&obj.count), sizeof(obj.count));
|
|
out.write(reinterpret_cast<const char*>(&obj.high), sizeof(obj.high));
|
|
out.write(reinterpret_cast<const char*>(&obj.low), sizeof(obj.low));
|
|
out << obj.name;
|
|
out << obj.status;
|
|
return out;
|
|
}
|
|
|
|
SerializableFile& operator>>(SerializableFile& in, NntpListEntry& obj)
|
|
{
|
|
in.read(reinterpret_cast<char*>(&obj.count), sizeof(obj.count));
|
|
in.read(reinterpret_cast<char*>(&obj.high), sizeof(obj.high));
|
|
in.read(reinterpret_cast<char*>(&obj.low), sizeof(obj.low));
|
|
in >> obj.name;
|
|
in >> obj.status;
|
|
return in;
|
|
}
|
|
|
|
SerializableFile& operator<<(SerializableFile& out, const uint64_t& obj)
|
|
{
|
|
out.write(reinterpret_cast<const char*>(&obj), sizeof(obj));
|
|
return out;
|
|
}
|
|
|
|
SerializableFile& operator>>(SerializableFile& in, uint64_t& obj)
|
|
{
|
|
in.read(reinterpret_cast<char*>(&obj), sizeof(obj));
|
|
return in;
|
|
}
|
|
|
|
} // namespace usenetsearch
|