1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
#include "cpp11/list.hpp"
#include "cpp11/sexp.hpp"
#include "cpp11/strings.hpp"
#include "connection.h"
#include "grisu3.h"
#include <array>
#include <fstream>
#include <sstream>
enum quote_escape_t { DOUBLE = 1, BACKSLASH = 2, NONE = 3 };
void stream_delim(
const cpp11::sexp& connection,
const cpp11::sexp& x,
int i,
char delim,
const std::string& na,
quote_escape_t escape);
void stream_delim_row(
const cpp11::sexp& connection,
const cpp11::list& x,
int i,
char delim,
const std::string& na,
quote_escape_t escape,
const char* eol) {
int p = Rf_length(x);
for (int j = 0; j < p - 1; ++j) {
stream_delim(connection, x.at(j), i, delim, na, escape);
write_bytes(connection, &delim, 1);
}
stream_delim(connection, x.at(p - 1), i, delim, na, escape);
write_bytes(connection, eol, strlen(eol));
}
bool needs_quote(const char* string, char delim, const std::string& na) {
if (string == na) {
return true;
}
for (const char* cur = string; *cur != '\0'; ++cur) {
if (*cur == '\n' || *cur == '\r' || *cur == '"' || *cur == delim) {
return true;
}
}
return false;
}
void stream_delim(
const cpp11::sexp& connection,
const char* string,
char delim,
const std::string& na,
quote_escape_t escape) {
bool quotes = needs_quote(string, delim, na);
if (quotes) {
write_bytes(connection, "\"", 1);
}
for (const char* cur = string; *cur != '\0'; ++cur) {
switch (*cur) {
case '"':
switch (escape) {
case DOUBLE:
write_bytes(connection, "\"\"", 2);
break;
case BACKSLASH:
write_bytes(connection, "\\\"", 2);
break;
case NONE:
write_bytes(connection, "\"", 1);
break;
}
break;
default:
write_bytes(connection, cur, 1);
}
}
if (quotes) {
write_bytes(connection, "\"", 1);
}
}
void validate_col_type(SEXP x, const std::string& name) {
switch (TYPEOF(x)) {
case LGLSXP:
case INTSXP:
case REALSXP:
case STRSXP:
break;
default:
cpp11::stop(
"Don't know how to handle vector of type %s in column '%s'.",
Rf_type2char(TYPEOF(x)),
name.c_str());
}
}
void stream_delim(
const cpp11::sexp& connection,
const cpp11::list& df,
char delim,
const std::string& na,
bool col_names,
bool bom,
quote_escape_t escape,
const char* eol) {
int p = Rf_length(df);
if (p == 0) {
return;
}
if (bom) {
write_bytes(connection, "\xEF\xBB\xBF", 3);
}
cpp11::strings names(df.attr("names"));
// Validate column types
for (int j = 0; j < p; ++j) {
validate_col_type(df.at(j), names[j]);
}
if (col_names) {
cpp11::strings names(df.attr("names"));
for (int j = 0; j < p; ++j) {
stream_delim(connection, names, j, delim, na, escape);
if (j != p - 1) {
write_bytes(connection, &delim, 1);
}
}
write_bytes(connection, eol, strlen(eol));
}
cpp11::sexp first_col = df[0];
int n = Rf_length(first_col);
for (int i = 0; i < n; ++i) {
stream_delim_row(connection, df, i, delim, na, escape, eol);
}
}
[[cpp11::register]] void stream_delim_(
const cpp11::list& df,
const cpp11::sexp& connection,
char delim,
const std::string& na,
bool col_names,
bool bom,
int quote_escape,
const char* eol) {
stream_delim(
connection,
df,
delim,
na,
col_names,
bom,
static_cast<quote_escape_t>(quote_escape),
eol);
}
// =============================================================================
// Derived from EncodeElementS in RPostgreSQL
// Written by: tomoakin@kenroku.kanazawa-u.ac.jp
// License: GPL-2
void stream_delim(
const cpp11::sexp& connection,
const cpp11::sexp& x,
int i,
char delim,
const std::string& na,
quote_escape_t escape) {
switch (TYPEOF(x)) {
case LGLSXP: {
int value = LOGICAL(x)[i];
if (value == TRUE) {
write_bytes(connection, "TRUE", 4);
} else if (value == FALSE) {
write_bytes(connection, "FALSE", 5);
} else {
write_bytes(connection, na.c_str(), na.size());
}
break;
}
case INTSXP: {
int value = INTEGER(x)[i];
if (value == NA_INTEGER) {
write_bytes(connection, na.c_str(), na.size());
} else {
std::array<char, 32> str;
int len = snprintf(str.data(), 32, "%i", value);
if (len > 32) {
cpp11::stop("integer too big");
}
write_bytes(connection, str.data(), len);
}
break;
}
case REALSXP: {
double value = REAL(x)[i];
if (!R_FINITE(value)) {
if (ISNA(value) || ISNAN(value)) {
write_bytes(connection, na.c_str(), na.size());
} else if (value > 0) {
write_bytes(connection, "Inf", 3);
} else {
write_bytes(connection, "-Inf", 4);
}
} else {
std::array<char, 32> str;
int len = dtoa_grisu3(value, str.data());
write_bytes(connection, str.data(), len);
}
break;
}
case STRSXP: {
if (STRING_ELT(x, i) == NA_STRING) {
write_bytes(connection, na.c_str(), na.size());
} else {
stream_delim(
connection,
Rf_translateCharUTF8(STRING_ELT(x, i)),
delim,
na,
escape);
}
break;
}
default:
cpp11::stop(
"Don't know how to handle vector of type %s.", Rf_type2char(TYPEOF(x)));
}
}
|