Skip to content

Commit

Permalink
Start making into class to ensure destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Mar 16, 2015
1 parent 6def875 commit fc076c4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/h-xls.cpp → src/XlsWorkBook.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <Rcpp.h>
using namespace Rcpp;

#include "XlsWorkBook.h"
#include <libxls/xls.h>

typedef std::map<int,std::string> formatMap;
Expand Down
29 changes: 29 additions & 0 deletions src/XlsWorkBook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <Rcpp.h>
#include <libxls/xls.h>

inline std::string normalizePath(std::string path) {
Rcpp::Environment baseEnv = Rcpp::Environment::base_env();
Rcpp::Function normalizePath = baseEnv["normalizePath"];
return Rcpp::as<std::string>(normalizePath(path, false));
}

class XlsWorkBook {
std::string path_;
xls::xlsWorkBook* pWB_;

public:

XlsWorkBook(std::string path) {
path_ = normalizePath(path);
pWB_ = xls::xls_open(path.c_str(), "UTF8");

if (pWB_ == NULL)
Rcpp::stop("Failed to open %s", path);
}

~XlsWorkBook() {
try {
xls_close(pWB_);
} catch(...) {}
}
};

0 comments on commit fc076c4

Please sign in to comment.