@@ -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