-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathopenxlsx2_types.h
131 lines (115 loc) · 3.9 KB
/
openxlsx2_types.h
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
/******************************************************************************
* *
* This file defines typedefs. R expects it to be called <pkgname>_types.h *
* *
******************************************************************************/
/* create custom Rcpp::wrap function to be used with std::vector<xml_col> */
#include <RcppCommon.h>
const std::string openxlsxNA = "_openxlsx_NA_";
typedef struct {
std::string row_r;
std::string c_r; // CellReference
std::string c_s; // StyleIndex
std::string c_t; // DataType
std::string c_cm; // CellMetaIndex
std::string c_ph; // ShowPhonetic
std::string c_vm; // ValueMetaIndex
std::string v; // CellValue
std::string f; // CellFormula
std::string f_t;
std::string f_ref;
std::string f_ca;
std::string f_si;
std::string is; // inlineStr
} xml_col;
typedef struct {
std::string v;
std::string c_s;
std::string c_t;
std::string is;
std::string f;
std::string f_t;
std::string f_ref;
std::string typ;
std::string r;
} celltyp;
// matches openxlsx2_celltype in openxlsx2.R
enum celltype {
short_date = 0,
long_date = 1,
numeric = 2,
logical = 3,
character = 4,
formula = 5,
accounting = 6,
percentage = 7,
scientific = 8,
comma = 9,
hyperlink = 10,
array_formula = 11,
factor = 12
};
#include <Rcpp.h>
// custom wrap function
// Converts the imported values from c++ std::vector<xml_col> to an R dataframe.
// Whenever new fields are spotted they have to be added here
namespace Rcpp {
template <>
inline SEXP wrap(const std::vector<xml_col> &x) {
size_t n = x.size();
// Vector structure identical to xml_col from openxlsx2_types.h
Rcpp::CharacterVector row_r(no_init(n)); // row name: 1, 2, ..., 9999
Rcpp::CharacterVector c_r(no_init(n)); // col name: A, B, ..., ZZ
Rcpp::CharacterVector c_s(no_init(n)); // cell style
Rcpp::CharacterVector c_t(no_init(n)); // cell type
Rcpp::CharacterVector c_cm(no_init(n));
Rcpp::CharacterVector c_ph(no_init(n));
Rcpp::CharacterVector c_vm(no_init(n));
Rcpp::CharacterVector v(no_init(n)); // <v> tag
Rcpp::CharacterVector f(no_init(n)); // <f> tag
Rcpp::CharacterVector f_t(no_init(n)); // <f t=""> attribute most likely shared
Rcpp::CharacterVector f_ref(no_init(n)); // <f ref=""> attribute most likely reference
Rcpp::CharacterVector f_ca(no_init(n)); // <f ca=""> attribute most likely conditional formatting
Rcpp::CharacterVector f_si(no_init(n)); // <f si=""> attribute most likely sharedString
Rcpp::CharacterVector is(no_init(n)); // <is> tag
// struct to vector
for (size_t i = 0; i < n; ++i) {
row_r[i] = x[i].row_r;
c_r[i] = x[i].c_r;
c_s[i] = x[i].c_s;
c_t[i] = x[i].c_t;
c_cm[i] = x[i].c_cm;
c_ph[i] = x[i].c_ph;
c_vm[i] = x[i].c_vm;
v[i] = x[i].v;
f[i] = x[i].f;
f_t[i] = x[i].f_t;
f_ref[i] = x[i].f_ref;
f_ca[i] = x[i].f_ca;
f_si[i] = x[i].f_si;
is[i] = x[i].is;
}
// Assign and return a dataframe
return Rcpp::wrap(Rcpp::DataFrame::create(
Rcpp::Named("row_r") = row_r,
Rcpp::Named("c_r") = c_r,
Rcpp::Named("c_s") = c_s,
Rcpp::Named("c_t") = c_t,
Rcpp::Named("c_cm") = c_cm,
Rcpp::Named("c_ph") = c_ph,
Rcpp::Named("c_vm") = c_vm,
Rcpp::Named("v") = v,
Rcpp::Named("f") = f,
Rcpp::Named("f_t") = f_t,
Rcpp::Named("f_ref") = f_ref,
Rcpp::Named("f_ca") = f_ca,
Rcpp::Named("f_si") = f_si,
Rcpp::Named("is") = is
)
);
}
}
// pugixml defines. This creates the xmlptr
#include "pugixml.hpp"
typedef pugi::xml_document xmldoc;
typedef Rcpp::XPtr<xmldoc> XPtrXML;