Skip to content

Commit

Permalink
Remove much of what we don't need
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Jul 16, 2023
1 parent f4cdcea commit 4959ec2
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 190 deletions.
78 changes: 0 additions & 78 deletions configure

This file was deleted.

35 changes: 16 additions & 19 deletions src/Makevars
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

#PKG_CPPFLAGS = -I. -I../inst/include/pugixml -Ixlcpp -DXLCPP_EXPORT
PKG_CXXFLAGS = -I. -Ixlcpp -I../inst/include/pugixml -DXLCPP_EXPORT

#PKG_LIBS=-Lpugixml -Lcfbf -L/usr/lib -larchive -L/usr/lib -lfmt
PKG_LIBS = -larchive -Lpugixml -Lxlcpp
PKG_LIBS = -Lpugixml -Lxlcpp

PKGROOT = ./xlcpp

Expand All @@ -15,24 +13,23 @@ OBJECTS = decrypt.o \
styles_xml.o \
write_file.o \
$(PKGROOT)/aes.o \
$(PKGROOT)/b64.o \
$(PKGROOT)/cfbf.o \
$(PKGROOT)/mmap.o \
$(PKGROOT)/sha1.o \
$(PKGROOT)/sha512.o \
$(PKGROOT)/xlcpp.o \
$(PKGROOT)/xml-reader.o \
RcppExports.o

$(PKGROOT)/b64.o \
$(PKGROOT)/cfbf.o \
$(PKGROOT)/mmap.o \
$(PKGROOT)/sha1.o \
$(PKGROOT)/sha512.o \
$(PKGROOT)/xlcpp.o \
$(PKGROOT)/xml-reader.o \
RcppExports.o

XLCPP = $(PKGROOT)/aes.cpp \
$(PKGROOT)/b64.cpp \
$(PKGROOT)/cfbf.cpp \
$(PKGROOT)/mmap.cpp \
$(PKGROOT)/sha1.cpp \
$(PKGROOT)/sha512.cpp \
$(PKGROOT)/xml-reader.cpp \
$(PKGROOT)/xlcpp.cpp
$(PKGROOT)/b64.cpp \
$(PKGROOT)/cfbf.cpp \
$(PKGROOT)/mmap.cpp \
$(PKGROOT)/sha1.cpp \
$(PKGROOT)/sha512.cpp \
$(PKGROOT)/xml-reader.cpp \
$(PKGROOT)/xlcpp.cpp

PUGIXML = ../inst/include/pugixml/pugixml.cpp

Expand Down
39 changes: 0 additions & 39 deletions src/Makevars.in

This file was deleted.

86 changes: 44 additions & 42 deletions src/xlcpp/xlcpp-pimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include <optional>
#include <stack>
#include <span>
#include <archive.h>
#include <archive_entry.h>
// #include <archive.h>
// #include <archive_entry.h>

