rds2cpp
Read and write RDS/RDA files in C++
Loading...
Searching...
No Matches
write_rda.hpp
Go to the documentation of this file.
1#ifndef RDS2CPP_WRITE_RDA_HPP
2#define RDS2CPP_WRITE_RDA_HPP
3
4#include <vector>
5#include <cstddef>
6
7#include "byteme/byteme.hpp"
9
10#include "RdaFile.hpp"
11#include "RObject.hpp"
12#include "utils_write.hpp"
13#include "write_object.hpp"
14#include "SharedWriteInfo.hpp"
15
22namespace rds2cpp {
23
31 bool parallel = false;
32
38};
39
49template<class Writer>
50void write_rda(const RdaFile& info, Writer& writer, const WriteRdaOptions& options) {
51 std::unique_ptr<byteme::BufferedWriter<unsigned char> > bufwriter;
52 if (options.parallel) {
53 bufwriter.reset(new byteme::ParallelBufferedWriter<unsigned char, Writer*>(&writer, options.buffer_size));
54 } else {
55 bufwriter.reset(new byteme::SerialBufferedWriter<unsigned char, Writer*>(&writer, options.buffer_size));
56 }
57
58 bufwriter->write("RDX3\nX\n");
59 inject_integer<std::int32_t>(info.format_version, *bufwriter);
60 write_version(info.writer_version, *bufwriter);
61 write_version(info.reader_version, *bufwriter);
62
63 const std::string encoding = string_encoding_to_name(info.encoding);
64 inject_integer<std::int32_t>(sanisizer::cast<std::int32_t>(encoding.size()), *bufwriter);
65 bufwriter->write(encoding);
66
67 SharedWriteInfo shared(info.symbols, info.environments, info.external_pointers);
68
69 // We need to write it out as a tagged pairlist but we can't move the unique_ptrs into a temporary PairList while keeping things const.
70 // So we'll just do it manually, by duplicating the logic in write_pairlist().
71 for (const auto& entry : info.objects) {
72 Header details;
73 details[0] = 0;
74 details[1] = 0;
75 details[2] = 0x4; // i.e., has tag.
76 details[3] = static_cast<unsigned char>(SEXPType::LIST);
77 bufwriter->write(details.data(), details.size());
78 write_symbol(&(entry.name), *bufwriter, shared);
79 write_object(entry.value.get(), *bufwriter, shared);
80 }
81
82 inject_header(SEXPType::NILVALUE_, *bufwriter);
83}
84
92inline void write_rda(const RdaFile& info, const char* path, const WriteRdaOptions& options) {
93 byteme::GzipFileWriter writer(path, {});
94 write_rda(info, writer, options);
95}
96
104inline void write_rda(const RdaFile& info, std::string path, const WriteRdaOptions& options) {
105 write_rda(info, path.c_str(), options);
106}
107
108
109}
110
111#endif
Representations of unserialized R objects.
Information about an RDA file.
Parse an RDS file in C++.
Definition StringEncoding.hpp:12
void write_rda(const RdaFile &info, Writer &writer, const WriteRdaOptions &options)
Definition write_rda.hpp:50
constexpr Dest_ cap(Value_ x)
constexpr Dest_ cast(Value_ x)
Contents of the parsed RDA file.
Definition RdaFile.hpp:47
std::vector< RdaObject > objects
Definition RdaFile.hpp:71
std::vector< Symbol > symbols
Definition RdaFile.hpp:83
StringEncoding encoding
Definition RdaFile.hpp:66
std::vector< ExternalPointer > external_pointers
Definition RdaFile.hpp:89
std::vector< Environment > environments
Definition RdaFile.hpp:77
Version writer_version
Definition RdaFile.hpp:56
Version reader_version
Definition RdaFile.hpp:61
std::int32_t format_version
Definition RdaFile.hpp:51
Options for write_rda().
Definition write_rda.hpp:27
std::size_t buffer_size
Definition write_rda.hpp:37
bool parallel
Definition write_rda.hpp:31