Skip to content

Commit cd54aed

Browse files
committed
Migrate xml2_output.cpp
1 parent 66f4c60 commit cd54aed

File tree

5 files changed

+59
-83
lines changed

5 files changed

+59
-83
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ SystemRequirements: libxml2: libxml2-dev (deb), libxml2-devel (rpm)
4242
Collate:
4343
'S4.R'
4444
'as_list.R'
45-
'cpp11.R'
4645
'xml_parse.R'
4746
'as_xml_document.R'
4847
'classes.R'
48+
'cpp11.R'
4949
'format.R'
5050
'import-standalone-obj-type.R'
5151
'import-standalone-purrr.R'

src/connection.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ SEXP read_bin(SEXP con, size_t bytes) {
2020
}
2121

2222
// Wrapper around R's write_bin function
23-
SEXP write_bin(SEXP data, SEXP con) {
24-
SEXP e;
25-
PROTECT(e = Rf_lang3(Rf_install("writeBin"), data, con));
26-
SEXP res = Rf_eval(e, R_GlobalEnv);
27-
UNPROTECT(1);
28-
return res;
23+
cpp11::sexp write_bin(cpp11::sexp data, cpp11::sexp con) {
24+
cpp11::function write_bin = cpp11::package("base")["writeBin"];
25+
return write_bin(data, con);
2926
}
3027

3128
// Read data from a connection in chunks and then combine into a single

src/connection.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#include <cstring>
1111

1212
SEXP read_bin(SEXP con, size_t bytes = 64 * 1024);
13-
SEXP write_bin(SEXP data, SEXP con);
14-
13+
cpp11::sexp write_bin(cpp11::sexp data, cpp11::sexp con);
1514

1615
inline size_t R_WriteConnection(SEXP con, void* buf, size_t n) {
1716
SEXP payload = PROTECT(Rf_allocVector(RAWSXP, n));

src/cpp11.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -349,52 +349,52 @@ extern "C" SEXP _xml2_node_new_dtd(SEXP doc_sxp, SEXP name_sxp, SEXP eid_sxp, SE
349349
END_CPP11
350350
}
351351
// xml2_output.cpp
352-
cpp11::sexp xml_save_options_();
352+
cpp11::writable::integers xml_save_options_();
353353
extern "C" SEXP _xml2_xml_save_options_() {
354354
BEGIN_CPP11
355355
return cpp11::as_sexp(xml_save_options_());
356356
END_CPP11
357357
}
358358
// xml2_output.cpp
359-
cpp11::sexp doc_write_file(SEXP doc_sxp, SEXP path_sxp, SEXP encoding_sxp, SEXP options_sxp);
359+
cpp11::sexp doc_write_file(cpp11::sexp doc_sxp, cpp11::strings path_sxp, cpp11::strings encoding_sxp, cpp11::integers options_sxp);
360360
extern "C" SEXP _xml2_doc_write_file(SEXP doc_sxp, SEXP path_sxp, SEXP encoding_sxp, SEXP options_sxp) {
361361
BEGIN_CPP11
362-
return cpp11::as_sexp(doc_write_file(cpp11::as_cpp<cpp11::decay_t<SEXP>>(doc_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(path_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(encoding_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(options_sxp)));
362+
return cpp11::as_sexp(doc_write_file(cpp11::as_cpp<cpp11::decay_t<cpp11::sexp>>(doc_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::strings>>(path_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::strings>>(encoding_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::integers>>(options_sxp)));
363363
END_CPP11
364364
}
365365
// xml2_output.cpp
366-
cpp11::sexp doc_write_connection(SEXP doc_sxp, SEXP connection, SEXP encoding_sxp, SEXP options_sxp);
366+
cpp11::sexp doc_write_connection(cpp11::sexp doc_sxp, cpp11::sexp connection, cpp11::strings encoding_sxp, cpp11::integers options_sxp);
367367
extern "C" SEXP _xml2_doc_write_connection(SEXP doc_sxp, SEXP connection, SEXP encoding_sxp, SEXP options_sxp) {
368368
BEGIN_CPP11
369-
return cpp11::as_sexp(doc_write_connection(cpp11::as_cpp<cpp11::decay_t<SEXP>>(doc_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(connection), cpp11::as_cpp<cpp11::decay_t<SEXP>>(encoding_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(options_sxp)));
369+
return cpp11::as_sexp(doc_write_connection(cpp11::as_cpp<cpp11::decay_t<cpp11::sexp>>(doc_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::sexp>>(connection), cpp11::as_cpp<cpp11::decay_t<cpp11::strings>>(encoding_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::integers>>(options_sxp)));
370370
END_CPP11
371371
}
372372
// xml2_output.cpp
373-
cpp11::sexp doc_write_character(SEXP doc_sxp, SEXP encoding_sxp, SEXP options_sxp);
373+
cpp11::writable::strings doc_write_character(cpp11::sexp doc_sxp, cpp11::strings encoding_sxp, cpp11::integers options_sxp);
374374
extern "C" SEXP _xml2_doc_write_character(SEXP doc_sxp, SEXP encoding_sxp, SEXP options_sxp) {
375375
BEGIN_CPP11
376-
return cpp11::as_sexp(doc_write_character(cpp11::as_cpp<cpp11::decay_t<SEXP>>(doc_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(encoding_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(options_sxp)));
376+
return cpp11::as_sexp(doc_write_character(cpp11::as_cpp<cpp11::decay_t<cpp11::sexp>>(doc_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::strings>>(encoding_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::integers>>(options_sxp)));
377377
END_CPP11
378378
}
379379
// xml2_output.cpp
380-
cpp11::sexp node_write_file(SEXP node_sxp, SEXP path_sxp, SEXP encoding_sxp, SEXP options_sxp);
380+
cpp11::sexp node_write_file(cpp11::sexp node_sxp, cpp11::strings path_sxp, cpp11::strings encoding_sxp, cpp11::integers options_sxp);
381381
extern "C" SEXP _xml2_node_write_file(SEXP node_sxp, SEXP path_sxp, SEXP encoding_sxp, SEXP options_sxp) {
382382
BEGIN_CPP11
383-
return cpp11::as_sexp(node_write_file(cpp11::as_cpp<cpp11::decay_t<SEXP>>(node_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(path_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(encoding_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(options_sxp)));
383+
return cpp11::as_sexp(node_write_file(cpp11::as_cpp<cpp11::decay_t<cpp11::sexp>>(node_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::strings>>(path_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::strings>>(encoding_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::integers>>(options_sxp)));
384384
END_CPP11
385385
}
386386
// xml2_output.cpp
387-
cpp11::sexp node_write_connection(SEXP node_sxp, SEXP connection, SEXP encoding_sxp, SEXP options_sxp);
387+
cpp11::sexp node_write_connection(cpp11::sexp node_sxp, cpp11::sexp connection, cpp11::strings encoding_sxp, cpp11::integers options_sxp);
388388
extern "C" SEXP _xml2_node_write_connection(SEXP node_sxp, SEXP connection, SEXP encoding_sxp, SEXP options_sxp) {
389389
BEGIN_CPP11
390-
return cpp11::as_sexp(node_write_connection(cpp11::as_cpp<cpp11::decay_t<SEXP>>(node_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(connection), cpp11::as_cpp<cpp11::decay_t<SEXP>>(encoding_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(options_sxp)));
390+
return cpp11::as_sexp(node_write_connection(cpp11::as_cpp<cpp11::decay_t<cpp11::sexp>>(node_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::sexp>>(connection), cpp11::as_cpp<cpp11::decay_t<cpp11::strings>>(encoding_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::integers>>(options_sxp)));
391391
END_CPP11
392392
}
393393
// xml2_output.cpp
394-
cpp11::sexp node_write_character(SEXP node_sxp, SEXP encoding_sxp, SEXP options_sxp);
394+
cpp11::writable::strings node_write_character(cpp11::sexp node_sxp, cpp11::strings encoding_sxp, cpp11::integers options_sxp);
395395
extern "C" SEXP _xml2_node_write_character(SEXP node_sxp, SEXP encoding_sxp, SEXP options_sxp) {
396396
BEGIN_CPP11
397-
return cpp11::as_sexp(node_write_character(cpp11::as_cpp<cpp11::decay_t<SEXP>>(node_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(encoding_sxp), cpp11::as_cpp<cpp11::decay_t<SEXP>>(options_sxp)));
397+
return cpp11::as_sexp(node_write_character(cpp11::as_cpp<cpp11::decay_t<cpp11::sexp>>(node_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::strings>>(encoding_sxp), cpp11::as_cpp<cpp11::decay_t<cpp11::integers>>(options_sxp)));
398398
END_CPP11
399399
}
400400
// xml2_schema.cpp

src/xml2_output.cpp

Lines changed: 41 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef struct {
4646
} xml_save_def;
4747

4848
[[cpp11::register]]
49-
cpp11::sexp xml_save_options_() {
49+
cpp11::writable::integers xml_save_options_() {
5050

5151
static const xml_save_def entries[] = {
5252
{"format", "Format output", XML_SAVE_FORMAT},
@@ -71,21 +71,18 @@ cpp11::sexp xml_save_options_() {
7171
++n;
7272
}
7373

74-
SEXP names = PROTECT(Rf_allocVector(STRSXP, n));
75-
SEXP descriptions = PROTECT(Rf_allocVector(STRSXP, n));
76-
SEXP values = PROTECT(Rf_allocVector(INTSXP, n));
74+
cpp11::writable::strings names(n);
75+
cpp11::writable::strings descriptions(n);
76+
cpp11::writable::integers values(n);
7777

78-
79-
for (R_xlen_t i = 0;i < n; ++i) {
80-
SET_STRING_ELT(names, i, Rf_mkChar(entries[i].name));
81-
SET_STRING_ELT(descriptions, i, Rf_mkChar(entries[i].description));
82-
INTEGER(values)[i] = entries[i].value;
78+
for (R_xlen_t i = 0; i < n; ++i) {
79+
names[i] = entries[i].name;
80+
descriptions[i] = entries[i].description;
81+
values[i] = entries[i].value;
8382
}
8483

85-
Rf_setAttrib(values, R_NamesSymbol, names);
86-
Rf_setAttrib(values, Rf_install("descriptions"), descriptions);
87-
88-
UNPROTECT(3);
84+
values.names() = names;
85+
values.attr("descriptions") = descriptions;
8986

9087
return values;
9188
}
@@ -94,62 +91,56 @@ int xml_write_callback(SEXP con, const char * buffer, int len) {
9491
size_t write_size;
9592

9693
if ((write_size = R_WriteConnection(con, (void *) buffer, len)) != static_cast<size_t>(len)) {
97-
Rf_error("write failed, expected %l, got %l", len, write_size);
94+
cpp11::stop("write failed, expected %l, got %l", len, write_size);
9895
}
9996
return write_size;
10097
}
10198

10299
[[cpp11::register]]
103-
cpp11::sexp doc_write_file(SEXP doc_sxp, SEXP path_sxp, SEXP encoding_sxp, SEXP options_sxp) {
104-
105-
BEGIN_CPP
100+
cpp11::sexp doc_write_file(cpp11::sexp doc_sxp, cpp11::strings path_sxp, cpp11::strings encoding_sxp, cpp11::integers options_sxp) {
106101
XPtrDoc doc(doc_sxp);
107-
const char* path = CHAR(STRING_ELT(path_sxp, 0));
108-
const char* encoding = CHAR(STRING_ELT(encoding_sxp, 0));
109-
int options = INTEGER(options_sxp)[0];
102+
const char* path = cpp11::as_cpp<const char*>(path_sxp);
103+
const char* encoding = cpp11::as_cpp<const char*>(encoding_sxp);
104+
int options = options_sxp[0];
110105

111106
xmlSaveCtxtPtr savectx = xmlSaveToFilename(
112107
path,
113108
encoding,
114109
options);
115110
xmlSaveDoc(savectx, doc.checked_get());
116111
if (xmlSaveClose(savectx) == -1) {
117-
Rf_error("Error closing file");
112+
cpp11::stop("Error closing file");
118113
}
119114

120115
return R_NilValue;
121-
END_CPP
122116
}
123117

124118
[[cpp11::register]]
125-
cpp11::sexp doc_write_connection(SEXP doc_sxp, SEXP connection, SEXP encoding_sxp, SEXP options_sxp) {
126-
BEGIN_CPP
119+
cpp11::sexp doc_write_connection(cpp11::sexp doc_sxp, cpp11::sexp connection, cpp11::strings encoding_sxp, cpp11::integers options_sxp) {
127120
XPtrDoc doc(doc_sxp);
128-
const char* encoding = CHAR(STRING_ELT(encoding_sxp, 0));
129-
int options = INTEGER(options_sxp)[0];
121+
const char* encoding = cpp11::as_cpp<const char*>(encoding_sxp);
122+
int options = options_sxp[0];
130123

131124
xmlSaveCtxtPtr savectx = xmlSaveToIO(
132125
reinterpret_cast<xmlOutputWriteCallback>(xml_write_callback),
133126
NULL,
134-
con,
127+
connection,
135128
encoding,
136129
options);
137130

138131
xmlSaveDoc(savectx, doc.checked_get());
139132
if (xmlSaveClose(savectx) == -1) {
140-
Rf_error("Error closing connection");
133+
cpp11::stop("Error closing connection");
141134
}
142135

143136
return R_NilValue;
144-
END_CPP
145137
}
146138

147139
[[cpp11::register]]
148-
cpp11::sexp doc_write_character(SEXP doc_sxp, SEXP encoding_sxp, SEXP options_sxp) {
149-
BEGIN_CPP
140+
cpp11::writable::strings doc_write_character(cpp11::sexp doc_sxp, cpp11::strings encoding_sxp, cpp11::integers options_sxp) {
150141
XPtrDoc doc(doc_sxp);
151-
const char* encoding = CHAR(STRING_ELT(encoding_sxp, 0));
152-
int options = INTEGER(options_sxp)[0];
142+
const char* encoding = cpp11::as_cpp<const char*>(encoding_sxp);
143+
int options = options_sxp[0];
153144

154145
xmlBufferPtr buffer = xmlBufferCreate();
155146

@@ -161,69 +152,60 @@ cpp11::sexp doc_write_character(SEXP doc_sxp, SEXP encoding_sxp, SEXP options_sx
161152
xmlSaveDoc(savectx, doc.checked_get());
162153
if (xmlSaveClose(savectx) == -1) {
163154
xmlFree(buffer);
164-
Rf_error("Error writing to buffer");
155+
cpp11::stop("Error writing to buffer");
165156
}
166-
SEXP out = PROTECT(Rf_allocVector(STRSXP, 1));
167-
SET_STRING_ELT(out, 0, Xml2String(buffer->content).asRString());
157+
cpp11::writable::strings out(Xml2String(buffer->content).asRString());
168158

169159
xmlFree(buffer);
170160

171-
UNPROTECT(1);
172-
173161
return out;
174-
END_CPP
175162
}
176163

177164
[[cpp11::register]]
178-
cpp11::sexp node_write_file(SEXP node_sxp, SEXP path_sxp, SEXP encoding_sxp, SEXP options_sxp) {
179-
BEGIN_CPP
165+
cpp11::sexp node_write_file(cpp11::sexp node_sxp, cpp11::strings path_sxp, cpp11::strings encoding_sxp, cpp11::integers options_sxp) {
180166
XPtrNode node(node_sxp);
181-
const char* path = CHAR(STRING_ELT(path_sxp, 0));
182-
const char* encoding = CHAR(STRING_ELT(encoding_sxp, 0));
183-
int options = INTEGER(options_sxp)[0];
167+
const char* path = cpp11::as_cpp<const char*>(path_sxp);
168+
const char* encoding = cpp11::as_cpp<const char*>(encoding_sxp);
169+
int options = options_sxp[0];
184170

185171
xmlSaveCtxtPtr savectx = xmlSaveToFilename(
186172
path,
187173
encoding,
188174
options);
189175
xmlSaveTree(savectx, node.checked_get());
190176
if (xmlSaveClose(savectx) == -1) {
191-
Rf_error("Error closing file");
177+
cpp11::stop("Error closing file");
192178
}
193179

194180
return R_NilValue;
195-
END_CPP
196181
}
197182

198183
[[cpp11::register]]
199-
cpp11::sexp node_write_connection(SEXP node_sxp, SEXP connection, SEXP encoding_sxp, SEXP options_sxp) {
200-
BEGIN_CPP
184+
cpp11::sexp node_write_connection(cpp11::sexp node_sxp, cpp11::sexp connection, cpp11::strings encoding_sxp, cpp11::integers options_sxp) {
201185
XPtrNode node(node_sxp);
202-
const char* encoding = CHAR(STRING_ELT(encoding_sxp, 0));
203-
int options = INTEGER(options_sxp)[0];
186+
const char* encoding = cpp11::as_cpp<const char*>(encoding_sxp);
187+
int options = options_sxp[0];
204188

205189
xmlSaveCtxtPtr savectx = xmlSaveToIO(
206190
(xmlOutputWriteCallback)xml_write_callback,
207191
NULL,
208-
con,
192+
connection,
209193
encoding,
210194
options);
211195

212196
xmlSaveTree(savectx, node.checked_get());
213197
if (xmlSaveClose(savectx) == -1) {
214-
Rf_error("Error closing connection");
198+
cpp11::stop("Error closing connection");
215199
}
216200

217201
return R_NilValue;
218-
END_CPP
219202
}
220203

221204
[[cpp11::register]]
222-
cpp11::sexp node_write_character(SEXP node_sxp, SEXP encoding_sxp, SEXP options_sxp) {
223-
BEGIN_CPP
205+
cpp11::writable::strings node_write_character(cpp11::sexp node_sxp, cpp11::strings encoding_sxp, cpp11::integers options_sxp) {
224206
XPtrNode node(node_sxp);
225-
const char* encoding = CHAR(STRING_ELT(encoding_sxp, 0));
226-
int options = INTEGER(options_sxp)[0];
207+
const char* encoding = cpp11::as_cpp<const char*>(encoding_sxp);
208+
int options = options_sxp[0];
227209

228210
xmlBufferPtr buffer = xmlBufferCreate();
229211

@@ -235,12 +217,10 @@ cpp11::sexp node_write_character(SEXP node_sxp, SEXP encoding_sxp, SEXP options_
235217
xmlSaveTree(savectx, node.checked_get());
236218
if (xmlSaveClose(savectx) == -1) {
237219
xmlFree(buffer);
238-
Rf_error("Error writing to buffer");
220+
cpp11::stop("Error writing to buffer");
239221
}
240-
SEXP out = PROTECT(Rf_ScalarString(Xml2String(buffer->content).asRString()));
222+
cpp11::writable::strings out(Xml2String(buffer->content).asRString());
241223
xmlFree(buffer);
242224

243-
UNPROTECT(1);
244225
return out;
245-
END_CPP
246226
}

0 commit comments

Comments
 (0)