namespace xlcpp {

Expand All @@ -33,8 +33,8 @@ class workbook_pimpl {
workbook_pimpl(std::span<const uint8_t> sv, std::string_view password, std::string_view outfile);
std::string data() const;

void write_archive(struct archive* a) const;
la_ssize_t write_callback(struct archive* a, const void* buffer, size_t length) const;
// void write_archive(struct archive* a) const;
// la_ssize_t write_callback(struct archive* a, const void* buffer, size_t length) const;
void load_archive(struct archive* a);

#ifdef _WIN32
Expand All @@ -53,32 +53,34 @@ class workbook_pimpl {
void load_from_memory(std::span<const uint8_t> sv, std::string_view password, std::string_view outfile);
};

}; // end namespace xlcpp


enum class xml_node {
unknown,
text,
whitespace,
element,
end_element,
processing_instruction,
comment,
cdata
unknown,
text,
whitespace,
element,
end_element,
processing_instruction,
comment,
cdata
};


class xml_enc_string_view {
public:
xml_enc_string_view() { }
xml_enc_string_view(std::string_view sv) : sv(sv) { }
xml_enc_string_view() { }
xml_enc_string_view(std::string_view sv) : sv(sv) { }

bool empty() const noexcept {
return sv.empty();
}
bool empty() const noexcept {
return sv.empty();
}

std::string decode() const;
bool cmp(std::string_view str) const;
std::string decode() const;
bool cmp(std::string_view str) const;

private:
std::string_view sv;
std::string_view sv;
};

using ns_list = std::vector<std::pair<std::string_view, xml_enc_string_view>>;
Expand All @@ -104,24 +106,24 @@ class xml_reader {
std::vector<ns_list> namespaces;
};

class archive_read_closer {
public:
typedef archive* pointer;

void operator()(archive* a) {
archive_read_free(a);
}
};

using archive_read_t = std::unique_ptr<archive*, archive_read_closer>;

class archive_write_closer {
public:
typedef archive* pointer;

void operator()(archive* a) {
archive_write_free(a);
}
};

using archive_write_t = std::unique_ptr<archive*, archive_write_closer>;
// class archive_read_closer {
// public:
// typedef archive* pointer;
//
// void operator()(archive* a) {
// archive_read_free(a);
// }
// };
//
// using archive_read_t = std::unique_ptr<archive*, archive_read_closer>;
//
// class archive_write_closer {
// public:
// typedef archive* pointer;
//
// void operator()(archive* a) {
// archive_write_free(a);
// }
// };
//
// using archive_write_t = std::unique_ptr<archive*, archive_write_closer>;
22 changes: 10 additions & 12 deletions src/xlcpp/xlcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
#include "mmap.h"
#include "cfbf.h"
#include "utf16.h"
#include <archive.h>
#include <archive_entry.h>
#include <vector>
#include <array>
#include <charconv>

#ifdef _WIN32
#include <windows.h>
#endif
// #ifdef _WIN32
// #include <windows.h>
// #endif

// #define FMT_HEADER_ONLY
// #include <fmt/format.h>
Expand Down Expand Up @@ -70,17 +68,17 @@ __inline string utf16_to_utf8(const u16string_view& s) {
#endif

workbook_pimpl::workbook_pimpl(const filesystem::path& fn, string_view password, string_view outfile) {
#ifdef _WIN32
unique_handle hup{CreateFileW((LPCWSTR)fn.u16string().c_str(), FILE_READ_DATA | DELETE, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, nullptr)};
if (hup.get() == INVALID_HANDLE_VALUE)
throw last_error("CreateFile", GetLastError());
#else
// #ifdef _WIN32
// unique_handle hup{CreateFileW((LPCWSTR)fn.u16string().c_str(), FILE_READ_DATA | DELETE, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
// FILE_ATTRIBUTE_NORMAL, nullptr)};
// if (hup.get() == INVALID_HANDLE_VALUE)
// throw last_error("CreateFile", GetLastError());
// #else
unique_handle hup{open(fn.string().c_str(), O_RDONLY)};

if (hup.get() == -1)
Rcpp::stop("open failed (errno = {})", errno);
#endif
// #endif

mmap m(hup.get());

Expand Down
60 changes: 60 additions & 0 deletions src/xlcpp/xml-reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,63 @@ static string esc_char(string_view s) {
return string{string_view(t, 4)};
}
}

string xml_enc_string_view::decode() const {
auto v = sv;
string s;

s.reserve(v.length());

while (!v.empty()) {
if (v.front() == '&') {
v.remove_prefix(1);

if (v.starts_with("amp;")) {
s += "&";
v.remove_prefix(4);
} else if (v.starts_with("lt;")) {
s += "<";
v.remove_prefix(3);
} else if (v.starts_with("gt;")) {
s += ">";
v.remove_prefix(3);
} else if (v.starts_with("quot;")) {
s += "\"";
v.remove_prefix(5);
} else if (v.starts_with("apos;")) {
s += "'";
v.remove_prefix(5);
} else if (v.starts_with("#")) {
string_view bit;

v.remove_prefix(1);

auto sc = v.find_first_of(';');
if (sc == string::npos) {
bit = v;
v = "";
} else {
bit = v.substr(0, sc);
v.remove_prefix(sc + 1);
}

s += esc_char(bit);
} else
s += "&";
} else {
s += v.front();
v.remove_prefix(1);
}
}

return s;
}

bool xml_enc_string_view::cmp(string_view str) const {
for (auto c : sv) {
if (c == '&')
return decode() == str;
}

return sv == str;
}

0 comments on commit 4959ec2

Please sign in to comment.