From 768990564ff910461ae5b6fee4db0472039627d2 Mon Sep 17 00:00:00 2001 From: Bob Rudis Date: Mon, 15 Aug 2016 09:33:42 -0400 Subject: [PATCH] ensured dir isn't empty --- README.md | 4 +++- inst/img/example_dir/test.txt | 1 + src/wand.cpp | 46 +++++++++++++++++++++++++++++++++++-------- tests/testthat/test-wand.R | 26 ++++++++++++------------ 4 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 inst/img/example_dir/test.txt diff --git a/README.md b/README.md index dfd5033..bf0bd8f 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,8 @@ system.file("img", package="wand") %>% ## $ description "directory", "C source, ASCII text", "HTML document, ASCII text, with CRLF line terminators", "... ``` r +# Use a non-system magic-file + system.file("img", package="wand") %>% list.files(full.names=TRUE) %>% incant(magic_wand_file()) %>% @@ -79,7 +81,7 @@ library(testthat) date() ``` - ## [1] "Mon Aug 15 08:17:44 2016" + ## [1] "Mon Aug 15 09:30:22 2016" ``` r test_dir("tests/") diff --git a/inst/img/example_dir/test.txt b/inst/img/example_dir/test.txt new file mode 100644 index 0000000..ae218d0 --- /dev/null +++ b/inst/img/example_dir/test.txt @@ -0,0 +1 @@ +Test text file since the dir can't be empty and I need to test out dir functionality. \ No newline at end of file diff --git a/src/wand.cpp b/src/wand.cpp index 5514909..742b287 100644 --- a/src/wand.cpp +++ b/src/wand.cpp @@ -12,7 +12,6 @@ using namespace Rcpp; #ifndef WINDOWS #include "magic.h" -#include "limits.h" #endif #ifndef WINDOWS @@ -48,6 +47,9 @@ DataFrame incant_(CharacterVector path, std::string magic_db="system") { // bits of info. I may just switch this over to a single call (all the // availabel flags) and do string parsing before pushing to CRAN. + int flags = MAGIC_MIME_TYPE; + magic_t cookie = magic_open(flags); + for (unsigned int i=0; i(path[i]); std::string fullPath(R_ExpandFileName(path_str.c_str())); - int flags = MAGIC_MIME_TYPE; - magic_t cookie = magic_open(flags); if (cookie == NULL) { mime_type[i] = NA_STRING; } else { @@ -71,8 +71,18 @@ DataFrame incant_(CharacterVector path, std::string magic_db="system") { } } - flags = MAGIC_MIME_ENCODING; - cookie = magic_open(flags); + } + + flags = MAGIC_MIME_ENCODING; + cookie = magic_open(flags); + + for (unsigned int i=0; i(path[i]); + std::string fullPath(R_ExpandFileName(path_str.c_str())); + if (cookie == NULL) { encoding[i] = NA_STRING; } else { @@ -87,7 +97,17 @@ DataFrame incant_(CharacterVector path, std::string magic_db="system") { } } - if (version >= 528) { + } + + if (version >= 528) { + + for (unsigned int i=0; i(path[i]); + std::string fullPath(R_ExpandFileName(path_str.c_str())); + flags = MAGIC_EXTENSION; cookie = magic_open(flags); if (cookie == NULL) { @@ -105,8 +125,17 @@ DataFrame incant_(CharacterVector path, std::string magic_db="system") { } } - flags = MAGIC_NONE; - cookie = magic_open(flags); + } + + flags = MAGIC_NONE; + cookie = magic_open(flags); + for (unsigned int i=0; i(path[i]); + std::string fullPath(R_ExpandFileName(path_str.c_str())); + if (cookie == NULL) { description[i] = NA_STRING; } else { @@ -120,6 +149,7 @@ DataFrame incant_(CharacterVector path, std::string magic_db="system") { description(i) = res; } } + } DataFrame df; diff --git a/tests/testthat/test-wand.R b/tests/testthat/test-wand.R index a68d293..2749fed 100644 --- a/tests/testthat/test-wand.R +++ b/tests/testthat/test-wand.R @@ -1,13 +1,13 @@ -context("basic functionality") -test_that("we can do something", { - - tmp <- incant(list.files(system.file("img", package="wand"), - full.names=TRUE), - magic_wand_file()) - tmp <- tmp$description - tmp <- unlist(tmp, use.names=FALSE) - tmp <- sort(tmp) - - expect_that(substr(tmp[8], 1, 3), equals("PNG")) - -}) +context("basic functionality") +test_that("we can do something", { + + tmp <- incant(list.files(system.file("img", package="wand"), + full.names=TRUE), + magic_wand_file()) + tmp <- tmp$description + tmp <- unlist(tmp, use.names=FALSE) + tmp <- sort(tmp) + + expect_that(any(substr(tmp, 1, 3) == "PNG"), equals(TRUE)) + +})