-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathread_workbook.cpp
750 lines (525 loc) · 18.3 KB
/
read_workbook.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
#include "openxlsx.h"
IntegerVector which_cpp(Rcpp::LogicalVector x) {
IntegerVector v = seq(0, x.size() - 1);
return v[x];
}
// [[Rcpp::export]]
CharacterVector get_shared_strings(std::string xmlFile, bool isFile){
CharacterVector x;
size_t pos = 0;
std::string line;
std::vector<std::string> lines;
if(isFile){
// READ IN FILE
ifstream file;
file.open(xmlFile.c_str());
while ( std::getline(file, line) )
{
// skip empty lines:
if (line.empty())
continue;
lines.push_back(line);
}
line = "";
int n = lines.size();
for(int i = 0;i < n; ++i)
line += lines[i] + "\n";
}else{
line = xmlFile;
}
x = getNodes(line, "<si>");
// define variables for sharedString part
int n = x.size();
CharacterVector strs(n);
std::fill(strs.begin(), strs.end(), NA_STRING);
std::string xml;
size_t endPos = 0;
std::string ttag = "<t";
std::string tag = ">";
std::string tagEnd = "<";
// Check for rPh and remove if found
std::string rPh_tag = "<rPh";
std::string rPh_tag_end = "rPh>";
for(int i = 0; i < n; i++){
xml = x[i]; // find opening tag
pos = xml.find(rPh_tag, 0); // find ttag
if(pos != std::string::npos){
if(xml[pos+2] != '/'){
endPos = xml.find(rPh_tag_end, pos+2);
xml.erase(pos, endPos - pos + 4);
x[i] = xml;
}
}
}
// Now check for inline formatting
pos = line.find("<rPr>", 0);
if(pos == std::string::npos){
// NO INLINE FORMATTING
for(int i = 0; i < n; i++){
// find opening tag
xml = x[i];
pos = xml.find(ttag, 0); // find ttag
if(pos != std::string::npos){
if(xml[pos+2] != '/'){
pos = xml.find(tag, pos+1); // find where opening ttag ends
endPos = xml.find(tagEnd, pos+1); // find where the node ends </t> (closing tag)
strs[i] = xml.substr(pos+1, endPos-pos - 1).c_str();
}
}
}
}else{ // we have inline formatting
for(int i = 0; i < n; i++){
// find opening tag
xml = x[i];
pos = xml.find(ttag, 0); // find ttag
if(xml[pos+2] != '/'){
strs[i] = "";
while(1){
if(xml[pos+2] == '/'){
break;
}else{
pos = xml.find(tag, pos+1); // find where opening ttag ends
endPos = xml.find(tagEnd, pos+1); // find where the node ends </t> (closing tag)
strs[i] += xml.substr(pos+1, endPos-pos - 1).c_str();
pos = xml.find(ttag, endPos); // find ttag
if(pos == std::string::npos)
break;
}
}
}
} // end of for loop
} // end of else inline formatting
return wrap(strs);
}
// [[Rcpp::export]]
List getCellInfo(std::string xmlFile,
CharacterVector sharedStrings,
bool skipEmptyRows,
int startRow,
IntegerVector rows,
bool getDates){
//read in file
std::string buf;
std::string xml = read_file_newline(xmlFile);
std::string xml2 = "";
std::string rtag = "r=";
std::string ttag = " t=";
std::string stag = " s=";
std::string tagEnd = "\"";
std::string vtag = "<v>";
std::string vtag2 = "<v ";
std::string vtagEnd = "</v>";
std::string cell;
List res(6);
std::size_t pos = xml.find("<sheetData>"); // find sheetData
size_t endPos = 0;
// If no data
if(pos == std::string::npos){
res = List::create(Rcpp::Named("nRows") = 0, Rcpp::Named("r") = 0);
return res;
}
xml = xml.substr(pos + 11); // get from "sheedData" to the end
xml2 = xml;
// startRow cut off
int row_i = 0;
if(startRow > 1){
//find r and check the row number
pos = xml.find("<row r=\"", 0);
while(pos != std::string::npos){
endPos = xml.find(tagEnd, pos + 8);
row_i = atoi(xml.substr(pos + 8, endPos - pos - 8).c_str());
if(row_i >= startRow){
xml = xml.substr(pos);
break;
}else{
pos = pos + 8;
}
pos = xml.find("<row r=\"", pos);
}
// no rows
if(pos == std::string::npos){
res = List::create(Rcpp::Named("nRows") = 0, Rcpp::Named("r") = 0);
return res;
}
}
// getting rows
//rows cut off, loop over entire string and only take rows specified in rows vector
if(!is_na(rows)[0]){
CharacterVector xml_rows = getNodes(xml, "<row");
int nr = xml_rows.size();
// no rows
if(nr == 0){
res = List::create(Rcpp::Named("nRows") = 0, Rcpp::Named("r") = 0);
return res;
}
// for each row pull out the row number, check the row number against 'rows'
std::string row_xml_i;
xml = "";
int place = 0;
// get first one and remove beginning of rows
row_xml_i = xml_rows[0];
pos = row_xml_i.find("<row r=\"", 0);
endPos = row_xml_i.find(tagEnd, pos + 8);
row_i = atoi(row_xml_i.substr(pos + 8, endPos - pos - 8).c_str());
rows = rows[rows >= row_i];
int nr_sub = rows.size();
if(nr_sub > 0){
for(int i = 0; i < nr; i++){
row_xml_i = xml_rows[i];
pos = row_xml_i.find("<row r=\"", 0);
endPos = row_xml_i.find(tagEnd, pos + 8);
row_i = atoi(row_xml_i.substr(pos + 8, endPos - pos - 8).c_str());
if (std::find(rows.begin()+place, rows.end(), row_i)!= rows.end()){
xml += row_xml_i + ' ';
place++;
if(place == nr_sub)
break;
}
} // end of cut off unwanted xml
}
}
// count cells with children
int ocs = 0;
// can not use pos for start, as the xml and pos were changed
string::size_type start = 0;
// get number of nodes, start is only required for this while loop
while((start = xml.find("<c ", start)) != string::npos){
++ocs;
start += 4;
}
if(ocs == 0){
res = List::create(Rcpp::Named("nRows") = 0, Rcpp::Named("r") = 0);
return res;
}
// pull out cell merges
CharacterVector merge_cell_xml = getChildlessNode(xml2, "mergeCell");
CharacterVector s(ocs);
CharacterVector r(ocs);
CharacterVector t(ocs);
CharacterVector v(ocs);
CharacterVector string_refs(ocs);
t.fill("n");
v.fill(NA_STRING);
s.fill(NA_STRING);
string_refs.fill(NA_STRING);
int j = 0;
size_t nextPos = 3;
bool has_v = false;
bool has_f = false;
pos = xml.find("<c ", 0);
size_t pos_t = pos;
size_t pos_f = pos;
// PULL OUT CELL AND ATTRIBUTES
while(j < ocs){
if(pos != std::string::npos){
nextPos = xml.find("<c ", pos + 9);
cell = xml.substr(pos, nextPos - pos);
// Pull out ref
pos = cell.find("r=", 0); // find r="
endPos = cell.find(tagEnd, pos + 3); // find next "
r[j] = cell.substr(pos + 3, endPos - pos - 3).c_str();
buf = cell.substr(pos + 3, endPos - pos - 3);
buf.erase(std::remove_if(buf.begin(), buf.end(), ::isalpha), buf.end());
// Pull out style
pos = cell.find(" s=", 0); // find s="
if(pos != std::string::npos){
endPos = cell.find(tagEnd, pos + 4); // find next "
s[j] = cell.substr(pos + 4, endPos - pos - 4);
}
// find <v> tag and </v> end tag
endPos = cell.find("</v>", 0);
if(endPos != std::string::npos){
pos = cell.find("<v", 0);
pos = cell.find(">", pos);
v[j] = cell.substr(pos + 1, endPos - pos - 1);
has_v = true;
}
// find <is><t> tag and </t></is> end tag
endPos = cell.find("</t></is>", 0);
if(endPos != std::string::npos){
pos = cell.find("<is><t", 0);
pos = cell.find(">", pos);
v[j] = cell.substr(pos + 4, endPos - pos - 4); // skip <t> and </t
has_v = true;
}
// Pull out type
pos_t = cell.find(" t=", 0);
pos_f = cell.find("<f", 0);
// have both
if((pos_f != std::string::npos) & (pos_t != std::string::npos)){ // have f
// will always have f
// endPos = cell.find("</f>", pos_f + 3);
// if(endPos == std::string::npos){
// endPos = cell.find("/>", pos_f + 3);
// f[j] = cell.substr(pos_f, endPos - pos_f + 2);
// }else{
// f[j] = cell.substr(pos_f, endPos - pos_f + 4);
// }
has_f = true;
// do we really have t
if(pos_t < pos_f){
endPos = cell.find(tagEnd, pos_t + 4); // find next "
t[j] = cell.substr(pos_t + 4, endPos - pos_t - 4);
}
}else if(pos_t != std::string::npos){ // only have t
endPos = cell.find(tagEnd, pos_t + 4); // find next "
t[j] = cell.substr(pos_t + 4, endPos - pos_t - 4);
}else if(pos_f != std::string::npos){ // only have f
// endPos = cell.find("</f>", pos_f + 3);
// if(endPos == std::string::npos){
// endPos = cell.find("/>", pos_f + 3);
// f[j] = cell.substr(pos_f, endPos - pos_f + 2);
// }else{
// f[j] = cell.substr(pos_f, endPos - pos_f + 4);
// }
has_f = true;
}
/* since we return only a data frame, we do the preparation here */
if(t[j] == "s"){
auto ss_ind = atoi(v[j]);
v[j] = sharedStrings[ss_ind];
if(v[j] == "openxlsx_na_vlu"){
v[j] = NA_STRING;
}
string_refs[j] = r[j];
}else if(t[j] == "e") {
v[j] = NA_STRING; // exception from loadWorkbook
}else if(t[j] == "b"){
if(v[j] == "1"){
v[j] = "TRUE";
}else{
v[j] = "FALSE";
}
string_refs[j] = r[j];
}else if((t[j] == "str") || (t[j] == "inlineStr")){
string_refs[j] = r[j];
}
/* preparation is finished */
if(has_f & (!has_v) & (t[j] != "n")){
v[j] = NA_STRING;
}else if(has_f & !has_v){
t[j] = NA_STRING;
v[j] = NA_STRING;
}else if(has_f | has_v){
}else{ //only have s and r
t[j] = NA_STRING;
v[j] = NA_STRING;
}
j++; // INCREMENT OVER OCCURENCES
pos = nextPos;
pos_t = nextPos;
pos_f = nextPos;
} // end of while loop over occurences
} // END OF CELL AND ATTRIBUTION GATHERING
string_refs = string_refs[!is_na(string_refs)];
int nRows = calc_number_rows(r, skipEmptyRows);
res = List::create(Rcpp::Named("r") = r,
Rcpp::Named("string_refs") = string_refs,
Rcpp::Named("v") = v,
Rcpp::Named("s") = s,
Rcpp::Named("nRows") = nRows,
Rcpp::Named("cellMerge") = merge_cell_xml
);
return wrap(res);
}
// [[Rcpp::export]]
SEXP read_workbook(IntegerVector cols_in,
IntegerVector rows_in,
CharacterVector v,
IntegerVector string_inds,
LogicalVector is_date,
bool hasColNames,
char hasSepNames,
bool skipEmptyRows,
bool skipEmptyCols,
int nRows,
Function clean_names
){
IntegerVector cols = clone(cols_in);
IntegerVector rows = clone(rows_in);
int nCells = rows.size();
int nDates = is_date.size();
/* do we have any dates */
bool has_date;
if(nDates == 1){
if(is_true(any(is_na(is_date)))){
has_date = false;
}else{
has_date = true;
}
}else if(nDates == nCells){
has_date = true;
}else{
has_date = false;
}
bool has_strings = true;
IntegerVector st_inds0 (1);
if (string_inds.size())
st_inds0[0] = string_inds[0];
if(is_true(all(is_na(st_inds0))))
has_strings = false;
IntegerVector uni_cols = sort_unique(cols);
if(!skipEmptyCols){ // want to keep all columns - just create a sequence from 1:max(cols)
uni_cols = seq(1, max(uni_cols));
cols = cols - 1;
}else{
cols = match(cols, uni_cols) - 1;
}
// scale columns from i:j to 1:(j-i+1)
int nCols = *std::max_element(cols.begin(), cols.end()) + 1;
// scale rows from i:j to 1:(j-i+1)
IntegerVector uni_rows = sort_unique(rows);
if(skipEmptyRows){
rows = match(rows, uni_rows) - 1;
//int nRows = *std::max_element(rows.begin(), rows.end()) + 1;
}else{
rows = rows - rows[0];
}
// Check if first row are all strings
//get first row number
CharacterVector col_names(nCols);
IntegerVector removeFlag;
int pos = 0;
// If we are told col_names exist take the first row and fill any gaps with X.i
if(hasColNames){
int row_1 = rows[0];
IntegerVector row1_inds = which_cpp(rows == row_1);
IntegerVector header_cols = cols[row1_inds];
IntegerVector header_inds = match(seq(0, nCols), na_omit(header_cols));
LogicalVector missing_header = is_na(header_inds);
// looping over each column
for(unsigned short i=0; i < nCols; i++){
std::string nm = "X" + std::to_string(i + 1);
if(missing_header[i]){ // a missing header element
col_names[i] = nm.c_str();
}else{ // this is a header elements
col_names[i] = v[pos];
if(col_names[i] == "NA"){
col_names[i] = nm.c_str();
}
pos++;
}
}
// tidy up column names
col_names = clean_names(col_names,hasSepNames);
//--------------------------------------------------------------------------------
// Remove elements from rows, cols, v that have been used as headers
// I've used the first pos elements as headers
// stringInds contains the indexes of v which are strings
// string_inds <- string_inds[string_inds > pos]
if(has_strings){
string_inds = string_inds[string_inds > pos];
string_inds = string_inds - pos;
}
rows.erase (rows.begin(), rows.begin() + pos);
rows = rows - 1;
v.erase (v.begin(), v.begin() + pos);
//If nothing left return a data.frame with 0 rows
if(rows.size() == 0){
List dfList(nCols);
IntegerVector rowNames(0);
for(int i = 0; i < nCols; i++){
dfList[i] = LogicalVector(0); // this is what read.table does (bool type)
}
dfList.attr("names") = col_names;
dfList.attr("row.names") = rowNames;
dfList.attr("class") = "data.frame";
return wrap(dfList);
}
cols.erase(cols.begin(), cols.begin() + pos);
nRows--; // decrement number of rows as first row is now being used as col_names
nCells = nCells - pos;
// End Remove elements from rows, cols, v that have been used as headers
//--------------------------------------------------------------------------------
}else{ // else col_names is FALSE
for(unsigned short i =0; i < nCols; i++){
std::string nm = "X" + std::to_string(i + 1);
col_names[i] = nm.c_str();
}
}
// ------------------ column names complete
// Possible there are no string_inds to begin with and value of string_inds is 0
// Possible we have string_inds but they have now all been used up by headers
bool allNumeric = false;
if((string_inds.size() == 0) || is_true(all(is_na(string_inds))))
allNumeric = true;
if(has_date){
if(is_true(any(is_date)))
allNumeric = false;
}
// If we have colnames some elements were used to create these -so we remove the corresponding number of elements
if(hasColNames && has_date)
is_date.erase(is_date.begin(), is_date.begin() + pos);
//Intialise return data.frame
SEXP m;
// for(int i = 0; i < rows.size(); i++)
// Rcout << "rows[i]: " << rows[i] << endl;
//
// Rcout << "nRows " << nRows << endl;
// Rcout << "nCols: " << nCols << endl;
// Rcout << "cols.size(): " << cols.size() << endl;
// Rcout << "rows.size(): " << rows.size() << endl;
// Rcout << "is_date.size(): " << is_date.size() << endl;
// Rcout << "v.size(): " << v.size() << endl;
// Rcout << "has_date: " << has_date << endl;
if(allNumeric){
m = buildMatrixNumeric(v, rows, cols, col_names, nRows, nCols);
}else{
// If it contains any strings it will be a character column
IntegerVector char_cols_unique;
if(all(is_na(string_inds))){
char_cols_unique = -1;
}else{
IntegerVector columns_which_are_characters = cols[string_inds - 1];
char_cols_unique = unique(columns_which_are_characters);
}
//date columns
IntegerVector date_columns(1);
if(has_date){
date_columns = cols[is_date];
date_columns = sort_unique(date_columns);
}else{
date_columns[0] = -1;
}
// List d(10);
// d[0] = v;
// d[2] = rows;
// d[3] = cols;
// d[4] = col_names;
// d[5] = nRows;
// d[6] = nCols;
// d[7] = char_cols_unique;
// d[8] = date_columns;
// return(wrap(d));
// Rcout << "Running buildMatrixMixed" << endl;
m = buildMatrixMixed(v, rows, cols, col_names, nRows, nCols, char_cols_unique, date_columns);
}
return wrap(m) ;
}
// [[Rcpp::export]]
int calc_number_rows(CharacterVector x, bool skipEmptyRows){
int n = x.size();
if(n == 0)
return(0);
int nRows;
if(skipEmptyRows){
CharacterVector res(n);
std::string r;
for(int i = 0; i < n; i++){
r = x[i];
r.erase(std::remove_if(r.begin(), r.end(), ::isalpha), r.end());
res[i] = r;
}
CharacterVector uRes = unique(res);
nRows = uRes.size();
}else{
std::string fRef = as<std::string>(x[0]);
std::string lRef = as<std::string>(x[n-1]);
fRef.erase(std::remove_if(fRef.begin(), fRef.end(), ::isalpha), fRef.end());
lRef.erase(std::remove_if(lRef.begin(), lRef.end(), ::isalpha), lRef.end());
int firstRow = atoi(fRef.c_str());
int lastRow = atoi(lRef.c_str());
nRows = lastRow - firstRow + 1;
}
return(nRows);
}